From 2097db778fe35ee381848320b32cd4c310d0f9ea Mon Sep 17 00:00:00 2001 From: Joe DeCapo Date: Tue, 15 Aug 2017 19:56:39 -0500 Subject: [PATCH 1/2] Use IntRef class for Chapter 4 tests with identity comparisons --- EOP/Types/IntRef.swift | 22 ++++ .../project.pbxproj | 8 +- ElementsOfProgramming/Chapter01.swift | 14 +- .../Chapter04Tests.swift | 120 ++++++++++++------ 4 files changed, 112 insertions(+), 52 deletions(-) create mode 100644 EOP/Types/IntRef.swift diff --git a/EOP/Types/IntRef.swift b/EOP/Types/IntRef.swift new file mode 100644 index 0000000..dbdb206 --- /dev/null +++ b/EOP/Types/IntRef.swift @@ -0,0 +1,22 @@ +// +// IntRef.swift +// EOP +// + +public class IntRef { + public var value: Int + + public init(_ value: Int) { + self.value = value + } +} + +extension IntRef: Regular { + public static func <(lhs: IntRef, rhs: IntRef) -> Bool { + return lhs.value < rhs.value + } + + public static func ==(lhs: IntRef, rhs: IntRef) -> Bool { + return lhs.value == rhs.value + } +} diff --git a/ElementsOfProgramming.xcodeproj/project.pbxproj b/ElementsOfProgramming.xcodeproj/project.pbxproj index 4273076..33f10f9 100644 --- a/ElementsOfProgramming.xcodeproj/project.pbxproj +++ b/ElementsOfProgramming.xcodeproj/project.pbxproj @@ -8,6 +8,7 @@ /* Begin PBXBuildFile section */ C60C392F1F1EE64200243F20 /* Chapter09.swift in Sources */ = {isa = PBXBuildFile; fileRef = C60C392E1F1EE64200243F20 /* Chapter09.swift */; }; + C62843881F43CF8800481E76 /* Chapter04Tests.swift in Sources */ = {isa = PBXBuildFile; fileRef = C65D60B31F1C45BD00F62E3B /* Chapter04Tests.swift */; }; C6459EC81F26F65700F0E894 /* Chapter12.swift in Sources */ = {isa = PBXBuildFile; fileRef = C6459EC71F26F65700F0E894 /* Chapter12.swift */; }; C65D60B11F1B275000F62E3B /* Chapter04.swift in Sources */ = {isa = PBXBuildFile; fileRef = C65D60B01F1B275000F62E3B /* Chapter04.swift */; }; C66BEB321F23F65A00BDECD8 /* Chapter07.swift in Sources */ = {isa = PBXBuildFile; fileRef = C66BEB311F23F65A00BDECD8 /* Chapter07.swift */; }; @@ -45,7 +46,6 @@ C6C3BA161F3806F200D1CC51 /* ConceptTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = C6910EAF1F1D7442009F84EB /* ConceptTests.swift */; }; C6C3BA171F38075800D1CC51 /* Chapter02Tests.swift in Sources */ = {isa = PBXBuildFile; fileRef = C61124C71F184B74005B18ED /* Chapter02Tests.swift */; }; C6C3BA181F38080C00D1CC51 /* Chapter03Tests.swift in Sources */ = {isa = PBXBuildFile; fileRef = C66DB47B1F1AE8110019A2BB /* Chapter03Tests.swift */; }; - C6C3BA191F38093600D1CC51 /* Chapter04Tests.swift in Sources */ = {isa = PBXBuildFile; fileRef = C65D60B31F1C45BD00F62E3B /* Chapter04Tests.swift */; }; C6C3FDD61F37D3B800936E7A /* Rational.swift in Sources */ = {isa = PBXBuildFile; fileRef = C6CC1AB21F2ADD8400E468B3 /* Rational.swift */; }; C6CC1AA31F2AD9CB00E468B3 /* Concepts.swift in Headers */ = {isa = PBXBuildFile; fileRef = C6FEC39C1F16ECD60001B732 /* Concepts.swift */; }; C6CC1AA41F2AD9CB00E468B3 /* TypeFunctions.swift in Headers */ = {isa = PBXBuildFile; fileRef = C6FEC39A1F16DAC10001B732 /* TypeFunctions.swift */; }; @@ -66,6 +66,7 @@ C6F3083A1F25A2D100AFBEB3 /* Chapter10.swift in Sources */ = {isa = PBXBuildFile; fileRef = C6F308391F25A2D100AFBEB3 /* Chapter10.swift */; }; C6F549931F2AE2BF0057BC02 /* Tuples.swift in Sources */ = {isa = PBXBuildFile; fileRef = C6F549921F2AE2BF0057BC02 /* Tuples.swift */; }; C6F646351F43B60C00D902EA /* IntegerSpecialCaseProcedures.swift in Sources */ = {isa = PBXBuildFile; fileRef = C6F646341F43B60C00D902EA /* IntegerSpecialCaseProcedures.swift */; }; + C6F646371F43C5CB00D902EA /* IntRef.swift in Sources */ = {isa = PBXBuildFile; fileRef = C6F646361F43C5CB00D902EA /* IntRef.swift */; }; C6FEC3901F16DA210001B732 /* main.swift in Sources */ = {isa = PBXBuildFile; fileRef = C6FEC38F1F16DA210001B732 /* main.swift */; }; C6FEC3971F16DA4C0001B732 /* Chapter01.swift in Sources */ = {isa = PBXBuildFile; fileRef = C6FEC3961F16DA4C0001B732 /* Chapter01.swift */; }; C6FEC3991F16DA810001B732 /* Chapter02.swift in Sources */ = {isa = PBXBuildFile; fileRef = C6FEC3981F16DA810001B732 /* Chapter02.swift */; }; @@ -159,6 +160,7 @@ C6F308391F25A2D100AFBEB3 /* Chapter10.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Chapter10.swift; sourceTree = ""; }; C6F549921F2AE2BF0057BC02 /* Tuples.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Tuples.swift; sourceTree = ""; }; C6F646341F43B60C00D902EA /* IntegerSpecialCaseProcedures.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = IntegerSpecialCaseProcedures.swift; sourceTree = ""; }; + C6F646361F43C5CB00D902EA /* IntRef.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = IntRef.swift; sourceTree = ""; }; C6FEC38C1F16DA210001B732 /* ElementsOfProgramming */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = ElementsOfProgramming; sourceTree = BUILT_PRODUCTS_DIR; }; C6FEC38F1F16DA210001B732 /* main.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = main.swift; sourceTree = ""; }; C6FEC3961F16DA4C0001B732 /* Chapter01.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Chapter01.swift; sourceTree = ""; }; @@ -249,6 +251,7 @@ C6B297A51F2060E2000E53B6 /* Int64+Extensions.swift */, C6B297A61F2060E2000E53B6 /* Int+Extensions.swift */, C6F646341F43B60C00D902EA /* IntegerSpecialCaseProcedures.swift */, + C6F646361F43C5CB00D902EA /* IntRef.swift */, C6CC1AB21F2ADD8400E468B3 /* Rational.swift */, C66BEB281F231C1B00BDECD8 /* SList.swift */, C6E03B6E1F282CF100B65BD5 /* String.swift */, @@ -467,13 +470,13 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( - C6C3BA191F38093600D1CC51 /* Chapter04Tests.swift in Sources */, C6C3BA181F38080C00D1CC51 /* Chapter03Tests.swift in Sources */, C6C3BA171F38075800D1CC51 /* Chapter02Tests.swift in Sources */, C6C3BA161F3806F200D1CC51 /* ConceptTests.swift in Sources */, C6C3BA151F3806C600D1CC51 /* Chapter01Tests.swift in Sources */, C6C3BA091F3806B400D1CC51 /* Chapter01.swift in Sources */, C6C3BA0A1F3806B400D1CC51 /* Chapter02.swift in Sources */, + C62843881F43CF8800481E76 /* Chapter04Tests.swift in Sources */, C6C3BA0B1F3806B400D1CC51 /* Chapter03.swift in Sources */, C6C3BA0C1F3806B400D1CC51 /* Chapter04.swift in Sources */, C6C3BA0D1F3806B400D1CC51 /* Chapter05.swift in Sources */, @@ -514,6 +517,7 @@ C6CC1AB01F2ADB9000E468B3 /* Concepts.swift in Sources */, C6CC1AB11F2ADB9000E468B3 /* TypeFunctions.swift in Sources */, C6CC1AAE1F2ADB1900E468B3 /* SList.swift in Sources */, + C6F646371F43C5CB00D902EA /* IntRef.swift in Sources */, C6F549931F2AE2BF0057BC02 /* Tuples.swift in Sources */, C6CC1AAF1F2ADB1900E468B3 /* String.swift in Sources */, C6CC1AA71F2ADB1900E468B3 /* Float+Extensions.swift in Sources */, diff --git a/ElementsOfProgramming/Chapter01.swift b/ElementsOfProgramming/Chapter01.swift index aa3610b..a9e132c 100644 --- a/ElementsOfProgramming/Chapter01.swift +++ b/ElementsOfProgramming/Chapter01.swift @@ -14,17 +14,7 @@ func plus_1(a: inout Int, b: inout Int) -> Int { return a + b } -class IntReference { - var value: Int - - init(_ value: Int) { - self.value = value - } -} - -func plus_2(a: IntReference, - b: IntReference, - c: IntReference) { +func plus_2(a: IntRef, b: IntRef, c: IntRef) { c.value = a.value + b.value } @@ -58,7 +48,7 @@ public func equal(x: T, y: T) -> Bool { return x == y } // MARK: Playground examples func playgroundPlus2() { - let a = IntReference(2), b = IntReference(2), c = IntReference(0) + let a = IntRef(2), b = IntRef(2), c = IntRef(0) print(c.value) plus_2(a: a, b: b, c: c) print(c.value) diff --git a/ElementsOfProgrammingTests/Chapter04Tests.swift b/ElementsOfProgrammingTests/Chapter04Tests.swift index c25101d..66ddcbc 100644 --- a/ElementsOfProgrammingTests/Chapter04Tests.swift +++ b/ElementsOfProgrammingTests/Chapter04Tests.swift @@ -42,69 +42,113 @@ class Chapter04Tests: XCTestCase { // clusters: != > <= >= -- see concept_TotallyOrdered } - func testOrderSelection() { + func testSelect_0_2() { let a = 3 let b = 3 let c = 4 - let d = 4 - XCTAssert(select_0_2(a: a, b: b, r: less) == a) - XCTAssert(select_0_2(a: b, b: a, r: less) == b) - XCTAssert(select_0_2(a: a, b: c, r: less) == a) - XCTAssert(select_0_2(a: c, b: a, r: less) == a) + let aRef = IntRef(a) + let bRef = IntRef(b) + let cRef = IntRef(c) + XCTAssert(select_0_2(a: aRef, b: bRef, r: less) === aRef) + XCTAssert(select_0_2(a: bRef, b: aRef, r: less) === bRef) + XCTAssert(select_0_2(a: aRef, b: cRef, r: less) === aRef) + XCTAssert(select_0_2(a: cRef, b: aRef, r: less) === aRef) XCTAssert(select_0_2(a: a, b: c, r: less) == a) XCTAssert(select_0_2(a: c, b: a, r: less) == a) - - XCTAssert(select_1_2(a: a, b: b, r: less) == b) - XCTAssert(select_1_2(a: b, b: a, r: less) == a) - XCTAssert(select_1_2(a: a, b: c, r: less) == c) - XCTAssert(select_1_2(a: c, b: a, r: less) == c) - XCTAssert(select_1_2(a: a, b: c, r: less) == c) - XCTAssert(select_1_2(a: c, b: a, r: less) == c) - + let p1 = Pair(m0: 1, m1: 1) let p2 = Pair(m0: 1, m1: 2) XCTAssert(select_0_2(ia: 1, ib: 2, a: p1, b: p2, r: lessFirst) == p1) XCTAssert(select_0_2(ia: 1, ib: 2, a: p2, b: p1, r: lessFirst) == p2) + } + + func testSelect_1_2() { + let a = 3 + let b = 3 + let c = 4 + let aRef = IntRef(a) + let bRef = IntRef(b) + let cRef = IntRef(c) + XCTAssert(select_1_2(a: aRef, b: bRef, r: less) === bRef) + XCTAssert(select_1_2(a: bRef, b: aRef, r: less) === aRef) + XCTAssert(select_1_2(a: aRef, b: cRef, r: less) === cRef) + XCTAssert(select_1_2(a: cRef, b: aRef, r: less) === cRef) + XCTAssert(select_1_2(a: a, b: c, r: less) == c) + XCTAssert(select_1_2(a: c, b: a, r: less) == c) + + let p1 = Pair(m0: 1, m1: 1) + let p2 = Pair(m0: 1, m1: 2) XCTAssert(select_1_2(ia: 1, ib: 2, a: p1, b: p2, r: lessFirst) == p2) XCTAssert(select_1_2(ia: 1, ib: 2, a: p2, b: p1, r: lessFirst) == p1) - - XCTAssert(select_0_3(a: a, b: b, c: c, r: less) == a) - XCTAssert(select_0_3(a: a, b: c, c: b, r: less) == a) - XCTAssert(select_0_3(a: b, b: a, c: c, r: less) == b) - XCTAssert(select_0_3(a: b, b: c, c: a, r: less) == b) - XCTAssert(select_0_3(a: c, b: a, c: b, r: less) == a) - XCTAssert(select_0_3(a: c, b: b, c: a, r: less) == b) + } + + func testSelect_0_3() { + let a = 3 + let b = 3 + let c = 4 + let d = 4 + let aRef = IntRef(a) + let bRef = IntRef(b) + let cRef = IntRef(c) + XCTAssert(select_0_3(a: aRef, b: bRef, c: cRef, r: less) === aRef) + XCTAssert(select_0_3(a: aRef, b: cRef, c: bRef, r: less) === aRef) + XCTAssert(select_0_3(a: bRef, b: aRef, c: cRef, r: less) === bRef) + XCTAssert(select_0_3(a: bRef, b: cRef, c: aRef, r: less) === bRef) + XCTAssert(select_0_3(a: cRef, b: aRef, c: bRef, r: less) === aRef) + XCTAssert(select_0_3(a: cRef, b: bRef, c: aRef, r: less) === bRef) XCTAssert(select_0_3(a: a, b: c, c: d, r: less) == a) XCTAssert(select_0_3(a: c, b: a, c: d, r: less) == a) XCTAssert(select_0_3(a: d, b: c, c: a, r: less) == a) - - XCTAssert(select_2_3(a: b, b: c, c: d, r: less) == d) - XCTAssert(select_2_3(a: c, b: b, c: d, r: less) == d) - XCTAssert(select_2_3(a: b, b: d, c: c, r: less) == c) - XCTAssert(select_2_3(a: d, b: b, c: c, r: less) == c) - XCTAssert(select_2_3(a: c, b: d, c: b, r: less) == d) - XCTAssert(select_2_3(a: d, b: c, c: b, r: less) == c) + } + + func testSelect_2_3() { + let a = 3 + let b = 3 + let c = 4 + let d = 4 + let bRef = IntRef(b) + let cRef = IntRef(c) + let dRef = IntRef(d) + XCTAssert(select_2_3(a: bRef, b: cRef, c: dRef, r: less) === dRef) + XCTAssert(select_2_3(a: cRef, b: bRef, c: dRef, r: less) === dRef) + XCTAssert(select_2_3(a: bRef, b: dRef, c: cRef, r: less) === cRef) + XCTAssert(select_2_3(a: dRef, b: bRef, c: cRef, r: less) === cRef) + XCTAssert(select_2_3(a: cRef, b: dRef, c: bRef, r: less) === dRef) + XCTAssert(select_2_3(a: dRef, b: cRef, c: bRef, r: less) === cRef) XCTAssert(select_2_3(a: a, b: c, c: d, r: less) == d) XCTAssert(select_2_3(a: c, b: a, c: d, r: less) == c) XCTAssert(select_2_3(a: d, b: c, c: a, r: less) == c) - - // Test select13ab - - XCTAssert(select_1_3(a: a, b: b, c: c, r: less) == b) - XCTAssert(select_1_3(a: a, b: c, c: b, r: less) == b) - XCTAssert(select_1_3(a: b, b: a, c: c, r: less) == a) - XCTAssert(select_1_3(a: b, b: c, c: a, r: less) == a) - XCTAssert(select_1_3(a: c, b: a, c: b, r: less) == b) - XCTAssert(select_1_3(a: c, b: b, c: a, r: less) == a) + } + + // Test select13ab + func testSelect_1_3() { + let a = 3 + let b = 3 + let c = 4 + let d = 4 + let aRef = IntRef(a) + let bRef = IntRef(b) + let cRef = IntRef(c) + XCTAssert(select_1_3(a: aRef, b: bRef, c: cRef, r: less) === bRef) + XCTAssert(select_1_3(a: aRef, b: cRef, c: bRef, r: less) === bRef) + XCTAssert(select_1_3(a: bRef, b: aRef, c: cRef, r: less) === aRef) + XCTAssert(select_1_3(a: bRef, b: cRef, c: aRef, r: less) === aRef) + XCTAssert(select_1_3(a: cRef, b: aRef, c: bRef, r: less) === bRef) + XCTAssert(select_1_3(a: cRef, b: bRef, c: aRef, r: less) === aRef) XCTAssert(select_1_3(a: a, b: c, c: d, r: less) == c) XCTAssert(select_1_3(a: c, b: a, c: d, r: less) == c) XCTAssert(select_1_3(a: d, b: c, c: a, r: less) == c) - + } + + func testSelect_1_4() { // Test select_1_4_ab_cd // Test select_1_4_ab // FIXME: Fix these tests // algorithmSelect_1_4() // algorithmSelect_1_4_stabilityIndices() + } + + func testSelect_2_5() { algorithmSelect_2_5_stabilityIndices() } From 9061b783179f7d40e23e389bee33dadd5629709b Mon Sep 17 00:00:00 2001 From: Joe DeCapo Date: Tue, 15 Aug 2017 23:23:41 -0500 Subject: [PATCH 2/2] [wip] select_1_4 --- EOP/Types/ArrayIterator.swift | 86 +++++++++++++++ EOP/Types/Tuples.swift | 16 +++ .../project.pbxproj | 4 + .../ElementsOfProgrammingTests.xcscheme | 2 +- .../Chapter04Tests.swift | 101 ++++++++++-------- 5 files changed, 165 insertions(+), 44 deletions(-) create mode 100644 EOP/Types/ArrayIterator.swift diff --git a/EOP/Types/ArrayIterator.swift b/EOP/Types/ArrayIterator.swift new file mode 100644 index 0000000..6674507 --- /dev/null +++ b/EOP/Types/ArrayIterator.swift @@ -0,0 +1,86 @@ +// +// ArrayIterator.swift +// EOP +// + +public final class ArrayIterator: BidirectionalIterator, Mutable { + + private class ArrayRef { + var value: [U?] + + init(_ value: [U?]) { + self.value = value + } + } + + private var array: ArrayRef + private var index = 0 + + public init(_ array: [T?]) { + self.array = ArrayRef(array) + } + + public init(_ array: [T]) { + self.array = ArrayRef(array) + } + + private init(_ array: ArrayRef, _ index: Int) { + self.array = array + self.index = index + } + + public var sink: T? { + get { + return array.value[index] + } + set { + array.value[index] = newValue + } + } + + public var iteratorSuccessor: ArrayIterator? { + return ArrayIterator(array, index + 1) + } + + public var iteratorPredecessor: ArrayIterator? { + return ArrayIterator(array, index - 1) + } + + public var deref: T? { + get { + return array.value[index] + } + set { + if let newValue = newValue { + array.value[index] = newValue + } + } + } + + public func predecessor(at distance: DistanceType) -> ArrayIterator? { + var i: ArrayIterator? + for _ in 0.. Bool { + // FIXME: Actually implement this + return false + } + + public static func ==(lhs: ArrayIterator, rhs: ArrayIterator) -> Bool { + return lhs.index == rhs.index && + lhs.array.value[lhs.index] == rhs.array.value[rhs.index] + } + + public func advanced(by steps: Int) -> ArrayIterator? { + return ArrayIterator(array, index + steps) + } + + public var source: T? { + return array.value[index] + } +} diff --git a/EOP/Types/Tuples.swift b/EOP/Types/Tuples.swift index 0f10281..bcd974a 100644 --- a/EOP/Types/Tuples.swift +++ b/EOP/Types/Tuples.swift @@ -24,6 +24,22 @@ public struct Pair: Regular { } } +public class PairRef: Regular { + public var value: Pair + + public init(m0: T0, m1: T1) { + self.value = Pair(m0: m0, m1: m1) + } + + public static func == (x: PairRef, y: PairRef) -> Bool { + return x.value == y.value + } + + public static func < (x: PairRef, y: PairRef) -> Bool { + return x.value < y.value + } +} + // type triple (see Exercise 12.2 of Elements of Programming) // model Regular(triple) diff --git a/ElementsOfProgramming.xcodeproj/project.pbxproj b/ElementsOfProgramming.xcodeproj/project.pbxproj index 33f10f9..c43adbb 100644 --- a/ElementsOfProgramming.xcodeproj/project.pbxproj +++ b/ElementsOfProgramming.xcodeproj/project.pbxproj @@ -9,6 +9,7 @@ /* Begin PBXBuildFile section */ C60C392F1F1EE64200243F20 /* Chapter09.swift in Sources */ = {isa = PBXBuildFile; fileRef = C60C392E1F1EE64200243F20 /* Chapter09.swift */; }; C62843881F43CF8800481E76 /* Chapter04Tests.swift in Sources */ = {isa = PBXBuildFile; fileRef = C65D60B31F1C45BD00F62E3B /* Chapter04Tests.swift */; }; + C628438A1F43D47900481E76 /* ArrayIterator.swift in Sources */ = {isa = PBXBuildFile; fileRef = C62843891F43D47900481E76 /* ArrayIterator.swift */; }; C6459EC81F26F65700F0E894 /* Chapter12.swift in Sources */ = {isa = PBXBuildFile; fileRef = C6459EC71F26F65700F0E894 /* Chapter12.swift */; }; C65D60B11F1B275000F62E3B /* Chapter04.swift in Sources */ = {isa = PBXBuildFile; fileRef = C65D60B01F1B275000F62E3B /* Chapter04.swift */; }; C66BEB321F23F65A00BDECD8 /* Chapter07.swift in Sources */ = {isa = PBXBuildFile; fileRef = C66BEB311F23F65A00BDECD8 /* Chapter07.swift */; }; @@ -117,6 +118,7 @@ C61124C51F184B74005B18ED /* ElementsOfProgrammingTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = ElementsOfProgrammingTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; }; C61124C71F184B74005B18ED /* Chapter02Tests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Chapter02Tests.swift; sourceTree = ""; }; C61124C91F184B74005B18ED /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; + C62843891F43D47900481E76 /* ArrayIterator.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ArrayIterator.swift; sourceTree = ""; }; C6459EC71F26F65700F0E894 /* Chapter12.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Chapter12.swift; sourceTree = ""; }; C65D60B01F1B275000F62E3B /* Chapter04.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Chapter04.swift; sourceTree = ""; }; C65D60B31F1C45BD00F62E3B /* Chapter04Tests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Chapter04Tests.swift; sourceTree = ""; }; @@ -244,6 +246,7 @@ C6B297A11F2060E2000E53B6 /* Types */ = { isa = PBXGroup; children = ( + C62843891F43D47900481E76 /* ArrayIterator.swift */, C6B297A21F2060E2000E53B6 /* Double+Additions.swift */, C6ED467F1F3D2AB300C3EF18 /* EOPError.swift */, C6B297A31F2060E2000E53B6 /* Float+Extensions.swift */, @@ -521,6 +524,7 @@ C6F549931F2AE2BF0057BC02 /* Tuples.swift in Sources */, C6CC1AAF1F2ADB1900E468B3 /* String.swift in Sources */, C6CC1AA71F2ADB1900E468B3 /* Float+Extensions.swift in Sources */, + C628438A1F43D47900481E76 /* ArrayIterator.swift in Sources */, C6F646351F43B60C00D902EA /* IntegerSpecialCaseProcedures.swift in Sources */, C6CC1AA81F2ADB1900E468B3 /* Int32+Extensions.swift in Sources */, C6CC1AAA1F2ADB1900E468B3 /* Int+Extensions.swift in Sources */, diff --git a/ElementsOfProgramming.xcodeproj/xcshareddata/xcschemes/ElementsOfProgrammingTests.xcscheme b/ElementsOfProgramming.xcodeproj/xcshareddata/xcschemes/ElementsOfProgrammingTests.xcscheme index 0c210e2..9a19e62 100644 --- a/ElementsOfProgramming.xcodeproj/xcshareddata/xcschemes/ElementsOfProgrammingTests.xcscheme +++ b/ElementsOfProgramming.xcodeproj/xcshareddata/xcschemes/ElementsOfProgrammingTests.xcscheme @@ -7,7 +7,7 @@ buildImplicitDependencies = "YES"> (x: PairRef) -> T { +// return x.value.m0 +// } + func keyOrdering( f: @escaping UnaryFunction, r: @escaping Relation) @@ -216,23 +220,27 @@ class Chapter04Tests: XCTestCase { return p0.m0 < p1.m0 } - // FIXME: Fix these methods -// func algorithmSelect_1_4() { -// typealias T = Pair -// let t = pointer(T(m0: 1, m1: 1), T(m0: 2, m1: 2), T(m0: 2, m1: 3), T(m0: 3, m1: 4)) -// let l = t.advanced(by: 4) -// let ls: Relation> = lessSecond -// repeat { -// let fst: UnaryFunction, Int> = first -// let ls: Relation = less -// let ko = keyOrdering(f: fst, r: ls) -// let r = select_1_4(a: t[0], b: t[1], c: t[2], d: t[3], r: ko) -// let eqf: UnaryPredicate> = eqFirst(x: 2) -// let f = findIf(f: t, l: l, p: eqf) -// XCTAssert(f != l && source(f) == r) -// } while nextPermutation(f: t, l: l, r: ls) -// } -// + func algorithmSelect_1_4() { + let pairs = [Pair(m0: 1, m1: 1), + Pair(m0: 2, m1: 2), + Pair(m0: 2, m1: 3), + Pair(m0: 3, m1: 4)] + let t = ArrayIterator(pairs) + let l = t.advanced(by: 4)! + repeat { + let fst: UnaryFunction, Int> = first + let ko = keyOrdering(f: fst, r: less) + let r = select_1_4(ia: 0, ib: 1, ic: 2, id: 3, + a: pairs[0], b: pairs[1], + c: pairs[2], d: pairs[3], + r: ko) + let eqf: UnaryPredicate> = eqFirst(x: 2) + let f = findIf(f: t, l: l, p: eqf)! + XCTAssert(f != l && f.source! == r) + } while nextPermutation(f: t, l: l, r: lessSecond) + } + + // FIXME: Fix this // func algorithmSelect_1_4_stabilityIndices() { // typealias T = Pair // let t = pointer(T(m0: 1, m1: 1), T(m0: 2, m1: 2), T(m0: 2, m1: 3), T(m0: 3, m1: 4)) @@ -394,30 +402,33 @@ class Chapter04Tests: XCTestCase { // } while nextPermutation(f: p, l: p.advanced(by: 1), r: ls) // } - // FIXME: Fix this method -// func nextPermutation(f: Pointer, l: Pointer, r: Relation) -> Bool { -// // Precondition: weak_ordering(r) -// if (f == l || f.successor() == l) { return false } -// var i = l.iteratorPredecessor! -// -// while true { -// let ii = i -// i = i.predecessor() -// if r(i.source!, ii.source!) { -// var j = l -// repeat { -// j = j.predecessor() -// } while !r(i.source!, j.source!) -// exchangeValues(x: i, y: j) -// reverseBidirectional(f: ii, l: l) -// return true -// } -// if i == f { -// reverseBidirectional(f: f, l: l) -// return false -// } -// } -// } + func nextPermutation( + f: I, + l: I, + r: Relation + ) -> Bool { + // Precondition: weak_ordering(r) + guard !(f == l || f.iteratorSuccessor! == l) else { return false } + var i = l.iteratorPredecessor! + + while true { + let ii = i + i = i.iteratorPredecessor! + if r(i.source!, ii.source!) { + var j = l + repeat { + j = j.iteratorPredecessor! + } while !r(i.source!, j.source!) + exchangeValues(x: i, y: j) + try! reverseBidirectional(f: ii, l: l) + return true + } + if i == f { + try! reverseBidirectional(f: f, l: l) + return false + } + } + } func eqFirst(x: T0) -> UnaryPredicate> { return { p in @@ -428,4 +439,8 @@ class Chapter04Tests: XCTestCase { func lessSecond(p0: Pair, p1: Pair) -> Bool { return p0.m1 < p1.m1 } + +// func lessSecond(p0: PairRef, p1: PairRef) -> Bool { +// return p0.value.m1 < p1.value.m1 +// } }