SortedSet

public struct SortedSet<Element> : SetCollection where Element : Comparable

A collection whose elements are unique and sorted.

SortedSet is implemented using Red-Black Tree, so can perform insertion, search, deletion operations in logarithmic time.

Example

var s1: SortedSet = [5, 2, 3, 4]
print(s1) // [2, 3, 4, 5]

s1.insert(1)
print(s1[s1.startIndex]) // 1
print(s1) // [1, 2, 3, 4, 5]

print(s1.remove(5)!) // 5
print(s1.last!) // 4
print(s1) // [1, 2, 3, 4]

let s2: SortedSet = [3, 7, 1, 9]
print(s1.union(s2)) // [1, 2, 3, 4, 7, 9]
  • Creates an empty SortedSet.

    Declaration

    Swift

    @inlinable
    @inline(__always)
    public init()
  • Create a new SortedSet whose only elements for which the given closure isIncluded returns true.

    Complexity

    O(m + n), where m is the length of the original SortedSet and n is the length of the resulting SortedSet.

    Declaration

    Swift

    @inlinable
    public func filter(_ isIncluded: (Element) throws -> Bool) rethrows
        -> SortedSet

    Parameters

    isIncluded

    A closure that is called with each element in this SortedSet and returns whether the element should be included in the result.

    Return Value

    A SortedSet whose elements for which the given closure isIncluded returns true.

  • Removes the minimum element in the SortedSet.

    Complexity

    Amortized O(1).

    Declaration

    Swift

    @discardableResult
    @inlinable
    public mutating func removeFirst() -> Element

    Return Value

    The minimum element in the SortedSet.

  • Removes the element for the specified index.

    Complexity

    Amortized O(1).

    Declaration

    Swift

    @discardableResult
    @inlinable
    public mutating func remove(at position: Index) -> Element

    Parameters

    position

    The index for the element to remove.

    Return Value

    If the element for the specified key was removed, returns the value of the element, otherwise nil.

  • Removes all elements in the SortedSet.

    Complexity

    O(n), where n is the length of this SortedSet.

    Note

    keepingCapacity is always ignored.

    Declaration

    Swift

    @inlinable
    @inline(__always)
    public mutating func removeAll(
        keepingCapacity keepCapacity: Bool = false)

    Parameters

    keepCapacity

    The value is always ignored.

  • Declaration

    Swift

    @inlinable
    @inline(__always)
    public static func == (
        lhs: SortedSet<Element>, rhs: SortedSet<Element>) -> Bool
  • Declaration

    Swift

    @inlinable
    @inline(__always)
    public static func < (lhs: SortedSet, rhs: SortedSet) -> Bool
  • Declaration

    Swift

    @inlinable
    @inline(__always)
    public func hash(into hasher: inout Hasher)
  • Declaration

    Swift

    public struct Iterator : IteratorProtocol
  • Declaration

    Swift

    @inlinable
    @inline(__always)
    public func makeIterator() -> Iterator
  • Returns the element which is the minimum in the SortedSet.

    Complexity

    O(1).

    Declaration

    Swift

    @warn_unqualified_access
    @inlinable
    @inline(__always)
    public func min() -> Element?

    Return Value

    The element which is the minimum in this SortedSset. If the this SortedSet has no elements, returns nil.

  • Returns the element which is the maximum in the SortedSet.

    Complexity

    O(1).

    Declaration

    Swift

    @warn_unqualified_access
    @inlinable
    @inline(__always)
    public func max() -> Element?

    Return Value

    The element which is the maximum in the SortedSet. If this SortedSet has no elements, returns nil.

  • Declaration

    Swift

    @inlinable
    @inline(__always)
    public func contains(_ element: Element) -> Bool
  • Returns the sorted elements of the SortedSet.

    Complexity

    O(n), where n is the length of this SortedSet.

    Declaration

    Swift

    @inlinable
    @inline(__always)
    public func sorted() -> [Element]

    Return Value

    The sorted elements of the SortedSet.

  • Declaration

    Swift

    public struct Index : Comparable, Hashable
  • Declaration

    Swift

    @inlinable
    public var startIndex: Index { get }
  • Declaration

    Swift

    @inlinable
    public var endIndex: Index { get }
  • Declaration

    Swift

    @inlinable
    @inline(__always)
    public subscript(position: Index) -> Element { get }
  • Declaration

    Swift

    @inlinable
    @inline(__always)
    public func index(after index: Index) -> Index
  • Returns an index to the element equivalent to element in the SortedSet.

    Complexity

    O(log n), where n is the length of the sequence.

    Declaration

    Swift

    @inlinable
    @inline(__always)
    public func firstIndex(of member: Element) -> Index?

    Parameters

    member

    An element to search.

    Return Value

    The index to the element if it exists in the Sorted Set, otherwise nil.

  • Removes and returns the minimum element in the SortedSet.

    Complexity

    Amortized O(1).

    Declaration

    Swift

    @inlinable
    @inline(__always)
    public mutating func popFirst() -> Element?

    Return Value

    If this SortedSet isn’t empty, the minimum element, otherwise nil.

  • Declaration

    Swift

    @inlinable
    public var count: Int { get }
  • Declaration

    Swift

    @inlinable
    public var isEmpty: Bool { get }
  • Declaration

    Swift

    @inlinable
    @inline(__always)
    public func index(before index: Index) -> Index
  • Returns an index to the element equivalent to element in the SortedSet.

    Complexity

    O(log n), where n is the length of the sequence.

    Declaration

    Swift

    @inlinable
    @inline(__always)
    public func lastIndex(of element: Element) -> Index?

    Parameters

    member

    An element to search.

    Return Value

    The index to the element if it exists in the SortedSet, otherwise nil.

  • Removes the maximum element in the SortedSet.

    Complexity

    Amortized O(1).

    Declaration

    Swift

    @discardableResult
    @inlinable
    public mutating func removeLast() -> Element

    Return Value

    The maximum element in the SortedSet.

  • Removes and returns the maximum element in the SortedSet.

    Complexity

    Amortized O(1).

    Declaration

    Swift

    @inlinable
    @inline(__always)
    public mutating func popLast() -> Element?

    Return Value

    If this SortedSet isn’t empty, the maximum element, otherwise nil.

  • Inserts the new element to the SortedSet.

    Complexity

    O(log n), where n is the length of this SortedSet.

    Declaration

    Swift

    @discardableResult
    @inlinable
    @inline(__always)
    public mutating func insert(_ newMember: Element)
        -> (inserted: Bool, memberAfterInsert: Element)

    Parameters

    newMember

    An element to insert into the SortedSet.

    Return Value

    If newMember was already included in the SortedSet, (inserted: false, memberAfterInsert: existing-element), otherwise (inserted: true, memberAfterInsert: newMember).

  • Removes the element from the SortedSet.

    Complexity

    O(log n), where n is the length of this SortedSet.

    Declaration

    Swift

    @inlinable
    @inline(__always)
    public mutating func remove(_ member: Element) -> Element?

    Parameters

    element

    The element to remove.

    Return Value

    If the element for the specified key was removed, returns the value of the element, otherwise nil.

  • Updates the element in the SortedSet.

    Complexity

    O(log n), where n is the length of this SortedSet.

    Declaration

    Swift

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

    Parameters

    newMember

    An element to insert into this SortedSet.

    Return Value

    If newMember was already included in this SortedSet, the old element, otherwise nil.

  • Returns a new SortedSet whose elements are union of self and other.

    Declaration

    Swift

    @inlinable
    public func union<S: Sequence>(_ other: S) -> SortedSet
        where Element == S.Element

    Parameters

    other

    A sequence of elements.

    Return Value

    A new SortedSet whose elements are union of self and other.

  • Forms union of self and other.

    Declaration

    Swift

    @inlinable
    public mutating func formUnion<S: Sequence>(_ other: S)
        where Element == S.Element

    Parameters

    other

    A sequence of elements.

  • Returns a new SortedSet whose elements are intersection of self and other.

    Declaration

    Swift

    @inlinable
    public func intersection<S: Sequence>(_ other: S) -> SortedSet
        where Element == S.Element

    Parameters

    other

    A sequence of elements.

    Return Value

    A new SortedSet whose elements are intersection of self and other.

  • Forms intersection of self and other.

    Declaration

    Swift

    @inlinable
    public mutating func formIntersection<S: Sequence>(_ other: S)
        where Element == S.Element

    Parameters

    other

    A sequence of elements.

  • Returns a new SortedSet whose elements are symmetric difference of self and other.

    Declaration

    Swift

    @inlinable
    public func symmetricDifference<S: Sequence>(_ other: S) -> SortedSet
        where Element == S.Element

    Parameters

    other

    A sequence of elements.

    Return Value

    A new SortedSet whose elements are symmetric difference of self and other.

  • Forms symmetric difference of self and other.

    Declaration

    Swift

    @inlinable
    public mutating func formSymmetricDifference<S: Sequence>(_ other: S)
        where Element == S.Element

    Parameters

    other

    A sequence of elements.

  • Returns a new SortedSet whose elements are difference of self and other.

    Declaration

    Swift

    @inlinable
    public func subtracting<S: Sequence>(_ other: S) -> SortedSet
        where Element == S.Element

    Parameters

    other

    A sequence of elements.

    Return Value

    A new SortedSet whose elements are difference of self and other.

  • Subtracts other from self.

    Declaration

    Swift

    @inlinable
    public mutating func subtract<S: Sequence>(_ other: S)
        where Element == S.Element

    Parameters

    other

    A sequence of elements.

  • Returns whether self is disjoint with other or not.

    Declaration

    Swift

    @inlinable
    public func isDisjoint<S: Sequence>(with other: S) -> Bool
        where Element == S.Element

    Parameters

    other

    A sequence of elements.

    Return Value

    true if self is disjoint with other, otherwise false.

  • Returns whether self is superset of other or not.

    Declaration

    Swift

    @inlinable
    public func isSuperset<S: Sequence>(of other: S) -> Bool
        where Element == S.Element

    Parameters

    other

    A sequence of elements.

    Return Value

    true if self is superset of other, otherwise false.

  • Returns whether self is superset of other or not.

    Declaration

    Swift

    @inlinable
    public func isSubset<S: Sequence>(of other: S) -> Bool
        where Element == S.Element

    Parameters

    other

    A sequence of elements.

    Return Value

    true if self is superset of other, otherwise false.

  • Returns whether self is strict superset of other or not.

    Declaration

    Swift

    @inlinable
    public func isStrictSuperset<S: Sequence>(of other: S) -> Bool
        where Element == S.Element

    Parameters

    other

    A sequence of elements.

    Return Value

    true if self is strict superset of other, otherwise false.

  • Returns whether self is strict subset of other or not.

    Declaration

    Swift

    @inlinable
    public func isStrictSubset<S: Sequence>(of other: S) -> Bool
        where Element == S.Element

    Parameters

    other

    A sequence of elements.

    Return Value

    true if self is strict subset of other, otherwise false.

  • Returns an index to the element not less than element in the SortedSet.

    Complexity

    O(log n), where n is the length of this SortedSet.

    Declaration

    Swift

    @inlinable
    @inline(__always)
    public func lowerBound(of element: Element) -> Index

    Parameters

    key

    the key for the element.

    Return Value

    The index to the element if it exists in the sorted set, otherwise endIndex.

  • Returns an index to the element greater than element in the SortedSet.

    Complexity

    O(log n), where n is the length of this SortedSet.

    Declaration

    Swift

    @inlinable
    @inline(__always)
    public func upperBound(of element: Element) -> Index

    Parameters

    key

    the key for the element.

    Return Value

    The index to the element if it exists in the sorted set, otherwise endIndex.

  • Inserts the new element to the SortedSet.

    Complexity

    O(log n), where n is the length of this SortedSet. If newMember is located either at, or just before, or just after hint, amortized O(1).

    Declaration

    Swift

    @discardableResult
    @inlinable
    public mutating func insert(_ newMember: Element, hint: Index)
        -> (inserted: Bool, memberAfterInsert: Element)

    Parameters

    newMember

    An element to insert into this SortedSet.

    hint

    The closest position where newMember is located.

    Return Value

    If newMember was already included in the SortedSet, (inserted: false, memberAfterInsert: existing-element), otherwise (inserted: true, memberAfterInsert: newMember).

  • Updates the element in the SortedSet.

    Complexity

    O(log n), where n is the length of this SortedSset. If newMember is located either at, or just before, or just after hint, amortized O(1).

    Declaration

    Swift

    @inlinable
    public mutating func update(with newMember: Element, hint: Index)
        -> Element?

    Parameters

    newMember

    An element to insert into this SortedSet.

    hint

    The closest position where newMember is located.

    Return Value

    If newMember was already included in this SortedSet, the old element, otherwise nil.

  • Returns a new SortedSet whose elements are union of self and other.

    Complexity

    O(m + n), where m is the length of this SortedSet and n is the length of the other SortedSet.

    Declaration

    Swift

    @inlinable
    public func union(_ other: SortedSet) -> SortedSet

    Parameters

    other

    A SorteSet of the same type as self.

    Return Value

    A new SortedSet whose elements are union of self and other.

  • Forms union of self and other.

  • Paramrters:
    • other: A SorteSet of the same type as self.
  • Complexity

    O(m + n), where m is the length of this SortedSet and n is the length of the other SortedSet.

    Declaration

    Swift

    @inlinable
    public mutating func formUnion(_ other: SortedSet)
  • Returns a new SortedSet whose elements are intersection of self and other.

    Complexity

    O(m + n), where m is the length of this SortedSet and n is the length of the other SortedSet.

    Declaration

    Swift

    @inlinable
    public func intersection(_ other: SortedSet) -> SortedSet

    Parameters

    other

    A SorteSet of the same type as self.

    Return Value

    A new SortedSet whose elements are intersection of self and other.

  • Forms intersection of self and other.

    Complexity

    O(m + n), where m is the length of this SortedSet and n is the length of the other SortedSet.

    Declaration

    Swift

    @inlinable
    public mutating func formIntersection(_ other: SortedSet)

    Parameters

    other

    A SorteSet of the same type as self.

  • Returns a new SortedSet whose elements are symmetric difference of self and other.

    Complexity

    O(m + n), where m is the length of this SortedSet and n is the length of the other SortedSet.

    Declaration

    Swift

    @inlinable
    public func symmetricDifference(_ other: SortedSet) -> SortedSet

    Parameters

    other

    A SorteSet of the same type as self.

    Return Value

    A new SortedSet whose elements are intersection of self and other.

  • Forms symmetric difference of self and other.

    Complexity

    O(m + n), where m is the length of this SortedSet and n is the length of the other SortedSet.

    Declaration

    Swift

    @inlinable
    @inline(__always)
    public mutating func formSymmetricDifference(_ other: SortedSet)

    Parameters

    other

    A SorteSet of the same type as self.

  • Returns a new SortedSet whose elements are difference of self and other.

    Complexity

    O(m + n), where m is the length of this SortedSet and n is the length of the other SortedSet.

    Declaration

    Swift

    @inlinable
    public func subtracting(_ other: SortedSet) -> SortedSet

    Parameters

    other

    A SorteSet of the same type as self.

    Return Value

    A new SortedSet whose elements are difference of self and other.

  • Subtracts other from self.

    Complexity

    O(m + n), where m is the length of this SortedSet and n is the length of the other SortedSet.

    Declaration

    Swift

    @inlinable
    public mutating func subtract(_ other: SortedSet)

    Parameters

    other

    A SorteSet of the same type as self.

  • Returns whether self is disjoint with other or not.

    Complexity

    O(m + n), where m is the length of this SortedSet and n is the length of the other SortedSet.

    Declaration

    Swift

    @inlinable
    public func isDisjoint(with other: SortedSet) -> Bool

    Parameters

    other

    A SorteSet of the same type as self.

    Return Value

    true if self is disjoint with other, otherwise false.

  • Returns whether self is superset of other or not.

    Complexity

    O(m + n), where m is the length of this SortedSet and n is the length of the other SortedSet.

    Declaration

    Swift

    @inlinable
    public func isSuperset(of other: SortedSet) -> Bool

    Parameters

    other

    A SorteSet of the same type as self.

    Return Value

    true if self is superset of other, otherwise false.

  • Returns whether self is superset of other or not.

    Complexity

    O(m + n), where m is the length of this SortedSet and n is the length of the other SortedSet.

    Declaration

    Swift

    @inlinable
    public func isSubset(of other: SortedSet) -> Bool

    Parameters

    other

    A SorteSet of the same type as self.

    Return Value

    true if self is superset of other, otherwise false.

  • Returns whether self is strict superset of other or not.

    Complexity

    O(m + n), where m is the length of this SortedSet and n is the length of the other SortedSet.

    Declaration

    Swift

    @inlinable
    public func isStrictSuperset(of other: SortedSet) -> Bool

    Parameters

    other

    A SorteSet of the same type as self.

    Return Value

    true if self is strict superset of other, otherwise false.

  • Returns whether self is strict subset of other or not.

    Complexity

    O(m + n), where m is the length of this SortedSet and n is the length of the other SortedSet.

    Declaration

    Swift

    @inlinable
    public func isStrictSubset(of other: SortedSet) -> Bool

    Parameters

    other

    A SorteSet of the same type as self.

    Return Value

    true if self is strict subset of other, otherwise false.

  • Declaration

    Swift

    public func encode(to encoder: Encoder) throws
  • Declaration

    Swift

    public init(from decoder: Decoder) throws
  • Declaration

    Swift

    public var customMirror: Mirror { get }
  • Declaration

    Swift

    public var description: String { get }
  • Declaration

    Swift

    public var debugDescription: String { get }