-
-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathMultiDictionary.swift
114 lines (99 loc) · 3.41 KB
/
MultiDictionary.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
//
// MultiDictionary.swift
// Embassy
//
// Created by Fang-Pen Lin on 6/1/16.
// Copyright © 2016 Fang-Pen Lin. All rights reserved.
//
import Foundation
/// Transformer for MultiDictionary keys, like lower case
public protocol KeyTransformer {
associatedtype Key: Hashable
static func transform(key: Key) -> Key
}
/// A key transformer that does nothing to the key but simply return it
public struct NoOpKeyTransform<T: Hashable>: KeyTransformer {
public typealias Key = T
public static func transform(key: T) -> Key {
return key
}
}
/// A key transformer that lowers case of the String key, so that the MultiDictionary will be
/// case-insenstive
public struct LowercaseKeyTransform: KeyTransformer {
public typealias Key = String
public static func transform(key: Key) -> Key {
return key.lowercased()
}
}
/// MultiDictionary is a Dictionary and Array like container, it allows one key to have multiple
/// values
public struct MultiDictionary<
Key,
Value,
KeyTransform: KeyTransformer>
where KeyTransform.Key == Key
{
public typealias ArrayType = Array<(Key, Value)>
public typealias DictionaryType = Dictionary<Key, Array<Value>>
// Items in this multi dictionary
fileprivate let items: ArrayType
// Dictionary mapping from key to tuple of original key (before transform) and all values in
/// order
fileprivate let keyValuesMap: DictionaryType
public init(items: Array<(Key, Value)>) {
self.items = items
var keyValuesMap: DictionaryType = [:]
for (key, value) in items {
let transformedKey = KeyTransform.transform(key: key)
var values = keyValuesMap[transformedKey] ?? []
values.append(value)
keyValuesMap[transformedKey] = values
}
self.keyValuesMap = keyValuesMap
}
/// Get all values for given key in occurrence order
/// - Parameter key: the key
/// - Returns: tuple of array of values for given key
public func valuesFor(key: Key) -> Array<Value>? {
return keyValuesMap[KeyTransform.transform(key: key)]
}
/// Get the first value for given key if available
/// - Parameter key: the key
/// - Returns: first value for the key if available, otherwise nil will be returned
public subscript(key: Key) -> Value? {
return valuesFor(key: key)?.first
}
}
// MARK: CollectionType
extension MultiDictionary: Collection {
public typealias Index = ArrayType.Index
public var startIndex: Index {
return items.startIndex
}
public var endIndex: Index {
return items.endIndex
}
public subscript(position: Index) -> Element {
return items[position]
}
public func index(after i: Index) -> Index {
guard i != endIndex else { fatalError("Cannot increment endIndex") }
return i + 1
}
}
// MARK: ArrayLiteralConvertible
extension MultiDictionary: ExpressibleByArrayLiteral {
public typealias Element = ArrayType.Element
public init(arrayLiteral elements: Element...) {
items = elements
var keyValuesMap: DictionaryType = [:]
for (key, value) in items {
let transformedKey = KeyTransform.transform(key: key)
var values = keyValuesMap[transformedKey] ?? []
values.append(value)
keyValuesMap[transformedKey] = values
}
self.keyValuesMap = keyValuesMap
}
}