|
ADS
Algorithms and Data Structures
|
The trie of exercise 13.3 of Morin's book, beyond the book: a SORTED SET of strings of any length, each with a value, whose lowerBound( s ) (the smallest string \( \ge s \) in the lexicographic order of the bytes, a prefix before its extensions), insert( s ) and remove( s ) take \( O( |s| ) \) time, whatever the number of strings. More...
#include "ADS/Contract.h"#include "ADS/Iterators.h"#include <array>#include <bit>#include <cstddef>#include <cstdint>#include <iterator>#include <optional>#include <string>#include <string_view>#include <utility>#include <variant>#include <vector>#include "ADS/dictionary/impl/BitmapTrie.tpp"Go to the source code of this file.
Classes | |
| class | ADS::BitmapTrie< V > |
| a trie of strings with the bitmaps of the children, the list of the strings and the smallest and largest string below each node More... | |
| class | ADS::BitmapTrie< V >::const_iterator |
| the bidirectional iterator along the list More... | |
Namespaces | |
| namespace | ADS |
The trie of exercise 13.3 of Morin's book, beyond the book: a SORTED SET of strings of any length, each with a value, whose lowerBound( s ) (the smallest string \( \ge s \) in the lexicographic order of the bytes, a prefix before its extensions), insert( s ) and remove( s ) take \( O( |s| ) \) time, whatever the number of strings.
It extends the BinaryTrie [see BinaryTrie.h] from strings of \( w \) bits to strings of bytes: the node at depth \( i \) stands for a prefix of \( i \) bytes, a string ends at the node of its last byte, the strings are also a doubly linked list in increasing order, and each node keeps the smallest and the largest string below it, which play the part of the jump pointers. The exercise suggests a hash table of the children in each node; here a node has instead a bitmap of 256 bits of the bytes of its children, and the children themselves in an array sorted by their byte, so that the child of the byte \( c \) is at the rank of \( c \) among the bits set (a population count, as in the hash array mapped tries of Bagwell), with no hashing, and the next child after \( c \) is the next bit set, which a hash table could not give. A search that stops at the node \( u \) of depth \( i \), whose child of \( s_i \) is missing, thus returns the smallest string below the next child of \( u \), if there is one, and otherwise the string after the largest one below \( u \), \( O( 1 ) \) time after the walk of \( O( |s| ) \) nodes.
An insertion adds the missing nodes of the path of \( s \), links the new string before its successor, and, going back to the root, makes it the smallest string of each node in which nothing precedes it (no string ends there and no child has a smaller byte) and the largest of each node in which nothing follows it; a removal unlinks it, removes the nodes of its path left empty, and replaces it, where it was the smallest or the largest string of a node, by the next or the previous one in the list, since the strings below a node are consecutive in the list.
The contracts [see Contract.h]: insert() ensures that the string is there and remove() that it is not; the invariant (the list in order, the smallest and the largest string of each node, the bitmaps) is checked in \( O( N ) \) time, \( N \) the number of nodes, at the level 3 while there are at most 256 strings. The operations are defined in BitmapTrie.tpp.