From bc7b0ff73d8a82a8b3be90c625fe06d44b47821d Mon Sep 17 00:00:00 2001 From: Karoy Lorentey Date: Fri, 27 Feb 2026 17:58:21 -0800 Subject: [PATCH] [BasicContainers] Add Producer-based array operations --- .../RigidArray/RigidArray+Append.swift | 69 +++++++++++++++++++ .../RigidArray/RigidArray+Insertions.swift | 48 +++++++++++++ .../RigidArray/RigidArray+Replacements.swift | 61 ++++++++++++++++ .../UniqueArray/UniqueArray+Append.swift | 65 +++++++++++++++++ .../UniqueArray/UniqueArray+Insertions.swift | 50 ++++++++++++++ .../UniqueArray+Replacements.swift | 59 ++++++++++++++++ 6 files changed, 352 insertions(+) diff --git a/Sources/BasicContainers/RigidArray/RigidArray+Append.swift b/Sources/BasicContainers/RigidArray/RigidArray+Append.swift index 0d4d58f1..992b50c8 100644 --- a/Sources/BasicContainers/RigidArray/RigidArray+Append.swift +++ b/Sources/BasicContainers/RigidArray/RigidArray+Append.swift @@ -203,6 +203,75 @@ extension RigidArray where Element: ~Copyable { #endif } +@available(SwiftStdlib 5.0, *) +extension RigidArray where Element: ~Copyable { +#if COLLECTIONS_UNSTABLE_CONTAINERS_PREVIEW + /// Appends at most `newItemCount` items generated by a producer to the end of + /// this array. + /// + /// If the array doesn't have sufficient capacity to accommodate the specified + /// number of new elements, then this method reallocates the array's storage + /// to grow it, using a geometric growth rate. + /// + /// This operation appends as many items as the producer can generate before + /// either reaching its end (or throwing an error), or filling the specified + /// capacity. This operation only consumes the first `newItemCount` items in the + /// producer; if the producer has more, then they remain available after this + /// method returns. + /// + /// - Parameters: + /// - newItemCount: The maximum number of items to append to the array. + /// - producer: A producer that generates the items to append. + /// + /// - Complexity: O(*n*) where *n* is the number of new items, when + /// amortized over many similar invocations on the same array. + @_alwaysEmitIntoClient + public mutating func append< + E: Error, + P: Producer & ~Copyable & ~Escapable + >( + addingCount newItemCount: Int, + from producer: inout P + ) throws(E) { + try self.append(addingCount: newItemCount) { target throws(E) in + while !target.isFull { + guard try producer.generate(into: &target) else { break } + } + } + } +#endif + +#if COLLECTIONS_UNSTABLE_CONTAINERS_PREVIEW + /// Appends all the items generated by a producer to the end of this deque. + /// + /// If the deque doesn't have sufficient capacity to accommodate all the + /// elements, then this method reallocates the deque's storage + /// to grow it, using a geometric growth rate. + /// + /// - Parameters: + /// - producer: A producer that generates the items to append. + /// + /// - Complexity: O(*n*) where *n* is the number of new items, when + /// amortized over many similarly-sized invocations on the same deque. + @_alwaysEmitIntoClient + public mutating func append< + E: Error, + P: Producer & ~Copyable & ~Escapable + >( + from producer: inout P + ) throws(E) { + var done = false + while !done { + try self.append(addingCount: freeCapacity) { target throws(E) in + while !target.isFull, !done { + done = try !producer.generate(into: &target) + } + } + } + } +#endif +} + @available(SwiftStdlib 5.0, *) extension RigidArray { /// Copies the elements of a buffer to the end of this array. diff --git a/Sources/BasicContainers/RigidArray/RigidArray+Insertions.swift b/Sources/BasicContainers/RigidArray/RigidArray+Insertions.swift index f3336d3a..22a2c8fa 100644 --- a/Sources/BasicContainers/RigidArray/RigidArray+Insertions.swift +++ b/Sources/BasicContainers/RigidArray/RigidArray+Insertions.swift @@ -264,6 +264,54 @@ extension RigidArray where Element: ~Copyable { #endif } +@available(SwiftStdlib 5.0, *) +extension RigidArray where Element: ~Copyable { +#if COLLECTIONS_UNSTABLE_CONTAINERS_PREVIEW + /// Inserts at most `newItemCount` items generated by a producer into this + /// array, starting at the given index. + /// + /// Existing elements in the array's storage are moved as needed to make room + /// for the new items. + /// + /// If the target array does not have sufficient capacity to hold the + /// specified number of new items, then this triggers a runtime error. + /// + /// This operation inserts as many items as the producer can generate before + /// either reaching `newItemCount`, or the producer hitting its end, or + /// throwing an error. If the producer has more than `newItemCount` items + /// left in its underlying sequence, then extra items remain available after + /// this method returns. + /// + /// If the operation inserts fewer than `newItemCount` items, then it results + /// in a gap in ring buffer storage that needs to be closed by moving some + /// items to their correct positions given the adjusted count. This adds some + /// overhead compared to adding exactly as many items as promised. + /// + /// - Parameters: + /// - newItemCount: The maximum number of items to insert into the array. + /// - index: The position at which to insert the new items. + /// `index` must be a valid index in the array. + /// - producer: A producer that generates the items to append. + /// + /// - Complexity: O(`self.count` + `newItemCount`) + @_alwaysEmitIntoClient + public mutating func insert< + E: Error, + P: Producer & ~Copyable & ~Escapable + >( + addingCount newItemCount: Int, + from producer: inout P, + at index: Int + ) throws(E) { + try insert(addingCount: newItemCount, at: index) { target throws(E) in + while !target.isFull, try producer.generate(into: &target) { + // Do nothing + } + } + } + #endif +} + @available(SwiftStdlib 5.0, *) extension RigidArray { /// Copies the elements of a fully initialized buffer pointer into this diff --git a/Sources/BasicContainers/RigidArray/RigidArray+Replacements.swift b/Sources/BasicContainers/RigidArray/RigidArray+Replacements.swift index 2c9d0b2c..77f73aaa 100644 --- a/Sources/BasicContainers/RigidArray/RigidArray+Replacements.swift +++ b/Sources/BasicContainers/RigidArray/RigidArray+Replacements.swift @@ -404,6 +404,67 @@ extension RigidArray where Element: ~Copyable { #endif } +@available(SwiftStdlib 5.0, *) +extension RigidArray where Element: ~Copyable { +#if COLLECTIONS_UNSTABLE_CONTAINERS_PREVIEW + /// Replaces the specified range of elements by at most `newItemCount` items + /// generated by a producer. + /// + /// This method has the effect of removing the specified range of elements + /// from the array and inserting the new elements starting at the same + /// location. The number of new elements need not match the number of elements + /// being removed. + /// + /// If the capacity of the array isn't sufficient to accommodate the new + /// elements, then this method triggers a runtime error. + /// + /// If you pass a zero-length range as the `subrange` parameter, this method + /// inserts the items generated by `producer` at `subrange.lowerBound`. This + /// case is more directly expressed by calling `insert(moving:at:)`. + /// + /// Likewise, if the given producer is empty, then this method removes the + /// elements in the given subrange without replacement. This case is more + /// directly expressed by calling `removeSubrange`. + /// + /// This operation inserts as many items as the producer can generate before + /// either reaching `newItemCount`, or the producer hitting its end, or + /// throwing an error. If the producer has more than `newItemCount` items left in + /// its underlying sequence, then extra items remain available after this + /// method returns. + /// + /// If the operation inserts fewer than `newItemCount` items, then it results in + /// a gap in array storage that needs to be closed by moving some + /// items to their correct positions given the adjusted count. This adds some + /// overhead compared to adding exactly as many items as promised. + /// + /// - Parameters: + /// - subrange: The subrange of the array to replace. The bounds of + /// the range must be valid indices in the array. + /// - newItemCount: The maximum number of items to insert into the array. + /// - producer: A producer that generates the items to append. + /// + /// - Complexity: O(`self.count` + `maximumCount`) + @_alwaysEmitIntoClient + public mutating func replace< + E: Error, + P: Producer & ~Copyable & ~Escapable + >( + removing subrange: Range, + addingCount newItemCount: Int, + from producer: inout P + ) throws(E) { + try replace( + removing: subrange, + addingCount: newItemCount + ) { target throws(E) in + while !target.isFull { + guard try producer.generate(into: &target) else { break } + } + } + } +#endif +} + @available(SwiftStdlib 5.0, *) extension RigidArray { /// Replaces the specified subrange of elements by copying the elements of diff --git a/Sources/BasicContainers/UniqueArray/UniqueArray+Append.swift b/Sources/BasicContainers/UniqueArray/UniqueArray+Append.swift index 55275d1c..67aae1b3 100644 --- a/Sources/BasicContainers/UniqueArray/UniqueArray+Append.swift +++ b/Sources/BasicContainers/UniqueArray/UniqueArray+Append.swift @@ -176,6 +176,71 @@ extension UniqueArray where Element: ~Copyable { self.append(moving: &items) } #endif + +#if COLLECTIONS_UNSTABLE_CONTAINERS_PREVIEW + /// Appends at most `newItemCount` items generated by a producer to the end of + /// this array. + /// + /// If the array doesn't have sufficient capacity to accommodate the specified + /// number of new elements, then this method reallocates the array's storage + /// to grow it, using a geometric growth rate. + /// + /// This operation appends as many items as the producer can generate before + /// either reaching its end (or throwing an error), or filling the specified + /// capacity. This operation only consumes the first `newItemCount` items in the + /// producer; if the producer has more, then they remain available after this + /// method returns. + /// + /// - Parameters: + /// - newItemCount: The maximum number of items to append to the array. + /// - producer: A producer that generates the items to append. + /// + /// - Complexity: O(*n*) where *n* is the number of new items, when + /// amortized over many similar invocations on the same array. + @_alwaysEmitIntoClient + public mutating func append< + E: Error, + P: Producer & ~Copyable & ~Escapable + >( + addingCount newItemCount: Int, + from producer: inout P + ) throws(E) { + _ensureFreeCapacity(newItemCount) + try _storage.append(addingCount: newItemCount, from: &producer) + } +#endif + +#if COLLECTIONS_UNSTABLE_CONTAINERS_PREVIEW + /// Appends all the items generated by a producer to the end of this deque. + /// + /// If the deque doesn't have sufficient capacity to accommodate all the + /// elements, then this method reallocates the deque's storage + /// to grow it, using a geometric growth rate. + /// + /// - Parameters: + /// - producer: A producer that generates the items to append. + /// + /// - Complexity: O(*n*) where *n* is the number of new items, when + /// amortized over many similarly-sized invocations on the same deque. + @_alwaysEmitIntoClient + public mutating func append< + E: Error, + P: Producer & ~Copyable & ~Escapable + >( + from producer: inout P + ) throws(E) { + var done = false + while !done { + let c = Swift.max(producer.underestimatedCount, 1) + _ensureFreeCapacity(c) + try self.append(addingCount: freeCapacity) { target throws(E) in + while !target.isFull, !done { + done = try !producer.generate(into: &target) + } + } + } + } +#endif } @available(SwiftStdlib 5.0, *) diff --git a/Sources/BasicContainers/UniqueArray/UniqueArray+Insertions.swift b/Sources/BasicContainers/UniqueArray/UniqueArray+Insertions.swift index 55e936b2..e96a3470 100644 --- a/Sources/BasicContainers/UniqueArray/UniqueArray+Insertions.swift +++ b/Sources/BasicContainers/UniqueArray/UniqueArray+Insertions.swift @@ -225,6 +225,56 @@ extension UniqueArray where Element: ~Copyable { #endif } +@available(SwiftStdlib 5.0, *) +extension UniqueArray where Element: ~Copyable { +#if COLLECTIONS_UNSTABLE_CONTAINERS_PREVIEW + /// Inserts at most `newItemCount` items generated by a producer into this + /// array, starting at the given index. + /// + /// Existing elements in the array's storage are moved as needed to make room + /// for the new items. + /// + /// If the array does not have sufficient capacity to hold enough items, + /// then this reallocates the array's storage to grow its capacity, using a + /// geometric growth rate. + /// + /// This operation inserts as many items as the producer can generate before + /// either reaching `newItemCount`, or the producer hitting its end, or + /// throwing an error. If the producer has more than `newItemCount` items + /// left in its underlying sequence, then extra items remain available after + /// this method returns. + /// + /// If the operation inserts fewer than `newItemCount` items, then it results + /// in a gap in array storage that needs to be closed by moving some + /// items to their correct positions given the adjusted count. This adds some + /// overhead compared to adding exactly as many items as promised. + /// + /// - Parameters: + /// - newItemCount: The maximum number of items to insert into the array. + /// - index: The position at which to insert the new items. + /// `index` must be a valid index in the array. + /// - producer: A producer that generates the items to append. + /// + /// - Complexity: O(`self.count` + `newItemCount`) when amortized over many + /// similar invocations on the same array. + @_alwaysEmitIntoClient + public mutating func insert< + E: Error, + P: Producer & ~Copyable & ~Escapable + >( + addingCount newItemCount: Int, + from producer: inout P, + at index: Int + ) throws(E) { + try insert(addingCount: newItemCount, at: index) { target throws(E) in + while !target.isFull, try producer.generate(into: &target) { + // Do nothing + } + } + } + #endif +} + @available(SwiftStdlib 5.0, *) extension UniqueArray { /// Copyies the elements of a fully initialized buffer pointer into this diff --git a/Sources/BasicContainers/UniqueArray/UniqueArray+Replacements.swift b/Sources/BasicContainers/UniqueArray/UniqueArray+Replacements.swift index aa6af999..f73a1758 100644 --- a/Sources/BasicContainers/UniqueArray/UniqueArray+Replacements.swift +++ b/Sources/BasicContainers/UniqueArray/UniqueArray+Replacements.swift @@ -341,6 +341,65 @@ extension UniqueArray where Element: ~Copyable { } } +@available(SwiftStdlib 5.0, *) +extension UniqueArray where Element: ~Copyable { +#if COLLECTIONS_UNSTABLE_CONTAINERS_PREVIEW + /// Replaces the specified range of elements by at most `maximumCount` items + /// generated by a producer. + /// + /// This method has the effect of removing the specified range of elements + /// from the array and inserting the new elements starting at the same + /// location. The number of new elements need not match the number of elements + /// being removed. + /// + /// If the array doesn't have sufficient capacity to accommodate the new + /// elements, then this method reallocates the array's storage to grow it, + /// using a geometric growth rate. + /// + /// If you pass a zero-length range as the `subrange` parameter, this method + /// inserts the items generated by `producer` at `subrange.lowerBound`. This + /// case is more directly expressed by calling `insert(moving:at:)`. + /// + /// Likewise, if the given producer is empty, then this method removes the + /// elements in the given subrange without replacement. This case is more + /// directly expressed by calling `removeSubrange`. + /// + /// This operation inserts as many items as the producer can generate before + /// either reaching `maximumCount`, or the producer hitting its end, or + /// throwing an error. If the producer has more than `maximumCount` items left in + /// its underlying sequence, then extra items remain available after this + /// method returns. + /// + /// If the operation inserts fewer than `newItemCount` items, then it results in + /// a gap in array storage that needs to be closed by moving some + /// items to their correct positions given the adjusted count. This adds some + /// overhead compared to adding exactly as many items as promised. + /// + /// - Parameters: + /// - subrange: The subrange of the array to replace. The bounds of + /// the range must be valid indices in the array. + /// - newItemCount: The maximum number of items to insert into the deque. + /// - producer: A producer that generates the items to append. + /// + /// - Complexity: O(`self.count` + `newItemCount`) + @_alwaysEmitIntoClient + public mutating func replace< + E: Error, + P: Producer & ~Copyable & ~Escapable + >( + removing subrange: Range, + addingCount newItemCount: Int, + from producer: inout P + ) throws(E) { + try replace(removing: subrange, addingCount: newItemCount) { target throws(E) in + while !target.isFull, try producer.generate(into: &target) { + // Do nothing + } + } + } +#endif +} + @available(SwiftStdlib 5.0, *) extension UniqueArray { /// Replaces the specified subrange of elements by copying the elements of