std.ArrayHashMap: ensureUnusedCapacity and ensureTotalCapacity

Same as 22015c1b3b, but for ArrayHashMap.
This commit is contained in:
Andrew Kelley
2021-05-06 12:50:53 -07:00
parent 17067e0e6b
commit 426e4c784c

View File

@@ -158,10 +158,20 @@ pub fn ArrayHashMap(
return self.unmanaged.getOrPutValue(self.allocator, key, value);
}
/// Deprecated: call `ensureUnusedCapacity` or `ensureTotalCapacity`.
pub const ensureCapacity = ensureTotalCapacity;
/// Increases capacity, guaranteeing that insertions up until the
/// `expected_count` will not cause an allocation, and therefore cannot fail.
pub fn ensureCapacity(self: *Self, new_capacity: usize) !void {
return self.unmanaged.ensureCapacity(self.allocator, new_capacity);
pub fn ensureTotalCapacity(self: *Self, new_capacity: usize) !void {
return self.unmanaged.ensureTotalCapacity(self.allocator, new_capacity);
}
/// Increases capacity, guaranteeing that insertions up until
/// `additional_count` **more** items will not cause an allocation, and
/// therefore cannot fail.
pub fn ensureUnusedCapacity(self: *Self, additional_count: usize) !void {
return self.unmanaged.ensureUnusedCapacity(self.allocator, additional_count);
}
/// Returns the number of total elements which may be present before it is
@@ -472,10 +482,13 @@ pub fn ArrayHashMapUnmanaged(
return res.entry;
}
/// Deprecated: call `ensureUnusedCapacity` or `ensureTotalCapacity`.
pub const ensureCapacity = ensureTotalCapacity;
/// Increases capacity, guaranteeing that insertions up until the
/// `expected_count` will not cause an allocation, and therefore cannot fail.
pub fn ensureCapacity(self: *Self, allocator: *Allocator, new_capacity: usize) !void {
try self.entries.ensureCapacity(allocator, new_capacity);
pub fn ensureTotalCapacity(self: *Self, allocator: *Allocator, new_capacity: usize) !void {
try self.entries.ensureTotalCapacity(allocator, new_capacity);
if (new_capacity <= linear_scan_max) return;
// Ensure that the indexes will be at most 60% full if
@@ -501,6 +514,17 @@ pub fn ArrayHashMapUnmanaged(
}
}
/// Increases capacity, guaranteeing that insertions up until
/// `additional_count` **more** items will not cause an allocation, and
/// therefore cannot fail.
pub fn ensureUnusedCapacity(
self: *Self,
allocator: *Allocator,
additional_capacity: usize,
) !void {
return self.ensureTotalCapacity(allocator, self.count() + additional_capacity);
}
/// Returns the number of total elements which may be present before it is
/// no longer guaranteed that no allocations will be performed.
pub fn capacity(self: Self) usize {