17#ifndef WPIUTIL_WPI_MAPVECTOR_H
18#define WPIUTIL_WPI_MAPVECTOR_H
33template <
typename KeyT,
typename ValueT,
34 typename MapType = DenseMap<KeyT, unsigned>,
41 std::is_integral_v<typename MapType::mapped_type>,
42 "The mapped_type of the specified Map must be an integral type");
49 using iterator =
typename VectorType::iterator;
57 return std::move(Vector);
65 Map.reserve(NumEntries);
66 Vector.reserve(NumEntries);
80 return Vector.empty();
83 std::pair<KeyT, ValueT> &
front() {
return Vector.front(); }
84 const std::pair<KeyT, ValueT> &
front()
const {
return Vector.front(); }
85 std::pair<KeyT, ValueT> &
back() {
return Vector.back(); }
86 const 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);
101 auto &I = Result.first->second;
103 Vector.push_back(std::make_pair(Key, ValueT()));
104 I = Vector.size() - 1;
106 return Vector[I].second;
111 static_assert(std::is_copy_constructible_v<ValueT>,
112 "Cannot call lookup() if ValueT is not copyable.");
113 typename MapType::const_iterator Pos = Map.find(Key);
114 return Pos == Map.end()? ValueT() : Vector[Pos->second].second;
117 template <
typename... Ts>
118 std::pair<iterator, bool>
try_emplace(
const KeyT &Key, Ts &&...Args) {
119 auto [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)...));
124 return std::make_pair(std::prev(
end()),
true);
126 return std::make_pair(
begin() + It->second,
false);
128 template <
typename... Ts>
130 auto [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)...));
136 return std::make_pair(std::prev(
end()),
true);
138 return 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) {
145 return try_emplace(std::move(KV.first), std::move(KV.second));
148 template <
typename V>
152 Ret.first->second = std::forward<V>(Val);
155 template <
typename V>
157 auto Ret =
try_emplace(std::move(Key), std::forward<V>(Val));
159 Ret.first->second = std::forward<V>(Val);
163 bool contains(
const KeyT &Key)
const {
return Map.find(Key) != Map.end(); }
168 typename MapType::const_iterator Pos = Map.find(Key);
169 return Pos == Map.end()? Vector.end() :
170 (Vector.begin() + Pos->second);
174 typename MapType::const_iterator Pos = Map.find(Key);
175 return Pos == Map.end()? Vector.end() :
176 (Vector.begin() + Pos->second);
181 typename MapType::iterator Pos = Map.find(Vector.back().first);
193 typename VectorType::iterator
erase(
typename VectorType::iterator Iterator) {
194 Map.erase(Iterator->first);
195 auto Next = Vector.erase(Iterator);
196 if (Next == Vector.end())
200 size_t Index = Next - Vector.begin();
201 for (
auto &I : Map) {
202 assert(I.second != Index &&
"Index was already erased!");
203 if (I.second > Index)
213 auto Iterator =
find(Key);
214 if (Iterator ==
end())
224 template <
class Predicate>
void remove_if(Predicate Pred);
227template <
typename KeyT,
typename ValueT,
typename MapType,
typename VectorType>
228template <
class Function>
230 auto O = Vector.begin();
231 for (
auto I = O, E = Vector.end(); I != E; ++I) {
241 Map[O->first] = O - Vector.begin();
246 Vector.erase(O, Vector.end());
251template <
typename KeyT,
typename ValueT,
unsigned N>
253 :
MapVector<KeyT, ValueT, SmallDenseMap<KeyT, unsigned, N>,
254 SmallVector<std::pair<KeyT, ValueT>, N>> {
This file defines the DenseMap class.
This file defines the SmallVector class.
This class implements a map that also provides access to all stored values in a deterministic order.
Definition MapVector.h:36
size_type erase(const KeyT &Key)
Remove all elements with the key value Key.
Definition MapVector.h:212
void clear()
Definition MapVector.h:88
std::pair< iterator, bool > try_emplace(const KeyT &Key, Ts &&...Args)
Definition MapVector.h:118
std::pair< KeyT, ValueT > & back()
Definition MapVector.h:85
const std::pair< KeyT, ValueT > & back() const
Definition MapVector.h:86
typename VectorType::size_type size_type
Definition MapVector.h:47
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
Definition MapVector.h:141
bool contains(const KeyT &Key) const
Definition MapVector.h:163
const_reverse_iterator rend() const
Definition MapVector.h:77
typename VectorType::const_reverse_iterator const_reverse_iterator
Definition MapVector.h:52
void reserve(size_type NumEntries)
Grow the MapVector so that it can contain at least NumEntries items before resizing again.
Definition MapVector.h:64
reverse_iterator rend()
Definition MapVector.h:76
reverse_iterator rbegin()
Definition MapVector.h:74
iterator end()
Definition MapVector.h:71
std::pair< iterator, bool > insert_or_assign(const KeyT &Key, V &&Val)
Definition MapVector.h:149
KeyT key_type
Definition MapVector.h:45
iterator find(const KeyT &Key)
Definition MapVector.h:167
typename VectorType::reverse_iterator reverse_iterator
Definition MapVector.h:51
const_reverse_iterator rbegin() const
Definition MapVector.h:75
void pop_back()
Remove the last element from the vector.
Definition MapVector.h:180
ValueT & operator[](const KeyT &Key)
Definition MapVector.h:98
std::pair< KeyT, ValueT > & front()
Definition MapVector.h:83
size_type count(const KeyT &Key) const
Definition MapVector.h:165
void swap(MapVector &RHS)
Definition MapVector.h:93
std::pair< iterator, bool > insert(std::pair< KeyT, ValueT > &&KV)
Definition MapVector.h:144
VectorType::iterator erase(typename VectorType::iterator Iterator)
Remove the element given by Iterator.
Definition MapVector.h:193
typename VectorType::iterator iterator
Definition MapVector.h:49
std::pair< iterator, bool > try_emplace(KeyT &&Key, Ts &&...Args)
Definition MapVector.h:129
typename VectorType::const_iterator const_iterator
Definition MapVector.h:50
ValueT lookup(const KeyT &Key) const
Definition MapVector.h:110
typename VectorType::value_type value_type
Definition MapVector.h:46
const_iterator find(const KeyT &Key) const
Definition MapVector.h:173
std::pair< iterator, bool > insert_or_assign(KeyT &&Key, V &&Val)
Definition MapVector.h:156
size_type size() const
Definition MapVector.h:60
iterator begin()
Definition MapVector.h:69
bool empty() const
Definition MapVector.h:79
const_iterator begin() const
Definition MapVector.h:70
const std::pair< KeyT, ValueT > & front() const
Definition MapVector.h:84
void remove_if(Predicate Pred)
Remove the elements that match the predicate.
const_iterator end() const
Definition MapVector.h:72
VectorType takeVector()
Clear the MapVector and return the underlying vector.
Definition MapVector.h:55
WPI_BASIC_JSON_TPL_DECLARATION void swap(wpi::WPI_BASIC_JSON_TPL &j1, wpi::WPI_BASIC_JSON_TPL &j2) noexcept(//NOLINT(readability-inconsistent-declaration-parameter-name, cert-dcl58-cpp) is_nothrow_move_constructible< wpi::WPI_BASIC_JSON_TPL >::value &&//NOLINT(misc-redundant-expression, cppcoreguidelines-noexcept-swap, performance-noexcept-swap) is_nothrow_move_assignable< wpi::WPI_BASIC_JSON_TPL >::value)
exchanges the values of two JSON objects
Definition json.h:5258
Foonathan namespace.
Definition ntcore_cpp.h:26
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
Definition SmallVector.h:1138
A MapVector that performs no allocations if smaller than a certain size.
Definition MapVector.h:254