Post by August KarlstromPost by fitzer8Hey gang,
I'm using oo2c 32-2.1.11, and I'm trying to figure out how to use
ADT:LinkedList.
Does anybody have a simple program that uses this? I would like to use
it as a reference.
I have never used it but after browsing through the documentation I
MODULE Example;
IMPORT Out, ADT:LinkedList, Object;
TYPE
Entry = POINTER TO EntryDesc;
EntryDesc = RECORD (Object.ObjectDesc)
value: INTEGER
END;
VAR
ls: LinkedList.LinkedList;
e: Entry;
x: Object.Object;
BEGIN
ls := LinkedList.New();
NEW(e);
e.value := 37;
ls.Insert(0, e);
x := ls.Get(0);
Out.Int(x(Entry).value, 0);
Out.Ln
END Example.
This is exactly how the original ADT:LinkedList worked.
In Version 2 of the compiler, all collection types in ADT (eg.
LinkedList, ArrayList, Dictionary) became parametric (ie. generic). If
you don't specify a type parameter it assumes the bound type which
generally is Object.ObjectDesc. It is still completely compatible with
the old style usage, but you can now do this sort of thing too:
MODULE TestLinkedList;
IMPORT Out, ADT:LinkedList, Object;
TYPE
Entry = POINTER TO EntryDesc;
EntryDesc = RECORD (Object.ObjectDesc)
value: INTEGER
END;
VAR
ls: LinkedList.LinkedList(Entry);
e: Entry;
BEGIN
ls := NEW(LinkedList.LinkedList(Entry));
NEW(e);
e.value := 37;
ls.Insert(0, e);
e := ls.Get(0);
Out.Int(e.value, 0);
Out.Ln
END TestLinkedList.
MODULE Object:Boxed gives you object wrappers for all of the basic
Oberon types. So the following is equivalent to the previous example:
MODULE TestLinkedList2;
IMPORT Out, ADT:LinkedList, Object:Boxed;
TYPE
IntList = LinkedList.LinkedList(Boxed.LongInt);
VAR
ls: IntList;
e : Boxed.LongInt;
BEGIN
ls := NEW(IntList);
e := NEW(Boxed.LongInt, 37);
ls.Insert(0, e);
e := ls.Get(0);
Out.Int(e.value, 0);
Out.Ln
END TestLinkedList2.
If you're just dealing with text, remember that the built-in STRING type
is an Object, so you can use it directly as a type parameter. Also, ADT
collections have their own iterators which you can use to enumerate the
elements, allowing insertions and deletions as you go. Example:
MODULE TestLinkedList3;
IMPORT Out, ADT:LinkedList;
TYPE
StringList = LinkedList.LinkedList(STRING);
VAR
ls: StringList;
iter : LinkedList.Iterator(STRING);
BEGIN
ls := NEW(StringList);
ls.Append("one");
ls.Prepend("two");
ls.Insert(0, "three");
iter := ls.GetIterator(NIL);
WHILE iter.HasNext() DO
Out.Object(iter.Next());
Out.Ln
END;
END TestLinkedList3.
Hope this helps.
Cheers,
Stewart