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>
63template<
typename TStored>
86template<
typename TValueDescriptor, lang::Caching THashCaching>
93 && !std::is_arithmetic<typename TValueDescriptor::KeyType>::value );
98 HTElementCached <typename TValueDescriptor::StoredType>,
117template<
typename TAllocator,
118 typename TValueDescriptor,
125 typename HTElementSelector<TValueDescriptor, THashCaching>::Type >
132 using T =
typename TValueDescriptor::StoredType;
135 using TKey =
typename TValueDescriptor::KeyType;
138 using TMapped =
typename TValueDescriptor::MappedType;
168 :: template HookType<TAllocator,
187 if constexpr ( Element::CachedHashCodes )
188 return elem->getCached();
190 return THash{}( TValueDescriptor().Key( elem->value ) );
197 Element* elem= recyclerType::Get();
198 elem->fixHashCode( hashCode );
215 template<
typename TConstOrMutable>
220 friend class containers::HashTable<TAllocator, TValueDescriptor,THash,TEqual,THashCaching,TRecycling>;
224 using THashtable = std::conditional_t< !std::is_const_v< TConstOrMutable>,
251 if( !pTable->buckets[pBbucketIx].isEmpty() ) {
253 element = pTable->buckets[pBbucketIx].first();
299 template<
typename TMutable>
300 requires std::same_as<TMutable, TIterator<T>>
373 return TValueDescriptor().Key(
element->value );
381 return TValueDescriptor().Mapped(
element->value );
396 template<
typename TConstOrMutable>
401 friend class containers::HashTable<TAllocator,TValueDescriptor,THash,TEqual,THashCaching,TRecycling>;
429 template<
typename TMutable>
430 requires std::same_as<TMutable, TLocalIterator<T>>
506 return TValueDescriptor().Key(
element->value );
514 return TValueDescriptor().Mapped(
element->value );
552 && TEqual{}( TValueDescriptor().Key( lhs->value ),
553 TValueDescriptor().Key( rhs->value ) );
563 return ( !Element::CachedHashCodes || ( keyHashCode ==
getHashCode(elem) ) )
564 && TEqual{}( TValueDescriptor().Key( elem->value ), key );
577 return static_cast<Element*
>(result);
579 result= result->
next();
595 result= result->
next();
597 return result->
hasNext() ? result :
nullptr;
609 if( previous ==
nullptr )
635 float pBaseLoadFactor,
636 float pMaxLoadFactor )
653 template<
typename TIf= TAllocator>
654 requires std::is_default_constructible_v<TIf>
656 float pMaxLoadFactor )
671 template<
typename TSharedRecycler= SharedRecyclerType,
typename TIf=TAllocator>
672 requires (!std::same_as<TSharedRecycler , void>)
674 float pBaseLoadFactor = 1.0,
675 float pMaxLoadFactor = 2.0 )
695 if ( first !=
nullptr )
696 recyclerType::DisposeList(first);
714 if (
buckets[bucketIdx].first() ) {
715 recyclerType::RecycleList(
buckets[bucketIdx].first() );
759 "Internal error: Rehashing to equal or smaller bucket count." )
766 for(
uinteger bucketIdx= 0 ; bucketIdx < oldBucketCount ; ++bucketIdx ) {
768 if( first !=
nullptr )
780 while( actual !=
nullptr ) {
790 recyclerType::template RecycleChunk<FwdList>( oldData, oldBucketCount );
804 const auto hashCode= THash{}(key);
807 if( element ==
nullptr )
811 auto result= std::make_pair(
TIterator<T>(
this, bucketIdx, element ),
815 if( result.second.element ==
nullptr
816 || !
areEqual( result.second.element, key, hashCode ) )
838 if (element !=
nullptr )
839 return std::make_pair(
TIterator<T>(
this, bucketIdx, element ),
false);
844 buckets[bucketIdx].pushFront( newElement );
845 return std::make_pair(
TIterator<T>(
this, bucketIdx, newElement ) ,
true);
859 auto* elem=
findElement( bucketIdx, key, hashCode );
860 if( elem !=
nullptr )
861 return std::make_pair(
TIterator<T>(
this, bucketIdx, elem ),
false);
866 buckets[bucketIdx].pushFront( newElem );
867 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)