LLVM 22.0.0git
IndexedMap.h
Go to the documentation of this file.
1//===- llvm/ADT/IndexedMap.h - An index map implementation ------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8///
9/// \file
10/// This file implements an indexed map. The index map template takes two
11/// types. The first is the mapped type and the second is a functor
12/// that maps its argument to a size_t. On instantiation a "null" value
13/// can be provided to be used as a "does not exist" indicator in the
14/// map. A member function grow() is provided that given the value of
15/// the maximally indexed key (the argument of the functor) makes sure
16/// the map has enough space for it.
17///
18//===----------------------------------------------------------------------===//
19
20#ifndef LLVM_ADT_INDEXEDMAP_H
21#define LLVM_ADT_INDEXEDMAP_H
22
23#include "llvm/ADT/STLExtras.h"
25#include "llvm/ADT/identity.h"
26#include <cassert>
27
28namespace llvm {
29
30template <typename T, typename ToIndexT = identity<unsigned>> class IndexedMap {
31 using IndexT = typename ToIndexT::argument_type;
32 // Prefer SmallVector with zero inline storage over std::vector. IndexedMaps
33 // can grow very large and SmallVector grows more efficiently as long as T
34 // is trivially copyable.
35 using StorageT = SmallVector<T, 0>;
36
37 StorageT storage_;
38 T nullVal_;
39 ToIndexT toIndex_;
40
41public:
42 IndexedMap() : nullVal_(T()) {}
43
44 explicit IndexedMap(const T &val) : nullVal_(val) {}
45
46 typename StorageT::reference operator[](IndexT n) {
47 assert(toIndex_(n) < storage_.size() && "index out of bounds!");
48 return storage_[toIndex_(n)];
49 }
50
51 typename StorageT::const_reference operator[](IndexT n) const {
52 assert(toIndex_(n) < storage_.size() && "index out of bounds!");
53 return storage_[toIndex_(n)];
54 }
55
56 void reserve(typename StorageT::size_type s) { storage_.reserve(s); }
57
58 void resize(typename StorageT::size_type s) { storage_.resize(s, nullVal_); }
59
60 void clear() { storage_.clear(); }
61
62 void grow(IndexT n) {
63 unsigned NewSize = toIndex_(n) + 1;
64 if (NewSize > storage_.size())
65 resize(NewSize);
66 }
67
68 bool inBounds(IndexT n) const { return toIndex_(n) < storage_.size(); }
69
70 typename StorageT::size_type size() const { return storage_.size(); }
71};
72
73} // namespace llvm
74
75#endif // LLVM_ADT_INDEXEDMAP_H
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
#define T
This file contains some templates that are useful if you are working with the STL at all.
This file defines the SmallVector class.
StorageT::size_type size() const
Definition IndexedMap.h:70
void resize(typename StorageT::size_type s)
Definition IndexedMap.h:58
void grow(IndexT n)
Definition IndexedMap.h:62
StorageT::reference operator[](IndexT n)
Definition IndexedMap.h:46
void reserve(typename StorageT::size_type s)
Definition IndexedMap.h:56
StorageT::const_reference operator[](IndexT n) const
Definition IndexedMap.h:51
IndexedMap(const T &val)
Definition IndexedMap.h:44
bool inBounds(IndexT n) const
Definition IndexedMap.h:68
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
This is an optimization pass for GlobalISel generic memory operations.