-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathANKFullWidth+Comparisons.swift
77 lines (61 loc) · 2.66 KB
/
ANKFullWidth+Comparisons.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
//=----------------------------------------------------------------------------=
// This source file is part of the AwesomeNumbersKit open source project.
//
// Copyright (c) 2022 Oscar Byström Ericsson
// Licensed under Apache License, Version 2.0
//
// See http://www.apache.org/licenses/LICENSE-2.0 for license information.
//=----------------------------------------------------------------------------=
import ANKCoreKit
//*============================================================================*
// MARK: * ANK x Full Width x Comparisons
//*============================================================================*
extension ANKFullWidth {
//=------------------------------------------------------------------------=
// MARK: Accessors
//=------------------------------------------------------------------------=
@inlinable public var isZero: Bool {
self.low.isZero && self.high.isZero
}
@_transparent public var isLessThanZero: Bool {
self.high.isLessThanZero
}
@inlinable public var isMoreThanZero: Bool {
!(self.isLessThanZero || self.isZero)
}
@inlinable public func signum() -> Int {
self.isLessThanZero ? -1 : self.isZero ? 0 : 1
}
//=------------------------------------------------------------------------=
// MARK: Utilities
//=------------------------------------------------------------------------=
@inlinable public func hash(into hasher: inout Hasher) {
hasher.combine(self.low )
hasher.combine(self.high)
}
//=------------------------------------------------------------------------=
// MARK: Utilities
//=------------------------------------------------------------------------=
@inlinable public static func ==(lhs: Self, rhs: Self) -> Bool {
lhs.low == rhs.low && lhs.high == rhs.high
}
@inlinable public static func <(lhs: Self, rhs: Self) -> Bool {
lhs.compared(to: rhs) == -1
}
@inlinable public func compared(to other: Self) -> Int {
self .withUnsafeWords { this in
other.withUnsafeWords { other in
backwards: do {
let lhsWord: Digit = this .tail
let rhsWord: Digit = other.tail
if lhsWord != rhsWord { return lhsWord < rhsWord ? -1 : 1 }
}
backwards: for index in this.indices.dropLast().reversed() {
let lhsWord: UInt = this [index]
let rhsWord: UInt = other[index]
if lhsWord != rhsWord { return lhsWord < rhsWord ? -1 : 1 }
}
return Int.zero
}}
}
}