From f309a0c5f3e1f2f1eb66310f7e47bc42c618a7eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Sat, 2 Feb 2019 13:30:57 +0100 Subject: [PATCH] Documentation --- GRDB/Utils/OrderedDictionary.swift | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/GRDB/Utils/OrderedDictionary.swift b/GRDB/Utils/OrderedDictionary.swift index 6fc1cfc4b..c4fb38609 100644 --- a/GRDB/Utils/OrderedDictionary.swift +++ b/GRDB/Utils/OrderedDictionary.swift @@ -1,16 +1,31 @@ +/// A dictionary with guaranteed keys ordering. +/// +/// var dict = OrderedDictionary() +/// dict.append(1, forKey: "foo") +/// dict.append(2, forKey: "bar") +/// +/// dict["foo"] // 1 +/// dict["bar"] // 2 +/// dict["qux"] // nil +/// dict.map { $0.key } // ["foo", "bar"], in this order. struct OrderedDictionary { private var keys: [Key] private var values: [Key: Value] + /// Creates an empty ordered dictionary. init() { self.keys = [] self.values = [:] } + /// Returns the value associated with key, or nil. subscript(_ key: Key) -> Value? { return values[key] } + /// Appends the given value for the given key. + /// + /// - precondition: There is no value associated with key yet. mutating func append(value: Value, forKey key: Key) { guard values.updateValue(value, forKey: key) == nil else { fatalError("key is already defined") @@ -18,6 +33,7 @@ struct OrderedDictionary { keys.append(key) } + /// Removes the value associated with key. @discardableResult mutating func removeValue(forKey key: Key) -> Value? { guard let value = values.removeValue(forKey: key) else { @@ -28,13 +44,12 @@ struct OrderedDictionary { return value } + /// Returns a new ordered dictionary containing the keys of this dictionary + /// with the values transformed by the given closure. func mapValues(_ transform: (Value) throws -> T) rethrows -> OrderedDictionary { - var result = OrderedDictionary() - for key in keys { - let value = values[key]! - try result.append(value: transform(value), forKey: key) + return try reduce(into: OrderedDictionary()) { dict, pair in + dict.append(value: try transform(pair.value), forKey: pair.key) } - return result } }