杨锴
2024-08-14 909e20941e45f8712c012db602034b47da0bfdb0
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
//
//  Comparable.swift
//  CS.BigInt
//
//  Created by Károly Lőrentey on 2016-01-03.
//  Copyright © 2016-2017 Károly Lőrentey.
//
 
import Foundation
 
extension CS.BigUInt: Comparable {
    //MARK: Comparison
    
    /// Compare `a` to `b` and return an `NSComparisonResult` indicating their order.
    ///
    /// - Complexity: O(count)
    public static func compare(_ a: CS.BigUInt, _ b: CS.BigUInt) -> ComparisonResult {
        if a.count != b.count { return a.count > b.count ? .orderedDescending : .orderedAscending }
        for i in (0 ..< a.count).reversed() {
            let ad = a[i]
            let bd = b[i]
            if ad != bd { return ad > bd ? .orderedDescending : .orderedAscending }
        }
        return .orderedSame
    }
 
    /// Return true iff `a` is equal to `b`.
    ///
    /// - Complexity: O(count)
    public static func ==(a: CS.BigUInt, b: CS.BigUInt) -> Bool {
        return CS.BigUInt.compare(a, b) == .orderedSame
    }
 
    /// Return true iff `a` is less than `b`.
    ///
    /// - Complexity: O(count)
    public static func <(a: CS.BigUInt, b: CS.BigUInt) -> Bool {
        return CS.BigUInt.compare(a, b) == .orderedAscending
    }
}
 
extension CS.BigInt {
    /// Return true iff `a` is equal to `b`.
    public static func ==(a: CS.BigInt, b: CS.BigInt) -> Bool {
        return a.sign == b.sign && a.magnitude == b.magnitude
    }
 
    /// Return true iff `a` is less than `b`.
    public static func <(a: CS.BigInt, b: CS.BigInt) -> Bool {
        switch (a.sign, b.sign) {
        case (.plus, .plus):
            return a.magnitude < b.magnitude
        case (.plus, .minus):
            return false
        case (.minus, .plus):
            return true
        case (.minus, .minus):
            return a.magnitude > b.magnitude
        }
    }
}