SortedSet

public struct SortedSet<Element: Comparable>: SetAlgebra

A sorted collection of unique comparable elements. SortedSet is like Set in the standard library, but it always keeps its elements in ascending order. Lookup, insertion and removal of any element has logarithmic complexity.

SortedSet is a struct with copy-on-write value semantics, like Swift’s standard collection types. It uses an in-memory b-tree for element storage, whose individual nodes may be shared with other sorted sets. Mutating a set whose storage is (partially or completely) shared requires copying of only O(log(count)) elements. (Thus, mutation of shared SortedSets may be cheaper than ordinary Sets, which need to copy all elements.)

Set operations on sorted sets (such as taking the union, intersection or difference) can take as little as O(log(n)) time if the elements in the input sets aren’t too interleaved.

See also

SortedBag
  • Create an empty set.

    Declaration

    Swift

    public init()
  • Create a set from a finite sequence of items. The sequence need not be sorted. If the sequence contains duplicate items, only the last instance will be kept in the set.

    Complexity

    O(n * log(n)), where n is the number of items in the sequence.

    Declaration

    Swift

    public init<S: Sequence>(_ elements: S) where S.Iterator.Element == Element
  • Create a set from a sorted finite sequence of items. If the sequence contains duplicate items, only the last instance will be kept in the set.

    Complexity

    O(n), where n is the number of items in the sequence.

    Declaration

    Swift

    public init<S: Sequence>(sortedElements elements: S) where S.Iterator.Element == Element
  • Create a set with the specified list of items. If the array literal contains duplicate items, only the last instance will be kept.

    Declaration

    Swift

    public init(arrayLiteral elements: Element...)
  • Returns the element at the given index.

    Requires

    index originated from an unmutated copy of this set.

    Complexity

    O(1)

    Declaration

    Swift

    public subscript(index: Index) -> Element
  • Return the subset consisting of elements in the given range of indexes.

    Requires

    The indices in range originated from an unmutated copy of this set.

    Complexity

    O(log(count))

    Declaration

    Swift

    public subscript(range: Range<Index>) -> SortedSet<Element>
  • The index of the first element when non-empty. Otherwise the same as endIndex.

    Complexity

    O(log(count))

    Declaration

    Swift

    public var startIndex: Index
  • The past-the-end element index; the successor of the last valid subscript argument.

    Complexity

    O(1)

    Declaration

    Swift

    public var endIndex: Index
  • The number of elements in this set.

    Declaration

    Swift

    public var count: Int
  • True iff this collection has no elements.

    Declaration

    Swift

    public var isEmpty: Bool
  • Return an iterator over all elements in this map, in ascending key order.

    Declaration

    Swift

    public func makeIterator() -> Iterator
  • Returns the successor of the given index.

    Requires

    index is a valid index of this set and it is not equal to endIndex.

    Complexity

    Amortized O(1).

    Declaration

    Swift

    public func index(after index: Index) -> Index
  • Replaces the given index with its successor.

    Requires

    index is a valid index of this set and it is not equal to endIndex.

    Complexity

    Amortized O(1).

    Declaration

    Swift

    public func formIndex(after index: inout Index)
  • Returns the predecessor of the given index.

    Requires

    index is a valid index of this set and it is not equal to startIndex.

    Complexity

    Amortized O(1).

    Declaration

    Swift

    public func index(before index: Index) -> Index
  • Replaces the given index with its predecessor.

    Requires

    index is a valid index of this set and it is not equal to startIndex.

    Complexity

    Amortized O(1).

    Declaration

    Swift

    public func formIndex(before index: inout Index)
  • Returns an index that is at the specified distance from the given index.

    Requires

    index must be a valid index of this set. If n is positive, it must not exceed the distance from index to endIndex. If n is negative, it must not be less than the distance from index to startIndex.

    Complexity

    O(log(count)) where count is the number of elements in the set.

    Declaration

    Swift

    public func index(_ i: Index, offsetBy n: Int) -> Index
  • Offsets the given index by the specified distance.

    Requires

    index must be a valid index of this set. If n is positive, it must not exceed the distance from index to endIndex. If n is negative, it must not be less than the distance from index to startIndex.

    Complexity

    O(log(count)) where count is the number of elements in the set.

    Declaration

    Swift

    public func formIndex(_ i: inout Index, offsetBy n: Int)
  • Returns an index that is at the specified distance from the given index, unless that distance is beyond a given limiting index.

    Requires

    index and limit must be valid indices in this set. The operation must not advance the index beyond endIndex or before startIndex.

    Complexity

    O(log(count)) where count is the number of elements in the set.

    Declaration

    Swift

    public func index(_ i: Index, offsetBy n: Int, limitedBy limit: Index) -> Index?
  • Offsets the given index by the specified distance, or so that it equals the given limiting index.

    Requires

    index and limit must be valid indices in this set. The operation must not advance the index beyond endIndex or before startIndex.

    Complexity

    O(log(count)) where count is the number of elements in the set.

    Declaration

    Swift

    public func formIndex(_ i: inout Index, offsetBy n: Int, limitedBy limit: Index) -> Bool
  • Returns the distance between two indices.

    Requires

    start and end must be valid indices in this set.

    Complexity

    O(1)

    Declaration

    Swift

    public func distance(from start: Index, to end: Index) -> Int
  • Returns the element at offset from the start of the set.

    Complexity

    O(log(count))

    Declaration

    Swift

    public subscript(offset: Int) -> Element
  • Returns the subset containing elements in the specified range of offsets from the start of the set.

    Complexity

    O(log(count))

    Declaration

    Swift

    public subscript(offsetRange: Range<Int>) -> SortedSet<Element>
  • Return the offset of member, if it is an element of this set. Otherwise, return nil.

    Complexity

    O(log(count))

    Declaration

    Swift

    public func offset(of member: Element) -> Int?
  • Returns the offset of the element at index.

    Complexity

    O(log(count))

    Declaration

    Swift

    public func index(ofOffset offset: Int) -> Index
  • Returns the index of the element at offset.

    Requires

    offset >= 0 && offset < count

    Complexity

    O(log(count))

    Declaration

    Swift

    public func offset(of index: Index) -> Int
  • Call body on each element in self in ascending order.

    Declaration

    Swift

    public func forEach(_ body: (Element) throws -> Void) rethrows
  • Return an Array containing the results of mapping transform over self.

    Declaration

    Swift

    public func map<T>(_ transform: (Element) throws -> T) rethrows -> [T]
  • Return an Array containing the concatenated results of mapping transform over self.

    Declaration

    Swift

    public func flatMap<S : Sequence>(_ transform: (Element) throws -> S) rethrows -> [S.Iterator.Element]
  • Return an Array containing the non-nil results of mapping transform over self.

    Declaration

    Swift

    public func flatMap<T>(_ transform: (Element) throws -> T?) rethrows -> [T]
  • Return an Array containing the elements of self, in ascending order, that satisfy the predicate includeElement.

    Declaration

    Swift

    public func filter(_ includeElement: (Element) throws -> Bool) rethrows -> [Element]
  • Return the result of repeatedly calling combine with an accumulated value initialized to initial and each element of self, in turn. I.e., return combine(combine(...combine(combine(initial, self[0]), self[1]),...self[count-2]), self[count-1]).

    Declaration

    Swift

    public func reduce<T>(_ initialResult: T, _ nextPartialResult: (T, Element) throws -> T) rethrows -> T
  • Return the smallest element in the set, or nil if the set is empty.

    Complexity

    O(log(count))

    Declaration

    Swift

    public var first: Element?
  • Return the largest element in the set, or nil if the set is empty.

    Complexity

    O(log(count))

    Declaration

    Swift

    public var last: Element?
  • Return the smallest element in the set, or nil if the set is empty.

    Complexity

    O(log(count))

    Declaration

    Swift

    public func min() -> Element?
  • Return the largest element in the set, or nil if the set is empty.

    Complexity

    O(log(count))

    Declaration

    Swift

    public func max() -> Element?
  • Return a copy of this set with the smallest element removed. If this set is empty, the result is an empty set.

    Complexity

    O(log(count))

    Declaration

    Swift

    public func dropFirst() -> SortedSet
  • Return a copy of this set with the n smallest elements removed. If n exceeds the number of elements in the set, the result is an empty set.

    Complexity

    O(log(count))

    Declaration

    Swift

    public func dropFirst(_ n: Int) -> SortedSet
  • Return a copy of this set with the largest element removed. If this set is empty, the result is an empty set.

    Complexity

    O(log(count))

    Declaration

    Swift

    public func dropLast() -> SortedSet
  • Return a copy of this set with the n largest elements removed. If n exceeds the number of elements in the set, the result is an empty set.

    Complexity

    O(log(count))

    Declaration

    Swift

    public func dropLast(_ n: Int) -> SortedSet
  • Returns a subset, up to maxLength in size, containing the smallest elements in this set.

    If maxLength exceeds the number of elements, the result contains all the elements of self.

    Complexity

    O(log(count))

    Declaration

    Swift

    public func prefix(_  maxLength: Int) -> SortedSet
  • Returns a subset containing all members of this set at or before the specified index.

    Complexity

    O(log(count))

    Declaration

    Swift

    public func prefix(through index: Index) -> SortedSet
  • Returns a subset containing all members of this set less than or equal to the specified element (which may or may not be a member of this set).

    Complexity

    O(log(count))

    Declaration

    Swift

    public func prefix(through element: Element) -> SortedSet
  • Returns a subset containing all members of this set before the specified index.

    Complexity

    O(log(count))

    Declaration

    Swift

    public func prefix(upTo end: Index) -> SortedSet
  • Returns a subset containing all members of this set less than the specified element (which may or may not be a member of this set).

    Complexity

    O(log(count))

    Declaration

    Swift

    public func prefix(upTo end: Element) -> SortedSet
  • Returns a subset, up to maxLength in size, containing the largest elements in this set.

    If maxLength exceeds self.count, the result contains all the elements of self.

    Complexity

    O(log(count))

    Declaration

    Swift

    public func suffix(_ maxLength: Int) -> SortedSet
  • Returns a subset containing all members of this set at or after the specified index.

    Complexity

    O(log(count))

    Declaration

    Swift

    public func suffix(from index: Index) -> SortedSet
  • Returns a subset containing all members of this set greater than or equal to the specified element (which may or may not be a member of this set).

    Complexity

    O(log(count))

    Declaration

    Swift

    public func suffix(from element: Element) -> SortedSet
  • Return true if the set contains element.

    Complexity

    O(log(count))

    Declaration

    Swift

    public func contains(_ element: Element) -> Bool
  • Returns the index of a given member, or nil if the member is not present in the set.

    Complexity

    O(log(count))

    Declaration

    Swift

    public func index(of member: Element) -> BTreeIndex<Element, Void>?
  • Returns the index of the lowest member of this set that is strictly greater than element, or nil if there is no such element.

    This function never returns endIndex. (If it returns non-nil, the returned index can be used to subscript the set.)

    Complexity

    O(log(count))

    Declaration

    Swift

    public func indexOfFirstElement(after element: Element) -> BTreeIndex<Element, Void>?
  • Returns the index of the lowest member of this set that is greater than or equal to element, or nil if there is no such element.

    This function never returns endIndex. (If it returns non-nil, the returned index can be used to subscript the set.)

    Complexity

    O(log(count))

    Declaration

    Swift

    public func indexOfFirstElement(notBefore element: Element) -> BTreeIndex<Element, Void>?
  • Returns the index of the highest member of this set that is strictly less than element, or nil if there is no such element.

    This function never returns endIndex. (If it returns non-nil, the returned index can be used to subscript the set.)

    Complexity

    O(log(count))

    Declaration

    Swift

    public func indexOfLastElement(before element: Element) -> BTreeIndex<Element, Void>?
  • Returns the index of the highest member of this set that is less than or equal to element, or nil if there is no such element.

    This function never returns endIndex. (If it returns non-nil, the returned index can be used to subscript the set.)

    Complexity

    O(log(count))

    Declaration

    Swift

    public func indexOfLastElement(notAfter element: Element) -> BTreeIndex<Element, Void>?
  • Return true iff self and other contain the same elements.

    This method skips over shared subtrees when possible; this can drastically improve performance when the two sets are divergent mutations originating from the same value.

    Complexity

    O(count)

    Declaration

    Swift

    public func elementsEqual(_ other: SortedSet<Element>) -> Bool
  • Returns true iff a contains the same elements as b.

    This function skips over shared subtrees when possible; this can drastically improve performance when the two sets are divergent mutations originating from the same value.

    Complexity

    O(count)

    Declaration

    Swift

    public static func ==(a: SortedSet<Element>, b: SortedSet<Element>) -> Bool
  • Returns true iff no members in this set are also included in other.

    The elements of the two input sets may be freely interleaved. However, if there are long runs of non-interleaved elements, parts of the input sets will be simply linked into the result instead of copying, which can drastically improve performance.

    Complexity

    • O(min(self.count, other.count)) in general.
    • O(log(self.count + other.count)) if there are only a constant amount of interleaving element runs.

    Declaration

    Swift

    public func isDisjoint(with other: SortedSet<Element>) -> Bool
  • Returns true iff all members in this set are also included in other.

    The elements of the two input sets may be freely interleaved. However, if there are long runs of non-interleaved elements, parts of the input sets will be simply linked into the result instead of copying, which can drastically improve performance.

    Complexity

    • O(min(self.count, other.count)) in general.
    • O(log(self.count + other.count)) if there are only a constant amount of interleaving element runs.

    Declaration

    Swift

    public func isSubset(of other: SortedSet<Element>) -> Bool
  • Returns true iff all members in this set are also included in other, but the two sets aren’t equal.

    The elements of the two input sets may be freely interleaved. However, if there are long runs of non-interleaved elements, parts of the input sets may be skipped instead of elementwise processing, which can drastically improve performance.

    Complexity

    • O(min(self.count, other.count)) in general.
    • O(log(self.count + other.count)) if there are only a constant amount of interleaving element runs.

    Declaration

    Swift

    public func isStrictSubset(of other: SortedSet<Element>) -> Bool
  • Returns true iff all members in other are also included in this set.

    The elements of the two input sets may be freely interleaved. However, if there are long runs of non-interleaved elements, parts of the input sets may be skipped instead of elementwise processing, which can drastically improve performance.

    Complexity

    • O(min(self.count, other.count)) in general.
    • O(log(self.count + other.count)) if there are only a constant amount of interleaving element runs.

    Declaration

    Swift

    public func isSuperset(of other: SortedSet<Element>) -> Bool
  • Returns true iff all members in other are also included in this set, but the two sets aren’t equal.

    The elements of the two input sets may be freely interleaved. However, if there are long runs of non-interleaved elements, parts of the input sets may be skipped instead of elementwise processing, which can drastically improve performance.

    Complexity

    • O(min(self.count, other.count)) in general.
    • O(log(self.count + other.count)) if there are only a constant amount of interleaving element runs.

    Declaration

    Swift

    public func isStrictSuperset(of other: SortedSet<Element>) -> Bool
  • Insert a member into the set if it is not already present.

    Returns

    (true, newMember) if newMember was not contained in the set. If an element equal to newMember was already contained in the set, the method returns (false, oldMember), where oldMember is the element that was equal to newMember. In some cases, oldMember may be distinguishable from newMember by identity comparison or some other means.

    Complexity

    O(log(count))

    Declaration

    Swift

    public mutating func insert(_ newMember: Element) -> (inserted: Bool, memberAfterInsert: Element)

    Return Value

    (true, newMember) if newMember was not contained in the set. If an element equal to newMember was already contained in the set, the method returns (false, oldMember), where oldMember is the element that was equal to newMember. In some cases, oldMember may be distinguishable from newMember by identity comparison or some other means.

  • Inserts the given element into the set unconditionally.

    If an element equal to newMember is already contained in the set, newMember replaces the existing element.

    Parameter

    Parameter newMember: An element to insert into the set.

    Returns

    The element equal to newMember that was originally in the set, if exists; otherwise, nil. In some cases, the returned element may be distinguishable from newMember by identity comparison or some other means.

    Declaration

    Swift

    public mutating func update(with newMember: Element) -> Element?

    Parameters

    newMember

    An element to insert into the set.

    Return Value

    The element equal to newMember that was originally in the set, if exists; otherwise, nil. In some cases, the returned element may be distinguishable from newMember by identity comparison or some other means.

  • Remove the member from the set and return it if it was present.

    Complexity

    O(log(count))

    Declaration

    Swift

    public mutating func remove(_ element: Element) -> Element?
  • Remove the member referenced by the given index.

    Complexity

    O(log(count))

    Declaration

    Swift

    public mutating func remove(at index: Index) -> Element
  • Remove the member at the given offset.

    Complexity

    O(log(count))

    Declaration

    Swift

    public mutating func remove(atOffset offset: Int) -> Element
  • Remove and return the smallest member in this set, or return nil if the set is empty.

    Complexity

    O(log(count))

    Declaration

    Swift

    public mutating func popFirst() -> Element?
  • Remove and return the largest member in this set, or return nil if the set is empty.

    Complexity

    O(log(count))

    Declaration

    Swift

    public mutating func popLast() -> Element?
  • Remove all members from this set.

    Declaration

    Swift

    public mutating func removeAll()
  • Return an Array containing the members of this set, in ascending order.

    SortedSet already keeps its elements sorted, so this is equivalent to Array(self).

    Complexity

    O(count)

    Declaration

    Swift

    public func sorted() -> [Element]
  • Return a set containing all members in both this set and other.

    The elements of the two input sets may be freely interleaved. However, if there are long runs of non-interleaved elements, parts of the input sets will be simply linked into the result instead of copying, which can drastically improve performance.

    Complexity

    • O(min(self.count, other.count)) in general.
    • O(log(self.count + other.count)) if there are only a constant amount of interleaving element runs.

    Declaration

    Swift

    public func union(_ other: SortedSet<Element>) -> SortedSet<Element>
  • Return a set consisting of all members in other that are also in this set.

    The elements of the two input sets may be freely interleaved. However, if there are long runs of non-interleaved elements, parts of the input sets will be simply linked into the result instead of copying, which can drastically improve performance.

    Complexity

    • O(min(self.count, other.count)) in general.
    • O(log(self.count + other.count)) if there are only a constant amount of interleaving element runs.

    Declaration

    Swift

    public func intersection(_ other: SortedSet<Element>) -> SortedSet<Element>
  • Return a set consisting of members from self and other that aren’t in both sets at once.

    The elements of the two input sets may be freely interleaved. However, if there are long runs of non-interleaved elements, parts of the input sets will be simply linked into the result instead of copying, which can drastically improve performance.

    Complexity

    • O(min(self.count, other.count)) in general.
    • O(log(self.count + other.count)) if there are only a constant amount of interleaving element runs.

    Declaration

    Swift

    public func symmetricDifference(_ other: SortedSet<Element>) -> SortedSet<Element>
  • Add all members in other to this set.

    The elements of the two input sets may be freely interleaved. However, if there are long runs of non-interleaved elements, parts of the input sets will be simply linked into the result instead of copying, which can drastically improve performance.

    Complexity

    • O(min(self.count, other.count)) in general.
    • O(log(self.count + other.count)) if there are only a constant amount of interleaving element runs.

    Declaration

    Swift

    public mutating func formUnion(_ other: SortedSet<Element>)
  • Remove all members from this set that are not included in other.

    The elements of the two input sets may be freely interleaved. However, if there are long runs of non-interleaved elements, parts of the input sets will be simply linked into the result instead of copying, which can drastically improve performance.

    Complexity

    • O(min(self.count, other.count)) in general.
    • O(log(self.count + other.count)) if there are only a constant amount of interleaving element runs.

    Declaration

    Swift

    public mutating func formIntersection(_ other: SortedSet<Element>)
  • Replace self with a set consisting of members from self and other that aren’t in both sets at once.

    The elements of the two input sets may be freely interleaved. However, if there are long runs of non-interleaved elements, parts of the input sets will be simply linked into the result instead of copying, which can drastically improve performance.

    Complexity

    • O(min(self.count, other.count)) in general.
    • O(log(self.count + other.count)) if there are only a constant amount of interleaving element runs.

    Declaration

    Swift

    public mutating func formSymmetricDifference(_ other: SortedSet<Element>)
  • Return a set containing those members of this set that aren’t also included in other.

    The elements of the two input sets may be freely interleaved. However, if there are long runs of non-interleaved elements, parts of the input sets will be simply linked into the result instead of copying, which can drastically improve performance.

    Complexity

    • O(min(self.count, other.count)) in general.
    • O(log(self.count + other.count)) if there are only a constant amount of interleaving element runs.

    Declaration

    Swift

    public func subtracting(_ other: SortedSet) -> SortedSet
  • Remove all members from this set that are also included in other.

    The elements of the two input sets may be freely interleaved. However, if there are long runs of non-interleaved elements, parts of the input sets will be simply linked into the result instead of copying, which can drastically improve performance.

    Complexity

    • O(min(self.count, other.count)) in general.
    • O(log(self.count + other.count)) if there are only a constant amount of interleaving element runs.

    Declaration

    Swift

    public mutating func subtract(_ other: SortedSet)
  • Return the count of elements in this set that are in range.

    Complexity

    O(log(self.count))

    Declaration

    Swift

    public func count(elementsIn range: Range<Element>) -> Int
  • Return the count of elements in this set that are in range.

    Complexity

    O(log(self.count))

    Declaration

    Swift

    public func count(elementsIn range: ClosedRange<Element>) -> Int
  • Return a set consisting of all members in self that are also in range.

    Complexity

    O(log(self.count))

    Declaration

    Swift

    public func intersection(elementsIn range: Range<Element>) -> SortedSet<Element>
  • Return a set consisting of all members in self that are also in range.

    Complexity

    O(log(self.count))

    Declaration

    Swift

    public func intersection(elementsIn range: ClosedRange<Element>) -> SortedSet<Element>
  • Remove all members from this set that are not included in range.

    Complexity

    O(log(self.count))

    Declaration

    Swift

    public mutating func formIntersection(elementsIn range: Range<Element>)
  • Remove all members from this set that are not included in range.

    Complexity

    O(log(self.count))

    Declaration

    Swift

    public mutating func formIntersection(elementsIn range: ClosedRange<Element>)
  • Remove all elements in range from this set.

    Complexity

    O(log(self.count))

    Declaration

    Swift

    public mutating func subtract(elementsIn range: Range<Element>)
  • Remove all elements in range from this set.

    Complexity

    O(log(self.count))

    Declaration

    Swift

    public mutating func subtract(elementsIn range: ClosedRange<Element>)
  • Return a set containing those members of this set that aren’t also included in range.

    Complexity

    O(log(self.count))

    Declaration

    Swift

    public mutating func subtracting(elementsIn range: Range<Element>) -> SortedSet<Element>
  • Return a set containing those members of this set that aren’t also included in range.

    Complexity

    O(log(self.count))

    Declaration

    Swift

    public mutating func subtracting(elementsIn range: ClosedRange<Element>) -> SortedSet<Element>
  • Shift the value of all elements starting at start by delta. For a positive delta, this shifts elements to the right, creating an empty gap in start ..< start + delta. For a negative delta, this shifts elements to the left, removing any elements in the range start + delta ..< start that were previously in the set.

    Complexity

    O(self.count). The elements are modified in place.

    Declaration

    Swift

    public mutating func shift(startingAt start: Element, by delta: Element.Stride)