-
Notifications
You must be signed in to change notification settings - Fork 3
/
NBKFlexibleWidth.swift
153 lines (122 loc) · 5.89 KB
/
NBKFlexibleWidth.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
//=----------------------------------------------------------------------------=
// 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.
//=----------------------------------------------------------------------------=
import NBKCoreKit
//*============================================================================*
// MARK: * NBK x Flexible Width
//*============================================================================*
/// A signed, flexible-width, binary integer.
///
/// This name reserves a spot for a signed 2's-complement-in-memory integer.
///
/// - Note: You can use `NBKSigned<UIntXL>` until `IntXL` becomes available.
///
@frozen public struct NBKFlexibleWidth {
@usableFromInline typealias Elements = ContiguousArray<UInt>
//*========================================================================*
// MARK: * Magnitude
//*========================================================================*
/// An unsigned, flexible-width, binary integer.
@frozen public struct Magnitude: NBKUnsignedInteger, IntXLOrUIntXL {
public typealias Digit = UInt
public typealias Words = ContiguousArray<UInt> // TODO: make opaque
@usableFromInline typealias Elements = NBKFlexibleWidth.Elements
//=--------------------------------------------------------------------=
// MARK: State
//=--------------------------------------------------------------------=
/// The integer's underlying storage.
///
/// It must be `normal` and `nonempty` at the start and end of each access.
///
@usableFromInline var storage: Storage
//=--------------------------------------------------------------------=
// MARK: Initializers
//=--------------------------------------------------------------------=
@inlinable init(_ storage: Storage) {
self.storage = storage
precondition(self.storage.isNormal)
}
@inlinable init(normalizing storage: Storage) {
self.storage = storage
self.storage.normalize()
}
@inlinable init(unchecked storage: Storage) {
self.storage = storage
Swift.assert(self.storage.isNormal)
}
//*====================================================================*
// MARK: * Storage
//*====================================================================*
/// An unsigned, resizable, collection of at least one word.
///
/// Its operations have fixed-width semantics unless stated otherwise.
///
@frozen @usableFromInline struct Storage {
@usableFromInline typealias Elements = NBKFlexibleWidth.Elements
//=----------------------------------------------------------------=
// MARK: State
//=----------------------------------------------------------------=
/// A collection of unsigned integers.
///
/// It must be `nonempty` at the start and end of each access.
///
@usableFromInline var elements: Elements
//=----------------------------------------------------------------=
// MARK: Initializers
//=----------------------------------------------------------------=
@inlinable init(_ elements: Elements) {
self.elements = elements
precondition(!self.elements.isEmpty)
}
@inlinable init(nonemptying elements: Elements) {
self.elements = elements
if self.elements.isEmpty {
self.elements.append(0)
}
}
@inlinable init(unchecked elements: Elements) {
self.elements = elements
Swift.assert(!self.elements.isEmpty)
}
}
}
}
//=----------------------------------------------------------------------------=
// MARK: + Details
//=----------------------------------------------------------------------------=
extension IntXLOrUIntXL {
//=------------------------------------------------------------------------=
// MARK: Utilities
//=------------------------------------------------------------------------=
/// A `description` of this type.
///
/// ```
/// ┌─────────────────────────── → ────────────┐
/// │ type │ description │
/// ├─────────────────────────── → ────────────┤
/// │ NBKFlexibleWidth │ "IntXL" │
/// │ NBKFlexibleWidth.Magnitude │ "UIntXL" │
/// └─────────────────────────── → ────────────┘
/// ```
///
@inlinable public static var description: String {
Self.isSigned ? "IntXL" : "UIntXL"
}
}
//*============================================================================*
// MARK: * NBK x Flexible Width x Aliases
//*============================================================================*
/// A signed, flexible-width, integer.
///
/// This name reserves a spot for a signed 2's-complement-in-memory integer.
///
/// - Note: You can use `NBKSigned<UIntXL>` until `IntXL` becomes available.
///
public typealias IntXL = NBKFlexibleWidth
/// An unsigned, flexible-width, integer.
public typealias UIntXL = NBKFlexibleWidth.Magnitude