From 8fbc3ca8752685d5140eda64f3caf494f838c99e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Wed, 6 Mar 2019 22:09:01 +0100 Subject: [PATCH] From future import OrderedDictionary --- GRDB/QueryInterface/SQLRelation.swift | 10 ++-- GRDB/Utils/OrderedDictionary.swift | 79 +++++++++++++++++++++------ 2 files changed, 66 insertions(+), 23 deletions(-) diff --git a/GRDB/QueryInterface/SQLRelation.swift b/GRDB/QueryInterface/SQLRelation.swift index 7187abdb1..144d91185 100644 --- a/GRDB/QueryInterface/SQLRelation.swift +++ b/GRDB/QueryInterface/SQLRelation.swift @@ -81,9 +81,9 @@ extension SQLRelation { // can't merge fatalError("The association key \"\(key)\" is ambiguous. Use the Association.forKey(_:) method is order to disambiguate.") } - relation.joins.append(value: mergedJoin, forKey: key) + relation.joins.appendValue(mergedJoin, forKey: key) } else { - relation.joins.append(value: join, forKey: key) + relation.joins.appendValue(join, forKey: key) } return relation } @@ -390,13 +390,13 @@ extension SQLRelation { // can't merge return nil } - mergedJoins.append(value: mergedJoin, forKey: key) + mergedJoins.appendValue(mergedJoin, forKey: key) } else { - mergedJoins.append(value: join, forKey: key) + mergedJoins.appendValue(join, forKey: key) } } for (key, join) in other.joins where mergedJoins[key] == nil { - mergedJoins.append(value: join, forKey: key) + mergedJoins.appendValue(join, forKey: key) } // replace selection unless empty diff --git a/GRDB/Utils/OrderedDictionary.swift b/GRDB/Utils/OrderedDictionary.swift index c4fb38609..d6a6a7ab4 100644 --- a/GRDB/Utils/OrderedDictionary.swift +++ b/GRDB/Utils/OrderedDictionary.swift @@ -9,37 +9,74 @@ /// dict["qux"] // nil /// dict.map { $0.key } // ["foo", "bar"], in this order. struct OrderedDictionary { - private var keys: [Key] - private var values: [Key: Value] + private(set) var keys: [Key] + private(set) var dictionary: [Key: Value] + + var values: [Value] { + return keys.map { dictionary[$0]! } + } /// Creates an empty ordered dictionary. init() { - self.keys = [] - self.values = [:] + keys = [] + dictionary = [:] + } + + /// Creates an empty ordered dictionary. + init(minimumCapacity: Int) { + keys = [] + keys.reserveCapacity(minimumCapacity) + dictionary = Dictionary(minimumCapacity: minimumCapacity) } /// Returns the value associated with key, or nil. + @inlinable subscript(_ key: Key) -> Value? { - return values[key] + get { return dictionary[key] } + set { + if let value = newValue { + updateValue(value, forKey: key) + } else { + removeValue(forKey: 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 { + mutating func appendValue(_ value: Value, forKey key: Key) { + guard updateValue(value, forKey: key) == nil else { fatalError("key is already defined") } + } + + /// Updates the value stored in the dictionary for the given key, or + /// appnds a new key-value pair if the key does not exist. + /// + /// Use this method instead of key-based subscripting when you need to know + /// whether the new value supplants the value of an existing key. If the + /// value of an existing key is updated, updateValue(_:forKey:) returns the + /// original value. If the given key is not present in the dictionary, this + /// method appends the key-value pair and returns nil. + @discardableResult + @inlinable + mutating func updateValue(_ value: Value, forKey key: Key) -> Value? { + if let oldValue = dictionary.updateValue(value, forKey: key) { + return oldValue + } keys.append(key) + return nil } /// Removes the value associated with key. @discardableResult + @usableFromInline mutating func removeValue(forKey key: Key) -> Value? { - guard let value = values.removeValue(forKey: key) else { + guard let value = dictionary.removeValue(forKey: key) else { return nil } - let index = keys.index { $0 == key }! + let index = keys.firstIndex { $0 == key }! keys.remove(at: index) return value } @@ -48,35 +85,41 @@ struct OrderedDictionary { /// with the values transformed by the given closure. func mapValues(_ transform: (Value) throws -> T) rethrows -> OrderedDictionary { return try reduce(into: OrderedDictionary()) { dict, pair in - dict.append(value: try transform(pair.value), forKey: pair.key) + dict.appendValue(try transform(pair.value), forKey: pair.key) } } } extension OrderedDictionary: Collection { - typealias Index = Int + @usableFromInline typealias Index = Int - var startIndex: Int { + @usableFromInline var startIndex: Int { return 0 } - var endIndex: Int { + @usableFromInline var endIndex: Int { return keys.count } - func index(after i: Int) -> Int { + @usableFromInline func index(after i: Int) -> Int { return i + 1 } - subscript(position: Int) -> (key: Key, value: Value) { + @usableFromInline subscript(position: Int) -> (key: Key, value: Value) { let key = keys[position] - return (key: key, value: values[key]!) + return (key: key, value: dictionary[key]!) } } extension OrderedDictionary: ExpressibleByDictionaryLiteral { - init(dictionaryLiteral elements: (Key, Value)...) { + @usableFromInline init(dictionaryLiteral elements: (Key, Value)...) { self.keys = elements.map { $0.0 } - self.values = Dictionary(uniqueKeysWithValues: elements) + self.dictionary = Dictionary(uniqueKeysWithValues: elements) + } +} + +extension Dictionary { + init(_ orderedDictionary: OrderedDictionary) { + self = orderedDictionary.dictionary } }