![]() |
Basic data types | ![]() |
In this chapter, we give a rough description of TeXmacs's basic data types, most of which can be found in the directory Kernel of the C++ sources (
src/src/Kernel
). The description of the exported functions is non exhaustive and we refer to the corresponding header files for more precision. How these types are implemented, what their operations cost and which pitfalls follow are explained in inside the kernel.
The file
System/Misc/fast_alloc.hpp
declares the TeXmacs memory allocation routines. Objects should be created with tm_new<T> (args) and destroyed with tm_delete; arrays are handled by tm_new_array and tm_delete_array. These routines are very fast for small sizes (below MAX_FAST bytes), since for each such size, TeXmacs maintains a linked list of freed objects of that size. Larger blocks are allocated with the standard malloc. There is no garbage collection: memory is managed through reference counting.
Modulo a few exceptions, all TeXmacs composite data structures are constructed using the macros CONCRETE, ABSTRACT, CONCRETE_NULL and ABSTRACT_NULL (as well as their _TEMPLATE variants), which are defined in
Kernel/Abstractions/basic.hpp
. Consequently, these data structures are pointers to representation classes, which derive from concrete_struct or abstract_struct, which may be abstract in the case of ABSTRACT and ABSTRACT_NULL, and which always contain a reference counter. Because of the reference counter, the C++ copy operator is very fast. Most of the implemented data structures also export a function copy, which should be used if one really wants to physically duplicate an object. Conversely, one should keep in mind that an assignment like a= b makes a and b share the same representation, so that a subsequent in-place modification of b also affects a.
For classes constructed using CONCRETE_NULL or ABSTRACT_NULL, the pointer to the representation class is allowed to be NULL and we have a default constructor which initializes this pointer with NULL. Instances of these classes are tested to be NULL using the function is_nil. Examples of such classes are lists, commands and widgets.
TeXmacs implements three “array-like” structures:
string (
Kernel/Types/string.hpp
) is the string type, which may contain '\0' characters. Strings are sequences of bytes; the interpretation of these bytes is described in the chapter on the general architecture.
tree (
Kernel/Types/tree.hpp
) is the tree type. A tree is either atomic (a leaf labeled by a string) or compound (a node labeled by a tree_label with an array of children).
array<T> (
Kernel/Containers/array.hpp
) is the generic array type with elements of type T.
Array-like structures export the following operations:
N computes the length of an array.
[] accesses an element.
<< is used for appending elements or arrays.
For strings, * concatenates two strings and s (start, end) extracts a substring. For arrays, the analogous operations are append and range; for trees, t (start, end) yields a tree with the same label and a subrange of the children.
For an atomic tree t, t->label yields the string label of the tree. For a compound tree, L(t) yields its label, A(t) the array of its children and t[i] its i-th child. The predicates is_atomic and is_compound distinguish between both kinds of trees and is_func (t, lab, n) tests whether t is a compound tree with label lab and arity n. The second argument of << for trees is either a tree or an array of trees. Besides its label or its children, every tree also carries an observer (the field obs), which is used for keeping track of modifications (see
Kernel/Abstractions/observer.hpp
and the chapter on the general architecture).
The implementation has been made such that the << operation is fast, which is useful when considering arrays as buffers. Actually, the allocated space for arrays with more than five elements (resp. strings with more than 23 characters) is always a power of two, so that new elements can be appended quickly (see round_length in
array.cpp
and
string.cpp
).
Generic lists are implemented by the class list<T> in
Kernel/Containers/list.hpp
. The “nil” list is created using list<T>(), an atom using list<T>(T x) and a general list using list<T>(T x, list<T> next). If l is a list, l->item and l->next correspond to its label and its successor respectively (car and cdr in Scheme). The functions is_nil and is_atom test whether a list is nil or an atom. The function N computes the length of a list, l * x appends an element at the end, and reverse, last_item, head and tail have the usual meanings.
The type list<int> is also denoted by path (
Kernel/Types/path.hpp
), because some additional functions are defined for it. Indeed, paths are used for accessing descendants in tree like structures. For instance, we implemented the functions tree& subtree (tree& t, path p), path_up, path_less and p / q, which removes the prefix q from p. The inverse paths which are stored in typeset boxes are also of type path; see the chapter on boxes.
The hashmap<T,U> class (
Kernel/Containers/hashmap.hpp
) implements hash tables with entries in T and values in U. A function hash should be implemented for T (see
Kernel/Containers/hashfunc.hpp
). The constructor hashmap<T,U> (U init) specifies the default value init which is returned for keys without an entry. Given a hash table H, we set elements through
H(x)= y; |
and access elements through
H[x] |
The methods H->contains (x) and H->reset (x) test whether x has an entry, resp. remove the entry for x. Similarly, hashset<T> implements sets of elements of type T.
We also implemented a variant rel_hashmap<T,U> of hash tables (
Kernel/Containers/rel_hashmap.hpp
), which also have a list-like structure. The methods extend and shorten push and pop a new level of definitions, which makes them useful for implementing recursive environments. They are used for instance by the LaTeX importer in order to handle local macro definitions.
iterator<T> (
Kernel/Containers/iterator.hpp
) implements generic iterators. An iterator it over the keys of a hash table H is obtained using iterate (H); one then loops using while (it->busy ()) { T x= it->next (); … }.
pair<T1,T2> and similar tuples (
Kernel/Containers/ntuple.hpp
), hash trees (
hashtree.hpp
) and promises (
promise.hpp
).
command (
Kernel/Abstractions/command.hpp
) implements abstract commands, with a virtual method apply.
blackbox (
Kernel/Abstractions/blackbox.hpp
) allows to store values of an arbitrary type in a type-safe way.
observer and modification (
Kernel/Abstractions/observer.hpp
,
Kernel/Types/modification.hpp
) implement the observers attached to trees and the elementary modifications of trees.
rectangle and rectangles (
Kernel/Types/rectangles.hpp
) implement rectangles and lists of rectangles.
space (
Kernel/Types/space.hpp
) implements stretchable spaces with a minimal, a default and a maximal size.
url (
System/Classes/url.hpp
) implements file names, which may be local or remote, and search paths.
Files are read and written using functions like load_string and save_string in
System/Files/file.hpp
, and timers using functions like texmacs_time and bench_start in
System/Classes/tm_timer.hpp
.