Skip to content

Commit

Permalink
Add generic comparison to DataInt (#92).
Browse files Browse the repository at this point in the history
I'll rework this in (#104). But, it is useful until then.
  • Loading branch information
oscbyspro committed Oct 21, 2024
1 parent bb114aa commit 0029086
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 55 deletions.
9 changes: 1 addition & 8 deletions Sources/CoreKit/BinaryInteger+Comparison.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
Expand Down
44 changes: 43 additions & 1 deletion Sources/CoreKit/Models/DataInt+Comparison.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand Down Expand Up @@ -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<OtherElement>(
lhs: consuming Self,
mode lhsSignedness: Signedness,
rhs: consuming DataInt<OtherElement>,
mode rhsSignedness: Signedness
) -> Signum {

if Self.Element.size <= OtherElement.size {
rhs.reinterpret(as: Self.Element.self) { rhs in
DataInt<Self.Element>.compare(
lhs: lhs, mode: lhsSignedness,
rhs: rhs, mode: rhsSignedness
)
}

} else {
lhs.reinterpret(as: OtherElement.self) { lhs in
DataInt<OtherElement>.compare(
lhs: lhs, mode: lhsSignedness,
rhs: rhs, mode: rhsSignedness
)
}
}
}
}

//*============================================================================*
Expand Down
46 changes: 0 additions & 46 deletions Sources/TestKit2/Proposals/DataInt+Comparison.swift

This file was deleted.

0 comments on commit 0029086

Please sign in to comment.