-
Notifications
You must be signed in to change notification settings - Fork 84
/
Copy pathSortedMultiDictionary.swift
402 lines (351 loc) · 11 KB
/
SortedMultiDictionary.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
/*
* The MIT License (MIT)
*
* Copyright (C) 2019, CosmicMind, Inc. <http://cosmicmind.com>.
* All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
public struct SortedMultiDictionary<Key: Comparable, Value>: Probable, Collection, Equatable, CustomStringConvertible where Key: Hashable {
public typealias Element = RedBlackTree<Key, Value>.Element
/// Returns the position immediately after the given index.
///
/// - Parameter i: A valid index of the collection. `i` must be less than
/// `endIndex`.
/// - Returns: The index value immediately after `i`.
public func index(after i: Int) -> Int {
return i < endIndex ? i + 1 : 0
}
public typealias Iterator = AnyIterator<(key: Key, value: Value?)>
/// Total number of elements within the RedBlackTree
public internal(set) var count = 0
/// Internal storage of (key, value) pairs.
internal var tree: RedBlackTree<Key, Value>
/// Get the data as a Dictionary.
public var asDictionary: [Key: Value?] {
var d = [Key: Value?]()
for (k, v) in self {
d[k] = v
}
return d
}
/// Conforms to the Printable Protocol. Outputs the
public var description: String {
return tree.description
}
/// Get the first (key, value) pair.
public var first: (key: Key, value: Value?)? {
return tree.first
}
/// Get the last (key, value) pair.
public var last: (key: Key, value: Value?)? {
return tree.last
}
/// A boolean of whether the SortedMultiDictionary is empty.
public var isEmpty: Bool {
return 0 == count
}
/// Conforms to the Collection Protocol.
public var startIndex: Int {
return 0
}
/// Conforms to the Collection Protocol.
public var endIndex: Int {
return count
}
/// Retrieves an Array of the key values in order.
public var keys: [Key] {
return tree.keys
}
/// Retrieves an Array of the values that are sorted based
public var values: [Value] {
return tree.values
}
/// Initializer.
public init() {
tree = RedBlackTree<Key, Value>(uniqueKeys: false)
}
/**
Initializes with a given list of elements.
- Parameter elements: A list of (key, value) pairs.
*/
public init(elements: (Key, Value?)...) {
self.init(elements: elements)
}
/**
Initializes with a given Array of elements.
- Parameter elements: An Array of (key, value) pairs.
*/
public init(elements: [(Key, Value?)]) {
self.init()
insert(elements)
}
public func _customIndexOfEquatableElement(_ element: Key) -> Int?? {
return nil
}
public func makeIterator() -> SortedMultiDictionary.Iterator {
var i = indices.makeIterator()
return AnyIterator { i.next().map { self[$0] } }
}
/**
Retrieves the total count of instances for the given
keys.
- Parameter of keys: A list of Key types.
- Returns: An Int.
*/
public func count(of keys: Key...) -> Int {
return count(of: keys)
}
/**
Retrieves the total count of instances for the given
keys.
- Parameter of keys: An Array of Key types.
- Returns: An Int.
*/
public func count(of keys: [Key]) -> Int {
return tree.count(of: keys)
}
/**
Calculates the probability of the given keys.
- Parameter of keys: A list of Key types.
- Returns: A Double.
*/
public func probability(of keys: Key...) -> Double {
return probability(of: keys)
}
/**
Calculates the probability of the given keys.
- Parameter of keys: An Array of Key types.
- Returns: A Double.
*/
public func probability(of keys: [Key]) -> Double {
return tree.probability(of: keys)
}
/**
Calculates the probability using a block.
- Parameter execute block: A block function to execute.
- Returns: A Double.
*/
public func probability(execute block: (Key, Value?) -> Bool) -> Double {
return tree.probability(execute: block)
}
/**
The expected value of given keys based on a number of trials.
- Parameter trials: An Int.
- Parameter for keys: A list of Elements.
- Returns: A Double.
*/
public func expectedValue(trials: Int, for keys: Key...) -> Double {
return expectedValue(trials: trials, for: keys)
}
/**
The expected value of given keys based on a number of trials.
- Parameter trials: An Int.
- Parameter for keys: A list of Elements.
- Returns: A Double.
*/
public func expectedValue(trials: Int, for keys: [Key]) -> Double {
return tree.expectedValue(trials: trials, for: keys)
}
/**
Property key mapping. If the key type is a String, this feature
allows access like a Dictionary.
- Parameter key: A Key type.
- Returns: An optional Value type.
*/
public subscript(key: Key) -> Value? {
get {
return tree[key]
}
set(value) {
tree[key] = value
count = tree.count
}
}
/**
Returns the Key value at a given position.
- Parameter position: An Int.
- Returns: A Key.
*/
public subscript(position: Int) -> Key {
return tree[position]
}
/**
Allows Array like access of the index. Items are kept in order,
so when iterating through the items, they are returned in their
ordered form.
- Parameter index: An Int.
- Returns: A (key: Key, value: Value?)
*/
public subscript(index: Int) -> (key: Key, value: Value?) {
get {
return tree[index]
}
set(value) {
tree[index] = value
count = tree.count
}
}
/**
Returns the Index of a given member, or -1 if the member is not
present.
- Parameter of key: A Key type.
- Returns: An Int.
*/
public func index(of key: Key) -> Int {
return tree.index(of: key)
}
/**
Insert a key / value pair.
- Parameter value: An optional Value type.
- Parameter for key: A Key type.
- Returns: A boolean of the result, true if inserted, false
otherwise.
*/
@discardableResult
mutating public func insert(value: Value?, for key: Key) -> Bool {
let result = tree.insert(value: value, for: key)
count = tree.count
return result
}
/**
Inserts a list of key / value pairs.
- Parameter elements: A list of (Key, Value?) tuples.
*/
mutating public func insert(_ elements: (Key, Value?)...) {
insert(elements)
}
/**
Inserts an Array of key / value pairs.
- Parameter elements: An Array of (Key, Value?) tuples.
*/
mutating public func insert(_ elements: [(Key, Value?)]) {
tree.insert(elements)
count = tree.count
}
/**
Removes key / value pairs based on the keys given.
- Parameter for keys: A list of Key types.
*/
mutating public func removeValue(for keys: Key...) {
removeValue(for: keys)
}
/**
Removes key / value pairs based on the keys given.
- Parameter for keys: An Array of Key types.
*/
mutating public func removeValue(for keys: [Key]) {
tree.removeValue(for: keys)
count = tree.count
}
/// Removes all key / value pairs.
mutating public func removeAll() {
tree.removeAll()
count = tree.count
}
/**
Updates a vlue for the given key.
- Parameter value: An optional Value type.
- Parameter for key: A Key type.
*/
mutating public func update(value: Value?, for key: Key) {
tree.update(value: value, for: key)
}
/**
Finds the value for a given key.
- Parameter for key: A Key type.
- Returns: An optional Value type.
*/
public func findValue(for key: Key) -> Value? {
return tree.findValue(for: key)
}
/**
Searches for given keys in the SortedMultiDictionary.
- Parameter for keys: A list of Key types.
- Returns: A SortedMultiDictionary<Key, Value>.
*/
public func search(for keys: Key...) -> SortedMultiDictionary<Key, Value> {
return search(for: keys)
}
/**
Searches for given keys in the SortedMultiDictionary.
- Parameter for keys: An Array of Key types.
- Returns: A SortedMultiDictionary<Key, Value>.
*/
public func search(for keys: [Key]) -> SortedMultiDictionary<Key, Value> {
var d = SortedMultiDictionary<Key, Value>()
for key in keys {
traverse(for: key, node: tree.root, dictionary: &d)
}
return d
}
/**
Traverses the SortedMultiDictionary, looking for a key matches.
- Parameter for key: A Key type.
- Parameter node: A RedBlackNode<Key, Value>.
- Parameter dictionary: A SortedMultiDictionary<Key, Value> to map the results too.
*/
internal func traverse(for key: Key, node: RedBlackNode<Key, Value>, dictionary: inout SortedMultiDictionary<Key, Value>) {
guard tree.sentinel !== node else {
return
}
if key == node.key {
dictionary.insert(value: node.value, for: key)
}
traverse(for: key, node: node.left, dictionary: &dictionary)
traverse(for: key, node: node.right, dictionary: &dictionary)
}
}
public func ==<Key, Value>(lhs: SortedMultiDictionary<Key, Value>, rhs: SortedMultiDictionary<Key, Value>) -> Bool {
if lhs.count != rhs.count {
return false
}
for i in 0..<lhs.count {
if lhs[i].key != rhs[i].key {
return false
}
}
return true
}
public func !=<Key, Value>(lhs: SortedMultiDictionary<Key, Value>, rhs: SortedMultiDictionary<Key, Value>) -> Bool {
return !(lhs == rhs)
}
public func +<Key, Value>(lhs: SortedMultiDictionary<Key, Value>, rhs: SortedMultiDictionary<Key, Value>) -> SortedMultiDictionary<Key, Value> {
var t = SortedMultiDictionary<Key, Value>()
for (k, v) in lhs {
t.insert(value: v, for: k)
}
for (k, v) in rhs {
t.insert(value: v, for: k)
}
return t
}
public func +=<Key, Value>(lhs: inout SortedMultiDictionary<Key, Value>, rhs: SortedMultiDictionary<Key, Value>) {
for (k, v) in rhs {
lhs.insert(value: v, for: k)
}
}
public func -<Key, Value>(lhs: SortedMultiDictionary<Key, Value>, rhs: SortedMultiDictionary<Key, Value>) -> SortedMultiDictionary<Key, Value> {
var t = lhs
t.removeValue(for: rhs.keys)
return t
}
public func -=<Key, Value>(lhs: inout SortedMultiDictionary<Key, Value>, rhs: SortedMultiDictionary<Key, Value>) {
lhs.removeValue(for: rhs.keys)
}