Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
OhashCommon< HashTbl, Key > Class Template Reference

CRTP mixin providing common operations for open addressing hash tables. More...

#include <hashDry.H>

Inheritance diagram for OhashCommon< HashTbl, Key >:
[legend]

Classes

class  Iterator
 Bidirectional iterator for traversing hash table entries. More...
 
struct  Stats
 Statistics about hash table performance. More...
 

Public Types

using Const_Iterator = Aleph::Const_Iterator< Iterator >
 Read-only iterator: the iterator of a constant table.
 

Public Member Functions

constexpr float get_lower_alpha () const noexcept
 Returns the lower load factor threshold for automatic shrinking.
 
constexpr float get_upper_alpha () const noexcept
 Returns the upper load factor threshold for automatic growing.
 
constexpr bool get_with_resize () const noexcept
 Returns whether the table resizes itself.
 
void set_with_resize (const bool with_resize) noexcept
 Turns the automatic resizing on or off.
 
void copy_from_table (const HashTbl &other)
 Copies all entries from another hash table.
 
void clean_table ()
 Removes all entries from the table without deallocating storage.
 
bool check_resize_before_insert ()
 Makes room for one more key, if the table can take it.
 
Key * insert (const Key &key)
 Inserts a key into the hash table (copy version).
 
Key * insert (Key &&key)
 Inserts a key into the hash table (move version).
 
Key * search_or_insert (const Key &key)
 Searches for a key, inserting it if not found (copy version).
 
Key * search_or_insert (Key &&key)
 Searches for a key, inserting it if not found (move version).
 
std::pair< Key *, bool > contains_or_insert (const Key &key)
 Searches for a key, inserting if not found, with existence flag (copy version).
 
std::pair< Key *, bool > contains_or_insert (Key &&key)
 Searches for a key, inserting if not found, with existence flag (move version).
 
Key * append (const Key &key)
 Alias for insert() (copy version).
 
Key * append (Key &&key)
 Alias for insert() (move version).
 
constexpr bool has (const Key &key) const noexcept
 Checks if a key exists in the table.
 
constexpr bool contains (const Key &key) const noexcept
 Alias for has().
 
Key & find (const Key &key)
 Finds a key and returns a reference to it.
 
const Key & find (const Key &key) const
 Finds a key and returns a const reference to it.
 
const Key & operator[] (const Key &key) const
 Subscript operator for const access.
 
const Key & operator[] (const Key &key)
 Subscript operator with insertion semantics.
 
void remove_ptr (Key *key)
 Removes an entry by pointer.
 
size_t resize (size_t new_size)
 Resizes the hash table to a new capacity.
 
void rehash ()
 Rebuilds the hash table with the same capacity.
 
void empty ()
 Clears all entries and resets to default capacity.
 
void clear ()
 Empties the container.
 
constexpr size_t size () const noexcept
 Returns the number of entries in the table.
 
constexpr bool is_empty () const noexcept
 Checks if the table is empty.
 
constexpr size_t capacity () const noexcept
 Returns the current capacity of the table.
 
DynList< Key > keys () const
 Returns a list containing all keys in the table.
 
auto items () const
 Alias for keys().
 
void print_stats (const Stats &stats) const
 Prints statistics to standard output.
 
constexpr float current_alpha () const noexcept
 Returns the current load factor (alpha).
 

Protected Member Functions

void rebuild (const size_t new_len)
 Places the keys of the table in new_len new buckets.
 
size_t rebuild_capacity () const
 Capacity to rebuild a table that resizes itself with, when its buckets in use are too many.
 
size_t capacity_after_insertion () const
 Capacity that an insertion leaves the table with, or 0 if it keeps it.
 
void shrink_after_removal () noexcept
 Shrinks a table that resizes itself, after a removal took its load factor under the lower threshold.
 

Private Member Functions

HashTbl * me ()
 Returns a pointer to the derived class (CRTP pattern).
 
const HashTbl * const_me () const
 Returns a const pointer to the derived class (CRTP pattern).
 
template<class Bucket >
void store_copy (Bucket *bucket, const Key &key)
 Stores a copy of a key in the bucket reserved for it.
 
template<class Bucket >
void store_move (Bucket *bucket, Key &key)
 Moves a key into the bucket reserved for it.
 

Static Private Attributes

static constexpr bool nothrow_key_swap
 Whether two keys can be swapped without throwing.
 

Detailed Description

template<class HashTbl, typename Key>
class OhashCommon< HashTbl, Key >

CRTP mixin providing common operations for open addressing hash tables.

This template class provides shared implementation for hash table operations that are common between ODhashTable (double hashing) and OLhashTable (linear probing). It uses CRTP to achieve static polymorphism, avoiding virtual function overhead while enabling code reuse.

Template Parameters
HashTblThe derived hash table class (ODhashTable or OLhashTable). Must provide: allocate_bucket(), hard_allocate_bucket(), deallocate_bucket(), test_resize(), search(), table, len, N, deleted_buckets, lower_alpha, upper_alpha, with_resize, current_alpha(), Bucket.
KeyThe type of keys stored in the table.
Provided Operations:
Capacity:
The table leaves at least one bucket without a key, so that a search that misses can stop at an empty bucket: it is full with capacity() - 1 keys. A full table that does not resize itself takes no more keys: the insertions return nullptr and operator[] throws std::overflow_error.
Automatic Resizing:
When with_resize is enabled in the derived class, the table automatically:
  • Grows when it becomes nearly full (before insert would fail)
  • Shrinks when load factor drops below lower_alpha threshold, if the keys fit under upper_alpha in a table of half the capacity. Both remove() and remove_ptr() shrink it; the del() of an iterator does not, so that the iterator stays valid. set_with_resize() turns it on or off after construction.
Removed keys:
A removed key may leave its bucket marked, so that the searches that went through it still do. In a table that resizes itself, those buckets count as used to decide when it is full or over upper_alpha, and when they take it there, it is rebuilt without them: in place, if the keys alone fill less than half of upper_alpha, and twice as large otherwise. So it always keeps an empty bucket. A table that does not resize itself is never rebuilt by itself, so that its keys do not move: the buckets of its removed keys may take its empty buckets, and a search that misses then goes through all of them, until rehash() clears them.
Thread Safety:
This class is NOT thread-safe. External synchronization is required for concurrent access.
See also
ODhashTable
OLhashTable

Definition at line 171 of file hashDry.H.

Member Typedef Documentation

◆ Const_Iterator

template<class HashTbl , typename Key >
using OhashCommon< HashTbl, Key >::Const_Iterator = Aleph::Const_Iterator<Iterator>

Read-only iterator: the iterator of a constant table.

Definition at line 1236 of file hashDry.H.

Member Function Documentation

◆ append() [1/2]

template<class HashTbl , typename Key >
Key * OhashCommon< HashTbl, Key >::append ( const Key &  key)
inline

Alias for insert() (copy version).

Provides STL-like naming for insert operations.

Parameters
keyThe key to append/insert.
Returns
Pointer to the inserted key, or nullptr on failure.
See also
insert(const Key &)

Definition at line 733 of file hashDry.H.

References OhashCommon< HashTbl, Key >::insert().

Referenced by Aleph::import_network_from_dimacs(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), and TEST_F().

◆ append() [2/2]

template<class HashTbl , typename Key >
Key * OhashCommon< HashTbl, Key >::append ( Key &&  key)
inline

Alias for insert() (move version).

Provides STL-like naming for insert operations.

Parameters
keyThe key to append/insert (will be moved from).
Returns
Pointer to the inserted key, or nullptr on failure.
See also
insert(Key &&)

Definition at line 746 of file hashDry.H.

References OhashCommon< HashTbl, Key >::insert().

◆ capacity()

template<class HashTbl , typename Key >
constexpr size_t OhashCommon< HashTbl, Key >::capacity ( ) const
inlineconstexprnoexcept

Returns the current capacity of the table.

Returns
The number of buckets in the internal array.

Definition at line 964 of file hashDry.H.

References OhashCommon< HashTbl, Key >::const_me().

Referenced by demo_odhash(), OhashCommon< HashTbl, Key >::print_stats(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), and TEST_F().

◆ capacity_after_insertion()

template<class HashTbl , typename Key >
size_t OhashCommon< HashTbl, Key >::capacity_after_insertion ( ) const
inlineprotected

Capacity that an insertion leaves the table with, or 0 if it keeps it.

A table that resizes itself is rebuilt when its buckets in use, those of removed keys included, reach the upper threshold. Removing a key does not change how many buckets are in use, so it is enough to check after each insertion.

Returns
0, or the capacity for rebuild().

Definition at line 327 of file hashDry.H.

References OhashCommon< HashTbl, Key >::const_me(), and OhashCommon< HashTbl, Key >::rebuild_capacity().

◆ check_resize_before_insert()

template<class HashTbl , typename Key >
bool OhashCommon< HashTbl, Key >::check_resize_before_insert ( )
inline

Makes room for one more key, if the table can take it.

Called before each insert operation. The table leaves at least one bucket without a key, as a sentinel for probing, so it is full with capacity() - 1 keys. A full table doubles its capacity if automatic resizing is enabled; otherwise it takes no more keys.

The buckets of removed keys are not empty. If they would leave a table that resizes itself without an empty bucket, it is rebuilt without them. A table that does not resize itself is never rebuilt, so that its keys do not move.

A table whose storage was taken by a move has no buckets. It gets here a table of the default capacity, whether or not automatic resizing is enabled.

Returns
true if the table can take one more key; false if it is full and does not resize itself.
Exceptions
std::bad_allocIf there is no memory for the new buckets, and anything that copying a key throws when the table is rebuilt and its swap may throw. The table is left as it was.

Definition at line 466 of file hashDry.H.

References OhashCommon< HashTbl, Key >::empty(), OhashCommon< HashTbl, Key >::me(), Primes::next_prime(), OhashCommon< HashTbl, Key >::rebuild(), and OhashCommon< HashTbl, Key >::rebuild_capacity().

Referenced by OhashCommon< HashTbl, Key >::contains_or_insert(), OhashCommon< HashTbl, Key >::contains_or_insert(), OhashCommon< HashTbl, Key >::insert(), OhashCommon< HashTbl, Key >::insert(), OhashCommon< HashTbl, Key >::search_or_insert(), and OhashCommon< HashTbl, Key >::search_or_insert().

◆ clean_table()

template<class HashTbl , typename Key >
void OhashCommon< HashTbl, Key >::clean_table ( )
inline

Removes all entries from the table without deallocating storage.

Resets all buckets to their initial empty state and sets the entry count to zero. The table capacity remains unchanged.

Postcondition
size() == 0
capacity() unchanged

Definition at line 436 of file hashDry.H.

References OhashCommon< HashTbl, Key >::me().

◆ clear()

template<class HashTbl , typename Key >
void OhashCommon< HashTbl, Key >::clear ( )
inline

Empties the container.

Exceptions
std::bad_allocIf there is no memory for the new buckets. The table is left as it was.
See also
empty()

Definition at line 949 of file hashDry.H.

References OhashCommon< HashTbl, Key >::empty().

Referenced by TEST(), TEST(), TEST_F(), and Moved_From_Case< MinHash< int > >::use().

◆ const_me()

◆ contains()

template<class HashTbl , typename Key >
constexpr bool OhashCommon< HashTbl, Key >::contains ( const Key &  key) const
inlineconstexprnoexcept

Alias for has().

Parameters
keyThe key to search for.
Returns
true if the key exists, false otherwise.
See also
has()

Definition at line 769 of file hashDry.H.

References OhashCommon< HashTbl, Key >::has().

◆ contains_or_insert() [1/2]

template<class HashTbl , typename Key >
std::pair< Key *, bool > OhashCommon< HashTbl, Key >::contains_or_insert ( const Key &  key)
inline

Searches for a key, inserting if not found, with existence flag (copy version).

Similar to search_or_insert(), but also indicates whether the key was already present in the table.

Parameters
keyThe key to search for or insert.
Returns
A pair containing:
  • first: Pointer to the key (existing or newly inserted), or nullptr if the key is not in the table and the table is full and does not resize itself.
  • second: true if the key was already present, false if inserted.
Exceptions
Anythingthat copying the key throws, and std::bad_alloc if the table cannot grow. The table is left as it was.
Note
This function is marked [[nodiscard]].
Example:
auto [ptr, existed] = table.contains_or_insert(key);
if (ptr) {
if (existed) std::cout << "Key already existed\n";
else std::cout << "Key was inserted\n";
}
Complexity:
  • Average case: O(1) amortized
  • Worst case: O(n) when rehashing is triggered

Definition at line 661 of file hashDry.H.

References OhashCommon< HashTbl, Key >::check_resize_before_insert(), OhashCommon< HashTbl, Key >::me(), r, and OhashCommon< HashTbl, Key >::store_copy().

◆ contains_or_insert() [2/2]

template<class HashTbl , typename Key >
std::pair< Key *, bool > OhashCommon< HashTbl, Key >::contains_or_insert ( Key &&  key)
inline

Searches for a key, inserting if not found, with existence flag (move version).

Similar to search_or_insert(), but also indicates whether the key was already present in the table.

Parameters
keyThe key to search for or insert (moved from on insert).
Returns
A pair containing:
  • first: Pointer to the key (existing or newly inserted), or nullptr if the key is not in the table and the table is full and does not resize itself.
  • second: true if the key was already present, false if inserted.
Exceptions
std::bad_allocIf the table cannot grow, and anything that copying the key throws when the table has to copy it. The table is left as it was; key may have been moved from.
Note
This function is marked [[nodiscard]].
Complexity:
  • Average case: O(1) amortized
  • Worst case: O(n) when rehashing is triggered

Definition at line 704 of file hashDry.H.

References OhashCommon< HashTbl, Key >::check_resize_before_insert(), OhashCommon< HashTbl, Key >::me(), r, and OhashCommon< HashTbl, Key >::store_move().

◆ copy_from_table()

template<class HashTbl , typename Key >
void OhashCommon< HashTbl, Key >::copy_from_table ( const HashTbl &  other)
inline

Copies all entries from another hash table.

Copies all BUSY entries from the source table to this table. This table must be empty and have sufficient capacity.

Parameters
otherSource hash table to copy from.
Precondition
This table must be empty (N == 0).
This table must have capacity >= other.N.
Postcondition
This table contains all entries from other.

Definition at line 413 of file hashDry.H.

References Aleph::and, and OhashCommon< HashTbl, Key >::me().

Referenced by Aleph::ODhashTable< Key, Cmp >::ODhashTable(), and Aleph::OLhashTable< Key, Cmp >::OLhashTable().

◆ current_alpha()

template<class HashTbl , typename Key >
constexpr float OhashCommon< HashTbl, Key >::current_alpha ( ) const
inlineconstexprnoexcept

Returns the current load factor (alpha).

The load factor is the ratio of stored entries to table capacity. A higher load factor means more efficient memory usage but potentially longer probe sequences.

Returns
The load factor as N/capacity, or zero if the table has no buckets because a move took them.

Definition at line 1310 of file hashDry.H.

References OhashCommon< HashTbl, Key >::const_me().

Referenced by OhashCommon< HashTbl, Key >::shrink_after_removal().

◆ empty()

template<class HashTbl , typename Key >
void OhashCommon< HashTbl, Key >::empty ( )
inline

Clears all entries and resets to default capacity.

Removes all entries and resets the table to its default initial capacity (Primes::DefaultPrime).

Postcondition
size() == 0
capacity() == Primes::DefaultPrime
Exceptions
std::bad_allocIf there is no memory for the new buckets. The table is left as it was.

Definition at line 933 of file hashDry.H.

References Primes::DefaultPrime, and OhashCommon< HashTbl, Key >::me().

Referenced by OhashCommon< HashTbl, Key >::check_resize_before_insert(), OhashCommon< HashTbl, Key >::clear(), TEST(), TEST(), TEST(), and TYPED_TEST().

◆ find() [1/2]

template<class HashTbl , typename Key >
Key & OhashCommon< HashTbl, Key >::find ( const Key &  key)
inline

Finds a key and returns a reference to it.

Searches for the key and returns a reference to it. If the key is not found, throws an exception.

Parameters
keyThe key to find.
Returns
Reference to the stored key.
Exceptions
std::domain_errorIf the key is not found.
Complexity: O(1) average, O(n) worst case.

Definition at line 782 of file hashDry.H.

References ah_domain_error_if, and OhashCommon< HashTbl, Key >::me().

Referenced by OhashCommon< HashTbl, Key >::find(), OhashCommon< HashTbl, Key >::operator[](), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), and Moved_From_Case< PersistentHashMap< int, int > >::use().

◆ find() [2/2]

template<class HashTbl , typename Key >
const Key & OhashCommon< HashTbl, Key >::find ( const Key &  key) const
inline

Finds a key and returns a const reference to it.

Const version of find().

Parameters
keyThe key to find.
Returns
Const reference to the stored key.
Exceptions
std::domain_errorIf the key is not found.
See also
find(const Key &)

Definition at line 800 of file hashDry.H.

References OhashCommon< HashTbl, Key >::find().

◆ get_lower_alpha()

template<class HashTbl , typename Key >
constexpr float OhashCommon< HashTbl, Key >::get_lower_alpha ( ) const
inlineconstexprnoexcept

Returns the lower load factor threshold for automatic shrinking.

When automatic resizing is enabled and the load factor drops below this value after a removal, the table shrinks.

Returns
The lower alpha threshold (typically 0.25).

Definition at line 375 of file hashDry.H.

References OhashCommon< HashTbl, Key >::const_me().

◆ get_upper_alpha()

template<class HashTbl , typename Key >
constexpr float OhashCommon< HashTbl, Key >::get_upper_alpha ( ) const
inlineconstexprnoexcept

Returns the upper load factor threshold for automatic growing.

When automatic resizing is enabled and the load factor would exceed this value, the table grows before insertion.

Returns
The upper alpha threshold (typically 0.75).

Definition at line 384 of file hashDry.H.

References OhashCommon< HashTbl, Key >::const_me().

◆ get_with_resize()

template<class HashTbl , typename Key >
constexpr bool OhashCommon< HashTbl, Key >::get_with_resize ( ) const
inlineconstexprnoexcept

Returns whether the table resizes itself.

Returns
true if the table grows and shrinks by itself.

Definition at line 389 of file hashDry.H.

References OhashCommon< HashTbl, Key >::const_me().

◆ has()

template<class HashTbl , typename Key >
constexpr bool OhashCommon< HashTbl, Key >::has ( const Key &  key) const
inlineconstexprnoexcept

Checks if a key exists in the table.

Parameters
keyThe key to search for.
Returns
true if the key exists, false otherwise.
Complexity: O(1) average, O(n) worst case.

Definition at line 758 of file hashDry.H.

References OhashCommon< HashTbl, Key >::const_me().

Referenced by OhashCommon< HashTbl, Key >::contains(), TEST(), TEST(), and TEST().

◆ insert() [1/2]

template<class HashTbl , typename Key >
Key * OhashCommon< HashTbl, Key >::insert ( const Key &  key)
inline

Inserts a key into the hash table (copy version).

Attempts to insert a copy of the key. If the key already exists, the insertion fails and nullptr is returned.

Parameters
keyThe key to insert.
Returns
Pointer to the inserted key, or nullptr if key already exists or the table is full and does not resize itself.
Exceptions
Anythingthat copying the key throws, and std::bad_alloc if the table cannot grow. The table is left as it was.
Note
This function is marked [[nodiscard]] - check the return value to detect insertion failures.
Complexity:
  • Average case: O(1) amortized
  • Worst case: O(n) when rehashing is triggered

Definition at line 507 of file hashDry.H.

References OhashCommon< HashTbl, Key >::check_resize_before_insert(), OhashCommon< HashTbl, Key >::me(), and OhashCommon< HashTbl, Key >::store_copy().

Referenced by OhashCommon< HashTbl, Key >::append(), OhashCommon< HashTbl, Key >::append(), demo_odhash(), demo_performance(), main(), Moved_From_Case< PersistentHashMap< int, int > >::make(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST_F(), TEST_F(), TYPED_TEST(), and TYPED_TEST().

◆ insert() [2/2]

template<class HashTbl , typename Key >
Key * OhashCommon< HashTbl, Key >::insert ( Key &&  key)
inline

Inserts a key into the hash table (move version).

Attempts to insert a key using move semantics. If the key already exists, the insertion fails and nullptr is returned.

Parameters
keyThe key to insert (will be moved from on success).
Returns
Pointer to the inserted key, or nullptr if key already exists or the table is full and does not resize itself.
Exceptions
std::bad_allocIf the table cannot grow, and anything that copying the key throws when the table has to copy it. The table is left as it was; key may have been moved from.
Note
This function is marked [[nodiscard]] - check the return value to detect insertion failures.
Complexity:
  • Average case: O(1) amortized
  • Worst case: O(n) when rehashing is triggered

Definition at line 541 of file hashDry.H.

References OhashCommon< HashTbl, Key >::check_resize_before_insert(), OhashCommon< HashTbl, Key >::me(), and OhashCommon< HashTbl, Key >::store_move().

◆ is_empty()

◆ items()

template<class HashTbl , typename Key >
auto OhashCommon< HashTbl, Key >::items ( ) const
inline

Alias for keys().

Returns
DynList<Key> containing all keys.
See also
keys()

Definition at line 1256 of file hashDry.H.

References keys.

Referenced by TEST().

◆ keys()

template<class HashTbl , typename Key >
DynList< Key > OhashCommon< HashTbl, Key >::keys ( ) const
inline

Returns a list containing all keys in the table.

Creates and returns a DynList containing copies of all keys stored in the hash table.

Returns
DynList<Key> containing all keys.
Complexity: O(n) where n is the number of entries.

Definition at line 1247 of file hashDry.H.

References OhashCommon< HashTbl, Key >::const_me().

Referenced by TEST(), TEST(), TEST(), TEST(), TEST(), TYPED_TEST(), TYPED_TEST(), and Moved_From_Case< PersistentHashMap< int, int > >::use().

◆ me()

◆ operator[]() [1/2]

template<class HashTbl , typename Key >
const Key & OhashCommon< HashTbl, Key >::operator[] ( const Key &  key)
inline

Subscript operator with insertion semantics.

Returns a reference to the key. If the key doesn't exist, it is inserted first.

Parameters
keyThe key to access or insert.
Returns
Reference to the stored key.
Exceptions
std::overflow_errorIf the key is not in the table and the table is full and does not resize itself.
Anythingthat copying the key throws, and std::bad_alloc if the table cannot grow. The table is left as it was.

Definition at line 830 of file hashDry.H.

References ah_overflow_error_if, and OhashCommon< HashTbl, Key >::search_or_insert().

◆ operator[]() [2/2]

template<class HashTbl , typename Key >
const Key & OhashCommon< HashTbl, Key >::operator[] ( const Key &  key) const
inline

Subscript operator for const access.

Returns a const reference to the key. Equivalent to find().

Parameters
keyThe key to access.
Returns
Const reference to the stored key.
Exceptions
std::domain_errorIf the key is not found.

Definition at line 813 of file hashDry.H.

References OhashCommon< HashTbl, Key >::find().

◆ print_stats()

template<class HashTbl , typename Key >
void OhashCommon< HashTbl, Key >::print_stats ( const Stats &  stats) const
inline

Prints statistics to standard output.

Displays formatted statistics including capacity, size, bucket states, load factor, and probe length distribution.

Parameters
statsStatistics object to print.

Definition at line 1288 of file hashDry.H.

References OhashCommon< HashTbl, Key >::capacity(), OhashCommon< HashTbl, Key >::const_me(), OhashCommon< HashTbl, Key >::Stats::lens, OhashCommon< HashTbl, Key >::Stats::max_len, OhashCommon< HashTbl, Key >::Stats::num_busy, OhashCommon< HashTbl, Key >::Stats::num_deleted, OhashCommon< HashTbl, Key >::Stats::num_empty, OhashCommon< HashTbl, Key >::size(), and Aleph::DynArray< T >::size().

◆ rebuild()

template<class HashTbl , typename Key >
void OhashCommon< HashTbl, Key >::rebuild ( const size_t  new_len)
inlineprotected

Places the keys of the table in new_len new buckets.

The keys are swapped into the new buckets. A key whose swap may throw is copied instead, so that the old buckets keep every key until all of them are placed. The buckets of removed keys are left behind.

Parameters
new_lenNumber of new buckets. It must leave an empty bucket.
Exceptions
std::overflow_errorIf the bytes of the new buckets do not fit in a size_t, std::bad_alloc if there is no memory for them, and anything that copying a key throws. The table is left as it was.

Definition at line 255 of file hashDry.H.

References OhashCommon< HashTbl, Key >::me().

Referenced by OhashCommon< HashTbl, Key >::check_resize_before_insert(), OhashCommon< HashTbl, Key >::rehash(), and OhashCommon< HashTbl, Key >::resize().

◆ rebuild_capacity()

template<class HashTbl , typename Key >
size_t OhashCommon< HashTbl, Key >::rebuild_capacity ( ) const
inlineprotected

Capacity to rebuild a table that resizes itself with, when its buckets in use are too many.

The buckets in use include the ones of removed keys, which a search does not stop at, and a rebuild leaves behind. If the keys alone fill less than half of the upper threshold, the table keeps its capacity, so that many insertions come before it has to be rebuilt again; otherwise it doubles.

Returns
The capacity for rebuild().

Definition at line 309 of file hashDry.H.

References OhashCommon< HashTbl, Key >::const_me(), and Primes::next_prime().

Referenced by OhashCommon< HashTbl, Key >::capacity_after_insertion(), and OhashCommon< HashTbl, Key >::check_resize_before_insert().

◆ rehash()

template<class HashTbl , typename Key >
void OhashCommon< HashTbl, Key >::rehash ( )
inline

Rebuilds the hash table with the same capacity.

Creates a new internal table with the same capacity and re-inserts all entries. This can help improve performance after many deletions by eliminating deleted bucket markers. A table that does not resize itself never does it by itself.

Exceptions
std::bad_allocIf there is no memory for the new buckets, and anything that copying a key throws when its swap may throw. The table is left as it was.
Complexity: O(n) where n is the number of entries.

Definition at line 915 of file hashDry.H.

References OhashCommon< HashTbl, Key >::me(), and OhashCommon< HashTbl, Key >::rebuild().

Referenced by Aleph::ODhashTable< Key, Cmp >::set_second_hash_fct().

◆ remove_ptr()

template<class HashTbl , typename Key >
void OhashCommon< HashTbl, Key >::remove_ptr ( Key *  key)
inline

Removes an entry by pointer.

Removes the entry pointed to by the given pointer. The pointer must have been obtained from this table (via insert, search, etc.).

If automatic resizing is enabled and the load factor drops below the lower threshold, the table shrinks to about half its capacity, provided the keys fit there under the upper threshold and leave an empty bucket. With a lower threshold over one half they may not fit; the table then keeps its capacity until enough keys are removed. If there is no memory for the smaller table, the table keeps its capacity too, and the key stays removed.

Parameters
keyPointer to the key to remove (must be in this table).
Exceptions
std::invalid_argumentIf key does not point into the table.
std::domain_errorIf the key of its bucket was removed.
Postcondition
The entry is removed and size() decreases by 1.
Complexity: O(1) amortized.

Definition at line 860 of file hashDry.H.

References ah_domain_error_if, ah_invalid_argument_if, OhashCommon< HashTbl, Key >::me(), and OhashCommon< HashTbl, Key >::shrink_after_removal().

◆ resize()

template<class HashTbl , typename Key >
size_t OhashCommon< HashTbl, Key >::resize ( size_t  new_size)
inline

Resizes the hash table to a new capacity.

Changes the table capacity to the specified size and rehashes all entries. The new size must be large enough to hold all current entries plus at least one empty sentinel bucket.

Parameters
new_sizeThe new capacity (should be a prime number).
Returns
The actual new capacity (may differ from requested if new_size was 0 or equal to current capacity).
Exceptions
std::overflow_errorIf new_size is too small for current entries, or the bytes of that many buckets do not fit in a size_t.
std::bad_allocIf there is no memory for the new buckets, and anything that copying a key throws when its swap may throw. The table is left as it was.
Complexity: O(n) where n is the number of entries.

Definition at line 888 of file hashDry.H.

References ah_overflow_error_if, OhashCommon< HashTbl, Key >::me(), and OhashCommon< HashTbl, Key >::rebuild().

Referenced by Aleph::Johnson< GT, Distance, Ait, NAit, SA >::Johnson(), OhashCommon< HashTbl, Key >::shrink_after_removal(), and Aleph::Johnson< GT, Distance, Ait, NAit, SA >::wide_arcs().

◆ search_or_insert() [1/2]

template<class HashTbl , typename Key >
Key * OhashCommon< HashTbl, Key >::search_or_insert ( const Key &  key)
inline

Searches for a key, inserting it if not found (copy version).

If the key exists, returns a pointer to it. Otherwise, inserts a copy of the key and returns a pointer to the newly inserted key.

Parameters
keyThe key to search for or insert.
Returns
Pointer to the existing or newly inserted key, or nullptr if the key is not in the table and the table is full and does not resize itself.
Exceptions
Anythingthat copying the key throws, and std::bad_alloc if the table cannot grow. The table is left as it was.
Note
This function is marked [[nodiscard]] - the return value indicates success/failure and provides the key pointer.
Complexity:
  • Average case: O(1) amortized
  • Worst case: O(n) when rehashing is triggered

Definition at line 575 of file hashDry.H.

References OhashCommon< HashTbl, Key >::check_resize_before_insert(), OhashCommon< HashTbl, Key >::me(), r, and OhashCommon< HashTbl, Key >::store_copy().

Referenced by OhashCommon< HashTbl, Key >::operator[]().

◆ search_or_insert() [2/2]

template<class HashTbl , typename Key >
Key * OhashCommon< HashTbl, Key >::search_or_insert ( Key &&  key)
inline

Searches for a key, inserting it if not found (move version).

If the key exists, returns a pointer to it. Otherwise, inserts the key using move semantics and returns a pointer to the newly inserted key.

Parameters
keyThe key to search for or insert (moved from on insert).
Returns
Pointer to the existing or newly inserted key, or nullptr if the key is not in the table and the table is full and does not resize itself.
Exceptions
std::bad_allocIf the table cannot grow, and anything that copying the key throws when the table has to copy it. The table is left as it was; key may have been moved from.
Note
This function is marked [[nodiscard]] - the return value indicates success/failure and provides the key pointer.
Complexity:
  • Average case: O(1) amortized
  • Worst case: O(n) when rehashing is triggered

Definition at line 614 of file hashDry.H.

References OhashCommon< HashTbl, Key >::check_resize_before_insert(), OhashCommon< HashTbl, Key >::me(), r, and OhashCommon< HashTbl, Key >::store_move().

◆ set_with_resize()

template<class HashTbl , typename Key >
void OhashCommon< HashTbl, Key >::set_with_resize ( const bool  with_resize)
inlinenoexcept

Turns the automatic resizing on or off.

The change applies from the next insertion or removal on: turning it on does not rebuild the table at once. Turned off, the table keeps its capacity and its keys do not move by themselves; a full table then takes no more keys.

Parameters
with_resizetrue for the table to grow and shrink by itself.

Definition at line 400 of file hashDry.H.

References OhashCommon< HashTbl, Key >::me().

◆ shrink_after_removal()

template<class HashTbl , typename Key >
void OhashCommon< HashTbl, Key >::shrink_after_removal ( )
inlineprotectednoexcept

Shrinks a table that resizes itself, after a removal took its load factor under the lower threshold.

The table goes to about half its capacity, provided the keys fit there under the upper threshold and leave an empty bucket. With a lower threshold over one half they may not fit; the table then keeps its capacity until enough keys are removed. If the smaller table cannot be built, because there is no memory for it or a key cannot be copied, the table keeps its capacity too: the removal stands.

Definition at line 346 of file hashDry.H.

References Aleph::and, OhashCommon< HashTbl, Key >::current_alpha(), OhashCommon< HashTbl, Key >::me(), Primes::next_prime(), and OhashCommon< HashTbl, Key >::resize().

Referenced by Aleph::ODhashTable< Key, Cmp >::remove(), Aleph::OLhashTable< Key, Cmp >::remove(), and OhashCommon< HashTbl, Key >::remove_ptr().

◆ size()

template<class HashTbl , typename Key >
constexpr size_t OhashCommon< HashTbl, Key >::size ( ) const
inlineconstexprnoexcept

Returns the number of entries in the table.

Returns
The number of stored keys.

Definition at line 954 of file hashDry.H.

References OhashCommon< HashTbl, Key >::const_me().

Referenced by Aleph::analyze_fit(), Aleph::boyer_moore_horspool_search(), Aleph::damerau_levenshtein_distance(), demo_odhash(), demo_performance(), Aleph::find_disjoint_negative_cycles(), Moved_From_Case< Field_Grid >::fingerprint(), Aleph::Gen_MultiPolynomial< Coefficient, MonomOrder >::fit(), Aleph::Gen_MultiPolynomial< Coefficient, MonomOrder >::fit_parallel(), Aleph::Gen_MultiPolynomial< Coefficient, MonomOrder >::fit_ridge(), Aleph::Gen_MultiPolynomial< Coefficient, MonomOrder >::fit_weighted(), Moved_From_Case< Field_Grid >::is_empty(), Moved_From_Case< Distinct_Count_Mo_On_Trees< Graph > >::is_empty(), Moved_From_Case< Distinct_Count_Mo_On_Tree_Node< int > >::is_empty(), Moved_From_Case< MinHash< int > >::is_empty(), Aleph::longest_common_subsequence(), Aleph::longest_common_substring(), make_matrix(), Aleph::map_to_Array(), Aleph::maphash_to_Array(), Aleph::maptree_to_Array(), Aleph::matrix_dims(), Aleph::CA::Graph_Lattice< T >::max_degree(), Aleph::most_negative_cycle_bounded(), Aleph::FlatMap< Key, T, Compare >::operator==(), OhashCommon< HashTbl, Key >::print_stats(), Aleph::rabin_karp_search(), Aleph::solve_transportation(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TYPED_TEST(), Moved_From_Case< Distinct_Count_Mo_On_Trees< Graph > >::use(), Moved_From_Case< Distinct_Count_Mo_On_Tree_Node< int > >::use(), and Moved_From_Case< PersistentHashMap< int, int > >::use().

◆ store_copy()

template<class HashTbl , typename Key >
template<class Bucket >
void OhashCommon< HashTbl, Key >::store_copy ( Bucket *  bucket,
const Key &  key 
)
inlineprivate

Stores a copy of a key in the bucket reserved for it.

If copying the key throws, the reservation is undone, so the table does not keep a bucket that counts as an entry and holds another key.

Template Parameters
BucketThe bucket type of the table. It is a parameter because the table is not a complete type here.
Parameters
bucketBucket that allocate_bucket() or hard_allocate_bucket() has just reserved for key.
keyThe key to store.
Exceptions
Anythingthat copying the key throws. The table is left as it was before the bucket was reserved.

Definition at line 203 of file hashDry.H.

References OhashCommon< HashTbl, Key >::me().

Referenced by OhashCommon< HashTbl, Key >::contains_or_insert(), OhashCommon< HashTbl, Key >::insert(), OhashCommon< HashTbl, Key >::search_or_insert(), and OhashCommon< HashTbl, Key >::store_move().

◆ store_move()

template<class HashTbl , typename Key >
template<class Bucket >
void OhashCommon< HashTbl, Key >::store_move ( Bucket *  bucket,
Key &  key 
)
inlineprivate

Moves a key into the bucket reserved for it.

The key and the previous content of the bucket are swapped. A key whose swap may throw is copied instead: a copy that fails leaves the key intact, so the reservation can be undone.

Template Parameters
BucketThe bucket type of the table.
Parameters
bucketBucket that allocate_bucket() or hard_allocate_bucket() has just reserved for key.
keyThe key to store.
Exceptions
Anythingthat copying the key throws, when it is copied. The table is left as it was before the bucket was reserved.

Definition at line 233 of file hashDry.H.

References OhashCommon< HashTbl, Key >::nothrow_key_swap, and OhashCommon< HashTbl, Key >::store_copy().

Referenced by OhashCommon< HashTbl, Key >::contains_or_insert(), OhashCommon< HashTbl, Key >::insert(), and OhashCommon< HashTbl, Key >::search_or_insert().

Member Data Documentation

◆ nothrow_key_swap

template<class HashTbl , typename Key >
constexpr bool OhashCommon< HashTbl, Key >::nothrow_key_swap
staticconstexprprivate
Initial value:
=
noexcept(std::swap(std::declval<Key &>(), std::declval<Key &>()))

Whether two keys can be swapped without throwing.

The keys whose swap can throw are copied instead, so that a failure leaves the table as it was.

Definition at line 186 of file hashDry.H.

Referenced by Aleph::OLhashTable< int >::contains(), and OhashCommon< HashTbl, Key >::store_move().


The documentation for this class was generated from the following file: