authorgravatar for squeek502@hotmail.comRyan Liptak <squeek502@hotmail.com> 2019-05-02 00:58:26-07:00
committergravatar for squeek502@hotmail.comRyan Liptak <squeek502@hotmail.com> 2019-05-02 00:58:26-07:00
log0afa2d040a6f51b5423269cb588f4fa483e8cfba
treed45f1fb9ed815e96fe924cb86687cd33e7bed3e6
parent8b7c59a41419b8802af843ac023ba0c6fbfbb83b

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


1 files changed, 8 insertions(+), 1 deletions(-)

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