8 #ifndef META_OCEAN_BASE_RING_MAP_H
9 #define META_OCEAN_BASE_RING_MAP_H
30 template <
typename TKey,
typename T,
bool tThreadsafe,
bool tOrderedKeys = false>
33 template <
typename TKey2,
typename T2,
bool tThreadsafe2,
bool tOrderedKeys2>
friend class RingMapT;
70 using ValuePair = std::pair<T, typename KeyList::iterator>;
147 template <AccessMode tAccessMode = AM_MATCH>
176 template <AccessMode tAccessMode = AM_MATCH>
225 template <
bool tThreadSafeSecond>
241 template <
bool tThreadSafeSecond>
267 template <
typename TKey,
typename T,
bool tThreadsafe,
bool tOrderedKeys>
270 *
this = std::move(ringMap);
273 template <
typename TKey,
typename T,
bool tThreadsafe,
bool tOrderedKeys>
279 template <
typename TKey,
typename T,
bool tThreadsafe,
bool tOrderedKeys>
281 storageCapacity_(capacity)
286 template <
typename TKey,
typename T,
bool tThreadsafe,
bool tOrderedKeys>
289 return storageCapacity_;
292 template <
typename TKey,
typename T,
bool tThreadsafe,
bool tOrderedKeys>
296 ocean_assert(isValid());
298 return keyMap_.size();
301 template <
typename TKey,
typename T,
bool tThreadsafe,
bool tOrderedKeys>
305 ocean_assert(isValid());
307 if (capacity == storageCapacity_)
318 else if (capacity < storageCapacity_)
320 while (keyMap_.size() > capacity)
322 ocean_assert(keyMap_.find(keyList_.front()) != keyMap_.cend());
323 keyMap_.erase(keyList_.front());
325 keyList_.pop_front();
329 storageCapacity_ = capacity;
331 ocean_assert(isValid());
334 template <
typename TKey,
typename T,
bool tThreadsafe,
bool tOrderedKeys>
337 T copyElement(element);
339 return insertElement(key, std::move(copyElement), forceOverwrite);
342 template <
typename TKey,
typename T,
bool tThreadsafe,
bool tOrderedKeys>
346 ocean_assert(isValid());
348 if (storageCapacity_ == 0)
354 const typename KeyMap::iterator iMap = keyMap_.find(key);
355 if (iMap != keyMap_.cend())
362 iMap->second.first = std::move(element);
365 keyList_.splice(keyList_.cend(), keyList_, iMap->second.second);
372 ocean_assert(keyMap_.size() <= storageCapacity_);
374 if (keyMap_.size() >= storageCapacity_)
377 const TKey oldKey(keyList_.front());
378 keyList_.pop_front();
380 ocean_assert(keyMap_.find(oldKey) != keyMap_.end());
381 keyMap_.erase(oldKey);
384 ocean_assert(keyMap_.size() < storageCapacity_);
386 keyList_.emplace_back(key);
388 keyMap_[key] = std::make_pair(std::move(element), --keyList_.end());
390 ocean_assert(isValid());
394 template <
typename TKey,
typename T,
bool tThreadsafe,
bool tOrderedKeys>
395 template <typename RingMapT<TKey, T, tThreadsafe, tOrderedKeys>::AccessMode tAccessMode>
399 ocean_assert(isValid());
401 if (storageCapacity_ == 0 || keyMap_.empty())
406 typename KeyMap::const_iterator iMap = keyMap_.find(key);
407 if (iMap == keyMap_.end())
409 if constexpr (tOrderedKeys)
411 if constexpr (tAccessMode == AM_MATCH_OR_HIGHEST)
413 iMap = keyMap_.rbegin().base();
416 else if constexpr (tAccessMode == AM_MATCH_OR_LOWEST)
418 iMap = keyMap_.begin();
422 ocean_assert(tAccessMode == AM_MATCH);
428 ocean_assert(tAccessMode == AM_MATCH);
433 element = iMap->second.first;
435 ocean_assert(isValid());
439 template <
typename TKey,
typename T,
bool tThreadsafe,
bool tOrderedKeys>
443 ocean_assert(isValid());
450 if constexpr (tOrderedKeys)
452 element = keyMap_.rbegin()->second.first;
460 template <
typename TKey,
typename T,
bool tThreadsafe,
bool tOrderedKeys>
464 ocean_assert(isValid());
471 if constexpr (tOrderedKeys)
473 element = keyMap_.begin()->second;
481 template <
typename TKey,
typename T,
bool tThreadsafe,
bool tOrderedKeys>
482 template <typename RingMapT<TKey, T, tThreadsafe, tOrderedKeys>::AccessMode tAccessMode>
486 ocean_assert(isValid());
488 if (storageCapacity_ == 0 || keyMap_.empty())
493 typename KeyMap::iterator iMap = keyMap_.find(key);
494 if (iMap == keyMap_.end())
496 if constexpr (tOrderedKeys)
498 if constexpr (tAccessMode == AM_MATCH_OR_HIGHEST)
500 iMap = keyMap_.rbegin().base();
503 else if constexpr (tAccessMode == AM_MATCH_OR_LOWEST)
505 iMap = keyMap_.begin();
509 ocean_assert(tAccessMode == AM_MATCH);
515 ocean_assert(tAccessMode == AM_MATCH);
520 keyList_.erase(iMap->second.second);
522 element = std::move(iMap->second.first);
526 ocean_assert(isValid());
530 template <
typename TKey,
typename T,
bool tThreadsafe,
bool tOrderedKeys>
534 ocean_assert(isValid());
536 return keyMap_.find(key) != keyMap_.end();
539 template <
typename TKey,
typename T,
bool tThreadsafe,
bool tOrderedKeys>
543 ocean_assert(isValid());
545 std::vector<T> result;
546 result.reserve(keyMap_.size());
548 for (
typename KeyMap::const_iterator iMap = keyMap_.cbegin(); iMap != keyMap_.cend(); ++iMap)
550 result.emplace_back(iMap->second.first);
556 template <
typename TKey,
typename T,
bool tThreadsafe,
bool tOrderedKeys>
560 ocean_assert(isValid());
562 const typename KeyMap::const_iterator iMap = keyMap_.find(key);
564 if (iMap == keyMap_.cend())
570 keyList_.splice(keyList_.cend(), keyList_, iMap->second.second);
572 ocean_assert(isValid());
576 template <
typename TKey,
typename T,
bool tThreadsafe,
bool tOrderedKeys>
580 ocean_assert(isValid());
585 ocean_assert(isValid());
588 template <
typename TKey,
typename T,
bool tThreadsafe,
bool tOrderedKeys>
591 ocean_assert(keyMap_.size() == keyList_.size());
593 return keyMap_.size() <= storageCapacity_ && keyMap_.size() == keyList_.size();
596 template <
typename TKey,
typename T,
bool tThreadsafe,
bool tOrderedKeys>
600 ocean_assert(isValid());
602 return keyMap_.empty();
605 template <
typename TKey,
typename T,
bool tThreadsafe,
bool tOrderedKeys>
608 if (
this != &ringMap)
613 keyMap_ = std::move(ringMap.keyMap_);
614 keyList_ = std::move(ringMap.keyList_);
615 storageCapacity_ = ringMap.storageCapacity_;
616 ringMap.storageCapacity_ = 0;
622 template <
typename TKey,
typename T,
bool tThreadsafe,
bool tOrderedKeys>
623 template <
bool tThreadSafeSecond>
626 if ((
void*)(
this) != (
void*)(&ringMap))
631 keyMap_ = std::move(ringMap.keyMap_);
632 keyList_ = std::move(ringMap.keyList_);
633 storageCapacity_ = ringMap.storageCapacity_;
634 ringMap.storageCapacity_ = 0;
640 template <
typename TKey,
typename T,
bool tThreadsafe,
bool tOrderedKeys>
643 if (
this != &ringMap)
656 template <
typename TKey,
typename T,
bool tThreadsafe,
bool tOrderedKeys>
657 template <
bool tThreadSafeSecond>
660 if ((
void*)(
this) != (
void*)(&ringMap))
This class implements a recursive lock object.
Definition: Lock.h:31
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:32
std::pair< T, typename KeyList::iterator > ValuePair
Definition of a pair combining a value with a list iterator.
Definition: RingMap.h:70
KeyMap keyMap_
The map mapping keys to value pairs.
Definition: RingMap.h:255
bool hasElement(const TKey &key) const
Returns whether this storage container holds a specific element.
Definition: RingMap.h:531
RingMapT< TKey, T, tThreadsafe, tOrderedKeys > & operator=(RingMapT< TKey, T, tThreadsafe, tOrderedKeys > &&ringMap) noexcept
Move operator.
Definition: RingMap.h:606
std::list< TKey > KeyList
Definition of a double-linked list holding the keys in order how they have been inserted.
Definition: RingMap.h:65
MapTyper< tOrderedKeys >::template TMap< TKey, ValuePair > KeyMap
Definition of a map that maps keys to value pairs.
Definition: RingMap.h:75
bool lowestElement(T &element) const
Returns the element with lowest key.
Definition: RingMap.h:461
bool isValid() const
Returns whether the internal states of this storage container is valid.
Definition: RingMap.h:589
RingMapT< TKey, T, tThreadsafe, tOrderedKeys > & operator=(RingMapT< TKey, T, tThreadSafeSecond, tOrderedKeys > &&ringMap) noexcept
Move operator.
Definition: RingMap.h:624
bool refreshElement(const TKey &key)
Checks whether a specified element exists and changes the age of this element.
Definition: RingMap.h:557
RingMapT< TKey, T, tThreadsafe, tOrderedKeys > & operator=(const RingMapT< TKey, T, tThreadsafe, tOrderedKeys > &ringMap)
Copy operator.
Definition: RingMap.h:641
RingMapT< TKey, T, tThreadsafe, tOrderedKeys > & operator=(const RingMapT< TKey, T, tThreadSafeSecond, tOrderedKeys > &ringMap)
Copy operator.
Definition: RingMap.h:658
void clear()
Clears all elements of this storage container.
Definition: RingMap.h:577
RingMapT(const RingMapT< TKey, T, tThreadsafe, tOrderedKeys > &ringMap)
Copy constructor.
Definition: RingMap.h:274
size_t size() const
Returns the number of elements that are currently stored in this container.
Definition: RingMap.h:293
size_t capacity() const
Returns the capacity of this storage container.
Definition: RingMap.h:287
bool highestElement(T &element) const
Returns the element with highest key.
Definition: RingMap.h:440
TKey TypeKey
The data type of the keys that are used to address the data objects.
Definition: RingMap.h:45
KeyList keyList_
The list holding the keys in order how they have been added, oldest keys first.
Definition: RingMap.h:258
std::vector< T > elements() const
Returns all elements of this map as a vector.
Definition: RingMap.h:540
T Type
The data type of the objects that are stored in this container.
Definition: RingMap.h:40
bool element(const TKey &key, T &element) const
Returns an element of this storage container.
Definition: RingMap.h:396
Lock lock_
The container lock.
Definition: RingMap.h:264
bool insertElement(const TKey &key, T &&element, const bool forceOverwrite=false)
Inserts a new element into this storage container.
Definition: RingMap.h:343
bool insertElement(const TKey &key, const T &element, const bool forceOverwrite=false)
Inserts a new element into this storage container.
Definition: RingMap.h:335
bool isEmpty() const
Returns whether this ring map holds at least one element.
Definition: RingMap.h:597
RingMapT()=default
Creates a new ring storage object with no capacity.
size_t storageCapacity_
The capacity of this storage container.
Definition: RingMap.h:261
AccessMode
Definition of individual element access modes.
Definition: RingMap.h:51
@ 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:57
@ 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:55
@ AM_MATCH
The element's key must be a perfect match.
Definition: RingMap.h:53
bool checkoutElement(const TKey &key, T &element)
Returns an element of this storage container and removes the element from the container.
Definition: RingMap.h:483
RingMapT(const size_t capacity)
Creates a new ring storage object with a specified capacity.
Definition: RingMap.h:280
friend class RingMapT
Definition: RingMap.h:33
void setCapacity(const size_t capacity)
Sets or changes the capacity of this storage container.
Definition: RingMap.h:302
RingMapT(RingMapT< TKey, T, tThreadsafe, tOrderedKeys > &&ringMap) noexcept
Move constructor.
Definition: RingMap.h:268
This class implements a recursive scoped lock object that is activated by a boolean template paramete...
Definition: Lock.h:178
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15