Skip to content

Commit 662db5b

Browse files
bwetherfieldjsbean
authored andcommitted
Remove conformance conditions from Weight (#200)
1 parent 092b958 commit 662db5b

File tree

4 files changed

+33
-6
lines changed

4 files changed

+33
-6
lines changed

Sources/Algebra/AdditiveGroup.swift

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//
2+
// AdditiveGroup.swift
3+
// Algebra
4+
//
5+
// Created by Benjamin Wetherfield on 10/11/2018.
6+
//
7+
8+
public protocol AdditiveGroup: Additive, Invertible {
9+
10+
static prefix func - (_ element: Self) -> Self
11+
static func - (lhs: Self, rhs: Self) -> Self
12+
}
13+
14+
extension AdditiveGroup {
15+
16+
static prefix func - (_ element: Self) -> Self {
17+
return element.inverse
18+
}
19+
20+
static func - (lhs: Self, rhs: Self) -> Self {
21+
return lhs + -rhs
22+
}
23+
}

Sources/DataStructures/Graph/Protocols/WeightedGraphProtocol.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@
55
// Created by James Bean on 9/27/18.
66
//
77

8+
import Algebra
9+
810
/// Interface for weighted graphs.
911
public protocol WeightedGraphProtocol: GraphProtocol {
1012

1113
// MARK: - Associated Type
1214

1315
/// The type of the weight of an `Edge`.
14-
associatedtype Weight: Numeric
16+
associatedtype Weight
1517

1618
// MARK: - Instance Properties
1719

Sources/DataStructures/Graph/WeightedDirectedGraph.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@
44
//
55
// Created by James Bean on 9/27/18.
66
//
7+
import Algebra
78

89
/// Weighted, directed graph.
9-
public struct WeightedDirectedGraph <Node: Hashable, Weight: Numeric>:
10+
public struct WeightedDirectedGraph <Node: Hashable, Weight>:
1011
WeightedGraphProtocol,
1112
DirectedGraphProtocol
1213
{
@@ -44,6 +45,6 @@ extension WeightedDirectedGraph {
4445
}
4546
}
4647

47-
extension WeightedDirectedGraph: Equatable { }
48+
extension WeightedDirectedGraph: Equatable where Weight: Equatable { }
4849
extension WeightedDirectedGraph: Hashable where Weight: Hashable { }
4950

Sources/DataStructures/Graph/WeightedGraph.swift

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@
44
//
55
// Created by James Bean on 9/27/18.
66
//
7+
import Algebra
78

89
/// Weighted, undirected graph.
9-
public struct WeightedGraph <Node: Hashable, Weight: Numeric>:
10+
public struct WeightedGraph <Node: Hashable, Weight>:
1011
WeightedGraphProtocol,
1112
UndirectedGraphProtocol
1213
{
@@ -19,7 +20,7 @@ public struct WeightedGraph <Node: Hashable, Weight: Numeric>:
1920

2021
/// All of the edges contained herein stored with their weight.
2122
///
22-
/// An `Edge` is an `UnorderedPair` of `Node` values, and a `Weight` is any `Numeric`-conforming
23+
/// An `Edge` is an `UnorderedPair` of `Node` values, and a `Weight` is any `AdditiveGroup`-conforming
2324
/// value.
2425
public var weights: [Edge: Weight]
2526
}
@@ -52,5 +53,5 @@ extension WeightedGraph {
5253
}
5354
}
5455

55-
extension WeightedGraph: Equatable { }
56+
extension WeightedGraph: Equatable where Weight: Equatable { }
5657
extension WeightedGraph: Hashable where Weight: Hashable { }

0 commit comments

Comments
 (0)