From 6da58357a35185c7257456db1fd025a5e65183a4 Mon Sep 17 00:00:00 2001 From: Alejandro Alonso Date: Wed, 10 Jun 2026 10:31:22 -0700 Subject: [PATCH 1/7] Rename reallocate to setCapacity for Rigid and UniqueArray Update UniqueArray.swift Update UniqueArray.swift --- .../RigidArray/RigidArray+Deprecated.swift | 26 +++++++++++++++++- .../RigidArray/RigidArray.swift | 7 +++-- .../UniqueArray/UniqueArray+Deprecated.swift | 17 ++++++++++++ .../UniqueArray/UniqueArray.swift | 4 +-- .../RigidArrayTests.swift | 27 +++++++++++++++++++ .../UniqueArrayTests.swift | 27 +++++++++++++++++++ 6 files changed, 101 insertions(+), 7 deletions(-) diff --git a/Sources/BasicContainers/RigidArray/RigidArray+Deprecated.swift b/Sources/BasicContainers/RigidArray/RigidArray+Deprecated.swift index 715bd90c..97eed354 100644 --- a/Sources/BasicContainers/RigidArray/RigidArray+Deprecated.swift +++ b/Sources/BasicContainers/RigidArray/RigidArray+Deprecated.swift @@ -77,7 +77,31 @@ extension RigidArray where Element: ~Copyable { } return result! } - + + /// Grow or shrink the capacity of a rigid array instance without discarding + /// its contents. + /// + /// This operation replaces the array's storage buffer with a newly allocated + /// buffer of the specified capacity, moving all existing elements + /// to its new storage. The old storage is then deallocated. + /// + /// - Parameter newCapacity: The desired new capacity. `newCapacity` must be + /// greater than or equal to the current count. + /// + /// - Complexity: O(`count`) + @available(*, deprecated, renamed: "setCapacity(_:)") + @inlinable + public mutating func reallocate(capacity newCapacity: Int) { + precondition(newCapacity >= count, "RigidArray capacity overflow") + guard newCapacity != capacity else { return } + let newStorage: UnsafeMutableBufferPointer = .allocate( + capacity: newCapacity) + let i = unsafe newStorage.moveInitialize(fromContentsOf: self._items) + assert(i == count) + unsafe _storage.deallocate() + unsafe _storage = newStorage + } + /// Replaces the specified range of elements by a given count of new items, /// using a callback to directly initialize array storage by populating /// an output span. diff --git a/Sources/BasicContainers/RigidArray/RigidArray.swift b/Sources/BasicContainers/RigidArray/RigidArray.swift index 74193181..810514c8 100644 --- a/Sources/BasicContainers/RigidArray/RigidArray.swift +++ b/Sources/BasicContainers/RigidArray/RigidArray.swift @@ -349,11 +349,10 @@ extension RigidArray where Element: ~Copyable { /// /// - Complexity: O(`count`) @inlinable - public mutating func reallocate(capacity newCapacity: Int) { - precondition(newCapacity >= count, "RigidArray capacity overflow") + public mutating func setCapacity(_ newCapacity: Int) { guard newCapacity != capacity else { return } let newStorage: UnsafeMutableBufferPointer = .allocate( - capacity: newCapacity) + capacity: Swift.max(newCapacity, count)) let i = unsafe newStorage.moveInitialize(fromContentsOf: self._items) assert(i == count) unsafe _storage.deallocate() @@ -371,7 +370,7 @@ extension RigidArray where Element: ~Copyable { @inlinable public mutating func reserveCapacity(_ n: Int) { guard capacity < n else { return } - reallocate(capacity: n) + setCapacity(n) } } diff --git a/Sources/BasicContainers/UniqueArray/UniqueArray+Deprecated.swift b/Sources/BasicContainers/UniqueArray/UniqueArray+Deprecated.swift index 5a4dc66a..8a7b7028 100644 --- a/Sources/BasicContainers/UniqueArray/UniqueArray+Deprecated.swift +++ b/Sources/BasicContainers/UniqueArray/UniqueArray+Deprecated.swift @@ -87,6 +87,23 @@ extension UniqueArray where Element: ~Copyable { return result! } + /// Grow or shrink the capacity of a unique array instance without discarding + /// its contents. + /// + /// This operation replaces the array's storage buffer with a newly allocated + /// buffer of the specified capacity, moving all existing elements + /// to its new storage. The old storage is then deallocated. + /// + /// - Parameter newCapacity: The desired new capacity. `newCapacity` must be + /// greater than or equal to the current count. + /// + /// - Complexity: O(`count`) + @available(*, deprecated, renamed: "setCapacity(_:)") + @inlinable + public mutating func reallocate(capacity: Int) { + _storage.reallocate(capacity: capacity) + } + /// Replaces the specified range of elements by a given count of new items, /// using a callback to directly initialize array storage by populating /// an output span. diff --git a/Sources/BasicContainers/UniqueArray/UniqueArray.swift b/Sources/BasicContainers/UniqueArray/UniqueArray.swift index 3142a430..df9fd7f8 100644 --- a/Sources/BasicContainers/UniqueArray/UniqueArray.swift +++ b/Sources/BasicContainers/UniqueArray/UniqueArray.swift @@ -259,8 +259,8 @@ extension UniqueArray where Element: ~Copyable { /// /// - Complexity: O(`count`) @inlinable - public mutating func reallocate(capacity: Int) { - _storage.reallocate(capacity: capacity) + public mutating func setCapacity(_ newCapacity: Int) { + _storage.setCapacity(newCapacity) } /// Ensure that the array has capacity to store the specified number of diff --git a/Tests/BasicContainersTests/RigidArrayTests.swift b/Tests/BasicContainersTests/RigidArrayTests.swift index f0946309..02879ffb 100644 --- a/Tests/BasicContainersTests/RigidArrayTests.swift +++ b/Tests/BasicContainersTests/RigidArrayTests.swift @@ -517,6 +517,33 @@ class RigidArrayTests: CollectionTestCase { } } + func test_setCapacity() { + withSomeArrayLayouts("layout", ofCapacities: [0, 10, 100]) { layout in + withEvery( + "newCapacity", + in: [ + layout.capacity, layout.count, layout.count + 1, layout.capacity + 1, + 0, layout.count - 1 + ] as Set + ) { newCapacity in + withLifetimeTracking { tracker in + var a = tracker.rigidArray(layout: layout) + expectEqual(a.count, layout.count) + expectEqual(a.capacity, layout.capacity) + a.setCapacity(newCapacity) + expectEqual(a.count, layout.count) + expectEqual(a.capacity, Swift.max(newCapacity, layout.count)) + expectEqual(tracker.instances, layout.count) + expectIterableContents( + a, + equivalentTo: 0 ..< layout.count, + by: { $0.payload == $1 }, + printer: { "\($0.payload)" }) + } + } + } + } + func test_reserveCapacity() { withSomeArrayLayouts("layout", ofCapacities: [0, 10, 100]) { layout in withEvery( diff --git a/Tests/BasicContainersTests/UniqueArrayTests.swift b/Tests/BasicContainersTests/UniqueArrayTests.swift index cc756283..72bdebb1 100644 --- a/Tests/BasicContainersTests/UniqueArrayTests.swift +++ b/Tests/BasicContainersTests/UniqueArrayTests.swift @@ -465,6 +465,33 @@ class UniqueArrayTests: CollectionTestCase { } } + func test_setCapacity() { + withSomeArrayLayouts("layout", ofCapacities: [0, 10, 100]) { layout in + withEvery( + "newCapacity", + in: [ + layout.capacity, layout.count, layout.count + 1, layout.capacity + 1, + 0, layout.count - 1 + ] as Set + ) { newCapacity in + withLifetimeTracking { tracker in + var a = tracker.uniqueArray(layout: layout) + expectEqual(a.count, layout.count) + expectEqual(a.capacity, layout.capacity) + a.setCapacity(newCapacity) + expectEqual(a.count, layout.count) + expectEqual(a.capacity, Swift.max(newCapacity, layout.count)) + expectEqual(tracker.instances, layout.count) + expectIterableContents( + a, + equivalentTo: 0 ..< layout.count, + by: { $0.payload == $1 }, + printer: { "\($0.payload)" }) + } + } + } + } + func test_reserveCapacity() { withSomeArrayLayouts("layout", ofCapacities: [0, 10, 100]) { layout in withEvery( From c6a1dabfd082aeb89be7a59be5119ea36b784421 Mon Sep 17 00:00:00 2001 From: Alejandro Alonso Date: Wed, 10 Jun 2026 14:31:58 -0700 Subject: [PATCH 2/7] Update Sources/BasicContainers/RigidArray/RigidArray+Deprecated.swift Co-authored-by: Karoy Lorentey --- .../RigidArray/RigidArray+Deprecated.swift | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/Sources/BasicContainers/RigidArray/RigidArray+Deprecated.swift b/Sources/BasicContainers/RigidArray/RigidArray+Deprecated.swift index 97eed354..43cc2db5 100644 --- a/Sources/BasicContainers/RigidArray/RigidArray+Deprecated.swift +++ b/Sources/BasicContainers/RigidArray/RigidArray+Deprecated.swift @@ -92,14 +92,7 @@ extension RigidArray where Element: ~Copyable { @available(*, deprecated, renamed: "setCapacity(_:)") @inlinable public mutating func reallocate(capacity newCapacity: Int) { - precondition(newCapacity >= count, "RigidArray capacity overflow") - guard newCapacity != capacity else { return } - let newStorage: UnsafeMutableBufferPointer = .allocate( - capacity: newCapacity) - let i = unsafe newStorage.moveInitialize(fromContentsOf: self._items) - assert(i == count) - unsafe _storage.deallocate() - unsafe _storage = newStorage + setCapacity(newCapacity) } /// Replaces the specified range of elements by a given count of new items, From eb5c24f71a3d449e7ccec5fe896c4524843e0ea5 Mon Sep 17 00:00:00 2001 From: Alejandro Alonso Date: Wed, 10 Jun 2026 14:32:06 -0700 Subject: [PATCH 3/7] Update Sources/BasicContainers/RigidArray/RigidArray.swift Co-authored-by: Karoy Lorentey --- Sources/BasicContainers/RigidArray/RigidArray.swift | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Sources/BasicContainers/RigidArray/RigidArray.swift b/Sources/BasicContainers/RigidArray/RigidArray.swift index 810514c8..5d9fdaf7 100644 --- a/Sources/BasicContainers/RigidArray/RigidArray.swift +++ b/Sources/BasicContainers/RigidArray/RigidArray.swift @@ -345,7 +345,8 @@ extension RigidArray where Element: ~Copyable { /// to its new storage. The old storage is then deallocated. /// /// - Parameter newCapacity: The desired new capacity. `newCapacity` must be - /// greater than or equal to the current count. +/// - Parameter newCapacity: The desired new capacity. The new capacity +/// is set to the maximum of `newCapacity` and the current count. /// /// - Complexity: O(`count`) @inlinable From 2a39308613dc34ab565442c671002f8c2ed77a86 Mon Sep 17 00:00:00 2001 From: Alejandro Alonso Date: Wed, 10 Jun 2026 14:32:13 -0700 Subject: [PATCH 4/7] Update Sources/BasicContainers/UniqueArray/UniqueArray.swift Co-authored-by: Karoy Lorentey --- Sources/BasicContainers/UniqueArray/UniqueArray.swift | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Sources/BasicContainers/UniqueArray/UniqueArray.swift b/Sources/BasicContainers/UniqueArray/UniqueArray.swift index df9fd7f8..007b03aa 100644 --- a/Sources/BasicContainers/UniqueArray/UniqueArray.swift +++ b/Sources/BasicContainers/UniqueArray/UniqueArray.swift @@ -255,7 +255,8 @@ extension UniqueArray where Element: ~Copyable { /// to its new storage. The old storage is then deallocated. /// /// - Parameter newCapacity: The desired new capacity. `newCapacity` must be - /// greater than or equal to the current count. +/// - Parameter newCapacity: The desired new capacity. The new capacity +/// is set to the maximum of `newCapacity` and the current count. /// /// - Complexity: O(`count`) @inlinable From 06423d7329435d88b61e5ae3e89fb5cc6df74d8d Mon Sep 17 00:00:00 2001 From: Alejandro Alonso Date: Wed, 10 Jun 2026 14:41:21 -0700 Subject: [PATCH 5/7] Fix broken docs --- Sources/BasicContainers/RigidArray/RigidArray.swift | 5 ++--- Sources/BasicContainers/UniqueArray/UniqueArray.swift | 5 ++--- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/Sources/BasicContainers/RigidArray/RigidArray.swift b/Sources/BasicContainers/RigidArray/RigidArray.swift index 5d9fdaf7..bf7713e2 100644 --- a/Sources/BasicContainers/RigidArray/RigidArray.swift +++ b/Sources/BasicContainers/RigidArray/RigidArray.swift @@ -344,9 +344,8 @@ extension RigidArray where Element: ~Copyable { /// buffer of the specified capacity, moving all existing elements /// to its new storage. The old storage is then deallocated. /// - /// - Parameter newCapacity: The desired new capacity. `newCapacity` must be -/// - Parameter newCapacity: The desired new capacity. The new capacity -/// is set to the maximum of `newCapacity` and the current count. + /// - Parameter newCapacity: The desired new capacity. The new capacity + /// is set to the maximum of `newCapacity` and the current count. /// /// - Complexity: O(`count`) @inlinable diff --git a/Sources/BasicContainers/UniqueArray/UniqueArray.swift b/Sources/BasicContainers/UniqueArray/UniqueArray.swift index 007b03aa..f89cd353 100644 --- a/Sources/BasicContainers/UniqueArray/UniqueArray.swift +++ b/Sources/BasicContainers/UniqueArray/UniqueArray.swift @@ -254,9 +254,8 @@ extension UniqueArray where Element: ~Copyable { /// buffer of the specified capacity, moving all existing elements /// to its new storage. The old storage is then deallocated. /// - /// - Parameter newCapacity: The desired new capacity. `newCapacity` must be -/// - Parameter newCapacity: The desired new capacity. The new capacity -/// is set to the maximum of `newCapacity` and the current count. + /// - Parameter newCapacity: The desired new capacity. The new capacity + /// is set to the maximum of `newCapacity` and the current count. /// /// - Complexity: O(`count`) @inlinable From 28e9d98a7e76f6873abd47cd1f0e26eeb92c78fd Mon Sep 17 00:00:00 2001 From: Karoy Lorentey Date: Wed, 10 Jun 2026 16:03:59 -0700 Subject: [PATCH 6/7] Fix deprecation warnings --- Sources/BasicContainers/UniqueArray/UniqueArray.swift | 2 +- Tests/BasicContainersTests/RigidArrayTests.swift | 1 + Tests/BasicContainersTests/UniqueArrayTests.swift | 1 + 3 files changed, 3 insertions(+), 1 deletion(-) diff --git a/Sources/BasicContainers/UniqueArray/UniqueArray.swift b/Sources/BasicContainers/UniqueArray/UniqueArray.swift index f89cd353..9816fb11 100644 --- a/Sources/BasicContainers/UniqueArray/UniqueArray.swift +++ b/Sources/BasicContainers/UniqueArray/UniqueArray.swift @@ -294,7 +294,7 @@ extension UniqueArray where Element: ~Copyable { @inlinable internal mutating func _ensureFreeCapacitySlow(_ freeCapacity: Int) { let newCapacity = _grow(freeCapacity: freeCapacity) - reallocate(capacity: newCapacity) + setCapacity(newCapacity) } } diff --git a/Tests/BasicContainersTests/RigidArrayTests.swift b/Tests/BasicContainersTests/RigidArrayTests.swift index 02879ffb..e43acec3 100644 --- a/Tests/BasicContainersTests/RigidArrayTests.swift +++ b/Tests/BasicContainersTests/RigidArrayTests.swift @@ -491,6 +491,7 @@ class RigidArrayTests: CollectionTestCase { } } + @available(*, deprecated) func test_reallocate() { withSomeArrayLayouts("layout", ofCapacities: [0, 10, 100]) { layout in withEvery( diff --git a/Tests/BasicContainersTests/UniqueArrayTests.swift b/Tests/BasicContainersTests/UniqueArrayTests.swift index 72bdebb1..59f128fc 100644 --- a/Tests/BasicContainersTests/UniqueArrayTests.swift +++ b/Tests/BasicContainersTests/UniqueArrayTests.swift @@ -439,6 +439,7 @@ class UniqueArrayTests: CollectionTestCase { } } + @available(*, deprecated) func test_reallocate() { withSomeArrayLayouts("layout", ofCapacities: [0, 10, 100]) { layout in withEvery( From 9aca86f36b57197101c10b620f614090e4a15544 Mon Sep 17 00:00:00 2001 From: Karoy Lorentey Date: Wed, 17 Jun 2026 13:58:54 -0700 Subject: [PATCH 7/7] Update to reflect new name of expect{Rigid,Unique}ArrayContents --- Tests/BasicContainersTests/RigidArrayTests.swift | 2 +- Tests/BasicContainersTests/UniqueArrayTests.swift | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Tests/BasicContainersTests/RigidArrayTests.swift b/Tests/BasicContainersTests/RigidArrayTests.swift index e43acec3..fb0983a9 100644 --- a/Tests/BasicContainersTests/RigidArrayTests.swift +++ b/Tests/BasicContainersTests/RigidArrayTests.swift @@ -535,7 +535,7 @@ class RigidArrayTests: CollectionTestCase { expectEqual(a.count, layout.count) expectEqual(a.capacity, Swift.max(newCapacity, layout.count)) expectEqual(tracker.instances, layout.count) - expectIterableContents( + expectRigidArrayContents( a, equivalentTo: 0 ..< layout.count, by: { $0.payload == $1 }, diff --git a/Tests/BasicContainersTests/UniqueArrayTests.swift b/Tests/BasicContainersTests/UniqueArrayTests.swift index 59f128fc..d47a48fd 100644 --- a/Tests/BasicContainersTests/UniqueArrayTests.swift +++ b/Tests/BasicContainersTests/UniqueArrayTests.swift @@ -483,7 +483,7 @@ class UniqueArrayTests: CollectionTestCase { expectEqual(a.count, layout.count) expectEqual(a.capacity, Swift.max(newCapacity, layout.count)) expectEqual(tracker.instances, layout.count) - expectIterableContents( + expectUniqueArrayContents( a, equivalentTo: 0 ..< layout.count, by: { $0.payload == $1 },