authorgravatar for Sahnvour@users.noreply.github.comSahnvour <Sahnvour@users.noreply.github.com> 2020-09-02 08:52:32+02:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2020-09-02 08:52:32+02:00
log90ace40e07de7bca2558da72e6d67cf660f86192
treeb3cc13576ad7d9fc94a80776aa4008316c0ff746
parent1b2154dfe2f9b5030f487e7c4be8c706ce6e59b5
parent575fbd5e3592cff70cbfc5153884d919e6bed89f
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #5999 from Sahnvour/hashmap

New hashmap implementation

17 files changed, 2017 insertions(+), 767 deletions(-)

lib/std/array_hash_map.zig created+1087
......@@ -0,0 +1,1087 @@
1// SPDX-License-Identifier: MIT
2// Copyright (c) 2015-2020 Zig Contributors
3// This file is part of [zig](https://ziglang.org/), which is MIT licensed.
4// The MIT license requires this copyright notice to be included in all copies
5// and substantial portions of the software.
6const std = @import("std.zig");
7const debug = std.debug;
8const assert = debug.assert;
9const testing = std.testing;
10const math = std.math;
11const mem = std.mem;
12const meta = std.meta;
13const trait = meta.trait;
14const autoHash = std.hash.autoHash;
15const Wyhash = std.hash.Wyhash;
16const Allocator = mem.Allocator;
17const builtin = @import("builtin");
18const hash_map = @This();
19
20pub fn AutoArrayHashMap(comptime K: type, comptime V: type) type {
21 return ArrayHashMap(K, V, getAutoHashFn(K), getAutoEqlFn(K), autoEqlIsCheap(K));
22}
23
24pub fn AutoArrayHashMapUnmanaged(comptime K: type, comptime V: type) type {
25 return ArrayHashMapUnmanaged(K, V, getAutoHashFn(K), getAutoEqlFn(K), autoEqlIsCheap(K));
26}
27
28/// Builtin hashmap for strings as keys.
29pub fn StringArrayHashMap(comptime V: type) type {
30 return ArrayHashMap([]const u8, V, hashString, eqlString, true);
31}
32
33pub fn StringArrayHashMapUnmanaged(comptime V: type) type {
34 return ArrayHashMapUnmanaged([]const u8, V, hashString, eqlString, true);
35}
36
37pub fn eqlString(a: []const u8, b: []const u8) bool {
38 return mem.eql(u8, a, b);
39}
40
41pub fn hashString(s: []const u8) u32 {
42 return @truncate(u32, std.hash.Wyhash.hash(0, s));
43}
44
45/// Insertion order is preserved.
46/// Deletions perform a "swap removal" on the entries list.
47/// Modifying the hash map while iterating is allowed, however one must understand
48/// the (well defined) behavior when mixing insertions and deletions with iteration.
49/// For a hash map that can be initialized directly that does not store an Allocator
50/// field, see `ArrayHashMapUnmanaged`.
51/// When `store_hash` is `false`, this data structure is biased towards cheap `eql`
52/// functions. It does not store each item's hash in the table. Setting `store_hash`
53/// to `true` incurs slightly more memory cost by storing each key's hash in the table
54/// but only has to call `eql` for hash collisions.
55/// If typical operations (except iteration over entries) need to be faster, prefer
56/// the alternative `std.HashMap`.
57pub fn ArrayHashMap(
58 comptime K: type,
59 comptime V: type,
60 comptime hash: fn (key: K) u32,
61 comptime eql: fn (a: K, b: K) bool,
62 comptime store_hash: bool,
63) type {
64 return struct {
65 unmanaged: Unmanaged,
66 allocator: *Allocator,
67
68 pub const Unmanaged = ArrayHashMapUnmanaged(K, V, hash, eql, store_hash);
69 pub const Entry = Unmanaged.Entry;
70 pub const Hash = Unmanaged.Hash;
71 pub const GetOrPutResult = Unmanaged.GetOrPutResult;
72
73 /// Deprecated. Iterate using `items`.
74 pub const Iterator = struct {
75 hm: *const Self,
76 /// Iterator through the entry array.
77 index: usize,
78
79 pub fn next(it: *Iterator) ?*Entry {
80 if (it.index >= it.hm.unmanaged.entries.items.len) return null;
81 const result = &it.hm.unmanaged.entries.items[it.index];
82 it.index += 1;
83 return result;
84 }
85
86 /// Reset the iterator to the initial index
87 pub fn reset(it: *Iterator) void {
88 it.index = 0;
89 }
90 };
91
92 const Self = @This();
93 const Index = Unmanaged.Index;
94
95 pub fn init(allocator: *Allocator) Self {
96 return .{
97 .unmanaged = .{},
98 .allocator = allocator,
99 };
100 }
101
102 pub fn deinit(self: *Self) void {
103 self.unmanaged.deinit(self.allocator);
104 self.* = undefined;
105 }
106
107 pub fn clearRetainingCapacity(self: *Self) void {
108 return self.unmanaged.clearRetainingCapacity();
109 }
110
111 pub fn clearAndFree(self: *Self) void {
112 return self.unmanaged.clearAndFree(self.allocator);
113 }
114
115 /// Deprecated. Use `items().len`.
116 pub fn count(self: Self) usize {
117 return self.items().len;
118 }
119
120 /// Deprecated. Iterate using `items`.
121 pub fn iterator(self: *const Self) Iterator {
122 return Iterator{
123 .hm = self,
124 .index = 0,
125 };
126 }
127
128 /// If key exists this function cannot fail.
129 /// If there is an existing item with `key`, then the result
130 /// `Entry` pointer points to it, and found_existing is true.
131 /// Otherwise, puts a new item with undefined value, and
132 /// the `Entry` pointer points to it. Caller should then initialize
133 /// the value (but not the key).
134 pub fn getOrPut(self: *Self, key: K) !GetOrPutResult {
135 return self.unmanaged.getOrPut(self.allocator, key);
136 }
137
138 /// If there is an existing item with `key`, then the result
139 /// `Entry` pointer points to it, and found_existing is true.
140 /// Otherwise, puts a new item with undefined value, and
141 /// the `Entry` pointer points to it. Caller should then initialize
142 /// the value (but not the key).
143 /// If a new entry needs to be stored, this function asserts there
144 /// is enough capacity to store it.
145 pub fn getOrPutAssumeCapacity(self: *Self, key: K) GetOrPutResult {
146 return self.unmanaged.getOrPutAssumeCapacity(key);
147 }
148
149 pub fn getOrPutValue(self: *Self, key: K, value: V) !*Entry {
150 return self.unmanaged.getOrPutValue(self.allocator, key, value);
151 }
152
153 /// Increases capacity, guaranteeing that insertions up until the
154 /// `expected_count` will not cause an allocation, and therefore cannot fail.
155 pub fn ensureCapacity(self: *Self, new_capacity: usize) !void {
156 return self.unmanaged.ensureCapacity(self.allocator, new_capacity);
157 }
158
159 /// Returns the number of total elements which may be present before it is
160 /// no longer guaranteed that no allocations will be performed.
161 pub fn capacity(self: *Self) usize {
162 return self.unmanaged.capacity();
163 }
164
165 /// Clobbers any existing data. To detect if a put would clobber
166 /// existing data, see `getOrPut`.
167 pub fn put(self: *Self, key: K, value: V) !void {
168 return self.unmanaged.put(self.allocator, key, value);
169 }
170
171 /// Inserts a key-value pair into the hash map, asserting that no previous
172 /// entry with the same key is already present
173 pub fn putNoClobber(self: *Self, key: K, value: V) !void {
174 return self.unmanaged.putNoClobber(self.allocator, key, value);
175 }
176
177 /// Asserts there is enough capacity to store the new key-value pair.
178 /// Clobbers any existing data. To detect if a put would clobber
179 /// existing data, see `getOrPutAssumeCapacity`.
180 pub fn putAssumeCapacity(self: *Self, key: K, value: V) void {
181 return self.unmanaged.putAssumeCapacity(key, value);
182 }
183
184 /// Asserts there is enough capacity to store the new key-value pair.
185 /// Asserts that it does not clobber any existing data.
186 /// To detect if a put would clobber existing data, see `getOrPutAssumeCapacity`.
187 pub fn putAssumeCapacityNoClobber(self: *Self, key: K, value: V) void {
188 return self.unmanaged.putAssumeCapacityNoClobber(key, value);
189 }
190
191 /// Inserts a new `Entry` into the hash map, returning the previous one, if any.
192 pub fn fetchPut(self: *Self, key: K, value: V) !?Entry {
193 return self.unmanaged.fetchPut(self.allocator, key, value);
194 }
195
196 /// Inserts a new `Entry` into the hash map, returning the previous one, if any.
197 /// If insertion happuns, asserts there is enough capacity without allocating.
198 pub fn fetchPutAssumeCapacity(self: *Self, key: K, value: V) ?Entry {
199 return self.unmanaged.fetchPutAssumeCapacity(key, value);
200 }
201
202 pub fn getEntry(self: Self, key: K) ?*Entry {
203 return self.unmanaged.getEntry(key);
204 }
205
206 pub fn getIndex(self: Self, key: K) ?usize {
207 return self.unmanaged.getIndex(key);
208 }
209
210 pub fn get(self: Self, key: K) ?V {
211 return self.unmanaged.get(key);
212 }
213
214 pub fn contains(self: Self, key: K) bool {
215 return self.unmanaged.contains(key);
216 }
217
218 /// If there is an `Entry` with a matching key, it is deleted from
219 /// the hash map, and then returned from this function.
220 pub fn remove(self: *Self, key: K) ?Entry {
221 return self.unmanaged.remove(key);
222 }
223
224 /// Asserts there is an `Entry` with matching key, deletes it from the hash map,
225 /// and discards it.
226 pub fn removeAssertDiscard(self: *Self, key: K) void {
227 return self.unmanaged.removeAssertDiscard(key);
228 }
229
230 pub fn items(self: Self) []Entry {
231 return self.unmanaged.items();
232 }
233
234 pub fn clone(self: Self) !Self {
235 var other = try self.unmanaged.clone(self.allocator);
236 return other.promote(self.allocator);
237 }
238 };
239}
240
241/// General purpose hash table.
242/// Insertion order is preserved.
243/// Deletions perform a "swap removal" on the entries list.
244/// Modifying the hash map while iterating is allowed, however one must understand
245/// the (well defined) behavior when mixing insertions and deletions with iteration.
246/// This type does not store an Allocator field - the Allocator must be passed in
247/// with each function call that requires it. See `ArrayHashMap` for a type that stores
248/// an Allocator field for convenience.
249/// Can be initialized directly using the default field values.
250/// This type is designed to have low overhead for small numbers of entries. When
251/// `store_hash` is `false` and the number of entries in the map is less than 9,
252/// the overhead cost of using `ArrayHashMapUnmanaged` rather than `std.ArrayList` is
253/// only a single pointer-sized integer.
254/// When `store_hash` is `false`, this data structure is biased towards cheap `eql`
255/// functions. It does not store each item's hash in the table. Setting `store_hash`
256/// to `true` incurs slightly more memory cost by storing each key's hash in the table
257/// but guarantees only one call to `eql` per insertion/deletion.
258pub fn ArrayHashMapUnmanaged(
259 comptime K: type,
260 comptime V: type,
261 comptime hash: fn (key: K) u32,
262 comptime eql: fn (a: K, b: K) bool,
263 comptime store_hash: bool,
264) type {
265 return struct {
266 /// It is permitted to access this field directly.
267 entries: std.ArrayListUnmanaged(Entry) = .{},
268
269 /// When entries length is less than `linear_scan_max`, this remains `null`.
270 /// Once entries length grows big enough, this field is allocated. There is
271 /// an IndexHeader followed by an array of Index(I) structs, where I is defined
272 /// by how many total indexes there are.
273 index_header: ?*IndexHeader = null,
274
275 /// Modifying the key is illegal behavior.
276 /// Modifying the value is allowed.
277 /// Entry pointers become invalid whenever this ArrayHashMap is modified,
278 /// unless `ensureCapacity` was previously used.
279 pub const Entry = struct {
280 /// This field is `void` if `store_hash` is `false`.
281 hash: Hash,
282 key: K,
283 value: V,
284 };
285
286 pub const Hash = if (store_hash) u32 else void;
287
288 pub const GetOrPutResult = struct {
289 entry: *Entry,
290 found_existing: bool,
291 };
292
293 pub const Managed = ArrayHashMap(K, V, hash, eql, store_hash);
294
295 const Self = @This();
296
297 const linear_scan_max = 8;
298
299 pub fn promote(self: Self, allocator: *Allocator) Managed {
300 return .{
301 .unmanaged = self,
302 .allocator = allocator,
303 };
304 }
305
306 pub fn deinit(self: *Self, allocator: *Allocator) void {
307 self.entries.deinit(allocator);
308 if (self.index_header) |header| {
309 header.free(allocator);
310 }
311 self.* = undefined;
312 }
313
314 pub fn clearRetainingCapacity(self: *Self) void {
315 self.entries.items.len = 0;
316 if (self.index_header) |header| {
317 header.max_distance_from_start_index = 0;
318 switch (header.capacityIndexType()) {
319 .u8 => mem.set(Index(u8), header.indexes(u8), Index(u8).empty),
320 .u16 => mem.set(Index(u16), header.indexes(u16), Index(u16).empty),
321 .u32 => mem.set(Index(u32), header.indexes(u32), Index(u32).empty),
322 .usize => mem.set(Index(usize), header.indexes(usize), Index(usize).empty),
323 }
324 }
325 }
326
327 pub fn clearAndFree(self: *Self, allocator: *Allocator) void {
328 self.entries.shrink(allocator, 0);
329 if (self.index_header) |header| {
330 header.free(allocator);
331 self.index_header = null;
332 }
333 }
334
335 /// If key exists this function cannot fail.
336 /// If there is an existing item with `key`, then the result
337 /// `Entry` pointer points to it, and found_existing is true.
338 /// Otherwise, puts a new item with undefined value, and
339 /// the `Entry` pointer points to it. Caller should then initialize
340 /// the value (but not the key).
341 pub fn getOrPut(self: *Self, allocator: *Allocator, key: K) !GetOrPutResult {
342 self.ensureCapacity(allocator, self.entries.items.len + 1) catch |err| {
343 // "If key exists this function cannot fail."
344 return GetOrPutResult{
345 .entry = self.getEntry(key) orelse return err,
346 .found_existing = true,
347 };
348 };
349 return self.getOrPutAssumeCapacity(key);
350 }
351
352 /// If there is an existing item with `key`, then the result
353 /// `Entry` pointer points to it, and found_existing is true.
354 /// Otherwise, puts a new item with undefined value, and
355 /// the `Entry` pointer points to it. Caller should then initialize
356 /// the value (but not the key).
357 /// If a new entry needs to be stored, this function asserts there
358 /// is enough capacity to store it.
359 pub fn getOrPutAssumeCapacity(self: *Self, key: K) GetOrPutResult {
360 const header = self.index_header orelse {
361 // Linear scan.
362 const h = if (store_hash) hash(key) else {};
363 for (self.entries.items) |*item| {
364 if (item.hash == h and eql(key, item.key)) {
365 return GetOrPutResult{
366 .entry = item,
367 .found_existing = true,
368 };
369 }
370 }
371 const new_entry = self.entries.addOneAssumeCapacity();
372 new_entry.* = .{
373 .hash = if (store_hash) h else {},
374 .key = key,
375 .value = undefined,
376 };
377 return GetOrPutResult{
378 .entry = new_entry,
379 .found_existing = false,
380 };
381 };
382
383 switch (header.capacityIndexType()) {
384 .u8 => return self.getOrPutInternal(key, header, u8),
385 .u16 => return self.getOrPutInternal(key, header, u16),
386 .u32 => return self.getOrPutInternal(key, header, u32),
387 .usize => return self.getOrPutInternal(key, header, usize),
388 }
389 }
390
391 pub fn getOrPutValue(self: *Self, allocator: *Allocator, key: K, value: V) !*Entry {
392 const res = try self.getOrPut(allocator, key);
393 if (!res.found_existing)
394 res.entry.value = value;
395
396 return res.entry;
397 }
398
399 /// Increases capacity, guaranteeing that insertions up until the
400 /// `expected_count` will not cause an allocation, and therefore cannot fail.
401 pub fn ensureCapacity(self: *Self, allocator: *Allocator, new_capacity: usize) !void {
402 try self.entries.ensureCapacity(allocator, new_capacity);
403 if (new_capacity <= linear_scan_max) return;
404
405 // Ensure that the indexes will be at most 60% full if
406 // `new_capacity` items are put into it.
407 const needed_len = new_capacity * 5 / 3;
408 if (self.index_header) |header| {
409 if (needed_len > header.indexes_len) {
410 // An overflow here would mean the amount of memory required would not
411 // be representable in the address space.
412 const new_indexes_len = math.ceilPowerOfTwo(usize, needed_len) catch unreachable;
413 const new_header = try IndexHeader.alloc(allocator, new_indexes_len);
414 self.insertAllEntriesIntoNewHeader(new_header);
415 header.free(allocator);
416 self.index_header = new_header;
417 }
418 } else {
419 // An overflow here would mean the amount of memory required would not
420 // be representable in the address space.
421 const new_indexes_len = math.ceilPowerOfTwo(usize, needed_len) catch unreachable;
422 const header = try IndexHeader.alloc(allocator, new_indexes_len);
423 self.insertAllEntriesIntoNewHeader(header);
424 self.index_header = header;
425 }
426 }
427
428 /// Returns the number of total elements which may be present before it is
429 /// no longer guaranteed that no allocations will be performed.
430 pub fn capacity(self: Self) usize {
431 const entry_cap = self.entries.capacity;
432 const header = self.index_header orelse return math.min(linear_scan_max, entry_cap);
433 const indexes_cap = (header.indexes_len + 1) * 3 / 4;
434 return math.min(entry_cap, indexes_cap);
435 }
436
437 /// Clobbers any existing data. To detect if a put would clobber
438 /// existing data, see `getOrPut`.
439 pub fn put(self: *Self, allocator: *Allocator, key: K, value: V) !void {
440 const result = try self.getOrPut(allocator, key);
441 result.entry.value = value;
442 }
443
444 /// Inserts a key-value pair into the hash map, asserting that no previous
445 /// entry with the same key is already present
446 pub fn putNoClobber(self: *Self, allocator: *Allocator, key: K, value: V) !void {
447 const result = try self.getOrPut(allocator, key);
448 assert(!result.found_existing);
449 result.entry.value = value;
450 }
451
452 /// Asserts there is enough capacity to store the new key-value pair.
453 /// Clobbers any existing data. To detect if a put would clobber
454 /// existing data, see `getOrPutAssumeCapacity`.
455 pub fn putAssumeCapacity(self: *Self, key: K, value: V) void {
456 const result = self.getOrPutAssumeCapacity(key);
457 result.entry.value = value;
458 }
459
460 /// Asserts there is enough capacity to store the new key-value pair.
461 /// Asserts that it does not clobber any existing data.
462 /// To detect if a put would clobber existing data, see `getOrPutAssumeCapacity`.
463 pub fn putAssumeCapacityNoClobber(self: *Self, key: K, value: V) void {
464 const result = self.getOrPutAssumeCapacity(key);
465 assert(!result.found_existing);
466 result.entry.value = value;
467 }
468
469 /// Inserts a new `Entry` into the hash map, returning the previous one, if any.
470 pub fn fetchPut(self: *Self, allocator: *Allocator, key: K, value: V) !?Entry {
471 const gop = try self.getOrPut(allocator, key);
472 var result: ?Entry = null;
473 if (gop.found_existing) {
474 result = gop.entry.*;
475 }
476 gop.entry.value = value;
477 return result;
478 }
479
480 /// Inserts a new `Entry` into the hash map, returning the previous one, if any.
481 /// If insertion happens, asserts there is enough capacity without allocating.
482 pub fn fetchPutAssumeCapacity(self: *Self, key: K, value: V) ?Entry {
483 const gop = self.getOrPutAssumeCapacity(key);
484 var result: ?Entry = null;
485 if (gop.found_existing) {
486 result = gop.entry.*;
487 }
488 gop.entry.value = value;
489 return result;
490 }
491
492 pub fn getEntry(self: Self, key: K) ?*Entry {
493 const index = self.getIndex(key) orelse return null;
494 return &self.entries.items[index];
495 }
496
497 pub fn getIndex(self: Self, key: K) ?usize {
498 const header = self.index_header orelse {
499 // Linear scan.
500 const h = if (store_hash) hash(key) else {};
501 for (self.entries.items) |*item, i| {
502 if (item.hash == h and eql(key, item.key)) {
503 return i;
504 }
505 }
506 return null;
507 };
508 switch (header.capacityIndexType()) {
509 .u8 => return self.getInternal(key, header, u8),
510 .u16 => return self.getInternal(key, header, u16),
511 .u32 => return self.getInternal(key, header, u32),
512 .usize => return self.getInternal(key, header, usize),
513 }
514 }
515
516 pub fn get(self: Self, key: K) ?V {
517 return if (self.getEntry(key)) |entry| entry.value else null;
518 }
519
520 pub fn contains(self: Self, key: K) bool {
521 return self.getEntry(key) != null;
522 }
523
524 /// If there is an `Entry` with a matching key, it is deleted from
525 /// the hash map, and then returned from this function.
526 pub fn remove(self: *Self, key: K) ?Entry {
527 const header = self.index_header orelse {
528 // Linear scan.
529 const h = if (store_hash) hash(key) else {};
530 for (self.entries.items) |item, i| {
531 if (item.hash == h and eql(key, item.key)) {
532 return self.entries.swapRemove(i);
533 }
534 }
535 return null;
536 };
537 switch (header.capacityIndexType()) {
538 .u8 => return self.removeInternal(key, header, u8),
539 .u16 => return self.removeInternal(key, header, u16),
540 .u32 => return self.removeInternal(key, header, u32),
541 .usize => return self.removeInternal(key, header, usize),
542 }
543 }
544
545 /// Asserts there is an `Entry` with matching key, deletes it from the hash map,
546 /// and discards it.
547 pub fn removeAssertDiscard(self: *Self, key: K) void {
548 assert(self.remove(key) != null);
549 }
550
551 pub fn items(self: Self) []Entry {
552 return self.entries.items;
553 }
554
555 pub fn clone(self: Self, allocator: *Allocator) !Self {
556 var other: Self = .{};
557 try other.entries.appendSlice(allocator, self.entries.items);
558
559 if (self.index_header) |header| {
560 const new_header = try IndexHeader.alloc(allocator, header.indexes_len);
561 other.insertAllEntriesIntoNewHeader(new_header);
562 other.index_header = new_header;
563 }
564 return other;
565 }
566
567 fn removeInternal(self: *Self, key: K, header: *IndexHeader, comptime I: type) ?Entry {
568 const indexes = header.indexes(I);
569 const h = hash(key);
570 const start_index = header.constrainIndex(h);
571 var roll_over: usize = 0;
572 while (roll_over <= header.max_distance_from_start_index) : (roll_over += 1) {
573 const index_index = header.constrainIndex(start_index + roll_over);
574 var index = &indexes[index_index];
575 if (index.isEmpty())
576 return null;
577
578 const entry = &self.entries.items[index.entry_index];
579
580 const hash_match = if (store_hash) h == entry.hash else true;
581 if (!hash_match or !eql(key, entry.key))
582 continue;
583
584 const removed_entry = self.entries.swapRemove(index.entry_index);
585 if (self.entries.items.len > 0 and self.entries.items.len != index.entry_index) {
586 // Because of the swap remove, now we need to update the index that was
587 // pointing to the last entry and is now pointing to this removed item slot.
588 self.updateEntryIndex(header, self.entries.items.len, index.entry_index, I, indexes);
589 }
590
591 // Now we have to shift over the following indexes.
592 roll_over += 1;
593 while (roll_over < header.indexes_len) : (roll_over += 1) {
594 const next_index_index = header.constrainIndex(start_index + roll_over);
595 const next_index = &indexes[next_index_index];
596 if (next_index.isEmpty() or next_index.distance_from_start_index == 0) {
597 index.setEmpty();
598 return removed_entry;
599 }
600 index.* = next_index.*;
601 index.distance_from_start_index -= 1;
602 index = next_index;
603 }
604 unreachable;
605 }
606 return null;
607 }
608
609 fn updateEntryIndex(
610 self: *Self,
611 header: *IndexHeader,
612 old_entry_index: usize,
613 new_entry_index: usize,
614 comptime I: type,
615 indexes: []Index(I),
616 ) void {
617 const h = if (store_hash) self.entries.items[new_entry_index].hash else hash(self.entries.items[new_entry_index].key);
618 const start_index = header.constrainIndex(h);
619 var roll_over: usize = 0;
620 while (roll_over <= header.max_distance_from_start_index) : (roll_over += 1) {
621 const index_index = header.constrainIndex(start_index + roll_over);
622 const index = &indexes[index_index];
623 if (index.entry_index == old_entry_index) {
624 index.entry_index = @intCast(I, new_entry_index);
625 return;
626 }
627 }
628 unreachable;
629 }
630
631 /// Must ensureCapacity before calling this.
632 fn getOrPutInternal(self: *Self, key: K, header: *IndexHeader, comptime I: type) GetOrPutResult {
633 const indexes = header.indexes(I);
634 const h = hash(key);
635 const start_index = header.constrainIndex(h);
636 var roll_over: usize = 0;
637 var distance_from_start_index: usize = 0;
638 while (roll_over <= header.indexes_len) : ({
639 roll_over += 1;
640 distance_from_start_index += 1;
641 }) {
642 const index_index = header.constrainIndex(start_index + roll_over);
643 const index = indexes[index_index];
644 if (index.isEmpty()) {
645 indexes[index_index] = .{
646 .distance_from_start_index = @intCast(I, distance_from_start_index),
647 .entry_index = @intCast(I, self.entries.items.len),
648 };
649 header.maybeBumpMax(distance_from_start_index);
650 const new_entry = self.entries.addOneAssumeCapacity();
651 new_entry.* = .{
652 .hash = if (store_hash) h else {},
653 .key = key,
654 .value = undefined,
655 };
656 return .{
657 .found_existing = false,
658 .entry = new_entry,
659 };
660 }
661
662 // This pointer survives the following append because we call
663 // entries.ensureCapacity before getOrPutInternal.
664 const entry = &self.entries.items[index.entry_index];
665 const hash_match = if (store_hash) h == entry.hash else true;
666 if (hash_match and eql(key, entry.key)) {
667 return .{
668 .found_existing = true,
669 .entry = entry,
670 };
671 }
672 if (index.distance_from_start_index < distance_from_start_index) {
673 // In this case, we did not find the item. We will put a new entry.
674 // However, we will use this index for the new entry, and move
675 // the previous index down the line, to keep the max_distance_from_start_index
676 // as small as possible.
677 indexes[index_index] = .{
678 .distance_from_start_index = @intCast(I, distance_from_start_index),
679 .entry_index = @intCast(I, self.entries.items.len),
680 };
681 header.maybeBumpMax(distance_from_start_index);
682 const new_entry = self.entries.addOneAssumeCapacity();
683 new_entry.* = .{
684 .hash = if (store_hash) h else {},
685 .key = key,
686 .value = undefined,
687 };
688
689 distance_from_start_index = index.distance_from_start_index;
690 var prev_entry_index = index.entry_index;
691
692 // Find somewhere to put the index we replaced by shifting
693 // following indexes backwards.
694 roll_over += 1;
695 distance_from_start_index += 1;
696 while (roll_over < header.indexes_len) : ({
697 roll_over += 1;
698 distance_from_start_index += 1;
699 }) {
700 const next_index_index = header.constrainIndex(start_index + roll_over);
701 const next_index = indexes[next_index_index];
702 if (next_index.isEmpty()) {
703 header.maybeBumpMax(distance_from_start_index);
704 indexes[next_index_index] = .{
705 .entry_index = prev_entry_index,
706 .distance_from_start_index = @intCast(I, distance_from_start_index),
707 };
708 return .{
709 .found_existing = false,
710 .entry = new_entry,
711 };
712 }
713 if (next_index.distance_from_start_index < distance_from_start_index) {
714 header.maybeBumpMax(distance_from_start_index);
715 indexes[next_index_index] = .{
716 .entry_index = prev_entry_index,
717 .distance_from_start_index = @intCast(I, distance_from_start_index),
718 };
719 distance_from_start_index = next_index.distance_from_start_index;
720 prev_entry_index = next_index.entry_index;
721 }
722 }
723 unreachable;
724 }
725 }
726 unreachable;
727 }
728
729 fn getInternal(self: Self, key: K, header: *IndexHeader, comptime I: type) ?usize {
730 const indexes = header.indexes(I);
731 const h = hash(key);
732 const start_index = header.constrainIndex(h);
733 var roll_over: usize = 0;
734 while (roll_over <= header.max_distance_from_start_index) : (roll_over += 1) {
735 const index_index = header.constrainIndex(start_index + roll_over);
736 const index = indexes[index_index];
737 if (index.isEmpty())
738 return null;
739
740 const entry = &self.entries.items[index.entry_index];
741 const hash_match = if (store_hash) h == entry.hash else true;
742 if (hash_match and eql(key, entry.key))
743 return index.entry_index;
744 }
745 return null;
746 }
747
748 fn insertAllEntriesIntoNewHeader(self: *Self, header: *IndexHeader) void {
749 switch (header.capacityIndexType()) {
750 .u8 => return self.insertAllEntriesIntoNewHeaderGeneric(header, u8),
751 .u16 => return self.insertAllEntriesIntoNewHeaderGeneric(header, u16),
752 .u32 => return self.insertAllEntriesIntoNewHeaderGeneric(header, u32),
753 .usize => return self.insertAllEntriesIntoNewHeaderGeneric(header, usize),
754 }
755 }
756
757 fn insertAllEntriesIntoNewHeaderGeneric(self: *Self, header: *IndexHeader, comptime I: type) void {
758 const indexes = header.indexes(I);
759 entry_loop: for (self.entries.items) |entry, i| {
760 const h = if (store_hash) entry.hash else hash(entry.key);
761 const start_index = header.constrainIndex(h);
762 var entry_index = i;
763 var roll_over: usize = 0;
764 var distance_from_start_index: usize = 0;
765 while (roll_over < header.indexes_len) : ({
766 roll_over += 1;
767 distance_from_start_index += 1;
768 }) {
769 const index_index = header.constrainIndex(start_index + roll_over);
770 const next_index = indexes[index_index];
771 if (next_index.isEmpty()) {
772 header.maybeBumpMax(distance_from_start_index);
773 indexes[index_index] = .{
774 .distance_from_start_index = @intCast(I, distance_from_start_index),
775 .entry_index = @intCast(I, entry_index),
776 };
777 continue :entry_loop;
778 }
779 if (next_index.distance_from_start_index < distance_from_start_index) {
780 header.maybeBumpMax(distance_from_start_index);
781 indexes[index_index] = .{
782 .distance_from_start_index = @intCast(I, distance_from_start_index),
783 .entry_index = @intCast(I, entry_index),
784 };
785 distance_from_start_index = next_index.distance_from_start_index;
786 entry_index = next_index.entry_index;
787 }
788 }
789 unreachable;
790 }
791 }
792 };
793}
794
795const CapacityIndexType = enum { u8, u16, u32, usize };
796
797fn capacityIndexType(indexes_len: usize) CapacityIndexType {
798 if (indexes_len < math.maxInt(u8))
799 return .u8;
800 if (indexes_len < math.maxInt(u16))
801 return .u16;
802 if (indexes_len < math.maxInt(u32))
803 return .u32;
804 return .usize;
805}
806
807fn capacityIndexSize(indexes_len: usize) usize {
808 switch (capacityIndexType(indexes_len)) {
809 .u8 => return @sizeOf(Index(u8)),
810 .u16 => return @sizeOf(Index(u16)),
811 .u32 => return @sizeOf(Index(u32)),
812 .usize => return @sizeOf(Index(usize)),
813 }
814}
815
816fn Index(comptime I: type) type {
817 return extern struct {
818 entry_index: I,
819 distance_from_start_index: I,
820
821 const Self = @This();
822
823 const empty = Self{
824 .entry_index = math.maxInt(I),
825 .distance_from_start_index = undefined,
826 };
827
828 fn isEmpty(idx: Self) bool {
829 return idx.entry_index == math.maxInt(I);
830 }
831
832 fn setEmpty(idx: *Self) void {
833 idx.entry_index = math.maxInt(I);
834 }
835 };
836}
837
838/// This struct is trailed by an array of `Index(I)`, where `I`
839/// and the array length are determined by `indexes_len`.
840const IndexHeader = struct {
841 max_distance_from_start_index: usize,
842 indexes_len: usize,
843
844 fn constrainIndex(header: IndexHeader, i: usize) usize {
845 // This is an optimization for modulo of power of two integers;
846 // it requires `indexes_len` to always be a power of two.
847 return i & (header.indexes_len - 1);
848 }
849
850 fn indexes(header: *IndexHeader, comptime I: type) []Index(I) {
851 const start = @ptrCast([*]Index(I), @ptrCast([*]u8, header) + @sizeOf(IndexHeader));
852 return start[0..header.indexes_len];
853 }
854
855 fn capacityIndexType(header: IndexHeader) CapacityIndexType {
856 return hash_map.capacityIndexType(header.indexes_len);
857 }
858
859 fn maybeBumpMax(header: *IndexHeader, distance_from_start_index: usize) void {
860 if (distance_from_start_index > header.max_distance_from_start_index) {
861 header.max_distance_from_start_index = distance_from_start_index;
862 }
863 }
864
865 fn alloc(allocator: *Allocator, len: usize) !*IndexHeader {
866 const index_size = hash_map.capacityIndexSize(len);
867 const nbytes = @sizeOf(IndexHeader) + index_size * len;
868 const bytes = try allocator.allocAdvanced(u8, @alignOf(IndexHeader), nbytes, .exact);
869 @memset(bytes.ptr + @sizeOf(IndexHeader), 0xff, bytes.len - @sizeOf(IndexHeader));
870 const result = @ptrCast(*IndexHeader, bytes.ptr);
871 result.* = .{
872 .max_distance_from_start_index = 0,
873 .indexes_len = len,
874 };
875 return result;
876 }
877
878 fn free(header: *IndexHeader, allocator: *Allocator) void {
879 const index_size = hash_map.capacityIndexSize(header.indexes_len);
880 const ptr = @ptrCast([*]u8, header);
881 const slice = ptr[0 .. @sizeOf(IndexHeader) + header.indexes_len * index_size];
882 allocator.free(slice);
883 }
884};
885
886test "basic hash map usage" {
887 var map = AutoArrayHashMap(i32, i32).init(std.testing.allocator);
888 defer map.deinit();
889
890 testing.expect((try map.fetchPut(1, 11)) == null);
891 testing.expect((try map.fetchPut(2, 22)) == null);
892 testing.expect((try map.fetchPut(3, 33)) == null);
893 testing.expect((try map.fetchPut(4, 44)) == null);
894
895 try map.putNoClobber(5, 55);
896 testing.expect((try map.fetchPut(5, 66)).?.value == 55);
897 testing.expect((try map.fetchPut(5, 55)).?.value == 66);
898
899 const gop1 = try map.getOrPut(5);
900 testing.expect(gop1.found_existing == true);
901 testing.expect(gop1.entry.value == 55);
902 gop1.entry.value = 77;
903 testing.expect(map.getEntry(5).?.value == 77);
904
905 const gop2 = try map.getOrPut(99);
906 testing.expect(gop2.found_existing == false);
907 gop2.entry.value = 42;
908 testing.expect(map.getEntry(99).?.value == 42);
909
910 const gop3 = try map.getOrPutValue(5, 5);
911 testing.expect(gop3.value == 77);
912
913 const gop4 = try map.getOrPutValue(100, 41);
914 testing.expect(gop4.value == 41);
915
916 testing.expect(map.contains(2));
917 testing.expect(map.getEntry(2).?.value == 22);
918 testing.expect(map.get(2).? == 22);
919
920 const rmv1 = map.remove(2);
921 testing.expect(rmv1.?.key == 2);
922 testing.expect(rmv1.?.value == 22);
923 testing.expect(map.remove(2) == null);
924 testing.expect(map.getEntry(2) == null);
925 testing.expect(map.get(2) == null);
926
927 map.removeAssertDiscard(3);
928}
929
930test "iterator hash map" {
931 // https://github.com/ziglang/zig/issues/5127
932 if (std.Target.current.cpu.arch == .mips) return error.SkipZigTest;
933
934 var reset_map = AutoArrayHashMap(i32, i32).init(std.testing.allocator);
935 defer reset_map.deinit();
936
937 // test ensureCapacity with a 0 parameter
938 try reset_map.ensureCapacity(0);
939
940 try reset_map.putNoClobber(0, 11);
941 try reset_map.putNoClobber(1, 22);
942 try reset_map.putNoClobber(2, 33);
943
944 var keys = [_]i32{
945 0, 2, 1,
946 };
947
948 var values = [_]i32{
949 11, 33, 22,
950 };
951
952 var buffer = [_]i32{
953 0, 0, 0,
954 };
955
956 var it = reset_map.iterator();
957 const first_entry = it.next().?;
958 it.reset();
959
960 var count: usize = 0;
961 while (it.next()) |entry| : (count += 1) {
962 buffer[@intCast(usize, entry.key)] = entry.value;
963 }
964 testing.expect(count == 3);
965 testing.expect(it.next() == null);
966
967 for (buffer) |v, i| {
968 testing.expect(buffer[@intCast(usize, keys[i])] == values[i]);
969 }
970
971 it.reset();
972 count = 0;
973 while (it.next()) |entry| {
974 buffer[@intCast(usize, entry.key)] = entry.value;
975 count += 1;
976 if (count >= 2) break;
977 }
978
979 for (buffer[0..2]) |v, i| {
980 testing.expect(buffer[@intCast(usize, keys[i])] == values[i]);
981 }
982
983 it.reset();
984 var entry = it.next().?;
985 testing.expect(entry.key == first_entry.key);
986 testing.expect(entry.value == first_entry.value);
987}
988
989test "ensure capacity" {
990 var map = AutoArrayHashMap(i32, i32).init(std.testing.allocator);
991 defer map.deinit();
992
993 try map.ensureCapacity(20);
994 const initial_capacity = map.capacity();
995 testing.expect(initial_capacity >= 20);
996 var i: i32 = 0;
997 while (i < 20) : (i += 1) {
998 testing.expect(map.fetchPutAssumeCapacity(i, i + 10) == null);
999 }
1000 // shouldn't resize from putAssumeCapacity
1001 testing.expect(initial_capacity == map.capacity());
1002}
1003
1004test "clone" {
1005 var original = AutoArrayHashMap(i32, i32).init(std.testing.allocator);
1006 defer original.deinit();
1007
1008 // put more than `linear_scan_max` so we can test that the index header is properly cloned
1009 var i: u8 = 0;
1010 while (i < 10) : (i += 1) {
1011 try original.putNoClobber(i, i * 10);
1012 }
1013
1014 var copy = try original.clone();
1015 defer copy.deinit();
1016
1017 i = 0;
1018 while (i < 10) : (i += 1) {
1019 testing.expect(copy.get(i).? == i * 10);
1020 }
1021}
1022
1023pub fn getHashPtrAddrFn(comptime K: type) (fn (K) u32) {
1024 return struct {
1025 fn hash(key: K) u32 {
1026 return getAutoHashFn(usize)(@ptrToInt(key));
1027 }
1028 }.hash;
1029}
1030
1031pub fn getTrivialEqlFn(comptime K: type) (fn (K, K) bool) {
1032 return struct {
1033 fn eql(a: K, b: K) bool {
1034 return a == b;
1035 }
1036 }.eql;
1037}
1038
1039pub fn getAutoHashFn(comptime K: type) (fn (K) u32) {
1040 return struct {
1041 fn hash(key: K) u32 {
1042 if (comptime trait.hasUniqueRepresentation(K)) {
1043 return @truncate(u32, Wyhash.hash(0, std.mem.asBytes(&key)));
1044 } else {
1045 var hasher = Wyhash.init(0);
1046 autoHash(&hasher, key);
1047 return @truncate(u32, hasher.final());
1048 }
1049 }
1050 }.hash;
1051}
1052
1053pub fn getAutoEqlFn(comptime K: type) (fn (K, K) bool) {
1054 return struct {
1055 fn eql(a: K, b: K) bool {
1056 return meta.eql(a, b);
1057 }
1058 }.eql;
1059}
1060
1061pub fn autoEqlIsCheap(comptime K: type) bool {
1062 return switch (@typeInfo(K)) {
1063 .Bool,
1064 .Int,
1065 .Float,
1066 .Pointer,
1067 .ComptimeFloat,
1068 .ComptimeInt,
1069 .Enum,
1070 .Fn,
1071 .ErrorSet,
1072 .AnyFrame,
1073 .EnumLiteral,
1074 => true,
1075 else => false,
1076 };
1077}
1078
1079pub fn getAutoHashStratFn(comptime K: type, comptime strategy: std.hash.Strategy) (fn (K) u32) {
1080 return struct {
1081 fn hash(key: K) u32 {
1082 var hasher = Wyhash.init(0);
1083 std.hash.autoHashStrat(&hasher, key, strategy);
1084 return @truncate(u32, hasher.final());
1085 }
1086 }.hash;
1087}
lib/std/buf_set.zig+2-1
......@@ -20,7 +20,8 @@ pub const BufSet = struct {
2020 }
2121
2222 pub fn deinit(self: *BufSet) void {
23 for (self.hash_map.items()) |entry| {
23 var it = self.hash_map.iterator();
24 while (it.next()) |entry| {
2425 self.free(entry.key);
2526 }
2627 self.hash_map.deinit();
lib/std/hash_map.zig+846-697
......@@ -4,91 +4,94 @@
44// The MIT license requires this copyright notice to be included in all copies
55// and substantial portions of the software.
66const std = @import("std.zig");
7const debug = std.debug;
7const builtin = @import("builtin");
88const assert = debug.assert;
9const testing = std.testing;
9const autoHash = std.hash.autoHash;
10const debug = std.debug;
11const warn = debug.warn;
1012const math = std.math;
1113const mem = std.mem;
1214const meta = std.meta;
1315const trait = meta.trait;
14const autoHash = std.hash.autoHash;
15const Wyhash = std.hash.Wyhash;
1616const Allocator = mem.Allocator;
17const builtin = @import("builtin");
18const hash_map = @This();
17const Wyhash = std.hash.Wyhash;
18
19pub fn getAutoHashFn(comptime K: type) (fn (K) u64) {
20 return struct {
21 fn hash(key: K) u64 {
22 if (comptime trait.hasUniqueRepresentation(K)) {
23 return Wyhash.hash(0, std.mem.asBytes(&key));
24 } else {
25 var hasher = Wyhash.init(0);
26 autoHash(&hasher, key);
27 return hasher.final();
28 }
29 }
30 }.hash;
31}
32
33pub fn getAutoEqlFn(comptime K: type) (fn (K, K) bool) {
34 return struct {
35 fn eql(a: K, b: K) bool {
36 return meta.eql(a, b);
37 }
38 }.eql;
39}
1940
2041pub fn AutoHashMap(comptime K: type, comptime V: type) type {
21 return HashMap(K, V, getAutoHashFn(K), getAutoEqlFn(K), autoEqlIsCheap(K));
42 return HashMap(K, V, getAutoHashFn(K), getAutoEqlFn(K), DefaultMaxLoadPercentage);
2243}
2344
2445pub fn AutoHashMapUnmanaged(comptime K: type, comptime V: type) type {
25 return HashMapUnmanaged(K, V, getAutoHashFn(K), getAutoEqlFn(K), autoEqlIsCheap(K));
46 return HashMapUnmanaged(K, V, getAutoHashFn(K), getAutoEqlFn(K), DefaultMaxLoadPercentage);
2647}
2748
2849/// Builtin hashmap for strings as keys.
2950pub fn StringHashMap(comptime V: type) type {
30 return HashMap([]const u8, V, hashString, eqlString, true);
51 return HashMap([]const u8, V, hashString, eqlString, DefaultMaxLoadPercentage);
3152}
3253
3354pub fn StringHashMapUnmanaged(comptime V: type) type {
34 return HashMapUnmanaged([]const u8, V, hashString, eqlString, true);
55 return HashMapUnmanaged([]const u8, V, hashString, eqlString, DefaultMaxLoadPercentage);
3556}
3657
3758pub fn eqlString(a: []const u8, b: []const u8) bool {
3859 return mem.eql(u8, a, b);
3960}
4061
41pub fn hashString(s: []const u8) u32 {
42 return @truncate(u32, std.hash.Wyhash.hash(0, s));
62pub fn hashString(s: []const u8) u64 {
63 return std.hash.Wyhash.hash(0, s);
4364}
4465
45/// Insertion order is preserved.
46/// Deletions perform a "swap removal" on the entries list.
47/// Modifying the hash map while iterating is allowed, however one must understand
48/// the (well defined) behavior when mixing insertions and deletions with iteration.
66pub const DefaultMaxLoadPercentage = 80;
67
68/// General purpose hash table.
69/// No order is guaranteed and any modification invalidates live iterators.
70/// It provides fast operations (lookup, insertion, deletion) with quite high
71/// load factors (up to 80% by default) for a low memory usage.
4972/// For a hash map that can be initialized directly that does not store an Allocator
5073/// field, see `HashMapUnmanaged`.
51/// When `store_hash` is `false`, this data structure is biased towards cheap `eql`
52/// functions. It does not store each item's hash in the table. Setting `store_hash`
53/// to `true` incurs slightly more memory cost by storing each key's hash in the table
54/// but only has to call `eql` for hash collisions.
74/// If iterating over the table entries is a strong usecase and needs to be fast,
75/// prefer the alternative `std.ArrayHashMap`.
5576pub fn HashMap(
5677 comptime K: type,
5778 comptime V: type,
58 comptime hash: fn (key: K) u32,
59 comptime eql: fn (a: K, b: K) bool,
60 comptime store_hash: bool,
79 comptime hashFn: fn (key: K) u64,
80 comptime eqlFn: fn (a: K, b: K) bool,
81 comptime MaxLoadPercentage: u64,
6182) type {
6283 return struct {
6384 unmanaged: Unmanaged,
6485 allocator: *Allocator,
6586
66 pub const Unmanaged = HashMapUnmanaged(K, V, hash, eql, store_hash);
87 pub const Unmanaged = HashMapUnmanaged(K, V, hashFn, eqlFn, MaxLoadPercentage);
6788 pub const Entry = Unmanaged.Entry;
6889 pub const Hash = Unmanaged.Hash;
90 pub const Iterator = Unmanaged.Iterator;
91 pub const Size = Unmanaged.Size;
6992 pub const GetOrPutResult = Unmanaged.GetOrPutResult;
7093
71 /// Deprecated. Iterate using `items`.
72 pub const Iterator = struct {
73 hm: *const Self,
74 /// Iterator through the entry array.
75 index: usize,
76
77 pub fn next(it: *Iterator) ?*Entry {
78 if (it.index >= it.hm.unmanaged.entries.items.len) return null;
79 const result = &it.hm.unmanaged.entries.items[it.index];
80 it.index += 1;
81 return result;
82 }
83
84 /// Reset the iterator to the initial index
85 pub fn reset(it: *Iterator) void {
86 it.index = 0;
87 }
88 };
89
9094 const Self = @This();
91 const Index = Unmanaged.Index;
9295
9396 pub fn init(allocator: *Allocator) Self {
9497 return .{
......@@ -110,17 +113,12 @@ pub fn HashMap(
110113 return self.unmanaged.clearAndFree(self.allocator);
111114 }
112115
113 /// Deprecated. Use `items().len`.
114116 pub fn count(self: Self) usize {
115 return self.items().len;
117 return self.unmanaged.count();
116118 }
117119
118 /// Deprecated. Iterate using `items`.
119120 pub fn iterator(self: *const Self) Iterator {
120 return Iterator{
121 .hm = self,
122 .index = 0,
123 };
121 return self.unmanaged.iterator();
124122 }
125123
126124 /// If key exists this function cannot fail.
......@@ -150,13 +148,13 @@ pub fn HashMap(
150148
151149 /// Increases capacity, guaranteeing that insertions up until the
152150 /// `expected_count` will not cause an allocation, and therefore cannot fail.
153 pub fn ensureCapacity(self: *Self, new_capacity: usize) !void {
154 return self.unmanaged.ensureCapacity(self.allocator, new_capacity);
151 pub fn ensureCapacity(self: *Self, expected_count: Size) !void {
152 return self.unmanaged.ensureCapacity(self.allocator, expected_count);
155153 }
156154
157155 /// Returns the number of total elements which may be present before it is
158156 /// no longer guaranteed that no allocations will be performed.
159 pub fn capacity(self: *Self) usize {
157 pub fn capacity(self: *Self) Size {
160158 return self.unmanaged.capacity();
161159 }
162160
......@@ -197,18 +195,14 @@ pub fn HashMap(
197195 return self.unmanaged.fetchPutAssumeCapacity(key, value);
198196 }
199197
200 pub fn getEntry(self: Self, key: K) ?*Entry {
201 return self.unmanaged.getEntry(key);
202 }
203
204 pub fn getIndex(self: Self, key: K) ?usize {
205 return self.unmanaged.getIndex(key);
206 }
207
208198 pub fn get(self: Self, key: K) ?V {
209199 return self.unmanaged.get(key);
210200 }
211201
202 pub fn getEntry(self: Self, key: K) ?*Entry {
203 return self.unmanaged.getEntry(key);
204 }
205
212206 pub fn contains(self: Self, key: K) bool {
213207 return self.unmanaged.contains(key);
214208 }
......@@ -225,10 +219,6 @@ pub fn HashMap(
225219 return self.unmanaged.removeAssertDiscard(key);
226220 }
227221
228 pub fn items(self: Self) []Entry {
229 return self.unmanaged.items();
230 }
231
232222 pub fn clone(self: Self) !Self {
233223 var other = try self.unmanaged.clone(self.allocator);
234224 return other.promote(self.allocator);
......@@ -236,63 +226,152 @@ pub fn HashMap(
236226 };
237227}
238228
239/// General purpose hash table.
240/// Insertion order is preserved.
241/// Deletions perform a "swap removal" on the entries list.
242/// Modifying the hash map while iterating is allowed, however one must understand
243/// the (well defined) behavior when mixing insertions and deletions with iteration.
244/// This type does not store an Allocator field - the Allocator must be passed in
245/// with each function call that requires it. See `HashMap` for a type that stores
246/// an Allocator field for convenience.
247/// Can be initialized directly using the default field values.
248/// This type is designed to have low overhead for small numbers of entries. When
249/// `store_hash` is `false` and the number of entries in the map is less than 9,
250/// the overhead cost of using `HashMapUnmanaged` rather than `std.ArrayList` is
251/// only a single pointer-sized integer.
252/// When `store_hash` is `false`, this data structure is biased towards cheap `eql`
253/// functions. It does not store each item's hash in the table. Setting `store_hash`
254/// to `true` incurs slightly more memory cost by storing each key's hash in the table
255/// but guarantees only one call to `eql` per insertion/deletion.
229/// A HashMap based on open addressing and linear probing.
230/// A lookup or modification typically occurs only 2 cache misses.
231/// No order is guaranteed and any modification invalidates live iterators.
232/// It achieves good performance with quite high load factors (by default,
233/// grow is triggered at 80% full) and only one byte of overhead per element.
234/// The struct itself is only 16 bytes for a small footprint. This comes at
235/// the price of handling size with u32, which should be reasonnable enough
236/// for almost all uses.
237/// Deletions are achieved with tombstones.
256238pub fn HashMapUnmanaged(
257239 comptime K: type,
258240 comptime V: type,
259 comptime hash: fn (key: K) u32,
260 comptime eql: fn (a: K, b: K) bool,
261 comptime store_hash: bool,
241 hashFn: fn (key: K) u64,
242 eqlFn: fn (a: K, b: K) bool,
243 comptime MaxLoadPercentage: u64,
262244) type {
245 comptime assert(MaxLoadPercentage > 0 and MaxLoadPercentage < 100);
246
263247 return struct {
264 /// It is permitted to access this field directly.
265 entries: std.ArrayListUnmanaged(Entry) = .{},
266
267 /// When entries length is less than `linear_scan_max`, this remains `null`.
268 /// Once entries length grows big enough, this field is allocated. There is
269 /// an IndexHeader followed by an array of Index(I) structs, where I is defined
270 /// by how many total indexes there are.
271 index_header: ?*IndexHeader = null,
272
273 /// Modifying the key is illegal behavior.
274 /// Modifying the value is allowed.
275 /// Entry pointers become invalid whenever this HashMap is modified,
276 /// unless `ensureCapacity` was previously used.
248 const Self = @This();
249
250 // This is actually a midway pointer to the single buffer containing
251 // a `Header` field, the `Metadata`s and `Entry`s.
252 // At `-@sizeOf(Header)` is the Header field.
253 // At `sizeOf(Metadata) * capacity + offset`, which is pointed to by
254 // self.header().entries, is the array of entries.
255 // This means that the hashmap only holds one live allocation, to
256 // reduce memory fragmentation and struct size.
257 /// Pointer to the metadata.
258 metadata: ?[*]Metadata = null,
259
260 /// Current number of elements in the hashmap.
261 size: Size = 0,
262
263 // Having a countdown to grow reduces the number of instructions to
264 // execute when determining if the hashmap has enough capacity already.
265 /// Number of available slots before a grow is needed to satisfy the
266 /// `MaxLoadPercentage`.
267 available: Size = 0,
268
269 // This is purely empirical and not a /very smart magic constant™/.
270 /// Capacity of the first grow when bootstrapping the hashmap.
271 const MinimalCapacity = 8;
272
273 // This hashmap is specially designed for sizes that fit in a u32.
274 const Size = u32;
275
276 // u64 hashes guarantee us that the fingerprint bits will never be used
277 // to compute the index of a slot, maximizing the use of entropy.
278 const Hash = u64;
279
277280 pub const Entry = struct {
278 /// This field is `void` if `store_hash` is `false`.
279 hash: Hash,
280281 key: K,
281282 value: V,
282283 };
283284
284 pub const Hash = if (store_hash) u32 else void;
285 const Header = packed struct {
286 entries: [*]Entry,
287 capacity: Size,
288 };
289
290 /// Metadata for a slot. It can be in three states: empty, used or
291 /// tombstone. Tombstones indicate that an entry was previously used,
292 /// they are a simple way to handle removal.
293 /// To this state, we add 6 bits from the slot's key hash. These are
294 /// used as a fast way to disambiguate between entries without
295 /// having to use the equality function. If two fingerprints are
296 /// different, we know that we don't have to compare the keys at all.
297 /// The 6 bits are the highest ones from a 64 bit hash. This way, not
298 /// only we use the `log2(capacity)` lowest bits from the hash to determine
299 /// a slot index, but we use 6 more bits to quickly resolve collisions
300 /// when multiple elements with different hashes end up wanting to be in / the same slot.
301 /// Not using the equality function means we don't have to read into
302 /// the entries array, avoiding a likely cache miss.
303 const Metadata = packed struct {
304 const FingerPrint = u6;
305
306 used: u1 = 0,
307 tombstone: u1 = 0,
308 fingerprint: FingerPrint = 0,
309
310 pub fn isUsed(self: Metadata) bool {
311 return self.used == 1;
312 }
313
314 pub fn isTombstone(self: Metadata) bool {
315 return self.tombstone == 1;
316 }
317
318 pub fn takeFingerprint(hash: Hash) FingerPrint {
319 const hash_bits = @typeInfo(Hash).Int.bits;
320 const fp_bits = @typeInfo(FingerPrint).Int.bits;
321 return @truncate(FingerPrint, hash >> (hash_bits - fp_bits));
322 }
323
324 pub fn fill(self: *Metadata, fp: FingerPrint) void {
325 self.used = 1;
326 self.tombstone = 0;
327 self.fingerprint = fp;
328 }
329
330 pub fn remove(self: *Metadata) void {
331 self.used = 0;
332 self.tombstone = 1;
333 self.fingerprint = 0;
334 }
335 };
336
337 comptime {
338 assert(@sizeOf(Metadata) == 1);
339 assert(@alignOf(Metadata) == 1);
340 }
341
342 const Iterator = struct {
343 hm: *const Self,
344 index: Size = 0,
345
346 pub fn next(it: *Iterator) ?*Entry {
347 assert(it.index <= it.hm.capacity());
348 if (it.hm.size == 0) return null;
349
350 const cap = it.hm.capacity();
351 const end = it.hm.metadata.? + cap;
352 var metadata = it.hm.metadata.? + it.index;
353
354 while (metadata != end) : ({
355 metadata += 1;
356 it.index += 1;
357 }) {
358 if (metadata[0].isUsed()) {
359 const entry = &it.hm.entries()[it.index];
360 it.index += 1;
361 return entry;
362 }
363 }
364
365 return null;
366 }
367 };
285368
286369 pub const GetOrPutResult = struct {
287370 entry: *Entry,
288371 found_existing: bool,
289372 };
290373
291 pub const Managed = HashMap(K, V, hash, eql, store_hash);
292
293 const Self = @This();
294
295 const linear_scan_max = 8;
374 pub const Managed = HashMap(K, V, hashFn, eqlFn, MaxLoadPercentage);
296375
297376 pub fn promote(self: Self, allocator: *Allocator) Managed {
298377 return .{
......@@ -301,167 +380,156 @@ pub fn HashMapUnmanaged(
301380 };
302381 }
303382
383 fn isUnderMaxLoadPercentage(size: Size, cap: Size) bool {
384 return size * 100 < MaxLoadPercentage * cap;
385 }
386
387 pub fn init(allocator: *Allocator) Self {
388 return .{};
389 }
390
304391 pub fn deinit(self: *Self, allocator: *Allocator) void {
305 self.entries.deinit(allocator);
306 if (self.index_header) |header| {
307 header.free(allocator);
308 }
392 self.deallocate(allocator);
309393 self.* = undefined;
310394 }
311395
312 pub fn clearRetainingCapacity(self: *Self) void {
313 self.entries.items.len = 0;
314 if (self.index_header) |header| {
315 header.max_distance_from_start_index = 0;
316 switch (header.capacityIndexType()) {
317 .u8 => mem.set(Index(u8), header.indexes(u8), Index(u8).empty),
318 .u16 => mem.set(Index(u16), header.indexes(u16), Index(u16).empty),
319 .u32 => mem.set(Index(u32), header.indexes(u32), Index(u32).empty),
320 .usize => mem.set(Index(usize), header.indexes(usize), Index(usize).empty),
321 }
322 }
323 }
396 fn deallocate(self: *Self, allocator: *Allocator) void {
397 if (self.metadata == null) return;
324398
325 pub fn clearAndFree(self: *Self, allocator: *Allocator) void {
326 self.entries.shrink(allocator, 0);
327 if (self.index_header) |header| {
328 header.free(allocator);
329 self.index_header = null;
330 }
399 const cap = self.capacity();
400 const meta_size = @sizeOf(Header) + cap * @sizeOf(Metadata);
401
402 const alignment = @alignOf(Entry) - 1;
403 const entries_size = @as(usize, cap) * @sizeOf(Entry) + alignment;
404
405 const total_size = meta_size + entries_size;
406
407 var slice: []u8 = undefined;
408 slice.ptr = @intToPtr([*]u8, @ptrToInt(self.header()));
409 slice.len = total_size;
410 allocator.free(slice);
411
412 self.metadata = null;
413 self.available = 0;
331414 }
332415
333 /// If key exists this function cannot fail.
334 /// If there is an existing item with `key`, then the result
335 /// `Entry` pointer points to it, and found_existing is true.
336 /// Otherwise, puts a new item with undefined value, and
337 /// the `Entry` pointer points to it. Caller should then initialize
338 /// the value (but not the key).
339 pub fn getOrPut(self: *Self, allocator: *Allocator, key: K) !GetOrPutResult {
340 self.ensureCapacity(allocator, self.entries.items.len + 1) catch |err| {
341 // "If key exists this function cannot fail."
342 return GetOrPutResult{
343 .entry = self.getEntry(key) orelse return err,
344 .found_existing = true,
345 };
346 };
347 return self.getOrPutAssumeCapacity(key);
416 fn capacityForSize(size: Size) Size {
417 var new_cap = @truncate(u32, (@as(u64, size) * 100) / MaxLoadPercentage + 1);
418 new_cap = math.ceilPowerOfTwo(u32, new_cap) catch unreachable;
419 return new_cap;
348420 }
349421
350 /// If there is an existing item with `key`, then the result
351 /// `Entry` pointer points to it, and found_existing is true.
352 /// Otherwise, puts a new item with undefined value, and
353 /// the `Entry` pointer points to it. Caller should then initialize
354 /// the value (but not the key).
355 /// If a new entry needs to be stored, this function asserts there
356 /// is enough capacity to store it.
357 pub fn getOrPutAssumeCapacity(self: *Self, key: K) GetOrPutResult {
358 const header = self.index_header orelse {
359 // Linear scan.
360 const h = if (store_hash) hash(key) else {};
361 for (self.entries.items) |*item| {
362 if (item.hash == h and eql(key, item.key)) {
363 return GetOrPutResult{
364 .entry = item,
365 .found_existing = true,
366 };
367 }
368 }
369 const new_entry = self.entries.addOneAssumeCapacity();
370 new_entry.* = .{
371 .hash = if (store_hash) h else {},
372 .key = key,
373 .value = undefined,
374 };
375 return GetOrPutResult{
376 .entry = new_entry,
377 .found_existing = false,
378 };
379 };
422 pub fn ensureCapacity(self: *Self, allocator: *Allocator, new_size: Size) !void {
423 if (new_size > self.size)
424 try self.growIfNeeded(allocator, new_size - self.size);
425 }
380426
381 switch (header.capacityIndexType()) {
382 .u8 => return self.getOrPutInternal(key, header, u8),
383 .u16 => return self.getOrPutInternal(key, header, u16),
384 .u32 => return self.getOrPutInternal(key, header, u32),
385 .usize => return self.getOrPutInternal(key, header, usize),
427 pub fn clearRetainingCapacity(self: *Self) void {
428 if (self.metadata) |_| {
429 self.initMetadatas();
430 self.size = 0;
431 self.available = 0;
386432 }
387433 }
388434
389 pub fn getOrPutValue(self: *Self, allocator: *Allocator, key: K, value: V) !*Entry {
390 const res = try self.getOrPut(allocator, key);
391 if (!res.found_existing)
392 res.entry.value = value;
435 pub fn clearAndFree(self: *Self, allocator: *Allocator) void {
436 self.deallocate(allocator);
437 self.size = 0;
438 self.available = 0;
439 }
393440
394 return res.entry;
441 pub fn count(self: *const Self) Size {
442 return self.size;
395443 }
396444
397 /// Increases capacity, guaranteeing that insertions up until the
398 /// `expected_count` will not cause an allocation, and therefore cannot fail.
399 pub fn ensureCapacity(self: *Self, allocator: *Allocator, new_capacity: usize) !void {
400 try self.entries.ensureCapacity(allocator, new_capacity);
401 if (new_capacity <= linear_scan_max) return;
402
403 // Ensure that the indexes will be at most 60% full if
404 // `new_capacity` items are put into it.
405 const needed_len = new_capacity * 5 / 3;
406 if (self.index_header) |header| {
407 if (needed_len > header.indexes_len) {
408 // An overflow here would mean the amount of memory required would not
409 // be representable in the address space.
410 const new_indexes_len = math.ceilPowerOfTwo(usize, needed_len) catch unreachable;
411 const new_header = try IndexHeader.alloc(allocator, new_indexes_len);
412 self.insertAllEntriesIntoNewHeader(new_header);
413 header.free(allocator);
414 self.index_header = new_header;
415 }
416 } else {
417 // An overflow here would mean the amount of memory required would not
418 // be representable in the address space.
419 const new_indexes_len = math.ceilPowerOfTwo(usize, needed_len) catch unreachable;
420 const header = try IndexHeader.alloc(allocator, new_indexes_len);
421 self.insertAllEntriesIntoNewHeader(header);
422 self.index_header = header;
423 }
445 fn header(self: *const Self) *Header {
446 return @ptrCast(*Header, @ptrCast([*]Header, self.metadata.?) - 1);
424447 }
425448
426 /// Returns the number of total elements which may be present before it is
427 /// no longer guaranteed that no allocations will be performed.
428 pub fn capacity(self: Self) usize {
429 const entry_cap = self.entries.capacity;
430 const header = self.index_header orelse return math.min(linear_scan_max, entry_cap);
431 const indexes_cap = (header.indexes_len + 1) * 3 / 4;
432 return math.min(entry_cap, indexes_cap);
449 fn entries(self: *const Self) [*]Entry {
450 return self.header().entries;
433451 }
434452
435 /// Clobbers any existing data. To detect if a put would clobber
436 /// existing data, see `getOrPut`.
437 pub fn put(self: *Self, allocator: *Allocator, key: K, value: V) !void {
438 const result = try self.getOrPut(allocator, key);
439 result.entry.value = value;
453 pub fn capacity(self: *const Self) Size {
454 if (self.metadata == null) return 0;
455
456 return self.header().capacity;
440457 }
441458
442 /// Inserts a key-value pair into the hash map, asserting that no previous
443 /// entry with the same key is already present
459 pub fn iterator(self: *const Self) Iterator {
460 return .{ .hm = self };
461 }
462
463 /// Insert an entry in the map. Assumes it is not already present.
444464 pub fn putNoClobber(self: *Self, allocator: *Allocator, key: K, value: V) !void {
445 const result = try self.getOrPut(allocator, key);
446 assert(!result.found_existing);
447 result.entry.value = value;
465 assert(!self.contains(key));
466 try self.growIfNeeded(allocator, 1);
467
468 self.putAssumeCapacityNoClobber(key, value);
448469 }
449470
450 /// Asserts there is enough capacity to store the new key-value pair.
451 /// Clobbers any existing data. To detect if a put would clobber
452 /// existing data, see `getOrPutAssumeCapacity`.
453471 pub fn putAssumeCapacity(self: *Self, key: K, value: V) void {
454 const result = self.getOrPutAssumeCapacity(key);
455 result.entry.value = value;
472 const hash = hashFn(key);
473 const mask = self.capacity() - 1;
474 const fingerprint = Metadata.takeFingerprint(hash);
475 var idx = @truncate(usize, hash & mask);
476
477 var first_tombstone_idx: usize = self.capacity(); // invalid index
478 var metadata = self.metadata.? + idx;
479 while (metadata[0].isUsed() or metadata[0].isTombstone()) {
480 if (metadata[0].isUsed() and metadata[0].fingerprint == fingerprint) {
481 const entry = &self.entries()[idx];
482 if (eqlFn(entry.key, key)) {
483 return;
484 }
485 } else if (first_tombstone_idx == self.capacity() and metadata[0].isTombstone()) {
486 first_tombstone_idx = idx;
487 }
488
489 idx = (idx + 1) & mask;
490 metadata = self.metadata.? + idx;
491 }
492
493 if (first_tombstone_idx < self.capacity()) {
494 // Cheap try to lower probing lengths after deletions. Recycle a tombstone.
495 idx = first_tombstone_idx;
496 metadata = self.metadata.? + idx;
497 } else {
498 // We're using a slot previously free.
499 self.available -= 1;
500 }
501
502 metadata[0].fill(fingerprint);
503 const entry = &self.entries()[idx];
504 entry.* = .{ .key = key, .value = undefined };
505 self.size += 1;
456506 }
457507
458 /// Asserts there is enough capacity to store the new key-value pair.
459 /// Asserts that it does not clobber any existing data.
460 /// To detect if a put would clobber existing data, see `getOrPutAssumeCapacity`.
508 /// Insert an entry in the map. Assumes it is not already present,
509 /// and that no allocation is needed.
461510 pub fn putAssumeCapacityNoClobber(self: *Self, key: K, value: V) void {
462 const result = self.getOrPutAssumeCapacity(key);
463 assert(!result.found_existing);
464 result.entry.value = value;
511 assert(!self.contains(key));
512
513 const hash = hashFn(key);
514 const mask = self.capacity() - 1;
515 var idx = @truncate(usize, hash & mask);
516
517 var metadata = self.metadata.? + idx;
518 while (metadata[0].isUsed()) {
519 idx = (idx + 1) & mask;
520 metadata = self.metadata.? + idx;
521 }
522
523 if (!metadata[0].isTombstone()) {
524 assert(self.available > 0);
525 self.available -= 1;
526 }
527
528 const fingerprint = Metadata.takeFingerprint(hash);
529 metadata[0].fill(fingerprint);
530 self.entries()[idx] = Entry{ .key = key, .value = value };
531
532 self.size += 1;
465533 }
466534
467535 /// Inserts a new `Entry` into the hash map, returning the previous one, if any.
......@@ -488,400 +556,622 @@ pub fn HashMapUnmanaged(
488556 }
489557
490558 pub fn getEntry(self: Self, key: K) ?*Entry {
491 const index = self.getIndex(key) orelse return null;
492 return &self.entries.items[index];
493 }
559 if (self.size == 0) {
560 return null;
561 }
494562
495 pub fn getIndex(self: Self, key: K) ?usize {
496 const header = self.index_header orelse {
497 // Linear scan.
498 const h = if (store_hash) hash(key) else {};
499 for (self.entries.items) |*item, i| {
500 if (item.hash == h and eql(key, item.key)) {
501 return i;
563 const hash = hashFn(key);
564 const mask = self.capacity() - 1;
565 const fingerprint = Metadata.takeFingerprint(hash);
566 var idx = @truncate(usize, hash & mask);
567
568 var metadata = self.metadata.? + idx;
569 while (metadata[0].isUsed() or metadata[0].isTombstone()) {
570 if (metadata[0].isUsed() and metadata[0].fingerprint == fingerprint) {
571 const entry = &self.entries()[idx];
572 if (eqlFn(entry.key, key)) {
573 return entry;
502574 }
503575 }
504 return null;
505 };
506 switch (header.capacityIndexType()) {
507 .u8 => return self.getInternal(key, header, u8),
508 .u16 => return self.getInternal(key, header, u16),
509 .u32 => return self.getInternal(key, header, u32),
510 .usize => return self.getInternal(key, header, usize),
576 idx = (idx + 1) & mask;
577 metadata = self.metadata.? + idx;
511578 }
512 }
513579
514 pub fn get(self: Self, key: K) ?V {
515 return if (self.getEntry(key)) |entry| entry.value else null;
580 return null;
516581 }
517582
518 pub fn contains(self: Self, key: K) bool {
519 return self.getEntry(key) != null;
583 /// Insert an entry if the associated key is not already present, otherwise update preexisting value.
584 /// Returns true if the key was already present.
585 pub fn put(self: *Self, allocator: *Allocator, key: K, value: V) !void {
586 const result = try self.getOrPut(allocator, key);
587 result.entry.value = value;
520588 }
521589
522 /// If there is an `Entry` with a matching key, it is deleted from
523 /// the hash map, and then returned from this function.
524 pub fn remove(self: *Self, key: K) ?Entry {
525 const header = self.index_header orelse {
526 // Linear scan.
527 const h = if (store_hash) hash(key) else {};
528 for (self.entries.items) |item, i| {
529 if (item.hash == h and eql(key, item.key)) {
530 return self.entries.swapRemove(i);
590 /// Get an optional pointer to the value associated with key, if present.
591 pub fn get(self: Self, key: K) ?V {
592 if (self.size == 0) {
593 return null;
594 }
595
596 const hash = hashFn(key);
597 const mask = self.capacity() - 1;
598 const fingerprint = Metadata.takeFingerprint(hash);
599 var idx = @truncate(usize, hash & mask);
600
601 var metadata = self.metadata.? + idx;
602 while (metadata[0].isUsed() or metadata[0].isTombstone()) {
603 if (metadata[0].isUsed() and metadata[0].fingerprint == fingerprint) {
604 const entry = &self.entries()[idx];
605 if (eqlFn(entry.key, key)) {
606 return entry.value;
531607 }
532608 }
533 return null;
534 };
535 switch (header.capacityIndexType()) {
536 .u8 => return self.removeInternal(key, header, u8),
537 .u16 => return self.removeInternal(key, header, u16),
538 .u32 => return self.removeInternal(key, header, u32),
539 .usize => return self.removeInternal(key, header, usize),
609 idx = (idx + 1) & mask;
610 metadata = self.metadata.? + idx;
540611 }
541 }
542612
543 /// Asserts there is an `Entry` with matching key, deletes it from the hash map,
544 /// and discards it.
545 pub fn removeAssertDiscard(self: *Self, key: K) void {
546 assert(self.remove(key) != null);
613 return null;
547614 }
548615
549 pub fn items(self: Self) []Entry {
550 return self.entries.items;
616 pub fn getOrPut(self: *Self, allocator: *Allocator, key: K) !GetOrPutResult {
617 try self.growIfNeeded(allocator, 1);
618
619 return self.getOrPutAssumeCapacity(key);
551620 }
552621
553 pub fn clone(self: Self, allocator: *Allocator) !Self {
554 var other: Self = .{};
555 try other.entries.appendSlice(allocator, self.entries.items);
622 pub fn getOrPutAssumeCapacity(self: *Self, key: K) GetOrPutResult {
623 const hash = hashFn(key);
624 const mask = self.capacity() - 1;
625 const fingerprint = Metadata.takeFingerprint(hash);
626 var idx = @truncate(usize, hash & mask);
627
628 var first_tombstone_idx: usize = self.capacity(); // invalid index
629 var metadata = self.metadata.? + idx;
630 while (metadata[0].isUsed() or metadata[0].isTombstone()) {
631 if (metadata[0].isUsed() and metadata[0].fingerprint == fingerprint) {
632 const entry = &self.entries()[idx];
633 if (eqlFn(entry.key, key)) {
634 return GetOrPutResult{ .entry = entry, .found_existing = true };
635 }
636 } else if (first_tombstone_idx == self.capacity() and metadata[0].isTombstone()) {
637 first_tombstone_idx = idx;
638 }
556639
557 if (self.index_header) |header| {
558 const new_header = try IndexHeader.alloc(allocator, header.indexes_len);
559 other.insertAllEntriesIntoNewHeader(new_header);
560 other.index_header = new_header;
640 idx = (idx + 1) & mask;
641 metadata = self.metadata.? + idx;
561642 }
562 return other;
643
644 if (first_tombstone_idx < self.capacity()) {
645 // Cheap try to lower probing lengths after deletions. Recycle a tombstone.
646 idx = first_tombstone_idx;
647 metadata = self.metadata.? + idx;
648 } else {
649 // We're using a slot previously free.
650 self.available -= 1;
651 }
652
653 metadata[0].fill(fingerprint);
654 const entry = &self.entries()[idx];
655 entry.* = .{ .key = key, .value = undefined };
656 self.size += 1;
657
658 return GetOrPutResult{ .entry = entry, .found_existing = false };
563659 }
564660
565 fn removeInternal(self: *Self, key: K, header: *IndexHeader, comptime I: type) ?Entry {
566 const indexes = header.indexes(I);
567 const h = hash(key);
568 const start_index = header.constrainIndex(h);
569 var roll_over: usize = 0;
570 while (roll_over <= header.max_distance_from_start_index) : (roll_over += 1) {
571 const index_index = header.constrainIndex(start_index + roll_over);
572 var index = &indexes[index_index];
573 if (index.isEmpty())
574 return null;
575
576 const entry = &self.entries.items[index.entry_index];
577
578 const hash_match = if (store_hash) h == entry.hash else true;
579 if (!hash_match or !eql(key, entry.key))
580 continue;
581
582 const removed_entry = self.entries.swapRemove(index.entry_index);
583 if (self.entries.items.len > 0 and self.entries.items.len != index.entry_index) {
584 // Because of the swap remove, now we need to update the index that was
585 // pointing to the last entry and is now pointing to this removed item slot.
586 self.updateEntryIndex(header, self.entries.items.len, index.entry_index, I, indexes);
587 }
661 pub fn getOrPutValue(self: *Self, allocator: *Allocator, key: K, value: V) !*Entry {
662 const res = try self.getOrPut(allocator, key);
663 if (!res.found_existing) res.entry.value = value;
664 return res.entry;
665 }
588666
589 // Now we have to shift over the following indexes.
590 roll_over += 1;
591 while (roll_over < header.indexes_len) : (roll_over += 1) {
592 const next_index_index = header.constrainIndex(start_index + roll_over);
593 const next_index = &indexes[next_index_index];
594 if (next_index.isEmpty() or next_index.distance_from_start_index == 0) {
595 index.setEmpty();
667 /// Return true if there is a value associated with key in the map.
668 pub fn contains(self: *const Self, key: K) bool {
669 return self.get(key) != null;
670 }
671
672 /// If there is an `Entry` with a matching key, it is deleted from
673 /// the hash map, and then returned from this function.
674 pub fn remove(self: *Self, key: K) ?Entry {
675 if (self.size == 0) return null;
676
677 const hash = hashFn(key);
678 const mask = self.capacity() - 1;
679 const fingerprint = Metadata.takeFingerprint(hash);
680 var idx = @truncate(usize, hash & mask);
681
682 var metadata = self.metadata.? + idx;
683 while (metadata[0].isUsed() or metadata[0].isTombstone()) {
684 if (metadata[0].isUsed() and metadata[0].fingerprint == fingerprint) {
685 const entry = &self.entries()[idx];
686 if (eqlFn(entry.key, key)) {
687 const removed_entry = entry.*;
688 metadata[0].remove();
689 entry.* = undefined;
690 self.size -= 1;
596691 return removed_entry;
597692 }
598 index.* = next_index.*;
599 index.distance_from_start_index -= 1;
600 index = next_index;
601693 }
602 unreachable;
694 idx = (idx + 1) & mask;
695 metadata = self.metadata.? + idx;
603696 }
697
604698 return null;
605699 }
606700
607 fn updateEntryIndex(
608 self: *Self,
609 header: *IndexHeader,
610 old_entry_index: usize,
611 new_entry_index: usize,
612 comptime I: type,
613 indexes: []Index(I),
614 ) void {
615 const h = if (store_hash) self.entries.items[new_entry_index].hash else hash(self.entries.items[new_entry_index].key);
616 const start_index = header.constrainIndex(h);
617 var roll_over: usize = 0;
618 while (roll_over <= header.max_distance_from_start_index) : (roll_over += 1) {
619 const index_index = header.constrainIndex(start_index + roll_over);
620 const index = &indexes[index_index];
621 if (index.entry_index == old_entry_index) {
622 index.entry_index = @intCast(I, new_entry_index);
623 return;
701 /// Asserts there is an `Entry` with matching key, deletes it from the hash map,
702 /// and discards it.
703 pub fn removeAssertDiscard(self: *Self, key: K) void {
704 assert(self.contains(key));
705
706 const hash = hashFn(key);
707 const mask = self.capacity() - 1;
708 const fingerprint = Metadata.takeFingerprint(hash);
709 var idx = @truncate(usize, hash & mask);
710
711 var metadata = self.metadata.? + idx;
712 while (metadata[0].isUsed() or metadata[0].isTombstone()) {
713 if (metadata[0].isUsed() and metadata[0].fingerprint == fingerprint) {
714 const entry = &self.entries()[idx];
715 if (eqlFn(entry.key, key)) {
716 metadata[0].remove();
717 entry.* = undefined;
718 self.size -= 1;
719 return;
720 }
624721 }
722 idx = (idx + 1) & mask;
723 metadata = self.metadata.? + idx;
625724 }
725
626726 unreachable;
627727 }
628728
629 /// Must ensureCapacity before calling this.
630 fn getOrPutInternal(self: *Self, key: K, header: *IndexHeader, comptime I: type) GetOrPutResult {
631 const indexes = header.indexes(I);
632 const h = hash(key);
633 const start_index = header.constrainIndex(h);
634 var roll_over: usize = 0;
635 var distance_from_start_index: usize = 0;
636 while (roll_over <= header.indexes_len) : ({
637 roll_over += 1;
638 distance_from_start_index += 1;
639 }) {
640 const index_index = header.constrainIndex(start_index + roll_over);
641 const index = indexes[index_index];
642 if (index.isEmpty()) {
643 indexes[index_index] = .{
644 .distance_from_start_index = @intCast(I, distance_from_start_index),
645 .entry_index = @intCast(I, self.entries.items.len),
646 };
647 header.maybeBumpMax(distance_from_start_index);
648 const new_entry = self.entries.addOneAssumeCapacity();
649 new_entry.* = .{
650 .hash = if (store_hash) h else {},
651 .key = key,
652 .value = undefined,
653 };
654 return .{
655 .found_existing = false,
656 .entry = new_entry,
657 };
658 }
729 fn initMetadatas(self: *Self) void {
730 @memset(@ptrCast([*]u8, self.metadata.?), 0, @sizeOf(Metadata) * self.capacity());
731 }
659732
660 // This pointer survives the following append because we call
661 // entries.ensureCapacity before getOrPutInternal.
662 const entry = &self.entries.items[index.entry_index];
663 const hash_match = if (store_hash) h == entry.hash else true;
664 if (hash_match and eql(key, entry.key)) {
665 return .{
666 .found_existing = true,
667 .entry = entry,
668 };
669 }
670 if (index.distance_from_start_index < distance_from_start_index) {
671 // In this case, we did not find the item. We will put a new entry.
672 // However, we will use this index for the new entry, and move
673 // the previous index down the line, to keep the max_distance_from_start_index
674 // as small as possible.
675 indexes[index_index] = .{
676 .distance_from_start_index = @intCast(I, distance_from_start_index),
677 .entry_index = @intCast(I, self.entries.items.len),
678 };
679 header.maybeBumpMax(distance_from_start_index);
680 const new_entry = self.entries.addOneAssumeCapacity();
681 new_entry.* = .{
682 .hash = if (store_hash) h else {},
683 .key = key,
684 .value = undefined,
685 };
686
687 distance_from_start_index = index.distance_from_start_index;
688 var prev_entry_index = index.entry_index;
689
690 // Find somewhere to put the index we replaced by shifting
691 // following indexes backwards.
692 roll_over += 1;
693 distance_from_start_index += 1;
694 while (roll_over < header.indexes_len) : ({
695 roll_over += 1;
696 distance_from_start_index += 1;
697 }) {
698 const next_index_index = header.constrainIndex(start_index + roll_over);
699 const next_index = indexes[next_index_index];
700 if (next_index.isEmpty()) {
701 header.maybeBumpMax(distance_from_start_index);
702 indexes[next_index_index] = .{
703 .entry_index = prev_entry_index,
704 .distance_from_start_index = @intCast(I, distance_from_start_index),
705 };
706 return .{
707 .found_existing = false,
708 .entry = new_entry,
709 };
710 }
711 if (next_index.distance_from_start_index < distance_from_start_index) {
712 header.maybeBumpMax(distance_from_start_index);
713 indexes[next_index_index] = .{
714 .entry_index = prev_entry_index,
715 .distance_from_start_index = @intCast(I, distance_from_start_index),
716 };
717 distance_from_start_index = next_index.distance_from_start_index;
718 prev_entry_index = next_index.entry_index;
719 }
720 }
721 unreachable;
722 }
723 }
724 unreachable;
733 // This counts the number of occupied slots, used + tombstones, which is
734 // what has to stay under the MaxLoadPercentage of capacity.
735 fn load(self: *const Self) Size {
736 const max_load = (self.capacity() * MaxLoadPercentage) / 100;
737 assert(max_load >= self.available);
738 return @truncate(Size, max_load - self.available);
725739 }
726740
727 fn getInternal(self: Self, key: K, header: *IndexHeader, comptime I: type) ?usize {
728 const indexes = header.indexes(I);
729 const h = hash(key);
730 const start_index = header.constrainIndex(h);
731 var roll_over: usize = 0;
732 while (roll_over <= header.max_distance_from_start_index) : (roll_over += 1) {
733 const index_index = header.constrainIndex(start_index + roll_over);
734 const index = indexes[index_index];
735 if (index.isEmpty())
736 return null;
737
738 const entry = &self.entries.items[index.entry_index];
739 const hash_match = if (store_hash) h == entry.hash else true;
740 if (hash_match and eql(key, entry.key))
741 return index.entry_index;
741 fn growIfNeeded(self: *Self, allocator: *Allocator, new_count: Size) !void {
742 if (new_count > self.available) {
743 try self.grow(allocator, capacityForSize(self.load() + new_count));
742744 }
743 return null;
744745 }
745746
746 fn insertAllEntriesIntoNewHeader(self: *Self, header: *IndexHeader) void {
747 switch (header.capacityIndexType()) {
748 .u8 => return self.insertAllEntriesIntoNewHeaderGeneric(header, u8),
749 .u16 => return self.insertAllEntriesIntoNewHeaderGeneric(header, u16),
750 .u32 => return self.insertAllEntriesIntoNewHeaderGeneric(header, u32),
751 .usize => return self.insertAllEntriesIntoNewHeaderGeneric(header, usize),
747 pub fn clone(self: Self, allocator: *Allocator) !Self {
748 var other = Self{};
749 if (self.size == 0)
750 return other;
751
752 const new_cap = capacityForSize(self.size);
753 try other.allocate(allocator, new_cap);
754 other.initMetadatas();
755 other.available = @truncate(u32, (new_cap * MaxLoadPercentage) / 100);
756
757 var i: Size = 0;
758 var metadata = self.metadata.?;
759 var entr = self.entries();
760 while (i < self.capacity()) : (i += 1) {
761 if (metadata[i].isUsed()) {
762 const entry = &entr[i];
763 other.putAssumeCapacityNoClobber(entry.key, entry.value);
764 if (other.size == self.size)
765 break;
766 }
752767 }
768
769 return other;
753770 }
754771
755 fn insertAllEntriesIntoNewHeaderGeneric(self: *Self, header: *IndexHeader, comptime I: type) void {
756 const indexes = header.indexes(I);
757 entry_loop: for (self.entries.items) |entry, i| {
758 const h = if (store_hash) entry.hash else hash(entry.key);
759 const start_index = header.constrainIndex(h);
760 var entry_index = i;
761 var roll_over: usize = 0;
762 var distance_from_start_index: usize = 0;
763 while (roll_over < header.indexes_len) : ({
764 roll_over += 1;
765 distance_from_start_index += 1;
766 }) {
767 const index_index = header.constrainIndex(start_index + roll_over);
768 const next_index = indexes[index_index];
769 if (next_index.isEmpty()) {
770 header.maybeBumpMax(distance_from_start_index);
771 indexes[index_index] = .{
772 .distance_from_start_index = @intCast(I, distance_from_start_index),
773 .entry_index = @intCast(I, entry_index),
774 };
775 continue :entry_loop;
776 }
777 if (next_index.distance_from_start_index < distance_from_start_index) {
778 header.maybeBumpMax(distance_from_start_index);
779 indexes[index_index] = .{
780 .distance_from_start_index = @intCast(I, distance_from_start_index),
781 .entry_index = @intCast(I, entry_index),
782 };
783 distance_from_start_index = next_index.distance_from_start_index;
784 entry_index = next_index.entry_index;
772 fn grow(self: *Self, allocator: *Allocator, new_capacity: Size) !void {
773 const new_cap = std.math.max(new_capacity, MinimalCapacity);
774 assert(new_cap > self.capacity());
775 assert(std.math.isPowerOfTwo(new_cap));
776
777 var map = Self{};
778 defer map.deinit(allocator);
779 try map.allocate(allocator, new_cap);
780 map.initMetadatas();
781 map.available = @truncate(u32, (new_cap * MaxLoadPercentage) / 100);
782
783 if (self.size != 0) {
784 const old_capacity = self.capacity();
785 var i: Size = 0;
786 var metadata = self.metadata.?;
787 var entr = self.entries();
788 while (i < old_capacity) : (i += 1) {
789 if (metadata[i].isUsed()) {
790 const entry = &entr[i];
791 map.putAssumeCapacityNoClobber(entry.key, entry.value);
792 if (map.size == self.size)
793 break;
785794 }
786795 }
787 unreachable;
788796 }
797
798 self.size = 0;
799 std.mem.swap(Self, self, &map);
800 }
801
802 fn allocate(self: *Self, allocator: *Allocator, new_capacity: Size) !void {
803 const meta_size = @sizeOf(Header) + new_capacity * @sizeOf(Metadata);
804
805 const alignment = @alignOf(Entry) - 1;
806 const entries_size = @as(usize, new_capacity) * @sizeOf(Entry) + alignment;
807
808 const total_size = meta_size + entries_size;
809
810 const slice = try allocator.alignedAlloc(u8, @alignOf(Header), total_size);
811 const ptr = @ptrToInt(slice.ptr);
812
813 const metadata = ptr + @sizeOf(Header);
814 var entry_ptr = ptr + meta_size;
815 entry_ptr = (entry_ptr + alignment) & ~@as(usize, alignment);
816 assert(entry_ptr + @as(usize, new_capacity) * @sizeOf(Entry) <= ptr + total_size);
817
818 const hdr = @intToPtr(*Header, ptr);
819 hdr.entries = @intToPtr([*]Entry, entry_ptr);
820 hdr.capacity = new_capacity;
821 self.metadata = @intToPtr([*]Metadata, metadata);
789822 }
790823 };
791824}
792825
793const CapacityIndexType = enum { u8, u16, u32, usize };
826const testing = std.testing;
827const expect = std.testing.expect;
828const expectEqual = std.testing.expectEqual;
829
830test "std.hash_map basic usage" {
831 var map = AutoHashMap(u32, u32).init(std.testing.allocator);
832 defer map.deinit();
833
834 const count = 5;
835 var i: u32 = 0;
836 var total: u32 = 0;
837 while (i < count) : (i += 1) {
838 try map.put(i, i);
839 total += i;
840 }
841
842 var sum: u32 = 0;
843 var it = map.iterator();
844 while (it.next()) |kv| {
845 sum += kv.key;
846 }
847 expect(sum == total);
848
849 i = 0;
850 sum = 0;
851 while (i < count) : (i += 1) {
852 expectEqual(map.get(i).?, i);
853 sum += map.get(i).?;
854 }
855 expectEqual(total, sum);
856}
857
858test "std.hash_map ensureCapacity" {
859 var map = AutoHashMap(i32, i32).init(std.testing.allocator);
860 defer map.deinit();
794861
795fn capacityIndexType(indexes_len: usize) CapacityIndexType {
796 if (indexes_len < math.maxInt(u8))
797 return .u8;
798 if (indexes_len < math.maxInt(u16))
799 return .u16;
800 if (indexes_len < math.maxInt(u32))
801 return .u32;
802 return .usize;
862 try map.ensureCapacity(20);
863 const initial_capacity = map.capacity();
864 testing.expect(initial_capacity >= 20);
865 var i: i32 = 0;
866 while (i < 20) : (i += 1) {
867 testing.expect(map.fetchPutAssumeCapacity(i, i + 10) == null);
868 }
869 // shouldn't resize from putAssumeCapacity
870 testing.expect(initial_capacity == map.capacity());
803871}
804872
805fn capacityIndexSize(indexes_len: usize) usize {
806 switch (capacityIndexType(indexes_len)) {
807 .u8 => return @sizeOf(Index(u8)),
808 .u16 => return @sizeOf(Index(u16)),
809 .u32 => return @sizeOf(Index(u32)),
810 .usize => return @sizeOf(Index(usize)),
873test "std.hash_map ensureCapacity with tombstones" {
874 var map = AutoHashMap(i32, i32).init(std.testing.allocator);
875 defer map.deinit();
876
877 var i: i32 = 0;
878 while (i < 100) : (i += 1) {
879 try map.ensureCapacity(@intCast(u32, map.count() + 1));
880 map.putAssumeCapacity(i, i);
881 // Remove to create tombstones that still count as load in the hashmap.
882 _ = map.remove(i);
811883 }
812884}
813885
814fn Index(comptime I: type) type {
815 return extern struct {
816 entry_index: I,
817 distance_from_start_index: I,
886test "std.hash_map clearRetainingCapacity" {
887 var map = AutoHashMap(u32, u32).init(std.testing.allocator);
888 defer map.deinit();
889
890 map.clearRetainingCapacity();
818891
819 const Self = @This();
892 try map.put(1, 1);
893 expectEqual(map.get(1).?, 1);
894 expectEqual(map.count(), 1);
820895
821 const empty = Self{
822 .entry_index = math.maxInt(I),
823 .distance_from_start_index = undefined,
824 };
896 const cap = map.capacity();
897 expect(cap > 0);
898
899 map.clearRetainingCapacity();
900 map.clearRetainingCapacity();
901 expectEqual(map.count(), 0);
902 expectEqual(map.capacity(), cap);
903 expect(!map.contains(1));
904}
905
906test "std.hash_map grow" {
907 var map = AutoHashMap(u32, u32).init(std.testing.allocator);
908 defer map.deinit();
825909
826 fn isEmpty(idx: Self) bool {
827 return idx.entry_index == math.maxInt(I);
910 const growTo = 12456;
911
912 var i: u32 = 0;
913 while (i < growTo) : (i += 1) {
914 try map.put(i, i);
915 }
916 expectEqual(map.count(), growTo);
917
918 i = 0;
919 var it = map.iterator();
920 while (it.next()) |kv| {
921 expectEqual(kv.key, kv.value);
922 i += 1;
923 }
924 expectEqual(i, growTo);
925
926 i = 0;
927 while (i < growTo) : (i += 1) {
928 expectEqual(map.get(i).?, i);
929 }
930}
931
932test "std.hash_map clone" {
933 var map = AutoHashMap(u32, u32).init(std.testing.allocator);
934 defer map.deinit();
935
936 var a = try map.clone();
937 defer a.deinit();
938
939 expectEqual(a.count(), 0);
940
941 try a.put(1, 1);
942 try a.put(2, 2);
943 try a.put(3, 3);
944
945 var b = try a.clone();
946 defer b.deinit();
947
948 expectEqual(b.count(), 3);
949 expectEqual(b.get(1), 1);
950 expectEqual(b.get(2), 2);
951 expectEqual(b.get(3), 3);
952}
953
954test "std.hash_map ensureCapacity with existing elements" {
955 var map = AutoHashMap(u32, u32).init(std.testing.allocator);
956 defer map.deinit();
957
958 try map.put(0, 0);
959 expectEqual(map.count(), 1);
960 expectEqual(map.capacity(), @TypeOf(map).Unmanaged.MinimalCapacity);
961
962 try map.ensureCapacity(65);
963 expectEqual(map.count(), 1);
964 expectEqual(map.capacity(), 128);
965}
966
967test "std.hash_map ensureCapacity satisfies max load factor" {
968 var map = AutoHashMap(u32, u32).init(std.testing.allocator);
969 defer map.deinit();
970
971 try map.ensureCapacity(127);
972 expectEqual(map.capacity(), 256);
973}
974
975test "std.hash_map remove" {
976 var map = AutoHashMap(u32, u32).init(std.testing.allocator);
977 defer map.deinit();
978
979 var i: u32 = 0;
980 while (i < 16) : (i += 1) {
981 try map.put(i, i);
982 }
983
984 i = 0;
985 while (i < 16) : (i += 1) {
986 if (i % 3 == 0) {
987 _ = map.remove(i);
828988 }
989 }
990 expectEqual(map.count(), 10);
991 var it = map.iterator();
992 while (it.next()) |kv| {
993 expectEqual(kv.key, kv.value);
994 expect(kv.key % 3 != 0);
995 }
829996
830 fn setEmpty(idx: *Self) void {
831 idx.entry_index = math.maxInt(I);
997 i = 0;
998 while (i < 16) : (i += 1) {
999 if (i % 3 == 0) {
1000 expect(!map.contains(i));
1001 } else {
1002 expectEqual(map.get(i).?, i);
8321003 }
833 };
1004 }
8341005}
8351006
836/// This struct is trailed by an array of `Index(I)`, where `I`
837/// and the array length are determined by `indexes_len`.
838const IndexHeader = struct {
839 max_distance_from_start_index: usize,
840 indexes_len: usize,
1007test "std.hash_map reverse removes" {
1008 var map = AutoHashMap(u32, u32).init(std.testing.allocator);
1009 defer map.deinit();
8411010
842 fn constrainIndex(header: IndexHeader, i: usize) usize {
843 // This is an optimization for modulo of power of two integers;
844 // it requires `indexes_len` to always be a power of two.
845 return i & (header.indexes_len - 1);
1011 var i: u32 = 0;
1012 while (i < 16) : (i += 1) {
1013 try map.putNoClobber(i, i);
8461014 }
8471015
848 fn indexes(header: *IndexHeader, comptime I: type) []Index(I) {
849 const start = @ptrCast([*]Index(I), @ptrCast([*]u8, header) + @sizeOf(IndexHeader));
850 return start[0..header.indexes_len];
1016 i = 16;
1017 while (i > 0) : (i -= 1) {
1018 _ = map.remove(i - 1);
1019 expect(!map.contains(i - 1));
1020 var j: u32 = 0;
1021 while (j < i - 1) : (j += 1) {
1022 expectEqual(map.get(j).?, j);
1023 }
8511024 }
8521025
853 fn capacityIndexType(header: IndexHeader) CapacityIndexType {
854 return hash_map.capacityIndexType(header.indexes_len);
1026 expectEqual(map.count(), 0);
1027}
1028
1029test "std.hash_map multiple removes on same metadata" {
1030 var map = AutoHashMap(u32, u32).init(std.testing.allocator);
1031 defer map.deinit();
1032
1033 var i: u32 = 0;
1034 while (i < 16) : (i += 1) {
1035 try map.put(i, i);
8551036 }
8561037
857 fn maybeBumpMax(header: *IndexHeader, distance_from_start_index: usize) void {
858 if (distance_from_start_index > header.max_distance_from_start_index) {
859 header.max_distance_from_start_index = distance_from_start_index;
1038 _ = map.remove(7);
1039 _ = map.remove(15);
1040 _ = map.remove(14);
1041 _ = map.remove(13);
1042 expect(!map.contains(7));
1043 expect(!map.contains(15));
1044 expect(!map.contains(14));
1045 expect(!map.contains(13));
1046
1047 i = 0;
1048 while (i < 13) : (i += 1) {
1049 if (i == 7) {
1050 expect(!map.contains(i));
1051 } else {
1052 expectEqual(map.get(i).?, i);
8601053 }
8611054 }
8621055
863 fn alloc(allocator: *Allocator, len: usize) !*IndexHeader {
864 const index_size = hash_map.capacityIndexSize(len);
865 const nbytes = @sizeOf(IndexHeader) + index_size * len;
866 const bytes = try allocator.allocAdvanced(u8, @alignOf(IndexHeader), nbytes, .exact);
867 @memset(bytes.ptr + @sizeOf(IndexHeader), 0xff, bytes.len - @sizeOf(IndexHeader));
868 const result = @ptrCast(*IndexHeader, bytes.ptr);
869 result.* = .{
870 .max_distance_from_start_index = 0,
871 .indexes_len = len,
872 };
873 return result;
1056 try map.put(15, 15);
1057 try map.put(13, 13);
1058 try map.put(14, 14);
1059 try map.put(7, 7);
1060 i = 0;
1061 while (i < 16) : (i += 1) {
1062 expectEqual(map.get(i).?, i);
1063 }
1064}
1065
1066test "std.hash_map put and remove loop in random order" {
1067 var map = AutoHashMap(u32, u32).init(std.testing.allocator);
1068 defer map.deinit();
1069
1070 var keys = std.ArrayList(u32).init(std.testing.allocator);
1071 defer keys.deinit();
1072
1073 const size = 32;
1074 const iterations = 100;
1075
1076 var i: u32 = 0;
1077 while (i < size) : (i += 1) {
1078 try keys.append(i);
1079 }
1080 var rng = std.rand.DefaultPrng.init(0);
1081
1082 while (i < iterations) : (i += 1) {
1083 std.rand.Random.shuffle(&rng.random, u32, keys.items);
1084
1085 for (keys.items) |key| {
1086 try map.put(key, key);
1087 }
1088 expectEqual(map.count(), size);
1089
1090 for (keys.items) |key| {
1091 _ = map.remove(key);
1092 }
1093 expectEqual(map.count(), 0);
1094 }
1095}
1096
1097test "std.hash_map remove one million elements in random order" {
1098 const Map = AutoHashMap(u32, u32);
1099 const n = 1000 * 1000;
1100 var map = Map.init(std.heap.page_allocator);
1101 defer map.deinit();
1102
1103 var keys = std.ArrayList(u32).init(std.heap.page_allocator);
1104 defer keys.deinit();
1105
1106 var i: u32 = 0;
1107 while (i < n) : (i += 1) {
1108 keys.append(i) catch unreachable;
1109 }
1110
1111 var rng = std.rand.DefaultPrng.init(0);
1112 std.rand.Random.shuffle(&rng.random, u32, keys.items);
1113
1114 for (keys.items) |key| {
1115 map.put(key, key) catch unreachable;
1116 }
1117
1118 std.rand.Random.shuffle(&rng.random, u32, keys.items);
1119 i = 0;
1120 while (i < n) : (i += 1) {
1121 const key = keys.items[i];
1122 _ = map.remove(key);
1123 }
1124}
1125
1126test "std.hash_map put" {
1127 var map = AutoHashMap(u32, u32).init(std.testing.allocator);
1128 defer map.deinit();
1129
1130 var i: u32 = 0;
1131 while (i < 16) : (i += 1) {
1132 _ = try map.put(i, i);
1133 }
1134
1135 i = 0;
1136 while (i < 16) : (i += 1) {
1137 expectEqual(map.get(i).?, i);
1138 }
1139
1140 i = 0;
1141 while (i < 16) : (i += 1) {
1142 try map.put(i, i * 16 + 1);
1143 }
1144
1145 i = 0;
1146 while (i < 16) : (i += 1) {
1147 expectEqual(map.get(i).?, i * 16 + 1);
1148 }
1149}
1150
1151test "std.hash_map getOrPut" {
1152 var map = AutoHashMap(u32, u32).init(std.testing.allocator);
1153 defer map.deinit();
1154
1155 var i: u32 = 0;
1156 while (i < 10) : (i += 1) {
1157 try map.put(i * 2, 2);
8741158 }
8751159
876 fn free(header: *IndexHeader, allocator: *Allocator) void {
877 const index_size = hash_map.capacityIndexSize(header.indexes_len);
878 const ptr = @ptrCast([*]u8, header);
879 const slice = ptr[0 .. @sizeOf(IndexHeader) + header.indexes_len * index_size];
880 allocator.free(slice);
1160 i = 0;
1161 while (i < 20) : (i += 1) {
1162 var n = try map.getOrPutValue(i, 1);
8811163 }
882};
8831164
884test "basic hash map usage" {
1165 i = 0;
1166 var sum = i;
1167 while (i < 20) : (i += 1) {
1168 sum += map.get(i).?;
1169 }
1170
1171 expectEqual(sum, 30);
1172}
1173
1174test "std.hash_map basic hash map usage" {
8851175 var map = AutoHashMap(i32, i32).init(std.testing.allocator);
8861176 defer map.deinit();
8871177
......@@ -925,85 +1215,10 @@ test "basic hash map usage" {
9251215 map.removeAssertDiscard(3);
9261216}
9271217
928test "iterator hash map" {
929 // https://github.com/ziglang/zig/issues/5127
930 if (std.Target.current.cpu.arch == .mips) return error.SkipZigTest;
931
932 var reset_map = AutoHashMap(i32, i32).init(std.testing.allocator);
933 defer reset_map.deinit();
934
935 // test ensureCapacity with a 0 parameter
936 try reset_map.ensureCapacity(0);
937
938 try reset_map.putNoClobber(0, 11);
939 try reset_map.putNoClobber(1, 22);
940 try reset_map.putNoClobber(2, 33);
941
942 var keys = [_]i32{
943 0, 2, 1,
944 };
945
946 var values = [_]i32{
947 11, 33, 22,
948 };
949
950 var buffer = [_]i32{
951 0, 0, 0,
952 };
953
954 var it = reset_map.iterator();
955 const first_entry = it.next().?;
956 it.reset();
957
958 var count: usize = 0;
959 while (it.next()) |entry| : (count += 1) {
960 buffer[@intCast(usize, entry.key)] = entry.value;
961 }
962 testing.expect(count == 3);
963 testing.expect(it.next() == null);
964
965 for (buffer) |v, i| {
966 testing.expect(buffer[@intCast(usize, keys[i])] == values[i]);
967 }
968
969 it.reset();
970 count = 0;
971 while (it.next()) |entry| {
972 buffer[@intCast(usize, entry.key)] = entry.value;
973 count += 1;
974 if (count >= 2) break;
975 }
976
977 for (buffer[0..2]) |v, i| {
978 testing.expect(buffer[@intCast(usize, keys[i])] == values[i]);
979 }
980
981 it.reset();
982 var entry = it.next().?;
983 testing.expect(entry.key == first_entry.key);
984 testing.expect(entry.value == first_entry.value);
985}
986
987test "ensure capacity" {
988 var map = AutoHashMap(i32, i32).init(std.testing.allocator);
989 defer map.deinit();
990
991 try map.ensureCapacity(20);
992 const initial_capacity = map.capacity();
993 testing.expect(initial_capacity >= 20);
994 var i: i32 = 0;
995 while (i < 20) : (i += 1) {
996 testing.expect(map.fetchPutAssumeCapacity(i, i + 10) == null);
997 }
998 // shouldn't resize from putAssumeCapacity
999 testing.expect(initial_capacity == map.capacity());
1000}
1001
1002test "clone" {
1218test "std.hash_map clone" {
10031219 var original = AutoHashMap(i32, i32).init(std.testing.allocator);
10041220 defer original.deinit();
10051221
1006 // put more than `linear_scan_max` so we can test that the index header is properly cloned
10071222 var i: u8 = 0;
10081223 while (i < 10) : (i += 1) {
10091224 try original.putNoClobber(i, i * 10);
......@@ -1017,69 +1232,3 @@ test "clone" {
10171232 testing.expect(copy.get(i).? == i * 10);
10181233 }
10191234}
1020
1021pub fn getHashPtrAddrFn(comptime K: type) (fn (K) u32) {
1022 return struct {
1023 fn hash(key: K) u32 {
1024 return getAutoHashFn(usize)(@ptrToInt(key));
1025 }
1026 }.hash;
1027}
1028
1029pub fn getTrivialEqlFn(comptime K: type) (fn (K, K) bool) {
1030 return struct {
1031 fn eql(a: K, b: K) bool {
1032 return a == b;
1033 }
1034 }.eql;
1035}
1036
1037pub fn getAutoHashFn(comptime K: type) (fn (K) u32) {
1038 return struct {
1039 fn hash(key: K) u32 {
1040 if (comptime trait.hasUniqueRepresentation(K)) {
1041 return @truncate(u32, Wyhash.hash(0, std.mem.asBytes(&key)));
1042 } else {
1043 var hasher = Wyhash.init(0);
1044 autoHash(&hasher, key);
1045 return @truncate(u32, hasher.final());
1046 }
1047 }
1048 }.hash;
1049}
1050
1051pub fn getAutoEqlFn(comptime K: type) (fn (K, K) bool) {
1052 return struct {
1053 fn eql(a: K, b: K) bool {
1054 return meta.eql(a, b);
1055 }
1056 }.eql;
1057}
1058
1059pub fn autoEqlIsCheap(comptime K: type) bool {
1060 return switch (@typeInfo(K)) {
1061 .Bool,
1062 .Int,
1063 .Float,
1064 .Pointer,
1065 .ComptimeFloat,
1066 .ComptimeInt,
1067 .Enum,
1068 .Fn,
1069 .ErrorSet,
1070 .AnyFrame,
1071 .EnumLiteral,
1072 => true,
1073 else => false,
1074 };
1075}
1076
1077pub fn getAutoHashStratFn(comptime K: type, comptime strategy: std.hash.Strategy) (fn (K) u32) {
1078 return struct {
1079 fn hash(key: K) u32 {
1080 var hasher = Wyhash.init(0);
1081 std.hash.autoHashStrat(&hasher, key, strategy);
1082 return @truncate(u32, hasher.final());
1083 }
1084 }.hash;
1085}
lib/std/heap/general_purpose_allocator.zig+3-2
......@@ -325,7 +325,8 @@ pub fn GeneralPurposeAllocator(comptime config: Config) type {
325325 break;
326326 }
327327 }
328 for (self.large_allocations.items()) |*large_alloc| {
328 var it = self.large_allocations.iterator();
329 while (it.next()) |large_alloc| {
329330 log.err("Memory leak detected: {}", .{large_alloc.value.getStackTrace()});
330331 leaks = true;
331332 }
......@@ -584,7 +585,7 @@ pub fn GeneralPurposeAllocator(comptime config: Config) type {
584585 if (new_aligned_size > largest_bucket_object_size) {
585586 try self.large_allocations.ensureCapacity(
586587 self.backing_allocator,
587 self.large_allocations.entries.items.len + 1,
588 self.large_allocations.count() + 1,
588589 );
589590
590591 const slice = try self.backing_allocator.allocFn(self.backing_allocator, len, ptr_align, len_align, ret_addr);
lib/std/http/headers.zig+5-4
......@@ -123,9 +123,9 @@ pub const Headers = struct {
123123
124124 pub fn deinit(self: *Self) void {
125125 {
126 for (self.index.items()) |*entry| {
127 const dex = &entry.value;
128 dex.deinit(self.allocator);
126 var it = self.index.iterator();
127 while (it.next()) |entry| {
128 entry.value.deinit(self.allocator);
129129 self.allocator.free(entry.key);
130130 }
131131 self.index.deinit(self.allocator);
......@@ -333,7 +333,8 @@ pub const Headers = struct {
333333
334334 fn rebuildIndex(self: *Self) void {
335335 // clear out the indexes
336 for (self.index.items()) |*entry| {
336 var it = self.index.iterator();
337 while (it.next()) |entry| {
337338 entry.value.shrinkRetainingCapacity(0);
338339 }
339340 // fill up indexes again; we know capacity is fine from before
lib/std/std.zig+7
......@@ -3,11 +3,15 @@
33// This file is part of [zig](https://ziglang.org/), which is MIT licensed.
44// The MIT license requires this copyright notice to be included in all copies
55// and substantial portions of the software.
6pub const ArrayHashMap = array_hash_map.ArrayHashMap;
7pub const ArrayHashMapUnmanaged = array_hash_map.ArrayHashMapUnmanaged;
68pub const ArrayList = @import("array_list.zig").ArrayList;
79pub const ArrayListAligned = @import("array_list.zig").ArrayListAligned;
810pub const ArrayListAlignedUnmanaged = @import("array_list.zig").ArrayListAlignedUnmanaged;
911pub const ArrayListSentineled = @import("array_list_sentineled.zig").ArrayListSentineled;
1012pub const ArrayListUnmanaged = @import("array_list.zig").ArrayListUnmanaged;
13pub const AutoArrayHashMap = array_hash_map.AutoArrayHashMap;
14pub const AutoArrayHashMapUnmanaged = array_hash_map.AutoArrayHashMapUnmanaged;
1115pub const AutoHashMap = hash_map.AutoHashMap;
1216pub const AutoHashMapUnmanaged = hash_map.AutoHashMapUnmanaged;
1317pub const BloomFilter = @import("bloom_filter.zig").BloomFilter;
......@@ -32,10 +36,13 @@ pub const SinglyLinkedList = @import("linked_list.zig").SinglyLinkedList;
3236pub const SpinLock = @import("spinlock.zig").SpinLock;
3337pub const StringHashMap = hash_map.StringHashMap;
3438pub const StringHashMapUnmanaged = hash_map.StringHashMapUnmanaged;
39pub const StringArrayHashMap = array_hash_map.StringArrayHashMap;
40pub const StringArrayHashMapUnmanaged = array_hash_map.StringArrayHashMapUnmanaged;
3541pub const TailQueue = @import("linked_list.zig").TailQueue;
3642pub const Target = @import("target.zig").Target;
3743pub const Thread = @import("thread.zig").Thread;
3844
45pub const array_hash_map = @import("array_hash_map.zig");
3946pub const atomic = @import("atomic.zig");
4047pub const base64 = @import("base64.zig");
4148pub const build = @import("build.zig");
src-self-hosted/Module.zig+14-13
......@@ -36,17 +36,17 @@ bin_file_path: []const u8,
3636/// It's rare for a decl to be exported, so we save memory by having a sparse map of
3737/// Decl pointers to details about them being exported.
3838/// The Export memory is owned by the `export_owners` table; the slice itself is owned by this table.
39decl_exports: std.AutoHashMapUnmanaged(*Decl, []*Export) = .{},
39decl_exports: std.AutoArrayHashMapUnmanaged(*Decl, []*Export) = .{},
4040/// We track which export is associated with the given symbol name for quick
4141/// detection of symbol collisions.
42symbol_exports: std.StringHashMapUnmanaged(*Export) = .{},
42symbol_exports: std.StringArrayHashMapUnmanaged(*Export) = .{},
4343/// This models the Decls that perform exports, so that `decl_exports` can be updated when a Decl
4444/// is modified. Note that the key of this table is not the Decl being exported, but the Decl that
4545/// is performing the export of another Decl.
4646/// This table owns the Export memory.
47export_owners: std.AutoHashMapUnmanaged(*Decl, []*Export) = .{},
47export_owners: std.AutoArrayHashMapUnmanaged(*Decl, []*Export) = .{},
4848/// Maps fully qualified namespaced names to the Decl struct for them.
49decl_table: std.HashMapUnmanaged(Scope.NameHash, *Decl, Scope.name_hash_hash, Scope.name_hash_eql, false) = .{},
49decl_table: std.ArrayHashMapUnmanaged(Scope.NameHash, *Decl, Scope.name_hash_hash, Scope.name_hash_eql, false) = .{},
5050
5151link_error_flags: link.File.ErrorFlags = .{},
5252
......@@ -57,13 +57,13 @@ work_queue: std.fifo.LinearFifo(WorkItem, .Dynamic),
5757/// The ErrorMsg memory is owned by the decl, using Module's allocator.
5858/// Note that a Decl can succeed but the Fn it represents can fail. In this case,
5959/// a Decl can have a failed_decls entry but have analysis status of success.
60failed_decls: std.AutoHashMapUnmanaged(*Decl, *ErrorMsg) = .{},
60failed_decls: std.AutoArrayHashMapUnmanaged(*Decl, *ErrorMsg) = .{},
6161/// Using a map here for consistency with the other fields here.
6262/// The ErrorMsg memory is owned by the `Scope`, using Module's allocator.
63failed_files: std.AutoHashMapUnmanaged(*Scope, *ErrorMsg) = .{},
63failed_files: std.AutoArrayHashMapUnmanaged(*Scope, *ErrorMsg) = .{},
6464/// Using a map here for consistency with the other fields here.
6565/// The ErrorMsg memory is owned by the `Export`, using Module's allocator.
66failed_exports: std.AutoHashMapUnmanaged(*Export, *ErrorMsg) = .{},
66failed_exports: std.AutoArrayHashMapUnmanaged(*Export, *ErrorMsg) = .{},
6767
6868/// Incrementing integer used to compare against the corresponding Decl
6969/// field to determine whether a Decl's status applies to an ongoing update, or a
......@@ -201,9 +201,9 @@ pub const Decl = struct {
201201 /// typed_value may need to be regenerated.
202202 dependencies: DepsTable = .{},
203203
204 /// The reason this is not `std.AutoHashMapUnmanaged` is a workaround for
204 /// The reason this is not `std.AutoArrayHashMapUnmanaged` is a workaround for
205205 /// stage1 compiler giving me: `error: struct 'Module.Decl' depends on itself`
206 pub const DepsTable = std.HashMapUnmanaged(*Decl, void, std.hash_map.getAutoHashFn(*Decl), std.hash_map.getAutoEqlFn(*Decl), false);
206 pub const DepsTable = std.ArrayHashMapUnmanaged(*Decl, void, std.array_hash_map.getAutoHashFn(*Decl), std.array_hash_map.getAutoEqlFn(*Decl), false);
207207
208208 pub fn destroy(self: *Decl, gpa: *Allocator) void {
209209 gpa.free(mem.spanZ(self.name));
......@@ -933,7 +933,8 @@ pub fn deinit(self: *Module) void {
933933 self.symbol_exports.deinit(gpa);
934934 self.root_scope.destroy(gpa);
935935
936 for (self.global_error_set.items()) |entry| {
936 var it = self.global_error_set.iterator();
937 while (it.next()) |entry| {
937938 gpa.free(entry.key);
938939 }
939940 self.global_error_set.deinit(gpa);
......@@ -1756,7 +1757,7 @@ fn analyzeRootSrcFile(self: *Module, root_scope: *Scope.File) !void {
17561757
17571758 // Keep track of the decls that we expect to see in this file so that
17581759 // we know which ones have been deleted.
1759 var deleted_decls = std.AutoHashMap(*Decl, void).init(self.gpa);
1760 var deleted_decls = std.AutoArrayHashMap(*Decl, void).init(self.gpa);
17601761 defer deleted_decls.deinit();
17611762 try deleted_decls.ensureCapacity(root_scope.decls.items.len);
17621763 for (root_scope.decls.items) |file_decl| {
......@@ -1877,7 +1878,7 @@ fn analyzeRootZIRModule(self: *Module, root_scope: *Scope.ZIRModule) !void {
18771878
18781879 // Keep track of the decls that we expect to see in this file so that
18791880 // we know which ones have been deleted.
1880 var deleted_decls = std.AutoHashMap(*Decl, void).init(self.gpa);
1881 var deleted_decls = std.AutoArrayHashMap(*Decl, void).init(self.gpa);
18811882 defer deleted_decls.deinit();
18821883 try deleted_decls.ensureCapacity(self.decl_table.items().len);
18831884 for (self.decl_table.items()) |entry| {
......@@ -2087,7 +2088,7 @@ pub fn getErrorValue(self: *Module, name: []const u8) !std.StringHashMapUnmanage
20872088 errdefer self.global_error_set.removeAssertDiscard(name);
20882089
20892090 gop.entry.key = try self.gpa.dupe(u8, name);
2090 gop.entry.value = @intCast(u16, self.global_error_set.items().len - 1);
2091 gop.entry.value = @intCast(u16, self.global_error_set.count() - 1);
20912092 return gop.entry.*;
20922093}
20932094
src-self-hosted/codegen.zig+4-4
......@@ -359,7 +359,7 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type {
359359 };
360360
361361 const Branch = struct {
362 inst_table: std.AutoHashMapUnmanaged(*ir.Inst, MCValue) = .{},
362 inst_table: std.AutoArrayHashMapUnmanaged(*ir.Inst, MCValue) = .{},
363363
364364 fn deinit(self: *Branch, gpa: *Allocator) void {
365365 self.inst_table.deinit(gpa);
......@@ -750,7 +750,7 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type {
750750 const ptr_bits = arch.ptrBitWidth();
751751 const ptr_bytes: u64 = @divExact(ptr_bits, 8);
752752 if (abi_size <= ptr_bytes) {
753 try self.registers.ensureCapacity(self.gpa, self.registers.items().len + 1);
753 try self.registers.ensureCapacity(self.gpa, self.registers.count() + 1);
754754 if (self.allocReg(inst)) |reg| {
755755 return MCValue{ .register = registerAlias(reg, abi_size) };
756756 }
......@@ -788,7 +788,7 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type {
788788 /// `reg_owner` is the instruction that gets associated with the register in the register table.
789789 /// This can have a side effect of spilling instructions to the stack to free up a register.
790790 fn copyToNewRegister(self: *Self, reg_owner: *ir.Inst, mcv: MCValue) !MCValue {
791 try self.registers.ensureCapacity(self.gpa, self.registers.items().len + 1);
791 try self.registers.ensureCapacity(self.gpa, @intCast(u32, self.registers.count() + 1));
792792
793793 const reg = self.allocReg(reg_owner) orelse b: {
794794 // We'll take over the first register. Move the instruction that was previously
......@@ -1247,7 +1247,7 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type {
12471247 if (inst.base.isUnused())
12481248 return MCValue.dead;
12491249
1250 try self.registers.ensureCapacity(self.gpa, self.registers.items().len + 1);
1250 try self.registers.ensureCapacity(self.gpa, self.registers.count() + 1);
12511251
12521252 const result = self.args[self.arg_index];
12531253 self.arg_index += 1;
src-self-hosted/codegen/c.zig+2-1
......@@ -110,7 +110,8 @@ const Context = struct {
110110 }
111111
112112 fn deinit(self: *Context) void {
113 for (self.inst_map.items()) |kv| {
113 var it = self.inst_map.iterator();
114 while (it.next()) |kv| {
114115 self.file.base.allocator.free(kv.value);
115116 }
116117 self.inst_map.deinit();
src-self-hosted/link.zig+1-1
......@@ -47,7 +47,7 @@ pub const File = struct {
4747 };
4848
4949 /// For DWARF .debug_info.
50 pub const DbgInfoTypeRelocsTable = std.HashMapUnmanaged(Type, DbgInfoTypeReloc, Type.hash, Type.eql, true);
50 pub const DbgInfoTypeRelocsTable = std.HashMapUnmanaged(Type, DbgInfoTypeReloc, Type.hash, Type.eql, std.hash_map.DefaultMaxLoadPercentage);
5151
5252 /// For DWARF .debug_info.
5353 pub const DbgInfoTypeReloc = struct {
src-self-hosted/link/Elf.zig+6-3
......@@ -1629,7 +1629,8 @@ pub fn updateDecl(self: *Elf, module: *Module, decl: *Module.Decl) !void {
16291629
16301630 var dbg_info_type_relocs: File.DbgInfoTypeRelocsTable = .{};
16311631 defer {
1632 for (dbg_info_type_relocs.items()) |*entry| {
1632 var it = dbg_info_type_relocs.iterator();
1633 while (it.next()) |entry| {
16331634 entry.value.relocs.deinit(self.base.allocator);
16341635 }
16351636 dbg_info_type_relocs.deinit(self.base.allocator);
......@@ -1917,7 +1918,8 @@ pub fn updateDecl(self: *Elf, module: *Module, decl: *Module.Decl) !void {
19171918 // Now we emit the .debug_info types of the Decl. These will count towards the size of
19181919 // the buffer, so we have to do it before computing the offset, and we can't perform the actual
19191920 // relocations yet.
1920 for (dbg_info_type_relocs.items()) |*entry| {
1921 var it = dbg_info_type_relocs.iterator();
1922 while (it.next()) |entry| {
19211923 entry.value.off = @intCast(u32, dbg_info_buffer.items.len);
19221924 try self.addDbgInfoType(entry.key, &dbg_info_buffer);
19231925 }
......@@ -1925,7 +1927,8 @@ pub fn updateDecl(self: *Elf, module: *Module, decl: *Module.Decl) !void {
19251927 try self.updateDeclDebugInfoAllocation(text_block, @intCast(u32, dbg_info_buffer.items.len));
19261928
19271929 // Now that we have the offset assigned we can finally perform type relocations.
1928 for (dbg_info_type_relocs.items()) |entry| {
1930 it = dbg_info_type_relocs.iterator();
1931 while (it.next()) |entry| {
19291932 for (entry.value.relocs.items) |off| {
19301933 mem.writeInt(
19311934 u32,
src-self-hosted/liveness.zig+26-15
......@@ -15,7 +15,7 @@ pub fn analyze(
1515
1616 var table = std.AutoHashMap(*ir.Inst, void).init(gpa);
1717 defer table.deinit();
18 try table.ensureCapacity(body.instructions.len);
18 try table.ensureCapacity(@intCast(u32, body.instructions.len));
1919 try analyzeWithTable(arena, &table, null, body);
2020}
2121
......@@ -84,8 +84,11 @@ fn analyzeInst(
8484 try analyzeWithTable(arena, table, &then_table, inst.then_body);
8585
8686 // Reset the table back to its state from before the branch.
87 for (then_table.items()) |entry| {
88 table.removeAssertDiscard(entry.key);
87 {
88 var it = then_table.iterator();
89 while (it.next()) |entry| {
90 table.removeAssertDiscard(entry.key);
91 }
8992 }
9093
9194 var else_table = std.AutoHashMap(*ir.Inst, void).init(table.allocator);
......@@ -97,28 +100,36 @@ fn analyzeInst(
97100 var else_entry_deaths = std.ArrayList(*ir.Inst).init(table.allocator);
98101 defer else_entry_deaths.deinit();
99102
100 for (else_table.items()) |entry| {
101 const else_death = entry.key;
102 if (!then_table.contains(else_death)) {
103 try then_entry_deaths.append(else_death);
103 {
104 var it = else_table.iterator();
105 while (it.next()) |entry| {
106 const else_death = entry.key;
107 if (!then_table.contains(else_death)) {
108 try then_entry_deaths.append(else_death);
109 }
104110 }
105111 }
106112 // This loop is the same, except it's for the then branch, and it additionally
107113 // has to put its items back into the table to undo the reset.
108 for (then_table.items()) |entry| {
109 const then_death = entry.key;
110 if (!else_table.contains(then_death)) {
111 try else_entry_deaths.append(then_death);
114 {
115 var it = then_table.iterator();
116 while (it.next()) |entry| {
117 const then_death = entry.key;
118 if (!else_table.contains(then_death)) {
119 try else_entry_deaths.append(then_death);
120 }
121 _ = try table.put(then_death, {});
112122 }
113 _ = try table.put(then_death, {});
114123 }
115124 // Now we have to correctly populate new_set.
116125 if (new_set) |ns| {
117 try ns.ensureCapacity(ns.items().len + then_table.items().len + else_table.items().len);
118 for (then_table.items()) |entry| {
126 try ns.ensureCapacity(@intCast(u32, ns.count() + then_table.count() + else_table.count()));
127 var it = then_table.iterator();
128 while (it.next()) |entry| {
119129 _ = ns.putAssumeCapacity(entry.key, {});
120130 }
121 for (else_table.items()) |entry| {
131 it = else_table.iterator();
132 while (it.next()) |entry| {
122133 _ = ns.putAssumeCapacity(entry.key, {});
123134 }
124135 }
src-self-hosted/translate_c.zig+6-19
......@@ -19,23 +19,9 @@ pub const Error = error{OutOfMemory};
1919const TypeError = Error || error{UnsupportedType};
2020const TransError = TypeError || error{UnsupportedTranslation};
2121
22const DeclTable = std.HashMap(usize, []const u8, addrHash, addrEql, false);
22const DeclTable = std.AutoArrayHashMap(usize, []const u8);
2323
24fn addrHash(x: usize) u32 {
25 switch (@typeInfo(usize).Int.bits) {
26 32 => return x,
27 // pointers are usually aligned so we ignore the bits that are probably all 0 anyway
28 // usually the larger bits of addr space are unused so we just chop em off
29 64 => return @truncate(u32, x >> 4),
30 else => @compileError("unreachable"),
31 }
32}
33
34fn addrEql(a: usize, b: usize) bool {
35 return a == b;
36}
37
38const SymbolTable = std.StringHashMap(*ast.Node);
24const SymbolTable = std.StringArrayHashMap(*ast.Node);
3925const AliasList = std.ArrayList(struct {
4026 alias: []const u8,
4127 name: []const u8,
......@@ -285,7 +271,7 @@ pub const Context = struct {
285271 /// a list of names that we found by visiting all the top level decls without
286272 /// translating them. The other maps are updated as we translate; this one is updated
287273 /// up front in a pre-processing step.
288 global_names: std.StringHashMap(void),
274 global_names: std.StringArrayHashMap(void),
289275
290276 fn getMangle(c: *Context) u32 {
291277 c.mangle_count += 1;
......@@ -380,7 +366,7 @@ pub fn translate(
380366 .alias_list = AliasList.init(gpa),
381367 .global_scope = try arena.allocator.create(Scope.Root),
382368 .clang_context = ZigClangASTUnit_getASTContext(ast_unit).?,
383 .global_names = std.StringHashMap(void).init(gpa),
369 .global_names = std.StringArrayHashMap(void).init(gpa),
384370 .token_ids = .{},
385371 .token_locs = .{},
386372 .errors = .{},
......@@ -6424,7 +6410,8 @@ fn getFnProto(c: *Context, ref: *ast.Node) ?*ast.Node.FnProto {
64246410}
64256411
64266412fn addMacros(c: *Context) !void {
6427 for (c.global_scope.macro_table.items()) |kv| {
6413 var it = c.global_scope.macro_table.iterator();
6414 while (it.next()) |kv| {
64286415 if (getFnProto(c, kv.value)) |proto_node| {
64296416 // If a macro aliases a global variable which is a function pointer, we conclude that
64306417 // the macro is intended to represent a function that assumes the function pointer
src-self-hosted/type.zig+2-2
......@@ -238,7 +238,7 @@ pub const Type = extern union {
238238 }
239239 }
240240
241 pub fn hash(self: Type) u32 {
241 pub fn hash(self: Type) u64 {
242242 var hasher = std.hash.Wyhash.init(0);
243243 const zig_type_tag = self.zigTypeTag();
244244 std.hash.autoHash(&hasher, zig_type_tag);
......@@ -303,7 +303,7 @@ pub const Type = extern union {
303303 // TODO implement more type hashing
304304 },
305305 }
306 return @truncate(u32, hasher.final());
306 return hasher.final();
307307 }
308308
309309 pub fn copy(self: Type, allocator: *Allocator) error{OutOfMemory}!Type {
src-self-hosted/value.zig+2-1
......@@ -358,7 +358,8 @@ pub const Value = extern union {
358358 .error_set => {
359359 const error_set = val.cast(Payload.ErrorSet).?;
360360 try out_stream.writeAll("error{");
361 for (error_set.fields.items()) |entry| {
361 var it = error_set.fields.iterator();
362 while (it.next()) |entry| {
362363 try out_stream.print("{},", .{entry.value});
363364 }
364365 return out_stream.writeAll("}");
src-self-hosted/zir.zig+3-3
......@@ -1049,7 +1049,7 @@ pub const Module = struct {
10491049 defer write.loop_table.deinit();
10501050
10511051 // First, build a map of *Inst to @ or % indexes
1052 try write.inst_table.ensureCapacity(self.decls.len);
1052 try write.inst_table.ensureCapacity(@intCast(u32, self.decls.len));
10531053
10541054 for (self.decls) |decl, decl_i| {
10551055 try write.inst_table.putNoClobber(decl.inst, .{ .inst = decl.inst, .index = null, .name = decl.name });
......@@ -1685,7 +1685,7 @@ pub fn emit(allocator: *Allocator, old_module: IrModule) !Module {
16851685 .arena = std.heap.ArenaAllocator.init(allocator),
16861686 .old_module = &old_module,
16871687 .next_auto_name = 0,
1688 .names = std.StringHashMap(void).init(allocator),
1688 .names = std.StringArrayHashMap(void).init(allocator),
16891689 .primitive_table = std.AutoHashMap(Inst.Primitive.Builtin, *Decl).init(allocator),
16901690 .indent = 0,
16911691 .block_table = std.AutoHashMap(*ir.Inst.Block, *Inst.Block).init(allocator),
......@@ -1758,7 +1758,7 @@ const EmitZIR = struct {
17581758 arena: std.heap.ArenaAllocator,
17591759 old_module: *const IrModule,
17601760 decls: std.ArrayListUnmanaged(*Decl),
1761 names: std.StringHashMap(void),
1761 names: std.StringArrayHashMap(void),
17621762 next_auto_name: usize,
17631763 primitive_table: std.AutoHashMap(Inst.Primitive.Builtin, *Decl),
17641764 indent: usize,
src-self-hosted/zir_sema.zig+1-1
......@@ -812,7 +812,7 @@ fn analyzeInstErrorSet(mod: *Module, scope: *Scope, inst: *zir.Inst.ErrorSet) In
812812 .fields = .{},
813813 .decl = undefined, // populated below
814814 };
815 try payload.fields.ensureCapacity(&new_decl_arena.allocator, inst.positionals.fields.len);
815 try payload.fields.ensureCapacity(&new_decl_arena.allocator, @intCast(u32, inst.positionals.fields.len));
816816
817817 for (inst.positionals.fields) |field_name| {
818818 const entry = try mod.getErrorValue(field_name);