Merge pull request #667 from Azoy/setcapacity

[BasicContainers] Rename reallocate to setCapacity for Rigid and UniqueArray
This commit is contained in:
Karoy Lorentey authored and GitHub committed 2026-06-17 14:26:01 -07:00
commit 232e6aeacf
6 files changed
+101 -12

No files matched your search

@@ -77,7 +77,24 @@ 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) {
setCapacity(newCapacity)
}
/// 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.
@@ -344,16 +344,15 @@ 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
/// 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
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<Element> = .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)
}
}
@@ -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.
@@ -254,13 +254,13 @@ 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
/// 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
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
@@ -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)
}
}
@@ -491,6 +491,7 @@ class RigidArrayTests: CollectionTestCase {
}
}
@available(*, deprecated)
func test_reallocate() {
withSomeArrayLayouts("layout", ofCapacities: [0, 10, 100]) { layout in
withEvery(
@@ -517,6 +518,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<Int>
) { 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)
expectRigidArrayContents(
a,
equivalentTo: 0 ..< layout.count,
by: { $0.payload == $1 },
printer: { "\($0.payload)" })
}
}
}
}
func test_reserveCapacity() {
withSomeArrayLayouts("layout", ofCapacities: [0, 10, 100]) { layout in
withEvery(
@@ -439,6 +439,7 @@ class UniqueArrayTests: CollectionTestCase {
}
}
@available(*, deprecated)
func test_reallocate() {
withSomeArrayLayouts("layout", ofCapacities: [0, 10, 100]) { layout in
withEvery(
@@ -465,6 +466,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<Int>
) { 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)
expectUniqueArrayContents(
a,
equivalentTo: 0 ..< layout.count,
by: { $0.payload == $1 },
printer: { "\($0.payload)" })
}
}
}
}
func test_reserveCapacity() {
withSomeArrayLayouts("layout", ofCapacities: [0, 10, 100]) { layout in
withEvery(