From 3b98c938537cce35b981468f5d4f138ecdf93a8e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oscar=20Bystr=C3=B6m=20Ericsson?= Date: Sat, 22 Jun 2024 04:27:14 +0200 Subject: [PATCH] Perform the DataInt/isZero comparison backwards (#26). 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. --- Sources/CoreKit/Models/DataInt+Comparison.swift | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/Sources/CoreKit/Models/DataInt+Comparison.swift b/Sources/CoreKit/Models/DataInt+Comparison.swift index 5d5ab02f..6304bbd1 100644 --- a/Sources/CoreKit/Models/DataInt+Comparison.swift +++ b/Sources/CoreKit/Models/DataInt+Comparison.swift @@ -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. @@ -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.