zig

fork of https://codeberg.org/ziglang/zig
Log | Files | Refs | README | LICENSE

commit 0afa2d040a6f51b5423269cb588f4fa483e8cfba (tree)
parent 8b7c59a41419b8802af843ac023ba0c6fbfbb83b
Author: Ryan Liptak <squeek502@hotmail.com>
Date:   Thu,  2 May 2019 00:58:26 -0700

make std.HashMap.ensureCapacity round up to the nearest power of two

Diffstat:
Mstd/hash_map.zig | 9++++++++-
1 file changed, 8 insertions(+), 1 deletion(-)

diff --git a/std/hash_map.zig b/std/hash_map.zig @@ -141,8 +141,15 @@ pub fn HashMap(comptime K: type, comptime V: type, comptime hash: fn (key: K) u3 if (new_capacity <= self.entries.len) { return; } + // make sure capacity is a power of two + var capacity = new_capacity; + const is_power_of_two = capacity & (capacity-1) == 0; + if (!is_power_of_two) { + const pow = math.log2_int_ceil(usize, capacity); + capacity = math.pow(usize, 2, pow); + } const old_entries = self.entries; - try self.initCapacity(new_capacity); + try self.initCapacity(capacity); if (old_entries.len > 0) { // dump all of the old elements into the new table for (old_entries) |*old_entry| {