SortedDictionary
public struct SortedDictionary<Key, Value> : DictionaryCollection where Key : Comparable
A collection whose elements are key and value pairs, sorted by keys.
SortedDictionary is implemented using Red-Black Tree, so can perform insertion, search, deletion operations in logarithmic time.
Unlike Dictionary.Values, SortedDictionary.Values doesn’t conform to MutableCollection.
Example
let countries = ["Singapore", "Canada", "Sweden", "Egypt", "Croatia"]
var d1 = SortedDictionary(grouping: countries) {
country in String(country.first!)
}
print(d1["C"]!) // ["Canada", "Croatia"]
print(d1) // ["C": ["Canada", "Croatia"], "E": ["Egypt"], "S": ["Singapore", "Sweden"]]
d1["G", default: []].append("Greece")
d1["E", default: []].append("Ecuador")
print(d1) // ["C": ["Canada", "Croatia"], "E": ["Egypt", "Ecuador"], "G": ["Greece"], "S": ["Singapore", "Sweden"]]
let d2 = d1.compactMapValues {
v in v.count > 1 ? v.map { $0.uppercased() } : nil
}
print(d2) // ["C": ["CANADA", "CROATIA"], "E": ["EGYPT", "ECUADOR"], "S": ["SINGAPORE", "SWEDEN"]]
-
The element type of a SortedDictionary: a tuple containing an individual key-value pair.
Declaration
Swift
public typealias Element = (key: Key, value: Value) -
Creates an empty SortedDictionary.
Declaration
Swift
@inlinable @inline(__always) public init() -
Creates a new SortedDictionary, initializing its contents with the given key and value pairs
keysAndvalues.let keys_and_values = [("c", 3), ("b", 2), ("d", 4), ("a", 1)] let d = SortedDictionary(uniqueKeysWithValues: keys_and_values) // ["a": 1, "b": 2, "c": 3, "d": 4]Precondition
Keys must be unique.Declaration
Swift
@inlinable public init<S: Sequence>(uniqueKeysWithValues keysAndValues: S) where S.Element == (Key, Value)Parameters
keysAndValuesA sequence of key and value pairs.
Return Value
A new SortedDictionary initialized with the elements of
keysAndValues. -
Creates a new SortedDictionary, initializing its contents with the given key and value pairs
keysAndValues, eliminating values that have duplicate key using closurecombine.let keys_and_values = [("b", 2), ("a", 1), ("a", 3), ("b", 4)] let d1 = SortedDictionary(keys_and_values) { old, _ in old } // ["a": 1, "b": 2] let d2 = SortedDictionary(keys_and_values) { _, new in new } // ["a": 3, "b": 4]Declaration
Swift
@inlinable public init<S: Sequence>( _ keysAndValues: S, uniquingKeysWith combine: (Value, Value) throws -> Value) rethrows where S.Element == (Key, Value)Parameters
keysAndValuesA sequence of key and value pairs.
combineA closure that is called with the old and new values for duplicate key.
-
Creates a new SortedDictionary, initializing its contents with the given values and its key retrieving from closure
keyForValue.let countries = ["Singapore", "Canada", "Sweden", "Egypt", "Croatia"] let countries_by_first_letter = SortedDictionary(grouping: countries) { country in String(country.first!) } // ["C": ["Canada", "Croatia"], "E": ["Egypt"], // "S": ["Singapore", "Sweden"]]Declaration
Parameters
valuesA sequence of values.
keyForValueA closure that is called with each element in
valuesand returns its key. -
Updates the value associated with the key. If the SortedDictionary does not contain element whose key is equivalent to
key, inserts key and value pair.Complexity
O(log n), where n is the length of the SortedDictionary.Declaration
Swift
@discardableResult @inlinable public mutating func updateValue( _ value: Value, forKey key: Key) -> Value?Parameters
valueThe new value for
key.keyThe key for
value.Return Value
If the new element was inserted, nil, otherwise the old value.
-
Merges key and value pairs into the SortedDictionary, eliminating values that have duplicate key using closure
combine.Complexity
O(n * log(m + n)), where m is the length of the SortedDictionary and n is the length of thekeysAndValues.Declaration
Swift
@inlinable public mutating func merge<S: Sequence>( _ keysAndValues: S, uniquingKeysWith combine: (Value, Value) throws -> Value) rethrows where S.Element == (Key, Value)Parameters
keysAndValuesA sequence of key and value pairs.
combineA closure that is called with the old and new values for duplicate key.
-
Merges the other SortedDictionary into the SortedDictionary, eliminating values that have duplicate key using closure
combine.Complexity
O(m + n), where m is the length of this SortedDictionary and n is the length of the other SortedDictionary.Declaration
Swift
@inlinable public mutating func merge( _ other: SortedDictionary, uniquingKeysWith combine: (Value, Value) throws -> Value) rethrowsParameters
otherAn other SortedDictionary.
combineA closure that is called with the old and new values for duplicate key.
-
Creates a new SortedDictionary by merging this SortedDictionary and the given sequence, eliminating values that have dupliacte key using closure
combine.Complexity
O(n * log(n)), where n is the length of the resulting SortedDictionary.Declaration
Swift
@inlinable public func merging<S: Sequence>( _ other: S, uniquingKeysWith combine: (Value, Value) throws -> Value) rethrows -> SortedDictionary where S.Element == (Key, Value)Parameters
keysAndValuesA sequence of key and value pairs.
combineA closure that is called with the old and new values for duplicate key.
-
Creates a new SortedDictionary by merging the SortedDictionary and the other SortedDictionary, eliminating values that have dupliacte key using closure
combine.Complexity
O(m + n), where m is the length of this SortedDictionary and n is the length of the other SortedDictionary.Declaration
Swift
@inlinable public func merging( _ other: SortedDictionary, uniquingKeysWith combine: (Value, Value) throws -> Value) rethrows -> SortedDictionaryParameters
otherAn other SortedDictionary.
combineA closure that is called with the old and new values for duplicate key.
-
Accesses the value for the specified key.
If you assign
nilto the value for the specified key, the element whose key is equivalent tokeyis deleted.Complexity
O(log n), where n is the length of this SortedDictionary.Declaration
Swift
@inlinable public subscript(key: Key) -> Value? { get set }Parameters
keyThe key for the value.
Return Value
The value for the specified key if the element exists in this SortedDictionary, otherwise nil.
-
Accesses the value for the specified key.
If the SortedDictionary doesn’t contain the element whose key is equivalent to
key, thekeyanddefaultValuepair is inserted into the SortedDictionary.Complexity
O(log n), where n is the length of this SortedDictionary.Declaration
Swift
@inlinable public subscript( key: Key, default defaultValue: @autoclosure () -> Value) -> ValueParameters
keyThe key for the value.
defaultThe default value for the key.
Return Value
The value for the specified key if the element exists in this SortedDictionary, or
defaultValue. -
Creates a new SortedDictionary whose values are transformed by the given closure
transform.Complexity
O(n), where n is the length of the SortedDictionary.Declaration
Swift
@inlinable public func mapValues<T>( _ transform: (Value) throws -> T) rethrows -> SortedDictionary<Key, T>Parameters
transformA closure that is called with each value in this SortedDictionary and returns a transformed value.
Return Value
A SortedDictionary whose values are transformed.
-
Creates a new SortedDictionary whose values are transformed by the given closure
transformand the results aren’t nil.Complexity
O(m + n), where m is the length of the original SortedDictionary and n is the length of the resulting SortedDictionary.Declaration
Swift
@inlinable public func compactMapValues<T>( _ transform: (Value) throws -> T?) rethrows -> SortedDictionary<Key, T>Parameters
transformA closure that is called with each value in this SortedDictionary and returns an optional transformed value.
Return Value
A SortedDictionary whose values are non-nil transformed.
-
Creates a new SortedDictionary whose only elements for which the given closure
isIncludedreturns true.Complexity
O(m + n), where m is the length of the original SortedDictionary and n is the length of the resulting SortedDictionary.Declaration
Swift
@inlinable public func filter(_ isIncluded: (Element) throws -> Bool) rethrows -> SortedDictionaryParameters
isIncludedA closure that is called with each element in this SortedDictionary and returns whether the element should be included in the result.
Return Value
A SortedDictionary whose elements for which the given closure
isIncludedreturns true. -
Removes the element for the specified key.
Complexity
O(log n), where n is the length of this SortedDictionary.Declaration
Swift
@discardableResult @inlinable @inline(__always) public mutating func removeValue(forKey key: Key) -> Value?Parameters
keyThe key 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 the element for the specified index.
Complexity
Amortized O(1).Declaration
Parameters
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 SortedDictionary.
Complexity
O(n), where n is the length of this SortedDictionary.Note
keepingCapacityis always ignored.Declaration
Swift
@inlinable @inline(__always) public mutating func removeAll( keepingCapacity keepCapacity: Bool = false)Parameters
keepCapacityThe value is always ignored.
-
Returns an index to the element with key not less than
keyin the SortedDictionary.Complexity
O(log n), where n is the length of this SortedDictionary.Declaration
Swift
@inlinable @inline(__always) public func lowerBound(of key: Key) -> IndexParameters
keythe key for the element.
Return Value
The index to the element if it exists in the sorted dictionary, otherwise
endIndex. -
Returns an index to the element with key greater than
keyin the SortedDictionary.Complexity
O(log n), where n is the length of this SortedDictionary.Declaration
Swift
@inlinable @inline(__always) public func upperBound(of key: Key) -> IndexParameters
keythe key for the element.
Return Value
The index to the element if it exists in this SortedDictionary, otherwise
endIndex. -
Updates the value of the element for the specified index.
Complexity
O(1).Declaration
Swift
@discardableResult @inlinable @inline(__always) public mutating func updateValue(_ value: Value, at position: Index) -> ValueParameters
valueThe new value for
key.positionThe index for the element to update.
Return Value
The old value for
key. -
Accesses the value for the specified key.
If you assign
nilto the value for the specified key, the element whose key is equivalent tokeyis deleted.Complexity
O(log n), where n is the length of this SortedDictionary. If the new element forkeyis located either at, or just before, or just afterhint, amortized O(1).Declaration
Swift
@inlinable public subscript(key: Key, hint hint: Index) -> Value? { get set }Parameters
keyThe key for the value.
hintThe closest position where the element for
keyis located.Return Value
The value for the specified key if the element exists in this SortedDictionary, otherwise nil.
-
Accesses the value for the specified key.
If the SortedDictionary doesn’t contain the element whose key is equivalent to
key, thekeyanddefaultValuepair is inserted into the SortedDictionary.Complexity
O(log n), where n is the length of this SortedDictionary. If the new element forkeyis located either at, or just before, or just afterhint, amortized O(1).Declaration
Swift
@inlinable public subscript( key: Key, hint hint: Index, default defaultValue: @autoclosure () -> Value) -> ValueParameters
keyThe key for the value.
hintThe closest position where the element for
keyis located.defaultThe default value for the key.
Return Value
The value for the specified key if the element exists in this SortedDictionary, or
defaultValue.
-
Creates a new SortedDictionary, initializing its contents with the given Dictionary.
Declaration
Swift
@inlinable @inline(__always) public init(_ dictionary: Dictionary<Key, Value>)Parameters
dictionaryA Dictionary.
Return Value
A new dictionary initialized with the elements of
dictionary.
-
Declaration
Swift
@inlinable @inline(__always) public static func == ( lhs: SortedDictionary<Key, Value>, rhs: SortedDictionary<Key, Value>) -> Bool
-
Declaration
Swift
@inlinable @inline(__always) public static func < ( lhs: SortedDictionary, rhs: SortedDictionary) -> 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 whose key is the minimum in the SortedDictionary.
Complexity
O(1).Declaration
Swift
@warn_unqualified_access @inlinable @inline(__always) public func min() -> Element?Return Value
The element whose key is the minimum in the sorted dictionary. If the SortedDictionary has no elements, returns nil.
-
Returns the element whose key is the maximum in the SortedDictionary.
Complexity
O(1).Declaration
Swift
@warn_unqualified_access @inlinable @inline(__always) public func max() -> Element?Return Value
The element that key is the maximum in the sorted dictionary. If the SortedDictionary has no elements, returns nil.
-
Returns the sorted elements of the SortedDictionary.
Complexity
O(n), where n is the length of this SortedDictionary.Declaration
Swift
@inlinable @inline(__always) public func sorted() -> [Element]Return Value
The sorted elements of the SortedDictionary.
-
Declaration
Swift
public struct Index : Comparable, Hashable -
Declaration
Swift
@inlinable public var startIndex: Index { get } -
Declaration
Swift
@inlinable public var endIndex: Index { get } -
Undocumented
Declaration
Swift
@inlinable @inline(__always) public func index(forKey key: Key) -> Index? -
Removes the element whose key is minimum, and returns the element.
Complexity
Amortized O(1).Declaration
Swift
@inlinable @inline(__always) public mutating func popFirst() -> Element?Return Value
If this SortedDictionary isn’t empty, the element whose key is minimum, otherwise
nil. -
Declaration
Swift
@inlinable public var count: Int { get } -
Declaration
Swift
@inlinable public var isEmpty: Bool { get }
-
Removes the element whose key is maximum, and returns the element.
Complexity
Amortized O(1).Declaration
Swift
@inlinable @inline(__always) public mutating func popLast() -> Element?Return Value
If this SortedDictionary isn’t empty, the element whose key is maximum, otherwise
nil.
-
Declaration
Swift
@inlinable public init(dictionaryLiteral elements: (Key, Value)...)
-
Declaration
Swift
public func encode(to encoder: Encoder) throws
-
Declaration
Swift
public init(from decoder: Decoder) throws
-
Declaration
Swift
public var description: String { get }
-
Declaration
Swift
public var debugDescription: String { get }
-
Declaration
Swift
public var customMirror: Mirror { get }
View on GitHub
SortedDictionary Structure Reference