Collections: Difference between revisions

4,143 bytes added ,  12 years ago
m (→‎sparse stemmed arrays: re-name a DO loop index, added whitespace. -- ~~~~)
Line 1,137:
 
The [http://code.google.com/p/ocaml-extlib/ extlib] also provides a type [http://ocaml-extlib.googlecode.com/svn/doc/apiref/Enum.html Enum.t].
 
=={{header|ooRexx}}==
ooRexx has multiple classes that are collections of other objects with different access and storage characteristics.
==Arrays==
ooRexx arrays are sequential lists of object references. The index values are the numeric position (1-based) within the array.
A given array may be sparse and arrays will be automatically expanded as needed.
<lang ooRexx>
a = .array~new(4) -- creates an array of 4 items, with all slots empty
say a~size a~items -- size is 4, but there are 0 items
a[1] = "Fred" -- assigns a value to the first item
a[5] = "Mike" -- assigns a value to the fifth slot, expanding the size
say a~size a~items -- size is now 5, with 2 items
</lang>
==Lists==
Lists are non-sparse sequential lists of object references. Item can be inserted or deleted at any position
and the positions will be adjusted accordingly. Lists are indexed using index cookies that are assigned when
an entry is added to the list and can be used to access entries or traverse through the list.
<lang ooRexx>
l = .list~new -- lists have no inherent size
index = l~insert('123') -- adds an item to this list, returning the index
l~insert('Fred', .nil) -- inserts this at the beginning
l~insert('Mike') -- adds this to the end
l~insert('Rick', index) -- inserts this after '123'
l[index] = l[index] + 1 -- the original item is now '124'
do item over l -- iterate over the items, displaying them in order
say item
end
</lang>
Output:
<pre>
Fred
124
Rick
Mike
</pre>
==Queues==
Queues are non-sparse sequential lists of object references. The index values are by numeric position (1-based), although access to
items is traditionally done by pushing or popping objects.
<lang ooRexx>
q = .queue~of(2,4,6) -- creates a queue containing 3 items
say q[1] q[3] -- displays "2 6"
i = q~pull -- removes the first item
q~queue(i) -- adds it to the end
say q[1] q[3] -- displays "4 2"
q[1] = q[1] + 1 -- updates the first item
say q[1] q[3] -- displays "5 2"
</lang>
==Tables==
Tables are collections that create a one-to-one relationship between an index object and a referenced object.
Although frequently used with string indexes, the index object can be of any class, with index identity determined by the "==" method.
<lang ooRexx>
t = .table~new
t['abc'] = 1
t['def'] = 2
say t['abc'] t['def'] -- displays "1 2"
</lang>
==Relations==
Relation collections create one-to-many data relationships. An addition to the collection will always create a new entry.
<lang ooRexx>
t = .table~new -- a table example to demonstrate the difference
t['abc'] = 1 -- sets an item at index 'abc'
t['abc'] = 2 -- updates that item
say t~items t['abc'] -- displays "1 2"
r = .relation~new
r['abc'] = 1 -- sets an item at index 'abc'
r['abc'] = 2 -- adds an additional item at the same index
say r~items r['abc'] -- displays "2 2" this has two items in it now
 
do item over r~allAt('abc') -- retrieves all items at the index 'abc'
say item
end
</lang>
==Directories==
Directory objects are like tables, but the index values must always be string objects.
<lang ooRexx>
d = .directory~new
d['abc'] = 1
d['def'] = 2
say d['abc'] d['def'] -- displays "1 2"
</lang>
Directory objects also support an UNKNOWN method that map messages to directory index entries. This allows
values to be set as if they were object attributes. The following example is another way of doing the same as
the first example:
<lang ooRexx>
d = .directory~new
d~abc = 1
d~def = 2
say d~abc d~def -- displays "1 2"
</lang>
Note that the index entries created in the example are the uppercase 'ABC' and 'DEF'.
==Sets==
Sets are unordered collections where the items added to the collection are unique values. Duplicate additions are collapsed to just a
single item. Sets are useful for collecting unique occurrences of items.
<lang ooRexx>
s = .set~new
text = "the quick brown fox jumped over the lazy dog"
do word over text~makearray(' ')
s~put(word)
end
 
say "text has" text~words", but only" s~items "unique words"
</lang>
 
=={{header|Oz}}==