Skip to content

Commit

Permalink
Move Codable conformance to Graph and Edge protocols
Browse files Browse the repository at this point in the history
  • Loading branch information
davecom committed Apr 12, 2019
1 parent 5911a06 commit 2d3a546
Show file tree
Hide file tree
Showing 22 changed files with 79 additions and 179 deletions.
4 changes: 2 additions & 2 deletions Sources/SwiftGraph/Constructors.swift
Expand Up @@ -18,7 +18,7 @@

/// A type used to construct an UnweightedGraph with vertices of type V that is isomorphic to a star graph.
/// https://en.wikipedia.org/wiki/Star_(graph_theory)
public enum StarGraph<V: Equatable> {
public enum StarGraph<V: Equatable & Codable> {

/// Constructs an undirected UnweightedGraph isomorphic to a star graph.
///
Expand All @@ -41,7 +41,7 @@ public enum StarGraph<V: Equatable> {

/// A type used to construct UnweightedGraph with vertices of type V that is isomorphic to a complete graph.
/// https://en.wikipedia.org/wiki/Complete_graph
public enum CompleteGraph<V: Equatable> {
public enum CompleteGraph<V: Equatable & Codable> {

/// Constructs an undirected UnweightedGraph isomorphic to a complete graph.
///
Expand Down
2 changes: 1 addition & 1 deletion Sources/SwiftGraph/Cycle.swift
Expand Up @@ -2,7 +2,7 @@
// Cycle.swift
// SwiftGraph
//
// Copyright (c) 2017 David Kopec
// Copyright (c) 2017-2019 David Kopec
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
4 changes: 2 additions & 2 deletions Sources/SwiftGraph/Edge.swift
Expand Up @@ -2,7 +2,7 @@
// Edge.swift
// SwiftGraph
//
// Copyright (c) 2014-2016 David Kopec
// Copyright (c) 2014-2019 David Kopec
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -17,7 +17,7 @@
// limitations under the License.

/// A protocol that all edges in a graph must conform to.
public protocol Edge: CustomStringConvertible {
public protocol Edge: CustomStringConvertible & Codable {
/// The origin vertex of the edge
var u: Int {get set} //made modifiable for changing when removing vertices
/// The destination vertex of the edge
Expand Down
6 changes: 3 additions & 3 deletions Sources/SwiftGraph/Graph.swift
Expand Up @@ -2,7 +2,7 @@
// Graph.swift
// SwiftGraph
//
// Copyright (c) 2014-2016 David Kopec
// Copyright (c) 2014-2019 David Kopec
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -19,8 +19,8 @@
/// The protocol for all graphs.
/// You should generally use one of its two canonical class implementations,
/// *UnweightedGraph* and *WeightedGraph*
public protocol Graph: class, CustomStringConvertible, Collection {
associatedtype V: Equatable
public protocol Graph: class, CustomStringConvertible, Collection, Codable {
associatedtype V: Equatable & Codable
associatedtype E: Edge & Equatable
var vertices: [V] { get set }
var edges: [[E]] { get set }
Expand Down
2 changes: 1 addition & 1 deletion Sources/SwiftGraph/MST.swift
Expand Up @@ -2,7 +2,7 @@
// MST.swift
// SwiftGraph
//
// Copyright (c) 2017 David Kopec
// Copyright (c) 2017-2019 David Kopec
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion Sources/SwiftGraph/Queue.swift
Expand Up @@ -2,7 +2,7 @@
// Queue.swift
// SwiftGraph
//
// Copyright (c) 2014-2016 David Kopec
// Copyright (c) 2014-2019 David Kopec
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion Sources/SwiftGraph/Search.swift
Expand Up @@ -2,7 +2,7 @@
// Search.swift
// SwiftGraph
//
// Copyright (c) 2014-2016 David Kopec
// Copyright (c) 2014-2019 David Kopec
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion Sources/SwiftGraph/Sort.swift
Expand Up @@ -2,7 +2,7 @@
// Sort.swift
// SwiftGraph
//
// Copyright (c) 2016 David Kopec
// Copyright (c) 2016-2019 David Kopec
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion Sources/SwiftGraph/Stack.swift
Expand Up @@ -2,7 +2,7 @@
// Stack.swift
// SwiftGraph
//
// Copyright (c) 2014-2016 David Kopec
// Copyright (c) 2014-2019 David Kopec
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion Sources/SwiftGraph/SwiftPriorityQueue.swift
Expand Up @@ -2,7 +2,7 @@
// SwiftPriorityQueue.swift
// SwiftPriorityQueue
//
// Copyright (c) 2015-2017 David Kopec
// Copyright (c) 2015-2019 David Kopec
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
Expand Down
6 changes: 3 additions & 3 deletions Sources/SwiftGraph/UniqueElementsGraph.swift
Expand Up @@ -16,11 +16,11 @@
// See the License for the specific language governing permissions and
// limitations under the License.

public typealias UnweightedUniqueElementsGraph<V: Equatable> = UniqueElementsGraph<V, UnweightedEdge>
public typealias WeightedUniqueElementsGraph<V: Equatable, W: Equatable> = UniqueElementsGraph<V, WeightedEdge<W>>
public typealias UnweightedUniqueElementsGraph<V: Equatable & Codable> = UniqueElementsGraph<V, UnweightedEdge>
public typealias WeightedUniqueElementsGraph<V: Equatable & Codable, W: Equatable & Codable> = UniqueElementsGraph<V, WeightedEdge<W>>

/// A subclass of UnweightedGraph that ensures there are no pairs of equal vertices and no repeated edges.
open class UniqueElementsGraph<V: Equatable, E: Edge&Equatable>: Graph {
open class UniqueElementsGraph<V: Equatable & Codable, E: Edge & Equatable>: Graph {
public var vertices: [V] = [V]()
public var edges: [[E]] = [[E]]() //adjacency lists

Expand Down
4 changes: 2 additions & 2 deletions Sources/SwiftGraph/UnweightedEdge.swift
Expand Up @@ -2,7 +2,7 @@
// UnweightedEdge.swift
// SwiftGraph
//
// Copyright (c) 2014-2016 David Kopec
// Copyright (c) 2014-2019 David Kopec
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -17,7 +17,7 @@
// limitations under the License.

/// A basic unweighted edge.
public struct UnweightedEdge: Edge, CustomStringConvertible, Codable, Equatable {
public struct UnweightedEdge: Edge, CustomStringConvertible, Equatable {
public var u: Int
public var v: Int
public var directed: Bool
Expand Down
38 changes: 2 additions & 36 deletions Sources/SwiftGraph/UnweightedGraph.swift
Expand Up @@ -2,7 +2,7 @@
// UnweightedGraph.swift
// SwiftGraph
//
// Copyright (c) 2014-2016 David Kopec
// Copyright (c) 2014-2019 David Kopec
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -17,7 +17,7 @@
// limitations under the License.

/// A subclass of Graph with some convenience methods for adding and removing UnweightedEdges. WeightedEdges may be added to an UnweightedGraph but their weights will be ignored.
open class UnweightedGraph<V: Equatable>: Graph {
open class UnweightedGraph<V: Equatable & Codable>: Graph {
public var vertices: [V] = [V]()
public var edges: [[UnweightedEdge]] = [[UnweightedEdge]]() //adjacency lists

Expand Down Expand Up @@ -131,37 +131,3 @@ extension Graph where E == UnweightedEdge {
return false
}
}

public final class CodableUnweightedGraph<V: Codable & Equatable> : UnweightedGraph<V>, Codable {
enum CodingKeys: String, CodingKey {
case vertices = "vertices"
case edges = "edges"
}

override public init() {
super.init()
}

required public init(vertices: [V]) {
super.init(vertices: vertices)
}

public convenience init(fromGraph g: UnweightedGraph<V>) {
self.init()
vertices = g.vertices
edges = g.edges
}

public required init(from decoder: Decoder) throws {
super.init()
let rootContainer = try decoder.container(keyedBy: CodingKeys.self)
self.vertices = try rootContainer.decode([V].self, forKey: CodingKeys.vertices)
self.edges = try rootContainer.decode([[E]].self, forKey: CodingKeys.edges)
}

public func encode(to encoder: Encoder) throws {
var rootContainer = encoder.container(keyedBy: CodingKeys.self)
try rootContainer.encode(self.vertices, forKey: CodingKeys.vertices)
try rootContainer.encode(self.edges, forKey: CodingKeys.edges)
}
}
6 changes: 2 additions & 4 deletions Sources/SwiftGraph/WeightedEdge.swift
Expand Up @@ -2,7 +2,7 @@
// WeightedEdge.swift
// SwiftGraph
//
// Copyright (c) 2014-2017 David Kopec
// Copyright (c) 2014-2019 David Kopec
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -29,7 +29,7 @@ extension WeightedEdge: WeightedEdgeProtocol {
}

/// A weighted edge, who's weight subscribes to Comparable.
public struct WeightedEdge<W: Equatable>: Edge, CustomStringConvertible, Equatable {
public struct WeightedEdge<W: Equatable & Codable>: Edge, CustomStringConvertible, Equatable {
public var u: Int
public var v: Int
public var directed: Bool
Expand Down Expand Up @@ -58,8 +58,6 @@ public struct WeightedEdge<W: Equatable>: Edge, CustomStringConvertible, Equatab

}

extension WeightedEdge: Codable where W: Codable {}

extension WeightedEdge: Comparable where W: Comparable {
static public func < (lhs: WeightedEdge, rhs: WeightedEdge) -> Bool {
return lhs.weight < rhs.weight
Expand Down
38 changes: 2 additions & 36 deletions Sources/SwiftGraph/WeightedGraph.swift
Expand Up @@ -2,7 +2,7 @@
// WeightedGraph.swift
// SwiftGraph
//
// Copyright (c) 2014-2016 David Kopec
// Copyright (c) 2014-2019 David Kopec
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -17,7 +17,7 @@
// limitations under the License.

/// A subclass of Graph that has convenience methods for adding and removing WeightedEdges. All added Edges should have the same generic Comparable type W as the WeightedGraph itself.
open class WeightedGraph<V: Equatable, W: Equatable>: Graph {
open class WeightedGraph<V: Equatable & Codable, W: Equatable & Codable>: Graph {
public var vertices: [V] = [V]()
public var edges: [[WeightedEdge<W>]] = [[WeightedEdge<W>]]() //adjacency lists

Expand Down Expand Up @@ -154,37 +154,3 @@ extension Graph where E: WeightedEdgeProtocol {
return d
}
}

public final class CodableWeightedGraph<V: Codable & Equatable, W: Comparable & Numeric & Codable> : WeightedGraph<V, W>, Codable {
enum CodingKeys: String, CodingKey {
case vertices = "vertices"
case edges = "edges"
}

override public init() {
super.init()
}

required public init(vertices: [V]) {
super.init(vertices: vertices)
}

public convenience init(fromGraph g: WeightedGraph<V, W>) {
self.init()
vertices = g.vertices
edges = g.edges
}

public required init(from decoder: Decoder) throws {
super.init()
let rootContainer = try decoder.container(keyedBy: CodingKeys.self)
self.vertices = try rootContainer.decode([V].self, forKey: CodingKeys.vertices)
self.edges = try rootContainer.decode([[E]].self, forKey: CodingKeys.edges)
}

public func encode(to encoder: Encoder) throws {
var rootContainer = encoder.container(keyedBy: CodingKeys.self)
try rootContainer.encode(self.vertices, forKey: CodingKeys.vertices)
try rootContainer.encode(self.edges, forKey: CodingKeys.edges)
}
}
4 changes: 0 additions & 4 deletions SwiftGraph.xcodeproj/project.pbxproj
Expand Up @@ -49,7 +49,6 @@
B5EACB292172336900E527BD /* SearchPerformanceTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = B5DD6DA42171EEE1007EFF44 /* SearchPerformanceTests.swift */; };
B5EF143121791009008FCC5C /* UniqueElementsGraphInitTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = B5EF143021791009008FCC5C /* UniqueElementsGraphInitTests.swift */; };
B5EF143321791348008FCC5C /* UniqueElementsGraphHashableTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = B5EF143221791348008FCC5C /* UniqueElementsGraphHashableTests.swift */; };
B5EF1437217913F1008FCC5C /* EquatableTypes.swift in Sources */ = {isa = PBXBuildFile; fileRef = B5EF1436217913F1008FCC5C /* EquatableTypes.swift */; };
B5F07B6B222EB43000824F08 /* ArraysHaveSameElements.swift in Sources */ = {isa = PBXBuildFile; fileRef = B5F07B6A222EB43000824F08 /* ArraysHaveSameElements.swift */; };
B5F07B6D222EB4BD00824F08 /* WeightedGraphTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = B5F07B6C222EB4BD00824F08 /* WeightedGraphTests.swift */; };
B5FE1C212231DA0C008BACAA /* UnionPerformanceTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = B5FE1C202231DA0C008BACAA /* UnionPerformanceTests.swift */; };
Expand Down Expand Up @@ -139,7 +138,6 @@
B5EACB222172315E00E527BD /* SwiftGraphPerformanceTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = SwiftGraphPerformanceTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
B5EF143021791009008FCC5C /* UniqueElementsGraphInitTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UniqueElementsGraphInitTests.swift; sourceTree = "<group>"; };
B5EF143221791348008FCC5C /* UniqueElementsGraphHashableTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UniqueElementsGraphHashableTests.swift; sourceTree = "<group>"; };
B5EF1436217913F1008FCC5C /* EquatableTypes.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EquatableTypes.swift; sourceTree = "<group>"; };
B5F07B6A222EB43000824F08 /* ArraysHaveSameElements.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ArraysHaveSameElements.swift; sourceTree = "<group>"; };
B5F07B6C222EB4BD00824F08 /* WeightedGraphTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WeightedGraphTests.swift; sourceTree = "<group>"; };
B5FE1C202231DA0C008BACAA /* UnionPerformanceTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UnionPerformanceTests.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -338,7 +336,6 @@
B5EF1435217913E0008FCC5C /* Utils */ = {
isa = PBXGroup;
children = (
B5EF1436217913F1008FCC5C /* EquatableTypes.swift */,
B5F07B6A222EB43000824F08 /* ArraysHaveSameElements.swift */,
);
path = Utils;
Expand Down Expand Up @@ -558,7 +555,6 @@
B518BAB72232E8A40059DCB8 /* WeightedUniqueElementsGraphTests.swift in Sources */,
55E784281ED2971E003899D0 /* MSTTests.swift in Sources */,
B5100A4D208B97AA00C7A73A /* UnweightedGraphTests.swift in Sources */,
B5EF1437217913F1008FCC5C /* EquatableTypes.swift in Sources */,
B5F07B6D222EB4BD00824F08 /* WeightedGraphTests.swift in Sources */,
7985B91D1E5A4FCB00C100E7 /* DijkstraGraphTests.swift in Sources */,
7985B91F1E5A4FCB00C100E7 /* SwiftGraphSortTests.swift in Sources */,
Expand Down
6 changes: 3 additions & 3 deletions SwiftGraphSampleApp/AppDelegate.swift
Expand Up @@ -2,7 +2,7 @@
// AppDelegate.swift
// SwiftGraph
//
// Copyright (c) 2014-2016 David Kopec
// Copyright (c) 2014-2019 David Kopec
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -86,7 +86,7 @@ class NineTailView: NSView {
}
}

enum Coin: String {
enum Coin: String, Codable {
case Heads = "heads"
case Tails = "tails"
mutating func flip() {
Expand All @@ -98,7 +98,7 @@ enum Coin: String {
}
}

struct NineTailPosition: Equatable {
struct NineTailPosition: Equatable, Codable {
fileprivate var positionMatrix: [[Coin]]
init(matrix: [[Coin]]) {
positionMatrix = matrix
Expand Down
Expand Up @@ -21,7 +21,7 @@ import XCTest

class ConstructorsPerformanceTests: XCTestCase {

struct AnyEquatable<T: Equatable>: Equatable {
struct AnyEquatable<T: Equatable & Codable>: Equatable, Codable {
let value: T
}

Expand Down

0 comments on commit 2d3a546

Please sign in to comment.