mirror of
https://github.com/apple/swift-collections
synced 2026-11-26 04:47:48 +00:00
[BitArray] Use a platform-independent Codable encoding
This commit is contained in:
1 parent
a137a71c6f
commit
141ef3c706
2 files changed
+64
-49
No files matched your search
@@ -12,35 +12,45 @@
|
||||
extension BitArray: Codable {
|
||||
/// Encodes this bit array into the given encoder.
|
||||
///
|
||||
/// Bit arrays are encoded as an unkeyed container of `UInt` values,
|
||||
/// Bit arrays are encoded as an unkeyed container of `UInt64` values,
|
||||
/// representing the total number of bits in the array, followed by
|
||||
/// UInt-sized pieces of the underlying bitmap.
|
||||
/// UInt64-sized pieces of the underlying bitmap.
|
||||
///
|
||||
/// - Parameter encoder: The encoder to write data to.
|
||||
public func encode(to encoder: Encoder) throws {
|
||||
var container = encoder.unkeyedContainer()
|
||||
try container.encode(_count)
|
||||
for word in _storage {
|
||||
try container.encode(word.value)
|
||||
}
|
||||
try container.encode(UInt64(truncatingIfNeeded: _count))
|
||||
try _storage._encodeAsUInt64(to: &container)
|
||||
}
|
||||
|
||||
/// Creates a new bit array by decoding from the given decoder.
|
||||
///
|
||||
/// Bit arrays are encoded as an unkeyed container of `UInt` values,
|
||||
/// Bit arrays are encoded as an unkeyed container of `UInt64` values,
|
||||
/// representing the total number of bits in the array, followed by
|
||||
/// UInt-sized pieces of the underlying bitmap.
|
||||
/// UInt64-sized pieces of the underlying bitmap.
|
||||
///
|
||||
/// - Parameter decoder: The decoder to read data from.
|
||||
public init(from decoder: Decoder) throws {
|
||||
var container = try decoder.unkeyedContainer()
|
||||
let count = try container.decode(UInt.self)
|
||||
var words: [_Word] = []
|
||||
if let c = container.count {
|
||||
words.reserveCapacity(c)
|
||||
guard let count = UInt(exactly: try container.decode(UInt64.self)) else {
|
||||
let context = DecodingError.Context(
|
||||
codingPath: container.codingPath,
|
||||
debugDescription: "Bit Array too long")
|
||||
throw DecodingError.dataCorrupted(context)
|
||||
}
|
||||
while !container.isAtEnd {
|
||||
words.append(_Word(try container.decode(UInt.self)))
|
||||
var words = try [_Word](
|
||||
_fromUInt64: &container,
|
||||
reservingCount: container.count.map { Swift.max(1, $0) - 1 })
|
||||
if _Word.capacity < UInt64.bitWidth,
|
||||
count <= (words.count - 1) * _Word.capacity
|
||||
{
|
||||
let last = words.removeLast()
|
||||
guard last.isEmpty else {
|
||||
let context = DecodingError.Context(
|
||||
codingPath: container.codingPath,
|
||||
debugDescription: "Unexpected bits after end")
|
||||
throw DecodingError.dataCorrupted(context)
|
||||
}
|
||||
}
|
||||
guard
|
||||
count <= words.count * _Word.capacity,
|
||||
|
||||
@@ -228,79 +228,84 @@ final class BitArrayTests: CollectionTestCase {
|
||||
|
||||
func test_Encodable() throws {
|
||||
let b1: BitArray = []
|
||||
let v1: MinimalEncoder.Value = .array([.uint(0)])
|
||||
let v1: MinimalEncoder.Value = .array([.uint64(0)])
|
||||
expectEqual(try MinimalEncoder.encode(b1), v1)
|
||||
|
||||
let b2: BitArray = [true, true, false, true]
|
||||
let v2: MinimalEncoder.Value = .array([.uint(4), .uint(11)])
|
||||
let v2: MinimalEncoder.Value = .array([.uint64(4), .uint64(11)])
|
||||
expectEqual(try MinimalEncoder.encode(b2), v2)
|
||||
|
||||
let b3 = BitArray(repeating: true, count: 2 * UInt.bitWidth + 17)
|
||||
let b3 = BitArray(repeating: true, count: 145)
|
||||
let v3: MinimalEncoder.Value = .array([
|
||||
.uint(UInt(2 * UInt.bitWidth + 17)),
|
||||
.uint(UInt.max),
|
||||
.uint(UInt.max),
|
||||
.uint((1 << 17) - 1)])
|
||||
.uint64(145),
|
||||
.uint64(UInt64.max),
|
||||
.uint64(UInt64.max),
|
||||
.uint64((1 << 17) - 1)])
|
||||
expectEqual(try MinimalEncoder.encode(b3), v3)
|
||||
|
||||
let b4 = BitArray(Array(repeating: false, count: 5 * UInt.bitWidth + 23) + [true])
|
||||
let b4 = BitArray(Array(repeating: false, count: 343) + [true])
|
||||
let v4: MinimalEncoder.Value = .array([
|
||||
.uint(UInt(5 * UInt.bitWidth + 24)),
|
||||
.uint(0),
|
||||
.uint(0),
|
||||
.uint(0),
|
||||
.uint(0),
|
||||
.uint(0),
|
||||
.uint(1 << 23),
|
||||
.uint64(344),
|
||||
.uint64(0),
|
||||
.uint64(0),
|
||||
.uint64(0),
|
||||
.uint64(0),
|
||||
.uint64(0),
|
||||
.uint64(1 << 23),
|
||||
])
|
||||
expectEqual(try MinimalEncoder.encode(b4), v4)
|
||||
}
|
||||
|
||||
func test_Decodable() throws {
|
||||
let b1: BitArray = []
|
||||
let v1: MinimalEncoder.Value = .array([.uint(0)])
|
||||
let v1: MinimalEncoder.Value = .array([.uint64(0)])
|
||||
expectEqual(try MinimalDecoder.decode(v1, as: BitArray.self), b1)
|
||||
|
||||
let b2: BitArray = [true, true, false, true]
|
||||
let v2: MinimalEncoder.Value = .array([.uint(4), .uint(11)])
|
||||
let v2: MinimalEncoder.Value = .array([.uint64(4), .uint64(11)])
|
||||
expectEqual(try MinimalDecoder.decode(v2, as: BitArray.self), b2)
|
||||
|
||||
let b3 = BitArray(repeating: true, count: 2 * UInt.bitWidth + 17)
|
||||
let b3 = BitArray(repeating: true, count: 145)
|
||||
let v3: MinimalEncoder.Value = .array([
|
||||
.uint(UInt(2 * UInt.bitWidth + 17)),
|
||||
.uint(UInt.max),
|
||||
.uint(UInt.max),
|
||||
.uint((1 << 17) - 1)])
|
||||
.uint64(145),
|
||||
.uint64(UInt64.max),
|
||||
.uint64(UInt64.max),
|
||||
.uint64((1 << 17) - 1)])
|
||||
expectEqual(try MinimalDecoder.decode(v3, as: BitArray.self), b3)
|
||||
|
||||
let b4 = BitArray(Array(repeating: false, count: 5 * UInt.bitWidth + 23) + [true])
|
||||
let b4 = BitArray(Array(repeating: false, count: 343) + [true])
|
||||
let v4: MinimalEncoder.Value = .array([
|
||||
.uint(UInt(5 * UInt.bitWidth + 24)),
|
||||
.uint(0),
|
||||
.uint(0),
|
||||
.uint(0),
|
||||
.uint(0),
|
||||
.uint(0),
|
||||
.uint(1 << 23),
|
||||
.uint64(344),
|
||||
.uint64(0),
|
||||
.uint64(0),
|
||||
.uint64(0),
|
||||
.uint64(0),
|
||||
.uint64(0),
|
||||
.uint64(1 << 23),
|
||||
])
|
||||
expectEqual(try MinimalDecoder.decode(v4, as: BitArray.self), b4)
|
||||
|
||||
let v5: MinimalEncoder.Value = .uint(42)
|
||||
let v5: MinimalEncoder.Value = .uint64(42)
|
||||
expectThrows(try MinimalDecoder.decode(v5, as: BitArray.self))
|
||||
|
||||
let v6: MinimalEncoder.Value = .array([])
|
||||
expectThrows(try MinimalDecoder.decode(v6, as: BitArray.self))
|
||||
|
||||
let v7: MinimalEncoder.Value = .array([.uint(1)])
|
||||
let v7: MinimalEncoder.Value = .array([.uint64(1)])
|
||||
expectThrows(try MinimalDecoder.decode(v7, as: BitArray.self))
|
||||
|
||||
let v8: MinimalEncoder.Value = .array([.uint(1), .uint(0), .uint(0)])
|
||||
let v8: MinimalEncoder.Value = .array([
|
||||
.uint64(1),
|
||||
.uint64(0),
|
||||
.uint64(0),
|
||||
.uint64(0)
|
||||
])
|
||||
expectThrows(try MinimalDecoder.decode(v8, as: BitArray.self))
|
||||
|
||||
let v9: MinimalEncoder.Value = .array([.uint(100), .uint(0)])
|
||||
let v9: MinimalEncoder.Value = .array([.uint64(100), .uint64(0)])
|
||||
expectThrows(try MinimalDecoder.decode(v9, as: BitArray.self))
|
||||
|
||||
let v10: MinimalEncoder.Value = .array([.uint(16), .uint(UInt.max)])
|
||||
let v10: MinimalEncoder.Value = .array([.uint64(16), .uint64(UInt64.max)])
|
||||
expectThrows(try MinimalDecoder.decode(v10, as: BitArray.self))
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user