Skip to content

Commit

Permalink
Add default conformance to Codable where possible
Browse files Browse the repository at this point in the history
  • Loading branch information
jsbean committed Aug 28, 2019
1 parent d08791c commit 9189c0a
Show file tree
Hide file tree
Showing 18 changed files with 57 additions and 3 deletions.
3 changes: 3 additions & 0 deletions Sources/DataStructures/AVLTree.swift
Expand Up @@ -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 { }
1 change: 1 addition & 0 deletions Sources/DataStructures/AdjacencyList.swift
Expand Up @@ -166,3 +166,4 @@ extension AdjacencyList {

extension AdjacencyList: Equatable { }
extension AdjacencyList: Hashable { }
extension AdjacencyList: Codable where Node: Codable { }
1 change: 1 addition & 0 deletions Sources/DataStructures/Bimap.swift
Expand Up @@ -238,3 +238,4 @@ extension Bimap {

extension Bimap: Equatable { }
extension Bimap: Hashable { }
extension Bimap: Codable where Key: Codable, Value: Codable { }
1 change: 1 addition & 0 deletions Sources/DataStructures/BinaryHeap.swift
Expand Up @@ -129,3 +129,4 @@ public struct BinaryHeap <Element: Hashable, Value: Comparable> {

extension BinaryHeap: Equatable { }
extension BinaryHeap: Hashable where Value: Hashable { }
extension BinaryHeap: Codable where Element: Codable, Value: Codable { }
2 changes: 2 additions & 0 deletions Sources/DataStructures/BinarySearchTree.swift
Expand Up @@ -155,3 +155,5 @@ extension BinarySearchTree: ExpressibleByArrayLiteral {

extension BinarySearchTree: Equatable { }
extension BinarySearchTree: Hashable where Value: Hashable { }

// TODO: `Codable`
1 change: 1 addition & 0 deletions Sources/DataStructures/CircularArray.swift
Expand Up @@ -193,3 +193,4 @@ extension Array {

extension CircularArray: Equatable where Element: Equatable { }
extension CircularArray: Hashable where Element: Hashable { }
extension CircularArray: Codable where Element: Codable { }
Expand Up @@ -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 { }
33 changes: 33 additions & 0 deletions Sources/DataStructures/Either.swift
Expand Up @@ -45,5 +45,38 @@ public enum Either <Left,Right> {
}
}

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 { }
3 changes: 2 additions & 1 deletion Sources/DataStructures/IntervalRelation.swift
Expand Up @@ -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

Expand Down Expand Up @@ -164,3 +164,4 @@ extension RangeProtocol {

extension IntervalRelation: Equatable { }
extension IntervalRelation: Hashable { }
extension IntervalRelation: Codable { }
2 changes: 2 additions & 0 deletions Sources/DataStructures/LinkedList.swift
Expand Up @@ -101,3 +101,5 @@ extension LinkedList: ExpressibleByArrayLiteral {

extension LinkedList: Equatable where Element: Equatable { }
extension LinkedList: Hashable where Element: Hashable { }

// TODO: Codable
1 change: 1 addition & 0 deletions Sources/DataStructures/Matrix.swift
Expand Up @@ -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 { }
1 change: 1 addition & 0 deletions Sources/DataStructures/OrderedDictionary.swift
Expand Up @@ -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 { }
4 changes: 2 additions & 2 deletions Sources/DataStructures/Pair/UnorderedPair.swift
Expand Up @@ -59,8 +59,6 @@ extension UnorderedPair: Hashable where T: Hashable {
}
}

extension UnorderedPair: Codable where T: Codable { }

extension UnorderedPair: CustomStringConvertible {

// MARK: - CustomStringConvertible
Expand All @@ -70,3 +68,5 @@ extension UnorderedPair: CustomStringConvertible {
return "{\(a),\(b)}"
}
}

extension UnorderedPair: Codable where T: Codable { }
1 change: 1 addition & 0 deletions Sources/DataStructures/Queue.swift
Expand Up @@ -72,3 +72,4 @@ extension Queue {

extension Queue: Equatable where Element: Equatable { }
extension Queue: Hashable where Element: Hashable { }
extension Queue: Codable where Element: Codable { }
1 change: 1 addition & 0 deletions Sources/DataStructures/SortedArray.swift
Expand Up @@ -172,3 +172,4 @@ extension SortedArray: ExpressibleByArrayLiteral {

extension SortedArray: Equatable { }
extension SortedArray: Hashable where Element: Hashable { }
extension SortedArray: Codable where Element: Codable { }
1 change: 1 addition & 0 deletions Sources/DataStructures/SortedDictionary.swift
Expand Up @@ -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 { }
1 change: 1 addition & 0 deletions Sources/DataStructures/Stack.swift
Expand Up @@ -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 { }
2 changes: 2 additions & 0 deletions Sources/DataStructures/Tree.swift
Expand Up @@ -368,3 +368,5 @@ public func zip <T,U,V> (_ a: Tree<T,T>, _ b: Tree<U,U>, _ f: (T, U) -> V) -> Tr

extension Tree: Equatable where Leaf: Equatable, Branch: Equatable { }
extension Tree: Hashable where Leaf: Hashable, Branch: Hashable { }

// TODO: Codable

0 comments on commit 9189c0a

Please sign in to comment.