|
ADS
Algorithms and Data Structures
|
A hash map whose pairs lie in a dense vector, beyond the book: the pairs \( ( \mathit{key} , \mathit{value} ) \) are the elements \( 0 , \ldots , n - 1 \) of a vector, in the order of their insertions up to the removals, and a separate index of open addressing maps a key to the position of its pair in the vector. More...
#include "ADS/Abi.h"#include "ADS/Contract.h"#include "ADS/Modification.h"#include "ADS/dictionary/FastHash.h"#include "ADS/dictionary/FlatHashTable.h"#include "ADS/memory/AllocatorAware.h"#include <bit>#include <cstddef>#include <cstdint>#include <cstring>#include <functional>#include <memory>#include <new>#include <optional>#include <tuple>#include <type_traits>#include <utility>#include <vector>#include "ADS/dictionary/impl/DenseHashMap.tpp"Go to the source code of this file.
Classes | |
| class | ADS::DenseHashMap< K, V, H, Eq, A, Policy > |
| a hash map with its pairs in a dense vector and an index of positions, its changes told through Policy More... | |
Namespaces | |
| namespace | ADS |
A hash map whose pairs lie in a dense vector, beyond the book: the pairs \( ( \mathit{key} , \mathit{value} ) \) are the elements \( 0 , \ldots , n - 1 \) of a vector, in the order of their insertions up to the removals, and a separate index of open addressing maps a key to the position of its pair in the vector.
A visit of the pairs is then a scan of \( n \) contiguous pairs, with no empty place in between, and the index is rebuilt from the vector without moving a pair; the price is one more access per search (the index, then the pair) and iterators that an insertion or a removal may invalidate, as for a vector [see FlatHashTable.h for the table whose pairs never move].
The index is made of groups of one line of cache, 64 bytes: 16 control bytes, of which the first 12 are those of the 12 places of the group, empty (0) or full, in which case the byte is a fingerprint of 8 bits of the hash of the key, never 0, and the 16th is a byte of overflow, and 12 positions of 32 bits in the vector. The control bytes are compared with the fingerprint of a key all at once, by the vectors of Highway or by broadword operations as in FlatHashTable, and the positions of the places that match are in the same line, so that a search costs one miss in the index and one in the vector. The hash is mixed as in FlatHashTable: the top bits give the first group, the low 8 bits the fingerprint and the bits 8 to 10 the bit of the key in the bytes of overflow; the \( G \) groups, a power of 2, are inspected in the triangular order \( g_i = ( g_0 + i ( i + 1 ) / 2 ) \bmod G \), an insertion that goes past a full group sets in its byte of overflow the bit of its key, and a search stops at the first group whose overflow lacks the bit of its key.
A removal empties the place of the key in the index and moves the last pair of the vector into the position left free (a swap with the last and a pop), so that the vector stays dense: the place of the last pair in the index is found by the inspection of its key, comparing the positions and not the keys, and takes the new position. The place emptied is written as a whole group, at an address that depends only on the hash, so that the loads of the next operations need not wait for the control bytes [see FlatHashTable.h]. A place emptied in a group with some overflow still counts as full until the next rebuild, while one emptied in a group with none is free at once; since only a full group gets some overflow, the places counted are exactly those emptied in groups with some, one more per removal there and one less per insertion there, with no scan of the groups, and when the full places and the counted ones reach \( 7 m / 8 \) of the \( m \) places the index is rebuilt, twice as large if the pairs exceed half of \( 7 m / 8 \). A rebuild reads the keys in the vector in order, hashes them again and fills the new groups, each in the order of its places; so each operation costs \( O( 1 ) \) expected amortized time, with the expected number of groups inspected by a search bounded by a constant for a load at most \( 7 / 8 \) and a hash that behaves as random.
Space: \( 16 n \) bytes for pairs of 16 bytes, plus 64 bytes per 12 places of the index, about \( 5.3 \) bytes per place, at a load between \( 7 / 16 \) and \( 7 / 8 \) after the first growth.
The iterators are those of the vector of the pairs, constant, in the order of the positions; an insertion of a new key may move the vector and invalidates them, a removal moves the last pair into the position of the removed one, and an insertion of a key already there replaces its value in place. find() gives a value to change in place, and try_emplace() the value of a key found or inserted by one search, as that of std::unordered_map. The keys and the values must be movable; there are fewer than \( 2^{32} \) pairs.
The map 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 map: Inserted by insert() and by try_emplace() of a key that is not there (with the value built), Assigned by insert() of a key that is there (the old and the new value), Erased by remove() of a key that is there, told before the removal with its value, Cleared by clear() and by a move from the map, and Rebuilt by swap(), hence by an assignment, to the observers of both maps, and at the end of a mute; the handle of an event is its key. The writes through the pointers that find() and try_emplace() give are not told, and an observer that needs them reads the value again; a removal moves the last pair into the place of the removed one, which is internal maintenance and is not told either, nor is a rebuild of the index. An observer that refuses a change by prepare() leaves the map as it was, and one that throws from on() of Inserted or Assigned gets the change undone (the strong guarantee). A copy or a move of a map has no observer, a map of static observers is not copied, and a map with observers is assigned only when its allocators are all equal (by the swap above).
The pairs and the groups come from the allocator A, with the rules of the standard [see AllocatorAware.h]; with the default one, std::allocator, the groups come from operator new aligned to their line, as they did before the map took an allocator.
The contracts [see Contract.h]: after insert() the key is in the map and after remove() it is not; the invariant is that each position of the vector is held by exactly one full place of the index, reached by the inspection of its key, and that the full places and the bound of those counted stay at most \( 7 / 8 \) of the places; on an index of more than 1024 places, only the inspection of the key of the operation is checked. The operations are defined in DenseHashMap.tpp.