diff --git a/Sources/BitCollections/BitArray/BitArray+Codable.swift b/Sources/BitCollections/BitArray/BitArray+Codable.swift new file mode 100644 index 00000000..081d56b4 --- /dev/null +++ b/Sources/BitCollections/BitArray/BitArray+Codable.swift @@ -0,0 +1,63 @@ +//===----------------------------------------------------------------------===// +// +// This source file is part of the Swift Collections open source project +// +// Copyright (c) 2022 Apple Inc. and the Swift project authors +// Licensed under Apache License v2.0 with Runtime Library Exception +// +// See https://swift.org/LICENSE.txt for license information +// +//===----------------------------------------------------------------------===// + +extension BitArray: Codable { + /// Encodes this bit array into the given encoder. + /// + /// Bit arrays are encoded as an unkeyed container of `UInt` values, + /// representing the total number of bits in the array, followed by + /// UInt-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) + } + } + + /// Creates a new bit array by decoding from the given decoder. + /// + /// Bit arrays are encoded as an unkeyed container of `UInt` values, + /// representing the total number of bits in the array, followed by + /// UInt-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) + } + while !container.isAtEnd { + words.append(_Word(try container.decode(UInt.self))) + } + guard + count <= words.count * _Word.capacity, + count > (words.count - 1) * _Word.capacity + else { + let context = DecodingError.Context( + codingPath: container.codingPath, + debugDescription: "Decoded words don't match expected count") + throw DecodingError.dataCorrupted(context) + } + let bit = _BitPosition(count).bit + if bit > 0, !words.last!.subtracting(_Word(upTo: bit)).isEmpty { + let context = DecodingError.Context( + codingPath: container.codingPath, + debugDescription: "Unexpected bits after end") + throw DecodingError.dataCorrupted(context) + } + self.init(_storage: words, count: count) + } +} diff --git a/Tests/BitCollectionsTests/BitArrayTests.swift b/Tests/BitCollectionsTests/BitArrayTests.swift index d9a45327..bada2521 100644 --- a/Tests/BitCollectionsTests/BitArrayTests.swift +++ b/Tests/BitCollectionsTests/BitArrayTests.swift @@ -226,6 +226,84 @@ final class BitArrayTests: CollectionTestCase { checkHashable(equivalenceClasses: classes) } + func test_Encodable() throws { + let b1: BitArray = [] + let v1: MinimalEncoder.Value = .array([.uint(0)]) + expectEqual(try MinimalEncoder.encode(b1), v1) + + let b2: BitArray = [true, true, false, true] + let v2: MinimalEncoder.Value = .array([.uint(4), .uint(11)]) + expectEqual(try MinimalEncoder.encode(b2), v2) + + let b3 = BitArray(repeating: true, count: 2 * UInt.bitWidth + 17) + let v3: MinimalEncoder.Value = .array([ + .uint(UInt(2 * UInt.bitWidth + 17)), + .uint(UInt.max), + .uint(UInt.max), + .uint((1 << 17) - 1)]) + expectEqual(try MinimalEncoder.encode(b3), v3) + + let b4 = BitArray(Array(repeating: false, count: 5 * UInt.bitWidth + 23) + [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), + ]) + expectEqual(try MinimalEncoder.encode(b4), v4) + } + + func test_Decodable() throws { + let b1: BitArray = [] + let v1: MinimalEncoder.Value = .array([.uint(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)]) + expectEqual(try MinimalDecoder.decode(v2, as: BitArray.self), b2) + + let b3 = BitArray(repeating: true, count: 2 * UInt.bitWidth + 17) + let v3: MinimalEncoder.Value = .array([ + .uint(UInt(2 * UInt.bitWidth + 17)), + .uint(UInt.max), + .uint(UInt.max), + .uint((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 v4: MinimalEncoder.Value = .array([ + .uint(UInt(5 * UInt.bitWidth + 24)), + .uint(0), + .uint(0), + .uint(0), + .uint(0), + .uint(0), + .uint(1 << 23), + ]) + expectEqual(try MinimalDecoder.decode(v4, as: BitArray.self), b4) + + let v5: MinimalEncoder.Value = .uint(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)]) + expectThrows(try MinimalDecoder.decode(v7, as: BitArray.self)) + + let v8: MinimalEncoder.Value = .array([.uint(1), .uint(0), .uint(0)]) + expectThrows(try MinimalDecoder.decode(v8, as: BitArray.self)) + + let v9: MinimalEncoder.Value = .array([.uint(100), .uint(0)]) + expectThrows(try MinimalDecoder.decode(v9, as: BitArray.self)) + + let v10: MinimalEncoder.Value = .array([.uint(16), .uint(UInt.max)]) + expectThrows(try MinimalDecoder.decode(v10, as: BitArray.self)) + } + func test_replaceSubrange_Array() { withSome("count", in: 0 ..< 512, maxSamples: 10) { count in print(count)