8#ifndef META_OCEAN_BASE_RING_MAP_H
9#define META_OCEAN_BASE_RING_MAP_H
31template <
typename TKey,
typename T,
bool tThreadsafe,
bool tOrderedKeys = false>
34 template <
typename TKey2,
typename T2,
bool tThreadsafe2,
bool tOrderedKeys2>
friend class RingMapT;
76 using ValuePair = std::pair<T, typename KeyList::iterator>;
153 template <AccessMode tAccessMode = AM_MATCH>
163 std::optional<T>
elements(
const TKey& key, std::optional<KeyElementPair>& lowerElement, std::optional<KeyElementPair>& higherElement)
const;
191 template <AccessMode tAccessMode = AM_MATCH>
240 template <
bool tThreadSafeSecond>
256 template <
bool tThreadSafeSecond>
282template <
typename TKey,
typename T,
bool tThreadsafe,
bool tOrderedKeys>
285 *
this = std::move(ringMap);
288template <
typename TKey,
typename T,
bool tThreadsafe,
bool tOrderedKeys>
294template <
typename TKey,
typename T,
bool tThreadsafe,
bool tOrderedKeys>
296 storageCapacity_(capacity)
301template <
typename TKey,
typename T,
bool tThreadsafe,
bool tOrderedKeys>
304 return storageCapacity_;
307template <
typename TKey,
typename T,
bool tThreadsafe,
bool tOrderedKeys>
311 ocean_assert(isValid());
313 return keyMap_.size();
316template <
typename TKey,
typename T,
bool tThreadsafe,
bool tOrderedKeys>
320 ocean_assert(isValid());
322 if (capacity == storageCapacity_)
333 else if (capacity < storageCapacity_)
335 while (keyMap_.size() > capacity)
337 ocean_assert(keyMap_.find(keyList_.front()) != keyMap_.cend());
338 keyMap_.erase(keyList_.front());
340 keyList_.pop_front();
344 storageCapacity_ = capacity;
346 ocean_assert(isValid());
349template <
typename TKey,
typename T,
bool tThreadsafe,
bool tOrderedKeys>
352 T copyElement(element);
354 return insertElement(key, std::move(copyElement), forceOverwrite);
357template <
typename TKey,
typename T,
bool tThreadsafe,
bool tOrderedKeys>
361 ocean_assert(isValid());
363 if (storageCapacity_ == 0)
369 const typename KeyMap::iterator iMap = keyMap_.find(key);
370 if (iMap != keyMap_.cend())
377 iMap->second.first = std::move(element);
380 keyList_.splice(keyList_.cend(), keyList_, iMap->second.second);
387 ocean_assert(keyMap_.size() <= storageCapacity_);
389 if (keyMap_.size() >= storageCapacity_)
392 const TKey oldKey(keyList_.front());
393 keyList_.pop_front();
395 ocean_assert(keyMap_.find(oldKey) != keyMap_.end());
396 keyMap_.erase(oldKey);
399 ocean_assert(keyMap_.size() < storageCapacity_);
401 keyList_.emplace_back(key);
403 keyMap_[key] = std::make_pair(std::move(element), --keyList_.end());
405 ocean_assert(isValid());
409template <
typename TKey,
typename T,
bool tThreadsafe,
bool tOrderedKeys>
410template <typename RingMapT<TKey, T, tThreadsafe, tOrderedKeys>::AccessMode tAccessMode>
414 ocean_assert(isValid());
416 if (storageCapacity_ == 0 || keyMap_.empty())
421 typename KeyMap::const_iterator iMap = keyMap_.find(key);
422 if (iMap == keyMap_.end())
424 if constexpr (tOrderedKeys)
426 if constexpr (tAccessMode == AM_MATCH_OR_HIGHEST)
428 iMap = keyMap_.rbegin().base();
431 else if constexpr (tAccessMode == AM_MATCH_OR_LOWEST)
433 iMap = keyMap_.begin();
437 ocean_assert(tAccessMode == AM_MATCH);
443 ocean_assert(tAccessMode == AM_MATCH);
448 element = iMap->second.first;
450 ocean_assert(isValid());
454template <
typename TKey,
typename T,
bool tThreadsafe,
bool tOrderedKeys>
457 if constexpr (!tOrderedKeys)
463 ocean_assert(isValid());
465 if (storageCapacity_ == 0 || keyMap_.empty())
470 lowerElement = std::nullopt;
471 higherElement = std::nullopt;
473 typename KeyMap::const_iterator iMap = keyMap_.find(key);
475 if (iMap != keyMap_.end())
479 return iMap->second.first;
484 iMap = keyMap_.lower_bound(key);
486 if (iMap != keyMap_.end())
491 if (iMap != keyMap_.begin())
493 typename KeyMap::const_iterator iLower = iMap;
496 lowerElement =
KeyElementPair(iLower->first, iLower->second.first);
499 ocean_assert(lowerElement.has_value() || higherElement.has_value());
504template <
typename TKey,
typename T,
bool tThreadsafe,
bool tOrderedKeys>
508 ocean_assert(isValid());
515 if constexpr (tOrderedKeys)
517 element = keyMap_.rbegin()->second.first;
525template <
typename TKey,
typename T,
bool tThreadsafe,
bool tOrderedKeys>
529 ocean_assert(isValid());
536 if constexpr (tOrderedKeys)
538 element = keyMap_.begin()->second;
546template <
typename TKey,
typename T,
bool tThreadsafe,
bool tOrderedKeys>
547template <typename RingMapT<TKey, T, tThreadsafe, tOrderedKeys>::AccessMode tAccessMode>
551 ocean_assert(isValid());
553 if (storageCapacity_ == 0 || keyMap_.empty())
558 typename KeyMap::iterator iMap = keyMap_.find(key);
559 if (iMap == keyMap_.end())
561 if constexpr (tOrderedKeys)
563 if constexpr (tAccessMode == AM_MATCH_OR_HIGHEST)
565 iMap = keyMap_.rbegin().base();
568 else if constexpr (tAccessMode == AM_MATCH_OR_LOWEST)
570 iMap = keyMap_.begin();
574 ocean_assert(tAccessMode == AM_MATCH);
580 ocean_assert(tAccessMode == AM_MATCH);
585 keyList_.erase(iMap->second.second);
587 element = std::move(iMap->second.first);
591 ocean_assert(isValid());
595template <
typename TKey,
typename T,
bool tThreadsafe,
bool tOrderedKeys>
599 ocean_assert(isValid());
601 return keyMap_.find(key) != keyMap_.end();
604template <
typename TKey,
typename T,
bool tThreadsafe,
bool tOrderedKeys>
608 ocean_assert(isValid());
610 std::vector<T> result;
611 result.reserve(keyMap_.size());
613 for (
typename KeyMap::const_iterator iMap = keyMap_.cbegin(); iMap != keyMap_.cend(); ++iMap)
615 result.emplace_back(iMap->second.first);
621template <
typename TKey,
typename T,
bool tThreadsafe,
bool tOrderedKeys>
625 ocean_assert(isValid());
627 const typename KeyMap::const_iterator iMap = keyMap_.find(key);
629 if (iMap == keyMap_.cend())
635 keyList_.splice(keyList_.cend(), keyList_, iMap->second.second);
637 ocean_assert(isValid());
641template <
typename TKey,
typename T,
bool tThreadsafe,
bool tOrderedKeys>
645 ocean_assert(isValid());
650 ocean_assert(isValid());
653template <
typename TKey,
typename T,
bool tThreadsafe,
bool tOrderedKeys>
656 ocean_assert(keyMap_.size() == keyList_.size());
658 return keyMap_.size() <= storageCapacity_ && keyMap_.size() == keyList_.size();
661template <
typename TKey,
typename T,
bool tThreadsafe,
bool tOrderedKeys>
665 ocean_assert(isValid());
667 return keyMap_.empty();
670template <
typename TKey,
typename T,
bool tThreadsafe,
bool tOrderedKeys>
673 if (
this != &ringMap)
678 keyMap_ = std::move(ringMap.keyMap_);
679 keyList_ = std::move(ringMap.keyList_);
680 storageCapacity_ = ringMap.storageCapacity_;
681 ringMap.storageCapacity_ = 0;
687template <
typename TKey,
typename T,
bool tThreadsafe,
bool tOrderedKeys>
688template <
bool tThreadSafeSecond>
691 if ((
void*)(
this) != (
void*)(&ringMap))
696 keyMap_ = std::move(ringMap.keyMap_);
697 keyList_ = std::move(ringMap.keyList_);
698 storageCapacity_ = ringMap.storageCapacity_;
699 ringMap.storageCapacity_ = 0;
705template <
typename TKey,
typename T,
bool tThreadsafe,
bool tOrderedKeys>
708 if (
this != &ringMap)
721template <
typename TKey,
typename T,
bool tThreadsafe,
bool tOrderedKeys>
722template <
bool tThreadSafeSecond>
725 if ((
void*)(
this) != (
void*)(&ringMap))
Helper class allowing to define an ordered or unordered map based on the template parameter 'tOrdered...
Definition DataType.h:518
This class implements a data storage map that stores the data elements in a ring manner.
Definition RingMap.h:33
std::pair< T, typename KeyList::iterator > ValuePair
Definition of a pair combining a value with a list iterator.
Definition RingMap.h:76
KeyMap keyMap_
The map mapping keys to value pairs.
Definition RingMap.h:270
bool hasElement(const TKey &key) const
Returns whether this storage container holds a specific element.
Definition RingMap.h:596
RingMapT< TKey, T, tThreadsafe, tOrderedKeys > & operator=(RingMapT< TKey, T, tThreadsafe, tOrderedKeys > &&ringMap) noexcept
Move operator.
Definition RingMap.h:671
std::list< TKey > KeyList
Definition of a double-linked list holding the keys in order how they have been inserted.
Definition RingMap.h:71
bool lowestElement(T &element) const
Returns the element with lowest key.
Definition RingMap.h:526
std::pair< TKey, T > KeyElementPair
Definition of a pair combining a key with an element.
Definition RingMap.h:51
T Type
The data type of the objects that are stored in this container.
Definition RingMap.h:41
TemplatedLock< tThreadsafe > lock_
The container lock.
Definition RingMap.h:279
bool isValid() const
Returns whether the internal states of this storage container is valid.
Definition RingMap.h:654
TKey TypeKey
The data type of the keys that are used to address the data objects.
Definition RingMap.h:46
RingMapT< TKey, T, tThreadsafe, tOrderedKeys > & operator=(RingMapT< TKey, T, tThreadSafeSecond, tOrderedKeys > &&ringMap) noexcept
Move operator.
Definition RingMap.h:689
bool refreshElement(const TKey &key)
Checks whether a specified element exists and changes the age of this element.
Definition RingMap.h:622
RingMapT< TKey, T, tThreadsafe, tOrderedKeys > & operator=(const RingMapT< TKey, T, tThreadsafe, tOrderedKeys > &ringMap)
Copy operator.
Definition RingMap.h:706
RingMapT< TKey, T, tThreadsafe, tOrderedKeys > & operator=(const RingMapT< TKey, T, tThreadSafeSecond, tOrderedKeys > &ringMap)
Copy operator.
Definition RingMap.h:723
void clear()
Clears all elements of this storage container.
Definition RingMap.h:642
RingMapT(const RingMapT< TKey, T, tThreadsafe, tOrderedKeys > &ringMap)
Copy constructor.
Definition RingMap.h:289
size_t size() const
Returns the number of elements that are currently stored in this container.
Definition RingMap.h:308
size_t capacity() const
Returns the capacity of this storage container.
Definition RingMap.h:302
bool highestElement(T &element) const
Returns the element with highest key.
Definition RingMap.h:505
std::optional< T > elements(const TKey &key, std::optional< KeyElementPair > &lowerElement, std::optional< KeyElementPair > &higherElement) const
Returns either the specified element if it exists or the two elements neighboring to the specified el...
Definition RingMap.h:455
KeyList keyList_
The list holding the keys in order how they have been added, oldest keys first.
Definition RingMap.h:273
std::vector< T > elements() const
Returns all elements of this map as a vector.
Definition RingMap.h:605
bool element(const TKey &key, T &element) const
Returns an element of this storage container.
Definition RingMap.h:411
bool insertElement(const TKey &key, T &&element, const bool forceOverwrite=false)
Inserts a new element into this storage container.
Definition RingMap.h:358
bool insertElement(const TKey &key, const T &element, const bool forceOverwrite=false)
Inserts a new element into this storage container.
Definition RingMap.h:350
bool isEmpty() const
Returns whether this ring map holds at least one element.
Definition RingMap.h:662
RingMapT()=default
Creates a new ring storage object with no capacity.
size_t storageCapacity_
The capacity of this storage container.
Definition RingMap.h:276
AccessMode
Definition of individual element access modes.
Definition RingMap.h:57
@ AM_MATCH_OR_LOWEST
The element with lowest key is returned if no perfect match can be found, only if 'tOrderedKeys == tr...
Definition RingMap.h:63
@ AM_MATCH_OR_HIGHEST
The element with highest key is returned if no perfect match can be found, only if 'tOrderedKeys == t...
Definition RingMap.h:61
@ AM_MATCH
The element's key must be a perfect match.
Definition RingMap.h:59
bool checkoutElement(const TKey &key, T &element)
Returns an element of this storage container and removes the element from the container.
Definition RingMap.h:548
RingMapT(const size_t capacity)
Creates a new ring storage object with a specified capacity.
Definition RingMap.h:295
friend class RingMapT
Definition RingMap.h:34
void setCapacity(const size_t capacity)
Sets or changes the capacity of this storage container.
Definition RingMap.h:317
RingMapT(RingMapT< TKey, T, tThreadsafe, tOrderedKeys > &&ringMap) noexcept
Move constructor.
Definition RingMap.h:283
typename MapTyper< tOrderedKeys >::template TMap< TKey, ValuePair > KeyMap
Definition of a map that maps keys to value pairs.
Definition RingMap.h:81
This class implements a template-based recursive lock object.
Definition Lock.h:99
This class implements a recursive scoped lock object that is activated by a boolean template paramete...
Definition Lock.h:190
The namespace covering the entire Ocean framework.
Definition Accessor.h:15