2020#ifndef LLVM_ADT_INDEXEDMAP_H
2121#define LLVM_ADT_INDEXEDMAP_H
2222
23- #include " llvm/ADT/STLForwardCompat .h"
23+ #include " llvm/ADT/STLExtras .h"
2424#include " llvm/ADT/SmallVector.h"
2525#include < cassert>
2626
2727namespace llvm {
2828
29- template <typename T, typename ToIndexT = llvm::identity_cxx20>
29+ namespace detail {
30+ template <class Ty > struct IdentityIndex {
31+ using is_transparent = void ;
32+ using argument_type = Ty;
33+
34+ Ty &operator ()(Ty &self) const { return self; }
35+ const Ty &operator ()(const Ty &self) const { return self; }
36+ };
37+ } // namespace detail
38+
39+ template <typename T, typename ToIndexT = detail::IdentityIndex<unsigned >>
3040class IndexedMap {
41+ using IndexT = typename ToIndexT::argument_type;
3142 // Prefer SmallVector with zero inline storage over std::vector. IndexedMaps
3243 // can grow very large and SmallVector grows more efficiently as long as T
3344 // is trivially copyable.
@@ -42,17 +53,14 @@ class IndexedMap {
4253
4354 explicit IndexedMap (const T &val) : nullVal_(val) {}
4455
45- template <typename IndexT> typename StorageT::reference operator [](IndexT n) {
46- unsigned Index = toIndex_ (n);
47- assert (Index < storage_.size () && " index out of bounds!" );
48- return storage_[Index];
56+ typename StorageT::reference operator [](IndexT n) {
57+ assert (toIndex_ (n) < storage_.size () && " index out of bounds!" );
58+ return storage_[toIndex_ (n)];
4959 }
5060
51- template <typename IndexT>
5261 typename StorageT::const_reference operator [](IndexT n) const {
53- unsigned Index = toIndex_ (n);
54- assert (Index < storage_.size () && " index out of bounds!" );
55- return storage_[Index];
62+ assert (toIndex_ (n) < storage_.size () && " index out of bounds!" );
63+ return storage_[toIndex_ (n)];
5664 }
5765
5866 void reserve (typename StorageT::size_type s) { storage_.reserve (s); }
@@ -61,16 +69,13 @@ class IndexedMap {
6169
6270 void clear () { storage_.clear (); }
6371
64- template < typename IndexT> void grow (IndexT n) {
72+ void grow (IndexT n) {
6573 unsigned NewSize = toIndex_ (n) + 1 ;
6674 if (NewSize > storage_.size ())
6775 resize (NewSize);
6876 }
6977
70- template <typename IndexT> bool inBounds (IndexT n) const {
71- unsigned Index = toIndex_ (n);
72- return Index < storage_.size ();
73- }
78+ bool inBounds (IndexT n) const { return toIndex_ (n) < storage_.size (); }
7479
7580 typename StorageT::size_type size () const { return storage_.size (); }
7681};
0 commit comments