diff --git a/Sources/BasicContainers/RigidArray/RigidArray+Container.swift b/Sources/BasicContainers/RigidArray/RigidArray+Container.swift index 6aa3b7e4..6ff71645 100644 --- a/Sources/BasicContainers/RigidArray/RigidArray+Container.swift +++ b/Sources/BasicContainers/RigidArray/RigidArray+Container.swift @@ -157,7 +157,7 @@ extension RigidArray where Element: ~Copyable { let p = _storage.baseAddress.unsafelyUnwrapped.advanced(by: index) return UnsafePointer(p) } - + @inlinable @inline(__always) internal mutating func _mutablePtr( to index: Int @@ -165,7 +165,8 @@ extension RigidArray where Element: ~Copyable { _checkItemIndex(index) return _storage.baseAddress.unsafelyUnwrapped.advanced(by: index) } - + +#if compiler(>=6.4) /// Accesses the element at the specified position. /// /// - Parameter position: The position of the element to access. @@ -186,6 +187,26 @@ extension RigidArray where Element: ~Copyable { &_mutablePtr(to: position).pointee } } +#else + /// Accesses the element at the specified position. + /// + /// - Parameter position: The position of the element to access. + /// The position must be a valid index of the array that is not equal + /// to the `endIndex` property. + /// + /// - Complexity: O(1) + @inlinable + public subscript(position: Int) -> Element { + @inline(__always) + unsafeAddress { + _ptr(to: position) + } + @inline(__always) + unsafeMutableAddress { + _mutablePtr(to: position) + } + } +#endif } @available(SwiftStdlib 5.0, *) diff --git a/Sources/BasicContainers/UniqueArray/UniqueArray+Container.swift b/Sources/BasicContainers/UniqueArray/UniqueArray+Container.swift index 03a596c4..3f6a5127 100644 --- a/Sources/BasicContainers/UniqueArray/UniqueArray+Container.swift +++ b/Sources/BasicContainers/UniqueArray/UniqueArray+Container.swift @@ -117,6 +117,7 @@ extension UniqueArray where Element: ~Copyable { @inline(__always) public var indices: Range { _storage.indices } +#if compiler(>=6.4) /// Accesses the element at the specified position. /// /// - Parameter position: The position of the element to access. @@ -136,6 +137,26 @@ extension UniqueArray where Element: ~Copyable { &_storage[position] } } +#else + /// Accesses the element at the specified position. + /// + /// - Parameter position: The position of the element to access. + /// The position must be a valid index of the array that is not equal + /// to the `endIndex` property. + /// + /// - Complexity: O(1) + @inlinable + public subscript(position: Int) -> Element { + @inline(__always) + unsafeAddress { + _storage._ptr(to: position) + } + @inline(__always) + unsafeMutableAddress { + _storage._mutablePtr(to: position) + } + } +#endif } @available(SwiftStdlib 5.0, *) diff --git a/Sources/ContainersPreview/Types/InputSpan.swift b/Sources/ContainersPreview/Types/InputSpan.swift index 1bc52c51..61bcd472 100644 --- a/Sources/ContainersPreview/Types/InputSpan.swift +++ b/Sources/ContainersPreview/Types/InputSpan.swift @@ -237,6 +237,7 @@ extension InputSpan where Element: ~Copyable { unsafe Range(uncheckedBounds: (0, _count)) } +#if compiler(>=6.4) /// Accesses the element at the specified position. /// /// - Parameter index: A valid index into this span. @@ -257,6 +258,26 @@ extension InputSpan where Element: ~Copyable { return unsafe &_unsafeAddressOfElement(uncheckedOffset: index).pointee } } +#else + /// Accesses the element at the specified position. + /// + /// - Parameter index: A valid index into this span. + /// + /// - Complexity: O(1) + @_alwaysEmitIntoClient + public subscript(_ index: Index) -> Element { + unsafeAddress { + precondition(indices.contains(index), "Index out of bounds") + return unsafe UnsafePointer(_unsafeAddressOfElement(uncheckedOffset: index)) + } + + @_lifetime(self: copy self) + unsafeMutableAddress { + precondition(indices.contains(index), "Index out of bounds") + return unsafe _unsafeAddressOfElement(uncheckedOffset: index) + } + } +#endif /// Accesses the element at the specified position. /// diff --git a/Sources/DequeModule/RigidDeque/RigidDeque.swift b/Sources/DequeModule/RigidDeque/RigidDeque.swift index a7e90e53..95a48a60 100644 --- a/Sources/DequeModule/RigidDeque/RigidDeque.swift +++ b/Sources/DequeModule/RigidDeque/RigidDeque.swift @@ -260,6 +260,7 @@ extension RigidDeque where Element: ~Copyable { "Index range out of bounds") } +#if compiler(>=6.4) @_alwaysEmitIntoClient public subscript(position: Int) -> Element { @inline(__always) @@ -279,6 +280,25 @@ extension RigidDeque where Element: ~Copyable { return &_handle.mutablePtr(at: slot).pointee } } +#else + @_alwaysEmitIntoClient + public subscript(position: Int) -> Element { + @inline(__always) + @_transparent + unsafeAddress { + _checkItemIndex(position) + let slot = _handle.slot(forOffset: position) + return _handle.ptr(at: slot) + } + @inline(__always) + @_transparent + unsafeMutableAddress { + _checkItemIndex(position) + let slot = _handle.slot(forOffset: position) + return _handle.mutablePtr(at: slot) + } + } +#endif } @available(SwiftStdlib 5.0, *) diff --git a/Sources/DequeModule/UniqueDeque/UniqueDeque.swift b/Sources/DequeModule/UniqueDeque/UniqueDeque.swift index 5d26bc64..bbf13983 100644 --- a/Sources/DequeModule/UniqueDeque/UniqueDeque.swift +++ b/Sources/DequeModule/UniqueDeque/UniqueDeque.swift @@ -194,6 +194,7 @@ extension UniqueDeque where Element: ~Copyable { @inline(__always) public var indices: Range { unsafe Range(uncheckedBounds: (0, count)) } +#if compiler(>=6.4) @_alwaysEmitIntoClient public subscript(position: Int) -> Element { @inline(__always) @@ -208,6 +209,25 @@ extension UniqueDeque where Element: ~Copyable { &_storage[position] } } +#else + @_alwaysEmitIntoClient + public subscript(position: Int) -> Element { + @inline(__always) + @_transparent + unsafeAddress { + precondition(position >= 0 && position < count, "Index out of bounds") + let slot = _storage._handle.slot(forOffset: position) + return _storage._handle.ptr(at: slot) + } + @inline(__always) + @_transparent + unsafeMutableAddress { + precondition(position >= 0 && position < count, "Index out of bounds") + let slot = _storage._handle.slot(forOffset: position) + return _storage._handle.mutablePtr(at: slot) + } + } +#endif } @available(SwiftStdlib 5.0, *)