Lists

Treescript has built-in support for linked lists and list trees. Both are based on a data structure called a list node, which contains 2 pointers, a left pointer and a right pointer. Every linked list has an associated data type, in which the left pointer always points to an object of this type or any of its subclasses, and the right pointer always points to another list node or is nil.

 

List trees consist of one or more list nodes, in which both the left and the right pointers can point to another list node or an object of any type. To determine whether or not a given pointer points to another list node, use the built-in boolean function atomic. This function returns false if its single argument points to a list node, and true if it points to an ordinary object.

 

List Trees

The following procedure demonstrates how to create a simple list tree containing 2 list nodes:

 

(proc tree-demo

  var (

    list my-node (new list);

    list root;

    My-class my-obj (new My-class);

  )

  do (

    pchild my-node my-obj;

    pnext my-node (new list);

    = root my-node;

    = my-node (pnext my-node);

    if (atomic (pchild root)) then (

      \ yes it is atomic

    );

    if (not (atomic root)) then (

      \ no it's not atomic

    );

  )

)

 

Linked Lists

The List class is used to maintain linked lists in which the left pointer of each list node points to an object of a given type. To declare an object of class List, use the keyword "list" followed by a class name, followed by the name of the object. Example:

 
list Foo my-list;
 

All elements of my-list are assumed to be objects of the class Foo or any of its sub-classes. Use the colon (:) operator to refer to the n-th element of the list, example:

 
(: my-list n)

 

\ Summary of List class

 

(class List

  var (private)(

    list first-node;

    list last-node;

    list curr-node;

    int curr-idx;     \ index of current element

    int count;        \ no. of elements in list

  )

  (proc first ... )   \ go to top of list

  (proc last ... )    \ go to bottom of list

  (proc next ... )    \ go to next element of list

  (proc prior ... )   \ go to previous element of list

  (func boolean is-top ... )          \ top of list?

  (func boolean is-bottom ... )       \ bottom of list?

  (func int get-curr-idx ... )        \ get index of current element

  (func int get-count ... )           \ get no. of elements in list

  (func list get-obj ... )            \ get ptr to current element

  (func list get-node ... )           \ get ptr to current list node

  (func list get-nth (int idx) ... )   \ get ptr to nth element

  (func boolean goto (int idx) ... )   \ go to nth element of list

  (proc add (list obj) ... )          \ insert obj at end of list

  (proc insert (list obj) ... )       \ insert obj before current element

  (proc replace (list obj) ... )      \ replace current element with obj

  (proc delete ... )                  \ delete current element

  (proc set-nth (int idx; list obj) ... )   \ set ptr to nth element

)