| SETS(2) | System Calls Manual | SETS(2) |
Sets - sets of non-negative integers
include "sets.m";
OR include "sets32.m";
sets := load Sets Sets->PATH;
A, B: import Sets;
Sets: adt {
init: fn();
set: fn(): Set;
str2set: fn(str: string): Set;
bytes2set: fn(d: array of byte): Set;
Set: adt {
# opaque data
X: fn(s1: self Set, op: int, s2: Set): Set;
add: fn(s: self Set, n: int): Set;
addlist: fn(s: self Set, ns: list of int): Set;
del: fn(s: self Set, n: int): Set;
invert: fn(s: self Set): Set;
eq: fn(s1: self Set, s2: Set): int;
holds: fn(s: self Set, n: int): int;
isempty: fn(s: self Set): int;
msb: fn(s: self Set): int;
limit: fn(s: self Set): int;
str: fn(s: self Set): string;
bytes: fn(s: self Set, n: int): array of byte;
};
};
The Sets module provides routines for manipulating sets of small non-negative integers. There are currently two implementations available: the implementation declared in sets32.m stores sets of numbers from 0 to 31 inclusive; the implementation in sets.m stores arbitrary sets of non-negative integers. The description given is for the more general implementation; behaviour of the other is undefined if an integer higher than 31 is used.
Init must be called first, to allow Sets to initialise its internal state. Set returns a new set, containing nothing. Str2set converts a string to a new set; the string should have been created with Set.str(). Bytes2set converts an array of bytes, d, as returned by Set.bytes(), to a new set.
Note that all set operations are copy operations; none change an existing set.
Given two sets, s1 and s2, s1.X(A&B, s2) gives their intersection; s1.X(A|B, s2) their union; s1.X(A&~B, s2) gives the set of all members of s1 that aren't in s2; s1.X(~(A|B), s2) gives the set of all integers in neither s1 nor s2.
sys->print("%s\n", set().addlist(1::2::5::nil)
.invert().X(A|B, set().add(2)).str());
produces the string ``dd:1'', corresponding to the set of all
non-negative integers except 1 and 5.