1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
|
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift Collections open source project
//
// Copyright (c) 2021 - 2024 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: Sequence {
/// The Boolean type representing the bit array's elements.
public typealias Element = Bool
/// The iterator type for a bit array.
public typealias Iterator = IndexingIterator<BitArray>
}
extension BitArray: RandomAccessCollection, MutableCollection {
/// The type representing a position in a bit array.
public typealias Index = Int
/// A collection representing a contiguous subrange of this collection's
/// elements. The subsequence shares indices with the original collection.
///
/// The subsequence type for bit arrays is the default `Slice`.
public typealias SubSequence = Slice<BitArray>
/// A type that represents the indices that are valid for subscripting the
/// collection, in ascending order.
public typealias Indices = Range<Int>
/// The number of elements in the bit array.
///
/// - Complexity: O(1)
@inlinable
public var count: Int {
Int(_count)
}
/// The position of the first element in a nonempty bit array, or `endIndex`
/// if the array is empty.
///
/// - Complexity: O(1)
@inlinable @inline(__always)
public var startIndex: Int { 0 }
/// The collection’s “past the end” position--that is, the position one step
/// after the last valid subscript argument.
///
/// - Complexity: O(1)
@inlinable @inline(__always)
public var endIndex: Int { count }
/// Returns the position immediately after the given index.
///
/// - Parameter `index`: A valid index of the bit set. `index` must be less than `endIndex`.
///
/// - Returns: The valid index immediately after `index`.
///
/// - Complexity: O(1)
@inlinable @inline(__always)
public func index(after i: Int) -> Int { i + 1 }
/// Returns the position immediately before the given index.
///
/// - Parameter `index`: A valid index of the bit set. `index` must be greater
/// than `startIndex`.
///
/// - Returns: The valid index immediately before `index`.
///
/// - Complexity: O(1)
@inlinable @inline(__always)
public func index(before i: Int) -> Int { i - 1 }
/// Replaces the given index with its successor.
///
/// - Parameter i: A valid index of the collection. `i` must be less than
/// `endIndex`.
///
/// - Complexity: O(1)
@inlinable @inline(__always)
public func formIndex(after i: inout Int) {
i += 1
}
/// Replaces the given index with its predecessor.
///
/// - Parameter i: A valid index of the collection. `i` must be greater than
/// `startIndex`.
///
/// - Complexity: O(1)
@inlinable @inline(__always)
public func formIndex(before i: inout Int) {
i -= 1
}
/// Returns an index that is the specified distance from the given index.
///
/// The value passed as `distance` must not offset `i` beyond the bounds of
/// the collection.
///
/// - Parameters:
/// - i: A valid index of the collection.
/// - distance: The distance to offset `i`.
/// - Returns: An index offset by `distance` from the index `i`. If
/// `distance` is positive, this is the same value as the result of
/// `distance` calls to `index(after:)`. If `distance` is negative, this
/// is the same value as the result of `abs(distance)` calls to
/// `index(before:)`.
///
/// - Complexity: O(1)
@inlinable @inline(__always)
public func index(_ i: Int, offsetBy distance: Int) -> Int {
i + distance
}
/// Returns an index that is the specified distance from the given index,
/// unless that distance is beyond a given limiting index.
///
/// The value passed as `distance` must not offset `i` beyond the bounds of
/// the collection, unless the index passed as `limit` prevents offsetting
/// beyond those bounds.
///
/// - Parameters:
/// - i: A valid index of the collection.
/// - distance: The distance to offset `i`.
/// - limit: A valid index of the collection to use as a limit. If
/// `distance > 0`, a limit that is less than `i` has no effect.
/// Likewise, if `distance < 0`, a limit that is greater than `i` has no
/// effect.
/// - Returns: An index offset by `distance` from the index `i`, unless that
/// index would be beyond `limit` in the direction of movement. In that
/// case, the method returns `nil`.
///
/// - Complexity: O(1)
@inlinable
public func index(
_ i: Index, offsetBy distance: Int, limitedBy limit: Index
) -> Index? {
let l = self.distance(from: i, to: limit)
if distance > 0 ? l >= 0 && l < distance : l <= 0 && distance < l {
return nil
}
return index(i, offsetBy: distance)
}
/// Returns the distance between two indices.
///
/// - Parameters:
/// - start: A valid index of the collection.
/// - end: Another valid index of the collection. If `end` is equal to
/// `start`, the result is zero.
/// - Returns: The distance between `start` and `end`.
///
/// - Complexity: O(1)
@inlinable @inline(__always)
public func distance(from start: Int, to end: Int) -> Int {
end - start
}
/// Accesses the element at the specified position.
///
/// You can subscript a collection with any valid index other than the
/// collection's end index. The end index refers to the position one past
/// the last element of a collection, so it doesn't correspond with an
/// element.
///
/// - Parameter position: The position of the element to access. `position`
/// must be a valid index of the collection that is not equal to the
/// `endIndex` property.
///
/// - Complexity: O(1)
public subscript(position: Int) -> Bool {
get {
precondition(position >= 0 && position < _count, "Index out of bounds")
return _read { handle in
handle[position]
}
}
set {
precondition(position >= 0 && position < _count, "Index out of bounds")
return _update { handle in
handle[position] = newValue
}
}
}
}
|