set.h

This interface exports an abstract type that represents sets of values.
Type
Set Defines the abstract type used to represent sets.
Functions
newSet(type) Creates an empty set of values of the specified base type.
newSetFromType(baseType) Creates a new set of values with the specified base type expressed as a string.
freeSet(set) Frees the storage associated with a set.
size(set) Returns the number of elements in the set.
isEmpty(set) Returns true if the set has no elements.
clear(set) Removes all elements from the set.
clone(set) Creates a copy of the set.
contains(set, value) Returns true if the specified value is a member of the set.
add(set, value) Adds the specified value to the set.
remove(set, value) Removes the specified value from the set.
equals(s1, s2) Returns true if s1 and s2 are equal, which means that they contain the same elements.
isSubset(s1, s2) Returns true if s1 is a subset of s2.
union(s1, s2) Returns a new set consisting of all elements in either s1 or s2.
intersection(s1, s2) Returns a new set consisting of all elements in both s1 and s2.
setDifference(s1, s2) Returns a new set consisting of all elements in s1 that are not elements of s2.
setCompareFn(set, cmpFn) Sets the comparison function for elements in the set.
getCompareFn(set) Returns the comparison function for elements in the set.

Type detail


typedef struct SetCDT *Set;
Defines the abstract type used to represent sets.

Function detail



Creates an empty set of values of the specified base type. The type parameter must be an explicit type name like int or string.

Usage:

set = newSet(type);

Set newSetFromType(string baseType);
Creates a new set of values with the specified base type expressed as a string.

Usage:

set = newSetFromType(baseType);

void freeSet(Set set);
Frees the storage associated with a set.

Usage:

freeSet(set);

int size(Set set);
Returns the number of elements in the set.

Usage:

n = size(set);

bool isEmpty(Set set);
Returns true if the set has no elements.

Usage:

if (isEmpty(set)) . . .

void clear(Set set);
Removes all elements from the set.

Usage:

clear(set);

Set clone(Set set);
Creates a copy of the set. The clone function copies only the first level of the structure and does not copy the individual elements.

Usage:

newset = clone(set);

bool contains(Set set, ...);
Returns true if the specified value is a member of the set.

Usage:

if (contains(set, value)) . . .

void add(Set set, ...);
Adds the specified value to the set.

Usage:

add(set, value);

void remove(Set set, ...);
Removes the specified value from the set.

Usage:

remove(set, value);

bool equals(Set s1, Set s2);
Returns true if s1 and s2 are equal, which means that they contain the same elements.

Usage:

if (equals(s1, s2)) . . .

bool isSubset(Set s1, Set s2);
Returns true if s1 is a subset of s2.

Usage:

if (isSubset(s1, s2)) . . .

Set union(Set s1, Set s2);
Returns a new set consisting of all elements in either s1 or s2.

Usage:

set = union(s1, s2);

Set intersection(Set s1, Set s2);
Returns a new set consisting of all elements in both s1 and s2.

Usage:

set = intersection(s1, s2);

Set setDifference(Set s1, Set s2);
Returns a new set consisting of all elements in s1 that are not elements of s2.

Usage:

set = setDifference(s1, s2);

void setCompareFn(Set set, CompareFn cmpFn);
Sets the comparison function for elements in the set. Clients who are using primitive types as base types will not need to call this function.

Usage:

setCompareFn(set, cmpFn);

CompareFn getCompareFn(Set set);
Returns the comparison function for elements in the set.

Usage:

cmpFn = getCompareFn(set);