Skip to content

OrderedDictionary

mattpolzin edited this page Jun 11, 2021 · 4 revisions

OrderedDictionary

A simple implementation of an Ordered Dictionary based on the existing Foundation Dictionary and Array types. Undoubtedly not the fastest performer, but should be sufficient for the purposes of this library.

public struct OrderedDictionary<Key, Value> where Key: Hashable 

The Encoding/Decoding behavior of this type differs from that of Dictionary both in that ordering is retained and also in that an OrderedDictionary with any Key type that is LosslessStringConvertible or RawRepresentable as String will encode as a hash whereas non-String keys result in Dictionary encoding as a list of alternating keys and values.

Inheritance

Collection, Decodable, Encodable, ExpressibleByDictionaryLiteral, StringRawKeyEncodable, LosslessStringKeyDecodable, StringRawKeyDecodable

Initializers

init()

public init() 

init(grouping:by:)

public init<S>(
        grouping values: S,
        by keyForValue: (S.Element) throws -> Key
    ) rethrows where Value == [S.Element], S : Sequence 

init(_:uniquingKeysWith:)

public init<S>(
        _ keysAndValues: S,
        uniquingKeysWith combine: (Value, Value) throws -> Value
    ) rethrows where S : Sequence, S.Element == (Key, Value) 

init(dictionaryLiteral:)

public init(dictionaryLiteral elements: (Key, Value)...) 

Properties

keys

Get the keys in this dictionary in order.

public var keys: [Key] 

values

Get the values in this dictionary in order.

public var values: [Value] 

startIndex

public var startIndex: Int 

endIndex

public var endIndex: Int 

Methods

contains(where:)

Returns whether this dictionary contains a key fulfilling the given predicate.

public func contains(where predicate: (Key) throws -> Bool) rethrows -> Bool 

contains(key:)

Returns whether the dictionary contains the given key.

public func contains(key: Key) -> Bool 

mapValues(_:)

Returns a new dictionary containing the keys of this dictionary with the values transformed by the given closure.

public func mapValues<T>(_ transform: (Value) throws -> T) rethrows -> OrderedDictionary<Key, T> 

Parameters

  • transform: A closure that transforms a value. transform accepts each value of the dictionary as its parameter and returns a transformed value of the same or of a different type.

Returns

A dictionary containing the keys and transformed values of this dictionary.

compactMapValues(_:)

Returns a new dictionary containing only the key-value pairs that have non-nil values as the result of transformation by the given closure.

public func compactMapValues<T>(_ transform: (Value) throws -> T?) rethrows -> OrderedDictionary<Key, T> 

Use this method to receive a dictionary with non-optional values when your transformation produces optional values.

In this example, note the difference in the result of using mapValues and compactMapValues with a transformation that returns an optional Int value.

let data = ["a": "1", "b": "three", "c": "///4///"]

let m: [String: Int?] = data.mapValues { str in Int(str) }
// ["a": 1, "b": nil, "c": nil]

let c: [String: Int] = data.compactMapValues { str in Int(str) }
// ["a": 1]

Parameters

  • transform: A closure that transforms a value. transform accepts each value of the dictionary as its parameter and returns an optional transformed value of the same or of a different type.

Returns

A dictionary containing the keys and non-nil transformed values of this dictionary.

index(after:)

public func index(after i: Int) -> Int 

makeIterator()

public func makeIterator() -> Iterator 
Types
Protocols
Global Functions
Extensions
Clone this wiki locally