The generic interface List
provides operations on linked lists of
arbitrary element types. This interface extends the operations you
can perform on the generic lists.
GENERIC INTERFACEListFuncs (Elem, List);
WhereElem.T
is the parameter passed to the generic listList
. Thus,Elem.T
contains
CONST Brand = <text-constant>; PROCEDURE Equal(k1, k2: Elem.T): BOOLEAN;as described inList.ig
.List
is the instead ofList.ig
instantiated withElem
.
CONST Brand = "(ListFuncs " & Elem.Brand & ")"; TYPE T = List.T;As with
List.ig
, 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 {\it
destructive}. By convention, their names end in D
.
\index{naming conventions!destructive list operations}
PROCEDURE DeleteD(VAR list: T; READONLY elem: Elem.T): T;
Delete the first occurrance ofelem
inlist
. Return the single element list containing this cell.list
will be modified if required. This happens if the first cell inlist
iselem
, for example.
PROCEDURE DeleteAllD(VAR list: T; READONLY elem: Elem.T): T;Delete the all occurrances of
elem
in list
. Return the list
containing these cells. list
will be modified if required. This
happens if the first cell in list
is an elem
, for example.
PROCEDURE Count(l: T; READONLY e: Elem.T): INTEGER;
Return the number of occurances ofe
inl
. The comparison is performed byElem.Equal
.
PROCEDURE New(READONLY elem: Elem.T): T;Allocate a new list element. Might reuse a cached element which was released by
Free
below.
PROCEDURE Free(list: T): T;Free the single list element pointed at by
list
and return the
remainder of the list pointed at by list.tail
. NOTE: since
freed elements may be retained, you should clear list.head
if it
contains a reference you don't want to be retained.
END ListFuncs.