Skip to content

Commit

Permalink
Perform the DataInt/isZero comparison backwards (#26).
Browse files Browse the repository at this point in the history
This changes makes DataInt/isZero an O(1) operation for normalized instances because the last element of a normalized instance must not be zero when the appendix is zero.
  • Loading branch information
oscbyspro committed Jun 22, 2024
1 parent 73e6f22 commit 3b98c93
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions Sources/CoreKit/Models/DataInt+Comparison.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@ extension DataInt {
//=------------------------------------------------------------------------=

/// Indicates whether this value is equal to zero.
///
/// - Note: This comparison is performed backwards.
///
@inlinable public var isZero: Bool {
self.body.isZero && !Bool(self.appendix)
!Bool(self.appendix) && self.body.isZero
}

/// Indicates whether the `body` is free of `appendix` extensions.
Expand Down Expand Up @@ -119,8 +122,11 @@ extension DataInt.Body {
//=------------------------------------------------------------------------=

/// Indicates whether this value is equal to zero.
///
/// - Note: This comparison is performed backwards.
///
@inlinable public var isZero: Bool {
self.buffer().allSatisfy({ $0.isZero })
self.buffer().reversed().allSatisfy({ $0.isZero })
}

/// Indicates whether the `body` is free of `appendix` extensions.
Expand Down

0 comments on commit 3b98c93

Please sign in to comment.