25 typedef typename VectorType::iterator iterator;
26 typedef typename VectorType::const_iterator const_iterator;
38 const_iterator begin()
const {
41 const_iterator end()
const {
45 iterator find(
const Key& key) {
46 for (
auto it = begin(); it != end(); ++it) {
47 if (it->first == key) {
54 const_iterator find(
const Key& key)
const {
55 for (
auto it = begin(); it != end(); ++it) {
56 if (it->first == key) {
63 template<
typename Less>
64 iterator lowest(Less less_than = Less()) {
65 iterator lowest = end();
66 for (iterator it = begin(); it != end(); ++it) {
67 if (lowest == end() || less_than(it->first, lowest->first)) {
74 template<
typename Less>
75 const_iterator lowest(Less less_than = Less())
const {
76 const_iterator lowest = end();
77 for (const_iterator it = begin(); it != end(); ++it) {
78 if (lowest == end() || less_than(it->first, lowest->first)) {
85 template<
typename Less>
86 iterator highest(Less less_than = Less()) {
87 iterator highest = end();
88 for (iterator it = begin(); it != end(); ++it) {
89 if (highest == end() || less_than(highest->first, it->first)) {
96 template<
typename Less>
97 const_iterator highest(Less less_than = Less())
const {
98 const_iterator highest = end();
99 for (const_iterator it = begin(); it != end(); ++it) {
100 if (highest == end() || less_than(highest->first, it->first)) {
109 bool get(
const Key& key, Value* value)
const {
110 const_iterator it = find(key);
118 Value get(
const Key& key,
bool* has=
nullptr)
const {
119 const_iterator it = find(key);
133 iterator it = find(key);
136 *result = InsertResult::kExists;
141 if (data.size() < N) {
142 data.push_back(
PairKV(key, value));
144 *result = InsertResult::kInserted;
147 return {
true, data.end() - 1};
150 *result = InsertResult::kMaxSize;
153 return {
false, end()};
156 bool update(
const Key& key,
const Value& value,
bool insert_if_missing =
true) {
157 iterator it = find(key);
161 }
else if (insert_if_missing) {
162 return insert(key, value).first;
167 Value& operator[](
const Key& key) {
168 iterator it = find(key);
172 data.push_back(
PairKV(key, Value()));
173 return data.back().second;
176 const Value& operator[](
const Key& key)
const {
177 const_iterator it = find(key);
181 static Value default_value;
182 return default_value;
185 bool next(
const Key& key,
Key* next_key,
bool allow_rollover =
false)
const {
186 const_iterator it = find(key);
190 *next_key = it->first;
192 }
else if (allow_rollover && !empty()) {
193 *next_key = begin()->first;
200 bool prev(
const Key& key,
Key* prev_key,
bool allow_rollover =
false)
const {
201 const_iterator it = find(key);
205 *prev_key = it->first;
207 }
else if (allow_rollover && !empty()) {
208 *prev_key = data[data.size() - 1].first;
217 constexpr size_t size()
const {
221 constexpr bool empty()
const {
226 constexpr size_t capacity()
const {
235 bool has(
const Key& it)
const {
236 return find(it) != end();
249 Pair(
const Key& k =
Key(),
const Value& v = Value())
250 : first(k), second(v) {}
255 bool operator()(
const Pair& a,
const Pair& b)
const {
256 return less(a.first, b.first);
268 : data(PairLess{less}) {
271 void setMaxSize(
size_t n) {
275 void reserve(
size_t n) {
279 bool insert(
const Key& key,
const Value& value, InsertResult* result =
nullptr) {
280 return data.insert(Pair(key, value), result);
283 bool has(
const Key& key)
const {
284 return data.has(Pair(key));
287 size_t size()
const {
return data.size(); }
288 bool empty()
const {
return data.empty(); }
289 bool full()
const {
return data.full(); }
290 size_t capacity()
const {
return data.capacity(); }
291 void clear() { data.clear(); }
294 iterator begin() {
return data.begin(); }
295 iterator end() {
return data.end(); }
300 return data.find(Pair(key));
303 return data.find(Pair(key));
306 bool erase(
const Key& key) {
307 return data.erase(Pair(key));
310 return data.erase(it);
314 return data.lower_bound(Pair(key));
318 return data.lower_bound(Pair(key));
323 if (it != end() && it->first == key) {
331 if (it != end() && it->first == key) {
337 Pair& front() {
return data.front(); }
338 const Pair& front()
const {
return data.front(); }
339 Pair& back() {
return data.back(); }
340 const Pair& back()
const {
return data.back(); }