5 #ifndef UAVCAN_UTIL_MAP_HPP_INCLUDED
6 #define UAVCAN_UTIL_MAP_HPP_INCLUDED
32 template <
typename Key,
typename Value>
46 KVPair(
const Key& arg_key,
const Value& arg_value) :
51 bool match(
const Key& rhs)
const {
return rhs == key; }
62 StaticAssert<(static_cast<unsigned>(NumKV) > 0)>::
check();
88 for (
unsigned i = 0; i < static_cast<unsigned>(NumKV); i++)
90 if (kvs[i].
match(key))
102 KVPair* findKey(
const Key& key);
108 bool operator()(
const Key&,
const Value&)
const {
return true; }
113 allocator_(allocator)
126 Value* access(
const Key& key);
131 Value* insert(
const Key& key,
const Value& value);
136 void remove(
const Key& key);
143 template <
typename Predicate>
144 void removeAllWhere(Predicate predicate);
151 template <
typename Predicate>
152 const Key* find(Predicate predicate)
const;
164 KVPair* getByIndex(
unsigned index);
165 const KVPair* getByIndex(
unsigned index)
const;
175 unsigned getSize()
const;
183 template <
typename Key,
typename Value>
186 KVGroup* p = list_.get();
189 KVPair*
const kv = p->find(key);
194 p = p->getNextListNode();
199 template <
typename Key,
typename Value>
202 KVGroup* p = list_.get();
205 KVGroup*
const next = p->getNextListNode();
206 bool remove_this =
true;
207 for (
int i = 0; i < KVGroup::NumKV; i++)
209 if (!p->kvs[i].match(Key()))
218 KVGroup::destroy(p, allocator_);
224 template <
typename Key,
typename Value>
228 KVPair*
const kv = findKey(key);
232 template <
typename Key,
typename Value>
238 KVPair*
const kv = findKey(Key());
241 *kv = KVPair(key, value);
245 KVGroup*
const kvg = KVGroup::instantiate(allocator_);
251 kvg->kvs[0] = KVPair(key, value);
252 return &kvg->kvs[0].value;
255 template <
typename Key,
typename Value>
259 KVPair*
const kv = findKey(key);
267 template <
typename Key,
typename Value>
268 template <
typename Predicate>
271 unsigned num_removed = 0;
273 KVGroup* p = list_.get();
276 KVGroup*
const next_group = p->getNextListNode();
278 for (
int i = 0; i < KVGroup::NumKV; i++)
280 const KVPair*
const kv = p->kvs + i;
281 if (!kv->match(Key()))
283 if (predicate(kv->key, kv->value))
286 p->kvs[i] = KVPair();
300 template <
typename Key,
typename Value>
301 template <
typename Predicate>
304 KVGroup* p = list_.get();
307 KVGroup*
const next_group = p->getNextListNode();
309 for (
int i = 0; i < KVGroup::NumKV; i++)
311 const KVPair*
const kv = p->kvs + i;
312 if (!kv->match(Key()))
314 if (predicate(kv->key, kv->value))
316 return &p->kvs[i].key;
326 template <
typename Key,
typename Value>
329 removeAllWhere(YesPredicate());
332 template <
typename Key,
typename Value>
336 KVGroup* p = list_.get();
339 KVGroup*
const next_group = p->getNextListNode();
341 for (
int i = 0; i < KVGroup::NumKV; i++)
343 KVPair*
const kv = p->kvs + i;
344 if (!kv->match(Key()))
360 template <
typename Key,
typename Value>
366 template <
typename Key,
typename Value>
370 KVGroup* p = list_.get();
373 for (
int i = 0; i < KVGroup::NumKV; i++)
375 const KVPair*
const kv = p->kvs + i;
376 if (!kv->match(Key()))
381 p = p->getNextListNode();
388 #endif // UAVCAN_UTIL_MAP_HPP_INCLUDED