diff --git a/Sources/BitCollections/BitArray/BinaryInteger extensions.swift b/Sources/BitCollections/BitArray/BinaryInteger extensions.swift new file mode 100644 index 00000000..57b1187c --- /dev/null +++ b/Sources/BitCollections/BitArray/BinaryInteger extensions.swift @@ -0,0 +1,216 @@ +//===----------------------------------------------------------------------===// +// +// This source file is part of the Swift Collections open source project +// +// Copyright (c) 2021 - 2026 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 +// +//===----------------------------------------------------------------------===// + +#if !COLLECTIONS_SINGLE_MODULE +import InternalCollectionsUtilities +#endif + +extension BitArray { + @inlinable + package func _convert( + to type: T.Type + ) -> ( + value: T, + isNegative: Bool + ) { + var value: T = .zero + let isNegative = self._foreachTwosComplementWordDownward( + isSigned: T.isSigned + ) { _, word in + value <<= UInt.bitWidth + value |= T(truncatingIfNeeded: word) + return true + } + return (value, isNegative) + } + + @inlinable + package func _convertTruncating(to type: T.Type) -> T { + self._convert(to: T.self).value + } + + @inlinable + package func _convertExactly(to type: T.Type) -> T? { + let (value, isNegative) = self._convert(to: T.self) + guard isNegative == (value < 0) else { return nil } + let words = value.words + var equal = true + _ = self._foreachTwosComplementWordDownward(isSigned: T.isSigned) { i, word in + assert(equal) + let w = ( + i < words.count ? words[i] + : isNegative ? UInt.max + : UInt.zero) + equal = (w == word) + return equal + } + guard equal else { return nil } + return value + } + + @inlinable + package func _convertForced(to type: T.Type) -> T { + guard let value = _convertExactly(to: T.self) else { + fatalError(""" + BitArray value cannot be converted to \(Self.self) because it is \ + outside the representable range + """) + } + return value + } +} + +extension BinaryInteger { + /// Creates a new instance by truncating or extending the bits in the given + /// bit array, as needed. The bit at position 0 in `source` will correspond + /// to the least-significant bit in the result. + /// + /// If `Self` is an unsigned integer type, then the result will contain as + /// many bits from `source` it can accommodate, truncating off any extras. + /// + /// UInt8(truncatingIfNeeded: "" as BitArray) // 0 + /// UInt8(truncatingIfNeeded: "0" as BitArray) // 0 + /// UInt8(truncatingIfNeeded: "1" as BitArray) // 1 + /// UInt8(truncatingIfNeeded: "11" as BitArray) // 3 + /// UInt8(truncatingIfNeeded: "11111111" as BitArray) // 255 + /// UInt8(truncatingIfNeeded: "1100000001" as BitArray) // 1 + /// UInt8(truncatingIfNeeded: "1100000101" as BitArray) // 5 + /// + /// If `Self` is a signed integer type, then the contents of the bit array + /// are interpreted to be a two's complement representation of a signed + /// integer value, with the last bit in the array representing the sign of + /// the result. + /// + /// Int8(truncatingIfNeeded: "" as BitArray) // 0 + /// Int8(truncatingIfNeeded: "0" as BitArray) // 0 + /// Int8(truncatingIfNeeded: "1" as BitArray) // -1 + /// Int8(truncatingIfNeeded: "01" as BitArray) // 1 + /// Int8(truncatingIfNeeded: "101" as BitArray) // -3 + /// Int8(truncatingIfNeeded: "0101" as BitArray) // 5 + /// + /// Int8(truncatingIfNeeded: "00000001" as BitArray) // 1 + /// Int8(truncatingIfNeeded: "00000101" as BitArray) // 5 + /// Int8(truncatingIfNeeded: "01111111" as BitArray) // 127 + /// Int8(truncatingIfNeeded: "10000000" as BitArray) // -128 + /// Int8(truncatingIfNeeded: "11111111" as BitArray) // -1 + /// + /// Int8(truncatingIfNeeded: "000011111111" as BitArray) // -1 + /// Int8(truncatingIfNeeded: "111100000000" as BitArray) // 0 + /// Int8(truncatingIfNeeded: "111100000001" as BitArray) // 1 + @inlinable + @inline(__always) + public init(truncatingIfNeeded source: BitArray) { + self = source._convertTruncating(to: Self.self) + } + + /// Creates a new instance from the bits in the given bit array, if the + /// corresponding integer value can be represented exactly. + /// If the value is not representable exactly, then the result is `nil`. + /// + /// If `Self` is an unsigned integer type, then the contents of the bit array + /// are interpreted to be the binary representation of a nonnegative + /// integer value. The bit array is allowed to contain bits in unrepresentable + /// positions, as long as they are all cleared. + /// + /// UInt8(exactly: "" as BitArray) // 0 + /// UInt8(exactly: "0" as BitArray) // 0 + /// UInt8(exactly: "1" as BitArray) // 1 + /// UInt8(exactly: "10" as BitArray) // 2 + /// UInt8(exactly: "00000000" as BitArray) // 0 + /// UInt8(exactly: "11111111" as BitArray) // 255 + /// UInt8(exactly: "0000000000000" as BitArray) // 0 + /// UInt8(exactly: "0000011111111" as BitArray) // 255 + /// UInt8(exactly: "0000100000000" as BitArray) // nil + /// UInt8(exactly: "1111111111111" as BitArray) // nil + /// + /// If `Self` is a signed integer type, then the contents of the bit array + /// are interpreted to be a two's complement representation of a signed + /// integer value, with the last bit in the array representing the sign of + /// the result. + /// + /// Int8(exactly: "" as BitArray) // 0 + /// Int8(exactly: "0" as BitArray) // 0 + /// Int8(exactly: "1" as BitArray) // -1 + /// Int8(exactly: "01" as BitArray) // 1 + /// Int8(exactly: "101" as BitArray) // -3 + /// Int8(exactly: "0101" as BitArray) // 5 + /// + /// Int8(exactly: "00000001" as BitArray) // 1 + /// Int8(exactly: "00000101" as BitArray) // 5 + /// Int8(exactly: "01111111" as BitArray) // 127 + /// Int8(exactly: "10000000" as BitArray) // -128 + /// Int8(exactly: "11111111" as BitArray) // -1 + /// + /// Int8(exactly: "00000000000" as BitArray) // 0 + /// Int8(exactly: "00001111111" as BitArray) // 127 + /// Int8(exactly: "00010000000" as BitArray) // nil + /// Int8(exactly: "11101111111" as BitArray) // nil + /// Int8(exactly: "11110000000" as BitArray) // -128 + /// Int8(exactly: "11111111111" as BitArray) // -1 + @inlinable + @inline(__always) + public init?(exactly source: BitArray) { + guard let v = source._convertExactly(to: Self.self) else { return nil } + self = v + } + + /// Creates a new instance from the bits in the given bit array, if the + /// corresponding integer value can be represented exactly. + /// If the value is not representable exactly, then a runtime error will + /// occur. + /// + /// If `Self` is an unsigned integer type, then the contents of the bit array + /// are interpreted to be the binary representation of a nonnegative + /// integer value. The bit array is allowed to contain bits in unrepresentable + /// positions, as long as they are all cleared. + /// + /// UInt8("" as BitArray) // 0 + /// UInt8("0" as BitArray) // 0 + /// UInt8("1" as BitArray) // 1 + /// UInt8("10" as BitArray) // 2 + /// UInt8("00000000" as BitArray) // 0 + /// UInt8("11111111" as BitArray) // 255 + /// UInt8("0000000000000" as BitArray) // 0 + /// UInt8("0000011111111" as BitArray) // 255 + /// UInt8("0000100000000" as BitArray) // ERROR + /// UInt8("1111111111111" as BitArray) // ERROR + /// + /// If `Self` is a signed integer type, then the contents of the bit array + /// are interpreted to be a two's complement representation of a signed + /// integer value, with the last bit in the array representing the sign of + /// the result. + /// + /// Int8("" as BitArray) // 0 + /// Int8("0" as BitArray) // 0 + /// Int8("1" as BitArray) // -1 + /// Int8("01" as BitArray) // 1 + /// Int8("101" as BitArray) // -3 + /// Int8("0101" as BitArray) // 5 + /// + /// Int8("00000001" as BitArray) // 1 + /// Int8("00000101" as BitArray) // 5 + /// Int8("01111111" as BitArray) // 127 + /// Int8("10000000" as BitArray) // -128 + /// Int8("11111111" as BitArray) // -1 + /// + /// Int8("00000000000" as BitArray) // 0 + /// Int8("00001111111" as BitArray) // 127 + /// Int8("00010000000" as BitArray) // ERROR + /// Int8("11101111111" as BitArray) // ERROR + /// Int8("11110000000" as BitArray) // -128 + /// Int8("11111111111" as BitArray) // -1 + @inlinable + @inline(__always) + public init(_ source: BitArray) { + self = source._convertForced(to: Self.self) + } +} + diff --git a/Sources/BitCollections/BitArray/BitArray+Initializers.swift b/Sources/BitCollections/BitArray/BitArray+Initializers.swift index d2ec9c8d..c8870d79 100644 --- a/Sources/BitCollections/BitArray/BitArray+Initializers.swift +++ b/Sources/BitCollections/BitArray/BitArray+Initializers.swift @@ -67,183 +67,6 @@ extension BitArray { } } -extension BinaryInteger { - @inlinable - internal static func _convert( - _ source: BitArray - ) -> (value: Self, isNegative: Bool) { - var value: Self = .zero - let isNegative = source._foreachTwosComplementWordDownward( - isSigned: Self.isSigned - ) { _, word in - value <<= UInt.bitWidth - value |= Self(truncatingIfNeeded: word) - return true - } - return (value, isNegative) - } - - /// Creates a new instance by truncating or extending the bits in the given - /// bit array, as needed. The bit at position 0 in `source` will correspond - /// to the least-significant bit in the result. - /// - /// If `Self` is an unsigned integer type, then the result will contain as - /// many bits from `source` it can accommodate, truncating off any extras. - /// - /// UInt8(truncatingIfNeeded: "" as BitArray) // 0 - /// UInt8(truncatingIfNeeded: "0" as BitArray) // 0 - /// UInt8(truncatingIfNeeded: "1" as BitArray) // 1 - /// UInt8(truncatingIfNeeded: "11" as BitArray) // 3 - /// UInt8(truncatingIfNeeded: "11111111" as BitArray) // 255 - /// UInt8(truncatingIfNeeded: "1100000001" as BitArray) // 1 - /// UInt8(truncatingIfNeeded: "1100000101" as BitArray) // 5 - /// - /// If `Self` is a signed integer type, then the contents of the bit array - /// are interpreted to be a two's complement representation of a signed - /// integer value, with the last bit in the array representing the sign of - /// the result. - /// - /// Int8(truncatingIfNeeded: "" as BitArray) // 0 - /// Int8(truncatingIfNeeded: "0" as BitArray) // 0 - /// Int8(truncatingIfNeeded: "1" as BitArray) // -1 - /// Int8(truncatingIfNeeded: "01" as BitArray) // 1 - /// Int8(truncatingIfNeeded: "101" as BitArray) // -3 - /// Int8(truncatingIfNeeded: "0101" as BitArray) // 5 - /// - /// Int8(truncatingIfNeeded: "00000001" as BitArray) // 1 - /// Int8(truncatingIfNeeded: "00000101" as BitArray) // 5 - /// Int8(truncatingIfNeeded: "01111111" as BitArray) // 127 - /// Int8(truncatingIfNeeded: "10000000" as BitArray) // -128 - /// Int8(truncatingIfNeeded: "11111111" as BitArray) // -1 - /// - /// Int8(truncatingIfNeeded: "000011111111" as BitArray) // -1 - /// Int8(truncatingIfNeeded: "111100000000" as BitArray) // 0 - /// Int8(truncatingIfNeeded: "111100000001" as BitArray) // 1 - @inlinable - public init(truncatingIfNeeded source: BitArray) { - self = Self._convert(source).value - } - - /// Creates a new instance from the bits in the given bit array, if the - /// corresponding integer value can be represented exactly. - /// If the value is not representable exactly, then the result is `nil`. - /// - /// If `Self` is an unsigned integer type, then the contents of the bit array - /// are interpreted to be the binary representation of a nonnegative - /// integer value. The bit array is allowed to contain bits in unrepresentable - /// positions, as long as they are all cleared. - /// - /// UInt8(exactly: "" as BitArray) // 0 - /// UInt8(exactly: "0" as BitArray) // 0 - /// UInt8(exactly: "1" as BitArray) // 1 - /// UInt8(exactly: "10" as BitArray) // 2 - /// UInt8(exactly: "00000000" as BitArray) // 0 - /// UInt8(exactly: "11111111" as BitArray) // 255 - /// UInt8(exactly: "0000000000000" as BitArray) // 0 - /// UInt8(exactly: "0000011111111" as BitArray) // 255 - /// UInt8(exactly: "0000100000000" as BitArray) // nil - /// UInt8(exactly: "1111111111111" as BitArray) // nil - /// - /// If `Self` is a signed integer type, then the contents of the bit array - /// are interpreted to be a two's complement representation of a signed - /// integer value, with the last bit in the array representing the sign of - /// the result. - /// - /// Int8(exactly: "" as BitArray) // 0 - /// Int8(exactly: "0" as BitArray) // 0 - /// Int8(exactly: "1" as BitArray) // -1 - /// Int8(exactly: "01" as BitArray) // 1 - /// Int8(exactly: "101" as BitArray) // -3 - /// Int8(exactly: "0101" as BitArray) // 5 - /// - /// Int8(exactly: "00000001" as BitArray) // 1 - /// Int8(exactly: "00000101" as BitArray) // 5 - /// Int8(exactly: "01111111" as BitArray) // 127 - /// Int8(exactly: "10000000" as BitArray) // -128 - /// Int8(exactly: "11111111" as BitArray) // -1 - /// - /// Int8(exactly: "00000000000" as BitArray) // 0 - /// Int8(exactly: "00001111111" as BitArray) // 127 - /// Int8(exactly: "00010000000" as BitArray) // nil - /// Int8(exactly: "11101111111" as BitArray) // nil - /// Int8(exactly: "11110000000" as BitArray) // -128 - /// Int8(exactly: "11111111111" as BitArray) // -1 - @inlinable - public init?(exactly source: BitArray) { - let (value, isNegative) = Self._convert(source) - guard isNegative == (value < 0) else { return nil } - let words = value.words - var equal = true - _ = source._foreachTwosComplementWordDownward(isSigned: Self.isSigned) { i, word in - assert(equal) - let w = ( - i < words.count ? words[i] - : isNegative ? UInt.max - : UInt.zero) - equal = (w == word) - return equal - } - guard equal else { return nil } - self = value - } - - /// Creates a new instance from the bits in the given bit array, if the - /// corresponding integer value can be represented exactly. - /// If the value is not representable exactly, then a runtime error will - /// occur. - /// - /// If `Self` is an unsigned integer type, then the contents of the bit array - /// are interpreted to be the binary representation of a nonnegative - /// integer value. The bit array is allowed to contain bits in unrepresentable - /// positions, as long as they are all cleared. - /// - /// UInt8("" as BitArray) // 0 - /// UInt8("0" as BitArray) // 0 - /// UInt8("1" as BitArray) // 1 - /// UInt8("10" as BitArray) // 2 - /// UInt8("00000000" as BitArray) // 0 - /// UInt8("11111111" as BitArray) // 255 - /// UInt8("0000000000000" as BitArray) // 0 - /// UInt8("0000011111111" as BitArray) // 255 - /// UInt8("0000100000000" as BitArray) // ERROR - /// UInt8("1111111111111" as BitArray) // ERROR - /// - /// If `Self` is a signed integer type, then the contents of the bit array - /// are interpreted to be a two's complement representation of a signed - /// integer value, with the last bit in the array representing the sign of - /// the result. - /// - /// Int8("" as BitArray) // 0 - /// Int8("0" as BitArray) // 0 - /// Int8("1" as BitArray) // -1 - /// Int8("01" as BitArray) // 1 - /// Int8("101" as BitArray) // -3 - /// Int8("0101" as BitArray) // 5 - /// - /// Int8("00000001" as BitArray) // 1 - /// Int8("00000101" as BitArray) // 5 - /// Int8("01111111" as BitArray) // 127 - /// Int8("10000000" as BitArray) // -128 - /// Int8("11111111" as BitArray) // -1 - /// - /// Int8("00000000000" as BitArray) // 0 - /// Int8("00001111111" as BitArray) // 127 - /// Int8("00010000000" as BitArray) // ERROR - /// Int8("11101111111" as BitArray) // ERROR - /// Int8("11110000000" as BitArray) // -128 - /// Int8("11111111111" as BitArray) // -1 - @inlinable - public init(_ source: BitArray) { - guard let value = Self(exactly: source) else { - fatalError(""" - BitArray value cannot be converted to \(Self.self) because it is \ - outside the representable range - """) - } - self = value - } -} - extension BitArray { @usableFromInline internal func _foreachTwosComplementWordDownward( diff --git a/Sources/BitCollections/CMakeLists.txt b/Sources/BitCollections/CMakeLists.txt index 9ca7a2d4..39523e27 100644 --- a/Sources/BitCollections/CMakeLists.txt +++ b/Sources/BitCollections/CMakeLists.txt @@ -1,7 +1,7 @@ #[[ This source file is part of the Swift Collections Open Source Project -Copyright (c) 2022 - 2024 Apple Inc. and the Swift project authors +Copyright (c) 2022 - 2026 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 @@ -23,6 +23,7 @@ else() endif() target_sources(${module_name} PRIVATE + "BitArray/BinaryInteger extensions.swift" "BitArray/BitArray+BitwiseOperations.swift" "BitArray/BitArray+ChunkedBitsIterators.swift" "BitArray/BitArray+Codable.swift" diff --git a/Sources/Collections/BitCollections reexports.swift b/Sources/Collections/BitCollections reexports.swift new file mode 100644 index 00000000..ce1a1641 --- /dev/null +++ b/Sources/Collections/BitCollections reexports.swift @@ -0,0 +1,164 @@ +//===----------------------------------------------------------------------===// +// +// This source file is part of the Swift Collections open source project +// +// Copyright (c) 2021 - 2026 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 +// +//===----------------------------------------------------------------------===// + +#if !COLLECTIONS_SINGLE_MODULE +import BitCollections + +public typealias BitSet = BitCollections.BitSet +public typealias BitArray = BitCollections.BitArray + +extension BinaryInteger { + /// Creates a new instance by truncating or extending the bits in the given + /// bit array, as needed. The bit at position 0 in `source` will correspond + /// to the least-significant bit in the result. + /// + /// If `Self` is an unsigned integer type, then the result will contain as + /// many bits from `source` it can accommodate, truncating off any extras. + /// + /// UInt8(truncatingIfNeeded: "" as BitArray) // 0 + /// UInt8(truncatingIfNeeded: "0" as BitArray) // 0 + /// UInt8(truncatingIfNeeded: "1" as BitArray) // 1 + /// UInt8(truncatingIfNeeded: "11" as BitArray) // 3 + /// UInt8(truncatingIfNeeded: "11111111" as BitArray) // 255 + /// UInt8(truncatingIfNeeded: "1100000001" as BitArray) // 1 + /// UInt8(truncatingIfNeeded: "1100000101" as BitArray) // 5 + /// + /// If `Self` is a signed integer type, then the contents of the bit array + /// are interpreted to be a two's complement representation of a signed + /// integer value, with the last bit in the array representing the sign of + /// the result. + /// + /// Int8(truncatingIfNeeded: "" as BitArray) // 0 + /// Int8(truncatingIfNeeded: "0" as BitArray) // 0 + /// Int8(truncatingIfNeeded: "1" as BitArray) // -1 + /// Int8(truncatingIfNeeded: "01" as BitArray) // 1 + /// Int8(truncatingIfNeeded: "101" as BitArray) // -3 + /// Int8(truncatingIfNeeded: "0101" as BitArray) // 5 + /// + /// Int8(truncatingIfNeeded: "00000001" as BitArray) // 1 + /// Int8(truncatingIfNeeded: "00000101" as BitArray) // 5 + /// Int8(truncatingIfNeeded: "01111111" as BitArray) // 127 + /// Int8(truncatingIfNeeded: "10000000" as BitArray) // -128 + /// Int8(truncatingIfNeeded: "11111111" as BitArray) // -1 + /// + /// Int8(truncatingIfNeeded: "000011111111" as BitArray) // -1 + /// Int8(truncatingIfNeeded: "111100000000" as BitArray) // 0 + /// Int8(truncatingIfNeeded: "111100000001" as BitArray) // 1 + @inlinable + @inline(__always) + public init(truncatingIfNeeded source: BitArray) { + self = source._convertTruncating(to: Self.self) + } + + /// Creates a new instance from the bits in the given bit array, if the + /// corresponding integer value can be represented exactly. + /// If the value is not representable exactly, then the result is `nil`. + /// + /// If `Self` is an unsigned integer type, then the contents of the bit array + /// are interpreted to be the binary representation of a nonnegative + /// integer value. The bit array is allowed to contain bits in unrepresentable + /// positions, as long as they are all cleared. + /// + /// UInt8(exactly: "" as BitArray) // 0 + /// UInt8(exactly: "0" as BitArray) // 0 + /// UInt8(exactly: "1" as BitArray) // 1 + /// UInt8(exactly: "10" as BitArray) // 2 + /// UInt8(exactly: "00000000" as BitArray) // 0 + /// UInt8(exactly: "11111111" as BitArray) // 255 + /// UInt8(exactly: "0000000000000" as BitArray) // 0 + /// UInt8(exactly: "0000011111111" as BitArray) // 255 + /// UInt8(exactly: "0000100000000" as BitArray) // nil + /// UInt8(exactly: "1111111111111" as BitArray) // nil + /// + /// If `Self` is a signed integer type, then the contents of the bit array + /// are interpreted to be a two's complement representation of a signed + /// integer value, with the last bit in the array representing the sign of + /// the result. + /// + /// Int8(exactly: "" as BitArray) // 0 + /// Int8(exactly: "0" as BitArray) // 0 + /// Int8(exactly: "1" as BitArray) // -1 + /// Int8(exactly: "01" as BitArray) // 1 + /// Int8(exactly: "101" as BitArray) // -3 + /// Int8(exactly: "0101" as BitArray) // 5 + /// + /// Int8(exactly: "00000001" as BitArray) // 1 + /// Int8(exactly: "00000101" as BitArray) // 5 + /// Int8(exactly: "01111111" as BitArray) // 127 + /// Int8(exactly: "10000000" as BitArray) // -128 + /// Int8(exactly: "11111111" as BitArray) // -1 + /// + /// Int8(exactly: "00000000000" as BitArray) // 0 + /// Int8(exactly: "00001111111" as BitArray) // 127 + /// Int8(exactly: "00010000000" as BitArray) // nil + /// Int8(exactly: "11101111111" as BitArray) // nil + /// Int8(exactly: "11110000000" as BitArray) // -128 + /// Int8(exactly: "11111111111" as BitArray) // -1 + @inlinable + @inline(__always) + public init?(exactly source: BitArray) { + guard let v = source._convertExactly(to: Self.self) else { return nil } + self = v + } + + /// Creates a new instance from the bits in the given bit array, if the + /// corresponding integer value can be represented exactly. + /// If the value is not representable exactly, then a runtime error will + /// occur. + /// + /// If `Self` is an unsigned integer type, then the contents of the bit array + /// are interpreted to be the binary representation of a nonnegative + /// integer value. The bit array is allowed to contain bits in unrepresentable + /// positions, as long as they are all cleared. + /// + /// UInt8("" as BitArray) // 0 + /// UInt8("0" as BitArray) // 0 + /// UInt8("1" as BitArray) // 1 + /// UInt8("10" as BitArray) // 2 + /// UInt8("00000000" as BitArray) // 0 + /// UInt8("11111111" as BitArray) // 255 + /// UInt8("0000000000000" as BitArray) // 0 + /// UInt8("0000011111111" as BitArray) // 255 + /// UInt8("0000100000000" as BitArray) // ERROR + /// UInt8("1111111111111" as BitArray) // ERROR + /// + /// If `Self` is a signed integer type, then the contents of the bit array + /// are interpreted to be a two's complement representation of a signed + /// integer value, with the last bit in the array representing the sign of + /// the result. + /// + /// Int8("" as BitArray) // 0 + /// Int8("0" as BitArray) // 0 + /// Int8("1" as BitArray) // -1 + /// Int8("01" as BitArray) // 1 + /// Int8("101" as BitArray) // -3 + /// Int8("0101" as BitArray) // 5 + /// + /// Int8("00000001" as BitArray) // 1 + /// Int8("00000101" as BitArray) // 5 + /// Int8("01111111" as BitArray) // 127 + /// Int8("10000000" as BitArray) // -128 + /// Int8("11111111" as BitArray) // -1 + /// + /// Int8("00000000000" as BitArray) // 0 + /// Int8("00001111111" as BitArray) // 127 + /// Int8("00010000000" as BitArray) // ERROR + /// Int8("11101111111" as BitArray) // ERROR + /// Int8("11110000000" as BitArray) // -128 + /// Int8("11111111111" as BitArray) // -1 + @inlinable + @inline(__always) + public init(_ source: BitArray) { + self = source._convertForced(to: Self.self) + } +} + +#endif diff --git a/Sources/Collections/CMakeLists.txt b/Sources/Collections/CMakeLists.txt index 9f534a46..d49d7584 100644 --- a/Sources/Collections/CMakeLists.txt +++ b/Sources/Collections/CMakeLists.txt @@ -1,20 +1,24 @@ #[[ This source file is part of the Swift Collections Open Source Project -Copyright (c) 2021 - 2024 Apple Inc. and the Swift project authors +Copyright (c) 2021 - 2026 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 #]] add_library(Collections - "Collections.swift") + "BitCollections reexports.swift" + "DequeModule reexports.swift" + "HashTreeCollections reexports.swift" + "HeapModule reexports.swift" + "OrderedCollections reexports.swift") target_link_libraries(Collections PRIVATE BitCollections DequeModule + HashTreeCollections HeapModule OrderedCollections - HashTreeCollections ) set_target_properties(Collections PROPERTIES INTERFACE_INCLUDE_DIRECTORIES ${CMAKE_Swift_MODULE_DIRECTORY}) diff --git a/Sources/Collections/Collections.swift b/Sources/Collections/DequeModule reexports.swift similarity index 53% rename from Sources/Collections/Collections.swift rename to Sources/Collections/DequeModule reexports.swift index 33d5c8a1..6aad0586 100644 --- a/Sources/Collections/Collections.swift +++ b/Sources/Collections/DequeModule reexports.swift @@ -2,7 +2,7 @@ // // This source file is part of the Swift Collections open source project // -// Copyright (c) 2021 - 2024 Apple Inc. and the Swift project authors +// Copyright (c) 2021 - 2026 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 @@ -10,11 +10,7 @@ //===----------------------------------------------------------------------===// #if !COLLECTIONS_SINGLE_MODULE -@_exported import BitCollections -@_exported import DequeModule -@_exported import HashTreeCollections -@_exported import HeapModule -@_exported import OrderedCollections -// Note: _RopeModule is very intentionally not reexported, as its contents -// aren't part of this package's stable API surface (yet). +import DequeModule + +public typealias Deque = DequeModule.Deque #endif diff --git a/Sources/Collections/HashTreeCollections reexports.swift b/Sources/Collections/HashTreeCollections reexports.swift new file mode 100644 index 00000000..e83a2589 --- /dev/null +++ b/Sources/Collections/HashTreeCollections reexports.swift @@ -0,0 +1,17 @@ +//===----------------------------------------------------------------------===// +// +// This source file is part of the Swift Collections open source project +// +// Copyright (c) 2021 - 2026 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 +// +//===----------------------------------------------------------------------===// + +#if !COLLECTIONS_SINGLE_MODULE +import HashTreeCollections + +public typealias TreeSet = HashTreeCollections.TreeSet +public typealias TreeDictionary = HashTreeCollections.TreeDictionary +#endif diff --git a/Sources/Collections/HeapModule reexports.swift b/Sources/Collections/HeapModule reexports.swift new file mode 100644 index 00000000..a99ce48f --- /dev/null +++ b/Sources/Collections/HeapModule reexports.swift @@ -0,0 +1,16 @@ +//===----------------------------------------------------------------------===// +// +// This source file is part of the Swift Collections open source project +// +// Copyright (c) 2021 - 2026 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 +// +//===----------------------------------------------------------------------===// + +#if !COLLECTIONS_SINGLE_MODULE +import HeapModule + +public typealias Heap = HeapModule.Heap +#endif diff --git a/Sources/Collections/OrderedCollections reexports.swift b/Sources/Collections/OrderedCollections reexports.swift new file mode 100644 index 00000000..f64b9927 --- /dev/null +++ b/Sources/Collections/OrderedCollections reexports.swift @@ -0,0 +1,17 @@ +//===----------------------------------------------------------------------===// +// +// This source file is part of the Swift Collections open source project +// +// Copyright (c) 2021 - 2026 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 +// +//===----------------------------------------------------------------------===// + +#if !COLLECTIONS_SINGLE_MODULE +import OrderedCollections + +public typealias OrderedSet = OrderedCollections.OrderedSet +public typealias OrderedDictionary = OrderedCollections.OrderedDictionary +#endif diff --git a/Xcode/Collections.xcodeproj/project.pbxproj b/Xcode/Collections.xcodeproj/project.pbxproj index 0b54b6d1..19458997 100644 --- a/Xcode/Collections.xcodeproj/project.pbxproj +++ b/Xcode/Collections.xcodeproj/project.pbxproj @@ -108,6 +108,7 @@ 7DAD89B52E8B09EC00DD5786 /* Debugging.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7DAD89A82E8B09EC00DD5786 /* Debugging.swift */; }; 7DAD89B62E8B09EC00DD5786 /* UnsafeMutableRawBufferPointer+Extras.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7DAD89AE2E8B09EC00DD5786 /* UnsafeMutableRawBufferPointer+Extras.swift */; }; 7DAD89B72E8B09EC00DD5786 /* UnsafeRawBufferPointer+Extras.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7DAD89AF2E8B09EC00DD5786 /* UnsafeRawBufferPointer+Extras.swift */; }; + 7DE448452F2D3705005853A7 /* BinaryInteger extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7DE448442F2D36F9005853A7 /* BinaryInteger extensions.swift */; }; 7DE91B2F29CA6721004483EB /* Collections.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 7DE91B2629CA6721004483EB /* Collections.framework */; }; 7DE9200D29CA70F3004483EB /* OrderedDictionary+Equatable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7DE91E7529CA70F3004483EB /* OrderedDictionary+Equatable.swift */; }; 7DE9200E29CA70F3004483EB /* OrderedDictionary+ExpressibleByDictionaryLiteral.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7DE91E7629CA70F3004483EB /* OrderedDictionary+ExpressibleByDictionaryLiteral.swift */; }; @@ -465,7 +466,6 @@ 7DEBDB9029CCE44A00ADC226 /* TestContext.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7DEBDB2229CCE43600ADC226 /* TestContext.swift */; }; 7DEBDB9129CCE44A00ADC226 /* CollectionTestCase.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7DEBDB2429CCE43600ADC226 /* CollectionTestCase.swift */; }; 7DEBDB9229CCE44A00ADC226 /* StringConvertibleValue.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7DEBDB2B29CCE43600ADC226 /* StringConvertibleValue.swift */; }; - 7DEBDB9D29CCE73D00ADC226 /* Collections.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7DE91F7B29CA70F3004483EB /* Collections.swift */; }; A93AC2152E7C841700A824DF /* RigidArray.swift in Sources */ = {isa = PBXBuildFile; fileRef = A93AC2102E7C841700A824DF /* RigidArray.swift */; }; A93AC2162E7C841700A824DF /* UniqueArray.swift in Sources */ = {isa = PBXBuildFile; fileRef = A93AC2122E7C841700A824DF /* UniqueArray.swift */; }; A93AC2172E7C841700A824DF /* RigidArray+Experimental.swift in Sources */ = {isa = PBXBuildFile; fileRef = A93AC2112E7C841700A824DF /* RigidArray+Experimental.swift */; }; @@ -599,6 +599,12 @@ 7DAD89AD2E8B09EC00DD5786 /* UnsafeMutableBufferPointer+Extras.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UnsafeMutableBufferPointer+Extras.swift"; sourceTree = ""; }; 7DAD89AE2E8B09EC00DD5786 /* UnsafeMutableRawBufferPointer+Extras.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UnsafeMutableRawBufferPointer+Extras.swift"; sourceTree = ""; }; 7DAD89AF2E8B09EC00DD5786 /* UnsafeRawBufferPointer+Extras.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UnsafeRawBufferPointer+Extras.swift"; sourceTree = ""; }; + 7DE448442F2D36F9005853A7 /* BinaryInteger extensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "BinaryInteger extensions.swift"; sourceTree = ""; }; + 7DE448462F2D3940005853A7 /* BitCollections reexports.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "BitCollections reexports.swift"; sourceTree = ""; }; + 7DE448472F2D3984005853A7 /* DequeModule reexports.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "DequeModule reexports.swift"; sourceTree = ""; }; + 7DE448482F2D39E9005853A7 /* HashTreeCollections reexports.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "HashTreeCollections reexports.swift"; sourceTree = ""; }; + 7DE448492F2D3B48005853A7 /* HeapModule reexports.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "HeapModule reexports.swift"; sourceTree = ""; }; + 7DE4484A2F2D3B74005853A7 /* OrderedCollections reexports.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "OrderedCollections reexports.swift"; sourceTree = ""; }; 7DE91B2629CA6721004483EB /* Collections.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Collections.framework; sourceTree = BUILT_PRODUCTS_DIR; }; 7DE91B2E29CA6721004483EB /* CollectionsTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = CollectionsTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; }; 7DE91E7229CA70F3004483EB /* OrderedCollections.docc */ = {isa = PBXFileReference; lastKnownFileType = folder.documentationcatalog; path = OrderedCollections.docc; sourceTree = ""; }; @@ -801,7 +807,6 @@ 7DE91F7629CA70F3004483EB /* BitArray+Equatable.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "BitArray+Equatable.swift"; sourceTree = ""; }; 7DE91F7829CA70F3004483EB /* BitArray+Testing.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "BitArray+Testing.swift"; sourceTree = ""; }; 7DE91F7A29CA70F3004483EB /* CMakeLists.txt */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = CMakeLists.txt; sourceTree = ""; }; - 7DE91F7B29CA70F3004483EB /* Collections.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Collections.swift; sourceTree = ""; }; 7DE91F7C29CA70F3004483EB /* Collections.docc */ = {isa = PBXFileReference; lastKnownFileType = folder.documentationcatalog; path = Collections.docc; sourceTree = ""; }; 7DE91FA829CA70F3004483EB /* CMakeLists.txt */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = CMakeLists.txt; sourceTree = ""; }; 7DE91FA929CA70F3004483EB /* HashTreeCollections.docc */ = {isa = PBXFileReference; lastKnownFileType = folder.documentationcatalog; path = HashTreeCollections.docc; sourceTree = ""; }; @@ -1162,11 +1167,11 @@ 7DE91E7029CA70F3004483EB /* Sources */ = { isa = PBXGroup; children = ( + 7DE91F7929CA70F3004483EB /* Collections */, A93AC2142E7C841700A824DF /* BasicContainers */, 7DE91F2229CA70F3004483EB /* InternalCollectionsUtilities */, 7D48BCCE2E51524200D7F6F7 /* ContainersPreview */, 7DE91F3929CA70F3004483EB /* BitCollections */, - 7DE91F7929CA70F3004483EB /* Collections */, 7DE91EB629CA70F3004483EB /* DequeModule */, 7DE91FA729CA70F3004483EB /* HashTreeCollections */, 7DE9200229CA70F3004483EB /* HeapModule */, @@ -1578,6 +1583,7 @@ 7DE91F7029CA70F3004483EB /* BitArray+RangeReplaceableCollection.swift */, 7D9B859129E4F74400B291CD /* BitArray+Shifts.swift */, 7DE91F7829CA70F3004483EB /* BitArray+Testing.swift */, + 7DE448442F2D36F9005853A7 /* BinaryInteger extensions.swift */, ); path = BitArray; sourceTree = ""; @@ -1585,9 +1591,13 @@ 7DE91F7929CA70F3004483EB /* Collections */ = { isa = PBXGroup; children = ( - 7DE91F7B29CA70F3004483EB /* Collections.swift */, - 7DE91F7C29CA70F3004483EB /* Collections.docc */, + 7DE448462F2D3940005853A7 /* BitCollections reexports.swift */, + 7DE448472F2D3984005853A7 /* DequeModule reexports.swift */, + 7DE448482F2D39E9005853A7 /* HashTreeCollections reexports.swift */, + 7DE448492F2D3B48005853A7 /* HeapModule reexports.swift */, + 7DE4484A2F2D3B74005853A7 /* OrderedCollections reexports.swift */, 7DE91F7A29CA70F3004483EB /* CMakeLists.txt */, + 7DE91F7C29CA70F3004483EB /* Collections.docc */, ); path = Collections; sourceTree = ""; @@ -2259,7 +2269,6 @@ 7DE9208629CA70F4004483EB /* BigString+LosslessStringConvertible.swift in Sources */, 7DE9204329CA70F3004483EB /* _HashTable+Bucket.swift in Sources */, 7DE9216A29CA70F4004483EB /* TreeDictionary.swift in Sources */, - 7DEBDB9D29CCE73D00ADC226 /* Collections.swift in Sources */, 7DE9215C29CA70F4004483EB /* _Bitmap.swift in Sources */, 7DE9207029CA70F4004483EB /* BigString+Chunk+RopeElement.swift in Sources */, 7DE9213729CA70F4004483EB /* TreeSet+Sequence.swift in Sources */, @@ -2487,6 +2496,7 @@ 7DE9206129CA70F4004483EB /* BigString.swift in Sources */, 7DE9215A29CA70F4004483EB /* _HashNode+Structural subtracting.swift in Sources */, 7DE9217E29CA70F4004483EB /* Heap+ExpressibleByArrayLiteral.swift in Sources */, + 7DE448452F2D3705005853A7 /* BinaryInteger extensions.swift in Sources */, 7DE9204029CA70F3004483EB /* OrderedSet+Partial SetAlgebra isStrictSuperset.swift in Sources */, 7DE920BF29CA70F4004483EB /* BitSet+Codable.swift in Sources */, 7DE9203329CA70F3004483EB /* OrderedSet+Testing.swift in Sources */,