Adding Packed-Boolean Initializers

extension BitArray {    public init<W: UnsignedInteger>(word: W, bitCount: Int, bitIterationDirection: EndianTraversal) {
precondition(0...word.bitWidth ~= bitCount)
let coreWords: WordArray
switch bitIterationDirection {
case .lo2hi:
coreWords = WordArray(word.words)
case .hi2lo:
let (wbwq, wbwr) = word.bitWidth.quotientAndRemainder(dividingBy: Word.bitWidth)
let wordWords = WordArray(word.words)
assert(wbwq + wbwr.signum() == wordWords.count)
let highOrderWordBitCount = wbwr != 0 ? wbwr : wbwq.signum() * Word.bitWidth
let head = BitArray(coreWords: [wordWords.last! << (Word.bitWidth - highOrderWordBitCount)], bitCount: highOrderWordBitCount, bitIterationDirection: .hi2lo)
var tail = BitArray(coreWords: wordWords.dropLast().reversed(), bitCount: Word.bitWidth * (wordWords.count - 1), bitIterationDirection: .hi2lo)
tail.prependHead(head)
coreWords = tail.bits
}
self.init(coreWords: coreWords, bitCount: bitCount, bitIterationDirection: bitIterationDirection)
}
//...}
{    //...    public init<S>(words: S, bitCount: Int, bitIterationDirection: EndianTraversal) where S: Sequence, S.Element: UnsignedInteger {
let bitArraySlivers = words.map { BitArray(word: $0, bitCount: $0.bitWidth, bitIterationDirection: bitIterationDirection) }
var scratch = BitArray(coreWords: [], bitCount: 0, bitIterationDirection: bitIterationDirection)
for s in bitArraySlivers.reversed() {
scratch.prependHead(s)
}
self = scratch.head(bitCount: bitCount)
}
}
// An unsigned integer type bigger than any of the standard ones.
struct UInt72: FixedWidthInteger, UnsignedInteger {
// Implementation properties
var high: UInt8
var low: UInt64
// Main initializer
init(highOrderBits hi: UInt8, lowOrderBits lo: UInt64) { (high, low) = (hi, lo) }
// FixedWidthInteger secret initializer
init(_truncatingBits bits: UInt) { self.init(highOrderBits: 0, lowOrderBits: UInt64(bits)) }
// FixedWidthInteger properties
var byteSwapped: UInt72 {
return UInt72(highOrderBits: UInt8(truncatingIfNeeded: low), lowOrderBits: (low.byteSwapped << 8) | UInt64(high))
}
var leadingZeroBitCount: Int { return high != 0 ? high.leadingZeroBitCount : 8 + low.leadingZeroBitCount }
var nonzeroBitCount: Int { return high.nonzeroBitCount + low.nonzeroBitCount }
static var bitWidth: Int { return 72 } // BinaryInteger properties
var trailingZeroBitCount: Int { return low != 0 ? low.trailingZeroBitCount : high.trailingZeroBitCount + 64 }
var words: [UInt] { return Array(low.words) + high.words }
// ExpressibleByIntegerLiteral and Hashable support
init(integerLiteral value: UInt) { self.init(_truncatingBits: value) }
var hashValue: Int { return String(describing: self).hashValue } // BinaryInteger floating-point initializer
init<T>(_ source: T) where T : BinaryFloatingPoint { fatalError("\(#function) not implemented") }
// FixedWidthInteger core math
func addingReportingOverflow(_ rhs: UInt72) -> (partialValue: UInt72, overflow: Bool) {
fatalError("\(#function) not implemented")
}
func dividedReportingOverflow(by rhs: UInt72) -> (partialValue: UInt72, overflow: Bool) {
fatalError("\(#function) not implemented")
}
func dividingFullWidth(_ dividend: (high: UInt72, low: UInt72)) -> (quotient: UInt72, remainder: UInt72) {
fatalError("\(#function) not implemented")
}
func multipliedReportingOverflow(by rhs: UInt72) -> (partialValue: UInt72, overflow: Bool) {
fatalError("\(#function) not implemented")
}
func multipliedFullWidth(by other: UInt72) -> (high: UInt72, low: UInt72) {
fatalError("\(#function) not implemented")
}
func remainderReportingOverflow(dividingBy rhs: UInt72) -> (partialValue: UInt72, overflow: Bool) {
fatalError("\(#function) not implemented")
}
func subtractingReportingOverflow(_ rhs: UInt72) -> (partialValue: UInt72, overflow: Bool) {
fatalError("\(#function) not implemented")
}
// BinaryInteger operators
static func %(lhs: UInt72, rhs: UInt72) -> UInt72 {
let results = lhs.remainderReportingOverflow(dividingBy: rhs)
assert(!results.overflow)
return results.partialValue
}
static func *(lhs: UInt72, rhs: UInt72) -> UInt72 {
let results = lhs.multipliedReportingOverflow(by: rhs)
assert(!results.overflow)
return results.partialValue
}
static func +(lhs: UInt72, rhs: UInt72) -> UInt72 {
let results = lhs.addingReportingOverflow(rhs)
assert(!results.overflow)
return results.partialValue
}
static func -(lhs: UInt72, rhs: UInt72) -> UInt72 {
let results = lhs.subtractingReportingOverflow(rhs)
assert(!results.overflow)
return results.partialValue
}
static func /(lhs: UInt72, rhs: UInt72) -> UInt72 {
let results = lhs.dividedReportingOverflow(by: rhs)
assert(!results.overflow)
return results.partialValue
}
static func %=(lhs: inout UInt72, rhs: UInt72) { lhs = lhs % rhs }
static func *=(lhs: inout UInt72, rhs: UInt72) { lhs = lhs * rhs }
static func +=(lhs: inout UInt72, rhs: UInt72) { lhs = lhs + rhs }
static func -=(lhs: inout UInt72, rhs: UInt72) { lhs = lhs - rhs }
static func /=(lhs: inout UInt72, rhs: UInt72) { lhs = lhs / rhs }
}

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Simple Microservice Template by Apollo GraphQL

Cross-Origin Resource Sharing

Summer Internship Experience

Understanding Agile Project Management

Moving REST API To GraphQL

Living Documentation — Brief History and Evolution of the Concept

Skill hub initiative launched in J&K

The smart enemy gets even smarter!

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Daryle Walker

Daryle Walker

More from Medium

Singleton Design Pattern in Swift

Macbook with some code on the screen

Singleton in Swift New

Batch Convert XCode String files, from utf-16 to utf-8

Dependency Injection in iOS Applications Part 2 — Container based DI