-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathNBKStaticBigInt.swift
136 lines (107 loc) · 4.63 KB
/
NBKStaticBigInt.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
//=----------------------------------------------------------------------------=
// This source file is part of the Numberick open source project.
//
// Copyright (c) 2023 Oscar Byström Ericsson
// Licensed under Apache License, Version 2.0
//
// See http://www.apache.org/licenses/LICENSE-2.0 for license information.
//=----------------------------------------------------------------------------=
//*============================================================================*
// MARK: * NBK x Static Big Int
//*============================================================================*
#if SBI && swift(>=5.8)
/// It's like Swift.StaticBigInt, but a random access collection.
@frozen public struct NBKStaticBigInt: ExpressibleByIntegerLiteral, RandomAccessCollection, Sendable {
//=------------------------------------------------------------------------=
// MARK: State
//=------------------------------------------------------------------------=
@usableFromInline var base: StaticBigInt
//=------------------------------------------------------------------------=
// MARK: Initializers
//=------------------------------------------------------------------------=
@inlinable public init(_ other: StaticBigInt) {
self.base = other
}
@inlinable public init(integerLiteral other: StaticBigInt) {
self.init(other)
}
//=------------------------------------------------------------------------=
// MARK: Accessors
//=------------------------------------------------------------------------=
/// A three-way comparison against zero.
///
/// ```
/// ┌────── → ────────┐
/// │ self │ signum │
/// ├────── → ────────┤
/// │ -2 │ Int(-1) │ - less
/// │ 0 │ Int( 0) │ - same
/// │ 2 │ Int( 1) │ - more
/// └────── → ────────┘
/// ```
///
@inlinable public func signum() -> Int {
self.base.signum()
}
/// The number of bits in this value's binary representation.
@inlinable public var bitWidth: Int {
self.base.bitWidth
}
/// Accesses the word at the given index, from least significant to most.
@inlinable public subscript(index: Int) -> UInt {
self.base[index]
}
}
//=----------------------------------------------------------------------------=
// MARK: + Collection
//=----------------------------------------------------------------------------=
extension NBKStaticBigInt {
//=------------------------------------------------------------------------=
// MARK: Accessors
//=------------------------------------------------------------------------=
@inlinable public var count: Int {
let width = NBK.ZeroOrMore(unchecked: self.bitWidth)
let major = NBK.PBI .quotient(dividing: width, by: NBK.PowerOf2(bitWidth: UInt.self))
let minor = NBK.PBI.remainder(dividing: width, by: NBK.PowerOf2(bitWidth: UInt.self))
return major &+ Int(bit: minor.isMoreThanZero) as Int
}
@inlinable public var startIndex: Int {
0 as Int
}
@inlinable public var endIndex: Int {
self.count
}
@inlinable public var indices: Range<Int> {
0 as Int ..< self.count
}
//=------------------------------------------------------------------------=
// MARK: Utilities
//=------------------------------------------------------------------------=
@inlinable public func distance(from start: Int, to end: Int) -> Int {
end - start
}
@inlinable public func index(after index: Int) -> Int {
index + 1 as Int
}
@inlinable public func index(before index: Int) -> Int {
index - 1 as Int
}
@inlinable public func index(_ index: Int, offsetBy distance: Int) -> Int {
index + distance
}
@inlinable public func index(_ index: Int, offsetBy distance: Int, limitedBy limit: Int) -> Int? {
NBK.arrayIndex(index, offsetBy: distance, limitedBy: limit)
}
}
//*============================================================================*
// MARK: * NBK x Static Big Int x Swift
//*============================================================================*
extension Swift.StaticBigInt {
//=------------------------------------------------------------------------=
// MARK: Initializers
//=------------------------------------------------------------------------=
@inlinable public init(_ other: NBKStaticBigInt) {
self = other.base
}
}
#endif