From 84e99324783ca0faad6f851f3f593510281a69f3 Mon Sep 17 00:00:00 2001 From: Karoy Lorentey Date: Wed, 4 Feb 2026 17:31:47 -0800 Subject: [PATCH] Rigid/Unique types: Review consumption methods and unify their shapes Drain-returning variants will need to be added on RigidArray/UniqueArray later. --- .../RigidArray/RigidArray+Consumption.swift | 130 ++++++++++++++++++ .../RigidArray/RigidArray+Removals.swift | 79 ----------- .../UniqueArray/UniqueArray+Consumption.swift | 112 +++++++++++++++ .../UniqueArray/UniqueArray+Removals.swift | 53 ------- .../UniqueDeque/UniqueDeque+Consumption.swift | 121 ++++++++++++++++ 5 files changed, 363 insertions(+), 132 deletions(-) create mode 100644 Sources/BasicContainers/RigidArray/RigidArray+Consumption.swift create mode 100644 Sources/BasicContainers/UniqueArray/UniqueArray+Consumption.swift diff --git a/Sources/BasicContainers/RigidArray/RigidArray+Consumption.swift b/Sources/BasicContainers/RigidArray/RigidArray+Consumption.swift new file mode 100644 index 00000000..d5321cf5 --- /dev/null +++ b/Sources/BasicContainers/RigidArray/RigidArray+Consumption.swift @@ -0,0 +1,130 @@ +//===----------------------------------------------------------------------===// +// +// This source file is part of the Swift Collections open source project +// +// Copyright (c) 2024 - 2026 Apple Inc. and the Swift project authors +// Licensed under Apache License v2.0 with Runtime Library Exception +// +// See https://swift.org/LICENSE.txt for license information +// +//===----------------------------------------------------------------------===// + +#if !COLLECTIONS_SINGLE_MODULE +import InternalCollectionsUtilities +import ContainersPreview +#endif + +#if compiler(>=6.2) + +#if COLLECTIONS_UNSTABLE_CONTAINERS_PREVIEW +@available(SwiftStdlib 5.0, *) +extension RigidArray where Element: ~Copyable { + /// Remove the specified subrange of items from this array, + /// passing an input span to the given function to consume them in place. + /// + /// - Parameter subrange: The subrange of items to consume from this array. + /// - Parameter consumer: A function taking an input span of the removed items, + /// allowing them to be consumed straight out of the array's storage. + /// The function is not required to consume all items in the span; + /// however, the span's remaining items will still be removed from + /// the array. + /// + /// - Complexity: O(`self.count`) + @_alwaysEmitIntoClient + public mutating func consume( + _ subrange: Range, + consumingWith consumer: (inout InputSpan) -> Void + ) { + precondition( + subrange.lowerBound >= 0 && subrange.upperBound <= _count, + "Subrange out of bounds") + guard !subrange.isEmpty else { + var span = InputSpan() + consumer(&span) + return + } + let buffer = unsafe _storage.extracting(subrange) + var span = InputSpan(buffer: buffer, initializedCount: buffer.count) + + consumer(&span) + _ = consume span + + _closeGap(at: subrange.lowerBound, count: subrange.count) + _count -= subrange.count + } + + /// Remove the specified subrange of items from this deque, + /// passing a series of input spans to a given callback function to consume + /// them in place. + /// + /// The callback is not required to fully consume the contents of its + /// argument; any items it leaves in the input span get automatically + /// consumed. The underlying storage isn't necessarily contiguous, so + /// the callback may be called more than once, whether or not it fully + /// consumes its input. If the specified range is empty, the callback + /// may not be called at all. + /// + /// - Parameter subrange: The subrange of items to consume from this array. + /// - Parameter consumer: A function taking an input span of the removed items, + /// allowing them to be consumed straight out of the array's storage. + /// The function is called at most once. + /// + /// - Complexity: O(`self.count`) + @_alwaysEmitIntoClient + @inline(__always) + public mutating func consume>( + _ subrange: R, + consumingWith consumer: (inout InputSpan) -> Void + ) { + consume(subrange.relative(to: indices), consumingWith: consumer) + } + + /// Remove all items currently in this array, passing an input + /// span to a given callback function to consume them in place. + /// + /// The callback is not required to fully consume the contents of its + /// argument; any items it leaves in the input span get automatically + /// consumed. + /// + /// - Parameter consumer: A function taking an input span of the removed items, + /// allowing them to be consumed straight out of the array's storage. + /// The function is called at most once. + /// + /// - Complexity: O(`self.count`) + @_alwaysEmitIntoClient + public mutating func consumeAll( + consumingWith consumer: (inout InputSpan) -> Void + ) { + consume(indices, consumingWith: consumer) + } + + /// Remove the specified number of items from the end of this array, + /// passing an input span to a given callback function to consume them in + /// place. + /// + /// The callback is not required to fully consume the contents of its + /// argument; any items it leaves in the input span get automatically + /// consumed. + /// + /// - Parameter n: The number of items to consume from the end of the array. + /// `n` must be greater than or equal to zero and must not exceed + /// the count of the array. + /// - Parameter consumer: A function taking an input span of the removed items, + /// allowing them to be consumed straight out of the array's storage. + /// The function is called at most once. + /// - Complexity: O(`n`) + @inline(__always) + @_alwaysEmitIntoClient + public mutating func consumeLast( + _ n: Int, + consumingWith consumer: (inout InputSpan) -> Void + ) { + precondition( + n >= 0 && n <= _count, + "Count of elements to consume is out of bounds") + self.consume(_count &- n ..< _count, consumingWith: consumer) + } +} +#endif + +#endif diff --git a/Sources/BasicContainers/RigidArray/RigidArray+Removals.swift b/Sources/BasicContainers/RigidArray/RigidArray+Removals.swift index 1ff2d247..0b60b87c 100644 --- a/Sources/BasicContainers/RigidArray/RigidArray+Removals.swift +++ b/Sources/BasicContainers/RigidArray/RigidArray+Removals.swift @@ -135,83 +135,4 @@ extension RigidArray where Element: ~Copyable { } } -#if COLLECTIONS_UNSTABLE_CONTAINERS_PREVIEW -@available(SwiftStdlib 5.0, *) -extension RigidArray where Element: ~Copyable { - /// Remove all items currently in this array, returning an input span to - /// allow them to be consumed in place. - /// - /// The input span extends the exclusive mutating access initiated by this - /// operation to cover the span's lifetime. Once the span is destroyed, - /// the array becomes empty, but it preserves its original storage capacity. - /// - /// - Complexity: O(*n*), where *n* is the original count of the array. - @_lifetime(&self) - @_alwaysEmitIntoClient - public mutating func consumeAll() -> InputSpan { - let span = InputSpan(buffer: _items, initializedCount: self.count) - self._count = 0 - return _overrideLifetime(span, mutating: &self) - } - - /// Remove the specified number of items from the end of this array, - /// returning an input span to allow them to be consumed in place. - /// The input span extends the exclusive mutating access initiated by this - /// operation to cover the span's lifetime. - /// - /// - Parameter n: The number of items to consume from the end of the array. - /// `n` must be greater than or equal to zero and must not exceed - /// the count of the array. - /// - /// - Complexity: O(`n`) - @_lifetime(&self) - @_alwaysEmitIntoClient - public mutating func consumeLast(_ n: Int) -> InputSpan { - precondition( - n >= 0 && n <= _count, - "Count of elements to consume is out of bounds") - self._count &-= n - let span = InputSpan( - buffer: self._storage._extracting(first: n), - initializedCount: n) - return _overrideLifetime(span, mutating: &self) - } - - /// Remove the specified subrange of items from this array, - /// passing an input span to the given function to consume them in place. - /// - /// - Parameter bounds: The subrange of items to consume from this array. - /// - Parameter consumer: A function taking an input span of the removed items, - /// allowing them to be consumed straight out of the array's storage. - /// The function is not required to consume all items in the span; - /// however, the span's remaining items will still be removed from - /// the array. - /// - /// - Complexity: O(`self.count`) - @_alwaysEmitIntoClient - public mutating func consumeSubrange( - _ bounds: Range, - consumingWith consumer: (inout InputSpan) throws(E) -> Result - ) throws(E) -> Result { - precondition( - bounds.lowerBound >= 0 && bounds.upperBound <= _count, - "Subrange out of bounds") - guard !bounds.isEmpty else { - var span = InputSpan() - return try consumer(&span) - } - let buffer = unsafe _storage.extracting(bounds) - var span = InputSpan(buffer: buffer, initializedCount: buffer.count) - defer { - let remainder = span.finalize(for: buffer) - buffer._extracting(last: remainder).deinitialize() - _closeGap(at: bounds.lowerBound, count: bounds.count) - _count -= bounds.count - span = InputSpan() - } - return try consumer(&span) - } -} -#endif - #endif diff --git a/Sources/BasicContainers/UniqueArray/UniqueArray+Consumption.swift b/Sources/BasicContainers/UniqueArray/UniqueArray+Consumption.swift new file mode 100644 index 00000000..56ce174e --- /dev/null +++ b/Sources/BasicContainers/UniqueArray/UniqueArray+Consumption.swift @@ -0,0 +1,112 @@ +//===----------------------------------------------------------------------===// +// +// This source file is part of the Swift Collections open source project +// +// Copyright (c) 2024 - 2026 Apple Inc. and the Swift project authors +// Licensed under Apache License v2.0 with Runtime Library Exception +// +// See https://swift.org/LICENSE.txt for license information +// +//===----------------------------------------------------------------------===// + +#if !COLLECTIONS_SINGLE_MODULE +import InternalCollectionsUtilities +import ContainersPreview +#endif + +#if compiler(>=6.2) + +#if COLLECTIONS_UNSTABLE_CONTAINERS_PREVIEW +@available(SwiftStdlib 5.0, *) +extension UniqueArray where Element: ~Copyable { + /// Remove the specified subrange of items from this array, + /// passing an input span to the given function to consume them in place. + /// + /// - Parameter subrange: The subrange of items to consume from this array. + /// - Parameter consumer: A function taking an input span of the removed items, + /// allowing them to be consumed straight out of the array's storage. + /// The function is not required to consume all items in the span; + /// however, the span's remaining items will still be removed from + /// the array. + /// + /// - Complexity: O(`self.count`) + @_alwaysEmitIntoClient + public mutating func consume( + _ subrange: Range, + consumingWith consumer: (inout InputSpan) -> Void + ) { + _storage.consume(subrange, consumingWith: consumer) + } + + /// Remove the specified subrange of items from this deque, + /// passing a series of input spans to a given callback function to consume + /// them in place. + /// + /// The callback is not required to fully consume the contents of its + /// argument; any items it leaves in the input span get automatically + /// consumed. The underlying storage isn't necessarily contiguous, so + /// the callback may be called more than once, whether or not it fully + /// consumes its input. If the specified range is empty, the callback + /// may not be called at all. + /// + /// - Parameter subrange: The subrange of items to consume from this array. + /// - Parameter consumer: A function taking an input span of the removed items, + /// allowing them to be consumed straight out of the array's storage. + /// The function is called at most once. + /// + /// - Complexity: O(`self.count`) + @_alwaysEmitIntoClient + @inline(__always) + public mutating func consume>( + _ subrange: R, + consumingWith consumer: (inout InputSpan) -> Void + ) { + _storage.consume(subrange.relative(to: indices), consumingWith: consumer) + } + + /// Remove all items currently in this array, passing an input + /// span to a given callback function to consume them in place. + /// + /// The callback is not required to fully consume the contents of its + /// argument; any items it leaves in the input span get automatically + /// consumed. + /// + /// - Parameter consumer: A function taking an input span of the removed items, + /// allowing them to be consumed straight out of the array's storage. + /// The function is called at most once. + /// + /// - Complexity: O(`self.count`) + @_alwaysEmitIntoClient + public mutating func consumeAll( + consumingWith consumer: (inout InputSpan) -> Void + ) { + _storage.consume(indices, consumingWith: consumer) + } + + /// Remove the specified number of items from the end of this array, + /// passing an input span to a given callback function to consume them in + /// place. + /// + /// The callback is not required to fully consume the contents of its + /// argument; any items it leaves in the input span get automatically + /// consumed. + /// + /// - Parameter n: The number of items to consume from the end of the array. + /// `n` must be greater than or equal to zero and must not exceed + /// the count of the array. + /// - Parameter consumer: A function taking an input span of the removed items, + /// allowing them to be consumed straight out of the array's storage. + /// The function is called at most once. + /// - Complexity: O(`n`) + @inline(__always) + @_alwaysEmitIntoClient + public mutating func consumeLast( + _ n: Int, + consumingWith consumer: (inout InputSpan) -> Void + ) { + _storage.consumeLast(n, consumingWith: consumer) + } +} +#endif + +#endif diff --git a/Sources/BasicContainers/UniqueArray/UniqueArray+Removals.swift b/Sources/BasicContainers/UniqueArray/UniqueArray+Removals.swift index 619c72f4..ac8261a5 100644 --- a/Sources/BasicContainers/UniqueArray/UniqueArray+Removals.swift +++ b/Sources/BasicContainers/UniqueArray/UniqueArray+Removals.swift @@ -121,57 +121,4 @@ extension UniqueArray where Element: ~Copyable { } } -#if COLLECTIONS_UNSTABLE_CONTAINERS_PREVIEW -@available(SwiftStdlib 5.0, *) -extension UniqueArray where Element: ~Copyable { - /// Remove all items currently in this array, returning an input span to - /// allow them to be consumed in place. - /// The input span extends the exclusive mutating access initiated by this - /// operation to cover the span's lifetime. Once the span is destroyed, - /// the array becomes empty, but it preserves its original storage. - /// - /// - Complexity: O(*n*), where *n* is the original count of the array. - @_lifetime(&self) - @_alwaysEmitIntoClient - public mutating func consumeAll() -> InputSpan { - _storage.consumeAll() - } - - /// Remove the specified number of items from the end of this array, - /// returning an input span to allow them to be consumed in place. - /// The input span extends the exclusive mutating access initiated by this - /// operation to cover the span's lifetime. - /// - /// - Parameter n: The number of items to consume from the end of the array. - /// `n` must be greater than or equal to zero and must not exceed - /// the count of the array. - /// - /// - Complexity: O(`n`) - @_lifetime(&self) - @_alwaysEmitIntoClient - public mutating func consumeLast(_ count: Int) -> InputSpan { - _storage.consumeLast(count) - } - - /// Remove the specified subrange of items from this array, - /// passing an input span to the given function to consume them in place. - /// - /// - Parameter bounds: The subrange of items to consume from this array. - /// - Parameter consumer: A function taking an input span of the removed items, - /// allowing them to be consumed straight out of the array's storage. - /// The function is not required to consume all items in the span; - /// however, the span's remaining items will still be removed from - /// the array. - /// - /// - Complexity: O(`self.count`) - @_alwaysEmitIntoClient - public mutating func consumeSubrange( - _ bounds: Range, - consumingWith consumer: (inout InputSpan) throws(E) -> Result - ) throws(E) -> Result { - try _storage.consumeSubrange(bounds, consumingWith: consumer) - } -} -#endif - #endif diff --git a/Sources/DequeModule/UniqueDeque/UniqueDeque+Consumption.swift b/Sources/DequeModule/UniqueDeque/UniqueDeque+Consumption.swift index 6a8e65d1..e0cae681 100644 --- a/Sources/DequeModule/UniqueDeque/UniqueDeque+Consumption.swift +++ b/Sources/DequeModule/UniqueDeque/UniqueDeque+Consumption.swift @@ -19,6 +19,23 @@ import ContainersPreview @available(SwiftStdlib 5.0, *) extension UniqueDeque where Element: ~Copyable { #if COLLECTIONS_UNSTABLE_CONTAINERS_PREVIEW + /// Remove the specified subrange of items from this deque, + /// passing a series of input spans to a given callback function to consume + /// them in place. + /// + /// The callback is not required to fully consume the contents of its + /// argument; any items it leaves in the input span get automatically + /// consumed. The underlying storage isn't necessarily contiguous, so + /// the callback may be called more than once, whether or not it fully + /// consumes its input. If the specified range is empty, the callback + /// may not be called at all. + /// + /// - Parameter subrange: The subrange of items to consume from this deque. + /// - Parameter consumer: A function taking an input span of the removed items, + /// allowing them to be consumed straight out of the deque's storage. + /// The function is called at most once. + /// + /// - Complexity: O(`self.count`) @_alwaysEmitIntoClient @inline(__always) public mutating func consume( @@ -28,6 +45,23 @@ extension UniqueDeque where Element: ~Copyable { _storage.consume(subrange, consumingWith: consumer) } + /// Remove the specified subrange of items from this deque, + /// passing a series of input spans to a given callback function to consume + /// them in place. + /// + /// The callback is not required to fully consume the contents of its + /// argument; any items it leaves in the input span get automatically + /// consumed. The underlying storage isn't necessarily contiguous, so + /// the callback may be called more than once, whether or not it fully + /// consumes its input. If the specified range is empty, the callback + /// may not be called at all. + /// + /// - Parameter subrange: The subrange of items to consume from this deque. + /// - Parameter consumer: A function taking an input span of the removed items, + /// allowing them to be consumed straight out of the deque's storage. + /// The function is called at most once. + /// + /// - Complexity: O(`self.count`) @_alwaysEmitIntoClient @inline(__always) public mutating func consume>( @@ -37,6 +71,20 @@ extension UniqueDeque where Element: ~Copyable { consume(subrange.relative(to: indices), consumingWith: consumer) } + /// Remove all items currently in this deque, passing a series of input + /// spans to a given callback function to consume them in place. + /// + /// The callback is not required to fully consume the contents of its + /// argument; any items it leaves in the input span get automatically + /// consumed. The underlying storage isn't necessarily contiguous, so + /// the callback may be called more than once, whether or not it fully + /// consumes its input. If the specified range is empty, the callback + /// may not be called at all. + /// + /// - Parameter consumer: A function taking an input span of the removed items, + /// allowing them to be consumed straight out of the deque's storage. + /// The function is called at most once. + /// - Complexity: O(`self.count`) @_alwaysEmitIntoClient @inline(__always) public mutating func consumeAll( @@ -44,6 +92,59 @@ extension UniqueDeque where Element: ~Copyable { ) { consume(indices, consumingWith: consumer) } + + /// Remove the specified number of items from the end of this deque, + /// passing an input span to a given callback function to consume them in + /// place. + /// + /// The callback is not required to fully consume the contents of its + /// argument; any items it leaves in the input span get automatically + /// consumed. The underlying storage isn't necessarily contiguous, so + /// the callback may be called more than once, whether or not it fully + /// consumes its input. If the specified range is empty, the callback + /// may not be called at all. + /// + /// - Parameter n: The number of items to consume from the end of the deque. + /// `n` must be greater than or equal to zero and must not exceed + /// the count of the deque. + /// - Parameter consumer: A function taking an input span of the removed items, + /// allowing them to be consumed straight out of the deque's storage. + /// - Complexity: O(`n`) + @_alwaysEmitIntoClient + @inline(__always) + public mutating func consumeLast( + _ n: Int, + consumingWith consumer: (inout InputSpan) -> Void + ) { + _storage.consumeLast(n, consumingWith: consumer) + } + + /// Remove the specified number of items from the front of this deque, + /// passing an input span to a given callback function to consume them in + /// place. + /// + /// The callback is not required to fully consume the contents of its + /// argument; any items it leaves in the input span get automatically + /// consumed. The underlying storage isn't necessarily contiguous, so + /// the callback may be called more than once, whether or not it fully + /// consumes its input. If the specified range is empty, the callback + /// may not be called at all. + /// + /// - Parameter n: The number of items to consume from the front of the deque. + /// `n` must be greater than or equal to zero and must not exceed + /// the count of the deque. + /// - Parameter consumer: A function taking an input span of the removed items, + /// allowing them to be consumed straight out of the deque's storage. + /// - Complexity: O(`n`) + @_alwaysEmitIntoClient + @inline(__always) + public mutating func consumeFirst( + _ n: Int, + consumingWith consumer: (inout InputSpan) -> Void + ) { + _storage.consumeFirst(n, consumingWith: consumer) + } + #endif } @@ -76,6 +177,26 @@ extension UniqueDeque where Element: ~Copyable { public mutating func consumeAll() -> SubrangeConsumer { consume(indices) } + + @_alwaysEmitIntoClient + @inline(__always) + @_lifetime(&self) + public mutating func consumeLast(_ n: Int) -> SubrangeConsumer { + precondition( + n >= 0 && n <= self.count, + "Count of elements to consume is out of bounds") + return consume(self.count - n ..< self.count) + } + + @_alwaysEmitIntoClient + @inline(__always) + @_lifetime(&self) + public mutating func consumeFirst(_ n: Int) -> SubrangeConsumer { + precondition( + n >= 0 && n <= self.count, + "Count of elements to consume is out of bounds") + return consume(0 ..< n) + } } #endif