Skip to content

Commit 0bd7339

Browse files
bwetherfieldjsbean
authored andcommitted
Implement Cross Comparable conformance (#191)
1 parent ee95d59 commit 0bd7339

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

Sources/DataStructures/Pair/Cross.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,15 @@ extension Cross {
2929
}
3030
}
3131

32+
extension Cross: Comparable where T: Comparable, U: Comparable {
33+
34+
/// - Returns: true if and only if the first element of `lhs` is less than the first element
35+
/// of `rhs` OR if those elements are equal and the second element of `lhs` is less than the
36+
/// second element of `rhs` (lexicographic ordering).
37+
public static func < (lhs: Cross, rhs: Cross) -> Bool {
38+
return lhs.a < rhs.a || (lhs.a == rhs.a && lhs.b < rhs.b)
39+
}
40+
}
41+
3242
extension Cross: Equatable where T: Equatable, U: Equatable { }
3343
extension Cross: Hashable where T: Hashable, U: Hashable { }
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
//
2+
// CrossTests.swift
3+
// DataStructuresTests
4+
//
5+
// Created by James Bean on 10/20/18.
6+
//
7+
8+
import XCTest
9+
import DataStructures
10+
11+
class CrossTests: XCTestCase {
12+
13+
func testComparableFalseEqual() {
14+
let a = Cross(1,1)
15+
let b = Cross(1,1)
16+
XCTAssertEqual(a,b)
17+
XCTAssertFalse(a < b)
18+
}
19+
20+
func testComparableLexicographic() {
21+
let a = Cross(1,1)
22+
let b = Cross(1,2)
23+
XCTAssert(a < b)
24+
XCTAssert(b > a)
25+
}
26+
27+
func testComparableLexicographicFalse() {
28+
let a = Cross(1,2)
29+
let b = Cross(1,1)
30+
XCTAssertFalse(a < b)
31+
XCTAssertFalse(b > a)
32+
}
33+
}

0 commit comments

Comments
 (0)