GNU TeXmacsUser manualReferenceAboutEnglish·Français
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.

1.Memory allocation and data structures in TeXmacs

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.

2.Array-like structures

TeXmacs implements three “array-like” structures:

Array-like structures export the following operations:

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

).

3.Lists and paths

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.

4.Hash tables

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.

5.Other data structures

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