/// Appends a value to an array within a dictionary without triggering a copy. /// Necessary in Swift 3 but expected to be obsoleted as the way inout works /// will be changed (eliminating the need for the write-back copy) func append(value: V, toKey key: K, in dict: inout [K : Array]) { var a: [V]? = [] swap(&a, &dict[key]) a = a ?? [] a!.append(value) swap(&a, &dict[key]) } /// Removes a value from an array within a dictionary without triggering a copy. @discardableResult func remove(value: V, fromKey key: K, in dict: inout [K: Array]) -> Bool where V: Equatable { var a: [V]? = [] swap(&a, &dict[key]) a = a ?? [] let result = a!.removeElement(value) swap(&a, &dict[key]) return result } /// Removes an index from an array within a dictionary without triggering a copy. func remove(index: Int, fromKey key: K, in dict: inout [K: Array]) where V: Equatable { var a: [V]? = [] swap(&a, &dict[key]) a = a ?? [] a!.remove(at: index) swap(&a, &dict[key]) }