9#if ALIB_SIZEOF_INTEGER == 4
11#elif ALIB_SIZEOF_INTEGER == 8
15 #error "Unknown size of integer"
20template<
typename TAllocator,
21 typename TValueDescriptor,
40template<
typename TStored>
62template<
typename TStored>
84template<
typename TValueDescriptor, lang::Caching THashCaching>
90 && !std::is_arithmetic<typename TValueDescriptor::KeyType>::value );
95 HTElementCached <typename TValueDescriptor::StoredType>,
114template<
typename TAllocator,
115 typename TValueDescriptor,
122 typename HTElementSelector<TValueDescriptor, THashCaching>::Type >
129 using T =
typename TValueDescriptor::StoredType;
132 using TKey =
typename TValueDescriptor::KeyType;
135 using TMapped =
typename TValueDescriptor::MappedType;
165 :: template HookType<TAllocator,
184 if constexpr ( Element::CachedHashCodes )
185 return elem->getCached();
187 return THash{}( TValueDescriptor().Key( elem->value ) );
194 Element* elem= recyclerType::Get();
195 elem->fixHashCode( hashCode );
212 template<
typename TConstOrMutable>
216 friend class containers::HashTable<TAllocator, TValueDescriptor,THash,TEqual,THashCaching,TRecycling>;
220 using THashtable = std::conditional_t< !std::is_const_v< TConstOrMutable>,
247 if( !pTable->buckets[pBbucketIx].isEmpty() ) {
249 element = pTable->buckets[pBbucketIx].first();
295 template<
typename TMutable>
296 requires std::same_as<TMutable, TIterator<T>>
369 return TValueDescriptor().Key(
element->value );
377 return TValueDescriptor().Mapped(
element->value );
392 template<
typename TConstOrMutable>
396 friend class containers::HashTable<TAllocator,TValueDescriptor,THash,TEqual,THashCaching,TRecycling>;
424 template<
typename TMutable>
425 requires std::same_as<TMutable, TLocalIterator<T>>
501 return TValueDescriptor().Key(
element->value );
509 return TValueDescriptor().Mapped(
element->value );
547 && TEqual{}( TValueDescriptor().Key( lhs->value ),
548 TValueDescriptor().Key( rhs->value ) );
558 return ( !Element::CachedHashCodes || ( keyHashCode ==
getHashCode(elem) ) )
559 && TEqual{}( TValueDescriptor().Key( elem->value ), key );
572 return static_cast<Element*
>(result);
574 result= result->
next();
590 result= result->
next();
592 return result->
hasNext() ? result :
nullptr;
604 if( previous ==
nullptr )
630 float pBaseLoadFactor,
631 float pMaxLoadFactor )
648 template<
typename TIf= TAllocator>
649 requires std::is_default_constructible_v<TIf>
651 float pMaxLoadFactor )
666 template<
typename TSharedRecycler= SharedRecyclerType,
typename TIf=TAllocator>
667 requires (!std::same_as<TSharedRecycler , void>)
669 float pBaseLoadFactor = 1.0,
670 float pMaxLoadFactor = 2.0 )
690 if ( first !=
nullptr )
691 recyclerType::DisposeList(first);
709 if (
buckets[bucketIdx].first() ) {
710 recyclerType::RecycleList(
buckets[bucketIdx].first() );
754 "Internal error: Rehashing to equal or smaller bucket count." )
761 for(
uinteger bucketIdx= 0 ; bucketIdx < oldBucketCount ; ++bucketIdx ) {
763 if( first !=
nullptr )
775 while( actual !=
nullptr ) {
785 recyclerType::template RecycleChunk<FwdList>( oldData, oldBucketCount );
799 const auto hashCode= THash{}(key);
802 if( element ==
nullptr )
806 auto result= std::make_pair(
TIterator<T>(
this, bucketIdx, element ),
810 if( result.second.element ==
nullptr
811 || !
areEqual( result.second.element, key, hashCode ) )
833 if (element !=
nullptr )
834 return std::make_pair(
TIterator<T>(
this, bucketIdx, element ),
false);
839 buckets[bucketIdx].pushFront( newElement );
840 return std::make_pair(
TIterator<T>(
this, bucketIdx, newElement ) ,
true);
854 auto* elem=
findElement( bucketIdx, key, hashCode );
855 if( elem !=
nullptr )
856 return std::make_pair(
TIterator<T>(
this, bucketIdx, elem ),
false);
861 buckets[bucketIdx].pushFront( newElem );
862 return std::make_pair(
TIterator<T>(
this, bucketIdx, newElem ),
true);
#define ALIB_ASSERT_ERROR(cond, domain,...)
TIterator()=default
Default constructor. Keeps everything uninitialized.
TIterator & operator=(const TIterator &other)=default
std::forward_iterator_tag iterator_category
Implementation of std::iterator_traits.
TMapped value_type
Implementation of std::iterator_traits.
TIterator(THashtable *pTable, uinteger pBbucketIx)
TConstOrMutable * operator->() const
TIterator(const TIterator &other)=default
TConstOrMutable * pointer
Implementation of std::iterator_traits.
TConstOrMutable & Value() const
std::conditional_t< !std::is_const_v< TConstOrMutable >, HashTableBase, const HashTableBase > THashtable
Const or mutable version of HashTableBase.
uinteger bucketIdx
The actual bucket index.
TIterator(THashtable *pTable, uinteger pBbucketIx, Element *pElement)
bool operator!=(TIterator other) const
TIterator(const TMutable &mutableIt)
TConstOrMutable & operator*() const
bool operator==(TIterator other) const
THashtable * table
The pointer to the hash table.
Element * element
The pointer to the actual element.
TIterator operator++(int)
void repair()
Moves an iterator with a nulled element pointer to the next element.
integer difference_type
Implementation of std::iterator_traits.
TConstOrMutable & reference
Implementation of std::iterator_traits.
TConstOrMutable value_type
Implementation of std::iterator_traits.
TConstOrMutable & operator*() const
integer difference_type
Implementation of std::iterator_traits.
TLocalIterator & operator++()
TLocalIterator & operator=(const TLocalIterator &other)=default
TLocalIterator operator++(int)
TLocalIterator(const TLocalIterator &other)=default
TConstOrMutable & Value() const
TConstOrMutable * operator->() const
Element * element
The pointer to the actual element.
bool operator==(TLocalIterator other) const
TConstOrMutable & reference
Implementation of std::iterator_traits.
bool operator!=(TLocalIterator other) const
TLocalIterator(const TMutable &mutableIt)
TLocalIterator()
Default constructor.
uinteger bucketIdx
The index of the bucket that this iterator works on.
std::forward_iterator_tag iterator_category
Implementation of std::iterator_traits.
TLocalIterator(uinteger pBucketIdx, Element *pElement)
TConstOrMutable * pointer
Implementation of std::iterator_traits.
static constexpr int PRIME_TABLE_SIZE
The size of the static table of prime numbers. Depends on the platform.
Detail namespace of module ALib Containers.
void * DUMMY_BUCKET
A dummy bucket used for nulled hash tables to avoid otherwise necessary checks.
const uinteger PRIME_NUMBERS[PRIME_TABLE_SIZE]
Caching
Denotes if a cache mechanism is enabled or disabled.
@ Enabled
Caching is enabled.
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".
lang::uinteger uinteger
Type alias in namespace #"%alib".
TStored value
The custom data stored in nodes of this table.
size_t hashCode
The cached hash code.
void fixHashCode(size_t pHashCode)
static constexpr bool CachedHashCodes
Denotes that hash codes are cached.
std::conditional_t< IsCachingHashes(), HTElementCached< typename TValueDescriptor::StoredType >, HTElementUncached< typename TValueDescriptor::StoredType > > Type
The type stored in a hash table's bucket list.
static constexpr bool IsCachingHashes()
TStored value
The custom data stored in nodes of this table.
static constexpr bool CachedHashCodes
Denotes that hash codes are not cached.
void fixHashCode(size_t pHashCode)
HashTableBase(TSharedRecycler &pSharedRecycler, float pBaseLoadFactor=1.0, float pMaxLoadFactor=2.0)
typename TValueDescriptor::KeyType TKey
Type definition publishing template parameter TKey.
void clear()
Destructs and removes all entries from this hash table.
void rehash(uinteger newMinBucketCount)
Element * allocElement(const size_t hashCode)
HashTableBase(float pBaseLoadFactor, float pMaxLoadFactor)
lang::SidiListHook< Element > FwdList
Type of a single linked node list.
bool areEqual(Element *lhs, Element *rhs) const
static size_t getHashCode(Element *elem)
typename HTElementSelector< TValueDescriptor, THashCaching >::Type Element
The type stored in the bucket of class #"%HashTable".
uinteger insertInBucket(Element *element, const size_t hashCode)
integer size
The number of elements stored.
uinteger bucketCount
The number of buckets managed by this tree.
typename TValueDescriptor::MappedType TMapped
Type definition publishing template parameter TKey.
typename TValueDescriptor::StoredType T
Type definition publishing template parameter T.
void setMaxLoadFactor(float pMaxLoadFactor)
float baseLoadFactor
The load factor that is set when the table is rehashed automatically.
std::pair< TIterator< T >, bool > insertOrGet(const TKey &key, size_t hashCode)
integer sizeLimitToRehash
Calculated once with rehash. Product of #"maxLoadFactor" and #"bucketCount".
typename RecyclingSelector< TRecycling >::template Type< TAllocator, typename HTElementSelector< TValueDescriptor, THashCaching >::Type > base
Our base type.
HashTableBase(TAllocator &pAllocator, float pBaseLoadFactor, float pMaxLoadFactor)
Node * findElementBefore(uinteger bucketIdx, size_t keyHashCode, const TKey &key) const
Element * findElement(uinteger bucketIdx, const TKey &key, size_t keyHashCode) const
size_t increaseSize(integer increase, const size_t hashCode=0)
typename RecyclingSelector< TRecycling >::template Type< TAllocator, typename HTElementSelector< TValueDescriptor, THashCaching >::Type > recyclerType
Type of a single linked node list.
lang::SidiNodeBase< Element > Node
Type of a node in the #"%List".
FwdList * buckets
The list of buckets.
static constexpr bool IsCachingHashes()
std::pair< TIterator< T >, bool > insertIfNotExists(const TKey &key, size_t hashCode)
typename detail::RecyclingSelector< TRecycling > ::template HookType< TAllocator, typename HTElementSelector< TValueDescriptor, THashCaching >::Type > SharedRecyclerType
std::pair< TIterator< T >, TIterator< T > > findRange(const TKey &key)
bool areEqual(Element *elem, const TKey &key, size_t keyHashCode) const
TElement * first() const noexcept
void pushFront(TElement *elem) noexcept
TElement * addBehind(TElement *elem) noexcept
void next(SidiNodeBase *p)