17#ifndef LLVM_ADT_MAPVECTOR_H
18#define LLVM_ADT_MAPVECTOR_H
34template <
typename KeyT,
typename ValueT,
44 using iterator =
typename VectorType::iterator;
52 return std::move(Vector);
63 Map.reserve(NumEntries);
64 Vector.reserve(NumEntries);
74 return Vector.rbegin();
79 [[nodiscard]]
bool empty()
const {
return Vector.empty(); }
81 [[nodiscard]] std::pair<KeyT, ValueT> &
front() {
return Vector.front(); }
82 [[nodiscard]]
const std::pair<KeyT, ValueT> &
front()
const {
83 return Vector.front();
85 [[nodiscard]] std::pair<KeyT, ValueT> &
back() {
return Vector.back(); }
86 [[nodiscard]]
const std::pair<KeyT, ValueT> &
back()
const {
101 return try_emplace_impl(
Key).first->second;
111 static_assert(std::is_copy_constructible_v<ValueT>,
112 "Cannot call lookup() if ValueT is not copyable.");
114 return I ==
end() ? ValueT() :
I->second;
117 template <
typename... Ts>
119 return try_emplace_impl(
Key, std::forward<Ts>(Args)...);
121 template <
typename... Ts>
123 return try_emplace_impl(std::move(
Key), std::forward<Ts>(Args)...);
126 std::pair<iterator, bool>
insert(
const std::pair<KeyT, ValueT> &KV) {
127 return try_emplace_impl(KV.first, KV.second);
129 std::pair<iterator, bool>
insert(std::pair<KeyT, ValueT> &&KV) {
130 return try_emplace_impl(std::move(KV.first), std::move(KV.second));
133 template <
typename V>
137 Ret.first->second = std::forward<V>(Val);
140 template <
typename V>
144 Ret.first->second = std::forward<V>(Val);
157 if constexpr (canBeSmall())
159 return findInVector(Vector,
Key);
161 typename MapType::const_iterator Pos = Map.find(
Key);
162 return Pos == Map.end() ? Vector.end() : (Vector.begin() + Pos->second);
166 if constexpr (canBeSmall())
168 return findInVector(Vector,
Key);
170 typename MapType::const_iterator Pos = Map.find(
Key);
171 return Pos == Map.end() ? Vector.end() : (Vector.begin() + Pos->second);
178 assert(
I !=
end() &&
"MapVector::at failed due to a missing key");
186 assert(
I !=
end() &&
"MapVector::at failed due to a missing key");
192 if constexpr (canBeSmall())
198 typename MapType::iterator Pos = Map.find(Vector.back().first);
210 typename VectorType::iterator
erase(
typename VectorType::iterator Iterator) {
211 if constexpr (canBeSmall())
213 return Vector.erase(Iterator);
215 Map.erase(Iterator->first);
216 auto Next = Vector.erase(Iterator);
217 if (
Next == Vector.end())
221 size_t Index =
Next - Vector.begin();
222 for (
auto &
I : Map) {
223 assert(
I.second != Index &&
"Index was already erased!");
224 if (
I.second > Index)
235 if (Iterator ==
end())
248 template <
typename VectorT,
typename LookupKeyT>
249 [[nodiscard]]
static auto findInVector(VectorT &Vec,
const LookupKeyT &
Key) {
253 [[nodiscard]]
static constexpr bool canBeSmall() {
return N != 0; }
255 [[nodiscard]]
bool isSmall()
const {
return Map.empty(); }
258 if constexpr (canBeSmall()) {
260 for (
const auto &entry : Vector)
261 Map[entry.first] =
Index++;
268 static_assert(
N <= 32,
"Small size should be less than or equal to 32!");
271 std::is_integral_v<typename MapType::mapped_type>,
272 "The mapped_type of the specified Map must be an integral type");
274 template <
typename KeyArgT,
typename... Ts>
275 std::pair<iterator, bool> try_emplace_impl(KeyArgT &&
Key, Ts &&...Args) {
276 if constexpr (canBeSmall())
278 auto I = findInVector(Vector,
Key);
279 if (
I != Vector.end())
281 Vector.emplace_back(std::piecewise_construct,
282 std::forward_as_tuple(std::forward<KeyArgT>(
Key)),
283 std::forward_as_tuple(std::forward<Ts>(Args)...));
284 if (Vector.size() >
N)
286 return {std::prev(
end()),
true};
291 It->second = Vector.size();
292 Vector.emplace_back(std::piecewise_construct,
293 std::forward_as_tuple(std::forward<KeyArgT>(
Key)),
294 std::forward_as_tuple(std::forward<Ts>(Args)...));
295 return {std::prev(
end()),
true};
297 return {
begin() + It->second,
false};
301template <
typename KeyT,
typename ValueT,
typename MapType,
typename VectorType,
303template <
class Function>
305 if constexpr (canBeSmall())
311 auto O = Vector.begin();
312 for (
auto I = O,
E = Vector.end();
I !=
E; ++
I) {
322 Map[O->first] = O - Vector.begin();
327 Vector.erase(O, Vector.end());
332template <
typename KeyT,
typename ValueT,
unsigned N>
334 SmallVector<std::pair<KeyT, ValueT>, N>, N> {
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
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
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.
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.
FunctionAddr VTableAddr Next
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.
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.