alpha version, October 2000
Just like the CXDictionary, the CXSet represents a hash table, ie. a key-value structure organized so that a search for a key is extremely fast, and actually independent on the number of key-value pairs inside the dictionary. Unlike the dictionary, the set does not use any values: it just represent a set of objects, which can be checked extremely quickly for a presence of an object.
Just like a CXArray or CXDictionary, the CXSet is a typeless container: you can insert any objects into it, and there is no clumsy limitation of all the contained objects to be of same type. Even in the current first try implementation, the key search is pretty fast. You can freely use the sets almost in any algorithm where they might come handy; the most effectivity demanding ones aside there is no fear the key search might be too slow for the application to be user-friendly.
For example, should you need to prepare a random number generator, which will not generate one number twice, you can easily implement it this way:
CXSet *numbers;
...
void init(void)
{
numbers=[CXSet set];
}
int nextRandom(void)
{
int n;
do
n=random();
while (![numbers containsObject:XNUM2OBJ(n)]);
[numbers addObject:XNUM2OBJ(n)];
return n;
}
Note also that currently sets are mutable, ie. you can add and remove objects. That will change in future: there will be an immutable class CXSet, and a mutable one CXMutableSet--that will allow to write considerably more effective code without even thinking (like for an CXArray).
+CXSet *set;
Makes new empty set.
+CXSet *setWithArray(CXArray *array);
Makes new set with all the objects from the array.
+CXSet *setWithObject(id obj);
Makes new set with just the one object. See also the preprocessor for a nice and easily readable syntax for making sets.
+CXSet *setWithObjects(id obj,...);
Makes new set with all the objects given; the list is finished by a nil. See also the preprocessor for a nice and easily readable syntax for making sets.
-int count;
Returns a number of objects in the set.
-CXArray *allObjects;
Returns an array with all the objects from the set.
-CXEnumerator *objectEnumerator;
Makes an enumerator to go thru all the objects in the set.
-BOOL containsObject(id obj);
Enquires whether the set contains the object given.
Cutrrently, all the CXSet instances are mutable, ie. you can freely add and remove objects to them. Though, just like with the CXArray, this is subject to change.
-void addObject(id obj);
Adds the object to the set.
-void removeObject(id obj);
Removes the object from the set; in case the object was not there, does nothing.
-void addObjectsFromArray(CXArray *array);
Adds all objects from the array to the set.
-void removeAllObjects;
Empties the set.
Copyright © 1999-2000 X.soft, all rights reserved