GNU TeXmacsUser manualReferenceAboutEnglish
Strings, arrays, lists, trees and hash tables

1.Strings and arrays

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.

2.Lists

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>.

3.Trees and tree labels

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:

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.

4.Hash tables

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).

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:

rel_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

).

hashfunc

A function with a cache of its results (

hashfunc.hpp

).

hashtree

A trie: each node has a value and a hash map of children (

hashtree.hpp

).

hashmap<string,tree> extras

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.

Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.1 or any later version published by the Free Software Foundation; with no Invariant Sections, with no Front-Cover Texts, and with no Back-Cover Texts. A copy of the license is included in the section entitled "GNU Free Documentation License".