ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
PointerTable.h File Reference

A hash table of pointers with open addressing, beyond the book: the set of the objects of a search (its closed list), each found from its key, e.g., the state of a node. More...

#include "ADS/Contract.h"
#include "ADS/Iterators.h"
#include "ADS/Modification.h"
#include "ADS/Portable.h"
#include "ADS/dictionary/RandomHashing.h"
#include "ADS/memory/Prefetch.h"
#include <algorithm>
#include <bit>
#include <cstddef>
#include <cstdint>
#include <memory>
#include <stdexcept>
#include <type_traits>
#include <utility>
#include <vector>
#include "ADS/dictionary/impl/PointerTable.tpp"

Go to the source code of this file.

Classes

struct  ADS::StateEqual
 the default equality of PointerTable: p->state == key More...
struct  ADS::WidePlaces
 the places of 16 bytes of PointerTable, the default: the whole hash and the pointer to the object More...
struct  ADS::IndexPlaces< S >
 the places of 8 bytes of PointerTable: the high 32 bits of the hash and the index plus 1 of the object in the store \( S \), which gives at( i ) and index( p ) [see the file] More...
struct  ADS::WithPlaces< L, P >
 the subject policy P [see Modification.h] of a PointerTable on the places L, WidePlaces or IndexPlaces< S > More...
class  ADS::PointerTable< T, Eq, A, Policy >
 a hash table of pointers to objects found by their keys, on the places of ADS (the inspection above), its insertions told through Policy More...
struct  ADS::PointerTable< T, Eq, A, Policy >::Places
 the places of the table, for SlotIterator More...

Namespaces

namespace  ADS

Macros

#define ADS_POINTERTABLE_BOOST   0

Detailed description

A hash table of pointers with open addressing, beyond the book: the set of the objects of a search (its closed list), each found from its key, e.g., the state of a node.

A place holds the pointer and the whole hash of 64 bits of the key of its object, so that a probe compares two keys only when their hashes are equal and a growth moves the places without touching the objects; the inspection is linear [see OpenHashTable.h], i.e., along consecutive places and lines of cache, which beats the lists of overflow when the table does not fit in the cache, and the first place of a hash is its top bits after the product by \( \lfloor 2^{64} / \phi \rfloor \) [see fibonacciPlace()], so that hashes whose low bits are regular (e.g., the owner of a state in a parallel search, \( h \bmod p \)) still spread over the table. The load factor stays below \( 7 / 10 \), the table doubling past it. There is no removal, which a search does not need and which would cost the simplicity of the probes. find_or_insert( key , hash , make ) inserts the object that make() returns only if the key is not there, with one inspection; prefetch( hash ) asks for the first place of a hash, so that the misses of the lookups of all the successors of a node overlap. The places come from the allocator A, which may give, e.g., huge pages [see HugePageAllocator.h], and slotsFor( n ) tells how many places \( n \) objects take, so that a table built with them, or reserved by reserve( n ), never grows on the way.

Two keys are equal by eq( p , key ), with p a pointer of the table; by default p->state == key, the canonical node of a state of a search.

With ADS_WITH_BOOST defined (by the option of CMake of the same name, which defines it whenever the headers of Boost 1.83 or later are found, by the makefile, which does the same, or by hand), the places are those of boost::unordered_flat_set, which on the benchmark of the dictionaries inserts and finds 2 to 3 times faster than the linear inspection above, and ADS_POINTERTABLE_BOOST defined to 0 keeps the code of ADS. The interface stays the same, the members and their signatures, the equality Eq, the allocator A (rebound to the places, e.g., huge pages for Boost too) and the constructor from the number of places, with four differences. A place holds the hash and the pointer as above, but the first place of a hash and the inspection are those of Boost (groups of 15 places with a byte of control each, the hash mixed again by a product, since it does not declare is_avalanching). The load factor stays below \( 7 / 8 \), the maximum load of Boost (all the places up to 29 of them), and a table built with \( m \) places has room for \( 7 m / 10 \) objects with no growth, as many as the places of ADS hold, so that a table of slotsFor( n ) places still takes \( n \) objects with no growth, in about as many bytes; capacity() is the number of places of Boost, \( 15 \) times a power of 2, less 1, and reserve( n ) leaves room for \( n \) objects as Boost computes it. Finally, prefetch() does nothing, since Boost has no way to ask for the group of a hash, and the iterator is forward, the one of Boost.

The table tells its changes through the subject policy Policy [see Modification.h], NoObservers by default, with which every operation compiles to the code of the plain table, the same with the places of ADS and with those of Boost: Inserted by find_or_insert() when it inserts, after the insertion, its handle the pointer of the object and its key the hash, Cleared by clear() and by a move from the table, and Rebuilt by a move into the table and at the end of a mute. There is no removal, hence no Erased; a growth moves the places and not the objects, and is not told, nor are the changes of the objects, which the table does not see. The object is made before anybody is told, so that an observer cannot refuse an insertion and one that throws leaves it in the table, which has no removal to undo it. A table with observers is not copied, and a move of a table has no observer.

Two options change the places of ADS, the default staying as above. With the policy WithPlaces< IndexPlaces< S > , P > (the places, then the subject policy P, NoObservers by default), a place is a word of 8 bytes instead of 16: the high 32 bits of the hash and the index plus 1 of the object in a store S given to the constructor, which must outlive the table and give S::at( i ), the object of the index \( i \), and S::index( p ), the index of the object \( p \) (e.g., a pool of nodes in blocks of a power of 2); the places take half the memory, but a probe that finds the high 32 bits of its hash reads the object through the store, and two keys whose hashes differ only in the low bits are told apart by eq alone. The first place is then that of those 32 bits, so that a growth moves the places without touching the objects, as above; there are fewer than \( 2^{32} - 1 \) objects, and past \( 2^{32} \) places the first places no longer cover the table. With an allocator A that declares the member type is_zeroed (its blocks are all 0 bits, e.g., ZeroPageAllocator [see HugePageAllocator.h], whose large blocks are pages of the system not yet written, aligned to huge pages), the table and its growths write no 0 over the empty places, which the system gives as 0s at the first access; an empty place is then all 0 bits, the null pointer included, as on every target of ADS. Either option takes the places of ADS also with ADS_WITH_BOOST, and the events are the same on every place.

The contracts [see Contract.h]: make() returns a pointer that is not nil; the invariant is that the load factor stays below \( 7 / 10 \) and that each object is reached from the first place of its hash with no empty place in between (checked on the key of the operation), and with Boost that the object of the key is found again. The operations are defined in PointerTable.tpp.

See also
D. E. Knuth, The Art of Computer Programming, vol. 3, 2nd ed., Addison-Wesley, 1998, section 6.4 (linear probing, the multiplicative hashing)
E. Burns, M. Hatem, M. J. Leighton, W. Ruml, Implementing fast heuristic search code, Proceedings of the 5th Symposium on Combinatorial Search (SoCS), 25-32, 2012, doi:10.1609/socs.v3i1.18245
J. M. Lopez Munoz, Inside boost::unordered_flat_map, 2022 (the groups of Boost), https://bannalia.blogspot.com/2022/11/inside-boostunorderedflatmap.html
Author
Donato Meoli
Dipartimento di Informatica
Università di Pisa

Macro definition documentation

◆ ADS_POINTERTABLE_BOOST

#define ADS_POINTERTABLE_BOOST   0