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