8 #ifndef META_OCEAN_BASE_HASH_MAP_H
9 #define META_OCEAN_BASE_HASH_MAP_H
24 template <
typename TKey,
typename T>
32 typedef typename std::pair< std::pair<size_t, size_t>, std::pair<TKey, T> >
Element;
73 bool insert(
const TKey& key,
const T&
element,
const bool oneOnly =
true,
const bool extendCapacity =
true);
83 bool insert(TKey&& key, T&&
element,
const bool oneOnly =
true,
const bool extendCapacity =
true);
90 bool remove(
const TKey& key);
97 bool find(
const TKey& key)
const;
105 bool find(
const TKey& key,
const T*&
element)
const;
121 const T&
element(
const TKey& key)
const;
132 inline size_t size()
const;
194 template <
typename TKey,
typename T>
196 mapElements(hashMap.mapElements),
197 mapSize(hashMap.mapSize),
198 mapFunction(hashMap.mapFunction)
203 template <
typename TKey,
typename T>
205 mapElements(std::move(hashMap.mapElements)),
206 mapSize(hashMap.mapSize),
207 mapFunction(hashMap.mapFunction)
212 template <
typename TKey,
typename T>
214 mapElements(capacity),
216 mapFunction(function)
221 template <
typename TKey,
typename T>
223 mapElements(capacity),
225 mapFunction(hashMap.mapFunction)
227 ocean_assert(
capacity >= hashMap.size());
229 for (
typename Elements::iterator i = hashMap.mapElements.begin(); i != hashMap.mapElements.end(); ++i)
230 if (i->first.first != 0)
232 TKey& key = i->second.first;
238 ocean_assert(
size() == hashMap.size());
244 template <
typename TKey,
typename T>
247 ocean_assert(mapSize <= mapElements.size());
248 ocean_assert(isConsistent());
251 if (extendCapacity && mapSize >= mapElements.size() * 80 / 100)
253 *
this =
HashMap<TKey, T>(max(
size_t(32), mapElements.size() * 2), std::move(*
this));
254 ocean_assert(mapSize < mapElements.size() * 80 / 100);
257 if (mapSize == mapElements.size())
263 for (
size_t n = 0; n < mapElements.size(); ++n)
265 const size_t value = (mapFunction(key) + n) % mapElements.size();
268 if (mapElements[value].first.first == 0)
270 mapElements[value].first.first = 1;
271 mapElements[value].first.second = n;
272 mapElements[value].second.first = key;
273 mapElements[value].second.second = element;
278 else if (mapElements[value].second.first == key)
281 mapElements[value].first.first++;
287 for (
size_t n = 0; n < mapElements.size(); ++n)
289 const size_t value = (mapFunction(key) + n) % mapElements.size();
292 if (mapElements[value].first.first == 0)
294 mapElements[value].first.first = 1;
295 mapElements[value].first.second = n;
296 mapElements[value].second.first = key;
297 mapElements[value].second.second = element;
303 mapElements[value].first.first++;
307 ocean_assert(
false &&
"This must never happen!");
311 template <
typename TKey,
typename T>
314 ocean_assert(mapSize <= mapElements.size());
315 ocean_assert(isConsistent());
318 if (extendCapacity && mapSize >= mapElements.size() * 80 / 100)
320 *
this =
HashMap<TKey, T>(max(
size_t(32), mapElements.size() * 2), std::move(*
this));
321 ocean_assert(mapSize < mapElements.size() * 80 / 100);
324 if (mapSize == mapElements.size())
330 for (
size_t n = 0; n < mapElements.size(); ++n)
332 const size_t value = (mapFunction(key) + n) % mapElements.size();
335 if (mapElements[value].first.first == 0)
337 mapElements[value].first.first = 1;
338 mapElements[value].first.second = n;
339 mapElements[value].second.first = std::move(key);
340 mapElements[value].second.second = std::move(element);
345 else if (mapElements[value].second.first == key)
348 mapElements[value].first.first++;
354 for (
size_t n = 0; n < mapElements.size(); ++n)
356 const size_t value = (mapFunction(key) + n) % mapElements.size();
359 if (mapElements[value].first.first == 0)
361 mapElements[value].first.first = 1;
362 mapElements[value].first.second = n;
363 mapElements[value].second.first = std::move(key);
364 mapElements[value].second.second = std::move(element);
370 mapElements[value].first.first++;
374 ocean_assert(
false &&
"This must never happen!");
378 template <
typename TKey,
typename T>
381 ocean_assert(mapSize <= mapElements.size());
382 ocean_assert(isConsistent());
385 for (
size_t n = 0; n < mapElements.size(); ++n)
387 const size_t value = (mapFunction(key) + n) % mapElements.size();
390 if (mapElements[value].first.first == 0)
394 if (mapElements[value].first.first == 1)
396 if (mapElements[value].second.first == key)
398 mapElements[value].first.first = 0;
399 mapElements[value].second.first = TKey();
400 mapElements[value].second.second = T();
403 ocean_assert(isConsistent());
412 ocean_assert(mapElements[value].first.first > 1);
414 size_t elementOffset = 0u;
416 if (mapElements[value].second.first == key)
420 size_t localValue = value;
421 size_t endLocation = mapElements.size();
425 size_t lastOffset = 0;
428 for (
size_t i = 1; i < endLocation; ++i)
430 const size_t testValue = (localValue + i) % mapElements.size();
432 if (mapElements[testValue].first.first >= 1)
434 if (mapElements[testValue].first.second >= i)
438 if (mapElements[testValue].first.first <= 1)
445 ocean_assert(endLocation >= lastOffset);
446 endLocation -= lastOffset;
448 elementOffset += lastOffset;
450 const size_t lastValue = (localValue + lastOffset) % mapElements.size();
455 mapElements[localValue].first.second = mapElements[lastValue].first.second - lastOffset;
456 mapElements[localValue].second = mapElements[lastValue].second;
458 localValue = lastValue;
460 if (mapElements[lastValue].first.first == 1)
465 const size_t startIndex = mapFunction(key);
467 for (
size_t i = 0u; i < elementOffset + n; ++i)
468 mapElements[(startIndex + i) % mapElements.size()].first.first--;
470 mapElements[(value + elementOffset) % mapElements.size()].first.first = 0;
471 mapElements[(value + elementOffset) % mapElements.size()].second.first = TKey();
472 mapElements[(value + elementOffset) % mapElements.size()].second.second = T();
475 ocean_assert(isConsistent());
481 ocean_assert(
false &&
"This must never happen!");
485 template <
typename TKey,
typename T>
488 ocean_assert(mapSize <= mapElements.size());
489 ocean_assert(isConsistent());
492 for (
size_t n = 0; n < mapElements.size(); ++n)
494 const size_t value = (mapFunction(key) + n) % mapElements.size();
497 if (mapElements[value].first.first == 0)
501 if (mapElements[value].second.first == key)
505 if (mapElements[value].first.first == 1)
512 template <
typename TKey,
typename T>
515 ocean_assert(mapSize <= mapElements.size());
516 ocean_assert(isConsistent());
519 for (
size_t n = 0; n < mapElements.size(); ++n)
521 const size_t value = (mapFunction(key) + n) % mapElements.size();
524 if (mapElements[value].first.first == 0)
528 if (mapElements[value].second.first == key)
530 element = &mapElements[value].second.second;
535 if (mapElements[value].first.first == 1)
542 template <
typename TKey,
typename T>
545 ocean_assert(mapSize <= mapElements.size());
546 ocean_assert(isConsistent());
549 for (
size_t n = 0; n < mapElements.size(); ++n)
551 const size_t value = (mapFunction(key) + n) % mapElements.size();
554 if (mapElements[value].first.first == 0)
558 if (mapElements[value].second.first == key)
560 element = &mapElements[value].second.second;
565 if (mapElements[value].first.first == 1)
572 template <
typename TKey,
typename T>
575 ocean_assert(mapSize <= mapElements.size());
576 ocean_assert(isConsistent());
579 for (
size_t n = 0; n < mapElements.size(); ++n)
581 const size_t value = (mapFunction(key) + n) % mapElements.size();
584 if (mapElements[value].first.first == 0)
588 if (mapElements[value].second.first == key)
589 return mapElements[value].second.second;
592 if (mapElements[value].first.first == 1)
596 ocean_assert(
false &&
"Invalid key!");
597 return mapElements.cbegin()->second.second;
600 template <
typename TKey,
typename T>
603 ocean_assert(isConsistent());
605 for (
typename Elements::iterator i = mapElements.begin(); i != mapElements.end(); ++i)
610 ocean_assert(isConsistent());
613 template <
typename TKey,
typename T>
619 template <
typename TKey,
typename T>
622 return mapElements.size();
625 template <
typename TKey,
typename T>
631 template <
typename TKey,
typename T>
634 if (
this != &hashMap)
644 template <
typename TKey,
typename T>
647 if (
this != &hashMap)
649 mapElements = std::move(hashMap.mapElements);
650 mapSize = hashMap.mapSize;
651 mapFunction = hashMap.mapFunction;
659 template <
typename TKey,
typename T>
665 template <
typename TKey,
typename T>
670 for (
typename Elements::const_iterator i = mapElements.begin(); i != mapElements.end(); ++i)
671 if (i->first.first != 0)
674 return count == mapSize;
This class implements a hash map.
Definition: HashMap.h:26
size_t capacity() const
Returns the capacity of this hash map.
Definition: HashMap.h:620
bool isConsistent() const
Returns whether this hash map is still consistent.
Definition: HashMap.h:666
size_t mapSize
Number of elements this has map holds.
Definition: HashMap.h:188
void clear()
Removes all elements from this has map.
Definition: HashMap.h:601
ValueFunction mapFunction
Value function.
Definition: HashMap.h:191
bool isEmpty() const
Returns whether this hash map is empty.
Definition: HashMap.h:626
HashMap< TKey, T > & operator=(const HashMap< TKey, T > &hashMap)
Assign operator.
Definition: HashMap.h:632
size_t size() const
Returns the number of elements this hash map currently holds.
Definition: HashMap.h:614
bool find(const TKey &key) const
Returns whether this hash map holds a given element.
Definition: HashMap.h:486
const T & element(const TKey &key) const
Returns a specific element of this map.
Definition: HashMap.h:573
std::vector< Element > Elements
Definition of a vector holding the map objects.
Definition: HashMap.h:37
bool remove(const TKey &key)
Removes an element from this hash map.
Definition: HashMap.h:379
size_t(* ValueFunction)(const TKey &key)
Definition of a function pointer returning a hash map value.
Definition: HashMap.h:42
static size_t defaultHashFunction(const TKey &key)
Default hash function for elements supporting an cast size_t cast.
Definition: HashMap.h:660
bool insert(const TKey &key, const T &element, const bool oneOnly=true, const bool extendCapacity=true)
Adds a new element to this hash map.
Definition: HashMap.h:245
std::pair< std::pair< size_t, size_t >, std::pair< TKey, T > > Element
Definition of a pair combining a counter states and an object.
Definition: HashMap.h:32
HashMap(const HashMap< TKey, T > &hashMap)
Copy constructor.
Definition: HashMap.h:195
Elements mapElements
Hash map elements.
Definition: HashMap.h:185
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15