Skip to content

Commit

Permalink
Reorganize BinaryInteger+{Elements,Integers,Loading}.swift stuff.
Browse files Browse the repository at this point in the history
  • Loading branch information
oscbyspro committed Nov 4, 2024
1 parent abf8923 commit e51d7da
Show file tree
Hide file tree
Showing 11 changed files with 774 additions and 694 deletions.
233 changes: 6 additions & 227 deletions Sources/CoreKit/BinaryInteger+Elements.swift
Original file line number Diff line number Diff line change
Expand Up @@ -84,205 +84,16 @@ extension BinaryInteger {
initializer delegate: (MutableDataInt<Element.Magnitude>.Body) -> Void
) -> Optional<Self> {

guard !Self.isArbitrary else { return nil }
var instance = Self()
instance.withUnsafeMutableBinaryIntegerBody(delegate)
return instance
}

//=------------------------------------------------------------------------=
// MARK: Initializers x Data Int
//=------------------------------------------------------------------------=

/// Returns the bit pattern of `source` that fits.
@inlinable public init<OtherElement>(load source: DataInt<OtherElement>) {
if Self.Element.Magnitude.size <= OtherElement.size {
self = source.reinterpret(as: Self.Element.Magnitude.self, perform: Self.init(load:))
} else {
self = source.reinterpret(as: U8.self, perform: Self.init(load:))
}
}

/// Loads the `source` by trapping on `error`.
///
/// - Note: The `error` is set if the operation is `lossy`.
///
/// - Note: The `signedness` determines the significance of the `appendix`.
///
@inlinable public init<OtherElement>(
_ source: DataInt<OtherElement>,
mode signedness: Signedness = Self.mode
) {

self = Self.exactly(source, mode: signedness).unwrap()
}

/// Loads the `source` and returns an `error` indicator.
///
/// - Note: The `error` is set if the operation is `lossy`.
///
/// - Note: The `signedness` determines the significance of the `appendix`.
///
@inlinable public static func exactly<OtherElement>(
_ source: DataInt<OtherElement>,
mode signedness: Signedness = Self.mode
) -> Fallible<Self> {

if Self.Element.Magnitude.size <= OtherElement.size {
return source.reinterpret(as: Self.Element.Magnitude.self) {
Self.exactly($0, mode: signedness)
}
if Self.isArbitrary {
return nil

} else {
return source.reinterpret(as: U8.self) {
Self.exactly($0, mode: signedness)
}
}
}

/// Loads the `source` and returns an `error` indicator.
///
/// - Note: The `error` is set if the operation is `lossy`.
///
/// - Note: The `signedness` determines the significance of the `appendix`.
///
@inlinable public static func exactly(
_ source: DataInt<U8>,
mode signedness: Signedness = Self.mode
) -> Fallible<Self> {

let instance = Self(load: source)
let appendix = instance.appendix
var success = appendix == source.appendix

if success, signedness != Self.mode {
success = appendix == Bit .zero
}

if success, let size = UX(size: Self.self) {
let end = size / UX(size: U8.self)
success = source[end...].normalized().body.isEmpty
}

return instance.veto(!Bool(success))
}

/// Loads the `source` and returns an `error` indicator.
///
/// - Note: The `error` is set if the operation is `lossy`.
///
/// - Note: The `signedness` determines the significance of the `appendix`.
///
@inlinable public static func exactly(
_ source: DataInt<Element.Magnitude>,
mode signedness: Signedness = Self.mode
) -> Fallible<Self> {

let instance = Self(load: source)
let appendix = instance.appendix
var success = appendix == source.appendix

if success, signedness != Self.mode {
success = appendix == Bit .zero
}

if success, let size = UX(size: Self.self) {
let end = size / UX(size: Element.Magnitude.self)
success = source[end...].normalized().body.isEmpty
var instance = Self()
instance.withUnsafeMutableBinaryIntegerBody(delegate)
return instance
}

return instance.veto(!Bool(success))
}

//=------------------------------------------------------------------------=
// MARK: Initializers x Contiguous
//=------------------------------------------------------------------------=

/// Returns the bit pattern of `body` and `appendix` that fits.
@inlinable public init<Body: Contiguous>(
load body: borrowing Body,
repeating appendix: Bit = .zero
) where Body.Element: SystemsInteger & UnsignedInteger {

self = body.withUnsafeBufferPointer {
$0.withMemoryRebound(to: Body.Element.Element.self) {
Self(load: DataInt($0, repeating: appendix)!)
}
}
}

/// Loads the `body` and `appendix` by trapping on `error`.
///
/// - Note: The `error` is set if the operation is `lossy`.
///
/// - Note: The `signedness` determines the significance of the `appendix`.
///
@inlinable public init<Body: Contiguous>(
_ body: borrowing Body,
repeating appendix: Bit = .zero,
mode signedness: Signedness = Self.mode
) where Body.Element: SystemsInteger & UnsignedInteger {

self = body.withUnsafeBufferPointer {
$0.withMemoryRebound(to: Body.Element.Element.self) {
Self(DataInt($0, repeating: appendix)!, mode: signedness)
}
}
}

/// Loads the `body` and `appendix` and returns an `error` indicator.
///
/// - Note: The `error` is set if the operation is `lossy`.
///
/// - Note: The `signedness` determines the significance of the `appendix`.
///
@inlinable public static func exactly<Body: Contiguous>(
_ body: borrowing Body,
repeating appendix: Bit = .zero,
mode signedness: Signedness = Self.mode
) -> Fallible<Self> where Body.Element: SystemsInteger & UnsignedInteger {

body.withUnsafeBufferPointer {
$0.withMemoryRebound(to: Body.Element.Element.self) {
Self.exactly(DataInt($0, repeating: appendix)!, mode: signedness)
}
}
}

//=------------------------------------------------------------------------=
// MARK: Initializers x Binary Integer
//=------------------------------------------------------------------------=

/// Returns the bit pattern of `source` that fits.
@inlinable public init<Other>(load source: borrowing Other) where Other: BinaryInteger {
if Other.size <= Swift.min(Element.size, UX.size) {

if Other.isSigned {
self.init(load: Element.Signitude(load: IX(load: source)))
} else {
self.init(load: Element.Magnitude(load: UX(load: source)))
}

} else if Self.size <= UX.size || Other.size <= UX.size {

if Other.isSigned {
self.init(load: IX(load: source))
} else {
self.init(load: UX(load: source))
}

} else {
self = source.withUnsafeBinaryIntegerElements(Self.init(load:))
}
}
}

//=----------------------------------------------------------------------------=
// MARK: + Data Int Source
//=----------------------------------------------------------------------------=

extension BinaryInteger {

//=------------------------------------------------------------------------=
// MARK: Utilities
//=------------------------------------------------------------------------=
Expand Down Expand Up @@ -312,7 +123,7 @@ extension BinaryInteger {
}

//=------------------------------------------------------------------------=
// MARK: Utilities x Downsized
// MARK: Utilities x Reinterpreting
//=------------------------------------------------------------------------=

/// Performs the `action` on the `body` of `self` and temporarily
Expand Down Expand Up @@ -418,36 +229,4 @@ extension SystemsInteger {

nil // SystemsInteger != ArbitraryInteger
}

//=------------------------------------------------------------------------=
// MARK: Initializers
//=------------------------------------------------------------------------=

/// Creates a new instance from the bit pattern of `source` that fits.
///
/// - Note: This is the generic version of `BinaryInteger/load(as:)`.
///
@inlinable public init<Other>(load source: borrowing Other)
where Other: BinaryInteger, BitPattern == UX.BitPattern {
self.init(raw: source.load(as: BitPattern.self))
}

/// Creates a new instance from the bit pattern of `source` that fits.
///
/// - Note: This is the generic version of `BinaryInteger/load(as:)`.
///
@inlinable public init<Other>(load source: borrowing Other)
where Other: BinaryInteger, BitPattern == Other.Element.BitPattern {
self.init(raw: source.load(as: BitPattern.self))
}

/// Creates a new instance from the bit pattern of `source` that fits.
///
/// - Note: This is the generic version of `BinaryInteger/load(as:)`.
///
@inlinable public init<Other>(load source: borrowing Other)
where Other: BinaryInteger, BitPattern == Other.Element.BitPattern, BitPattern == UX.BitPattern {
let source: some BinaryInteger = copy source
self.init(raw: source.load(as: BitPattern.self))
}
}
Loading

0 comments on commit e51d7da

Please sign in to comment.