Structure

IndexSet

A collection of unique integer values that represent the indexes of elements in another collection.

Declaration

struct IndexSet

Overview

The range of valid integer values is 0..<INT_MAX-1. Anything outside this range is an error.

Topics

Creating an Index Set

init()

Initializes an empty index set.

init<S>(S)

Creates a new set from a finite sequence of items.

init(arrayLiteral: Int...)

Creates a set containing the elements of the given array literal.

init(integer: IndexSet.Element)

Initializes an index set with a single integer.

init(integersIn: Range<IndexSet.Element>)

Initializes an index set with a range of integers.

Counting Items in a Set

func count(in: Range<IndexSet.Element>) -> Int

Returns the count of integers in self that intersect range.

Accessing Elements

subscript(IndexSet.Index) -> IndexSet.Element

Accesses one element in the index set.

Combining Index Sets

func formIntersection(IndexSet)

Intersects the IndexSet with other.

func formSymmetricDifference(IndexSet)

Exclusive or the IndexSet with other.

func formUnion(IndexSet)

Union the IndexSet with other.

func intersection(IndexSet) -> IndexSet

Intersects the IndexSet with other.

func symmetricDifference(IndexSet) -> IndexSet

Exclusive or the IndexSet with other.

func union(IndexSet) -> IndexSet

Union the IndexSet with other.

Inserting Elements

Removing Elements

func remove(IndexSet.Element) -> IndexSet.Element?

Remove an integer from the IndexSet.

func remove(integersIn: Range<IndexSet.Element>)

Remove a range of integers from the IndexSet.

func remove(integersIn: ClosedRange<IndexSet.Element>)

Remove a range of integers from the IndexSet.

func removeAll()

Remove all values from the IndexSet.

func subtract(IndexSet)

Removes the elements of the given set from this set.

func subtracting(IndexSet) -> IndexSet

Returns a new set containing the elements of this set that do not occur in the given set.

Testing Set Membership

func contains(IndexSet.Element) -> Bool

Returns true if self contains integer.

func contains(integersIn: IndexSet) -> Bool

Returns true if self contains all of the integers in indexSet.

func contains(integersIn: Range<IndexSet.Element>) -> Bool

Returns true if self contains all of the integers in range.

func contains(where: (Int) -> Bool) -> Bool

Returns a Boolean value indicating whether the sequence contains an element that satisfies the given predicate.

func intersects(integersIn: Range<IndexSet.Element>) -> Bool

Returns true if self intersects any of the integers in range.

func starts<PossiblePrefix>(with: PossiblePrefix) -> Bool

Returns a Boolean value indicating whether the initial elements of the sequence are the same as the elements in another sequence.

func isDisjoint(with: IndexSet) -> Bool

Returns a Boolean value that indicates whether the set has no members in common with the given set.

func isStrictSubset(of: IndexSet) -> Bool

Returns a Boolean value that indicates whether this set is a strict subset of the given set.

func isStrictSuperset(of: IndexSet) -> Bool

Returns a Boolean value that indicates whether this set is a strict superset of the given set.

func isSubset(of: IndexSet) -> Bool

Returns a Boolean value that indicates whether the set is a subset of another set.

func isSuperset(of: IndexSet) -> Bool

Returns a Boolean value that indicates whether the set is a superset of the given set.

Manipulating Indexes

var startIndex: IndexSet.Index

The beginning index in the set.

var endIndex: IndexSet.Index

The ending index in the set.

func index(after: IndexSet.Index) -> IndexSet.Index

Returns the index that follows the given index in the set.

func index(before: IndexSet.Index) -> IndexSet.Index

Returns the index that precedes the given index in the set.

func formIndex(after: inout IndexSet.Index)

Modifies the given index to refer to the item after the one it currently refers to.

func formIndex(before: inout IndexSet.Index)

Modifies the given index to refer to the item before the one it currently refers to.

func indexRange(in: Range<IndexSet.Element>) -> Range<IndexSet.Index>

Return a Range<IndexSet.Index> which can be used to subscript the index set.

Finding Elements

func first(where: (Int) -> Bool) -> Int?

Returns the first element of the sequence that satisfies the given predicate.

func max() -> Int?

Returns the maximum element in the sequence.

func max(by: (Int, Int) -> Bool) -> Int?

Returns the maximum element in the sequence, using the given predicate as the comparison between elements.

func min() -> Int?

Returns the minimum element in the sequence.

func min(by: (Int, Int) -> Bool) -> Int?

Returns the minimum element in the sequence, using the given predicate as the comparison between elements.

func integerLessThanOrEqualTo(IndexSet.Element) -> IndexSet.Element?

Returns an integer contained in self which is less than or equal to integer, or nil if a result could not be found.

func integerGreaterThan(IndexSet.Element) -> IndexSet.Element?

Returns an integer contained in self which is greater than integer, or nil if a result could not be found.

func integerGreaterThanOrEqualTo(IndexSet.Element) -> IndexSet.Element?

Returns an integer contained in self which is greater than or equal to integer, or nil if a result could not be found.

func integerLessThan(IndexSet.Element) -> IndexSet.Element?

Returns an integer contained in self which is less than integer, or nil if a result could not be found.

Selecting Elements

func filter((Int) -> Bool) -> [Int]

Returns an array containing, in order, the elements of the sequence that satisfy the given predicate.

func filteredIndexSet(in: Range<IndexSet.Element>, includeInteger: (IndexSet.Element) -> Bool) -> IndexSet

Returns an IndexSet filtered according to the result of includeInteger.

func filteredIndexSet(includeInteger: (IndexSet.Element) -> Bool) -> IndexSet

Returns an IndexSet filtered according to the result of includeInteger.

func prefix(Int) -> Slice<IndexSet>

Returns a subsequence, up to the specified maximum length, containing the initial elements of the collection.

func prefix(through: IndexSet.Index) -> Slice<IndexSet>

Returns a subsequence from the start of the collection through the specified position.

func prefix(upTo: IndexSet.Index) -> Slice<IndexSet>

Returns a subsequence from the start of the collection up to, but not including, the specified position.

func prefix(while: (Int) -> Bool) -> Slice<IndexSet>

Returns a subsequence containing the initial elements until predicate returns false and skipping the remaining elements.

func suffix(Int) -> Slice<IndexSet>

Returns a subsequence, up to the given maximum length, containing the final elements of the collection.

func suffix(from: IndexSet.Index) -> Slice<IndexSet>

Returns a subsequence from the specified position to the end of the collection.

Excluding Elements

func dropLast() -> Slice<IndexSet>

Returns a subsequence containing all but the last element of the sequence.

func dropLast(Int) -> Slice<IndexSet>

Returns a subsequence containing all but the specified number of final elements.

func dropFirst() -> Slice<IndexSet>

Returns a subsequence containing all but the first element of the sequence.

func dropFirst(Int) -> Slice<IndexSet>

Returns a subsequence containing all but the given number of initial elements.

func drop(while: (Int) -> Bool) -> Slice<IndexSet>

Returns a subsequence by skipping elements while predicate returns true and returning the remaining elements.

Transforming Elements

func reduce<Result>(Result, (Result, Int) -> Result) -> Result

Returns the result of combining the elements of the sequence using the given closure.

var lazy: LazyCollection<IndexSet>

A view onto this collection that provides lazy implementations of normally eager operations, such as map and filter.

var lazy: LazySequence<IndexSet>

A sequence containing the same elements as this sequence, but on which some operations, such as map and filter, are implemented lazily.

Iterating Over Elements

func forEach((Int) -> Void)

Calls the given closure on each element in the sequence in the same order as a for-in loop.

func enumerated() -> EnumeratedSequence<IndexSet>

Returns a sequence of pairs (n, x), where n represents a consecutive integer starting at zero and x represents an element of the sequence.

func makeIterator() -> IndexingIterator<IndexSet>

Returns an iterator over the elements of this sequence.

Sorting Elements

func sorted() -> [Int]

Returns the elements of the sequence, sorted.

func sorted(by: (Int, Int) -> Bool) -> [Int]

Returns the elements of the sequence, sorted using the given predicate as the comparison between elements.

func reversed() -> ReversedCollection<IndexSet>

Returns a view presenting the elements of the collection in reverse order.

Shifting Index Groups

func shift(startingAt: IndexSet.Element, by: Int)

For a positive delta, shifts the indexes in [index, INT_MAX] to the right, thereby inserting an “empty space” [index, delta], for a negative delta, shifts the indexes in [index, INT_MAX] to the left, thereby deleting the indexes in the range [index - delta, delta].

Splitting the Set

func split(maxSplits: Int, omittingEmptySubsequences: Bool, whereSeparator: (Int) -> Bool) -> [Slice<IndexSet>]

Returns the longest possible subsequences of the collection, in order, that don’t contain elements satisfying the given predicate.

func split(separator: Int, maxSplits: Int, omittingEmptySubsequences: Bool) -> [Slice<IndexSet>]

Returns the longest possible subsequences of the collection, in order, around elements equal to the given element.

Comparing Index Sets

static func != (IndexSet, IndexSet) -> Bool

Returns a Boolean value indicating whether two values are not equal.

Getting a Range-Based View

var rangeView: IndexSet.RangeView

Returns a Range-based view of the entire contents of self.

struct IndexSet.RangeView

A view of the contents of an IndexSet, organized by range.

Describing an Index Set

var description: String

A textual description of the index set.

var debugDescription: String

A textual description of the index set suitable for debugging.

var customMirror: Mirror

A mirror that reflects the index set.

var hashValue: Int

The computed hash value for the index set.

Using Reference Types

class NSIndexSet

An object representing an immutable collection of unique integer values that bridges to IndexSet; use NSIndexSet when you need reference semantics or other Foundation-specific behavior.

class NSMutableIndexSet

An object representing a mutable collection of unique integer values that bridges to IndexSet; use NSMutableIndexSet when you need reference semantics or other Foundation-specific behavior.

typealias IndexSet.ReferenceType

An alias for this value type's equivalent reference type.

Type Aliases

typealias IndexSet.Element

An alias for the type that an index set holds.

Instance Properties

var count: Int

The number of elements in the collection.

var first: IndexSet.Element?

The first integer in self, or nil if self is empty.

var first: Int?

The first element of the collection.

var indices: DefaultIndices<IndexSet>

The indices that are valid for subscripting the collection, in ascending order.

var isEmpty: Bool

A Boolean value that indicates whether the set has no elements.

var isEmpty: Bool

A Boolean value indicating whether the collection is empty.

var last: IndexSet.Element?

The last integer in self, or nil if self is empty.

var last: Int?

The last element of the collection.

var underestimatedCount: Int

A value less than or equal to the number of elements in the sequence, calculated nondestructively.

var underestimatedCount: Int

A value less than or equal to the number of elements in the collection.

Instance Methods

func allSatisfy((Int) -> Bool) -> Bool

Returns a Boolean value indicating whether every element of a sequence satisfies a given predicate.

func compactMap<ElementOfResult>((Int) -> ElementOfResult?) -> [ElementOfResult]

Returns an array containing the non-nil results of calling the given transformation with each element of this sequence.

func elementsEqual<OtherSequence>(OtherSequence) -> Bool

Returns a Boolean value indicating whether this sequence and another sequence contain the same elements in the same order.

func elementsEqual<OtherSequence>(OtherSequence, by: (Int, OtherSequence.Element) -> Bool) -> Bool

Returns a Boolean value indicating whether this sequence and another sequence contain equivalent elements in the same order, using the given predicate as the equivalence test.

func firstIndex(of: Int) -> IndexSet.Index?

Returns the first index where the specified value appears in the collection.

func firstIndex(where: (Int) -> Bool) -> IndexSet.Index?

Returns the first index in which an element of the collection satisfies the given predicate.

func formIndex(inout IndexSet.Index, offsetBy: Int)

Offsets the given index by the specified distance.

func formIndex(inout IndexSet.Index, offsetBy: Int, limitedBy: IndexSet.Index) -> Bool

Offsets the given index by the specified distance, or so that it equals the given limiting index.

func index(IndexSet.Index, offsetBy: Int) -> IndexSet.Index

Returns an index that is the specified distance from the given index.

func index(IndexSet.Index, offsetBy: Int, limitedBy: IndexSet.Index) -> IndexSet.Index?

Returns an index that is the specified distance from the given index, unless that distance is beyond a given limiting index.

func last(where: (Int) -> Bool) -> Int?

Returns the last element of the sequence that satisfies the given predicate.

func lastIndex(of: Int) -> IndexSet.Index?

Returns the last index where the specified value appears in the collection.

func lastIndex(where: (Int) -> Bool) -> IndexSet.Index?

Returns the index of the last element in the collection that matches the given predicate.

func lexicographicallyPrecedes<OtherSequence>(OtherSequence) -> Bool

Returns a Boolean value indicating whether the sequence precedes another sequence in a lexicographical (dictionary) ordering, using the less-than operator (<) to compare elements.

func lexicographicallyPrecedes<OtherSequence>(OtherSequence, by: (Int, Int) -> Bool) -> Bool

Returns a Boolean value indicating whether the sequence precedes another sequence in a lexicographical (dictionary) ordering, using the given predicate to compare elements.

func map<T>((Int) -> T) -> [T]

Returns an array containing the results of mapping the given closure over the sequence’s elements.

func map<T>((Int) -> T) -> [T]

Returns an array containing the results of mapping the given closure over the sequence’s elements.

func randomElement() -> Int?

Returns a random element of the collection.

func randomElement<T>(using: inout T) -> Int?

Returns a random element of the collection, using the given generator as a source for randomness.

func reduce<Result>(into: Result, (inout Result, Int) -> ()) -> Result

Returns the result of combining the elements of the sequence using the given closure.

func shuffled() -> [Int]

Returns the elements of the sequence, shuffled.

func shuffled<T>(using: inout T) -> [Int]

Returns the elements of the sequence, shuffled using the given generator as a source for randomness.

func starts<PossiblePrefix>(with: PossiblePrefix, by: (Int, PossiblePrefix.Element) -> Bool) -> Bool

Returns a Boolean value indicating whether the initial elements of the sequence are equivalent to the elements in another sequence, using the given predicate as the equivalence test.

Subscripts

subscript<R>(R) -> Slice<IndexSet>

Accesses the contiguous subrange of the collection’s elements specified by a range expression.

subscript(Range<IndexSet.Index>) -> Slice<IndexSet>

Accesses a contiguous subrange of the collection’s elements.

Structures

struct IndexSet.Index

The mechanism for accessing the integers stored in an IndexSet.

See Also

Indexes

struct IndexPath

A list of indexes that together represent the path to a specific location in a tree of nested arrays.