Overview

The List interface is an example of a generic interface. It can hold any type of data, as long as that data is declared to be type T in some interface and the interface declares an appropriate Brand constant and Equal procedure. Be careful when using the Nth() procedure for a List, because it searches from the beginning of the list every time which can make iterating over the list very expensive.

The Details

The List interface

GENERIC INTERFACE List(Elem);
Where "Elem.T" is not an open array type and "Elem" contains
| CONST Brand = ;
| PROCEDURE Equal(k1, k2: Elem.T): BOOLEAN;
"Brand" must be a text constant. It will be used to construct a brand for any generic types instantiated with the "List" interface. For a non-generic interface, we recommend choosing the name of the interface.

"Equal" may be declared with a parameter mode of either "VALUE" or "READONLY", but not "VAR".

A "List.T" represents a linked list of items of type "Elem.T".

None of the operations of this interface modify the "head" field of an existing list element. Operations that may modify the "tail" field of existing list elements are called destructive . By convention, their names end in "D".

The ListSort interface

The generic interface "ListSort" extends the generic interface "List" with sorting operations.
GENERIC INTERFACE ListSort(Elem, ElemList);
Where "Elem.T" is not an open array type, "ElemList" equals "List(Elem)", and "Elem" contains
| CONST Brand = ;
| PROCEDURE Compare(e1, e2: Elem.T): [-1..1];
"Brand" must be a text constant. It will be used to construct a brand for any generic types instantiated with the "ListSort" interface. For a non-generic interface, we recommend choosing the name of the interface.

"Compare" must be a total order. It may be declared with any parameter mode, but must have no visible side-effects.

The implementation is time- and cons-efficient but not guaranteed to be stable. "Sort" copies the cells; "SortD" modifies the "tail" fields of the existing cells.

Using the interfaces

List

ListSort


garrett@cs.washington.edu