LLVM 24.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"
26#include <cassert>
27
28namespace llvm {
29
30namespace detail {
31template <class Ty> struct IdentityIndex {
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
39template <typename T, typename ToIndexT = detail::IdentityIndex<unsigned>>
41 using IndexT = typename ToIndexT::argument_type;
42 // Prefer SmallVector with zero inline storage over std::vector. IndexedMaps
43 // can grow very large and SmallVector grows more efficiently as long as T
44 // is trivially copyable.
45 using StorageT = SmallVector<T, 0>;
46
47 StorageT Storage;
48 T NullVal = T();
49 ToIndexT ToIndex;
50
51public:
52 IndexedMap() = default;
53
54 explicit IndexedMap(const T &Val) : NullVal(Val) {}
55
56 typename StorageT::reference operator[](IndexT N) {
57 assert(ToIndex(N) < Storage.size() && "index out of bounds!");
58 return Storage[ToIndex(N)];
59 }
60
61 typename StorageT::const_reference operator[](IndexT N) const {
62 assert(ToIndex(N) < Storage.size() && "index out of bounds!");
63 return Storage[ToIndex(N)];
64 }
65
66 void reserve(typename StorageT::size_type S) { Storage.reserve(S); }
67
68 void resize(typename StorageT::size_type S) { Storage.resize(S, NullVal); }
69
70 void clear() { Storage.clear(); }
71
72 void grow(IndexT N) {
73 unsigned NewSize = ToIndex(N) + 1;
74 if (NewSize > Storage.size())
75 resize(NewSize);
76 }
77
78 bool inBounds(IndexT N) const { return ToIndex(N) < Storage.size(); }
79
80 typename StorageT::size_type size() const { return Storage.size(); }
81};
82
83} // namespace llvm
84
85#endif // LLVM_ADT_INDEXEDMAP_H
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
#define LLVM_ATTRIBUTE_WARN_UNUSED
Definition Compiler.h:240
#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:80
StorageT::const_reference operator[](IndexT N) const
Definition IndexedMap.h:61
void grow(IndexT N)
Definition IndexedMap.h:72
void resize(typename StorageT::size_type S)
Definition IndexedMap.h:68
IndexedMap()=default
void reserve(typename StorageT::size_type S)
Definition IndexedMap.h:66
StorageT::reference operator[](IndexT N)
Definition IndexedMap.h:56
IndexedMap(const T &Val)
Definition IndexedMap.h:54
bool inBounds(IndexT N) const
Definition IndexedMap.h:78
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:34
const Ty & operator()(const Ty &self) const
Definition IndexedMap.h:35