@@ -38,7 +38,7 @@ public struct BigInt: Hashable,
38
38
39
39
// This is an array of `UInt32` words. The lowest significant word comes first in
40
40
// the array.
41
- private let uwords : [ UInt32 ]
41
+ private let uwords : ContiguousArray < UInt32 >
42
42
43
43
// `negative` signals whether the number is positive or negative.
44
44
private let negative : Bool
@@ -125,7 +125,7 @@ public struct BigInt: Hashable,
125
125
126
126
/// Internal primary constructor. It removes superfluous words and normalizes the
127
127
/// representation of zero.
128
- internal init ( words: [ UInt32 ] , negative: Bool ) {
128
+ internal init ( words: ContiguousArray < UInt32 > , negative: Bool ) {
129
129
var words = words
130
130
while words. count > 1 && words [ words. count - 1 ] == 0 {
131
131
words. removeLast ( )
@@ -137,7 +137,7 @@ public struct BigInt: Hashable,
137
137
/// Internal primary constructor. It removes superfluous words and normalizes the
138
138
/// representation of zero.
139
139
internal init ( words: [ UInt ] , negative: Bool ) {
140
- var uwords = [ UInt32] ( )
140
+ var uwords = ContiguousArray < UInt32 > ( )
141
141
uwords. reserveCapacity ( words. count * 2 )
142
142
for word in words {
143
143
let myword = UInt64 ( word)
@@ -179,7 +179,7 @@ public struct BigInt: Hashable,
179
179
/// `BigInt` numbers.
180
180
public init ( digits: [ UInt8 ] , negative: Bool = false , base: Base = BigInt . decBase) {
181
181
var digits = digits
182
- var words : [ UInt32 ] = [ ]
182
+ var words = ContiguousArray < UInt32 > ( )
183
183
var iterate : Bool
184
184
repeat {
185
185
var sum : UInt64 = 0
@@ -208,41 +208,40 @@ public struct BigInt: Hashable,
208
208
/// Creates a `BigInt` from a string containing a number using the given base.
209
209
public init ? ( from str: String , base: Base = BigInt . decBase) {
210
210
var negative = false
211
- let chars = str. characters
212
- var i = chars. startIndex
213
- while i < chars. endIndex && chars [ i] == " " {
214
- i = chars. index ( after: i)
211
+ var i = str. startIndex
212
+ while i < str. endIndex && str [ i] == " " {
213
+ i = str. index ( after: i)
215
214
}
216
- if i < chars . endIndex {
217
- if chars [ i] == " - " {
215
+ if i < str . endIndex {
216
+ if str [ i] == " - " {
218
217
negative = true
219
- i = chars . index ( after: i)
220
- } else if chars [ i] == " + " {
221
- i = chars . index ( after: i)
218
+ i = str . index ( after: i)
219
+ } else if str [ i] == " + " {
220
+ i = str . index ( after: i)
222
221
}
223
222
}
224
- if i < chars . endIndex && chars [ i] == " 0 " {
225
- while i < chars . endIndex && chars [ i] == " 0 " {
226
- i = chars . index ( after: i)
223
+ if i < str . endIndex && str [ i] == " 0 " {
224
+ while i < str . endIndex && str [ i] == " 0 " {
225
+ i = str . index ( after: i)
227
226
}
228
- if i == chars . endIndex {
227
+ if i == str . endIndex {
229
228
self . init ( 0 )
230
229
return
231
230
}
232
231
}
233
232
var temp : [ UInt8 ] = [ ]
234
- while i < chars . endIndex {
235
- if let digit = base. digitMap [ chars [ i] ] {
233
+ while i < str . endIndex {
234
+ if let digit = base. digitMap [ str [ i] ] {
236
235
temp. append ( digit)
237
- i = chars . index ( after: i)
236
+ i = str . index ( after: i)
238
237
} else {
239
238
break
240
239
}
241
240
}
242
- while i < chars . endIndex && chars [ i] == " " {
243
- i = chars . index ( after: i)
241
+ while i < str . endIndex && str [ i] == " " {
242
+ i = str . index ( after: i)
244
243
}
245
- guard i == chars . endIndex else {
244
+ guard i == str . endIndex else {
246
245
return nil
247
246
}
248
247
self . init ( digits: temp, negative: negative, base: base)
@@ -428,7 +427,7 @@ public struct BigInt: Hashable,
428
427
return self . minus ( rhs. negate)
429
428
}
430
429
let ( b1, b2) = self . uwords. count < rhs. uwords. count ? ( rhs, self ) : ( self , rhs)
431
- var res = [ UInt32] ( )
430
+ var res = ContiguousArray < UInt32 > ( )
432
431
res. reserveCapacity ( b1. uwords. count)
433
432
var sum : UInt64 = 0
434
433
for i in 0 ..< b2. uwords. count {
@@ -459,7 +458,7 @@ public struct BigInt: Hashable,
459
458
}
460
459
let negative = cmp < 0 ? !self . negative : self . negative
461
460
let ( b1, b2) = cmp < 0 ? ( rhs, self ) : ( self , rhs)
462
- var res = [ UInt32] ( )
461
+ var res = ContiguousArray < UInt32 > ( )
463
462
var carry : UInt64 = 0
464
463
for i in 0 ..< b2. uwords. count {
465
464
if UInt64 ( b1. uwords [ i] ) < UInt64 ( b2. uwords [ i] ) + carry {
@@ -485,7 +484,7 @@ public struct BigInt: Hashable,
485
484
/// Returns the result of mulitplying `self` with `rhs` as a `BigInt`
486
485
public func times( _ rhs: BigInt ) -> BigInt {
487
486
let ( b1, b2) = self . uwords. count < rhs. uwords. count ? ( rhs, self ) : ( self , rhs)
488
- var res = [ UInt32] ( repeating: 0 , count: b1. uwords. count + b2. uwords. count)
487
+ var res = ContiguousArray < UInt32 > ( repeating: 0 , count: b1. uwords. count + b2. uwords. count)
489
488
for i in 0 ..< b2. uwords. count {
490
489
var sum : UInt64 = 0
491
490
for j in 0 ..< b1. uwords. count {
@@ -499,8 +498,10 @@ public struct BigInt: Hashable,
499
498
return BigInt ( words: res, negative: b1. negative != b2. negative)
500
499
}
501
500
502
- private static func multSub( _ approx: UInt32 , _ divis: [ UInt32 ] ,
503
- _ rem: inout [ UInt32 ] , _ from: Int ) {
501
+ private static func multSub( _ approx: UInt32 ,
502
+ _ divis: ContiguousArray < UInt32 > ,
503
+ _ rem: inout ContiguousArray < UInt32 > ,
504
+ _ from: Int ) {
504
505
var sum : UInt64 = 0
505
506
var carry : UInt64 = 0
506
507
for j in 0 ..< divis. count {
@@ -517,7 +518,9 @@ public struct BigInt: Hashable,
517
518
}
518
519
}
519
520
520
- private static func subIfPossible( divis: [ UInt32 ] , rem: inout [ UInt32 ] , from: Int ) -> Bool {
521
+ private static func subIfPossible( divis: ContiguousArray < UInt32 > ,
522
+ rem: inout ContiguousArray < UInt32 > ,
523
+ from: Int ) -> Bool {
521
524
var i = divis. count
522
525
while i > 0 && divis [ i - 1 ] >= rem [ from + i - 1 ] {
523
526
if divis [ i - 1 ] > rem [ from + i - 1 ] {
@@ -553,13 +556,13 @@ public struct BigInt: Hashable,
553
556
return ( BigInt ( 0 ) , self . abs)
554
557
}
555
558
}
556
- var rem = [ UInt32] ( self . uwords)
559
+ var rem = ContiguousArray < UInt32 > ( self . uwords)
557
560
rem. append ( 0 )
558
- var divis = [ UInt32] ( rhs. uwords)
561
+ var divis = ContiguousArray < UInt32 > ( rhs. uwords)
559
562
divis. append ( 0 )
560
563
var sizediff = self . uwords. count - rhs. uwords. count
561
564
let div = UInt64 ( rhs. uwords [ rhs. uwords. count - 1 ] ) + 1
562
- var res = [ UInt32] ( repeating: 0 , count: sizediff + 1 )
565
+ var res = ContiguousArray < UInt32 > ( repeating: 0 , count: sizediff + 1 )
563
566
var divident = rem. count - 2
564
567
repeat {
565
568
var x = BigInt . joinwords ( rem [ divident] , rem [ divident + 1 ] )
@@ -617,7 +620,7 @@ public struct BigInt: Hashable,
617
620
/// Computes the bitwise `and` between this value and `rhs`.
618
621
public func and( _ rhs: BigInt ) -> BigInt {
619
622
let size = Swift . min ( self . uwords. count, rhs. uwords. count)
620
- var res = [ UInt32] ( )
623
+ var res = ContiguousArray < UInt32 > ( )
621
624
res. reserveCapacity ( size)
622
625
for i in 0 ..< size {
623
626
res. append ( self . uwords [ i] & rhs. uwords [ i] )
@@ -628,7 +631,7 @@ public struct BigInt: Hashable,
628
631
/// Computes the bitwise `or` between this value and `rhs`.
629
632
public func or( _ rhs: BigInt ) -> BigInt {
630
633
let size = Swift . max ( self . uwords. count, rhs. uwords. count)
631
- var res = [ UInt32] ( )
634
+ var res = ContiguousArray < UInt32 > ( )
632
635
res. reserveCapacity ( size)
633
636
for i in 0 ..< size {
634
637
let fst = i < self . uwords. count ? self . uwords [ i] : 0
@@ -641,7 +644,7 @@ public struct BigInt: Hashable,
641
644
/// Computes the bitwise `xor` between this value and `rhs`.
642
645
public func xor( _ rhs: BigInt ) -> BigInt {
643
646
let size = Swift . max ( self . uwords. count, rhs. uwords. count)
644
- var res = [ UInt32] ( )
647
+ var res = ContiguousArray < UInt32 > ( )
645
648
res. reserveCapacity ( size)
646
649
for i in 0 ..< size {
647
650
let fst = i < self . uwords. count ? self . uwords [ i] : 0
@@ -653,7 +656,7 @@ public struct BigInt: Hashable,
653
656
654
657
/// Inverts the bits in this `BigInt`.
655
658
public var invert : BigInt {
656
- var res = [ UInt32] ( )
659
+ var res = ContiguousArray < UInt32 > ( )
657
660
res. reserveCapacity ( self . uwords. count)
658
661
for word in self . uwords {
659
662
res. append ( ~ word)
@@ -676,7 +679,7 @@ public struct BigInt: Hashable,
676
679
private func shiftLeft( _ x: Int ) -> BigInt {
677
680
let swords = x / UInt32. bitWidth
678
681
let sbits = x % UInt32. bitWidth
679
- var res = [ UInt32] ( )
682
+ var res = ContiguousArray < UInt32 > ( )
680
683
res. reserveCapacity ( Int ( self . uwords. count) + swords)
681
684
for _ in 0 ..< swords {
682
685
res. append ( 0 )
@@ -695,7 +698,7 @@ public struct BigInt: Hashable,
695
698
private func shiftRight( _ x: Int ) -> BigInt {
696
699
let swords = x / UInt32. bitWidth
697
700
let sbits = x % UInt32. bitWidth
698
- var res = [ UInt32] ( )
701
+ var res = ContiguousArray < UInt32 > ( )
699
702
res. reserveCapacity ( Int ( self . uwords. count) - swords)
700
703
var carry : UInt32 = 0
701
704
var i = self . uwords. count - 1
@@ -705,7 +708,7 @@ public struct BigInt: Hashable,
705
708
carry = word << ( UInt32 . bitWidth - sbits)
706
709
i -= 1
707
710
}
708
- return BigInt ( words: res. reversed ( ) , negative: self . negative)
711
+ return BigInt ( words: ContiguousArray < UInt32 > ( res. reversed ( ) ) , negative: self . negative)
709
712
}
710
713
}
711
714
0 commit comments