forked from livekit/client-sdk-swift
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOrderedDictionary.swift
More file actions
97 lines (85 loc) · 3.05 KB
/
Copy pathOrderedDictionary.swift
File metadata and controls
97 lines (85 loc) · 3.05 KB
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
/*
* Copyright 2026 LiveKit
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// API inspired by swift-collections by Apple Inc.
// https://github.com/apple/swift-collections
struct OrderedDictionary<Key: Hashable, Value>: ExpressibleByDictionaryLiteral {
private var pairs: [(key: Key, value: Value)] = []
private var index: [Key: Int] = [:]
init() {}
init(dictionaryLiteral elements: (Key, Value)...) {
for (key, value) in elements {
pairs.append((key, value))
index[key] = pairs.count - 1
}
}
init(uniqueKeysWithValues keysAndValues: some Sequence<(Key, Value)>) {
for (key, value) in keysAndValues {
if let i = index[key] {
pairs[i] = (key, value)
} else {
index[key] = pairs.count
pairs.append((key, value))
}
}
}
var count: Int { pairs.count }
var keys: Keys { Keys(pairs: pairs) }
var values: Values { Values(pairs: pairs) }
subscript(key: Key) -> Value? {
get {
guard let i = index[key] else { return nil }
return pairs[i].value
}
set {
if let newValue {
if let i = index[key] {
pairs[i] = (key, newValue)
} else {
index[key] = pairs.count
pairs.append((key, newValue))
}
} else if let i = index.removeValue(forKey: key) {
pairs.remove(at: i)
for j in i ..< pairs.count {
index[pairs[j].key] = j
}
}
}
}
@discardableResult
mutating func updateValue(_ value: Value, forKey key: Key) -> Value? {
if let i = index[key] {
let old = pairs[i].value
pairs[i] = (key, value)
return old
}
index[key] = pairs.count
pairs.append((key, value))
return nil
}
struct Keys {
fileprivate let pairs: [(key: Key, value: Value)]
subscript(position: Int) -> Key { pairs[position].key }
}
struct Values {
fileprivate let pairs: [(key: Key, value: Value)]
var elements: [Value] { pairs.map(\.value) }
subscript(position: Int) -> Value { pairs[position].value }
}
}
extension OrderedDictionary: Sendable where Key: Sendable, Value: Sendable {}
extension OrderedDictionary.Keys: Sendable where Key: Sendable, Value: Sendable {}
extension OrderedDictionary.Values: Sendable where Key: Sendable, Value: Sendable {}