17#ifndef WPIUTIL_WPI_MAPVECTOR_H
18#define WPIUTIL_WPI_MAPVECTOR_H
34template<
typename KeyT,
typename ValueT,
35 typename MapType = DenseMap<KeyT, unsigned>,
36 typename VectorType = std::vector<std::pair<KeyT, ValueT>>>
42 std::is_integral<typename MapType::mapped_type>::value,
43 "The mapped_type of the specified Map must be an integral type");
50 using iterator =
typename VectorType::iterator;
58 return std::move(Vector);
66 Map.reserve(NumEntries);
67 Vector.reserve(NumEntries);
81 return Vector.empty();
84 std::pair<KeyT, ValueT> &
front() {
return Vector.front(); }
85 const std::pair<KeyT, ValueT> &
front()
const {
return Vector.front(); }
86 std::pair<KeyT, ValueT> &
back() {
return Vector.back(); }
87 const std::pair<KeyT, ValueT> &
back()
const {
return Vector.back(); }
100 std::pair<KeyT, typename MapType::mapped_type> Pair = std::make_pair(Key, 0);
101 std::pair<typename MapType::iterator, bool> Result = Map.insert(Pair);
102 auto &I = Result.first->second;
104 Vector.push_back(std::make_pair(Key, ValueT()));
105 I = Vector.size() - 1;
107 return Vector[I].second;
112 static_assert(std::is_copy_constructible<ValueT>::value,
113 "Cannot call lookup() if ValueT is not copyable.");
114 typename MapType::const_iterator Pos = Map.find(Key);
115 return Pos == Map.end()? ValueT() : Vector[Pos->second].second;
118 std::pair<iterator, bool>
insert(
const std::pair<KeyT, ValueT> &KV) {
119 std::pair<KeyT, typename MapType::mapped_type> Pair = std::make_pair(KV.first, 0);
120 std::pair<typename MapType::iterator, bool> Result = Map.insert(Pair);
121 auto &I = Result.first->second;
123 Vector.push_back(std::make_pair(KV.first, KV.second));
124 I = Vector.size() - 1;
125 return std::make_pair(std::prev(
end()),
true);
127 return std::make_pair(
begin() + I,
false);
130 std::pair<iterator, bool>
insert(std::pair<KeyT, ValueT> &&KV) {
132 std::pair<KeyT, typename MapType::mapped_type> Pair = std::make_pair(KV.first, 0);
133 std::pair<typename MapType::iterator, bool> Result = Map.insert(Pair);
134 auto &I = Result.first->second;
136 Vector.push_back(std::move(KV));
137 I = Vector.size() - 1;
138 return std::make_pair(std::prev(
end()),
true);
140 return std::make_pair(
begin() + I,
false);
144 typename MapType::const_iterator Pos = Map.find(Key);
145 return Pos == Map.end()? 0 : 1;
149 typename MapType::const_iterator Pos = Map.find(Key);
150 return Pos == Map.end()? Vector.end() :
151 (Vector.begin() + Pos->second);
155 typename MapType::const_iterator Pos = Map.find(Key);
156 return Pos == Map.end()? Vector.end() :
157 (Vector.begin() + Pos->second);
162 typename MapType::iterator Pos = Map.find(Vector.back().first);
174 typename VectorType::iterator
erase(
typename VectorType::iterator Iterator) {
175 Map.erase(Iterator->first);
176 auto Next = Vector.erase(Iterator);
177 if (Next == Vector.end())
181 size_t Index = Next - Vector.begin();
182 for (
auto &I : Map) {
183 assert(I.second !=
Index &&
"Index was already erased!");
184 if (I.second >
Index)
194 auto Iterator =
find(Key);
195 if (Iterator ==
end())
205 template <
class Predicate>
void remove_if(Predicate Pred);
208template <
typename KeyT,
typename ValueT,
typename MapType,
typename VectorType>
209template <
class Function>
211 auto O = Vector.begin();
212 for (
auto I = O, E = Vector.end(); I != E; ++I) {
222 Map[O->first] = O - Vector.begin();
227 Vector.erase(O, Vector.end());
232template <
typename KeyT,
typename ValueT,
unsigned N>
234 :
MapVector<KeyT, ValueT, SmallDenseMap<KeyT, unsigned, N>,
235 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:37
size_type erase(const KeyT &Key)
Remove all elements with the key value Key.
Definition: MapVector.h:193
void clear()
Definition: MapVector.h:89
std::pair< KeyT, ValueT > & back()
Definition: MapVector.h:86
const std::pair< KeyT, ValueT > & back() const
Definition: MapVector.h:87
typename VectorType::size_type size_type
Definition: MapVector.h:48
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
Definition: MapVector.h:118
const_reverse_iterator rend() const
Definition: MapVector.h:78
typename VectorType::const_reverse_iterator const_reverse_iterator
Definition: MapVector.h:53
void reserve(size_type NumEntries)
Grow the MapVector so that it can contain at least NumEntries items before resizing again.
Definition: MapVector.h:65
reverse_iterator rend()
Definition: MapVector.h:77
reverse_iterator rbegin()
Definition: MapVector.h:75
iterator end()
Definition: MapVector.h:72
KeyT key_type
Definition: MapVector.h:46
iterator find(const KeyT &Key)
Definition: MapVector.h:148
typename VectorType::reverse_iterator reverse_iterator
Definition: MapVector.h:52
const_reverse_iterator rbegin() const
Definition: MapVector.h:76
void pop_back()
Remove the last element from the vector.
Definition: MapVector.h:161
ValueT & operator[](const KeyT &Key)
Definition: MapVector.h:99
std::pair< KeyT, ValueT > & front()
Definition: MapVector.h:84
size_type count(const KeyT &Key) const
Definition: MapVector.h:143
void swap(MapVector &RHS)
Definition: MapVector.h:94
std::pair< iterator, bool > insert(std::pair< KeyT, ValueT > &&KV)
Definition: MapVector.h:130
VectorType::iterator erase(typename VectorType::iterator Iterator)
Remove the element given by Iterator.
Definition: MapVector.h:174
typename VectorType::iterator iterator
Definition: MapVector.h:50
typename VectorType::const_iterator const_iterator
Definition: MapVector.h:51
ValueT lookup(const KeyT &Key) const
Definition: MapVector.h:111
typename VectorType::value_type value_type
Definition: MapVector.h:47
const_iterator find(const KeyT &Key) const
Definition: MapVector.h:154
size_type size() const
Definition: MapVector.h:61
iterator begin()
Definition: MapVector.h:70
bool empty() const
Definition: MapVector.h:80
const_iterator begin() const
Definition: MapVector.h:71
const std::pair< KeyT, ValueT > & front() const
Definition: MapVector.h:85
void remove_if(Predicate Pred)
Remove the elements that match the predicate.
const_iterator end() const
Definition: MapVector.h:73
VectorType takeVector()
Clear the MapVector and return the underlying vector.
Definition: MapVector.h:56
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:74
void swap(wpi::SmallPtrSet< T, N > &LHS, wpi::SmallPtrSet< T, N > &RHS)
Implement std::swap in terms of SmallPtrSet swap.
Definition: SmallPtrSet.h:512
Definition: AprilTagFieldLayout.h:18
A MapVector that performs no allocations if smaller than a certain size.
Definition: MapVector.h:235