![]() |
Strings, arrays, lists, trees and hash tables | ![]() |
A string (
string.hpp
) is a handle to a string_rep with a length n and a buffer a of bytes, which may contain zeros and is not terminated; use c_string to obtain a C string. An array<T> (
array.hpp
) is the same with elements of type T.
The buffers grow by doubling: round_length gives the allocated size for a length n, which is n itself for short arrays (fewer than 6 elements) and short strings (rounded to 4 bytes below 24), and otherwise the smallest power of two at least n (at least 8 elements, resp. 32 bytes). Appending with << is therefore cheap on average, while s1 * s2, append and s (i, j) always build a new object. resize reallocates the buffer whenever the rounded size changes, in either direction, so a length which keeps crossing a power of two, or any change of a very short array, reallocates every time.
The hash function of strings (
string.cpp:203
) rotates and adds the bytes; hashing a string costs its length.
A list<T> (
list.hpp
) is a nil handle or a cell with item and next. Only the operations at the front are cheap. N (l), last_item, l * x (append at the end), l1 * l2, copy and == walk the whole list recursively, the appending functions copy every cell (
list.cpp:151
), and the in-place l << x also walks the whole list. Lists are fine for short sequences such as paths and the chains of hash tables; a loop which appends with l= l * x is quadratic, and a very long list can exhaust the stack in these functions. path is list<int>.
A tree (
tree.hpp
) points to a tree_rep, which holds the label op and the observer obs of the tree, and is one of three classes:
atomic_rep, for strings: the label is TMSTRING (0) and the string is in label (accessed as t->label);
compound_rep, for compound trees: a positive label and an array<tree> of children (A (t), t[i]);
generic_rep, with a negative label: a tree which wraps an arbitrary C++ value as a blackbox (
generic_tree.hpp
), used to pass C++ objects through Scheme and tree-based interfaces.
The default constructor tree () creates the empty string, not a nil tree. Equality t1 == t2 compares the trees recursively; strong_equal compares the representations. hash (t) and copy (t) also traverse the whole tree.
Labels are integers of the enumeration tree_label (
tree_label.hpp
): the primitives of the document format have fixed values, and any other name becomes an extension label the first time it is used, through make_tree_label or compound (name, …). The two tables CONSTRUCTOR_NAME and CONSTRUCTOR_CODE translate in both directions; they only grow, and the numbers of the extensions depend on the order in which names are met, so they must not be stored. Unknown names give UNKNOWN with as_tree_label.
The observer field is what makes the editing machinery work: every tree of a document can carry observers, which are told about modifications (see the general architecture and undo). Since the observers belong to the representation, a tree shared between two places of a document shares its observers too.
A hashmap<T,U> (
hashmap.hpp
,
hashmap.cpp
) has an array of n buckets, a power of two, each a list of entries holding the hash value, the key and the value; a key goes in bucket hash (key) & (n-1). The table also stores a default value init, given to the constructor (or the default value of the type for the default constructor).
H[x] (bracket_ro) returns the value, or init if there is none, and never modifies the table.
H(x) (bracket_rw) returns a reference to the value, creating the entry with init if there is none. It is meant for assignments, H(x)= y, but also inserts when only used for reading.
When the number of entries reaches n times max (1 by default), the table doubles (resize,
hashmap.cpp:49
), rebuilding every bucket with new list cells. reset (x) removes an entry and halves the table when it becomes less than half full; clear empties the buckets but keeps their number, and also, by mistake, the count of entries, so that N (H) and empty are wrong after it (see the pitfalls).
hashset<T> is similar, without values; it grows but never shrinks. iterate (H) (
iterator.cpp
) returns an iterator which keeps the table and walks its buckets lazily, so the order of the keys is that of the buckets, which depends on the hash values (for pointers, on addresses) and changes when the table grows. Code which needs a stable order sorts the keys.
The hash functions are defined next to the types: integers hash to themselves, pointers to a value computed from their address, strings and trees as above. For a type used as a key, hash and == must agree.
Relatives of hashmap:
A chain of hash maps for nested scopes; extend pushes a level and shorten pops it, and a lookup goes down the chain (
rel_hashmap.hpp
).
A function with a cache of its results (
hashfunc.hpp
).
A trie: each node has a value and a hash map of children (
hashtree.hpp
).
The typesetting environment is a hashmap<string,tree>, for which
hashmap_extra.cpp
adds patches: changes, invert, pre_patch, post_patch and write_back compute and apply the differences between two environments.