mirror of
https://github.com/apple/swift-collections
synced 2026-11-27 04:54:21 +00:00
[BitCollections] Adopt some instead of classic generic where possible
This commit is contained in:
26 files changed
+234
-229
No files matched your search
@@ -187,9 +187,7 @@ extension BitArray {
|
||||
}
|
||||
|
||||
@inlinable
|
||||
public mutating func toggleAll<R: RangeExpression>(
|
||||
in range: R
|
||||
) where R.Bound == Int {
|
||||
public mutating func toggleAll(in range: some RangeExpression<Int>) {
|
||||
toggleAll(in: range.relative(to: self))
|
||||
}
|
||||
}
|
||||
@@ -151,10 +151,10 @@ extension BitArray._UnsafeHandle {
|
||||
}
|
||||
|
||||
extension BitArray._UnsafeHandle {
|
||||
internal mutating func copy<S: Sequence>(
|
||||
from source: S,
|
||||
internal mutating func copy(
|
||||
from source: some Sequence<Bool>,
|
||||
to range: Range<Int>
|
||||
) where S.Element == Bool {
|
||||
) {
|
||||
assert(range.lowerBound >= 0 && range.upperBound <= self.count)
|
||||
var pos = _BitPosition(range.lowerBound)
|
||||
var it = source.makeIterator()
|
||||
|
||||
@@ -32,10 +32,15 @@ extension BitArray {
|
||||
}
|
||||
}
|
||||
|
||||
public mutating func fill<R: RangeExpression>(
|
||||
in range: R,
|
||||
/// Set every bit of this array within the specified range to `value`
|
||||
/// (`true` by default).
|
||||
///
|
||||
/// - Parameter range: The range whose elements to overwrite.
|
||||
/// - Parameter value: The Boolean value to which to set the array's elements.
|
||||
public mutating func fill(
|
||||
in range: some RangeExpression<Int>,
|
||||
with value: Bool = true
|
||||
) where R.Bound == Int {
|
||||
) {
|
||||
fill(in: range.relative(to: self), with: value)
|
||||
}
|
||||
}
|
||||
@@ -42,7 +42,7 @@ extension BitArray {
|
||||
/// BitArray(bitPattern: -1 as Int) // 1111111111...1111111111
|
||||
///
|
||||
/// - Complexity: O(value.bitWidth)
|
||||
public init<I: BinaryInteger>(bitPattern value: I) {
|
||||
public init(bitPattern value: some BinaryInteger) {
|
||||
var words = value.words.map { _Word($0) }
|
||||
let count = value.bitWidth
|
||||
if words.isEmpty {
|
||||
|
||||
@@ -32,7 +32,7 @@ extension BitArray: LosslessStringConvertible {
|
||||
self = bits
|
||||
}
|
||||
|
||||
internal init?<C: Collection>(_utf8 utf8: C) where C.Element == UInt8 {
|
||||
internal init?(_utf8 utf8: some Collection<UInt8>) {
|
||||
let c = utf8.count
|
||||
self.init(repeating: false, count: c)
|
||||
var i = c &- 1
|
||||
|
||||
@@ -24,9 +24,9 @@ extension BitArray {
|
||||
///
|
||||
/// - Parameter count: The number of random bits to generate.
|
||||
/// - Parameter rng: The random number generator to use.
|
||||
public static func randomBits<R: RandomNumberGenerator>(
|
||||
public static func randomBits(
|
||||
count: Int,
|
||||
using rng: inout R
|
||||
using rng: inout some RandomNumberGenerator
|
||||
) -> BitArray {
|
||||
precondition(count >= 0, "Invalid count")
|
||||
guard count > 0 else { return BitArray() }
|
||||
|
||||
@@ -9,6 +9,10 @@
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
#if !COLLECTIONS_SINGLE_MODULE
|
||||
import _CollectionsUtilities
|
||||
#endif
|
||||
|
||||
extension BitArray: RangeReplaceableCollection {}
|
||||
|
||||
extension BitArray {
|
||||
@@ -57,14 +61,14 @@ extension BitArray {
|
||||
/// - Complexity: O(*count*) where *count* is the number of values in
|
||||
/// `elements`.
|
||||
@inlinable
|
||||
public init<S: Sequence>(_ elements: S) where S.Element == Bool {
|
||||
public init(_ elements: some Sequence<Bool>) {
|
||||
defer { _checkInvariants() }
|
||||
if S.self == BitArray.self {
|
||||
self = elements as! BitArray
|
||||
if let elements = _specialize(elements, for: BitArray.self) {
|
||||
self.init(elements)
|
||||
return
|
||||
}
|
||||
if S.self == BitArray.SubSequence.self {
|
||||
self.init(elements as! BitArray.SubSequence)
|
||||
if let elements = _specialize(elements, for: BitArray.SubSequence.self) {
|
||||
self.init(elements)
|
||||
return
|
||||
}
|
||||
self.init()
|
||||
@@ -80,6 +84,7 @@ extension BitArray {
|
||||
/// `elements` must be finite.
|
||||
/// - Complexity: O(*count*) where *count* is the number of values in
|
||||
/// `elements`.
|
||||
@inlinable
|
||||
public init(_ elements: BitArray) {
|
||||
self = elements
|
||||
}
|
||||
@@ -143,16 +148,18 @@ extension BitArray {
|
||||
/// *m* is the length of `newElements`. If the call to this method simply
|
||||
/// appends the contents of `newElements` to the collection, this method is
|
||||
/// equivalent to `append(contentsOf:)`.
|
||||
public mutating func replaceSubrange<C: Collection>(
|
||||
public mutating func replaceSubrange(
|
||||
_ range: Range<Int>,
|
||||
with newElements: __owned C
|
||||
) where C.Element == Bool {
|
||||
with newElements: __owned some Collection<Bool>
|
||||
) {
|
||||
let c = newElements.count
|
||||
_prepareForReplaceSubrange(range, replacementCount: c)
|
||||
if C.self == BitArray.self {
|
||||
_copy(from: newElements as! BitArray, to: range.lowerBound)
|
||||
} else if C.self == BitArray.SubSequence.self {
|
||||
_copy(from: newElements as! BitArray.SubSequence, to: range.lowerBound)
|
||||
if let newElements = _specialize(newElements, for: BitArray.self) {
|
||||
_copy(from: newElements, to: range.lowerBound)
|
||||
} else if let newElements = _specialize(
|
||||
newElements, for: BitArray.SubSequence.self
|
||||
) {
|
||||
_copy(from: newElements, to: range.lowerBound)
|
||||
} else {
|
||||
_copy(from: newElements, to: range.lowerBound ..< range.lowerBound + c)
|
||||
}
|
||||
@@ -259,15 +266,17 @@ extension BitArray {
|
||||
///
|
||||
/// - Complexity: O(*m*), where *m* is the length of `newElements`, if
|
||||
/// `self` is the only copy of this bit array. Otherwise O(`count` + *m*).
|
||||
public mutating func append<S: Sequence>(
|
||||
contentsOf newElements: __owned S
|
||||
) where S.Element == Bool {
|
||||
if S.self == BitArray.self {
|
||||
self.append(contentsOf: newElements as! BitArray)
|
||||
public mutating func append(
|
||||
contentsOf newElements: __owned some Sequence<Bool>
|
||||
) {
|
||||
if let newElements = _specialize(newElements, for: BitArray.self) {
|
||||
self.append(contentsOf: newElements)
|
||||
return
|
||||
}
|
||||
if S.self == BitArray.SubSequence.self {
|
||||
self.append(contentsOf: newElements as! BitArray.SubSequence)
|
||||
if let newElements = _specialize(
|
||||
newElements, for: BitArray.SubSequence.self
|
||||
) {
|
||||
self.append(contentsOf: newElements)
|
||||
return
|
||||
}
|
||||
var it = newElements.makeIterator()
|
||||
@@ -372,20 +381,22 @@ extension BitArray {
|
||||
///
|
||||
/// - Complexity: O(`self.count` + `newElements.count`).
|
||||
/// If `i == endIndex`, this method is equivalent to `append(contentsOf:)`.
|
||||
public mutating func insert<C: Collection>(
|
||||
contentsOf newElements: __owned C,
|
||||
public mutating func insert(
|
||||
contentsOf newElements: __owned some Collection<Bool>,
|
||||
at i: Int
|
||||
) where C.Element == Bool {
|
||||
) {
|
||||
precondition(i >= 0 && i <= count)
|
||||
let c = newElements.count
|
||||
guard c > 0 else { return }
|
||||
_extend(by: c)
|
||||
_copy(from: i ..< count - c, to: i + c)
|
||||
|
||||
if C.self == BitArray.self {
|
||||
_copy(from: newElements as! BitArray, to: i)
|
||||
} else if C.self == BitArray.SubSequence.self {
|
||||
_copy(from: newElements as! BitArray.SubSequence, to: i)
|
||||
if let newElements = _specialize(newElements, for: BitArray.self) {
|
||||
_copy(from: newElements, to: i)
|
||||
} else if let newElements = _specialize(
|
||||
newElements, for: BitArray.SubSequence.self
|
||||
) {
|
||||
_copy(from: newElements, to: i)
|
||||
} else {
|
||||
_copy(from: newElements, to: i ..< i + c)
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user