1//===- llvm/ADT/MapVector.h - Map w/ deterministic value order --*- C++ -*-===// 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 7//===----------------------------------------------------------------------===// 10/// This file implements a map that provides insertion order iteration. The 11/// interface is purposefully minimal. The key is assumed to be cheap to copy 12/// and 2 copies are kept, one for indexing in a DenseMap, one for iteration in 15//===----------------------------------------------------------------------===// 17#ifndef LLVM_ADT_MAPVECTOR_H 18#define LLVM_ADT_MAPVECTOR_H 30/// This class implements a map that also provides access to all stored values 31/// in a deterministic order. The values are kept in a SmallVector<*, 0> and the 32/// mapping is done with DenseMap from Keys to indexes in that vector. 34typename MapType = DenseMap<KeyT, unsigned>,
35typenameVectorType = SmallVector<std::pair<KeyT, ValueT>, 0>>
41 std::is_integral_v<typename MapType::mapped_type>,
42"The mapped_type of the specified Map must be an integral type");
54 /// Clear the MapVector and return the underlying vector. 57return std::move(Vector);
62 /// Grow the MapVector so that it can contain at least \p NumEntries items 63 /// before resizing again. 65 Map.reserve(NumEntries);
66 Vector.reserve(NumEntries);
83 std::pair<KeyT, ValueT> &
front() {
return Vector.front(); }
84const std::pair<KeyT, ValueT> &
front()
const{
return Vector.front(); }
85 std::pair<KeyT, ValueT> &
back() {
return Vector.back(); }
86const std::pair<KeyT, ValueT> &
back()
const{
return Vector.back(); }
99 std::pair<KeyT, typename MapType::mapped_type> Pair = std::make_pair(Key, 0);
100 std::pair<typename MapType::iterator, bool> Result = Map.insert(Pair);
101auto &
I = Result.first->second;
103 Vector.push_back(std::make_pair(Key,
ValueT()));
104I = Vector.size() - 1;
106return Vector[
I].second;
109// Returns a copy of the value. Only allowed if ValueT is copyable. 111static_assert(std::is_copy_constructible_v<ValueT>,
112"Cannot call lookup() if ValueT is not copyable.");
113typename MapType::const_iterator Pos = Map.find(Key);
114return Pos == Map.end()?
ValueT() : Vector[Pos->second].second;
117template <
typename... Ts>
119auto [It, Inserted] = Map.insert(std::make_pair(Key, 0));
121 It->second = Vector.size();
122 Vector.emplace_back(std::piecewise_construct, std::forward_as_tuple(Key),
123 std::forward_as_tuple(std::forward<Ts>(Args)...));
124return std::make_pair(std::prev(
end()),
true);
126return std::make_pair(
begin() + It->second,
false);
128template <
typename... Ts>
130auto [It, Inserted] = Map.insert(std::make_pair(Key, 0));
132 It->second = Vector.size();
133 Vector.emplace_back(std::piecewise_construct,
134 std::forward_as_tuple(std::move(Key)),
135 std::forward_as_tuple(std::forward<Ts>(Args)...));
136return std::make_pair(std::prev(
end()),
true);
138return std::make_pair(
begin() + It->second,
false);
141 std::pair<iterator, bool>
insert(
const std::pair<KeyT, ValueT> &KV) {
144 std::pair<iterator, bool>
insert(std::pair<KeyT, ValueT> &&KV) {
145returntry_emplace(std::move(KV.first), std::move(KV.second));
152 Ret.first->second = std::forward<V>(Val);
157auto Ret =
try_emplace(std::move(Key), std::forward<V>(Val));
159 Ret.first->second = std::forward<V>(Val);
168typename MapType::const_iterator Pos = Map.find(Key);
169return Pos == Map.end()? Vector.end() :
170 (Vector.begin() + Pos->second);
174typename MapType::const_iterator Pos = Map.find(Key);
175return Pos == Map.end()? Vector.end() :
176 (Vector.begin() + Pos->second);
179 /// Remove the last element from the vector. 181typename MapType::iterator Pos = Map.find(Vector.back().first);
186 /// Remove the element given by Iterator. 188 /// Returns an iterator to the element following the one which was removed, 189 /// which may be end(). 191 /// \note This is a deceivingly expensive operation (linear time). It's 192 /// usually better to use \a remove_if() if possible. 193typename VectorType::iterator
erase(
typename VectorType::iterator Iterator) {
194 Map.erase(Iterator->first);
195auto Next = Vector.erase(Iterator);
196if (Next == Vector.end())
199// Update indices in the map. 200size_tIndex = Next - Vector.begin();
209 /// Remove all elements with the key value Key. 211 /// Returns the number of elements removed. 213auto Iterator =
find(Key);
220 /// Remove the elements that match the predicate. 222 /// Erase all elements that match \c Pred in a single pass. Takes linear 227template <
typename KeyT,
typename ValueT,
typename MapType,
typename VectorType>
228template <
class Function>
233// Erase from the map. 239// Move the value and update the index in the map. 241 Map[O->first] = O -
Vector.begin();
245// Erase trailing entries in the vector. 249/// A MapVector that performs no allocations if smaller than a certain 251template <
typename KeyT,
typename ValueT,
unsigned N>
253 :
MapVector<KeyT, ValueT, SmallDenseMap<KeyT, unsigned, N>,
254 SmallVector<std::pair<KeyT, ValueT>, N>> {
257}
// end namespace llvm 259#endif// LLVM_ADT_MAPVECTOR_H static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
This file defines the DenseMap class.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This file defines the SmallVector class.
This class implements a map that also provides access to all stored values in a deterministic order.
std::pair< iterator, bool > try_emplace(KeyT &&Key, Ts &&...Args)
size_type count(const KeyT &Key) const
typename VectorType::iterator iterator
void pop_back()
Remove the last element from the vector.
const_reverse_iterator rbegin() const
void swap(MapVector &RHS)
ValueT & operator[](const KeyT &Key)
const_iterator end() const
VectorType::iterator erase(typename VectorType::iterator Iterator)
Remove the element given by Iterator.
std::pair< iterator, bool > insert_or_assign(KeyT &&Key, V &&Val)
VectorType takeVector()
Clear the MapVector and return the underlying vector.
typename VectorType::size_type size_type
const std::pair< KeyT, ValueT > & back() const
const std::pair< KeyT, ValueT > & front() const
typename VectorType::const_reverse_iterator const_reverse_iterator
typename VectorType::value_type value_type
iterator find(const KeyT &Key)
void remove_if(Predicate Pred)
Remove the elements that match the predicate.
bool contains(const KeyT &Key) const
const_iterator begin() const
std::pair< iterator, bool > insert_or_assign(const KeyT &Key, V &&Val)
const_iterator find(const KeyT &Key) const
std::pair< iterator, bool > try_emplace(const KeyT &Key, Ts &&...Args)
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
ValueT lookup(const KeyT &Key) const
void reserve(size_type NumEntries)
Grow the MapVector so that it can contain at least NumEntries items before resizing again.
typename VectorType::reverse_iterator reverse_iterator
size_type erase(const KeyT &Key)
Remove all elements with the key value Key.
typename VectorType::const_iterator const_iterator
std::pair< KeyT, ValueT > & front()
std::pair< iterator, bool > insert(std::pair< KeyT, ValueT > &&KV)
const_reverse_iterator rend() const
reverse_iterator rbegin()
std::pair< KeyT, ValueT > & back()
Base class of all SIMD vector types.
This is an optimization pass for GlobalISel generic memory operations.
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.