Next: , Previous: , Up: Classes   [Index]


Collection

Collection Class

The Collection class is the superclass of all objects that contain groups of objects.

Internally, a Collection is made up of a series of Key objects. See Key. The only function of Key has is to maintain a reference to one of the collection’s items. The Key objects have no other references, while the contents that they refer to may be used elsewhere.

But programs can also refer to Key objects individually. Most of the math operators that are overloaded to work with Collections actually work on Key objects.

If you write a method that adds objects to collections, it’s important to add the attribute OBJECT_IS_MEMBER_OF_PARENT_COLLECTION to each Key object, which tells Ctalk that the Key object can be referred to individually, and not just by its parent object. See Attributes.

Instance Methods

* (void)

When used as a unary operator to overload C’s dereference (‘*’) prefix operator, returns the first element of the array or Collection. This method is actually a shortcut for the method head, below.

+ (Integer n)

Returns the nth member of the receiver Collection. This method is actually a shortcut for the following expressions.


Collection new myCollection;

.....  /* Add members to the collection. */

if (*(myCollection + 1)) {
...
}

Collection instanceMethod + nth (Integer new n) {
  Key new k;

  k = *self;
  k += n;
  return k;
}

at (char *keyString)

Retrieve the element of the receiver Collection at keyString.

atPut (char * keyString, OBJECT *elemObject)

Add elemObject to the receiver with the key keyString.

delete (void)

Remove all of the items in the receiver collection, leaving an empty collection. If any of the items are temporary (for example, a C variable alias), delete the item also.

getValue (void)

Return the value of the receiver, a Key object.

integerAt (String keyName)

Returns the value for keyString as an Integer. The method does not do any checking to make sure that the value is a valid Integer object; the program should ensure that the collection element is either an Integer object or is validly translatable to an Integer.

isEmtpy (void)

Return TRUE if the receiver collection is emtpy, FALSE otherwise.

head (void)

Return the first Key in the receiver collection, or NULL if the collection is empty.

keyExists (String keyName)

Return an Integer value of True if the key given as the argument is present in the receiver collection, False otherwise.

map (OBJECT *(*method)())

Execute method, an instance method of class AssociativeArray, for each key/value pair of the receiver array. The receiver of method is a Key object with the parent class of AssociativeArray. The return value of mapKeys is NULL.

removeAt (String key_name)

Remove the object stored at key_name from the receiver collection and return it. Returns NULL if the entry isn’t present in the receiver collection. In that case, the method does not create a new entry, so programs should then call the atPut method.

replaceAt (String key_name, Object new_value)

Replace the value at key_name in the receiver collection. Returns the old value object, or NULL if the key doesn’t exist. In that case, the method does not create a new entry, so in that case it’s necessary to add the key/value entry to the receiver collection using atPut.

size (void)

Return an Integer with the number of items in the receiver collection.

tail (void)

Return the last Key object that was added to the receiver collection.


Next: , Previous: , Up: Classes   [Index]