Skip to content

Commit 9189c0a

Browse files
committed
Add default conformance to Codable where possible
1 parent d08791c commit 9189c0a

18 files changed

+57
-3
lines changed

Sources/DataStructures/AVLTree.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,5 +222,8 @@ extension AVLTree.Node: Hashable where Key: Hashable, Value: Hashable {
222222
}
223223
}
224224

225+
extension AVLTree.Node: Codable where Key: Codable, Value: Codable { }
226+
225227
extension AVLTree: Equatable where Value: Equatable { }
226228
extension AVLTree: Hashable where Key: Hashable, Value: Hashable { }
229+
extension AVLTree: Codable where Key: Codable, Value: Codable { }

Sources/DataStructures/AdjacencyList.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,3 +166,4 @@ extension AdjacencyList {
166166

167167
extension AdjacencyList: Equatable { }
168168
extension AdjacencyList: Hashable { }
169+
extension AdjacencyList: Codable where Node: Codable { }

Sources/DataStructures/Bimap.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,3 +238,4 @@ extension Bimap {
238238

239239
extension Bimap: Equatable { }
240240
extension Bimap: Hashable { }
241+
extension Bimap: Codable where Key: Codable, Value: Codable { }

Sources/DataStructures/BinaryHeap.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,3 +129,4 @@ public struct BinaryHeap <Element: Hashable, Value: Comparable> {
129129

130130
extension BinaryHeap: Equatable { }
131131
extension BinaryHeap: Hashable where Value: Hashable { }
132+
extension BinaryHeap: Codable where Element: Codable, Value: Codable { }

Sources/DataStructures/BinarySearchTree.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,3 +155,5 @@ extension BinarySearchTree: ExpressibleByArrayLiteral {
155155

156156
extension BinarySearchTree: Equatable { }
157157
extension BinarySearchTree: Hashable where Value: Hashable { }
158+
159+
// TODO: `Codable`

Sources/DataStructures/CircularArray.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,3 +193,4 @@ extension Array {
193193

194194
extension CircularArray: Equatable where Element: Equatable { }
195195
extension CircularArray: Hashable where Element: Hashable { }
196+
extension CircularArray: Codable where Element: Codable { }

Sources/DataStructures/ContiguousSegmentCollection/ContiguousSegmentCollection.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -527,3 +527,4 @@ extension ContiguousSegmentCollection: CustomStringConvertible {
527527

528528
extension ContiguousSegmentCollection: Equatable where Segment: Equatable { }
529529
extension ContiguousSegmentCollection: Hashable where Segment: Hashable { }
530+
extension ContiguousSegmentCollection: Codable where Metric: Codable, Segment: Codable { }

Sources/DataStructures/Either.swift

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,5 +45,38 @@ public enum Either <Left,Right> {
4545
}
4646
}
4747

48+
extension Either: Codable where Left: Codable, Right: Codable {
49+
50+
enum DecodingError: Error {
51+
case invalidCase
52+
}
53+
54+
enum CodingKeys: String, CodingKey {
55+
case left
56+
case right
57+
}
58+
59+
public init(from decoder: Decoder) throws {
60+
let container = try decoder.container(keyedBy: CodingKeys.self)
61+
if let value = try? container.decode(Left.self, forKey: .left) {
62+
self = .left(value)
63+
} else if let value = try? container.decode(Right.self, forKey: .right) {
64+
self = .right(value)
65+
} else {
66+
throw DecodingError.invalidCase
67+
}
68+
}
69+
70+
public func encode(to encoder: Encoder) throws {
71+
var container = encoder.container(keyedBy: CodingKeys.self)
72+
switch self {
73+
case let .left(value):
74+
try container.encode(value, forKey: .left)
75+
case let .right(value):
76+
try container.encode(value, forKey: .right)
77+
}
78+
}
79+
}
80+
4881
extension Either: Equatable where Left: Equatable, Right: Equatable { }
4982
extension Either: Hashable where Left: Hashable, Right: Hashable { }

Sources/DataStructures/IntervalRelation.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
/// [Allen](https://en.wikipedia.org/wiki/James_F._Allen), refined by
2525
/// [Krokhin et al.](http://www.ics.uci.edu/~alspaugh/cls/shr/allen.html#Allen1983-mkti).
2626
///
27-
public enum IntervalRelation: InvertibleEnum {
27+
public enum IntervalRelation: String, InvertibleEnum {
2828

2929
// MARK: - Cases
3030

@@ -164,3 +164,4 @@ extension RangeProtocol {
164164

165165
extension IntervalRelation: Equatable { }
166166
extension IntervalRelation: Hashable { }
167+
extension IntervalRelation: Codable { }

Sources/DataStructures/LinkedList.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,3 +101,5 @@ extension LinkedList: ExpressibleByArrayLiteral {
101101

102102
extension LinkedList: Equatable where Element: Equatable { }
103103
extension LinkedList: Hashable where Element: Hashable { }
104+
105+
// TODO: Codable

Sources/DataStructures/Matrix.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,3 +156,4 @@ extension Matrix: CustomStringConvertible {
156156

157157
extension Matrix: Equatable where Element: Equatable { }
158158
extension Matrix: Hashable where Element: Hashable { }
159+
extension Matrix: Codable where Element: Codable { }

Sources/DataStructures/OrderedDictionary.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,3 +134,4 @@ extension OrderedDictionary: ExpressibleByDictionaryLiteral {
134134

135135
extension OrderedDictionary: Equatable where Value: Equatable { }
136136
extension OrderedDictionary: Hashable where Value: Hashable { }
137+
extension OrderedDictionary: Codable where Key: Codable, Value: Codable { }

Sources/DataStructures/Pair/UnorderedPair.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,6 @@ extension UnorderedPair: Hashable where T: Hashable {
5959
}
6060
}
6161

62-
extension UnorderedPair: Codable where T: Codable { }
63-
6462
extension UnorderedPair: CustomStringConvertible {
6563

6664
// MARK: - CustomStringConvertible
@@ -70,3 +68,5 @@ extension UnorderedPair: CustomStringConvertible {
7068
return "{\(a),\(b)}"
7169
}
7270
}
71+
72+
extension UnorderedPair: Codable where T: Codable { }

Sources/DataStructures/Queue.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,3 +72,4 @@ extension Queue {
7272

7373
extension Queue: Equatable where Element: Equatable { }
7474
extension Queue: Hashable where Element: Hashable { }
75+
extension Queue: Codable where Element: Codable { }

Sources/DataStructures/SortedArray.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,3 +172,4 @@ extension SortedArray: ExpressibleByArrayLiteral {
172172

173173
extension SortedArray: Equatable { }
174174
extension SortedArray: Hashable where Element: Hashable { }
175+
extension SortedArray: Codable where Element: Codable { }

Sources/DataStructures/SortedDictionary.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,3 +205,4 @@ extension SortedDictionary: Zero {
205205

206206
extension SortedDictionary: Equatable where Value: Equatable { }
207207
extension SortedDictionary: Hashable where Value: Hashable { }
208+
extension SortedDictionary: Codable where Key: Codable, Value: Codable { }

Sources/DataStructures/Stack.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,3 +168,4 @@ extension Stack: Monoid {
168168

169169
extension Stack: Equatable where Element: Equatable { }
170170
extension Stack: Hashable where Element: Hashable { }
171+
extension Stack: Codable where Element: Codable { }

Sources/DataStructures/Tree.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,3 +368,5 @@ public func zip <T,U,V> (_ a: Tree<T,T>, _ b: Tree<U,U>, _ f: (T, U) -> V) -> Tr
368368

369369
extension Tree: Equatable where Leaf: Equatable, Branch: Equatable { }
370370
extension Tree: Hashable where Leaf: Hashable, Branch: Hashable { }
371+
372+
// TODO: Codable

0 commit comments

Comments
 (0)