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!");
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!");
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!");
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)
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)
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)
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;
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