SE-0270: Add Collection Operations on Noncontiguous Elements

RangeSet is the set of all the values in its constitutive ranges, where the values can be of any Comparable type — integers, floats, strings, collection indices, etc. For example, here's a drawing of the space represented by a Comparable type T:

We can create a RangeSet called set1 that covers three different ranges of T — the yellow strips are the ranges comprised by set1, and the gray regions are the values of T that are contained in set1:

We can then define set2 as a set of different ranges, and then union the two, creating a third set that represents the union of the values represented by set1 and set2:


This is why the documentation for the two contains(_:) methods is the way it is: a value is contained by a RangeSet when it is contained within one of the set's ranges, and a range is only contained if it is fully contained within one of those same ranges.

Likewise, RangeSet has no notion of containing empty Ranges, because empty ranges represent no values in T. Inserting an empty range therefore never has an effect on a RangeSet.

5 Likes