Skip to content

Commit

Permalink
[NBKCoreKit] Cleanup.
Browse files Browse the repository at this point in the history
  • Loading branch information
oscbyspro committed Oct 26, 2023
1 parent a095025 commit 5c76dfc
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 46 deletions.
23 changes: 15 additions & 8 deletions Sources/NBKCoreKit/Private/NBK+GreatestCommonDivisor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,32 @@ extension NBK {
// MARK: Utilities
//=------------------------------------------------------------------------=

/// Finds the greatest common divisor of `lhs` and `rhs` by using [this binary algorithm][algorithm].
///
/// - TODO: Use `bitShift(...)` methods if/when NBKBinaryInteger...
/// Finds the GCD of `lhs` and `rhs` by using [this binary algorithm][algorithm].
///
/// [algorithm]: https://en.wikipedia.org/wiki/binary_GCD_algorithm
///
@inlinable public static func greatestCommonDivisorByBinaryAlgorithm<T>(
of lhs: T, and rhs: T) -> T.Magnitude where T: NBKBinaryInteger {
self.greatestCommonDivisorByBinaryAlgorithm(of: lhs.magnitude, and: rhs.magnitude)
}

/// Finds the GCD of `lhs` and `rhs` by using [this binary algorithm][algorithm].
///
/// [algorithm]: https://en.wikipedia.org/wiki/binary_GCD_algorithm
///
/// - TODO: Use `bitShift(...)` methods if/when NBKBinaryInteger...
///
@inlinable public static func greatestCommonDivisorByBinaryAlgorithm<T>(
of lhs: T, and rhs: T) -> T where T: NBKUnsignedInteger {
//=--------------------------------------=
var lhs = lhs.magnitude as T.Magnitude
if rhs.isZero { return lhs }
var rhs = rhs.magnitude as T.Magnitude
if lhs.isZero { return rhs }

//=--------------------------------------=
let lhsShift: Int = lhs.trailingZeroBitCount
let rhsShift: Int = rhs.trailingZeroBitCount
//=--------------------------------------=
lhs >>= lhsShift
rhs >>= rhsShift
var lhs: T = lhs >> lhsShift
var rhs: T = rhs >> rhsShift

while lhs != rhs {
if lhs < rhs {
Expand Down
80 changes: 42 additions & 38 deletions Tests/NBKCoreKitTests/Private/NBK+GreatestCommonDivisor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,15 @@ final class NBKTestsOnGreatestCommonDivisor: XCTestCase {
// MARK: Tests
//=------------------------------------------------------------------------=

func testPowersOf2() {
NBKAssertGreatestCommonDivisor( 2, 16, 2)
NBKAssertGreatestCommonDivisor( 4, 16, 4)
NBKAssertGreatestCommonDivisor( 8, 16, 8)
NBKAssertGreatestCommonDivisor(16, 16, 16)
func testSmallPowersOf2() {
NBKAssertGreatestCommonDivisor( 2 as Int, 16 as Int, 2 as UInt)
NBKAssertGreatestCommonDivisor( 4 as Int, 16 as Int, 4 as UInt)
NBKAssertGreatestCommonDivisor( 8 as Int, 16 as Int, 8 as UInt)
NBKAssertGreatestCommonDivisor(16 as Int, 16 as Int, 16 as UInt)
}

func testSmallPrimeProductsRepeated() {
NBKAssertGreatestCommonDivisor( 2 * 3 * 5 as Int, 16 * 81 as Int, 2 * 3 as UInt)
NBKAssertGreatestCommonDivisor( 4 * 9 * 25 as Int, 16 * 81 as Int, 4 * 9 as UInt)
NBKAssertGreatestCommonDivisor( 8 * 27 * 125 as Int, 16 * 81 as Int, 8 * 27 as UInt)
NBKAssertGreatestCommonDivisor(16 * 81 * 625 as Int, 16 * 81 as Int, 16 * 81 as UInt)
}

func testSmallPrimeProductsEvenEven() {
func testSmallPrimeProducts() {
// even x even
NBKAssertGreatestCommonDivisor(2 as Int, 2 * 5 * 11 as Int, 2 as UInt)
NBKAssertGreatestCommonDivisor(2 * 3 as Int, 2 * 5 * 11 as Int, 2 as UInt)
NBKAssertGreatestCommonDivisor(2 * 3 * 5 as Int, 2 * 5 * 11 as Int, 2 * 5 as UInt)
Expand All @@ -46,33 +40,39 @@ final class NBKTestsOnGreatestCommonDivisor: XCTestCase {
NBKAssertGreatestCommonDivisor( 5 * 7 * 11 as Int, 2 * 5 * 11 as Int, 5 * 11 as UInt)
NBKAssertGreatestCommonDivisor( 7 * 11 as Int, 2 * 5 * 11 as Int, 11 as UInt)
NBKAssertGreatestCommonDivisor( 11 as Int, 2 * 5 * 11 as Int, 11 as UInt)
// even x odd
NBKAssertGreatestCommonDivisor(2 as Int, 3 * 5 * 07 as Int, 1 as UInt)
NBKAssertGreatestCommonDivisor(2 * 3 as Int, 3 * 5 * 07 as Int, 3 as UInt)
NBKAssertGreatestCommonDivisor(2 * 3 * 5 as Int, 3 * 5 * 07 as Int, 3 * 5 as UInt)
NBKAssertGreatestCommonDivisor(2 * 3 * 5 * 7 as Int, 3 * 5 * 07 as Int, 3 * 5 * 07 as UInt)
NBKAssertGreatestCommonDivisor(2 * 3 * 5 * 7 * 11 as Int, 3 * 5 * 07 as Int, 3 * 5 * 07 as UInt)
NBKAssertGreatestCommonDivisor( 3 * 5 * 7 * 11 as Int, 3 * 5 * 07 as Int, 3 * 5 * 07 as UInt)
NBKAssertGreatestCommonDivisor( 5 * 7 * 11 as Int, 3 * 5 * 07 as Int, 5 * 07 as UInt)
NBKAssertGreatestCommonDivisor( 7 * 11 as Int, 3 * 5 * 07 as Int, 07 as UInt)
NBKAssertGreatestCommonDivisor( 11 as Int, 3 * 5 * 07 as Int, 01 as UInt)
// odd x even
NBKAssertGreatestCommonDivisor(1 as Int, 2 * 5 * 11 as Int, 1 as UInt)
NBKAssertGreatestCommonDivisor(1 * 3 as Int, 2 * 5 * 11 as Int, 1 as UInt)
NBKAssertGreatestCommonDivisor(1 * 3 * 5 as Int, 2 * 5 * 11 as Int, 1 * 5 as UInt)
NBKAssertGreatestCommonDivisor(1 * 3 * 5 * 7 as Int, 2 * 5 * 11 as Int, 1 * 5 as UInt)
NBKAssertGreatestCommonDivisor(1 * 3 * 5 * 7 * 11 as Int, 2 * 5 * 11 as Int, 1 * 5 * 11 as UInt)
NBKAssertGreatestCommonDivisor( 3 * 5 * 7 * 11 as Int, 2 * 5 * 11 as Int, 5 * 11 as UInt)
NBKAssertGreatestCommonDivisor( 5 * 7 * 11 as Int, 2 * 5 * 11 as Int, 5 * 11 as UInt)
NBKAssertGreatestCommonDivisor( 7 * 11 as Int, 2 * 5 * 11 as Int, 11 as UInt)
NBKAssertGreatestCommonDivisor( 11 as Int, 2 * 5 * 11 as Int, 11 as UInt)
// odd x odd
NBKAssertGreatestCommonDivisor(1 as Int, 3 * 5 * 07 as Int, 1 as UInt)
NBKAssertGreatestCommonDivisor(1 * 3 as Int, 3 * 5 * 07 as Int, 3 as UInt)
NBKAssertGreatestCommonDivisor(1 * 3 * 5 as Int, 3 * 5 * 07 as Int, 3 * 5 as UInt)
NBKAssertGreatestCommonDivisor(1 * 3 * 5 * 7 as Int, 3 * 5 * 07 as Int, 3 * 5 * 07 as UInt)
NBKAssertGreatestCommonDivisor(1 * 3 * 5 * 7 * 11 as Int, 3 * 5 * 07 as Int, 3 * 5 * 07 as UInt)
NBKAssertGreatestCommonDivisor( 3 * 5 * 7 * 11 as Int, 3 * 5 * 07 as Int, 3 * 5 * 07 as UInt)
NBKAssertGreatestCommonDivisor( 5 * 7 * 11 as Int, 3 * 5 * 07 as Int, 5 * 07 as UInt)
NBKAssertGreatestCommonDivisor( 7 * 11 as Int, 3 * 5 * 07 as Int, 07 as UInt)
NBKAssertGreatestCommonDivisor( 11 as Int, 3 * 5 * 07 as Int, 01 as UInt)
}

func testSmallPrimeProductsEvenOdd() {
NBKAssertGreatestCommonDivisor(2 as Int, 3 * 5 * 7 as Int, 1 as UInt)
NBKAssertGreatestCommonDivisor(2 * 3 as Int, 3 * 5 * 7 as Int, 3 as UInt)
NBKAssertGreatestCommonDivisor(2 * 3 * 5 as Int, 3 * 5 * 7 as Int, 3 * 5 as UInt)
NBKAssertGreatestCommonDivisor(2 * 3 * 5 * 7 as Int, 3 * 5 * 7 as Int, 3 * 5 * 7 as UInt)
NBKAssertGreatestCommonDivisor(2 * 3 * 5 * 7 * 11 as Int, 3 * 5 * 7 as Int, 3 * 5 * 7 as UInt)
NBKAssertGreatestCommonDivisor( 3 * 5 * 7 * 11 as Int, 3 * 5 * 7 as Int, 3 * 5 * 7 as UInt)
NBKAssertGreatestCommonDivisor( 5 * 7 * 11 as Int, 3 * 5 * 7 as Int, 5 * 7 as UInt)
NBKAssertGreatestCommonDivisor( 7 * 11 as Int, 3 * 5 * 7 as Int, 7 as UInt)
NBKAssertGreatestCommonDivisor( 11 as Int, 3 * 5 * 7 as Int, 1 as UInt)
}

func testSmallPrimeProductsOddOdd() {
NBKAssertGreatestCommonDivisor(1 as Int, 3 * 5 * 7 as Int, 1 as UInt)
NBKAssertGreatestCommonDivisor(1 * 3 as Int, 3 * 5 * 7 as Int, 3 as UInt)
NBKAssertGreatestCommonDivisor(1 * 3 * 5 as Int, 3 * 5 * 7 as Int, 3 * 5 as UInt)
NBKAssertGreatestCommonDivisor(1 * 3 * 5 * 7 as Int, 3 * 5 * 7 as Int, 3 * 5 * 7 as UInt)
NBKAssertGreatestCommonDivisor(1 * 3 * 5 * 7 * 11 as Int, 3 * 5 * 7 as Int, 3 * 5 * 7 as UInt)
NBKAssertGreatestCommonDivisor( 3 * 5 * 7 * 11 as Int, 3 * 5 * 7 as Int, 3 * 5 * 7 as UInt)
NBKAssertGreatestCommonDivisor( 5 * 7 * 11 as Int, 3 * 5 * 7 as Int, 5 * 7 as UInt)
NBKAssertGreatestCommonDivisor( 7 * 11 as Int, 3 * 5 * 7 as Int, 7 as UInt)
NBKAssertGreatestCommonDivisor( 11 as Int, 3 * 5 * 7 as Int, 1 as UInt)
}

func testEverythingDividesZero() {
func testEveryIntegerDividesZero() {
for other in -10 ... 10 {
NBKAssertGreatestCommonDivisor(0 as Int, other, other.magnitude as UInt)
}
Expand Down Expand Up @@ -103,6 +103,10 @@ file: StaticString = #file, line: UInt = #line) {
//=------------------------------------------=
with(lhs, rhs, gcd)
with(rhs, lhs, gcd)
//=------------------------------------------=
if T.isSigned {
NBKAssertGreatestCommonDivisor(lhs.magnitude, rhs.magnitude, gcd, file: file, line: line)
}
}

#endif

0 comments on commit 5c76dfc

Please sign in to comment.