17#ifndef LLVM_ADT_MAPVECTOR_H
18#define LLVM_ADT_MAPVECTOR_H
35template <
typename KeyT,
typename ValueT,
45 using iterator =
typename VectorType::iterator;
53 return std::move(Vector);
64 Map.reserve(NumEntries);
65 Vector.reserve(NumEntries);
75 return Vector.rbegin();
80 [[nodiscard]]
bool empty()
const {
return Vector.empty(); }
82 [[nodiscard]] std::pair<KeyT, ValueT> &
front() {
return Vector.front(); }
83 [[nodiscard]]
const std::pair<KeyT, ValueT> &
front()
const {
84 return Vector.front();
86 [[nodiscard]] std::pair<KeyT, ValueT> &
back() {
return Vector.back(); }
87 [[nodiscard]]
const std::pair<KeyT, ValueT> &
back()
const {
102 return try_emplace_impl(
Key).first->second;
112 static_assert(std::is_copy_constructible_v<ValueT>,
113 "Cannot call lookup() if ValueT is not copyable.");
115 return I ==
end() ? ValueT() :
I->second;
118 template <
typename... Ts>
120 return try_emplace_impl(
Key, std::forward<Ts>(Args)...);
122 template <
typename... Ts>
124 return try_emplace_impl(std::move(
Key), std::forward<Ts>(Args)...);
127 std::pair<iterator, bool>
insert(
const std::pair<KeyT, ValueT> &KV) {
128 return try_emplace_impl(KV.first, KV.second);
130 std::pair<iterator, bool>
insert(std::pair<KeyT, ValueT> &&KV) {
131 return try_emplace_impl(std::move(KV.first), std::move(KV.second));
134 template <
typename V>
138 Ret.first->second = std::forward<V>(Val);
141 template <
typename V>
145 Ret.first->second = std::forward<V>(Val);
158 if constexpr (canBeSmall())
160 return findInVector(Vector,
Key);
162 typename MapType::const_iterator Pos = Map.find(
Key);
163 return Pos == Map.end() ? Vector.end() : (Vector.begin() + Pos->second);
167 if constexpr (canBeSmall())
169 return findInVector(Vector,
Key);
171 typename MapType::const_iterator Pos = Map.find(
Key);
172 return Pos == Map.end() ? Vector.end() : (Vector.begin() + Pos->second);
179 assert(
I !=
end() &&
"MapVector::at failed due to a missing key");
187 assert(
I !=
end() &&
"MapVector::at failed due to a missing key");
193 if constexpr (canBeSmall())
199 typename MapType::iterator Pos = Map.find(Vector.back().first);
211 typename VectorType::iterator
erase(
typename VectorType::iterator Iterator) {
212 if constexpr (canBeSmall())
214 return Vector.erase(Iterator);
216 Map.erase(Iterator->first);
217 auto Next = Vector.erase(Iterator);
218 if (
Next == Vector.end())
222 size_t Index =
Next - Vector.begin();
223 for (
auto &
I : Map) {
224 assert(
I.second != Index &&
"Index was already erased!");
225 if (
I.second > Index)
236 if (Iterator ==
end())
257 template <
typename VectorT,
typename LookupKeyT>
258 [[nodiscard]]
static auto findInVector(VectorT &Vec,
const LookupKeyT &
Key) {
262 [[nodiscard]]
static constexpr bool canBeSmall() {
return N != 0; }
264 [[nodiscard]]
bool isSmall()
const {
return Map.empty(); }
267 if constexpr (canBeSmall()) {
269 for (
const auto &entry : Vector)
270 Map[entry.first] =
Index++;
277 static_assert(
N <= 32,
"Small size should be less than or equal to 32!");
280 std::is_integral_v<typename MapType::mapped_type>,
281 "The mapped_type of the specified Map must be an integral type");
283 template <
typename KeyArgT,
typename... Ts>
284 std::pair<iterator, bool> try_emplace_impl(KeyArgT &&
Key, Ts &&...Args) {
285 if constexpr (canBeSmall())
287 auto I = findInVector(Vector,
Key);
288 if (
I != Vector.end())
290 Vector.emplace_back(std::piecewise_construct,
291 std::forward_as_tuple(std::forward<KeyArgT>(
Key)),
292 std::forward_as_tuple(std::forward<Ts>(Args)...));
293 if (Vector.size() >
N)
295 return {std::prev(
end()),
true};
300 It->second = Vector.size();
301 Vector.emplace_back(std::piecewise_construct,
302 std::forward_as_tuple(std::forward<KeyArgT>(
Key)),
303 std::forward_as_tuple(std::forward<Ts>(Args)...));
304 return {std::prev(
end()),
true};
306 return {
begin() + It->second,
false};
312template <
class Function>
314 if constexpr (canBeSmall())
320 auto O = Vector.begin();
321 for (
auto I = O,
E = Vector.end();
I !=
E; ++
I) {
331 Map[O->first] = O - Vector.begin();
336 Vector.erase(O, Vector.end());
341template <
typename KeyT,
typename ValueT,
unsigned N>
343 :
MapVector<KeyT, ValueT, DenseMap<KeyT, unsigned>,
344 SmallVector<std::pair<KeyT, ValueT>, N>, N> {};
346template <
typename KeyT,
typename ValueT,
352 return X.getMemorySize();
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
#define LLVM_ATTRIBUTE_WARN_UNUSED
This file defines the DenseMap class.
This file defines the SmallVector class.
Represent a constant reference to an array (0 or more elements consecutively in memory),...
This class implements a map that also provides access to all stored values in a deterministic order.
const std::pair< KeyT, ValueT > & back() const
size_type erase(const KeyT &Key)
Remove all elements with the key value Key.
void pop_back()
Remove the last element from the vector.
const_iterator begin() const
ValueT lookup(const KeyT &Key) const
ArrayRef< value_type > getArrayRef() const
Returns an array reference of the underlying vector.
iterator find(const KeyT &Key)
std::pair< iterator, bool > try_emplace(KeyT &&Key, Ts &&...Args)
typename VectorType::const_iterator const_iterator
void reserve(size_type NumEntries)
Grow the MapVector so that it can contain at least NumEntries items before resizing again.
const_iterator end() const
std::pair< iterator, bool > try_emplace(const KeyT &Key, Ts &&...Args)
typename VectorType::iterator iterator
std::pair< iterator, bool > insert_or_assign(const KeyT &Key, V &&Val)
VectorType takeVector()
Clear the MapVector and return the underlying vector.
typename VectorType::reverse_iterator reverse_iterator
void swap(MapVector &RHS)
bool contains(const KeyT &Key) const
void remove_if(Predicate Pred)
Remove the elements that match the predicate.
std::pair< iterator, bool > insert(std::pair< KeyT, ValueT > &&KV)
ValueT & operator[](const KeyT &Key)
const std::pair< KeyT, ValueT > & front() const
std::pair< KeyT, ValueT > & back()
std::pair< iterator, bool > insert_or_assign(KeyT &&Key, V &&Val)
const_reverse_iterator rend() const
ValueT & at(const KeyT &Key)
at - Return the entry for the specified key, or abort if no such entry exists.
const_iterator find(const KeyT &Key) const
const ValueT & at(const KeyT &Key) const
at - Return the entry for the specified key, or abort if no such entry exists.
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
typename VectorType::size_type size_type
VectorType::iterator erase(typename VectorType::iterator Iterator)
Remove the element given by Iterator.
size_type count(const KeyT &Key) const
const_reverse_iterator rbegin() const
size_t getMemorySize() const
Return the approximate size (in bytes) of the data structure.
reverse_iterator rbegin()
std::pair< KeyT, ValueT > & front()
typename VectorType::value_type value_type
typename VectorType::const_reverse_iterator const_reverse_iterator
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Base class of all SIMD vector types.
This is an optimization pass for GlobalISel generic memory operations.
BitVector::size_type capacity_in_bytes(const BitVector &X)
auto make_first_range(ContainerTy &&c)
Given a container of pairs, return a range over the first elements.
LLVM_ATTRIBUTE_VISIBILITY_DEFAULT AnalysisKey InnerAnalysisManagerProxy< AnalysisManagerT, IRUnitT, ExtraArgTs... >::Key
auto remove_if(R &&Range, UnaryPredicate P)
Provide wrappers to std::remove_if which take ranges instead of having to pass begin/end explicitly.
auto make_second_range(ContainerTy &&c)
Given a container of pairs, return a range over the second elements.
auto find_if(R &&Range, UnaryPredicate P)
Provide wrappers to std::find_if which take ranges instead of having to pass begin/end explicitly.
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Next
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
A MapVector that performs no allocations if smaller than a certain size.