8#ifndef HPP_ALIB_MONOMEM_CONTAINERS_LRUCACHETABLE
9#define HPP_ALIB_MONOMEM_CONTAINERS_LRUCACHETABLE 1
13#include "alib/lang/tmp.hpp"
19namespace alib {
namespace containers {
136template<
typename TAllocator,
137 typename TValueDescriptor,
138 typename THash = std::hash <typename TValueDescriptor::KeyType>,
139 typename TEqual = std::equal_to<typename TValueDescriptor::KeyType> >
141#if ALIB_DEBUG_CRITICAL_SECTIONS
162 using KeyType =
typename TValueDescriptor::KeyType;
215 template<
typename TConstOrMutable>
337 return !(*
this == other);
364 template<
typename... TArgs>
368 return *
new( &
entry->
data ) TConstOrMutable(std::forward<TArgs>(args)...);
384 return TValueDescriptor().Key(
entry->
data);
393 return TValueDescriptor().Mapped(
entry->
data);
491 result+=
lists[i].count();
506 auto newCapacity= newQtyLists * newQtyEntriesPerList;
524 for(
integer i = 0; i < newQtyLists; ++i )
585 const size_t keyHash = THash{}(key);
588 auto& list =
lists[listIdx];
590 Entry* prev2 =
nullptr;
591 Entry* prev1 =
reinterpret_cast<Entry*
>(&list);
592 Entry* actual = list.first();
596 while( actual !=
nullptr )
599 && TEqual{}(TValueDescriptor().Key(actual->
data), key) )
602 if (prev1 !=
nullptr)
605 actual->
next( list.next() );
608 return std::make_pair(
true,
Iterator(actual,
this, listIdx ));
612 actual= actual->
next();
621 prev2->next(
nullptr );
622 prev1->
data.~StoredType();
624 prev1->
next( list.next() );
626 return std::make_pair(
false,
Iterator(list.first(),
this, listIdx ));
634 newEntry->next(list.next());
636 return std::make_pair(
false,
Iterator(list.first(),
this, listIdx ));
667template<
typename TAllocator,
668 typename TKey,
typename TMapped,
669 typename THash = std::hash <TKey>,
670 typename TEqual = std::equal_to<TKey> >
703template<
typename TAllocator,
705 typename THash = std::hash <T>,
706 typename TEqual = std::equal_to<T> >
714template<
typename TAllocator,
715 typename TValueDescriptor,
716 typename THash = std::hash <typename TValueDescriptor::KeyType>,
717 typename TEqual = std::equal_to<typename TValueDescriptor::KeyType> >
722template<
typename TAllocator,
723 typename TKey,
typename TMapped,
724 typename THash = std::hash <TKey>,
725 typename TEqual = std::equal_to<TKey> >
730template<
typename TAllocator,
732 typename THash = std::hash <T>,
733 typename TEqual = std::equal_to<T> >
TConstOrMutable * operator->() const
MappedType & Mapped() const
LRUCacheTable * table
The pointer to the actual cache entry.
TConstOrMutable & Construct(TArgs &&... args) const
TConstOrMutable value_type
Implementation of std::iterator_traits.
integer listIdx
The actual list.
TConstOrMutable & Value() const
TForwardIterator(const TMutable &mutableIt)
const KeyType & Key() const
std::forward_iterator_tag iterator_category
Implementation of std::iterator_traits.
bool operator!=(TForwardIterator other) const
TForwardIterator(LRUCacheTable *pTable, integer pListIdx)
TForwardIterator & operator=(const TForwardIterator &other)=default
bool operator==(TForwardIterator other) const
Entry * entry
The pointer to the actual cache entry.
TForwardIterator()
Default constructor.
TConstOrMutable & operator*() const
TForwardIterator & operator++()
TForwardIterator operator++(int)
TForwardIterator(Entry *pEntry, LRUCacheTable *pTable, integer pListIdx)
TConstOrMutable * pointer
Implementation of std::iterator_traits.
integer difference_type
Implementation of std::iterator_traits.
TConstOrMutable & reference
Implementation of std::iterator_traits.
TForwardIterator(const TForwardIterator &other)=default
ConstIterator cend() const
LRUCacheTable(integer tableSize, integer listSize)
TAllocator AllocatorType
Type definition publishing template parameter TAllocator.
typename TValueDescriptor::MappedType MappedType
integer CapacityEntries() const
void Reserve(integer newQtyLists, integer newQtyEntriesPerList)
integer Size() const noexcept
THash HashType
Type definition publishing template parameter THash.
ConstIterator end() const
typename TValueDescriptor::KeyType KeyType
Entry * elementPool
Pointer to reserved memory for elements. Size is capacityLists x capacityEntries.
integer CapacityLists() const
LRUCacheTable(TAllocator &pAllocator, integer tableSize, integer listSize)
~LRUCacheTable()
Destructor. Calls the destructor of each cached value.
ForwardList * lists
Array of size capacityLists that holds the list..
integer capacityLists
The number of LRU-lists.
ConstIterator cbegin() const
TEqual EqualType
Type definition publishing template parameter TEqual.
void Clear()
Clears this cache.
std::pair< bool, Iterator > Try(const KeyType &key)
Entry * nextPoolElement
The next element to use with a cache-miss on a list that is not of full length, yet.
TForwardIterator< StoredType > Iterator
The mutable iterator over the cache entries.
TValueDescriptor DescriptorType
Type definition publishing template parameter TValueDescriptor.
ConstIterator begin() const
integer capacityEntries
The number of entries collected in each LRU-list.
typename TValueDescriptor::StoredType StoredType
TForwardIterator< const StoredType > ConstIterator
The constant iterator over the cache entries.
#define ALIB_ASSERT_MODULE(modulename)
#define ALIB_WARNINGS_RESTORE
#define ATMP_EQ( T, TEqual)
#define ALIB_ASSERT_ERROR(cond,...)
#define ALIB_WARNINGS_ALLOW_UNSAFE_BUFFER_USAGE
#define ATMP_SELECT_IF_1TP(TParam, ...)
#define ALIB_DEBUG_CRITICAL_SECTIONS
static ALIB_FORCE_INLINE void Destruct(T &object)
lang::integer integer
Type alias in namespace alib.
The node type of the cache lists.
StoredType data
The data cached.
size_t hashCode
This entries hash code (calculated once on insertion)
AllocatorInterface< TAllocator > AI() const noexcept
ALIB_FORCE_INLINE DbgCriticalSections(const char *name)
void reset() noexcept
Resets this list to zero elements.
TElement * first() const noexcept
bool isEmpty() const noexcept
void next(SidiNodeBase *p)