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
isIncludedreturns 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 -> SortedSetParameters
isIncludedA 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
isIncludedreturns true. -
Removes the minimum element in the SortedSet.
Complexity
Amortized O(1).Declaration
Swift
@discardableResult @inlinable public mutating func removeFirst() -> ElementReturn 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) -> ElementParameters
positionThe 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
keepingCapacityis always ignored.Declaration
Swift
@inlinable @inline(__always) public mutating func removeAll( keepingCapacity keepCapacity: Bool = false)Parameters
keepCapacityThe 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 } -
Returns an index to the element equivalent to
elementin 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
memberAn 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 }
-
Returns an index to the element equivalent to
elementin 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
memberAn 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() -> ElementReturn 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
newMemberAn element to insert into the SortedSet.
Return Value
If
newMemberwas 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
elementThe 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
newMemberAn element to insert into this SortedSet.
Return Value
If
newMemberwas already included in this SortedSet, the old element, otherwise nil. -
Returns a new SortedSet whose elements are union of
selfandother.Declaration
Swift
@inlinable public func union<S: Sequence>(_ other: S) -> SortedSet where Element == S.ElementParameters
otherA sequence of elements.
Return Value
A new SortedSet whose elements are union of
selfandother. -
Forms union of
selfandother.Declaration
Swift
@inlinable public mutating func formUnion<S: Sequence>(_ other: S) where Element == S.ElementParameters
otherA sequence of elements.
-
Returns a new SortedSet whose elements are intersection of
selfandother.Declaration
Swift
@inlinable public func intersection<S: Sequence>(_ other: S) -> SortedSet where Element == S.ElementParameters
otherA sequence of elements.
Return Value
A new SortedSet whose elements are intersection of
selfandother. -
Forms intersection of
selfandother.Declaration
Swift
@inlinable public mutating func formIntersection<S: Sequence>(_ other: S) where Element == S.ElementParameters
otherA sequence of elements.
-
Returns a new SortedSet whose elements are symmetric difference of
selfandother.Declaration
Swift
@inlinable public func symmetricDifference<S: Sequence>(_ other: S) -> SortedSet where Element == S.ElementParameters
otherA sequence of elements.
Return Value
A new SortedSet whose elements are symmetric difference of
selfandother. -
Forms symmetric difference of
selfandother.Declaration
Swift
@inlinable public mutating func formSymmetricDifference<S: Sequence>(_ other: S) where Element == S.ElementParameters
otherA sequence of elements.
-
Returns a new SortedSet whose elements are difference of
selfandother.Declaration
Swift
@inlinable public func subtracting<S: Sequence>(_ other: S) -> SortedSet where Element == S.ElementParameters
otherA sequence of elements.
Return Value
A new SortedSet whose elements are difference of
selfandother. -
Subtracts
otherfromself.Declaration
Swift
@inlinable public mutating func subtract<S: Sequence>(_ other: S) where Element == S.ElementParameters
otherA sequence of elements.
-
Returns whether
selfis disjoint withotheror not.Declaration
Swift
@inlinable public func isDisjoint<S: Sequence>(with other: S) -> Bool where Element == S.ElementParameters
otherA sequence of elements.
Return Value
trueifselfis disjoint withother, otherwisefalse. -
Returns whether
selfis superset ofotheror not.Declaration
Swift
@inlinable public func isSuperset<S: Sequence>(of other: S) -> Bool where Element == S.ElementParameters
otherA sequence of elements.
Return Value
trueifselfis superset ofother, otherwisefalse. -
Returns whether
selfis superset ofotheror not.Declaration
Swift
@inlinable public func isSubset<S: Sequence>(of other: S) -> Bool where Element == S.ElementParameters
otherA sequence of elements.
Return Value
trueifselfis superset ofother, otherwisefalse. -
Returns whether
selfis strict superset ofotheror not.Declaration
Swift
@inlinable public func isStrictSuperset<S: Sequence>(of other: S) -> Bool where Element == S.ElementParameters
otherA sequence of elements.
Return Value
trueifselfis strict superset ofother, otherwisefalse. -
Returns whether
selfis strict subset ofotheror not.Declaration
Swift
@inlinable public func isStrictSubset<S: Sequence>(of other: S) -> Bool where Element == S.ElementParameters
otherA sequence of elements.
Return Value
trueifselfis strict subset ofother, otherwisefalse.
-
Returns an index to the element not less than
elementin 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) -> IndexParameters
keythe 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
elementin 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) -> IndexParameters
keythe 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. IfnewMemberis located either at, or just before, or just afterhint, amortized O(1).Declaration
Swift
@discardableResult @inlinable public mutating func insert(_ newMember: Element, hint: Index) -> (inserted: Bool, memberAfterInsert: Element)Parameters
newMemberAn element to insert into this SortedSet.
hintThe closest position where
newMemberis located.Return Value
If
newMemberwas 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. IfnewMemberis located either at, or just before, or just afterhint, amortized O(1).Declaration
Swift
@inlinable public mutating func update(with newMember: Element, hint: Index) -> Element?Parameters
newMemberAn element to insert into this SortedSet.
hintThe closest position where
newMemberis located.Return Value
If
newMemberwas already included in this SortedSet, the old element, otherwise nil.
-
Returns a new SortedSet whose elements are union of
selfandother.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) -> SortedSetParameters
otherA SorteSet of the same type as self.
Return Value
A new SortedSet whose elements are union of
selfandother. -
Forms union of
selfandother.- 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) - Paramrters:
-
Returns a new SortedSet whose elements are intersection of
selfandother.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) -> SortedSetParameters
otherA SorteSet of the same type as self.
Return Value
A new SortedSet whose elements are intersection of
selfandother. -
Forms intersection of
selfandother.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
otherA SorteSet of the same type as self.
-
Returns a new SortedSet whose elements are symmetric difference of
selfandother.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) -> SortedSetParameters
otherA SorteSet of the same type as self.
Return Value
A new SortedSet whose elements are intersection of
selfandother. -
Forms symmetric difference of
selfandother.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
otherA SorteSet of the same type as self.
-
Returns a new SortedSet whose elements are difference of
selfandother.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) -> SortedSetParameters
otherA SorteSet of the same type as self.
Return Value
A new SortedSet whose elements are difference of
selfandother. -
Subtracts
otherfromself.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
otherA SorteSet of the same type as self.
-
Returns whether
selfis disjoint withotheror 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) -> BoolParameters
otherA SorteSet of the same type as self.
Return Value
trueifselfis disjoint withother, otherwisefalse. -
Returns whether
selfis superset ofotheror 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) -> BoolParameters
otherA SorteSet of the same type as self.
Return Value
trueifselfis superset ofother, otherwisefalse. -
Returns whether
selfis superset ofotheror 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) -> BoolParameters
otherA SorteSet of the same type as self.
Return Value
trueifselfis superset ofother, otherwisefalse. -
Returns whether
selfis strict superset ofotheror 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) -> BoolParameters
otherA SorteSet of the same type as self.
Return Value
trueifselfis strict superset ofother, otherwisefalse. -
Returns whether
selfis strict subset ofotheror 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) -> BoolParameters
otherA SorteSet of the same type as self.
Return Value
trueifselfis strict subset ofother, otherwisefalse.
-
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 }
View on GitHub
SortedSet Structure Reference