-
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.
[NBKCoreKit] Cleanup. StrictSignMagnitude (#85).
- Loading branch information
Showing
12 changed files
with
357 additions
and
130 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
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
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
34 changes: 34 additions & 0 deletions
34
Sources/NBKCoreKit/Private/NBKStrictSignMagnitude+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,34 @@ | ||
//=----------------------------------------------------------------------------= | ||
// 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 Sign Magnitude x Comparisons x Sub Sequence | ||
//*============================================================================* | ||
|
||
extension NBK.StrictSignMagnitude.SubSequence { | ||
|
||
//=------------------------------------------------------------------------= | ||
// MARK: Utilities | ||
//=------------------------------------------------------------------------= | ||
|
||
/// Returns whether `components` is less than `zero`. | ||
@inlinable public static func isLessThanZero(_ components: Components) -> Bool { | ||
components.sign == Sign.minus && !components.magnitude.allSatisfy({ $0.isZero }) | ||
} | ||
|
||
/// Returns whether `components` is more than `zero`. | ||
@inlinable public static func isMoreThanZero(_ components: Components) -> Bool { | ||
components.sign == Sign.plus && !components.magnitude.allSatisfy({ $0.isZero }) | ||
} | ||
|
||
/// A three-way comparison of `components` against `zero`. | ||
@inlinable public static func signum(_ components: Components) -> Int { | ||
components.magnitude.allSatisfy({ $0.isZero }) ? 0 : components.sign == Sign.plus ? 1 : -1 | ||
} | ||
} |
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,53 @@ | ||
//=----------------------------------------------------------------------------= | ||
// 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 Sign Magnitude | ||
//*============================================================================* | ||
|
||
extension NBK { | ||
|
||
/// A namespace for strict sign-magnitude algorithms. | ||
/// | ||
/// The `base` must be `nonempty` at the start and end of each access. | ||
/// | ||
/// ```swift | ||
/// static func algorithm(_ base: inout Base, input: Input) -> Output | ||
/// ``` | ||
/// | ||
/// ### Development | ||
/// | ||
/// Remaking this as a view when Swift gets view types might be neat. | ||
/// | ||
@frozen public enum StrictSignMagnitude<Base> where Base: RandomAccessCollection, Base.Element: NBKCoreInteger & NBKUnsignedInteger { | ||
|
||
/// The sign and of this type. | ||
public typealias Sign = NBK.Sign | ||
|
||
/// The sign and a magnitude of this type. | ||
public typealias Components = SM<Base> | ||
|
||
//*====================================================================* | ||
// MARK: * Sub Sequence | ||
//*====================================================================* | ||
|
||
/// The sub sequence namespace of this type. | ||
/// | ||
/// The `base` may be `empty` at the start and end of each access. | ||
/// | ||
@frozen public enum SubSequence { | ||
|
||
/// The sign and of this type. | ||
public typealias Sign = NBK.Sign | ||
|
||
/// The sign and a magnitude of this type. | ||
public typealias Components = SM<Base> | ||
} | ||
} | ||
} |
Oops, something went wrong.