MODULE FNHistories;
IMPORT
KernelLog;
TYPE
HistoryItem = POINTER TO RECORD
item : ANY;
previous, next : HistoryItem
END;
History* = OBJECT
VAR
start, current : HistoryItem;
PROCEDURE & Init*;
BEGIN
start := NIL;
current := NIL
END Init;
PROCEDURE IsEmpty*() : BOOLEAN;
BEGIN
RETURN current = NIL
END IsEmpty;
PROCEDURE Insert*(item : ANY);
VAR new : HistoryItem;
BEGIN
NEW(new);
new.item := item;
new.previous := current;
IF IsEmpty() THEN
start := new;
ELSE
current.next := new;
END;
current := new
END Insert;
PROCEDURE GetCurrent*() : ANY;
BEGIN
IF IsEmpty() THEN
RETURN NIL
ELSE
RETURN current.item
END
END GetCurrent;
PROCEDURE Back*() : BOOLEAN;
BEGIN
IF (current = NIL) OR (current.previous = NIL) THEN
RETURN FALSE
ELSE
current := current.previous;
RETURN TRUE
END
END Back;
PROCEDURE BackTo*(item : ANY) : BOOLEAN;
BEGIN
WHILE current # NIL DO
IF current.item = item THEN RETURN TRUE END;
current := current.previous
END;
RETURN FALSE
END BackTo;
PROCEDURE Forward*() : BOOLEAN;
BEGIN
IF (current = NIL) OR (current.next = NIL) THEN
RETURN FALSE
ELSE
current := current.next;
RETURN TRUE
END
END Forward;
PROCEDURE ForwardTo*(item : ANY) : BOOLEAN;
BEGIN
WHILE current # NIL DO
IF current.item = item THEN RETURN TRUE END;
current := current.next
END;
RETURN FALSE
END ForwardTo;
PROCEDURE Dump*;
VAR i : LONGINT;
p : HistoryItem;
BEGIN
i := 0;
p := current;
WHILE p # NIL DO
KernelLog.String("[");
IF p.item = NIL THEN KernelLog.String("NIL") ELSE KernelLog.String("item") END;
KernelLog.String("] --> ");
p := p.previous;
INC(i)
END;
KernelLog.String("start -- ");
KernelLog.Int(i, 0); KernelLog.String(" items in the history"); KernelLog.Ln;
END Dump;
END History;
END FNHistories.