Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BitArray API refinements & additions #263

Merged
merged 11 commits into from
Apr 11, 2023
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// This source file is part of the Swift Collections open source project
//
// Copyright (c) 2021 Apple Inc. and the Swift project authors
// Copyright (c) 2021-2023 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
Expand Down Expand Up @@ -114,16 +114,50 @@ extension BitArray {
/// - Complexity: O(value.count)
public static prefix func ~(value: Self) -> Self {
var result = value
result._update { handle in
result.toggleAll()
return result
}

public mutating func toggleAll() {
_update { handle in
let w = handle._mutableWords
for i in 0 ..< handle._words.count {
handle._mutableWords[i].formComplement()
w[i].formComplement()
}
let p = _BitPosition(handle._count)
let p = handle.end
if p.bit > 0 {
handle._mutableWords[p.word].subtract(_Word(upTo: p.bit).complement())
w[p.word].subtract(_Word(upTo: p.bit).complement())
}
}
result._checkInvariants()
return result
_checkInvariants()
}

public mutating func toggleAll(in range: Range<Int>) {
precondition(range.upperBound <= count, "Range out of bounds")
_update { handle in
let words = handle._mutableWords
let start = _BitPosition(range.lowerBound)
let end = _BitPosition(range.upperBound)
if start.word == end.word {
let bits = _Word(from: start.bit, to: end.bit)
words[start.word].formSymmetricDifference(bits)
return
}
words[start.word].formSymmetricDifference(
_Word(upTo: start.bit).complement())
for i in stride(from: start.word + 1, to: end.word, by: 1) {
words[i].formComplement()
}
if end.bit > 0 {
words[end.word].formSymmetricDifference(_Word(upTo: end.bit))
}
}
}

@inlinable
public mutating func toggleAll<R: RangeExpression>(
in range: R
) where R.Bound == Int {
toggleAll(in: range.relative(to: self))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// This source file is part of the Swift Collections open source project
//
// Copyright (c) 2021-2022 Apple Inc. and the Swift project authors
// Copyright (c) 2021-2023 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
Expand Down
2 changes: 1 addition & 1 deletion Sources/BitCollections/BitArray/BitArray+Codable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// This source file is part of the Swift Collections open source project
//
// Copyright (c) 2022 Apple Inc. and the Swift project authors
// Copyright (c) 2022-2023 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// This source file is part of the Swift Collections open source project
//
// Copyright (c) 2021-2022 Apple Inc. and the Swift project authors
// Copyright (c) 2021-2023 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
Expand Down
2 changes: 1 addition & 1 deletion Sources/BitCollections/BitArray/BitArray+Copy.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// This source file is part of the Swift Collections open source project
//
// Copyright (c) 2021-2022 Apple Inc. and the Swift project authors
// Copyright (c) 2021-2023 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// This source file is part of the Swift Collections open source project
//
// Copyright (c) 2022 Apple Inc. and the Swift project authors
// Copyright (c) 2022-2023 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
Expand Down

This file was deleted.

63 changes: 63 additions & 0 deletions Sources/BitCollections/BitArray/BitArray+Descriptions.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift Collections open source project
//
// Copyright (c) 2022-2023 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 UInt8 {
@inline(__always)
internal static var _ascii0: Self { 48 }

@inline(__always)
internal static var _ascii1: Self { 49 }
}

extension BitArray: CustomStringConvertible {
/// A textual representation of this instance.
/// Bit arrays print themselves as a string of binary bits, with the highest-indexed elements
/// appearing first, as in the binary representation of integers:
///
/// let bits: BitArray = [false, false, false, true, true]
/// print(bits) // "11000"
///
/// The description of an empty bit array is an empty string.
public var description: String {
_bitString
}
}

extension BitArray: CustomDebugStringConvertible {
/// A textual representation of this instance, suitable for debugging.
public var debugDescription: String {
"BitArray(\(_bitString))"
}
}

extension BitArray {
internal var _bitString: String {
guard !isEmpty else { return "" }
var result: String
if #available(macOS 11.0, iOS 14.0, watchOS 7.0, tvOS 14.0, *) {
result = String(unsafeUninitializedCapacity: self.count) { target in
var i = count - 1
for v in self {
target.initializeElement(at: i, to: v ? ._ascii1 : ._ascii0)
i &-= 1
}
return count
}
} else {
result = ""
result.reserveCapacity(self.count)
for v in self.reversed() {
result.append(v ? "1" : "0")
}
}
return result
}
}
2 changes: 1 addition & 1 deletion Sources/BitCollections/BitArray/BitArray+Equatable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// This source file is part of the Swift Collections open source project
//
// Copyright (c) 2021 Apple Inc. and the Swift project authors
// Copyright (c) 2021-2023 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// This source file is part of the Swift Collections open source project
//
// Copyright (c) 2021-2022 Apple Inc. and the Swift project authors
// Copyright (c) 2021-2023 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,20 @@
//
// This source file is part of the Swift Collections open source project
//
// Copyright (c) 2022 Apple Inc. and the Swift project authors
// Copyright (c) 2023 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: CustomDebugStringConvertible {
/// A textual representation of this instance, suitable for debugging.
public var debugDescription: String {
"BitArray(\(_bitString))"
extension BitArray: ExpressibleByStringLiteral {
/// Creates an instance initialized with the given elements.
@inlinable
public init(stringLiteral value: String) {
guard let bits = Self(value) else {
fatalError("Invalid bit array literal")
}
self = bits
}
}
23 changes: 22 additions & 1 deletion Sources/BitCollections/BitArray/BitArray+Extras.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// This source file is part of the Swift Collections open source project
//
// Copyright (c) 2022 Apple Inc. and the Swift project authors
// Copyright (c) 2022-2023 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
Expand All @@ -28,3 +28,24 @@ extension BitArray {
}
}
}

extension BitArray {
public mutating func insert(
repeating value: Bool,
count: Int,
at index: Int
) {
precondition(count >= 0, "Can't insert a negative number of values")
precondition(index >= 0 && index <= count, "Index out of bounds")
guard count > 0 else { return }
_extend(by: count, with: false)
_copy(from: index ..< self.count - count, to: index + count)
fill(in: index ..< index + count, with: value)
}

public mutating func append(repeating value: Bool, count: Int) {
precondition(count >= 0, "Can't append a negative number of values")
guard count > 0 else { return }
_extend(by: count, with: value)
}
}
9 changes: 8 additions & 1 deletion Sources/BitCollections/BitArray/BitArray+Fill.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// This source file is part of the Swift Collections open source project
//
// Copyright (c) 2021-2022 Apple Inc. and the Swift project authors
// Copyright (c) 2021-2023 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
Expand Down Expand Up @@ -31,4 +31,11 @@ extension BitArray {
}
}
}

public mutating func fill<R: RangeExpression>(
in range: R,
with value: Bool = true
) where R.Bound == Int {
fill(in: range.relative(to: self), with: value)
}
}
2 changes: 1 addition & 1 deletion Sources/BitCollections/BitArray/BitArray+Hashable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// This source file is part of the Swift Collections open source project
//
// Copyright (c) 2021 Apple Inc. and the Swift project authors
// Copyright (c) 2021-2023 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
Expand Down
Loading