ListsImpl.mesa,
Copyright c 1986 by Xerox Corporation. All rights reserved.
Last Edited by Curry, March 11, 1986 9:59:09 pm PST
DIRECTORY Lists, Rope;
ListsImpl: CEDAR PROGRAM
IMPORTS Rope
EXPORTS Lists =
BEGIN
ROPE: TYPE = Lists.ROPE;
ReverseList: PUBLIC PROC [list: LIST OF ROPE] RETURNS [new: LIST OF ROPE] =
{FOR list ← list, list.rest WHILE list#NIL DO new ← CONS[list.first, new] ENDLOOP};
ReverseLIST: PUBLIC PROC [list: LIST OF REF] RETURNS [new: LIST OF REF] =
{FOR list ← list, list.rest WHILE list#NIL DO new ← CONS[list.first, new] ENDLOOP};
ListLength: PUBLIC PROC [list: LIST OF ROPE] RETURNS[size: INT] =
{FOR size ← 0, size+1 WHILE list#NIL DO list←list.rest ENDLOOP};
LISTLength: PUBLIC PROC [list: LIST OF REF] RETURNS[size: INT] =
{FOR size ← 0, size+1 WHILE list#NIL DO list←list.rest ENDLOOP};
ListIndexItem: PUBLIC PROC [list: LIST OF ROPE, index: INT] RETURNS[item: ROPE] = {
FOR index ← index, index-1 WHILE index#0 AND list#NIL DO list←list.rest ENDLOOP;
RETURN[IF list=NIL THEN NIL ELSE list.first]};
LISTIndexItem: PUBLIC PROC[list: LIST OF REF, index: INT] RETURNS[item: REF] = {
FOR index ← index, index-1 WHILE index#0 AND list#NIL DO list←list.rest ENDLOOP;
RETURN[IF list=NIL THEN NIL ELSE list.first]};
ListItemIndex: PUBLIC PROC [list: LIST OF ROPE, item: ROPE] RETURNS[index: INT] = {
IF item=NIL THEN RETURN[-1];
FOR index ← 0, index+1 WHILE list#NIL DO
IF Rope.Equal[list.first, item, FALSE] THEN RETURN[index];
list←list.rest ENDLOOP;
RETURN[-1]};
ListItemIndexMax: PUBLIC PROC [list: LIST OF ROPE, item: ROPE] RETURNS[index: INT] = {
count: INT ← -1;
index ← -1;
IF item=NIL THEN RETURN[-1];
FOR list ← list, list.rest WHILE list#NIL DO
count ← count+1;
IF Rope.Equal[list.first, item] THEN index ← count ENDLOOP};
ListItemIndexMin: PUBLIC PROC [list: LIST OF ROPE, item: ROPE] RETURNS[index: INT] = {
IF item=NIL THEN RETURN[-1];
FOR index ← 0, index+1 WHILE list#NIL DO
IF Rope.Equal[list.first, item] THEN RETURN[index];
list←list.rest ENDLOOP;
RETURN[-1]};
ListNonNILIndexMin: PUBLIC PROC [list: LIST OF ROPE] RETURNS[index: INT] = {
FOR index ← 0, index+1 WHILE list#NIL DO
IF list.first#NIL THEN RETURN[index];
list←list.rest ENDLOOP;
RETURN[-1]};
ListNonNILIndexMax: PUBLIC PROC [list: LIST OF ROPE] RETURNS[index: INT] = {
count: INT ← -1;
index ← -1;
FOR list ← list, list.rest WHILE list#NIL DO
count ← count+1;
IF list.first#NIL THEN index ← count ENDLOOP};
ListAllNil: PUBLIC PROC [list: LIST OF ROPE] RETURNS[allNIL: BOOL] =
{FOR list ← list, list.rest WHILE list#NIL DO
IF list.first#NIL THEN RETURN[FALSE]; ENDLOOP; RETURN[TRUE]};
ListConcat: PUBLIC PROC [list1, list2: LIST OF ROPE]
RETURNS[new: LIST OF ROPE] = {
FOR i: INT DECREASING IN [0..ListLength[list2]) DO
new ← CONS[ ListIndexItem[list2, i], new] ENDLOOP;
FOR i: INT DECREASING IN [0..ListLength[list1]) DO
new ← CONS[ ListIndexItem[list1, i], new] ENDLOOP};
END.