14 Container Library

One of the strengths of the Mjølner System is the large set of available libraries. One of very useful libraries, is the container libraries. The container library supports a number of different ways to store data: sets, multisets, lists, hashtables, stacks, etc. Here we will show how to use the list and the hashTable.

14.1 List Example

The list library is available in the '~beta/containers/list', thus this library must be included when using lists.

We will use the directory example above, and make a list to store the entries of the directory.

A list is simply declared as follows:

dirList: List
 (# element:: Text #);

Here we declare a pattern named dirList that inherits from the list pattern in the '~beta/containers/list' library. We specify the type of the elements in the list by extending the virtual pattern element to be the type of text. For details about the BETA concept of virtual patterns, see the BETA language introduction [MIA 94-26].

The dirList pattern inherits an operation from the list pattern that can be used to add elements, so we can simply add an element to the list by:

dirname[] -> dirList.append;

Lists have several other operations. Some useful operations are briefly described in the table below. Please see the container manual [MIA 92-22] for more details.

l.clear Removes all elements currently in the list, making it empty
l.empty -> b Returns true if the list is empty
l.size -> i Returns the number of elements currently in the list
equal:: (# ... #) Defines the equality test used by the implementation of the different operations. Users of list must further bind equal to contain the proper equality test for the specified element type. Default equality test for equal references (i.e. the same object)
e[] -> l.has Takes an element, and checks whether it is in the list
l.scan(# ... #) Scans through the list, invoking INNER for each element in the list. In each turn of the scan, "current" refers to the current element in the list.
l.copy-> ll[] Default copy is one-level (shallow) copying. I.e. copying the list and all objects in the list.
elm[]->l.prepend insert elm as first element
elm[]->l.append insert elm as last element

In the table above, it is mentioned, that the equality test should always be defined. For our directory list this can be done like:

dirList: List
 (# element:: Text;
    equal:: 
      (# (* since the element type is text simply test whether the
          * two text strings are equal
          *)
      do left[]->right.equal->value;
      #);
 #);

The extended directory listing program can the be as follows:

Program 18: SaveListDir.bet

ORIGIN '~beta/basiclib/directory';
INCLUDE '~beta/containers/list';
---program: descriptor---
(# dirList: @List
     (# element:: Text;
        equal:: 
          (# (* since out element type is text simply test whether the
              * two text strings are equal
              *)
          do left[]->right.equal->value;
          #);
     #);
   d: @directory;
do (if noOfArguments <> 2//true then
       'Usage: '->puttext; 1->arguments->puttext; ' path'->putline;
       stop;
   if);
   (* set name of directory *)
   2->arguments->d.name;
   (* print name of directory *)
   newline;
   (* initialize list *)
   dirList.init;
   (* scan the entries and append to list *)
   d.scanEntries
   (# (* found refers to the current entry *)
   do found.path->dirList.append;
   #);
   (* dirList now contains all the names of the entries in the
    * directory
    *)
   dirList.scan
   (# (* current referns to the current text element *)
   do current[]->putline; (* print the text *)
   #);
#)

Later we shall see how this list can be saved (persistent) and used in another program.


Libraries Tutorial
© 1994-2002 Mjølner Informatics
[Modified: Thursday October 19th 2000 at 14:10]