Skip to content

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 name
IndexSet* rows = new IndexSet();
rows.addRange(Range.make((i32)3, (i32)3)); // 3, 4, 5
rows.addIndex((i32)9);
rows.addIndex((i32)10);
rows.count(); // 5
rows.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.

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


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)

↑ Topics

void addIndex(i32 i)
void addRange(Range* r)

Every index of r; any part below 0 is ignored.

void addIndexes(IndexSet* other)

↑ Topics

void removeIndex(i32 i)
void removeRange(Range* r)
void removeIndexes(IndexSet* other)
void removeAllIndexes(void)

↑ Topics

bool containsIndex(i32 i)
bool containsRange(Range* r)

Whether every index of r is in the set; true for an empty range.

bool containsIndexes(IndexSet* other)
bool intersectsRange(Range* r)

Whether any index of r is in the set.

↑ Topics

u32 count(void)

The number of indexes (not ranges).

bool isEmpty(void)
i32 firstIndex(void)
i32 lastIndex(void)

The smallest and largest index, or notFound().

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.

static i32 notFound(void) // -1

↑ Topics

u32 rangeCount(void)
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.

↑ Topics

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.

↑ Topics