home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
OS/2 Shareware BBS: 10 Tools
/
10-Tools.zip
/
ober1096.zip
/
libsrc
/
intsequences.obe
< prev
next >
Wrap
Text File
|
1995-04-04
|
5KB
|
207 lines
MODULE IntSequences;
IMPORT SYSTEM;
TYPE
ElemPtr = POINTER TO Elem;
Elem = RECORD
prev,next : ElemPtr;
num : INTEGER;
END;
Sequence* = RECORD
first,last,cursor : ElemPtr;
length : INTEGER;
END;
PROCEDURE (VAR s : Sequence) Create*;
(* initialize the sequence *)
BEGIN
s.length := 0;
s.first := NIL;
s.last := NIL;
END Create;
PROCEDURE (VAR s : Sequence) LinkLeft* (num : INTEGER);
VAR
elem : ElemPtr;
BEGIN
NEW(elem);
elem.num := num;
IF s.last = NIL THEN
s.last := elem;
ELSE
elem.next := s.first;
s.first.prev := elem;
END;
s.first := elem;
INC(s.length);
END LinkLeft;
PROCEDURE (VAR s : Sequence) LinkRight* (num : INTEGER);
VAR
elem : ElemPtr;
BEGIN
NEW(elem);
elem.num := num;
IF s.first = NIL THEN
s.first := elem;
ELSE
elem.prev := s.last;
s.last.next := elem;
END;
s.last := elem;
INC(s.length);
END LinkRight;
PROCEDURE (VAR s : Sequence) GetNext* (VAR result : INTEGER);
(* pre: the cursor has been initialized using GetFirst or GetLast *)
(* post: returns 0 if ended *)
BEGIN
IF (s.cursor = NIL) OR (s.cursor.next = NIL) THEN
result := 0;
s.cursor := NIL;
ELSE
s.cursor := s.cursor.next;
result := s.cursor.num;
END;
END GetNext;
PROCEDURE (VAR s : Sequence) GetPrevious* (VAR result : INTEGER);
(* pre: the cursor has been initialized using GetFirst or GetLast *)
(* post: memory error if sequence is ended *)
BEGIN
IF (s.cursor = NIL) OR (s.cursor.prev = NIL) THEN
result := 0;
s.cursor := NIL;
ELSE
s.cursor := s.cursor.prev;
result := s.cursor.num;
END;
END GetPrevious;
PROCEDURE (VAR s : Sequence) GetFirst* (VAR result : INTEGER);
(* pre: none *)
(* post: the cursor is initialized and the first element is returned, *)
(* memory error if sequence is empty *)
BEGIN
IF s.first = NIL THEN
result := 0;
s.cursor := NIL;
ELSE
s.cursor := s.first;
result := s.first.num;
END;
END GetFirst;
PROCEDURE (VAR s : Sequence) GetLast* (VAR result : INTEGER);
(* pre: none *)
(* post: the cursor is initialized and the last element is returned, *)
(* memory error if sequence is empty *)
BEGIN
IF s.last = NIL THEN
result := 0;
s.cursor := NIL;
ELSE
s.cursor := s.last;
result := s.last.num;
END;
END GetLast;
PROCEDURE (VAR s : Sequence) LengthOf* () : INTEGER;
BEGIN
RETURN s.length;
END LengthOf;
PROCEDURE (VAR s : Sequence) Ended*() : BOOLEAN;
(* pre: a cursor is attached *)
(* post: returns cursor = NIL *)
BEGIN
RETURN s.cursor = NIL;
END Ended;
PROCEDURE (VAR s : Sequence) NextIsLast*() : BOOLEAN;
BEGIN
IF s.cursor = NIL THEN RETURN FALSE;
ELSE
RETURN s.cursor.next = s.last;
END;
END NextIsLast;
PROCEDURE (VAR s : Sequence) NextIsFirst*() : BOOLEAN;
BEGIN
IF s.cursor = NIL THEN RETURN FALSE;
ELSE
RETURN s.cursor.prev = s.first;
END;
END NextIsFirst;
PROCEDURE (VAR s : Sequence) IsEmpty*() : BOOLEAN;
BEGIN
RETURN s.length = 0;
END IsEmpty;
PROCEDURE (VAR s : Sequence) Remove*;
(* pre: sequence is not empty and a cursor has been attached *)
(* post: the element at the cursor position is removed from the sequence *)
VAR
temp : ElemPtr;
BEGIN
ASSERT(s.cursor # NIL);
temp := s.cursor;
temp.next.prev := temp.prev;
temp.prev.next := temp.next;
DEC(s.length);
s.cursor := temp.next;
IF s.first = temp THEN
s.first := temp.next;
END;
IF s.last = temp THEN
s.last := temp.prev;
END;
END Remove;
PROCEDURE (VAR s : Sequence) InsertRight* (num : INTEGER);
(* pre: sequence is not empty and a cursor has been attached *)
(* post: the element is inserted right of the element at the cursor position *)
VAR
temp : ElemPtr;
BEGIN
NEW(temp);
temp.num := num;
temp.next := s.cursor.next;
temp.prev := s.cursor;
s.cursor.next := temp;
INC(s.length);
IF s.last = s.cursor THEN
s.last := temp;
ELSE
temp.next.prev := temp;
END;
END InsertRight;
PROCEDURE (VAR s : Sequence) InsertLeft* (num : INTEGER);
(* pre: sequence is not empty and a cursor has been attached *)
(* post: the element is inserted left of the element at the cursor position *)
VAR
temp : ElemPtr;
BEGIN
NEW(temp);
temp.num := num;
temp.next := s.cursor;
temp.prev := s.cursor.prev;
s.cursor.prev := temp;
INC(s.length);
IF s.first = s.cursor THEN
s.first := temp;
ELSE
temp.prev.next := temp;
END;
END InsertLeft;
END IntSequences.