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

The x-fast tries of Willard, beyond the book: SORTED SETs of the integers of \( w \) bits, each with a value, in which lowerBound( x ) gives the smallest key \( \ge x \) (the find( x ) of the SSet of Morin's book) in \( O( \log w ) \) expected time. More...

#include "ADS/Contract.h"
#include "ADS/Iterators.h"
#include "ADS/dictionary/BinaryTrie.h"
#include "ADS/dictionary/FlatHashTable.h"
#include "ADS/memory/ObjectPool.h"
#include <array>
#include <concepts>
#include <cstddef>
#include <iterator>
#include <limits>
#include <optional>
#include <utility>
#include <variant>
#include "ADS/dictionary/impl/XFastTrie.tpp"

Go to the source code of this file.

Classes

class  ADS::XFastTrie< U, V, W >
 the x-fast trie: a binary trie with a hash table of the prefixes per depth More...
class  ADS::ListXFastTrie< U, V, W >
 the x-fast trie of a doubly linked list and \( w + 1 \) hash tables (exercise 13.2 of Morin's book) More...
class  ADS::ListXFastTrie< U, V, W >::const_iterator
 the bidirectional iterator along the list More...

Namespaces

namespace  ADS

Detailed description

The x-fast tries of Willard, beyond the book: SORTED SETs of the integers of \( w \) bits, each with a value, in which lowerBound( x ) gives the smallest key \( \ge x \) (the find( x ) of the SSet of Morin's book) in \( O( \log w ) \) expected time.

  • XFastTrie is the trie of section 13.2 of Morin's book: a BinaryTrie [see BinaryTrie.h] with, for each depth \( i = 1 , \ldots , w \), a hash table \( t[ i ] \) from the prefixes of \( i \) bits to their nodes. Since the prefixes of \( x \) that are in the trie are those of depth up to some \( \ell \), \( \ell \) is found by a binary search on the depth, \( O( \log w ) \) lookups, and the deepest node of the path of \( x \) gives the successor by its jump pointer as in the binary trie; insert() and remove() still change \( O( w ) \) nodes and entries of the tables. The space is \( O( n w ) \).
  • ListXFastTrie is the x-fast trie of exercise 13.2 of Morin's book, with no trie: the keys are a doubly linked list in increasing order, and the table \( t[ i ] \) maps each prefix of \( i \) bits to the smallest and the largest key with that prefix. The binary search finds \( \ell \) as above; if the bit \( \ell \) of \( x \) is 0, every key with the prefix of depth \( \ell \) has 1 there and is larger than \( x \), so that the successor is the smallest of them, and if it is 1, every such key is smaller, so that the successor follows the largest of them in the list. A removal replaces the smallest key of a prefix by the next one in the list, the largest by the previous one, the keys of a prefix being consecutive in the list.

Both have also findNear( x ), the search of exercise 13.4 of Morin's book, whose time depends on the distance \( d( x ) \) between \( x \) and its successor (\( 2^w \) if there is none): with \( k = \lceil \log_2( d( x ) + 1 ) \rceil \), the successor lies in \( [ x , x + 2^k ) \), which meets at most two blocks of \( 2^k \) keys aligned to \( 2^k \), those of the prefixes \( p \) and \( p + 1 \) of depth \( w - k \), with \( p = \lfloor x / 2^k \rfloor \). The search tries \( K = 1 , 2 , 4 , \ldots \) until one of the two prefixes of depth \( w - K \) is there, \( O( \log k ) \) steps, and then either searches \( \ell \) between \( w - K \) and \( w - K / 2 \), in \( O( \log K ) \) lookups, or takes the smallest key of the prefix \( p + 1 \): in \( O( 1 ) \) in ListXFastTrie, which keeps it, and by a walk of \( O( K ) \) nodes in XFastTrie. The expected time is \( O( 1 + \log \log d( x ) ) \) for ListXFastTrie, the second point of the exercise, and \( O( 1 + \log d( x ) ) \) for XFastTrie, the first one.

Where it is meant to be faster than the code of Morin's book: the tables are FlatHashTables, which compare a fingerprint of the hash with 15 places at once and touch about one line of cache per lookup, where the book uses its LinearHashTable of pairs, whose probes compare whole keys; a binary search makes \( \lceil \log_2( w + 1 ) \rceil \) lookups that depend on each other, so that the latency of each one is what counts. The nodes come from a pool [see BinaryTrie.h].

The tables are FlatHashTables [see FlatHashTable.h], whose expected time per operation is \( O( 1 ) \). The contracts [see Contract.h]: the keys are below \( 2^w \), insert() ensures that the key is there and remove() that it is not; the invariant (the order of the list and the tables of the prefixes) is checked in \( O( n w ) \) time at the level 3 while \( n \le 256 \), and around the key of the operation beyond. The operations are defined in XFastTrie.tpp.

See also
P. Morin, Open Data Structures (in C++), Edition 0.1G, 2013, https://opendatastructures.org/ods-cpp.pdf, section 13.2 and exercises 13.2 and 13.4
D. E. Willard, Log-logarithmic worst-case range queries are possible in space Theta(N), Information Processing Letters 17(2), 81-84, 1983, doi:10.1016/0020-0190(83)90075-3
Author
Donato Meoli
Dipartimento di Informatica
Università di Pisa