8#ifndef HPP_ALIB_MONOMEM_DETAIL_HASHTABLEBASE
9#define HPP_ALIB_MONOMEM_DETAIL_HASHTABLEBASE 1
11#if !defined(HPP_ALIB_MONOMEM_CONTAINERS_HASHTABLE)
12# error "ALib sources with ending '.inl' must not be included from outside."
22template<
typename TAllocator,
23 typename TValueDescriptor,
33#if ALIB_SIZEOF_INTEGER == 4
35#elif ALIB_SIZEOF_INTEGER == 8
39 #error "Unknown size of integer"
52template<
typename TStored>
75template<
typename TStored>
98template<
typename TValueDescriptor, lang::Caching THashCaching>
106 && !std::is_arithmetic<typename TValueDescriptor::KeyType>::value );
129template<
typename TAllocator,
130 typename TValueDescriptor,
137 typename HTElementSelector<TValueDescriptor, THashCaching>::Type >
144 using T =
typename TValueDescriptor::StoredType;
147 using TKey =
typename TValueDescriptor::KeyType;
150 using TMapped =
typename TValueDescriptor::MappedType;
180 :: template HookType<TAllocator,
200 if constexpr ( Element::CachedHashCodes )
201 return elem->getCached();
203 return THash{}( TValueDescriptor().Key( elem->value ) );
211 Element* elem= recyclerType::Get();
212 elem->fixHashCode( hashCode );
231 template<
typename TConstOrMutable>
236 friend class containers::HashTable<TAllocator, TValueDescriptor,THash,TEqual,THashCaching,TRecycling>;
269 if( !pTable->buckets[pBbucketIx].isEmpty() )
272 element = pTable->buckets[pBbucketIx].first();
386 return !(*
this == other);
421 return TValueDescriptor().Key(
element->value );
430 return TValueDescriptor().Mapped(
element->value );
447 template<
typename TConstOrMutable>
452 friend class containers::HashTable<TAllocator,TValueDescriptor,THash,TEqual,THashCaching,TRecycling>;
539 return !(*
this == other);
573 return TValueDescriptor().Key(
element->value );
582 return TValueDescriptor().Mapped(
element->value );
620 && TEqual{}( TValueDescriptor().Key( lhs->value ),
621 TValueDescriptor().Key( rhs->value ) );
632 return ( !Element::CachedHashCodes || ( keyHashCode ==
getHashCode(elem) ) )
633 && TEqual{}( TValueDescriptor().Key( elem->value ), key );
649 return static_cast<Element*
>(result);
651 result= result->next();
670 while( result->hasNext() && !
areEqual( result->next(), key, keyHashCode ) )
671 result= result->
next();
673 return result->hasNext() ? result :
nullptr;
687 if( previous ==
nullptr )
717 float pBaseLoadFactor,
718 float pMaxLoadFactor )
736 float pMaxLoadFactor )
756 float pBaseLoadFactor = 1.0,
757 float pMaxLoadFactor = 2.0 )
783 if ( first !=
nullptr )
784 recyclerType::DisposeList(first);
806 if (
buckets[bucketIdx].first() )
808 recyclerType::RecycleList(
buckets[bucketIdx].first() );
847 newMinBucketCount= (std::max)( newMinBucketCount,
861 "Internal error: Rehashing to equal or smaller bucket count." )
868 for(
uinteger bucketIdx= 0 ; bucketIdx < oldBucketCount ; ++bucketIdx )
872 if( first !=
nullptr )
885 while( actual !=
nullptr )
896 recyclerType::template RecycleChunk<FwdList>( oldData, oldBucketCount );
913 const auto hashCode= THash{}(key);
916 if( element ==
nullptr )
920 auto result= std::make_pair(
TIterator<T>(
this, bucketIdx, element ),
925 if( result.second.element ==
nullptr
926 || !
areEqual( result.second.element, key, hashCode ) )
951 if (element !=
nullptr )
952 return std::make_pair(
TIterator<T>(
this, bucketIdx, element ),
false);
959 return std::make_pair(
TIterator<T>(
this, bucketIdx, newElement ) ,
true);
977 auto* elem=
findElement( bucketIdx, key, hashCode );
978 if( elem !=
nullptr )
979 return std::make_pair(
TIterator<T>(
this, bucketIdx, elem ),
false);
987 return std::make_pair(
TIterator<T>(
this, bucketIdx, newElem ),
true);
TConstOrMutable * operator->() const
TIterator(THashtable *pTable, uinteger pBbucketIx, Element *pElement)
ATMP_IF_T_F(!ATMP_IS_CONST(TConstOrMutable), HashTableBase, const HashTableBase) THashtable
Const or mutable version of HashTableBase.
TConstOrMutable & Value() const
TIterator(THashtable *pTable, uinteger pBbucketIx)
TMapped value_type
Implementation of std::iterator_traits.
std::forward_iterator_tag iterator_category
Implementation of std::iterator_traits.
TIterator & operator=(const TIterator &other)=default
void repair()
Moves an iterator with a nulled element pointer to the next element.
TIterator(const TIterator &other)=default
THashtable * table
The pointer to the hash table.
TMutable TIterator(const TMutable &mutableIt)
TIterator operator++(int)
bool operator==(TIterator other) const
TConstOrMutable & operator*() const
bool operator!=(TIterator other) const
Element * element
The pointer to the actual element.
TConstOrMutable * pointer
Implementation of std::iterator_traits.
integer difference_type
Implementation of std::iterator_traits.
TConstOrMutable & reference
Implementation of std::iterator_traits.
uinteger bucketIdx
The actual bucket index.
TIterator()=default
Default constructor. Keeps everything uninitialized.
TLocalIterator & operator++()
TLocalIterator(const TLocalIterator &other)=default
TConstOrMutable * operator->() const
TLocalIterator operator++(int)
TConstOrMutable value_type
Implementation of std::iterator_traits.
TConstOrMutable & Value() const
TLocalIterator & operator=(const TLocalIterator &other)=default
TLocalIterator(uinteger pBucketIdx, Element *pElement)
std::forward_iterator_tag iterator_category
Implementation of std::iterator_traits.
TConstOrMutable & operator*() const
bool operator!=(TLocalIterator other) const
Element * element
The pointer to the actual element.
TConstOrMutable * pointer
Implementation of std::iterator_traits.
TMutable TLocalIterator(const TMutable &mutableIt)
integer difference_type
Implementation of std::iterator_traits.
TConstOrMutable & reference
Implementation of std::iterator_traits.
uinteger bucketIdx
The index of the bucket that this iterator works on.
TLocalIterator()
Default constructor.
bool operator==(TLocalIterator other) const
#define ATMP_IF_T_F( Cond, T, F)
#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 ATMP_T_IF(T, Cond)
ALIB_API const uinteger PRIME_NUMBERS[PRIME_TABLE_SIZE]
static constexpr int PRIME_TABLE_SIZE
The size of the static table of prime numbers. Depends on the platform.
ALIB_API void * DUMMY_BUCKET
A dummy bucket used for nulled hash tables to avoid otherwise necessary checks.
Caching
Denotes if a cache mechanism is enabled or disabled.
@ Enabled
Caching is enabled.
lang::uinteger uinteger
Type alias in namespace alib.
containers::HashTable< TAllocator, TValueDescriptor, THash, TEqual, THashCaching, TRecycling > HashTable
Type alias in namespace alib. See type definition alib::containers::HashSet.
lang::integer integer
Type alias in namespace alib.
size_t hashCode
The cached hash code.
TStored value
The custom data stored in nodes of this table.
static constexpr bool CachedHashCodes
TMP constant that denotes that hash codes are cached.
void fixHashCode(size_t pHashCode)
static constexpr bool IsCachingHashes()
ATMP_IF_T_F( IsCachingHashes(), HTElementCached< typename TValueDescriptor::StoredType >, HTElementUncached< typename TValueDescriptor::StoredType >) Type
The type stored in a hash table's bucket list.
TStored value
The custom data stored in nodes of this table.
static constexpr bool CachedHashCodes
TMP constant that denotes that hash codes are not cached.
void fixHashCode(size_t pHashCode)
static constexpr bool IsCachingHashes()
integer sizeLimitToRehash
Calculated once with rehash. Product of maxLoadFactor and bucketCount.
Element * findElement(uinteger bucketIdx, const TKey &key, size_t keyHashCode) const
uinteger bucketCount
The number of buckets managed by this tree.
bool areEqual(Element *lhs, Element *rhs) const
std::pair< TIterator< T >, bool > insertIfNotExists(const TKey &key, size_t hashCode)
size_t increaseSize(integer increase, const size_t hashCode=0)
typename HTElementSelector< TValueDescriptor, THashCaching >::Type Element
The type stored in the bucket of class HashTable.
typename TValueDescriptor::MappedType TMapped
Type definition publishing template parameter TKey.
integer size
The number of elements stored.
void rehash(uinteger newMinBucketCount)
typename RecyclingSelector< TRecycling >::template Type< TAllocator, typename HTElementSelector< TValueDescriptor, THashCaching >::Type > recyclerType
Type of a single linked node list.
std::pair< TIterator< T >, TIterator< T > > findRange(const TKey &key)
HashTableBase(float pBaseLoadFactor, float pMaxLoadFactor)
Element * allocElement(const size_t hashCode)
typename detail::RecyclingSelector< TRecycling > ::template HookType< TAllocator, typename HTElementSelector< TValueDescriptor, THashCaching >::Type > SharedRecyclerType
FwdList * buckets
The list of buckets.
HashTableBase(TSharedRecycler &pSharedRecycler, float pBaseLoadFactor=1.0, float pMaxLoadFactor=2.0)
std::pair< TIterator< T >, bool > insertOrGet(const TKey &key, size_t hashCode)
typename RecyclingSelector< TRecycling >::template Type< TAllocator, typename HTElementSelector< TValueDescriptor, THashCaching >::Type > base
Our base type.
HashTableBase(TAllocator &pAllocator, float pBaseLoadFactor, float pMaxLoadFactor)
float baseLoadFactor
The load factor that is set when the table is rehashed automatically.
static size_t getHashCode(Element *elem)
float maxLoadFactor
The maximum quotient of size and bucketCount that triggers a rehash.
uinteger insertInBucket(Element *element, const size_t hashCode)
typename TValueDescriptor::StoredType T
Type definition publishing template parameter T.
void clear()
Destructs and removes all entries from this hash table.
void setMaxLoadFactor(float pMaxLoadFactor)
typename TValueDescriptor::KeyType TKey
Type definition publishing template parameter TKey.
Node * findElementBefore(uinteger bucketIdx, size_t keyHashCode, const TKey &key) const
bool areEqual(Element *elem, const TKey &key, size_t keyHashCode) const
void reset() noexcept
Resets this list to zero elements.
void pushFront(TElement *elem) noexcept
TElement * first() const noexcept
void next(SidiNodeBase *p)
TElement * addBehind(TElement *elem) noexcept