17#ifndef WPIUTIL_WPI_MAPVECTOR_H
18#define WPIUTIL_WPI_MAPVECTOR_H
33template <
typename KeyT,
typename ValueT,
34 typename MapType = DenseMap<KeyT, unsigned>,
35 typename VectorType = 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");
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 std::pair<iterator, bool>
insert(
const std::pair<KeyT, ValueT> &KV) {
118 std::pair<KeyT, typename MapType::mapped_type> Pair = std::make_pair(KV.first, 0);
119 std::pair<typename MapType::iterator, bool> Result = Map.insert(Pair);
120 auto &I = Result.first->second;
122 Vector.push_back(std::make_pair(KV.first, KV.second));
123 I = Vector.size() - 1;
124 return std::make_pair(std::prev(
end()),
true);
126 return std::make_pair(
begin() + I,
false);
129 std::pair<iterator, bool>
insert(std::pair<KeyT, ValueT> &&KV) {
131 std::pair<KeyT, typename MapType::mapped_type> Pair = std::make_pair(KV.first, 0);
132 std::pair<typename MapType::iterator, bool> Result = Map.insert(Pair);
133 auto &I = Result.first->second;
135 Vector.push_back(std::move(KV));
136 I = Vector.size() - 1;
137 return std::make_pair(std::prev(
end()),
true);
139 return std::make_pair(
begin() + I,
false);
142 bool contains(
const KeyT &Key)
const {
return Map.find(Key) != Map.end(); }
147 typename MapType::const_iterator Pos = Map.find(Key);
148 return Pos == Map.end()? Vector.end() :
149 (Vector.begin() + Pos->second);
153 typename MapType::const_iterator Pos = Map.find(Key);
154 return Pos == Map.end()? Vector.end() :
155 (Vector.begin() + Pos->second);
160 typename MapType::iterator Pos = Map.find(Vector.back().first);
172 typename VectorType::iterator
erase(
typename VectorType::iterator Iterator) {
173 Map.erase(Iterator->first);
174 auto Next = Vector.erase(Iterator);
175 if (Next == Vector.end())
179 size_t Index = Next - Vector.begin();
180 for (
auto &I : Map) {
181 assert(I.second != Index &&
"Index was already erased!");
182 if (I.second > Index)
192 auto Iterator =
find(Key);
193 if (Iterator ==
end())
203 template <
class Predicate>
void remove_if(Predicate Pred);
206template <
typename KeyT,
typename ValueT,
typename MapType,
typename VectorType>
207template <
class Function>
209 auto O = Vector.begin();
210 for (
auto I = O, E = Vector.end(); I != E; ++I) {
220 Map[O->first] = O - Vector.begin();
225 Vector.erase(O, Vector.end());
230template <
typename KeyT,
typename ValueT,
unsigned N>
232 :
MapVector<KeyT, ValueT, SmallDenseMap<KeyT, unsigned, N>,
233 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:191
void clear()
Definition: MapVector.h:88
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:117
bool contains(const KeyT &Key) const
Definition: MapVector.h:142
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
KeyT key_type
Definition: MapVector.h:45
iterator find(const KeyT &Key)
Definition: MapVector.h:146
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:159
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:144
void swap(MapVector &RHS)
Definition: MapVector.h:93
std::pair< iterator, bool > insert(std::pair< KeyT, ValueT > &&KV)
Definition: MapVector.h:129
VectorType::iterator erase(typename VectorType::iterator Iterator)
Remove the element given by Iterator.
Definition: MapVector.h:172
typename VectorType::iterator iterator
Definition: MapVector.h:49
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:152
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) is_nothrow_move_constructible< wpi::WPI_BASIC_JSON_TPL >::value &&//NOLINT(misc-redundant-expression) is_nothrow_move_assignable< wpi::WPI_BASIC_JSON_TPL >::value)
exchanges the values of two JSON objects
Definition: json.h:5219
Definition: ntcore_cpp.h:26
A MapVector that performs no allocations if smaller than a certain size.
Definition: MapVector.h:233