|
20 | 20 | #ifndef LLVM_ADT_INDEXEDMAP_H |
21 | 21 | #define LLVM_ADT_INDEXEDMAP_H |
22 | 22 |
|
23 | | -#include "llvm/ADT/SmallVector.h" |
24 | 23 | #include "llvm/ADT/STLExtras.h" |
| 24 | +#include "llvm/ADT/SmallVector.h" |
25 | 25 | #include "llvm/ADT/identity.h" |
26 | 26 | #include <cassert> |
27 | 27 |
|
28 | 28 | namespace llvm { |
29 | 29 |
|
30 | | -template <typename T, typename ToIndexT = identity<unsigned>> |
31 | | - class IndexedMap { |
32 | | - using IndexT = typename ToIndexT::argument_type; |
33 | | - // Prefer SmallVector with zero inline storage over std::vector. IndexedMaps |
34 | | - // can grow very large and SmallVector grows more efficiently as long as T |
35 | | - // is trivially copyable. |
36 | | - using StorageT = SmallVector<T, 0>; |
37 | | - |
38 | | - StorageT storage_; |
39 | | - T nullVal_; |
40 | | - ToIndexT toIndex_; |
41 | | - |
42 | | - public: |
43 | | - IndexedMap() : nullVal_(T()) {} |
44 | | - |
45 | | - explicit IndexedMap(const T& val) : nullVal_(val) {} |
46 | | - |
47 | | - typename StorageT::reference operator[](IndexT n) { |
48 | | - assert(toIndex_(n) < storage_.size() && "index out of bounds!"); |
49 | | - return storage_[toIndex_(n)]; |
50 | | - } |
51 | | - |
52 | | - typename StorageT::const_reference operator[](IndexT n) const { |
53 | | - assert(toIndex_(n) < storage_.size() && "index out of bounds!"); |
54 | | - return storage_[toIndex_(n)]; |
55 | | - } |
56 | | - |
57 | | - void reserve(typename StorageT::size_type s) { |
58 | | - storage_.reserve(s); |
59 | | - } |
60 | | - |
61 | | - void resize(typename StorageT::size_type s) { |
62 | | - storage_.resize(s, nullVal_); |
63 | | - } |
64 | | - |
65 | | - void clear() { |
66 | | - storage_.clear(); |
67 | | - } |
68 | | - |
69 | | - void grow(IndexT n) { |
70 | | - unsigned NewSize = toIndex_(n) + 1; |
71 | | - if (NewSize > storage_.size()) |
72 | | - resize(NewSize); |
73 | | - } |
74 | | - |
75 | | - bool inBounds(IndexT n) const { |
76 | | - return toIndex_(n) < storage_.size(); |
77 | | - } |
78 | | - |
79 | | - typename StorageT::size_type size() const { |
80 | | - return storage_.size(); |
81 | | - } |
82 | | - }; |
83 | | - |
84 | | -} // end namespace llvm |
| 30 | +template <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 | + |
| 41 | +public: |
| 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 |
85 | 74 |
|
86 | 75 | #endif // LLVM_ADT_INDEXEDMAP_H |
0 commit comments