cai_lw's competitive programming library
 
Loading...
Searching...
No Matches
cplib::HashTable< T, Hash, Eq > Class Template Reference

Linear probing hash table with std non-compliant interface. More...

#include <table.hpp>

Public Types

using size_type = std::size_t
 
using value_type = T
 
using cell_iterator = typename std::vector< impl::HashCell< T > >::iterator
 

Public Member Functions

 HashTable ()
 Constructs an empty hash table.
 
template<typename InputIt >
 HashTable (InputIt first, InputIt last)
 Constructs a hash table containing elements from a pair of iterators.
 
size_type size () const
 Returns the number of elements in the hash table.
 
bool empty () const
 Returns whether the hash table contains no element.
 
size_type capacity () const
 Returns the size of the underlying array of cells.
 
cell_iterator cell_begin ()
 Returns pointer to the beginning of the cell array.
 
cell_iterator cell_end ()
 Returns pointer to the end of the cell array.
 
template<bool Revive = false>
cell_iterator find_cell (const T &x)
 Find an element that compares equal to the given value, or an empty cell if not found.
 
bool contains (const T &x) const
 Returns whether an element that compares equal to the given value is found in the hash table.
 
template<bool Replace = false>
bool insert (const T &x)
 Insert an element if there is no element already present that compares equal to it.
 
template<bool Replace = false>
bool insert (T &&x)
 Insert an element if there is no element already present that compares equal to it.
 
bool erase (const T &x)
 Remove the element that compares equal to the given value if it is present.
 
void rehash (size_type new_cap)
 Allocate the given capacity, and rehash all elements.
 
void reserve (size_type new_size)
 Reserve capacity and temporary disable shrinking.
 

Detailed Description

template<typename T, typename Hash = WyHash<T>, typename Eq = std::equal_to<T>>
class cplib::HashTable< T, Hash, Eq >

Linear probing hash table with std non-compliant interface.

This hash table aims to be fast for typical competitive programming use cases. Insertion, lookup and deletion takes expected \(O(1)\) time.

Only the lower bits of the hash value are used, so the hash function must be reasonably chaotic, and for integer types std::hash is not acceptable. By default it uses WyHash.

Due to lazy deletion, there are two load factors, the larger "non-empty" load factor which counts cells marked for deletion, and the smaller "occupied" load factor which doesn't. The rehashing policy is that both load factors are always within \([\frac{1}{8},\frac{1}{2}]\), unless the capacity is manually set (see HashTable::reserve). The lower bound is required for \(O(N)\) traversal. In addition, the capacity is always a power of two, and is at least 4.

Template Parameters
TType of elements.
HashHash function object.
EqEquality comparison function object.

Constructor & Destructor Documentation

◆ HashTable() [1/2]

template<typename T , typename Hash = WyHash<T>, typename Eq = std::equal_to<T>>
cplib::HashTable< T, Hash, Eq >::HashTable ( )
inline

Constructs an empty hash table.

Note that this constructor allocates cells for a capacity of 4 even for an empty hash table.

◆ HashTable() [2/2]

template<typename T , typename Hash = WyHash<T>, typename Eq = std::equal_to<T>>
template<typename InputIt >
cplib::HashTable< T, Hash, Eq >::HashTable ( InputIt  first,
InputIt  last 
)
inline

Constructs a hash table containing elements from a pair of iterators.

If the iterators are random access iterators, the number of elements is known, so sufficient space will be allocated in advance, so that no rehashing happens during construction.

Member Function Documentation

◆ capacity()

template<typename T , typename Hash = WyHash<T>, typename Eq = std::equal_to<T>>
size_type cplib::HashTable< T, Hash, Eq >::capacity ( ) const
inline

Returns the size of the underlying array of cells.

This is always a power of two.

◆ cell_end()

template<typename T , typename Hash = WyHash<T>, typename Eq = std::equal_to<T>>
cell_iterator cplib::HashTable< T, Hash, Eq >::cell_end ( )
inline

Returns pointer to the end of the cell array.

This IS safe to dereference and always points to a sentinel cell. Iterator adaptors that skip unoccupied cells can check for the sentinel cell instead of storing and comparing with the end iterator.

◆ find_cell()

template<typename T , typename Hash = WyHash<T>, typename Eq = std::equal_to<T>>
template<bool Revive = false>
cell_iterator cplib::HashTable< T, Hash, Eq >::find_cell ( const T &  x)
inline

Find an element that compares equal to the given value, or an empty cell if not found.

Template Parameters
ReviveIf true, may instead return a "phantom" (marked for deletion) cell when the value is not found. Used by HashTable::insert to reuse "phantom" cells and reduce rehashing.

◆ insert() [1/2]

template<typename T , typename Hash = WyHash<T>, typename Eq = std::equal_to<T>>
template<bool Replace = false>
bool cplib::HashTable< T, Hash, Eq >::insert ( const T &  x)
inline

Insert an element if there is no element already present that compares equal to it.

Template Parameters
ReplaceWhen an element that compares equal to x is found, will replace it if true, and will do nothing if false.

◆ insert() [2/2]

template<typename T , typename Hash = WyHash<T>, typename Eq = std::equal_to<T>>
template<bool Replace = false>
bool cplib::HashTable< T, Hash, Eq >::insert ( T &&  x)
inline

Insert an element if there is no element already present that compares equal to it.

Template Parameters
ReplaceWhen an element that compares equal to x is found, will replace it if true, and will do nothing if false.

◆ rehash()

template<typename T , typename Hash = WyHash<T>, typename Eq = std::equal_to<T>>
void cplib::HashTable< T, Hash, Eq >::rehash ( size_type  new_cap)
inline

Allocate the given capacity, and rehash all elements.

Parameters
new_capThe new capacity. Must be a power of two and at least 4.

◆ reserve()

template<typename T , typename Hash = WyHash<T>, typename Eq = std::equal_to<T>>
void cplib::HashTable< T, Hash, Eq >::reserve ( size_type  new_size)
inline

Reserve capacity and temporary disable shrinking.

If current capacity can hold at least new_size non-empty cells, does nothing. Note that if there are already some deleted cells, which are also considered non-empty, the hash table may not be able to hold new_size elements without rehashing.

Otherwise, rehashes and allocates enough capacity for holding at least new_size non-empty cells, After this, the capacity will never "naturally" shrink (due to low load factor), unless it has "naturally" grown (due to high load factor) at least once.

Note that the cost of traversing the hash table is proportional to its capacity, not its size. Thus traversal should be avoided after calling this function until all desired elements are inserted.


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