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 <cassert>
26
27namespace llvm {
28
29namespace detail {
30template <class Ty> struct IdentityIndex {
31 using argument_type = Ty;
32
33 Ty &operator()(Ty &self) const { return self; }
34 const Ty &operator()(const Ty &self) const { return self; }
35};
36} // namespace detail
37
38template <typename T, typename ToIndexT = detail::IdentityIndex<unsigned>>
40 using IndexT = typename ToIndexT::argument_type;
41 // Prefer SmallVector with zero inline storage over std::vector. IndexedMaps
42 // can grow very large and SmallVector grows more efficiently as long as T
43 // is trivially copyable.
44 using StorageT = SmallVector<T, 0>;
45
46 StorageT Storage;
47 T NullVal = T();
48 ToIndexT ToIndex;
49
50public:
51 IndexedMap() = default;
52
53 explicit IndexedMap(const T &Val) : NullVal(Val) {}
54
55 typename StorageT::reference operator[](IndexT N) {
56 assert(ToIndex(N) < Storage.size() && "index out of bounds!");
57 return Storage[ToIndex(N)];
58 }
59
60 typename StorageT::const_reference operator[](IndexT N) const {
61 assert(ToIndex(N) < Storage.size() && "index out of bounds!");
62 return Storage[ToIndex(N)];
63 }
64
65 void reserve(typename StorageT::size_type S) { Storage.reserve(S); }
66
67 void resize(typename StorageT::size_type S) { Storage.resize(S, NullVal); }
68
69 void clear() { Storage.clear(); }
70
71 void grow(IndexT N) {
72 unsigned NewSize = ToIndex(N) + 1;
73 if (NewSize > Storage.size())
74 resize(NewSize);
75 }
76
77 bool inBounds(IndexT N) const { return ToIndex(N) < Storage.size(); }
78
79 typename StorageT::size_type size() const { return Storage.size(); }
80};
81
82} // namespace llvm
83
84#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:79
StorageT::const_reference operator[](IndexT N) const
Definition IndexedMap.h:60
void grow(IndexT N)
Definition IndexedMap.h:71
void resize(typename StorageT::size_type S)
Definition IndexedMap.h:67
IndexedMap()=default
void reserve(typename StorageT::size_type S)
Definition IndexedMap.h:65
StorageT::reference operator[](IndexT N)
Definition IndexedMap.h:55
IndexedMap(const T &Val)
Definition IndexedMap.h:53
bool inBounds(IndexT N) const
Definition IndexedMap.h:77
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
A self-contained host- and target-independent arbitrary-precision floating-point software implementat...
Definition ADL.h:123
This is an optimization pass for GlobalISel generic memory operations.
#define N
Ty & operator()(Ty &self) const
Definition IndexedMap.h:33
const Ty & operator()(const Ty &self) const
Definition IndexedMap.h:34