Array, Deque types: Introduce _checkItemIndex, _checkValidIndex, _checkValidBounds

This commit is contained in:
Karoy Lorentey committed 2026-02-16 17:05:36 -08:00
1 parent ac7a8bf61b
commit 621374557c
16 files changed
+83 -58

No files matched your search

@@ -35,9 +35,7 @@ extension RigidArray where Element: ~Copyable {
_ subrange: Range<Int>,
consumingWith consumer: (inout InputSpan<Element>) -> Void
) {
precondition(
subrange.lowerBound >= 0 && subrange.upperBound <= _count,
"Subrange out of bounds")
_checkValidBounds(subrange)
guard !subrange.isEmpty else {
var span = InputSpan<Element>()
consumer(&span)
@@ -88,13 +88,37 @@ extension RigidArray where Element: ~Copyable {
@inlinable
@inline(__always)
public var indices: Range<Int> { unsafe Range(uncheckedBounds: (0, count)) }
@_alwaysEmitIntoClient
@_transparent
package func _checkItemIndex(_ index: Int) {
precondition(
UInt(bitPattern: index) < UInt(bitPattern: _count),
"Index out of bounds")
}
@_alwaysEmitIntoClient
@_transparent
package func _checkValidIndex(_ index: Int) {
precondition(
UInt(bitPattern: index) <= UInt(bitPattern: _count),
"Index out of bounds")
}
@_alwaysEmitIntoClient
@_transparent
package func _checkValidBounds(_ subrange: Range<Int>) {
precondition(
subrange.lowerBound >= 0 && subrange.upperBound <= _count,
"Index range out of bounds")
}
}
@available(SwiftStdlib 5.0, *)
extension RigidArray where Element: ~Copyable {
@inlinable @inline(__always)
internal func _ptr(to index: Int) -> UnsafePointer<Element> {
precondition(index >= 0 && index < _count, "Index out of bounds")
_checkItemIndex(index)
let p = _storage.baseAddress.unsafelyUnwrapped.advanced(by: index)
return UnsafePointer(p)
}
@@ -103,7 +127,7 @@ extension RigidArray where Element: ~Copyable {
internal mutating func _mutablePtr(
to index: Int
) -> UnsafeMutablePointer<Element> {
precondition(index >= 0 && index < _count, "Index out of bounds")
_checkItemIndex(index)
return _storage.baseAddress.unsafelyUnwrapped.advanced(by: index)
}
@@ -140,9 +164,8 @@ extension RigidArray where Element: ~Copyable {
/// - Complexity: O(1)
@inlinable
public mutating func swapAt(_ i: Int, _ j: Int) {
precondition(
i >= 0 && i < _count && j >= 0 && j < _count,
"Index out of bounds")
_checkItemIndex(i)
_checkItemIndex(j)
unsafe _items.swapAt(i, j)
}
}
@@ -310,7 +333,7 @@ extension RigidArray where Element: ~Copyable {
public func nextSpan(
after index: inout Int, maximumCount: Int
) -> Span<Element> {
precondition(UInt(bitPattern: index) <= _count, "Index out of bounds")
_checkValidIndex(index)
precondition(maximumCount > 0, "maximumCount must be positive")
let start = index
index = start &+ Swift.min(maximumCount, _count &- start)
@@ -22,7 +22,7 @@ extension RigidArray where Element: ~Copyable {
@inlinable
@_lifetime(borrow self)
public func borrowElement(at index: Int) -> Borrow<Element> {
precondition(index >= 0 && index < _count, "Index out of bounds")
_checkItemIndex(index)
return unsafe Borrow(
unsafeAddress: _storage.baseAddress.unsafelyUnwrapped.advanced(by: index),
borrowing: self
@@ -37,7 +37,7 @@ extension RigidArray where Element: ~Copyable {
@inlinable
@_lifetime(&self)
public mutating func mutateElement(at index: Int) -> Mut<Element> {
precondition(index >= 0 && index < _count)
_checkItemIndex(index)
return unsafe Mut(
unsafeAddress: _storage.baseAddress.unsafelyUnwrapped.advanced(by: index),
mutating: &self
@@ -37,7 +37,7 @@ extension RigidArray where Element: ~Copyable {
/// - Complexity: O(`self.count`)
@inlinable
public mutating func insert(_ item: consuming Element, at index: Int) {
precondition(index >= 0 && index <= count, "Index out of bounds")
_checkValidIndex(index)
precondition(!isFull, "RigidArray capacity overflow")
if index < count {
let source = unsafe _storage.extracting(index ..< count)
@@ -99,7 +99,7 @@ extension RigidArray where Element: ~Copyable {
at index: Int,
initializingWith initializer: (inout OutputSpan<Element>) throws(E) -> Void
) throws(E) {
precondition(index >= 0 && index <= self.count, "Index out of bounds")
_checkValidIndex(index)
precondition(newItemCount >= 0, "Cannot add a negative number of items")
precondition(newItemCount <= freeCapacity, "RigidArray capacity overflow")
let target = unsafe _openGap(at: index, count: newItemCount)
@@ -381,7 +381,7 @@ extension RigidArray {
copying items: some Collection<Element>,
newCount: Int
) {
precondition(index >= 0 && index <= _count, "Index out of bounds")
_checkValidIndex(index)
precondition(newCount <= freeCapacity, "RigidArray capacity overflow")
let gap = unsafe _openGap(at: index, count: newCount)
@@ -31,7 +31,7 @@ extension RigidArray where Element: ~Copyable {
@inlinable
@discardableResult
public mutating func remove(at index: Int) -> Element {
precondition(index >= 0 && index < _count, "Index out of bounds")
_checkItemIndex(index)
let old = unsafe _storage.moveElement(from: index)
_closeGap(at: index, count: 1)
_count -= 1
@@ -97,9 +97,7 @@ extension RigidArray where Element: ~Copyable {
/// - Complexity: O(`count`)
@inlinable
public mutating func removeSubrange(_ bounds: Range<Int>) {
precondition(
bounds.lowerBound >= 0 && bounds.upperBound <= _count,
"Subrange out of bounds")
_checkValidBounds(bounds)
guard !bounds.isEmpty else { return }
unsafe _storage.extracting(bounds).deinitialize()
_closeGap(at: bounds.lowerBound, count: bounds.count)
@@ -64,9 +64,7 @@ extension RigidArray where Element: ~Copyable {
addingCount newItemCount: Int,
initializingWith initializer: (inout OutputSpan<Element>) throws(E) -> Void
) throws(E) -> Void {
precondition(
subrange.lowerBound >= 0 && subrange.upperBound <= _count,
"Index range out of bounds")
_checkValidBounds(subrange)
precondition(newItemCount >= 0, "Cannot add a negative number of items")
precondition(
newItemCount - subrange.count <= freeCapacity,
@@ -158,9 +156,7 @@ extension RigidArray where Element: ~Copyable {
addingCount newItemCount: Int,
initializingWith initializer: (inout OutputSpan<Element>) throws(E) -> Void
) throws(E) -> Void {
precondition(
subrange.lowerBound >= 0 && subrange.upperBound <= _count,
"Index range out of bounds")
_checkValidBounds(subrange)
precondition(newItemCount >= 0, "Cannot add a negative number of items")
precondition(
newItemCount - subrange.count <= freeCapacity,
@@ -65,9 +65,7 @@ extension UniqueArray where Element: ~Copyable {
addingCount newItemCount: Int,
initializingWith initializer: (inout OutputSpan<Element>) throws(E) -> Void
) throws(E) -> Void {
precondition(
subrange.lowerBound >= 0 && subrange.upperBound <= self.count,
"Index range out of bounds")
_storage._checkValidBounds(subrange)
precondition(newItemCount >= 0, "Cannot add a negative number of items")
// FIXME: Avoid moving the subsequent elements twice on resize.
_ensureFreeCapacity(newItemCount - subrange.count)
@@ -136,9 +134,7 @@ extension UniqueArray where Element: ~Copyable {
addingCount newItemCount: Int,
initializingWith initializer: (inout OutputSpan<Element>) throws(E) -> Void
) throws(E) -> Void {
precondition(
subrange.lowerBound >= 0 && subrange.upperBound <= self.count,
"Index range out of bounds")
_storage._checkValidBounds(subrange)
precondition(newItemCount >= 0, "Cannot add a negative number of items")
_ensureFreeCapacity(newItemCount - subrange.count)
try _storage._uncheckedReplace(