SortedSet API ¶The BTreeSet_T implementation of Set_T (see Initializing a Set)
maintains its elements in sorted order. For the Set_T API, this has no effect other
than the iteration order. However, the SortedSet_T API provides additional methods
to take advantage of this ordering. The SortedSet_T structure is defined in the
collections.sortedset(T) module. Here is a list of the
relations among the various APIs implemented by BTreeSet_T:
BTreeSet_T has all the methods of Set_T and can be implicitly
cast to Set_T.
BTreeSet_T has all the methods of SortedSet_T and can be implicitly
cast to SortedSet_T.
SortedSet_T has all the methods of Set_T and can be implicitly
cast to Set_T.
Iterable_T.
In addition to the methods of Set_T, SortedSet_T has the following methods:
T min()returns the least element in the set, or nullT if the set is empty.
If the set is empty and nullT is not defined, an error is thrown.
T max()returns the greatest element in the set, or nullT if the set is empty.
If the set is empty and nullT is not defined, an error is thrown.
T popMin()removes and returns the least element in the set, or
returns nullT if the set is empty.
If the set is empty and nullT is not defined, an error is
thrown.
T popMax()removes and returns the greatest element in the set, or
returns nullT if the set is empty.
If the set is empty and nullT is not defined, an error is
thrown.
T after(T item)returns the least element in the set that is strictly greater than
item. If no such element exists, nullT is returned; if
nullT is not defined, an error is thrown.
T atOrAfter(T item)returns the least element in the set that is greater than or equal to
item. If no such element exists, nullT is returned; if
nullT is not defined, an error is thrown.
T before(T item)returns the greatest element in the set that is strictly less than
item. If no such element exists, nullT is returned; if
nullT is not defined, an error is thrown.
T atOrBefore(T item)returns the greatest element in the set that is less than or equal to
item. If no such element exists, nullT is returned; if
nullT is not defined, an error is thrown.
As an example, the following code iterates over the elements of a sorted set
sset between a (inclusive) and b (exclusive):
for (var x = sset.atOrAfter(a);
!sset.isNullT(x) && x < b;
x = sset.after(x)) {
<statements using x>
}
Of course, this code assumes that nullT is defined for sset.
Note that if you want to iterate over most or all of the elements in a sorted set,
it is usually more efficient to use a range-based for loop and skip any
elements that are not in the desired range:
for (var x : sset) {
if (x < a) continue;
if (x >= b) break;
<statements using x>
}