Skip to content

Commit

Permalink
[NBKFlexibleWidthKit] Cleanup.
Browse files Browse the repository at this point in the history
  • Loading branch information
oscbyspro committed Oct 23, 2023
1 parent 58873b0 commit 417832a
Show file tree
Hide file tree
Showing 11 changed files with 42 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ extension NBKFlexibleWidth.Magnitude {
}

//=------------------------------------------------------------------------=
// MARK: Transformations
// MARK: Transformations x Index
//=------------------------------------------------------------------------=

@_disfavoredOverload @inlinable public mutating func add(_ other: UInt, at index: Int) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ extension NBKFlexibleWidth.Magnitude {
}

//=------------------------------------------------------------------------=
// MARK: Transformations
// MARK: Transformations x Index
//=------------------------------------------------------------------------=

@inlinable public mutating func add(_ other: Self, at index: Int) {
Expand Down
6 changes: 3 additions & 3 deletions Sources/NBKFlexibleWidthKit/NBKFlexibleWidth+Division.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import NBKCoreKit
extension NBKFlexibleWidth.Magnitude {

//=------------------------------------------------------------------------=
// MARK: Transformations
// MARK: Transformations x Overflow
//=------------------------------------------------------------------------=

@inlinable public mutating func divideReportingOverflow(by other: Self) -> Bool {
Expand Down Expand Up @@ -47,13 +47,13 @@ extension NBKFlexibleWidth.Magnitude {
}

//=----------------------------------------------------------------------------=
// MARK: + Long Division
// MARK: + Long Division Algorithms
//=----------------------------------------------------------------------------=

extension NBKFlexibleWidth.Magnitude {

//=------------------------------------------------------------------------=
// MARK: Transformations x Private
// MARK: Transformations x Overflow x Private
//=------------------------------------------------------------------------=

/// Performs long division after some fast-path checks.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,19 @@ extension NBKFlexibleWidth.Magnitude {
}

//=------------------------------------------------------------------------=
// MARK: Transformations
// MARK: Transformations x Addition
//=------------------------------------------------------------------------=

@_disfavoredOverload @inlinable public mutating func multiply(by multiplicand: UInt, add addend: UInt) {
@_disfavoredOverload @inlinable public mutating func multiply(by multiplier: UInt, add addend: UInt) {
//=--------------------------------------=
if multiplicand.isZero {
if multiplier.isZero {
return self.update(addend)
}
//=--------------------------------------=
self.storage.reserveCapacity(self.storage.elements.count + 1)

let carry = self.storage.withUnsafeMutableBufferPointer {
NBK.SUISS.multiply(&$0, by: multiplicand, add: addend)
NBK.SUISS.multiply(&$0, by: multiplier, add: addend)
}

if !carry.isZero {
Expand All @@ -50,9 +50,9 @@ extension NBKFlexibleWidth.Magnitude {
Swift.assert(self.storage.isNormal)
}

@_disfavoredOverload @inlinable public func multiplied(by multiplicand: UInt, adding addend: UInt) -> Self {
@_disfavoredOverload @inlinable public func multiplied(by multiplier: UInt, adding addend: UInt) -> Self {
var result = self
result.multiply(by: multiplicand, add: addend)
result.multiply(by: multiplier, add: addend)
return result as Self
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,22 @@ extension NBKFlexibleWidth.Magnitude {
}

@inlinable public static func *(lhs: Self, rhs: Self) -> Self {
lhs.multipliedFullWidthUsingLongAlgorithm(by: rhs, adding: 0 as UInt)
lhs.multipliedUsingLongAlgorithm(by: rhs, adding: 0 as UInt)
}
}

//=----------------------------------------------------------------------------=
// MARK: + Long Multiplication
// MARK: + Long Multiplication Algorithms
//=----------------------------------------------------------------------------=

extension NBKFlexibleWidth.Magnitude {

//=------------------------------------------------------------------------=
// MARK: Transformations
// MARK: Transformations x Private
//=------------------------------------------------------------------------=

/// Performs long multiplication.
@inlinable func multipliedFullWidthUsingLongAlgorithm(by multiplier: Self, adding addend: UInt) -> Self {
@inlinable func multipliedUsingLongAlgorithm(by multiplier: Self, adding addend: UInt) -> Self {
Self.uninitialized(count: self.count + multiplier.count) { pro in
self.storage.withUnsafeBufferPointer { lhs in
multiplier.storage.withUnsafeBufferPointer { rhs in
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ extension NBKFlexibleWidth.Magnitude {
lhs.subtracting(rhs, at: 0 as Int)
}

//=------------------------------------------------------------------------=
// MARK: Transformations x Overflow
//=------------------------------------------------------------------------=

@_disfavoredOverload @inlinable public mutating func subtractReportingOverflow(_ other: UInt) -> Bool {
self.subtractReportingOverflow(other, at: 0 as Int)
}
Expand All @@ -36,7 +40,7 @@ extension NBKFlexibleWidth.Magnitude {
}

//=------------------------------------------------------------------------=
// MARK: Transformations
// MARK: Transformations x Index
//=------------------------------------------------------------------------=

@_disfavoredOverload @inlinable public mutating func subtract(_ other: UInt, at index: Int) {
Expand All @@ -50,6 +54,10 @@ extension NBKFlexibleWidth.Magnitude {
return pvo.partialValue as Self
}

//=------------------------------------------------------------------------=
// MARK: Transformations x Index x Overflow
//=------------------------------------------------------------------------=

@_disfavoredOverload @inlinable public mutating func subtractReportingOverflow(_ other: UInt, at index: Int) -> Bool {
//=--------------------------------------=
if other.isZero { return false }
Expand Down
10 changes: 9 additions & 1 deletion Sources/NBKFlexibleWidthKit/NBKFlexibleWidth+Subtraction.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ extension NBKFlexibleWidth.Magnitude {
lhs.subtracting(rhs, at: 0 as Int)
}

//=------------------------------------------------------------------------=
// MARK: Transformations x Overflow
//=------------------------------------------------------------------------=

@inlinable public mutating func subtractReportingOverflow(_ other: Self) -> Bool {
self.subtractReportingOverflow(other, at: 0 as Int)
}
Expand All @@ -36,7 +40,7 @@ extension NBKFlexibleWidth.Magnitude {
}

//=------------------------------------------------------------------------=
// MARK: Transformations
// MARK: Transformations x Index
//=------------------------------------------------------------------------=

@inlinable public mutating func subtract(_ other: Self, at index: Int) {
Expand All @@ -50,6 +54,10 @@ extension NBKFlexibleWidth.Magnitude {
return pvo.partialValue as Self
}

//=------------------------------------------------------------------------=
// MARK: Transformations x Index x Overflow
//=------------------------------------------------------------------------=

@inlinable public mutating func subtractReportingOverflow(_ other: Self, at index: Int) -> Bool {
//=--------------------------------------=
if other.isZero { return false }
Expand Down
34 changes: 0 additions & 34 deletions Sources/NBKFlexibleWidthKit/NBKFlexibleWidth+Text.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,6 @@ extension NBKFlexibleWidth.Magnitude {
// MARK: Details x Decoding
//=------------------------------------------------------------------------=

/// Creates a new instance from the given `description` and `radix`.
///
/// The `description` may contain a plus or minus sign (+ or -), followed by one
/// or more numeric digits (0-9) or letters (a-z or A-Z). If the description uses
/// an invalid format, or its value cannot be represented, the result is nil.
///
/// ```
/// ┌─────────────┬────── → ─────────────┐
/// │ description │ radix │ self │
/// ├─────────────┼────── → ─────────────┤
/// │ "123" │ 16 │ Int256( 291) │
/// │ "+123" │ 16 │ Int256( 291) │
/// │ "-123" │ 16 │ Int256(-291) │
/// │ "~123" │ 16 │ nil │
/// └─────────────┴────── → ─────────────┘
/// ```
///
/// - Note: The decoding strategy is case insensitive.
///
@inlinable public init?(_ description: some StringProtocol, radix: Int) {
let decoder = NBK.IntegerDescription.Decoder<Magnitude>(radix: radix)
guard let components: SM<Magnitude> = decoder.decode(description) else { return nil }
Expand All @@ -48,21 +29,6 @@ extension NBKFlexibleWidth.Magnitude {
// MARK: Details x Encoding
//=------------------------------------------------------------------------=

/// A `description` of this value in the given ASCII format.
///
/// The description may contain a minus sign (-), followed by one
/// or more numeric digits (0-9) or letters (a-z or A-Z). These represent
/// the integer's sign and magnitude. Zero is always non-negative.
///
/// ```
/// ┌──────────────┬───────┬─────────── → ────────────┐
/// │ self │ radix │ uppercase │ description │
/// ├──────────────┼───────┼─────────── → ────────────┤
/// │ Int256( 123) │ 12 │ true │ "A3" │
/// │ Int256(-123) │ 16 │ false │ "-7b" │
/// └──────────────┴───────┴─────────── → ────────────┘
/// ```
///
@inlinable public func description(radix: Int, uppercase: Bool) -> String {
NBK.IntegerDescription.Encoder(radix: radix, uppercase: uppercase).encode(self)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ extension NBKFlexibleWidth.Magnitude {
// MARK: Details x Contiguous UInt Collection
//=------------------------------------------------------------------------=

/// Grants unsafe access to the collection's contiguous storage.
/// Grants unsafe access to the words of this instance.
@inlinable public func withUnsafeBufferPointer<T>(
_ body: (UnsafeBufferPointer<UInt>) throws -> T) rethrows -> T {
try self.storage.withUnsafeBufferPointer(body)
}

/// Grants unsafe access to the collection's contiguous mutable storage.
/// Grants unsafe access to the mutable words of this instance.
@inlinable public mutating func withUnsafeMutableBufferPointer<T>(
_ body: (inout UnsafeMutableBufferPointer<UInt>) throws -> T) rethrows -> T {
defer{ self.storage.normalize() }
Expand All @@ -43,13 +43,13 @@ extension NBKFlexibleWidth.Magnitude.Storage {
// MARK: Details x Contiguous UInt Collection
//=------------------------------------------------------------------------=

/// Grants unsafe access to the collection's contiguous storage.
/// Grants unsafe access to the words of this instance.
@inlinable public func withUnsafeBufferPointer<T>(
_ body: (UnsafeBufferPointer<UInt>) throws -> T) rethrows -> T {
try self.elements.withUnsafeBufferPointer(body)
}

/// Grants unsafe access to the collection's contiguous mutable storage.
/// Grants unsafe access to the mutable words of this instance.
@inlinable public mutating func withUnsafeMutableBufferPointer<T>(
_ body: (inout UnsafeMutableBufferPointer<UInt>) throws -> T) rethrows -> T {
try self.elements.withUnsafeMutableBufferPointer(body)
Expand All @@ -59,7 +59,7 @@ extension NBKFlexibleWidth.Magnitude.Storage {
// MARK: Details x Contiguous UInt Collection x Sub Sequence
//=------------------------------------------------------------------------=

/// Grants unsafe access to the collection's contiguous storage in the given `range`.
/// Grants unsafe access to the words of this instance in the given `range`.
///
/// ### Development
///
Expand All @@ -74,7 +74,7 @@ extension NBKFlexibleWidth.Magnitude.Storage {
}
}

/// Grants unsafe access to the collection's contiguous mutable storage in the given `range`.
/// Grants unsafe access to the mutable words of this instance in the given `range`.
///
/// ### Development
///
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ file: StaticString = #file, line: UInt = #line) {
let pro = pro as? UIntXL
else { return }
//=------------------------------------------=
XCTAssertEqual(lhs.multipliedFullWidthUsingLongAlgorithm(by: rhs, adding: 0), pro, file: file, line: line)
XCTAssertEqual(lhs.multipliedUsingLongAlgorithm(by: rhs, adding: 0), pro, file: file, line: line)
}

private func NBKAssertMultiplicationByDigit<T: IntXLOrUIntXL>(
Expand All @@ -123,7 +123,7 @@ file: StaticString = #file, line: UInt = #line) {
XCTAssertEqual(lhs.multiplied(by: rhs, adding: add), pro, file: file, line: line)
XCTAssertEqual({ var lhs = lhs; lhs.multiply(by: rhs, add: add); return lhs }(), pro, file: file, line: line)
//=------------------------------------------=
XCTAssertEqual(lhs.multipliedFullWidthUsingLongAlgorithm(by: UIntXL(digit: rhs), adding: add), pro, file: file, line: line)
XCTAssertEqual(lhs.multipliedUsingLongAlgorithm(by: UIntXL(digit: rhs), adding: add), pro, file: file, line: line)
}

#endif
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ file: StaticString = #file, line: UInt = #line) {
//=------------------------------------------=
NBKAssertElementsEqual(integer.words, words, file: file, line: line)
integer.withUnsafeBufferPointer({ NBKAssertElementsEqual($0, words, file: file, line: line) })
integer.withUnsafeMutableBufferPointer({ NBKAssertElementsEqual($0, words, file: file, line: line) })
integer.withUnsafeMutableBufferPointer({ NBKAssertElementsEqual($0, words, file: file, line: line) })
}

//=----------------------------------------------------------------------------=
Expand Down

0 comments on commit 417832a

Please sign in to comment.