Skip to content

Commit 24743cd

Browse files
committed
Use Array.reserveCapacity and BitReader.bit() functions in bits()
It is a bit of an optimisation.
1 parent 9f109f2 commit 24743cd

2 files changed

Lines changed: 8 additions & 20 deletions

File tree

Sources/LsbBitReader.swift

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,10 @@ public final class LsbBitReader: ByteReader, BitReader {
1818
return []
1919
}
2020

21-
var array: [UInt8] = Array(repeating: 0, count: count)
22-
for i in 0..<count {
23-
array[i] = self.data[self.offset] & self.bitMask > 0 ? 1 : 0
24-
25-
if self.bitMask == 128 {
26-
self.offset += 1
27-
self.bitMask = 1
28-
} else {
29-
self.bitMask <<= 1
30-
}
21+
var array = [UInt8]()
22+
array.reserveCapacity(count)
23+
for _ in 0..<count {
24+
array.append(self.bit())
3125
}
3226

3327
return array

Sources/MsbBitReader.swift

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,10 @@ public final class MsbBitReader: ByteReader, BitReader {
1818
return []
1919
}
2020

21-
var array: [UInt8] = Array(repeating: 0, count: count)
22-
for i in 0..<count {
23-
array[i] = self.data[self.offset] & self.bitMask > 0 ? 1 : 0
24-
25-
if self.bitMask == 1 {
26-
self.offset += 1
27-
self.bitMask = 128
28-
} else {
29-
self.bitMask >>= 1
30-
}
21+
var array = [UInt8]()
22+
array.reserveCapacity(count)
23+
for _ in 0..<count {
24+
array.append(self.bit())
3125
}
3226

3327
return array

0 commit comments

Comments
 (0)