From 0029086d7865b3dfd993a107b5629552699f31b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oscar=20Bystr=C3=B6m=20Ericsson?= Date: Mon, 21 Oct 2024 02:42:26 +0200 Subject: [PATCH] Add generic comparison to DataInt (#92). I'll rework this in (#104). But, it is useful until then. --- .../CoreKit/BinaryInteger+Comparison.swift | 9 +--- .../CoreKit/Models/DataInt+Comparison.swift | 44 +++++++++++++++++- .../Proposals/DataInt+Comparison.swift | 46 ------------------- 3 files changed, 44 insertions(+), 55 deletions(-) delete mode 100644 Sources/TestKit2/Proposals/DataInt+Comparison.swift diff --git a/Sources/CoreKit/BinaryInteger+Comparison.swift b/Sources/CoreKit/BinaryInteger+Comparison.swift index 090a5437..6588ad89 100644 --- a/Sources/CoreKit/BinaryInteger+Comparison.swift +++ b/Sources/CoreKit/BinaryInteger+Comparison.swift @@ -199,15 +199,8 @@ extension Namespace { } } - } else if LHS.Element.Magnitude.size <= RHS.Element.Magnitude.size { - lhs.withUnsafeBinaryIntegerElements { lhs in - rhs.withUnsafeBinaryIntegerElements(as: LHS.Element.Magnitude.self) { rhs in - comparator.resolve(DataInt.compare(lhs: lhs, mode: LHS.mode, rhs: rhs, mode: RHS.mode)) - } - } - } else { - lhs.withUnsafeBinaryIntegerElements(as: RHS.Element.Magnitude.self) { lhs in + lhs.withUnsafeBinaryIntegerElements { lhs in rhs.withUnsafeBinaryIntegerElements { rhs in comparator.resolve(DataInt.compare(lhs: lhs, mode: LHS.mode, rhs: rhs, mode: RHS.mode)) } diff --git a/Sources/CoreKit/Models/DataInt+Comparison.swift b/Sources/CoreKit/Models/DataInt+Comparison.swift index 0fc1c617..3428525b 100644 --- a/Sources/CoreKit/Models/DataInt+Comparison.swift +++ b/Sources/CoreKit/Models/DataInt+Comparison.swift @@ -40,9 +40,14 @@ extension DataInt { //=------------------------------------------------------------------------= // MARK: Utilities //=------------------------------------------------------------------------= - + /// Performs a three-way comparson of `instance` versus `zero` where the mode /// of the `instance` is determined by `isSigned`. + /// + /// ### Development + /// + /// - TODO: Consider adding a static or dynamic mode to this model (#104). + /// @inline(never) @inlinable public static func signum( of instance: Self, mode signedness: Signedness ) -> Signum { @@ -60,6 +65,11 @@ extension DataInt { /// Performs a three-way comparson of `lhs` versus `rhs` where the mode /// of each instance is determined by `lhsSignedness` and `rhsSignedness`. + /// + /// ### Development + /// + /// - TODO: Consider adding a static or dynamic mode to this model (#104). + /// @inline(never) @inlinable public static func compare( lhs: consuming Self, mode lhsSignedness: Signedness, rhs: consuming Self, mode rhsSignedness: Signedness @@ -120,6 +130,38 @@ extension DataInt { //=--------------------------------------= return Signum.zero // as Signum as Signum } + + /// Performs a three-way comparson of `lhs` versus `rhs` where the mode + /// of each instance is determined by `lhsSignedness` and `rhsSignedness`. + /// + /// ### Development + /// + /// - TODO: Consider adding a static or dynamic mode to this model (#104). + /// + @inlinable public static func compare( + lhs: consuming Self, + mode lhsSignedness: Signedness, + rhs: consuming DataInt, + mode rhsSignedness: Signedness + ) -> Signum { + + if Self.Element.size <= OtherElement.size { + rhs.reinterpret(as: Self.Element.self) { rhs in + DataInt.compare( + lhs: lhs, mode: lhsSignedness, + rhs: rhs, mode: rhsSignedness + ) + } + + } else { + lhs.reinterpret(as: OtherElement.self) { lhs in + DataInt.compare( + lhs: lhs, mode: lhsSignedness, + rhs: rhs, mode: rhsSignedness + ) + } + } + } } //*============================================================================* diff --git a/Sources/TestKit2/Proposals/DataInt+Comparison.swift b/Sources/TestKit2/Proposals/DataInt+Comparison.swift deleted file mode 100644 index f2020c25..00000000 --- a/Sources/TestKit2/Proposals/DataInt+Comparison.swift +++ /dev/null @@ -1,46 +0,0 @@ -//=----------------------------------------------------------------------------= -// This source file is part of the Ultimathnum 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. -//=----------------------------------------------------------------------------= - -import CoreKit - -//*============================================================================* -// MARK: * Data Int x Comparison -//*============================================================================* - -extension DataInt { - - //=------------------------------------------------------------------------= - // MARK: Utilities - //=------------------------------------------------------------------------= - - /// Performs a three-way comparson of `lhs` versus `rhs` where the mode - /// of each instance is determined by `lhsSignedness` and `rhsSignedness`. - @inlinable public static func compare( - lhs: consuming Self, mode lhsSignedness: Signedness, - rhs: consuming DataInt, mode rhsSignedness: Signedness - ) -> Signum { - - if Self.Element.size <= OtherElement.size { - rhs.reinterpret(as: Self.Element.self) { rhs in - DataInt.compare( - lhs: lhs, mode: lhsSignedness, - rhs: rhs, mode: rhsSignedness - ) - } - - } else { - lhs.reinterpret(as: OtherElement.self) { lhs in - DataInt.compare( - lhs: lhs, mode: lhsSignedness, - rhs: rhs, mode: rhsSignedness - ) - } - } - } -}