diff --git a/Sources/DataStructures/Pair/Pair.swift b/Sources/DataStructures/Pair/Pair.swift index ba978cc..2c512b4 100644 --- a/Sources/DataStructures/Pair/Pair.swift +++ b/Sources/DataStructures/Pair/Pair.swift @@ -32,3 +32,14 @@ public protocol Pair { /// Creates a `Pair` with the given values. init(_ a: A, _ b: B) } + +extension Pair { + + // MARK: - Instance Methods + + /// - Returns: A `Pair` with its members transformed by the given function. + public func map (_ f: (A,B) -> (C,D)) -> P where P: Pair, P.A == C, P.B == D { + let (c,d) = f(self.a, self.b) + return P(c,d) + } +} diff --git a/Sources/DataStructures/Pair/SymmetricPair.swift b/Sources/DataStructures/Pair/SymmetricPair.swift index f4f5251..ba556a9 100644 --- a/Sources/DataStructures/Pair/SymmetricPair.swift +++ b/Sources/DataStructures/Pair/SymmetricPair.swift @@ -17,3 +17,13 @@ extension SymmetricPair where A: Equatable { return a == value || b == value } } + +extension SymmetricPair { + + // MARK: - Instance Methods + + /// - Returns: A `SymmetricPair` with its members transformed by the given function. + public func map (_ f: (A) -> C) -> P where P: SymmetricPair, P.A == C { + return P(f(self.a), f(self.b)) + } +} diff --git a/Tests/AlgorithmsPerformanceTests/XCTestManifests.swift b/Tests/AlgorithmsPerformanceTests/XCTestManifests.swift index 379bca3..dc63c09 100644 --- a/Tests/AlgorithmsPerformanceTests/XCTestManifests.swift +++ b/Tests/AlgorithmsPerformanceTests/XCTestManifests.swift @@ -2,7 +2,7 @@ import XCTest extension StableSortPerformanceTests { static let __allTests = [ - ("testStableSort", testStableSort_O_nlogn), + ("testStableSort_O_nlogn", testStableSort_O_nlogn), ] } diff --git a/Tests/DataStructuresTests/CrossTests.swift b/Tests/DataStructuresTests/CrossTests.swift index 071d359..97b1d7a 100644 --- a/Tests/DataStructuresTests/CrossTests.swift +++ b/Tests/DataStructuresTests/CrossTests.swift @@ -30,4 +30,13 @@ class CrossTests: XCTestCase { XCTAssertFalse(a < b) XCTAssertFalse(b > a) } + + func testMap() { + let start = Cross("a",2) + let expected = Cross(4,"aa") + let function: (String,Int) -> (Int,String) = { string, int in + (int*2, string+string) + } + XCTAssertEqual(start.map(function),expected) + } } diff --git a/Tests/DataStructuresTests/PairTests/OrderedPairTests.swift b/Tests/DataStructuresTests/PairTests/OrderedPairTests.swift new file mode 100644 index 0000000..3076e34 --- /dev/null +++ b/Tests/DataStructuresTests/PairTests/OrderedPairTests.swift @@ -0,0 +1,18 @@ +// +// OrderedPairTests.swift +// Structure +// +// Created by Benjamin Wetherfield on 15/11/2018. +// + +import XCTest +import DataStructures + +class OrderedPairTests: XCTestCase { + + func testMap() { + let start = OrderedPair("a","ab") + let expected = OrderedPair(1,2) + XCTAssertEqual(start.map { $0.count }, expected) + } +} diff --git a/Tests/DataStructuresTests/UnorderedPairTests.swift b/Tests/DataStructuresTests/PairTests/UnorderedPairTests.swift similarity index 91% rename from Tests/DataStructuresTests/UnorderedPairTests.swift rename to Tests/DataStructuresTests/PairTests/UnorderedPairTests.swift index 72f4173..7473a3b 100644 --- a/Tests/DataStructuresTests/UnorderedPairTests.swift +++ b/Tests/DataStructuresTests/PairTests/UnorderedPairTests.swift @@ -58,6 +58,12 @@ class UnorderedPairTests: XCTestCase { } } } + + func testMap() { + let start = UnorderedPair("a","ab") + let expected = UnorderedPair(1,2) + XCTAssertEqual(start.map { $0.count }, expected) + } } func randomString(maxLength: Int = 10) -> String { diff --git a/Tests/DataStructuresTests/XCTestManifests.swift b/Tests/DataStructuresTests/XCTestManifests.swift index 93bf0c9..afb0dcf 100644 --- a/Tests/DataStructuresTests/XCTestManifests.swift +++ b/Tests/DataStructuresTests/XCTestManifests.swift @@ -35,6 +35,8 @@ extension ArrayExtensionsTests { extension BimapTests { static let __allTests = [ + ("testCompose", testCompose), + ("testComposeOperator", testComposeOperator), ("testInitEmpty", testInitEmpty), ("testKeySubscript", testKeySubscript), ("testRemoveAll", testRemoveAll), @@ -43,8 +45,6 @@ extension BimapTests { ("testUpdateKey", testUpdateKey), ("testUpdateValue", testUpdateValue), ("testValueSubscript", testValueSubscript), - ("testCompose", testCompose), - ("testComposeOperator", testComposeOperator), ] } @@ -53,7 +53,7 @@ extension BinaryHeapTests { ("testBalance", testBalance), ("testBasicInsertPop", testBasicInsertPop), ("testPopNil", testPopNil), - ("testSimpleBalance", testSimpleBalance) + ("testSimpleBalance", testSimpleBalance), ] } @@ -110,6 +110,7 @@ extension CrossTests { ("testComparableFalseEqual", testComparableFalseEqual), ("testComparableLexicographic", testComparableLexicographic), ("testComparableLexicographicFalse", testComparableLexicographicFalse), + ("testMap", testMap), ] } @@ -282,6 +283,12 @@ extension OrderedDictionaryTests { ] } +extension OrderedPairTests { + static let __allTests = [ + ("testMap", testMap), + ] +} + extension PairsTests { static let __allTests = [ ("testPairs", testPairs), @@ -517,6 +524,7 @@ extension UnorderedPairTests { ("testManyHashValuesIntForCollisions", testManyHashValuesIntForCollisions), ("testManyHashValuesString", testManyHashValuesString), ("testManyHashValuesStringForCollisions", testManyHashValuesStringForCollisions), + ("testMap", testMap), ] } @@ -581,6 +589,7 @@ public func __allTests() -> [XCTestCaseEntry] { testCase(MutableGraphTests.__allTests), testCase(NewTypeTests.__allTests), testCase(OrderedDictionaryTests.__allTests), + testCase(OrderedPairTests.__allTests), testCase(PairsTests.__allTests), testCase(QueueTests.__allTests), testCase(ReferenceTreeProtocolTests.__allTests),