diff --git a/Sources/DataStructures/AVLTree.swift b/Sources/DataStructures/AVLTree.swift index 6ea961e..546ec64 100644 --- a/Sources/DataStructures/AVLTree.swift +++ b/Sources/DataStructures/AVLTree.swift @@ -222,5 +222,8 @@ extension AVLTree.Node: Hashable where Key: Hashable, Value: Hashable { } } +extension AVLTree.Node: Codable where Key: Codable, Value: Codable { } + extension AVLTree: Equatable where Value: Equatable { } extension AVLTree: Hashable where Key: Hashable, Value: Hashable { } +extension AVLTree: Codable where Key: Codable, Value: Codable { } diff --git a/Sources/DataStructures/AdjacencyList.swift b/Sources/DataStructures/AdjacencyList.swift index 0c8aff9..ce7d1ab 100644 --- a/Sources/DataStructures/AdjacencyList.swift +++ b/Sources/DataStructures/AdjacencyList.swift @@ -166,3 +166,4 @@ extension AdjacencyList { extension AdjacencyList: Equatable { } extension AdjacencyList: Hashable { } +extension AdjacencyList: Codable where Node: Codable { } diff --git a/Sources/DataStructures/Bimap.swift b/Sources/DataStructures/Bimap.swift index 035d1ef..9c08aac 100644 --- a/Sources/DataStructures/Bimap.swift +++ b/Sources/DataStructures/Bimap.swift @@ -238,3 +238,4 @@ extension Bimap { extension Bimap: Equatable { } extension Bimap: Hashable { } +extension Bimap: Codable where Key: Codable, Value: Codable { } diff --git a/Sources/DataStructures/BinaryHeap.swift b/Sources/DataStructures/BinaryHeap.swift index dbb0e9a..32a443c 100644 --- a/Sources/DataStructures/BinaryHeap.swift +++ b/Sources/DataStructures/BinaryHeap.swift @@ -129,3 +129,4 @@ public struct BinaryHeap { extension BinaryHeap: Equatable { } extension BinaryHeap: Hashable where Value: Hashable { } +extension BinaryHeap: Codable where Element: Codable, Value: Codable { } diff --git a/Sources/DataStructures/BinarySearchTree.swift b/Sources/DataStructures/BinarySearchTree.swift index 819355a..84a5064 100644 --- a/Sources/DataStructures/BinarySearchTree.swift +++ b/Sources/DataStructures/BinarySearchTree.swift @@ -155,3 +155,5 @@ extension BinarySearchTree: ExpressibleByArrayLiteral { extension BinarySearchTree: Equatable { } extension BinarySearchTree: Hashable where Value: Hashable { } + +// TODO: `Codable` diff --git a/Sources/DataStructures/CircularArray.swift b/Sources/DataStructures/CircularArray.swift index e69528d..f017798 100644 --- a/Sources/DataStructures/CircularArray.swift +++ b/Sources/DataStructures/CircularArray.swift @@ -193,3 +193,4 @@ extension Array { extension CircularArray: Equatable where Element: Equatable { } extension CircularArray: Hashable where Element: Hashable { } +extension CircularArray: Codable where Element: Codable { } diff --git a/Sources/DataStructures/ContiguousSegmentCollection/ContiguousSegmentCollection.swift b/Sources/DataStructures/ContiguousSegmentCollection/ContiguousSegmentCollection.swift index b1fde05..9f07dcd 100644 --- a/Sources/DataStructures/ContiguousSegmentCollection/ContiguousSegmentCollection.swift +++ b/Sources/DataStructures/ContiguousSegmentCollection/ContiguousSegmentCollection.swift @@ -527,3 +527,4 @@ extension ContiguousSegmentCollection: CustomStringConvertible { extension ContiguousSegmentCollection: Equatable where Segment: Equatable { } extension ContiguousSegmentCollection: Hashable where Segment: Hashable { } +extension ContiguousSegmentCollection: Codable where Metric: Codable, Segment: Codable { } diff --git a/Sources/DataStructures/Either.swift b/Sources/DataStructures/Either.swift index 5eccd5d..8757621 100644 --- a/Sources/DataStructures/Either.swift +++ b/Sources/DataStructures/Either.swift @@ -45,5 +45,38 @@ public enum Either { } } +extension Either: Codable where Left: Codable, Right: Codable { + + enum DecodingError: Error { + case invalidCase + } + + enum CodingKeys: String, CodingKey { + case left + case right + } + + public init(from decoder: Decoder) throws { + let container = try decoder.container(keyedBy: CodingKeys.self) + if let value = try? container.decode(Left.self, forKey: .left) { + self = .left(value) + } else if let value = try? container.decode(Right.self, forKey: .right) { + self = .right(value) + } else { + throw DecodingError.invalidCase + } + } + + public func encode(to encoder: Encoder) throws { + var container = encoder.container(keyedBy: CodingKeys.self) + switch self { + case let .left(value): + try container.encode(value, forKey: .left) + case let .right(value): + try container.encode(value, forKey: .right) + } + } +} + extension Either: Equatable where Left: Equatable, Right: Equatable { } extension Either: Hashable where Left: Hashable, Right: Hashable { } diff --git a/Sources/DataStructures/IntervalRelation.swift b/Sources/DataStructures/IntervalRelation.swift index ab6aa4d..00e2b1e 100644 --- a/Sources/DataStructures/IntervalRelation.swift +++ b/Sources/DataStructures/IntervalRelation.swift @@ -24,7 +24,7 @@ /// [Allen](https://en.wikipedia.org/wiki/James_F._Allen), refined by /// [Krokhin et al.](http://www.ics.uci.edu/~alspaugh/cls/shr/allen.html#Allen1983-mkti). /// -public enum IntervalRelation: InvertibleEnum { +public enum IntervalRelation: String, InvertibleEnum { // MARK: - Cases @@ -164,3 +164,4 @@ extension RangeProtocol { extension IntervalRelation: Equatable { } extension IntervalRelation: Hashable { } +extension IntervalRelation: Codable { } diff --git a/Sources/DataStructures/LinkedList.swift b/Sources/DataStructures/LinkedList.swift index c792021..c36744e 100644 --- a/Sources/DataStructures/LinkedList.swift +++ b/Sources/DataStructures/LinkedList.swift @@ -101,3 +101,5 @@ extension LinkedList: ExpressibleByArrayLiteral { extension LinkedList: Equatable where Element: Equatable { } extension LinkedList: Hashable where Element: Hashable { } + +// TODO: Codable diff --git a/Sources/DataStructures/Matrix.swift b/Sources/DataStructures/Matrix.swift index 6dcd21b..629c796 100644 --- a/Sources/DataStructures/Matrix.swift +++ b/Sources/DataStructures/Matrix.swift @@ -156,3 +156,4 @@ extension Matrix: CustomStringConvertible { extension Matrix: Equatable where Element: Equatable { } extension Matrix: Hashable where Element: Hashable { } +extension Matrix: Codable where Element: Codable { } diff --git a/Sources/DataStructures/OrderedDictionary.swift b/Sources/DataStructures/OrderedDictionary.swift index fff7ea1..0ca183c 100644 --- a/Sources/DataStructures/OrderedDictionary.swift +++ b/Sources/DataStructures/OrderedDictionary.swift @@ -134,3 +134,4 @@ extension OrderedDictionary: ExpressibleByDictionaryLiteral { extension OrderedDictionary: Equatable where Value: Equatable { } extension OrderedDictionary: Hashable where Value: Hashable { } +extension OrderedDictionary: Codable where Key: Codable, Value: Codable { } diff --git a/Sources/DataStructures/Pair/UnorderedPair.swift b/Sources/DataStructures/Pair/UnorderedPair.swift index 93d4f07..e4a65a0 100644 --- a/Sources/DataStructures/Pair/UnorderedPair.swift +++ b/Sources/DataStructures/Pair/UnorderedPair.swift @@ -59,8 +59,6 @@ extension UnorderedPair: Hashable where T: Hashable { } } -extension UnorderedPair: Codable where T: Codable { } - extension UnorderedPair: CustomStringConvertible { // MARK: - CustomStringConvertible @@ -70,3 +68,5 @@ extension UnorderedPair: CustomStringConvertible { return "{\(a),\(b)}" } } + +extension UnorderedPair: Codable where T: Codable { } diff --git a/Sources/DataStructures/Queue.swift b/Sources/DataStructures/Queue.swift index ac472e3..a2b28e5 100644 --- a/Sources/DataStructures/Queue.swift +++ b/Sources/DataStructures/Queue.swift @@ -72,3 +72,4 @@ extension Queue { extension Queue: Equatable where Element: Equatable { } extension Queue: Hashable where Element: Hashable { } +extension Queue: Codable where Element: Codable { } diff --git a/Sources/DataStructures/SortedArray.swift b/Sources/DataStructures/SortedArray.swift index aa580c9..48d5d92 100644 --- a/Sources/DataStructures/SortedArray.swift +++ b/Sources/DataStructures/SortedArray.swift @@ -172,3 +172,4 @@ extension SortedArray: ExpressibleByArrayLiteral { extension SortedArray: Equatable { } extension SortedArray: Hashable where Element: Hashable { } +extension SortedArray: Codable where Element: Codable { } diff --git a/Sources/DataStructures/SortedDictionary.swift b/Sources/DataStructures/SortedDictionary.swift index 70b32e1..e9a0698 100644 --- a/Sources/DataStructures/SortedDictionary.swift +++ b/Sources/DataStructures/SortedDictionary.swift @@ -205,3 +205,4 @@ extension SortedDictionary: Zero { extension SortedDictionary: Equatable where Value: Equatable { } extension SortedDictionary: Hashable where Value: Hashable { } +extension SortedDictionary: Codable where Key: Codable, Value: Codable { } diff --git a/Sources/DataStructures/Stack.swift b/Sources/DataStructures/Stack.swift index 35d69c7..c2c944d 100644 --- a/Sources/DataStructures/Stack.swift +++ b/Sources/DataStructures/Stack.swift @@ -168,3 +168,4 @@ extension Stack: Monoid { extension Stack: Equatable where Element: Equatable { } extension Stack: Hashable where Element: Hashable { } +extension Stack: Codable where Element: Codable { } diff --git a/Sources/DataStructures/Tree.swift b/Sources/DataStructures/Tree.swift index 1fce4f8..5909eb2 100644 --- a/Sources/DataStructures/Tree.swift +++ b/Sources/DataStructures/Tree.swift @@ -368,3 +368,5 @@ public func zip (_ a: Tree, _ b: Tree, _ f: (T, U) -> V) -> Tr extension Tree: Equatable where Leaf: Equatable, Branch: Equatable { } extension Tree: Hashable where Leaf: Hashable, Branch: Hashable { } + +// TODO: Codable