杨锴
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
//
//  Strideable.swift
//  CS.BigInt
//
//  Created by Károly Lőrentey on 2017-08-11.
//  Copyright © 2016-2017 Károly Lőrentey.
//
 
extension CS.BigUInt: Strideable {
    /// A type that can represent the distance between two values ofa `BigUInt`.
    public typealias Stride = CS.BigInt
 
    /// Adds `n` to `self` and returns the result. Traps if the result would be less than zero.
    public func advanced(by n: CS.BigInt) -> CS.BigUInt {
        return n.sign == .minus ? self - n.magnitude : self + n.magnitude
    }
 
    /// Returns the (potentially negative) difference between `self` and `other` as a `BigInt`. Never traps.
    public func distance(to other: CS.BigUInt) -> CS.BigInt {
        return CS.BigInt(other) - CS.BigInt(self)
    }
}
 
extension CS.BigInt: Strideable {
    public typealias Stride = CS.BigInt
 
    /// Returns `self + n`.
    public func advanced(by n: Stride) -> CS.BigInt {
        return self + n
    }
 
    /// Returns `other - self`.
    public func distance(to other: CS.BigInt) -> Stride {
        return other - self
    }
}