-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathContainers.swift
More file actions
166 lines (125 loc) · 4.29 KB
/
Containers.swift
File metadata and controls
166 lines (125 loc) · 4.29 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
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
//
// Containers.swift
// CBORCoding
//
// Copyright © 2021 SomeRandomiOSDev. All rights reserved.
//
import Foundation
// MARK: - ArrayWrapper Definition
/// Internal class based array for reference semantics without having to involve the
/// Objective-C runtime (NSArray)
internal class ArrayWrapper<Element>: MutableCollection, RandomAccessCollection, RangeReplaceableCollection {
// MARK: - Private Properties
var array: [Element]
// MARK: - Properties
let indefiniteLength: Bool
// MARK: - Initialization
required init() {
self.array = []
self.indefiniteLength = false
}
init(indefiniteLength: Bool) {
self.array = []
self.indefiniteLength = indefiniteLength
}
init(wrapping array: [Element] = [], indefiniteLength: Bool = false) {
self.array = array
self.indefiniteLength = indefiniteLength
}
// MARK: - Sequence/Collection/MutableCollection/BidirectionalCollection/RandomAccessCollection Protocol Requirements
func makeIterator() -> Array<Element>.Iterator {
return array.makeIterator()
}
var startIndex: Int {
return array.startIndex
}
var endIndex: Int {
return array.endIndex
}
func index(after i: Int) -> Int {
return array.index(after: i)
}
func index(before i: Int) -> Int {
return array.index(before: i)
}
subscript(index: Int) -> Element {
get { return array[index] }
set { array[index] = newValue }
}
// MARK: - RangeReplaceableCollection Protocol Requirements
func replaceSubrange<C, R>(_ subrange: R, with newElements: __owned C) where C: Collection, R: RangeExpression, Element == C.Element, Index == R.Bound {
array.replaceSubrange(subrange, with: newElements)
}
func insert(_ newElement: __owned Element, at i: Int) {
array.insert(newElement, at: i)
}
func append(_ newElement: __owned Element) {
array.append(newElement)
}
}
// MARK: - CodingKeyDictionary Definition
/// Internal class based dictionary for reference semantics without having to
/// involve the Objective-C runtime (NSDictionary)
internal class CodingKeyDictionary<Value>: Sequence, ExpressibleByDictionaryLiteral {
// MARK: - Private Properties
private var keyValuePairs: [(key: CodingKey, value: Value)]
// MARK: - Properties
var count: Int {
return keyValuePairs.count
}
var keys: [CodingKey] {
return keyValuePairs.map { $0.0 }
}
var values: [Value] {
return keyValuePairs.map { $0.1 }
}
let indefiniteLength: Bool
// MARK: - Initialization
init(indefiniteLength: Bool = false) {
self.keyValuePairs = []
self.indefiniteLength = indefiniteLength
}
// MARK: - Subscripting
subscript(key: CodingKey) -> Value? {
get {
let value: Value?
if let intValue = key.intValue {
if let pair = keyValuePairs.first(where: { $0.key.intValue == intValue }) {
value = pair.value
} else {
value = nil
}
} else {
if let pair = keyValuePairs.first(where: { $0.key.stringValue == key.stringValue }) {
value = pair.value
} else {
value = nil
}
}
return value
}
set {
if let index = keyValuePairs.firstIndex(where: { $0.key.stringValue == key.stringValue }) {
if let newValue = newValue {
keyValuePairs[index] = (key, newValue)
} else {
keyValuePairs.remove(at: index)
}
} else {
if let newValue = newValue {
keyValuePairs.append((key, newValue))
}
}
}
}
// MARK: - Sequence Protocol Requirements
func makeIterator() -> Array<(key: CodingKey, value: Value)>.Iterator {
return keyValuePairs.makeIterator()
}
// MARK: - ExpressibleByDictionaryLiteral Protocol Requirements
typealias Key = CodingKey
required init(dictionaryLiteral elements: (Key, Value)...) {
self.keyValuePairs = elements
self.indefiniteLength = false
}
}