IndexSet
IndexSet is a set of non-negative integer indexes (NSIndexSet in shape),
stored as a sorted list of Ranges that neither overlap
nor touch. A block of a million rows costs one range, and adding and removing
merge and split ranges to keep it so. It is what a table’s multi-row selection
wants. From 0.72.
#import "IndexSet.xc" // not in the Foundation umbrella: import it by nameOverview
Section titled “Overview”IndexSet* rows = new IndexSet();rows.addRange(Range.make((i32)3, (i32)3)); // 3, 4, 5rows.addIndex((i32)9);rows.addIndex((i32)10);rows.count(); // 5rows.rangeCount(); // 2: [3,6) and [9,11)Stdio.printf("%@\n", rows); // (3-5, 9-10)
rows.shiftIndexes((i32)4, (i32)2); // two rows inserted at 4: (3, 6-7, 11-12)Finding the range for an index is a binary search. Indexes are i32 and at
least 0; a method that finds an index returns
IndexSet.notFound() (-1) when there is none. Two index sets are
equal when they hold the same indexes, and hash alike.
Topics
Section titled “Topics”Creating · withIndex / withRange · copy
Adding · addIndex · addRange · addIndexes
Removing · removeIndex · removeRange · removeIndexes · removeAllIndexes
Testing · containsIndex · containsRange · containsIndexes · intersectsRange
Counting and finding · count · isEmpty · firstIndex / lastIndex · indexGreaterThan … · notFound
Ranges · rangeCount · rangeAt
Shifting · shiftIndexes
Creating
Section titled “Creating”withIndex / withRange
Section titled “withIndex / withRange”static IndexSet* withIndex(i32 i)static IndexSet* withRange(Range* r)A set of one index, or of every index in r. new IndexSet() is empty.
IndexSet* copy(void)Adding
Section titled “Adding”addIndex
Section titled “addIndex”void addIndex(i32 i)addRange
Section titled “addRange”void addRange(Range* r)Every index of r; any part below 0 is ignored.
addIndexes
Section titled “addIndexes”void addIndexes(IndexSet* other)Removing
Section titled “Removing”removeIndex
Section titled “removeIndex”void removeIndex(i32 i)removeRange
Section titled “removeRange”void removeRange(Range* r)removeIndexes
Section titled “removeIndexes”void removeIndexes(IndexSet* other)removeAllIndexes
Section titled “removeAllIndexes”void removeAllIndexes(void)Testing
Section titled “Testing”containsIndex
Section titled “containsIndex”bool containsIndex(i32 i)containsRange
Section titled “containsRange”bool containsRange(Range* r)Whether every index of r is in the set; true for an empty range.
containsIndexes
Section titled “containsIndexes”bool containsIndexes(IndexSet* other)intersectsRange
Section titled “intersectsRange”bool intersectsRange(Range* r)Whether any index of r is in the set.
Counting and finding
Section titled “Counting and finding”u32 count(void)The number of indexes (not ranges).
isEmpty
Section titled “isEmpty”bool isEmpty(void)firstIndex / lastIndex
Section titled “firstIndex / lastIndex”i32 firstIndex(void)i32 lastIndex(void)The smallest and largest index, or notFound().
indexGreaterThan …
Section titled “indexGreaterThan …”i32 indexGreaterThan(i32 i)i32 indexGreaterThanOrEqualTo(i32 i)i32 indexLessThan(i32 i)i32 indexLessThanOrEqualTo(i32 i)The nearest index in the set on that side of i, or
notFound(): what moving a selection with the arrow keys asks.
notFound
Section titled “notFound”static i32 notFound(void) // -1Ranges
Section titled “Ranges”rangeCount
Section titled “rangeCount”u32 rangeCount(void)rangeAt
Section titled “rangeAt”Range* rangeAt(u32 k)A copy of the k-th range, in order: walking the ranges is how to visit a large
selection without visiting every index.
Shifting
Section titled “Shifting”shiftIndexes
Section titled “shiftIndexes”void shiftIndexes(i32 start, i32 delta)Moves every index at or after start by delta, as when rows are inserted or
deleted. With delta > 0 a gap opens at start (a range that spans it is
split). With delta < 0 the indexes in [start + delta, start) go and the rest
close up.