-
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.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
8 changed files
with
226 additions
and
235 deletions.
There are no files selected for viewing
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
71 changes: 0 additions & 71 deletions
71
Sources/NBKCoreKit/Private/NBKSuccinctBinaryInteger+Comparisons.swift
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
64 changes: 64 additions & 0 deletions
64
Sources/NBKCoreKit/Private/NBKSuccinctInt+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,64 @@ | ||
//=----------------------------------------------------------------------------= | ||
// 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 Succinct Int x Comparisons | ||
//*============================================================================* | ||
|
||
extension NBK.SuccinctInt { | ||
|
||
//=------------------------------------------------------------------------= | ||
// MARK: Utilities | ||
//=------------------------------------------------------------------------= | ||
|
||
/// A three-way comparison of `self` against `other`. | ||
@inlinable public func compared(to rhs: Self) -> Int { | ||
//=--------------------------------------= | ||
// Plus & Minus | ||
//=--------------------------------------= | ||
if self.sign != rhs.sign { | ||
return self.sign ? -1 : 1 | ||
} | ||
//=---------------------------------------= | ||
return self.compared(toSameSign: rhs) | ||
} | ||
|
||
/// A three-way comparison of `self` against `other`. | ||
@inlinable public func compared(toSameSign other: Self) -> Int { | ||
//=--------------------------------------= | ||
Swift.assert(self.sign == other.sign) | ||
//=--------------------------------------= | ||
// Long & Short | ||
//=--------------------------------------= | ||
if self.body.count != other.body.count { | ||
return self.sign == (self.body.count > other.body.count) ? -1 : 1 | ||
} | ||
//=--------------------------------------= | ||
return self.compared(toSameSizeSameSign: other) | ||
} | ||
|
||
/// A three-way comparison of `self` against `other`. | ||
@inlinable public func compared(toSameSizeSameSign other: Self) -> Int { | ||
//=--------------------------------------= | ||
Swift.assert(self.sign/*--*/ == other.sign/*--*/) | ||
Swift.assert(self.body.count == other.body.count) | ||
//=--------------------------------------= | ||
// Word By Word, Back To Front | ||
//=--------------------------------------= | ||
for index in self.body.indices.reversed() { | ||
let lhsWord = self .body[index] as Base.Element | ||
let rhsWord = other.body[index] as Base.Element | ||
if lhsWord != rhsWord { return lhsWord < rhsWord ? -1 : 1 } | ||
} | ||
//=--------------------------------------= | ||
// Same | ||
//=--------------------------------------= | ||
return 0 as Int as Int as Int as Int as Int | ||
} | ||
} |
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
Oops, something went wrong.