mirror of
https://github.com/apple/swift-collections
synced 2026-11-30 05:18:44 +00:00
Simplify containers even more
This commit is contained in:
14 files changed
+228
-54
No files matched your search
@@ -21,7 +21,14 @@ extension Deque: RandomAccessContainer {
|
||||
|
||||
@available(SwiftCompatibilitySpan 5.0, *)
|
||||
@lifetime(borrow self)
|
||||
public func nextSpan(after index: inout Int, maximumCount: Int) -> Span<Element> {
|
||||
_storage.value.nextSpan(after: &index, maximumCount: maximumCount)
|
||||
public func nextSpan(after index: inout Int) -> Span<Element> {
|
||||
_storage.value.nextSpan(after: &index)
|
||||
}
|
||||
|
||||
@available(SwiftCompatibilitySpan 5.0, *)
|
||||
@lifetime(borrow self)
|
||||
public func previousSpan(before index: inout Int) -> Span<Element> {
|
||||
_storage.value.previousSpan(before: &index)
|
||||
}
|
||||
|
||||
}
|
||||
@@ -35,8 +35,14 @@ extension DynamicDeque: @unchecked Sendable where Element: Sendable & ~Copyable
|
||||
extension DynamicDeque: RandomAccessContainer where Element: ~Copyable {
|
||||
@available(SwiftCompatibilitySpan 5.0, *)
|
||||
@lifetime(borrow self)
|
||||
public func nextSpan(after index: inout Int, maximumCount: Int) -> Span<Element> {
|
||||
_storage.nextSpan(after: &index, maximumCount: maximumCount)
|
||||
public func nextSpan(after index: inout Int) -> Span<Element> {
|
||||
_storage.nextSpan(after: &index)
|
||||
}
|
||||
|
||||
@available(SwiftCompatibilitySpan 5.0, *)
|
||||
@lifetime(borrow self)
|
||||
public func previousSpan(before index: inout Int) -> Span<Element> {
|
||||
_storage.previousSpan(before: &index)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -82,17 +82,24 @@ extension RigidDeque where Element: ~Copyable {
|
||||
extension RigidDeque: RandomAccessContainer, MutableContainer where Element: ~Copyable {
|
||||
@inlinable
|
||||
@lifetime(borrow self)
|
||||
public func nextSpan(after index: inout Int, maximumCount: Int) -> Span<Element> {
|
||||
let slots = _handle.slotRange(following: &index, maximumCount: maximumCount)
|
||||
public func nextSpan(after index: inout Int) -> Span<Element> {
|
||||
let slots = _handle.slotRange(following: &index)
|
||||
return _span(over: slots)
|
||||
}
|
||||
|
||||
@inlinable
|
||||
@lifetime(borrow self)
|
||||
public func previousSpan(before index: inout Int) -> Span<Element> {
|
||||
let slots = _handle.slotRange(preceding: &index)
|
||||
return _span(over: slots)
|
||||
}
|
||||
|
||||
@inlinable
|
||||
@lifetime(&self)
|
||||
public mutating func nextMutableSpan(
|
||||
after index: inout Int, maximumCount: Int
|
||||
after index: inout Int
|
||||
) -> MutableSpan<Element> {
|
||||
let slots = _handle.slotRange(following: &index, maximumCount: maximumCount)
|
||||
let slots = _handle.slotRange(following: &index)
|
||||
return _mutableSpan(over: slots)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -386,24 +386,40 @@ extension _UnsafeDequeHandle where Element: ~Copyable {
|
||||
|
||||
extension _UnsafeDequeHandle where Element: ~Copyable {
|
||||
@inlinable
|
||||
internal func slotRange(following offset: inout Int, maximumCount: Int) -> Range<Slot> {
|
||||
precondition(offset >= 0 && offset <= count, "Invalid index")
|
||||
precondition(maximumCount > 0, "maximumCount must be positive")
|
||||
internal func slotRange(following offset: inout Int) -> Range<Slot> {
|
||||
precondition(offset >= 0 && offset <= count, "Index out of bounds")
|
||||
guard _buffer.baseAddress != nil else {
|
||||
return Range(uncheckedBounds: (Slot.zero, Slot.zero))
|
||||
}
|
||||
let wrapOffset = Swift.min(capacity - startSlot.position, count)
|
||||
|
||||
if offset < wrapOffset {
|
||||
let endOffset = offset + Swift.min(wrapOffset - offset, maximumCount)
|
||||
defer { offset += endOffset - offset }
|
||||
defer { offset += wrapOffset - offset }
|
||||
return Range(
|
||||
uncheckedBounds: (startSlot.advanced(by: offset), startSlot.advanced(by: endOffset)))
|
||||
uncheckedBounds: (startSlot.advanced(by: offset), startSlot.advanced(by: wrapOffset)))
|
||||
}
|
||||
let endOffset = offset + Swift.min(count - offset, maximumCount)
|
||||
let lowerSlot = Slot.zero.advanced(by: offset - wrapOffset)
|
||||
let upperSlot = lowerSlot.advanced(by: endOffset - wrapOffset)
|
||||
defer { offset += endOffset - offset }
|
||||
let upperSlot = lowerSlot.advanced(by: count - wrapOffset)
|
||||
defer { offset += count - offset }
|
||||
return Range(uncheckedBounds: (lower: lowerSlot, upper: upperSlot))
|
||||
}
|
||||
|
||||
@inlinable
|
||||
internal func slotRange(preceding offset: inout Int) -> Range<Slot> {
|
||||
precondition(offset >= 0 && offset <= count, "Index out of bounds")
|
||||
guard _buffer.baseAddress != nil else {
|
||||
return Range(uncheckedBounds: (Slot.zero, Slot.zero))
|
||||
}
|
||||
let wrapOffset = Swift.min(capacity - startSlot.position, count)
|
||||
|
||||
if offset <= wrapOffset {
|
||||
defer { offset = 0 }
|
||||
return Range(
|
||||
uncheckedBounds: (startSlot, startSlot.advanced(by: offset)))
|
||||
}
|
||||
let lowerSlot = Slot.zero
|
||||
let upperSlot = lowerSlot.advanced(by: offset - wrapOffset)
|
||||
defer { offset = wrapOffset }
|
||||
return Range(uncheckedBounds: (lower: lowerSlot, upper: upperSlot))
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user