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...@@ -141,8 +141,15 @@ pub fn HashMap(comptime K: type, comptime V: type, comptime hash: fn (key: K) u3
141 if (new_capacity <= self.entries.len) {141 if (new_capacity <= self.entries.len) {
142 return;142 return;
143 }143 }
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 }
144 const old_entries = self.entries;151 const old_entries = self.entries;
145 try self.initCapacity(new_capacity);152 try self.initCapacity(capacity);
146 if (old_entries.len > 0) {153 if (old_entries.len > 0) {
147 // dump all of the old elements into the new table154 // dump all of the old elements into the new table
148 for (old_entries) |*old_entry| {155 for (old_entries) |*old_entry| {