From cbabf10b673c48590bcb96152ae26f167a0ff6f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oscar=20Bystr=C3=B6m=20Ericsson?= Date: Sat, 28 Oct 2023 10:43:33 +0200 Subject: [PATCH] [NBKCoreKit] Cleanup. --- ...rBinaryInteger+GreatestCommonDivisor.swift | 43 +++++++++---------- ...rBinaryInteger+GreatestCommonDivisor.swift | 26 +++++------ 2 files changed, 33 insertions(+), 36 deletions(-) diff --git a/Sources/NBKCoreKit/Private/NBKProperBinaryInteger+GreatestCommonDivisor.swift b/Sources/NBKCoreKit/Private/NBKProperBinaryInteger+GreatestCommonDivisor.swift index aea5b57f..50917d8e 100644 --- a/Sources/NBKCoreKit/Private/NBKProperBinaryInteger+GreatestCommonDivisor.swift +++ b/Sources/NBKCoreKit/Private/NBKProperBinaryInteger+GreatestCommonDivisor.swift @@ -78,8 +78,8 @@ extension NBK.ProperBinaryInteger where Integer: NBKSignedInteger { let unsigned = Magnitude.greatestCommonDivisorByExtendedEuclideanAlgorithm(of: lhs.magnitude, and: rhs.magnitude) //=--------------------------------------= return (result: unsigned.result, - lhsCoefficient: Integer(sign: lhsIsLessThanZero != unsigned.isOddLoopCount ? .minus : .plus, magnitude: unsigned.lhsCoefficient)!, - rhsCoefficient: Integer(sign: rhsIsLessThanZero == unsigned.isOddLoopCount ? .minus : .plus, magnitude: unsigned.rhsCoefficient)!, + lhsCoefficient: Integer(sign: lhsIsLessThanZero != unsigned.iterationIsOdd ? .minus : .plus, magnitude: unsigned.lhsCoefficient)!, + rhsCoefficient: Integer(sign: rhsIsLessThanZero == unsigned.iterationIsOdd ? .minus : .plus, magnitude: unsigned.rhsCoefficient)!, lhsQuotient: Integer(sign: lhsIsLessThanZero /*----------------------*/ ? .minus : .plus, magnitude: unsigned.lhsQuotient )!, rhsQuotient: Integer(sign: rhsIsLessThanZero /*----------------------*/ ? .minus : .plus, magnitude: unsigned.rhsQuotient )!) } @@ -144,14 +144,11 @@ extension NBK.ProperBinaryInteger where Integer: NBKUnsignedInteger { /// ### Bézout's identity (unsigned) /// /// ```swift - /// var x = lhs &* lhsCoefficient - /// var y = rhs &* rhsCoefficient - /// - /// if isOddLoopCount { - /// swap(&x, &y) + /// if !iterationIsOdd { + /// precondition(result == (lhs &* lhsCoefficient &- rhs &* rhsCoefficient)) + /// } else { + /// precondition(result == (rhs &* rhsCoefficient &- lhs &* lhsCoefficient)) /// } - /// - /// precondition(result == x &- y) /// ``` /// /// ### Quotients of dividing by GCD @@ -165,28 +162,28 @@ extension NBK.ProperBinaryInteger where Integer: NBKUnsignedInteger { /// ### Loop count result /// /// ```swift - /// let lhsCoefficientSign = lhs.isLessThanZero != isOddLoopCount - /// let rhsCoefficientSign = rhs.isLessThanZero == isOddLoopCount + /// let lhsCoefficientSign = lhs.isLessThanZero != iterationIsOdd + /// let rhsCoefficientSign = rhs.isLessThanZero == iterationIsOdd /// ``` /// @inlinable public static func greatestCommonDivisorByExtendedEuclideanAlgorithm(of lhs: Integer, and rhs: Integer) - -> (result: Integer, lhsCoefficient: Integer, rhsCoefficient: Integer, lhsQuotient: Integer, rhsQuotient: Integer, isOddLoopCount: Bool) { + -> (result: Integer, lhsCoefficient: Integer, rhsCoefficient: Integer, lhsQuotient: Integer, rhsQuotient: Integer, iterationIsOdd: Bool) { //=--------------------------------------= - var (r0, r1) = (lhs, rhs) as (Integer,Integer) - var (s0, s1) = (001, 000) as (Integer,Integer) - var (t0, t1) = (000, 001) as (Integer,Integer) - var isOddLoopCount = ((((((((((false)))))))))) + var (a, b) = (lhs, rhs) as (Integer,Integer) + var (c, d) = (001, 000) as (Integer,Integer) + var (e, f) = (000, 001) as (Integer,Integer) + var iterationIsOdd = (((((((((false))))))))) //=--------------------------------------= - while !r1.isZero { + while !b.isZero { - isOddLoopCount.toggle() - let division = r0.quotientAndRemainder(dividingBy: r1) + iterationIsOdd.toggle() + let division = a.quotientAndRemainder(dividingBy: b) - (r0, r1) = (r1, division.remainder) - (s0, s1) = (s1, division.quotient * s1 + s0) - (t0, t1) = (t1, division.quotient * t1 + t0) + (a, b) = (b, division.remainder) + (c, d) = (d, division.quotient * d + c) + (e, f) = (f, division.quotient * f + e) } //=--------------------------------------= - return (result: r0, lhsCoefficient: s0, rhsCoefficient: t0, lhsQuotient: t1, rhsQuotient: s1, isOddLoopCount: isOddLoopCount) + return (result: a, lhsCoefficient: c, rhsCoefficient: e, lhsQuotient: f, rhsQuotient: d, iterationIsOdd: iterationIsOdd) } } diff --git a/Tests/NBKCoreKitTests/Private/NBKProperBinaryInteger+GreatestCommonDivisor.swift b/Tests/NBKCoreKitTests/Private/NBKProperBinaryInteger+GreatestCommonDivisor.swift index 36d083da..2ec290ff 100644 --- a/Tests/NBKCoreKitTests/Private/NBKProperBinaryInteger+GreatestCommonDivisor.swift +++ b/Tests/NBKCoreKitTests/Private/NBKProperBinaryInteger+GreatestCommonDivisor.swift @@ -120,8 +120,7 @@ final class NBKProperBinaryIntegerTestsOnGreatestCommonDivisorByExtendedEuclidea for lhs in Int8.min ... Int8.max { for rhs in Int8.min ... Int8.max { let extended = NBK.PBI.greatestCommonDivisorByExtendedEuclideanAlgorithm(of: lhs, and: rhs) - let identity = lhs &* extended.lhsCoefficient &+ rhs &* extended.rhsCoefficient - XCTAssertEqual(identity, Int8(bitPattern: extended.result)) + XCTAssertEqual(Int8(bitPattern: extended.result), lhs &* extended.lhsCoefficient &+ rhs &* extended.rhsCoefficient) } } } @@ -131,10 +130,11 @@ final class NBKProperBinaryIntegerTestsOnGreatestCommonDivisorByExtendedEuclidea for lhs in UInt8.min ... UInt8.max { for rhs in UInt8.min ... UInt8.max { let extended = NBK.PBI.greatestCommonDivisorByExtendedEuclideanAlgorithm(of: lhs, and: rhs) - let x = lhs &* extended.lhsCoefficient - let y = rhs &* extended.rhsCoefficient - let identity = extended.isOddLoopCount ? y &- x : x &- y - XCTAssertEqual(identity, extended.result) + if !extended.iterationIsOdd { + XCTAssertEqual(extended.result, lhs &* extended.lhsCoefficient &- rhs &* extended.rhsCoefficient) + } else { + XCTAssertEqual(extended.result, rhs &* extended.rhsCoefficient &- lhs &* extended.lhsCoefficient) + } } } } @@ -180,7 +180,7 @@ _ lhs: T, _ rhs: T, _ result: T.Magnitude, file: StaticString = #file, line: UInt = #line) { func with(_ lhs: T, _ rhs: T) { - NBKAssertGreatestCommonDivisorByBinaryAlgorithm(lhs, rhs, result, false, file: file, line: line) + NBKAssertGreatestCommonDivisorByBinaryAlgorithm(lhs, rhs, result, false, file: file, line: line) NBKAssertGreatestCommonDivisorByExtendedEuclideanAlgorithmAsUnsigned(lhs, rhs, result, file: file, line: line) } @@ -227,8 +227,8 @@ file: StaticString = #file, line: UInt = #line) { XCTAssertEqual(1, extended.lhsQuotient, file: file, line: line) XCTAssertEqual(0, extended.rhsQuotient, file: file, line: line) } else { - XCTAssertEqual(extended.lhsQuotient, lhs / T(result), file: file, line: line) - XCTAssertEqual(extended.rhsQuotient, rhs / T(result), file: file, line: line) + XCTAssertEqual(extended.lhsQuotient, lhs / T(result), file: file, line: line) + XCTAssertEqual(extended.rhsQuotient, rhs / T(result), file: file, line: line) } } @@ -236,13 +236,13 @@ private func NBKAssertGreatestCommonDivisorByExtendedEuclideanAlgorithmAsUnsigne _ lhs: T, _ rhs: T, _ result: T.Magnitude, file: StaticString = #file, line: UInt = #line) { //=------------------------------------------= - let extended = NBK.PBI.greatestCommonDivisorByExtendedEuclideanAlgorithm(of: lhs, and: rhs) + let extended = NBK.PBI.greatestCommonDivisorByExtendedEuclideanAlgorithm(of: lhs, and: rhs) //=------------------------------------------= XCTAssertEqual(extended.result, T(result), file: file, line: line) if !result.isZero { - XCTAssertEqual(extended.lhsQuotient, lhs / T(result), file: file, line: line) - XCTAssertEqual(extended.rhsQuotient, rhs / T(result), file: file, line: line) + XCTAssertEqual(extended.lhsQuotient, lhs / T(result), file: file, line: line) + XCTAssertEqual(extended.rhsQuotient, rhs / T(result), file: file, line: line) } else { XCTAssertEqual(0, lhs, file: file, line: line) XCTAssertEqual(0, rhs, file: file, line: line) @@ -253,7 +253,7 @@ file: StaticString = #file, line: UInt = #line) { XCTAssertEqual(0, extended.rhsCoefficient, file: file, line: line) } - if !extended.isOddLoopCount { + if !extended.iterationIsOdd { XCTAssertEqual(result, lhs &* extended.lhsCoefficient &- rhs &* extended.rhsCoefficient, file: file, line: line) } else { XCTAssertEqual(result, rhs &* extended.rhsCoefficient &- lhs &* extended.lhsCoefficient, file: file, line: line)