(****************************************************************)
(*                                                              *)
(*         Gardens Point Modula-2 Library Definition            *)
(*                                                              *)
(*                                                              *)
(*     (c) Copyright 1996 Faculty of Information Technology     *)
(*              Queensland University of Technology             *)
(*                                                              *)
(*     Permission is granted to use, copy and change this       *)
(*     program as long as the copyright message is left intact  *)
(*                                                              *)
(****************************************************************)

(*******************************************************************)
(******** General support for sequences...no random access *********)
(*******************************************************************)

(* !NONREC *) DEFINITION MODULE GenSequenceSupport; (* kjg nov '84 *)

FROM SYSTEM IMPORT ADDRESS;

TYPE ElemPtr;
TYPE Sequence = RECORD
                  first : ElemPtr; (* ptr to first element *)
                  last  : ElemPtr  (* ptr to last element  *)
                END;

PROCEDURE InitSequence(VAR seq : Sequence);
          (* sets all fields NIL *)

PROCEDURE LinkLeft (VAR seq : Sequence; Element : ADDRESS);
PROCEDURE LinkRight(VAR seq : Sequence; Element : ADDRESS);

PROCEDURE InitCursor(seq : Sequence; VAR cursor : ElemPtr);
(* postcondition: cursor is attached. GetNext will get first. *)

PROCEDURE GetFirst(       seq : Sequence;
                   VAR cursor : ElemPtr;
                   VAR result : ADDRESS );
(* returns the first element. GetFirst on empty sequence rtns NIL *)
(* postcondition: cursor is attached, next GetNext will fetch 2nd *)

PROCEDURE GetNext( VAR cursor : ElemPtr;
                   VAR result : ADDRESS );
(* precondition: cursor is already attached. Returns element and- *)
(* "increments" cursor. Returns NIL if sequence is already ended. *)

PROCEDURE Ended(cursor : ElemPtr) : BOOLEAN;
(* precondition: cursor is attached. Returns "cursor = NIL" *)

PROCEDURE NextIsLast(cursor : ElemPtr) : BOOLEAN;
(* precondition: cursor is attached. Returns "cursor = seq.last" *)

PROCEDURE IsEmpty(seq : Sequence) : BOOLEAN;
(* postcondition : returns "seq is the empty sequence" *)

PROCEDURE LengthOf(seq : Sequence) : CARDINAL;

PROCEDURE DisposeList(VAR seq : Sequence);
	  (* returns list but not the linked nodes,
	     also reinitializes the sequence header *)

END GenSequenceSupport.
