-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
282 additions
and
256 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
51 changes: 51 additions & 0 deletions
51
Sources/NBKCoreKit/Private/NBKStrictSignedInteger+Comparisons.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
//=----------------------------------------------------------------------------= | ||
// This source file is part of the Numberick open source project. | ||
// | ||
// Copyright (c) 2023 Oscar Byström Ericsson | ||
// Licensed under Apache License, Version 2.0 | ||
// | ||
// See http://www.apache.org/licenses/LICENSE-2.0 for license information. | ||
//=----------------------------------------------------------------------------= | ||
|
||
//*============================================================================* | ||
// MARK: * NBK x Strict Signed Integer x Comparisons | ||
//*============================================================================* | ||
//=----------------------------------------------------------------------------= | ||
// MARK: + where Base is Unsafe Buffer Pointer | ||
//=----------------------------------------------------------------------------= | ||
|
||
extension NBK.StrictSignedInteger { | ||
|
||
//=------------------------------------------------------------------------= | ||
// MARK: Utilities | ||
//=------------------------------------------------------------------------= | ||
|
||
/// A three-way comparison of `lhs` against `rhs`. | ||
/// | ||
/// ### Development | ||
/// | ||
/// Specializing this it where `T == UInt` makes it faster. | ||
/// | ||
@inlinable public static func compare<T>(_ lhs: Base, to rhs: Base) -> Int where Base == UnsafeBufferPointer<T> { | ||
let lhs = NBK.SuccinctInt(fromStrictSignedInteger: lhs)! | ||
let rhs = NBK.SuccinctInt(fromStrictSignedInteger: rhs)! | ||
return lhs.compared(to: rhs) as Int | ||
} | ||
|
||
/// A three-way comparison of `lhs` against `rhs` at `index`. | ||
/// | ||
/// ### Development | ||
/// | ||
/// Specializing this it where `T == UInt` makes it faster. | ||
/// | ||
@inlinable public static func compare<T>(_ lhs: Base, to rhs: Base, at index: Int) -> Int where Base == UnsafeBufferPointer<T> { | ||
let lhs = NBK.SuccinctInt(fromStrictSignedInteger: lhs)! | ||
let rhs = NBK.SuccinctInt(fromStrictSignedInteger: rhs)! | ||
let partition = Swift.min(index, lhs.body.endIndex) | ||
let suffix = Base(rebasing: lhs.body.suffix(from: partition)) | ||
let comparison = NBK.SuccinctInt(unchecked: suffix, sign: lhs.sign).compared(to: rhs) | ||
if !comparison.isZero { return comparison } | ||
let prefix = Base(rebasing: lhs.body.prefix(upTo: partition)) | ||
return Int(bit: partition == index ? !prefix.allSatisfy({ $0.isZero }) : lhs.sign) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
133 changes: 0 additions & 133 deletions
133
Tests/NBKCoreKitTests/Private/NBK+Limbs+Comparisons.swift
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.