Skip to content

Commit

Permalink
Add Abelian protocol, make AdditiveGroup default implementations publ…
Browse files Browse the repository at this point in the history
…ic (#222)
  • Loading branch information
jsbean authored Sep 6, 2019
1 parent aef5b88 commit 34b09ec
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 9 deletions.
9 changes: 9 additions & 0 deletions Sources/Algebra/Abelian.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
//
// Abelian.swift
// Algebra
//
// Created by James Bean on 9/6/19.
//

/// Interface defining a `Group` which is also commutative.
public protocol Abelian: Group { }
18 changes: 13 additions & 5 deletions Sources/Algebra/AdditiveGroup.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,27 @@
// Created by Benjamin Wetherfield on 10/11/2018.
//

/// The requirements for a type to display the behaviors of an `AdditiveGroup`.
///
/// The `AdditiveGroup` builds upon the `AdditiveMonoid` with an `inverse` operation.
public protocol AdditiveGroup: Additive, Invertible {


/// - Returns: The additive inverse of this `AdditiveGroup` element.
static prefix func - (_ element: Self) -> Self

/// - Returns: The difference between two `AdditiveGroup` elements.
static func - (lhs: Self, rhs: Self) -> Self
}

extension AdditiveGroup {

static prefix func - (_ element: Self) -> Self {

/// - Returns: The additive inverse of this `AdditiveGroup` element.
public static prefix func - (_ element: Self) -> Self {
return element.inverse
}

static func - (lhs: Self, rhs: Self) -> Self {

/// - Returns: The difference between two `AdditiveGroup` elements.
public static func - (lhs: Self, rhs: Self) -> Self {
return lhs + -rhs
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class GraphPerformanceTests: XCTestCase {
XCTAssert(benchmark.performance(is: .constant))
}

#warning("FIXME: Assess why the testEdgeFromSourceInCompleteGraph benchmark test files. It is currently disabled.")
// FIXME: Assess why the testEdgeFromSourceInCompleteGraph benchmark test files
func DISABLED_testEdgeFromSourceInCompleteGraph_O_n() {
let benchmark = Benchmark.mutating(
testPoints: Scale.small,
Expand Down
4 changes: 1 addition & 3 deletions Tests/DataStructuresTests/MatrixTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,9 @@ import DataStructures
class MatrixTests: XCTestCase {

func testInit() {

let amountRows = 2
let amountColumns = 3
var matrix = Matrix(height: amountRows, width: amountColumns, initial: 0)

let matrix = Matrix(height: amountRows, width: amountColumns, initial: 0)
for row in 0 ..< amountRows {
for column in 0 ..< amountColumns {
XCTAssertEqual(matrix[row, column], 0)
Expand Down

0 comments on commit 34b09ec

Please sign in to comment.