From 621374557ce25cc915c656ced3e0da8a71fe3627 Mon Sep 17 00:00:00 2001 From: Karoy Lorentey Date: Mon, 16 Feb 2026 17:05:36 -0800 Subject: [PATCH] Array, Deque types: Introduce _checkItemIndex, _checkValidIndex, _checkValidBounds --- .../RigidArray/RigidArray+Consumption.swift | 4 +-- .../RigidArray/RigidArray+Container.swift | 35 +++++++++++++++---- .../RigidArray/RigidArray+Experimental.swift | 4 +-- .../RigidArray/RigidArray+Insertions.swift | 6 ++-- .../RigidArray/RigidArray+Removals.swift | 6 ++-- .../RigidArray/RigidArray+Replacements.swift | 8 ++--- .../UniqueArray+Replacements.swift | 8 ++--- .../RigidDeque/RigidDeque+Consumption.swift | 5 ++- .../RigidDeque/RigidDeque+Container.swift | 2 +- .../RigidDeque/RigidDeque+Experimental.swift | 4 +-- .../RigidDeque/RigidDeque+Insertions.swift | 4 +-- .../RigidDeque/RigidDeque+Removals.swift | 2 +- .../RigidDeque/RigidDeque+Replacements.swift | 8 ++--- .../DequeModule/RigidDeque/RigidDeque.swift | 33 ++++++++++++++--- .../UniqueDeque/UniqueDeque+Insertions.swift | 4 +-- .../UniqueDeque+Replacements.swift | 8 ++--- 16 files changed, 83 insertions(+), 58 deletions(-) diff --git a/Sources/BasicContainers/RigidArray/RigidArray+Consumption.swift b/Sources/BasicContainers/RigidArray/RigidArray+Consumption.swift index d5321cf5..39ff9bb9 100644 --- a/Sources/BasicContainers/RigidArray/RigidArray+Consumption.swift +++ b/Sources/BasicContainers/RigidArray/RigidArray+Consumption.swift @@ -35,9 +35,7 @@ extension RigidArray where Element: ~Copyable { _ subrange: Range, consumingWith consumer: (inout InputSpan) -> Void ) { - precondition( - subrange.lowerBound >= 0 && subrange.upperBound <= _count, - "Subrange out of bounds") + _checkValidBounds(subrange) guard !subrange.isEmpty else { var span = InputSpan() consumer(&span) diff --git a/Sources/BasicContainers/RigidArray/RigidArray+Container.swift b/Sources/BasicContainers/RigidArray/RigidArray+Container.swift index 3e389e63..be5fc946 100644 --- a/Sources/BasicContainers/RigidArray/RigidArray+Container.swift +++ b/Sources/BasicContainers/RigidArray/RigidArray+Container.swift @@ -88,13 +88,37 @@ extension RigidArray where Element: ~Copyable { @inlinable @inline(__always) public var indices: Range { 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) { + 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 { - 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 { - 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 { - 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) diff --git a/Sources/BasicContainers/RigidArray/RigidArray+Experimental.swift b/Sources/BasicContainers/RigidArray/RigidArray+Experimental.swift index 214e0b44..cdfcd455 100644 --- a/Sources/BasicContainers/RigidArray/RigidArray+Experimental.swift +++ b/Sources/BasicContainers/RigidArray/RigidArray+Experimental.swift @@ -22,7 +22,7 @@ extension RigidArray where Element: ~Copyable { @inlinable @_lifetime(borrow self) public func borrowElement(at index: Int) -> Borrow { - 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 { - precondition(index >= 0 && index < _count) + _checkItemIndex(index) return unsafe Mut( unsafeAddress: _storage.baseAddress.unsafelyUnwrapped.advanced(by: index), mutating: &self diff --git a/Sources/BasicContainers/RigidArray/RigidArray+Insertions.swift b/Sources/BasicContainers/RigidArray/RigidArray+Insertions.swift index 722dc2b9..55b505fb 100644 --- a/Sources/BasicContainers/RigidArray/RigidArray+Insertions.swift +++ b/Sources/BasicContainers/RigidArray/RigidArray+Insertions.swift @@ -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) 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, 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) diff --git a/Sources/BasicContainers/RigidArray/RigidArray+Removals.swift b/Sources/BasicContainers/RigidArray/RigidArray+Removals.swift index 0b60b87c..4ee28a3f 100644 --- a/Sources/BasicContainers/RigidArray/RigidArray+Removals.swift +++ b/Sources/BasicContainers/RigidArray/RigidArray+Removals.swift @@ -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) { - 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) diff --git a/Sources/BasicContainers/RigidArray/RigidArray+Replacements.swift b/Sources/BasicContainers/RigidArray/RigidArray+Replacements.swift index 7b833944..4b4da101 100644 --- a/Sources/BasicContainers/RigidArray/RigidArray+Replacements.swift +++ b/Sources/BasicContainers/RigidArray/RigidArray+Replacements.swift @@ -64,9 +64,7 @@ extension RigidArray where Element: ~Copyable { addingCount newItemCount: Int, initializingWith initializer: (inout OutputSpan) 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) 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, diff --git a/Sources/BasicContainers/UniqueArray/UniqueArray+Replacements.swift b/Sources/BasicContainers/UniqueArray/UniqueArray+Replacements.swift index 5d5c977e..aa6af999 100644 --- a/Sources/BasicContainers/UniqueArray/UniqueArray+Replacements.swift +++ b/Sources/BasicContainers/UniqueArray/UniqueArray+Replacements.swift @@ -65,9 +65,7 @@ extension UniqueArray where Element: ~Copyable { addingCount newItemCount: Int, initializingWith initializer: (inout OutputSpan) 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) 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( diff --git a/Sources/DequeModule/RigidDeque/RigidDeque+Consumption.swift b/Sources/DequeModule/RigidDeque/RigidDeque+Consumption.swift index 05bc0695..0a19a35f 100644 --- a/Sources/DequeModule/RigidDeque/RigidDeque+Consumption.swift +++ b/Sources/DequeModule/RigidDeque/RigidDeque+Consumption.swift @@ -42,9 +42,7 @@ extension RigidDeque where Element: ~Copyable { _ subrange: Range, consumingWith consumer: (inout InputSpan) -> 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) { + _base._checkValidBounds(subrange) let segments = _base._handle.mutableSegments(forOffsets: offsetRange) self._buffer1 = segments.first self._buffer2 = segments.second ?? .init(start: nil, count: 0) diff --git a/Sources/DequeModule/RigidDeque/RigidDeque+Container.swift b/Sources/DequeModule/RigidDeque/RigidDeque+Container.swift index 92810ed6..16ed1ee5 100644 --- a/Sources/DequeModule/RigidDeque/RigidDeque+Container.swift +++ b/Sources/DequeModule/RigidDeque/RigidDeque+Container.swift @@ -93,7 +93,7 @@ extension RigidDeque where Element: ~Copyable { @_alwaysEmitIntoClient @_lifetime(borrow self) public func nextSpan(after index: inout Int, maximumCount: Int) -> Span { - 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) diff --git a/Sources/DequeModule/RigidDeque/RigidDeque+Experimental.swift b/Sources/DequeModule/RigidDeque/RigidDeque+Experimental.swift index cea19d9d..250ab5d3 100644 --- a/Sources/DequeModule/RigidDeque/RigidDeque+Experimental.swift +++ b/Sources/DequeModule/RigidDeque/RigidDeque+Experimental.swift @@ -23,7 +23,7 @@ extension RigidDeque where Element: ~Copyable { @_transparent @_lifetime(borrow self) public func borrowElement(at index: Int) -> Borrow { - 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 { - 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) } diff --git a/Sources/DequeModule/RigidDeque/RigidDeque+Insertions.swift b/Sources/DequeModule/RigidDeque/RigidDeque+Insertions.swift index 238e974f..bb530f91 100644 --- a/Sources/DequeModule/RigidDeque/RigidDeque+Insertions.swift +++ b/Sources/DequeModule/RigidDeque/RigidDeque+Insertions.swift @@ -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) 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( diff --git a/Sources/DequeModule/RigidDeque/RigidDeque+Removals.swift b/Sources/DequeModule/RigidDeque/RigidDeque+Removals.swift index a5fdf033..38f3048b 100644 --- a/Sources/DequeModule/RigidDeque/RigidDeque+Removals.swift +++ b/Sources/DequeModule/RigidDeque/RigidDeque+Removals.swift @@ -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) } diff --git a/Sources/DequeModule/RigidDeque/RigidDeque+Replacements.swift b/Sources/DequeModule/RigidDeque/RigidDeque+Replacements.swift index 8c6a1df5..cc789a05 100644 --- a/Sources/DequeModule/RigidDeque/RigidDeque+Replacements.swift +++ b/Sources/DequeModule/RigidDeque/RigidDeque+Replacements.swift @@ -64,9 +64,7 @@ extension RigidDeque where Element: ~Copyable { addingCount newItemCount: Int, initializingWith initializer: (inout OutputSpan) 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) 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, diff --git a/Sources/DequeModule/RigidDeque/RigidDeque.swift b/Sources/DequeModule/RigidDeque/RigidDeque.swift index caace267..d92efbb3 100644 --- a/Sources/DequeModule/RigidDeque/RigidDeque.swift +++ b/Sources/DequeModule/RigidDeque/RigidDeque.swift @@ -234,20 +234,44 @@ extension RigidDeque where Element: ~Copyable { @inline(__always) public var indices: Range { 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) { + 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) } } diff --git a/Sources/DequeModule/UniqueDeque/UniqueDeque+Insertions.swift b/Sources/DequeModule/UniqueDeque/UniqueDeque+Insertions.swift index bdb014e7..f0723b3a 100644 --- a/Sources/DequeModule/UniqueDeque/UniqueDeque+Insertions.swift +++ b/Sources/DequeModule/UniqueDeque/UniqueDeque+Insertions.swift @@ -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) 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( diff --git a/Sources/DequeModule/UniqueDeque/UniqueDeque+Replacements.swift b/Sources/DequeModule/UniqueDeque/UniqueDeque+Replacements.swift index bec74b5b..82963e7a 100644 --- a/Sources/DequeModule/UniqueDeque/UniqueDeque+Replacements.swift +++ b/Sources/DequeModule/UniqueDeque/UniqueDeque+Replacements.swift @@ -78,9 +78,7 @@ extension UniqueDeque where Element: ~Copyable { addingCount newItemCount: Int, initializingWith initializer: (inout OutputSpan) 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) 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(