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>;
158 template <AccessMode tAccessMode = AM_MATCH>
168 std::optional<T>
elements(
const TKey& key, std::optional<KeyElementPair>& lowerElement, std::optional<KeyElementPair>& higherElement)
const;
196 template <AccessMode tAccessMode = AM_MATCH>
245 template <
bool tThreadSafeSecond>
261 template <
bool tThreadSafeSecond>
287template <
typename TKey,
typename T,
bool tThreadsafe,
bool tOrderedKeys>
290 *
this = std::move(ringMap);
293template <
typename TKey,
typename T,
bool tThreadsafe,
bool tOrderedKeys>
299template <
typename TKey,
typename T,
bool tThreadsafe,
bool tOrderedKeys>
301 storageCapacity_(capacity)
306template <
typename TKey,
typename T,
bool tThreadsafe,
bool tOrderedKeys>
311 return storageCapacity_;
314template <
typename TKey,
typename T,
bool tThreadsafe,
bool tOrderedKeys>
318 ocean_assert(isValid());
320 return keyMap_.size();
323template <
typename TKey,
typename T,
bool tThreadsafe,
bool tOrderedKeys>
327 ocean_assert(isValid());
329 if (capacity == storageCapacity_)
340 else if (capacity < storageCapacity_)
342 while (keyMap_.size() > capacity)
344 ocean_assert(keyMap_.find(keyList_.front()) != keyMap_.cend());
345 keyMap_.erase(keyList_.front());
347 keyList_.pop_front();
351 storageCapacity_ = capacity;
353 ocean_assert(isValid());
356template <
typename TKey,
typename T,
bool tThreadsafe,
bool tOrderedKeys>
359 T copyElement(element);
361 return insertElement(key, std::move(copyElement), forceOverwrite);
364template <
typename TKey,
typename T,
bool tThreadsafe,
bool tOrderedKeys>
368 ocean_assert(isValid());
370 if (storageCapacity_ == 0)
376 const typename KeyMap::iterator iMap = keyMap_.find(key);
377 if (iMap != keyMap_.cend())
384 iMap->second.first = std::move(element);
387 keyList_.splice(keyList_.cend(), keyList_, iMap->second.second);
394 ocean_assert(keyMap_.size() <= storageCapacity_);
396 if (keyMap_.size() >= storageCapacity_)
399 const TKey oldKey(keyList_.front());
400 keyList_.pop_front();
402 ocean_assert(keyMap_.find(oldKey) != keyMap_.end());
403 keyMap_.erase(oldKey);
406 ocean_assert(keyMap_.size() < storageCapacity_);
408 keyList_.emplace_back(key);
410 keyMap_[key] = std::make_pair(std::move(element), --keyList_.end());
412 ocean_assert(isValid());
416template <
typename TKey,
typename T,
bool tThreadsafe,
bool tOrderedKeys>
417template <typename RingMapT<TKey, T, tThreadsafe, tOrderedKeys>::AccessMode tAccessMode>
421 ocean_assert(isValid());
423 if (storageCapacity_ == 0 || keyMap_.empty())
428 typename KeyMap::const_iterator iMap = keyMap_.find(key);
429 if (iMap == keyMap_.end())
431 if constexpr (tOrderedKeys)
433 if constexpr (tAccessMode == AM_MATCH_OR_HIGHEST)
435 iMap = keyMap_.rbegin().base();
438 else if constexpr (tAccessMode == AM_MATCH_OR_LOWEST)
440 iMap = keyMap_.begin();
444 ocean_assert(tAccessMode == AM_MATCH);
450 ocean_assert(tAccessMode == AM_MATCH);
455 element = iMap->second.first;
457 ocean_assert(isValid());
461template <
typename TKey,
typename T,
bool tThreadsafe,
bool tOrderedKeys>
464 if constexpr (!tOrderedKeys)
470 ocean_assert(isValid());
472 if (storageCapacity_ == 0 || keyMap_.empty())
477 lowerElement = std::nullopt;
478 higherElement = std::nullopt;
480 typename KeyMap::const_iterator iMap = keyMap_.find(key);
482 if (iMap != keyMap_.end())
486 return iMap->second.first;
491 iMap = keyMap_.lower_bound(key);
493 if (iMap != keyMap_.end())
498 if (iMap != keyMap_.begin())
500 typename KeyMap::const_iterator iLower = iMap;
503 lowerElement =
KeyElementPair(iLower->first, iLower->second.first);
506 ocean_assert(lowerElement.has_value() || higherElement.has_value());
511template <
typename TKey,
typename T,
bool tThreadsafe,
bool tOrderedKeys>
515 ocean_assert(isValid());
522 if constexpr (tOrderedKeys)
524 element = keyMap_.rbegin()->second.first;
532template <
typename TKey,
typename T,
bool tThreadsafe,
bool tOrderedKeys>
536 ocean_assert(isValid());
543 if constexpr (tOrderedKeys)
545 element = keyMap_.begin()->second.first;
553template <
typename TKey,
typename T,
bool tThreadsafe,
bool tOrderedKeys>
554template <typename RingMapT<TKey, T, tThreadsafe, tOrderedKeys>::AccessMode tAccessMode>
558 ocean_assert(isValid());
560 if (storageCapacity_ == 0 || keyMap_.empty())
565 typename KeyMap::iterator iMap = keyMap_.find(key);
566 if (iMap == keyMap_.end())
568 if constexpr (tOrderedKeys)
570 if constexpr (tAccessMode == AM_MATCH_OR_HIGHEST)
572 iMap = keyMap_.rbegin().base();
575 else if constexpr (tAccessMode == AM_MATCH_OR_LOWEST)
577 iMap = keyMap_.begin();
581 ocean_assert(tAccessMode == AM_MATCH);
587 ocean_assert(tAccessMode == AM_MATCH);
592 keyList_.erase(iMap->second.second);
594 element = std::move(iMap->second.first);
598 ocean_assert(isValid());
602template <
typename TKey,
typename T,
bool tThreadsafe,
bool tOrderedKeys>
606 ocean_assert(isValid());
608 return keyMap_.find(key) != keyMap_.end();
611template <
typename TKey,
typename T,
bool tThreadsafe,
bool tOrderedKeys>
615 ocean_assert(isValid());
617 std::vector<T> result;
618 result.reserve(keyMap_.size());
620 for (
typename KeyMap::const_iterator iMap = keyMap_.cbegin(); iMap != keyMap_.cend(); ++iMap)
622 result.emplace_back(iMap->second.first);
628template <
typename TKey,
typename T,
bool tThreadsafe,
bool tOrderedKeys>
632 ocean_assert(isValid());
634 const typename KeyMap::const_iterator iMap = keyMap_.find(key);
636 if (iMap == keyMap_.cend())
642 keyList_.splice(keyList_.cend(), keyList_, iMap->second.second);
644 ocean_assert(isValid());
648template <
typename TKey,
typename T,
bool tThreadsafe,
bool tOrderedKeys>
652 ocean_assert(isValid());
657 ocean_assert(isValid());
660template <
typename TKey,
typename T,
bool tThreadsafe,
bool tOrderedKeys>
663 ocean_assert(keyMap_.size() == keyList_.size());
665 return keyMap_.size() <= storageCapacity_ && keyMap_.size() == keyList_.size();
668template <
typename TKey,
typename T,
bool tThreadsafe,
bool tOrderedKeys>
672 ocean_assert(isValid());
674 return keyMap_.empty();
677template <
typename TKey,
typename T,
bool tThreadsafe,
bool tOrderedKeys>
680 if (
this != &ringMap)
683 keyMap_ = std::move(ringMap.keyMap_);
684 keyList_ = std::move(ringMap.keyList_);
685 storageCapacity_ = ringMap.storageCapacity_;
686 ringMap.storageCapacity_ = 0;
692template <
typename TKey,
typename T,
bool tThreadsafe,
bool tOrderedKeys>
693template <
bool tThreadSafeSecond>
696 if ((
void*)(
this) != (
void*)(&ringMap))
700 keyMap_ = std::move(ringMap.keyMap_);
701 keyList_ = std::move(ringMap.keyList_);
702 storageCapacity_ = ringMap.storageCapacity_;
703 ringMap.storageCapacity_ = 0;
709template <
typename TKey,
typename T,
bool tThreadsafe,
bool tOrderedKeys>
712 if (
this != &ringMap)
724template <
typename TKey,
typename T,
bool tThreadsafe,
bool tOrderedKeys>
725template <
bool tThreadSafeSecond>
728 if ((
void*)(
this) != (
void*)(&ringMap))
This class implements a scoped lock object that locks two lock objects in a deterministic order based...
Definition Lock.h:341
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:275
bool hasElement(const TKey &key) const
Returns whether this storage container holds a specific element.
Definition RingMap.h:603
RingMapT< TKey, T, tThreadsafe, tOrderedKeys > & operator=(RingMapT< TKey, T, tThreadsafe, tOrderedKeys > &&ringMap) noexcept
Move operator.
Definition RingMap.h:678
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:533
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:284
bool isValid() const
Returns whether the internal states of this storage container is valid.
Definition RingMap.h:661
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:694
bool refreshElement(const TKey &key)
Checks whether a specified element exists and changes the age of this element.
Definition RingMap.h:629
RingMapT< TKey, T, tThreadsafe, tOrderedKeys > & operator=(const RingMapT< TKey, T, tThreadsafe, tOrderedKeys > &ringMap)
Copy operator.
Definition RingMap.h:710
RingMapT< TKey, T, tThreadsafe, tOrderedKeys > & operator=(const RingMapT< TKey, T, tThreadSafeSecond, tOrderedKeys > &ringMap)
Copy operator.
Definition RingMap.h:726
void clear()
Clears all elements of this storage container.
Definition RingMap.h:649
RingMapT(const RingMapT< TKey, T, tThreadsafe, tOrderedKeys > &ringMap)
Copy constructor.
Definition RingMap.h:294
size_t size() const
Returns the number of elements that are currently stored in this container.
Definition RingMap.h:315
size_t capacity() const
Returns the capacity of this storage container.
Definition RingMap.h:307
bool highestElement(T &element) const
Returns the element with highest key.
Definition RingMap.h:512
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:462
KeyList keyList_
The list holding the keys in order how they have been added, oldest keys first.
Definition RingMap.h:278
std::vector< T > elements() const
Returns all elements of this map as a vector.
Definition RingMap.h:612
bool element(const TKey &key, T &element) const
Returns an element of this storage container.
Definition RingMap.h:418
bool insertElement(const TKey &key, T &&element, const bool forceOverwrite=false)
Inserts a new element into this storage container.
Definition RingMap.h:365
bool insertElement(const TKey &key, const T &element, const bool forceOverwrite=false)
Inserts a new element into this storage container.
Definition RingMap.h:357
bool isEmpty() const
Returns whether this ring map holds at least one element.
Definition RingMap.h:669
RingMapT()=default
Creates a new ring storage object with no capacity.
size_t storageCapacity_
The capacity of this storage container.
Definition RingMap.h:281
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:555
RingMapT(const size_t capacity)
Creates a new ring storage object with a specified capacity.
Definition RingMap.h:300
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:324
RingMapT(RingMapT< TKey, T, tThreadsafe, tOrderedKeys > &&ringMap) noexcept
Move constructor.
Definition RingMap.h:288
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