15.9 Pointers and Generic Structures

With all the caveats of the previous section ringing in ones ears, it scarcely seems appropriate to propose a scheme for more generic data types that uses even more pointers than thus far. However, as seen in section 15.7, standard Modula-2 has some limitations that need to be worked around if one is going to use structures like the list, stack, queue, and trees in the most generic way possible, that is, where the code can be written and compiled without reference to the type itself.

Yet another solution to the problem of making code is not to have any actual data in the data node, only its address. If the code can be constructed to work this way, there can be some efficiencies as well, because there will be a lot of copying that can be avoided.

With this in mind, here is yet another attempt to make the creation and management of lists generic. This time, only a stripped down definition module is presented, and the implementation is left to the student. Even the comments have been removed, as they are the same as in the version found in section 15.6.

DEFINITION MODULE Lists;

(* Generic implementation of lists (not safely type checked) *)
(* copyright © 1995 by R. Sutcliffe *)
(* last modification 1995 03 31 *)

FROM SYSTEM IMPORT
  ADDRESS;

TYPE
  List;
  Operation = (insert, delete, fetchup);

PROCEDURE Create () : List;

PROCEDURE Discard (VAR list : List);

PROCEDURE Length (list : List) : CARDINAL;

PROCEDURE SetAtHead (VAR list : List; op : Operation);

PROCEDURE SetAtTail (VAR list : List; op : Operation);

PROCEDURE SetAtPos (VAR list : List; op : Operation; itemNum : CARDINAL);

PROCEDURE Insert (VAR list : List; item : ADDRESS);

PROCEDURE Append (VAR list : List; item : ADDRESS);

PROCEDURE Update (VAR list : List; item : ADDRESS);

PROCEDURE Fetch (list : List; VAR item : ADDRESS);

PROCEDURE Delete (VAR list : List);

END Lists.

The implementation of this module is left as an exercise for the reader.


Contents