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

@@ -42,9 +42,7 @@ extension RigidDeque where Element: ~Copyable {
_ subrange: Range<Index>,
consumingWith consumer: (inout InputSpan<Element>) -> Void
) {
precondition(
subrange.lowerBound >= 0 && subrange.upperBound <= self.count,
"Subrange out of bounds")
_checkValidBounds(subrange)
let segments = self._handle.mutableSegments(forOffsets: subrange)
var span = InputSpan(buffer: segments.first, initializedCount: segments.first.count)
@@ -232,6 +230,7 @@ extension RigidDeque where Element: ~Copyable {
@inline(__always)
@_lifetime(&_base)
internal init(_base: inout RigidDeque, offsetRange: Range<Int>) {
_base._checkValidBounds(subrange)
let segments = _base._handle.mutableSegments(forOffsets: offsetRange)
self._buffer1 = segments.first
self._buffer2 = segments.second ?? .init(start: nil, count: 0)
@@ -93,7 +93,7 @@ extension RigidDeque where Element: ~Copyable {
@_alwaysEmitIntoClient
@_lifetime(borrow self)
public func nextSpan(after index: inout Int, maximumCount: Int) -> Span<Element> {
precondition(index >= 0 && index <= count, "Index out of bounds")
_checkValidIndex(index)
precondition(maximumCount > 0, "maximumCount must be positive")
let segment = self._handle
.nextSegment(from: index)
@@ -23,7 +23,7 @@ extension RigidDeque where Element: ~Copyable {
@_transparent
@_lifetime(borrow self)
public func borrowElement(at index: Int) -> Borrow<Element> {
precondition(index >= 0 && index < count, "Index out of bounds")
_checkItemIndex(index)
let slot = _handle.slot(forOffset: index)
return Borrow(unsafeAddress: _handle.ptr(at: slot), borrowing: self)
}
@@ -32,7 +32,7 @@ extension RigidDeque where Element: ~Copyable {
@_transparent
@_lifetime(&self)
public mutating func mutateElement(at index: Int) -> Inout<Element> {
precondition(index >= 0 && index < count, "Index out of bounds")
_checkItemIndex(index)
let slot = _handle.slot(forOffset: index)
return Inout(unsafeAddress: _handle.mutablePtr(at: slot), mutating: &self)
}
@@ -40,7 +40,7 @@ extension RigidDeque where Element: ~Copyable {
@inline(__always)
public mutating func insert(_ item: consuming Element, at index: Int) {
precondition(!isFull, "RigidDeque capacity overflow")
precondition(index >= 0 && index <= count, "Index out of bounds")
_checkValidIndex(index)
_handle.uncheckedInsert(item, at: index)
}
}
@@ -98,7 +98,7 @@ extension RigidDeque 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, "RigidDeque capacity overflow")
try _handle.uncheckedInsert(
@@ -32,7 +32,7 @@ extension RigidDeque where Element: ~Copyable {
@discardableResult
@_alwaysEmitIntoClient
public mutating func remove(at index: Int) -> Element {
precondition(index >= 0 && index < count, "Index out of bounds")
_checkItemIndex(index)
return _handle.uncheckedRemove(at: index)
}
@@ -64,9 +64,7 @@ extension RigidDeque where Element: ~Copyable {
addingCount newItemCount: Int,
initializingWith initializer: (inout OutputSpan<Element>) throws(E) -> Void
) throws(E) -> Void {
precondition(
subrange.lowerBound >= 0 && subrange.upperBound <= count,
"Subrange out of bounds")
_checkValidBounds(subrange)
precondition(newItemCount >= 0, "Cannot add a negative number of items")
precondition(
count - subrange.count + newItemCount <= capacity,
@@ -141,9 +139,7 @@ extension RigidDeque where Element: ~Copyable {
addingCount newItemCount: Int,
initializingWith initializer: (inout OutputSpan<Element>) throws(E) -> Void
) throws(E) -> Void {
precondition(
subrange.lowerBound >= 0 && subrange.upperBound <= count,
"Subrange out of bounds")
_checkValidBounds(subrange)
precondition(newItemCount >= 0, "Cannot add a negative number of items")
precondition(
count - subrange.count + newItemCount <= capacity,
@@ -234,20 +234,44 @@ extension RigidDeque where Element: ~Copyable {
@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: _handle.count),
"Index out of bounds")
}
@_alwaysEmitIntoClient
@_transparent
package func _checkValidIndex(_ index: Int) {
precondition(
UInt(bitPattern: index) <= UInt(bitPattern: _handle.count),
"Index out of bounds")
}
@_alwaysEmitIntoClient
@_transparent
package func _checkValidBounds(_ subrange: Range<Int>) {
precondition(
subrange.lowerBound >= 0 && subrange.upperBound <= _handle.count,
"Index range out of bounds")
}
@_alwaysEmitIntoClient
public subscript(position: Int) -> Element {
// FIXME: Replace with borrow/mutate accessors
@inline(__always)
@_transparent
unsafeAddress {
precondition(position >= 0 && position < count, "Index out of bounds")
_checkItemIndex(position)
let slot = _handle.slot(forOffset: position)
return _handle.ptr(at: slot)
}
@inline(__always)
@_transparent
unsafeMutableAddress {
precondition(position >= 0 && position < count, "Index out of bounds")
_checkItemIndex(position)
let slot = _handle.slot(forOffset: position)
return _handle.mutablePtr(at: slot)
}
@@ -267,9 +291,8 @@ extension RigidDeque 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)
_handle.uncheckedSwapAt(i, j)
}
}
@@ -41,7 +41,7 @@ extension UniqueDeque where Element: ~Copyable {
@_alwaysEmitIntoClient
@_transparent
public mutating func insert(_ item: consuming Element, at index: Int) {
precondition(index >= 0 && index <= self.count, "Index out of bounds")
_storage._checkValidIndex(index)
_ensureFreeCapacity(1)
_storage._handle.uncheckedInsert(item, at: index)
}
@@ -102,7 +102,7 @@ extension UniqueDeque 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")
_storage._checkValidIndex(index)
precondition(newItemCount >= 0, "Cannot add a negative number of items")
_ensureFreeCapacity(newItemCount)
try _storage._handle.uncheckedInsert(
@@ -78,9 +78,7 @@ extension UniqueDeque where Element: ~Copyable {
addingCount newItemCount: Int,
initializingWith initializer: (inout OutputSpan<Element>) throws(E) -> Void
) throws(E) -> Void {
precondition(
subrange.lowerBound >= 0 && subrange.upperBound <= count,
"Subrange out of bounds")
_storage._checkValidBounds(subrange)
precondition(newItemCount >= 0, "Cannot add a negative number of items")
_ensureFreeCapacity(newItemCount - subrange.count)
try _storage._handle.uncheckedReplace(
@@ -148,9 +146,7 @@ extension UniqueDeque where Element: ~Copyable {
addingCount newItemCount: Int,
initializingWith initializer: (inout OutputSpan<Element>) throws(E) -> Void
) throws(E) -> Void {
precondition(
subrange.lowerBound >= 0 && subrange.upperBound <= count,
"Subrange out of bounds")
_storage._checkValidBounds(subrange)
precondition(newItemCount >= 0, "Cannot add a negative number of items")
_ensureFreeCapacity(newItemCount - subrange.count)
try _storage._handle.uncheckedReplace(