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(