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 {...@@ -20,7 +20,8 @@ pub const BufSet = struct {
20 }20 }
2121
22 pub fn deinit(self: *BufSet) void {22 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| {
24 self.free(entry.key);25 self.free(entry.key);
25 }26 }
26 self.hash_map.deinit();27 self.hash_map.deinit();
lib/std/hash_map.zig+846-697
...@@ -4,91 +4,94 @@...@@ -4,91 +4,94 @@
4// The MIT license requires this copyright notice to be included in all copies4// The MIT license requires this copyright notice to be included in all copies
5// and substantial portions of the software.5// and substantial portions of the software.
6const std = @import("std.zig");6const std = @import("std.zig");
7const debug = std.debug;7const builtin = @import("builtin");
8const assert = debug.assert;8const assert = debug.assert;
9const testing = std.testing;9const autoHash = std.hash.autoHash;
10const debug = std.debug;
11const warn = debug.warn;
10const math = std.math;12const math = std.math;
11const mem = std.mem;13const mem = std.mem;
12const meta = std.meta;14const meta = std.meta;
13const trait = meta.trait;15const trait = meta.trait;
14const autoHash = std.hash.autoHash;
15const Wyhash = std.hash.Wyhash;
16const Allocator = mem.Allocator;16const Allocator = mem.Allocator;
17const builtin = @import("builtin");17const Wyhash = std.hash.Wyhash;
18const hash_map = @This();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
20pub fn AutoHashMap(comptime K: type, comptime V: type) type {41pub 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);
22}43}
2344
24pub fn AutoHashMapUnmanaged(comptime K: type, comptime V: type) type {45pub 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);
26}47}
2748
28/// Builtin hashmap for strings as keys.49/// Builtin hashmap for strings as keys.
29pub fn StringHashMap(comptime V: type) type {50pub fn StringHashMap(comptime V: type) type {
30 return HashMap([]const u8, V, hashString, eqlString, true);51 return HashMap([]const u8, V, hashString, eqlString, DefaultMaxLoadPercentage);
31}52}
3253
33pub fn StringHashMapUnmanaged(comptime V: type) type {54pub fn StringHashMapUnmanaged(comptime V: type) type {
34 return HashMapUnmanaged([]const u8, V, hashString, eqlString, true);55 return HashMapUnmanaged([]const u8, V, hashString, eqlString, DefaultMaxLoadPercentage);
35}56}
3657
37pub fn eqlString(a: []const u8, b: []const u8) bool {58pub fn eqlString(a: []const u8, b: []const u8) bool {
38 return mem.eql(u8, a, b);59 return mem.eql(u8, a, b);
39}60}
4061
41pub fn hashString(s: []const u8) u32 {62pub fn hashString(s: []const u8) u64 {
42 return @truncate(u32, std.hash.Wyhash.hash(0, s));63 return std.hash.Wyhash.hash(0, s);
43}64}
4465
45/// Insertion order is preserved.66pub const DefaultMaxLoadPercentage = 80;
46/// Deletions perform a "swap removal" on the entries list.67
47/// Modifying the hash map while iterating is allowed, however one must understand68/// General purpose hash table.
48/// the (well defined) behavior when mixing insertions and deletions with iteration.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.
49/// For a hash map that can be initialized directly that does not store an Allocator72/// For a hash map that can be initialized directly that does not store an Allocator
50/// field, see `HashMapUnmanaged`.73/// field, see `HashMapUnmanaged`.
51/// When `store_hash` is `false`, this data structure is biased towards cheap `eql`74/// If iterating over the table entries is a strong usecase and needs to be fast,
52/// functions. It does not store each item's hash in the table. Setting `store_hash`75/// prefer the alternative `std.ArrayHashMap`.
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.
55pub fn HashMap(76pub fn HashMap(
56 comptime K: type,77 comptime K: type,
57 comptime V: type,78 comptime V: type,
58 comptime hash: fn (key: K) u32,79 comptime hashFn: fn (key: K) u64,
59 comptime eql: fn (a: K, b: K) bool,80 comptime eqlFn: fn (a: K, b: K) bool,
60 comptime store_hash: bool,81 comptime MaxLoadPercentage: u64,
61) type {82) type {
62 return struct {83 return struct {
63 unmanaged: Unmanaged,84 unmanaged: Unmanaged,
64 allocator: *Allocator,85 allocator: *Allocator,
6586
66 pub const Unmanaged = HashMapUnmanaged(K, V, hash, eql, store_hash);87 pub const Unmanaged = HashMapUnmanaged(K, V, hashFn, eqlFn, MaxLoadPercentage);
67 pub const Entry = Unmanaged.Entry;88 pub const Entry = Unmanaged.Entry;
68 pub const Hash = Unmanaged.Hash;89 pub const Hash = Unmanaged.Hash;
90 pub const Iterator = Unmanaged.Iterator;
91 pub const Size = Unmanaged.Size;
69 pub const GetOrPutResult = Unmanaged.GetOrPutResult;92 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
90 const Self = @This();94 const Self = @This();
91 const Index = Unmanaged.Index;
9295
93 pub fn init(allocator: *Allocator) Self {96 pub fn init(allocator: *Allocator) Self {
94 return .{97 return .{
...@@ -110,17 +113,12 @@ pub fn HashMap(...@@ -110,17 +113,12 @@ pub fn HashMap(
110 return self.unmanaged.clearAndFree(self.allocator);113 return self.unmanaged.clearAndFree(self.allocator);
111 }114 }
112115
113 /// Deprecated. Use `items().len`.
114 pub fn count(self: Self) usize {116 pub fn count(self: Self) usize {
115 return self.items().len;117 return self.unmanaged.count();
116 }118 }
117119
118 /// Deprecated. Iterate using `items`.
119 pub fn iterator(self: *const Self) Iterator {120 pub fn iterator(self: *const Self) Iterator {
120 return Iterator{121 return self.unmanaged.iterator();
121 .hm = self,
122 .index = 0,
123 };
124 }122 }
125123
126 /// If key exists this function cannot fail.124 /// If key exists this function cannot fail.
...@@ -150,13 +148,13 @@ pub fn HashMap(...@@ -150,13 +148,13 @@ pub fn HashMap(
150148
151 /// Increases capacity, guaranteeing that insertions up until the149 /// Increases capacity, guaranteeing that insertions up until the
152 /// `expected_count` will not cause an allocation, and therefore cannot fail.150 /// `expected_count` will not cause an allocation, and therefore cannot fail.
153 pub fn ensureCapacity(self: *Self, new_capacity: usize) !void {151 pub fn ensureCapacity(self: *Self, expected_count: Size) !void {
154 return self.unmanaged.ensureCapacity(self.allocator, new_capacity);152 return self.unmanaged.ensureCapacity(self.allocator, expected_count);
155 }153 }
156154
157 /// Returns the number of total elements which may be present before it is155 /// Returns the number of total elements which may be present before it is
158 /// no longer guaranteed that no allocations will be performed.156 /// no longer guaranteed that no allocations will be performed.
159 pub fn capacity(self: *Self) usize {157 pub fn capacity(self: *Self) Size {
160 return self.unmanaged.capacity();158 return self.unmanaged.capacity();
161 }159 }
162160
...@@ -197,18 +195,14 @@ pub fn HashMap(...@@ -197,18 +195,14 @@ pub fn HashMap(
197 return self.unmanaged.fetchPutAssumeCapacity(key, value);195 return self.unmanaged.fetchPutAssumeCapacity(key, value);
198 }196 }
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
208 pub fn get(self: Self, key: K) ?V {198 pub fn get(self: Self, key: K) ?V {
209 return self.unmanaged.get(key);199 return self.unmanaged.get(key);
210 }200 }
211201
202 pub fn getEntry(self: Self, key: K) ?*Entry {
203 return self.unmanaged.getEntry(key);
204 }
205
212 pub fn contains(self: Self, key: K) bool {206 pub fn contains(self: Self, key: K) bool {
213 return self.unmanaged.contains(key);207 return self.unmanaged.contains(key);
214 }208 }
...@@ -225,10 +219,6 @@ pub fn HashMap(...@@ -225,10 +219,6 @@ pub fn HashMap(
225 return self.unmanaged.removeAssertDiscard(key);219 return self.unmanaged.removeAssertDiscard(key);
226 }220 }
227221
228 pub fn items(self: Self) []Entry {
229 return self.unmanaged.items();
230 }
231
232 pub fn clone(self: Self) !Self {222 pub fn clone(self: Self) !Self {
233 var other = try self.unmanaged.clone(self.allocator);223 var other = try self.unmanaged.clone(self.allocator);
234 return other.promote(self.allocator);224 return other.promote(self.allocator);
...@@ -236,63 +226,152 @@ pub fn HashMap(...@@ -236,63 +226,152 @@ pub fn HashMap(
236 };226 };
237}227}
238228
239/// General purpose hash table.229/// A HashMap based on open addressing and linear probing.
240/// Insertion order is preserved.230/// A lookup or modification typically occurs only 2 cache misses.
241/// Deletions perform a "swap removal" on the entries list.231/// No order is guaranteed and any modification invalidates live iterators.
242/// Modifying the hash map while iterating is allowed, however one must understand232/// It achieves good performance with quite high load factors (by default,
243/// the (well defined) behavior when mixing insertions and deletions with iteration.233/// grow is triggered at 80% full) and only one byte of overhead per element.
244/// This type does not store an Allocator field - the Allocator must be passed in234/// The struct itself is only 16 bytes for a small footprint. This comes at
245/// with each function call that requires it. See `HashMap` for a type that stores235/// the price of handling size with u32, which should be reasonnable enough
246/// an Allocator field for convenience.236/// for almost all uses.
247/// Can be initialized directly using the default field values.237/// Deletions are achieved with tombstones.
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.
256pub fn HashMapUnmanaged(238pub fn HashMapUnmanaged(
257 comptime K: type,239 comptime K: type,
258 comptime V: type,240 comptime V: type,
259 comptime hash: fn (key: K) u32,241 hashFn: fn (key: K) u64,
260 comptime eql: fn (a: K, b: K) bool,242 eqlFn: fn (a: K, b: K) bool,
261 comptime store_hash: bool,243 comptime MaxLoadPercentage: u64,
262) type {244) type {
245 comptime assert(MaxLoadPercentage > 0 and MaxLoadPercentage < 100);
246
263 return struct {247 return struct {
264 /// It is permitted to access this field directly.248 const Self = @This();
265 entries: std.ArrayListUnmanaged(Entry) = .{},249
266250 // This is actually a midway pointer to the single buffer containing
267 /// When entries length is less than `linear_scan_max`, this remains `null`.251 // a `Header` field, the `Metadata`s and `Entry`s.
268 /// Once entries length grows big enough, this field is allocated. There is252 // At `-@sizeOf(Header)` is the Header field.
269 /// an IndexHeader followed by an array of Index(I) structs, where I is defined253 // At `sizeOf(Metadata) * capacity + offset`, which is pointed to by
270 /// by how many total indexes there are.254 // self.header().entries, is the array of entries.
271 index_header: ?*IndexHeader = null,255 // This means that the hashmap only holds one live allocation, to
272256 // reduce memory fragmentation and struct size.
273 /// Modifying the key is illegal behavior.257 /// Pointer to the metadata.
274 /// Modifying the value is allowed.258 metadata: ?[*]Metadata = null,
275 /// Entry pointers become invalid whenever this HashMap is modified,259
276 /// unless `ensureCapacity` was previously used.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
277 pub const Entry = struct {280 pub const Entry = struct {
278 /// This field is `void` if `store_hash` is `false`.
279 hash: Hash,
280 key: K,281 key: K,
281 value: V,282 value: V,
282 };283 };
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
286 pub const GetOrPutResult = struct {369 pub const GetOrPutResult = struct {
287 entry: *Entry,370 entry: *Entry,
288 found_existing: bool,371 found_existing: bool,
289 };372 };
290373
291 pub const Managed = HashMap(K, V, hash, eql, store_hash);374 pub const Managed = HashMap(K, V, hashFn, eqlFn, MaxLoadPercentage);
292
293 const Self = @This();
294
295 const linear_scan_max = 8;
296375
297 pub fn promote(self: Self, allocator: *Allocator) Managed {376 pub fn promote(self: Self, allocator: *Allocator) Managed {
298 return .{377 return .{
...@@ -301,167 +380,156 @@ pub fn HashMapUnmanaged(...@@ -301,167 +380,156 @@ pub fn HashMapUnmanaged(
301 };380 };
302 }381 }
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
304 pub fn deinit(self: *Self, allocator: *Allocator) void {391 pub fn deinit(self: *Self, allocator: *Allocator) void {
305 self.entries.deinit(allocator);392 self.deallocate(allocator);
306 if (self.index_header) |header| {
307 header.free(allocator);
308 }
309 self.* = undefined;393 self.* = undefined;
310 }394 }
311395
312 pub fn clearRetainingCapacity(self: *Self) void {396 fn deallocate(self: *Self, allocator: *Allocator) void {
313 self.entries.items.len = 0;397 if (self.metadata == null) return;
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 }
324398
325 pub fn clearAndFree(self: *Self, allocator: *Allocator) void {399 const cap = self.capacity();
326 self.entries.shrink(allocator, 0);400 const meta_size = @sizeOf(Header) + cap * @sizeOf(Metadata);
327 if (self.index_header) |header| {401
328 header.free(allocator);402 const alignment = @alignOf(Entry) - 1;
329 self.index_header = null;403 const entries_size = @as(usize, cap) * @sizeOf(Entry) + alignment;
330 }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;
331 }414 }
332415
333 /// If key exists this function cannot fail.416 fn capacityForSize(size: Size) Size {
334 /// If there is an existing item with `key`, then the result417 var new_cap = @truncate(u32, (@as(u64, size) * 100) / MaxLoadPercentage + 1);
335 /// `Entry` pointer points to it, and found_existing is true.418 new_cap = math.ceilPowerOfTwo(u32, new_cap) catch unreachable;
336 /// Otherwise, puts a new item with undefined value, and419 return new_cap;
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);
348 }420 }
349421
350 /// If there is an existing item with `key`, then the result422 pub fn ensureCapacity(self: *Self, allocator: *Allocator, new_size: Size) !void {
351 /// `Entry` pointer points to it, and found_existing is true.423 if (new_size > self.size)
352 /// Otherwise, puts a new item with undefined value, and424 try self.growIfNeeded(allocator, new_size - self.size);
353 /// the `Entry` pointer points to it. Caller should then initialize425 }
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 };
380426
381 switch (header.capacityIndexType()) {427 pub fn clearRetainingCapacity(self: *Self) void {
382 .u8 => return self.getOrPutInternal(key, header, u8),428 if (self.metadata) |_| {
383 .u16 => return self.getOrPutInternal(key, header, u16),429 self.initMetadatas();
384 .u32 => return self.getOrPutInternal(key, header, u32),430 self.size = 0;
385 .usize => return self.getOrPutInternal(key, header, usize),431 self.available = 0;
386 }432 }
387 }433 }
388434
389 pub fn getOrPutValue(self: *Self, allocator: *Allocator, key: K, value: V) !*Entry {435 pub fn clearAndFree(self: *Self, allocator: *Allocator) void {
390 const res = try self.getOrPut(allocator, key);436 self.deallocate(allocator);
391 if (!res.found_existing)437 self.size = 0;
392 res.entry.value = value;438 self.available = 0;
439 }
393440
394 return res.entry;441 pub fn count(self: *const Self) Size {
442 return self.size;
395 }443 }
396444
397 /// Increases capacity, guaranteeing that insertions up until the445 fn header(self: *const Self) *Header {
398 /// `expected_count` will not cause an allocation, and therefore cannot fail.446 return @ptrCast(*Header, @ptrCast([*]Header, self.metadata.?) - 1);
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 }
424 }447 }
425448
426 /// Returns the number of total elements which may be present before it is449 fn entries(self: *const Self) [*]Entry {
427 /// no longer guaranteed that no allocations will be performed.450 return self.header().entries;
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);
433 }451 }
434452
435 /// Clobbers any existing data. To detect if a put would clobber453 pub fn capacity(self: *const Self) Size {
436 /// existing data, see `getOrPut`.454 if (self.metadata == null) return 0;
437 pub fn put(self: *Self, allocator: *Allocator, key: K, value: V) !void {455
438 const result = try self.getOrPut(allocator, key);456 return self.header().capacity;
439 result.entry.value = value;
440 }457 }
441458
442 /// Inserts a key-value pair into the hash map, asserting that no previous459 pub fn iterator(self: *const Self) Iterator {
443 /// entry with the same key is already present460 return .{ .hm = self };
461 }
462
463 /// Insert an entry in the map. Assumes it is not already present.
444 pub fn putNoClobber(self: *Self, allocator: *Allocator, key: K, value: V) !void {464 pub fn putNoClobber(self: *Self, allocator: *Allocator, key: K, value: V) !void {
445 const result = try self.getOrPut(allocator, key);465 assert(!self.contains(key));
446 assert(!result.found_existing);466 try self.growIfNeeded(allocator, 1);
447 result.entry.value = value;467
468 self.putAssumeCapacityNoClobber(key, value);
448 }469 }
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`.
453 pub fn putAssumeCapacity(self: *Self, key: K, value: V) void {471 pub fn putAssumeCapacity(self: *Self, key: K, value: V) void {
454 const result = self.getOrPutAssumeCapacity(key);472 const hash = hashFn(key);
455 result.entry.value = value;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;
456 }506 }
457507
458 /// Asserts there is enough capacity to store the new key-value pair.508 /// Insert an entry in the map. Assumes it is not already present,
459 /// Asserts that it does not clobber any existing data.509 /// and that no allocation is needed.
460 /// To detect if a put would clobber existing data, see `getOrPutAssumeCapacity`.
461 pub fn putAssumeCapacityNoClobber(self: *Self, key: K, value: V) void {510 pub fn putAssumeCapacityNoClobber(self: *Self, key: K, value: V) void {
462 const result = self.getOrPutAssumeCapacity(key);511 assert(!self.contains(key));
463 assert(!result.found_existing);512
464 result.entry.value = value;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;
465 }533 }
466534
467 /// Inserts a new `Entry` into the hash map, returning the previous one, if any.535 /// Inserts a new `Entry` into the hash map, returning the previous one, if any.
...@@ -488,400 +556,622 @@ pub fn HashMapUnmanaged(...@@ -488,400 +556,622 @@ pub fn HashMapUnmanaged(
488 }556 }
489557
490 pub fn getEntry(self: Self, key: K) ?*Entry {558 pub fn getEntry(self: Self, key: K) ?*Entry {
491 const index = self.getIndex(key) orelse return null;559 if (self.size == 0) {
492 return &self.entries.items[index];560 return null;
493 }561 }
494562
495 pub fn getIndex(self: Self, key: K) ?usize {563 const hash = hashFn(key);
496 const header = self.index_header orelse {564 const mask = self.capacity() - 1;
497 // Linear scan.565 const fingerprint = Metadata.takeFingerprint(hash);
498 const h = if (store_hash) hash(key) else {};566 var idx = @truncate(usize, hash & mask);
499 for (self.entries.items) |*item, i| {567
500 if (item.hash == h and eql(key, item.key)) {568 var metadata = self.metadata.? + idx;
501 return i;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;
502 }574 }
503 }575 }
504 return null;576 idx = (idx + 1) & mask;
505 };577 metadata = self.metadata.? + idx;
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),
511 }578 }
512 }
513579
514 pub fn get(self: Self, key: K) ?V {580 return null;
515 return if (self.getEntry(key)) |entry| entry.value else null;
516 }581 }
517582
518 pub fn contains(self: Self, key: K) bool {583 /// Insert an entry if the associated key is not already present, otherwise update preexisting value.
519 return self.getEntry(key) != null;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;
520 }588 }
521589
522 /// If there is an `Entry` with a matching key, it is deleted from590 /// Get an optional pointer to the value associated with key, if present.
523 /// the hash map, and then returned from this function.591 pub fn get(self: Self, key: K) ?V {
524 pub fn remove(self: *Self, key: K) ?Entry {592 if (self.size == 0) {
525 const header = self.index_header orelse {593 return null;
526 // Linear scan.594 }
527 const h = if (store_hash) hash(key) else {};595
528 for (self.entries.items) |item, i| {596 const hash = hashFn(key);
529 if (item.hash == h and eql(key, item.key)) {597 const mask = self.capacity() - 1;
530 return self.entries.swapRemove(i);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;
531 }607 }
532 }608 }
533 return null;609 idx = (idx + 1) & mask;
534 };610 metadata = self.metadata.? + idx;
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),
540 }611 }
541 }
542612
543 /// Asserts there is an `Entry` with matching key, deletes it from the hash map,613 return null;
544 /// and discards it.
545 pub fn removeAssertDiscard(self: *Self, key: K) void {
546 assert(self.remove(key) != null);
547 }614 }
548615
549 pub fn items(self: Self) []Entry {616 pub fn getOrPut(self: *Self, allocator: *Allocator, key: K) !GetOrPutResult {
550 return self.entries.items;617 try self.growIfNeeded(allocator, 1);
618
619 return self.getOrPutAssumeCapacity(key);
551 }620 }
552621
553 pub fn clone(self: Self, allocator: *Allocator) !Self {622 pub fn getOrPutAssumeCapacity(self: *Self, key: K) GetOrPutResult {
554 var other: Self = .{};623 const hash = hashFn(key);
555 try other.entries.appendSlice(allocator, self.entries.items);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| {640 idx = (idx + 1) & mask;
558 const new_header = try IndexHeader.alloc(allocator, header.indexes_len);641 metadata = self.metadata.? + idx;
559 other.insertAllEntriesIntoNewHeader(new_header);
560 other.index_header = new_header;
561 }642 }
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 };
563 }659 }
564660
565 fn removeInternal(self: *Self, key: K, header: *IndexHeader, comptime I: type) ?Entry {661 pub fn getOrPutValue(self: *Self, allocator: *Allocator, key: K, value: V) !*Entry {
566 const indexes = header.indexes(I);662 const res = try self.getOrPut(allocator, key);
567 const h = hash(key);663 if (!res.found_existing) res.entry.value = value;
568 const start_index = header.constrainIndex(h);664 return res.entry;
569 var roll_over: usize = 0;665 }
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 }
588666
589 // Now we have to shift over the following indexes.667 /// Return true if there is a value associated with key in the map.
590 roll_over += 1;668 pub fn contains(self: *const Self, key: K) bool {
591 while (roll_over < header.indexes_len) : (roll_over += 1) {669 return self.get(key) != null;
592 const next_index_index = header.constrainIndex(start_index + roll_over);670 }
593 const next_index = &indexes[next_index_index];671
594 if (next_index.isEmpty() or next_index.distance_from_start_index == 0) {672 /// If there is an `Entry` with a matching key, it is deleted from
595 index.setEmpty();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;
596 return removed_entry;691 return removed_entry;
597 }692 }
598 index.* = next_index.*;
599 index.distance_from_start_index -= 1;
600 index = next_index;
601 }693 }
602 unreachable;694 idx = (idx + 1) & mask;
695 metadata = self.metadata.? + idx;
603 }696 }
697
604 return null;698 return null;
605 }699 }
606700
607 fn updateEntryIndex(701 /// Asserts there is an `Entry` with matching key, deletes it from the hash map,
608 self: *Self,702 /// and discards it.
609 header: *IndexHeader,703 pub fn removeAssertDiscard(self: *Self, key: K) void {
610 old_entry_index: usize,704 assert(self.contains(key));
611 new_entry_index: usize,705
612 comptime I: type,706 const hash = hashFn(key);
613 indexes: []Index(I),707 const mask = self.capacity() - 1;
614 ) void {708 const fingerprint = Metadata.takeFingerprint(hash);
615 const h = if (store_hash) self.entries.items[new_entry_index].hash else hash(self.entries.items[new_entry_index].key);709 var idx = @truncate(usize, hash & mask);
616 const start_index = header.constrainIndex(h);710
617 var roll_over: usize = 0;711 var metadata = self.metadata.? + idx;
618 while (roll_over <= header.max_distance_from_start_index) : (roll_over += 1) {712 while (metadata[0].isUsed() or metadata[0].isTombstone()) {
619 const index_index = header.constrainIndex(start_index + roll_over);713 if (metadata[0].isUsed() and metadata[0].fingerprint == fingerprint) {
620 const index = &indexes[index_index];714 const entry = &self.entries()[idx];
621 if (index.entry_index == old_entry_index) {715 if (eqlFn(entry.key, key)) {
622 index.entry_index = @intCast(I, new_entry_index);716 metadata[0].remove();
623 return;717 entry.* = undefined;
718 self.size -= 1;
719 return;
720 }
624 }721 }
722 idx = (idx + 1) & mask;
723 metadata = self.metadata.? + idx;
625 }724 }
725
626 unreachable;726 unreachable;
627 }727 }
628728
629 /// Must ensureCapacity before calling this.729 fn initMetadatas(self: *Self) void {
630 fn getOrPutInternal(self: *Self, key: K, header: *IndexHeader, comptime I: type) GetOrPutResult {730 @memset(@ptrCast([*]u8, self.metadata.?), 0, @sizeOf(Metadata) * self.capacity());
631 const indexes = header.indexes(I);731 }
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 }
659732
660 // This pointer survives the following append because we call733 // This counts the number of occupied slots, used + tombstones, which is
661 // entries.ensureCapacity before getOrPutInternal.734 // what has to stay under the MaxLoadPercentage of capacity.
662 const entry = &self.entries.items[index.entry_index];735 fn load(self: *const Self) Size {
663 const hash_match = if (store_hash) h == entry.hash else true;736 const max_load = (self.capacity() * MaxLoadPercentage) / 100;
664 if (hash_match and eql(key, entry.key)) {737 assert(max_load >= self.available);
665 return .{738 return @truncate(Size, max_load - self.available);
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;
725 }739 }
726740
727 fn getInternal(self: Self, key: K, header: *IndexHeader, comptime I: type) ?usize {741 fn growIfNeeded(self: *Self, allocator: *Allocator, new_count: Size) !void {
728 const indexes = header.indexes(I);742 if (new_count > self.available) {
729 const h = hash(key);743 try self.grow(allocator, capacityForSize(self.load() + new_count));
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;
742 }744 }
743 return null;
744 }745 }
745746
746 fn insertAllEntriesIntoNewHeader(self: *Self, header: *IndexHeader) void {747 pub fn clone(self: Self, allocator: *Allocator) !Self {
747 switch (header.capacityIndexType()) {748 var other = Self{};
748 .u8 => return self.insertAllEntriesIntoNewHeaderGeneric(header, u8),749 if (self.size == 0)
749 .u16 => return self.insertAllEntriesIntoNewHeaderGeneric(header, u16),750 return other;
750 .u32 => return self.insertAllEntriesIntoNewHeaderGeneric(header, u32),751
751 .usize => return self.insertAllEntriesIntoNewHeaderGeneric(header, usize),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 }
752 }767 }
768
769 return other;
753 }770 }
754771
755 fn insertAllEntriesIntoNewHeaderGeneric(self: *Self, header: *IndexHeader, comptime I: type) void {772 fn grow(self: *Self, allocator: *Allocator, new_capacity: Size) !void {
756 const indexes = header.indexes(I);773 const new_cap = std.math.max(new_capacity, MinimalCapacity);
757 entry_loop: for (self.entries.items) |entry, i| {774 assert(new_cap > self.capacity());
758 const h = if (store_hash) entry.hash else hash(entry.key);775 assert(std.math.isPowerOfTwo(new_cap));
759 const start_index = header.constrainIndex(h);776
760 var entry_index = i;777 var map = Self{};
761 var roll_over: usize = 0;778 defer map.deinit(allocator);
762 var distance_from_start_index: usize = 0;779 try map.allocate(allocator, new_cap);
763 while (roll_over < header.indexes_len) : ({780 map.initMetadatas();
764 roll_over += 1;781 map.available = @truncate(u32, (new_cap * MaxLoadPercentage) / 100);
765 distance_from_start_index += 1;782
766 }) {783 if (self.size != 0) {
767 const index_index = header.constrainIndex(start_index + roll_over);784 const old_capacity = self.capacity();
768 const next_index = indexes[index_index];785 var i: Size = 0;
769 if (next_index.isEmpty()) {786 var metadata = self.metadata.?;
770 header.maybeBumpMax(distance_from_start_index);787 var entr = self.entries();
771 indexes[index_index] = .{788 while (i < old_capacity) : (i += 1) {
772 .distance_from_start_index = @intCast(I, distance_from_start_index),789 if (metadata[i].isUsed()) {
773 .entry_index = @intCast(I, entry_index),790 const entry = &entr[i];
774 };791 map.putAssumeCapacityNoClobber(entry.key, entry.value);
775 continue :entry_loop;792 if (map.size == self.size)
776 }793 break;
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;
785 }794 }
786 }795 }
787 unreachable;
788 }796 }
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);
789 }822 }
790 };823 };
791}824}
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 {862 try map.ensureCapacity(20);
796 if (indexes_len < math.maxInt(u8))863 const initial_capacity = map.capacity();
797 return .u8;864 testing.expect(initial_capacity >= 20);
798 if (indexes_len < math.maxInt(u16))865 var i: i32 = 0;
799 return .u16;866 while (i < 20) : (i += 1) {
800 if (indexes_len < math.maxInt(u32))867 testing.expect(map.fetchPutAssumeCapacity(i, i + 10) == null);
801 return .u32;868 }
802 return .usize;869 // shouldn't resize from putAssumeCapacity
870 testing.expect(initial_capacity == map.capacity());
803}871}
804872
805fn capacityIndexSize(indexes_len: usize) usize {873test "std.hash_map ensureCapacity with tombstones" {
806 switch (capacityIndexType(indexes_len)) {874 var map = AutoHashMap(i32, i32).init(std.testing.allocator);
807 .u8 => return @sizeOf(Index(u8)),875 defer map.deinit();
808 .u16 => return @sizeOf(Index(u16)),876
809 .u32 => return @sizeOf(Index(u32)),877 var i: i32 = 0;
810 .usize => return @sizeOf(Index(usize)),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);
811 }883 }
812}884}
813885
814fn Index(comptime I: type) type {886test "std.hash_map clearRetainingCapacity" {
815 return extern struct {887 var map = AutoHashMap(u32, u32).init(std.testing.allocator);
816 entry_index: I,888 defer map.deinit();
817 distance_from_start_index: I,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{896 const cap = map.capacity();
822 .entry_index = math.maxInt(I),897 expect(cap > 0);
823 .distance_from_start_index = undefined,898
824 };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 {910 const growTo = 12456;
827 return idx.entry_index == math.maxInt(I);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);
828 }988 }
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 {997 i = 0;
831 idx.entry_index = math.maxInt(I);998 while (i < 16) : (i += 1) {
999 if (i % 3 == 0) {
1000 expect(!map.contains(i));
1001 } else {
1002 expectEqual(map.get(i).?, i);
832 }1003 }
833 };1004 }
834}1005}
8351006
836/// This struct is trailed by an array of `Index(I)`, where `I`1007test "std.hash_map reverse removes" {
837/// and the array length are determined by `indexes_len`.1008 var map = AutoHashMap(u32, u32).init(std.testing.allocator);
838const IndexHeader = struct {1009 defer map.deinit();
839 max_distance_from_start_index: usize,
840 indexes_len: usize,
8411010
842 fn constrainIndex(header: IndexHeader, i: usize) usize {1011 var i: u32 = 0;
843 // This is an optimization for modulo of power of two integers;1012 while (i < 16) : (i += 1) {
844 // it requires `indexes_len` to always be a power of two.1013 try map.putNoClobber(i, i);
845 return i & (header.indexes_len - 1);
846 }1014 }
8471015
848 fn indexes(header: *IndexHeader, comptime I: type) []Index(I) {1016 i = 16;
849 const start = @ptrCast([*]Index(I), @ptrCast([*]u8, header) + @sizeOf(IndexHeader));1017 while (i > 0) : (i -= 1) {
850 return start[0..header.indexes_len];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 }
851 }1024 }
8521025
853 fn capacityIndexType(header: IndexHeader) CapacityIndexType {1026 expectEqual(map.count(), 0);
854 return hash_map.capacityIndexType(header.indexes_len);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);
855 }1036 }
8561037
857 fn maybeBumpMax(header: *IndexHeader, distance_from_start_index: usize) void {1038 _ = map.remove(7);
858 if (distance_from_start_index > header.max_distance_from_start_index) {1039 _ = map.remove(15);
859 header.max_distance_from_start_index = distance_from_start_index;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);
860 }1053 }
861 }1054 }
8621055
863 fn alloc(allocator: *Allocator, len: usize) !*IndexHeader {1056 try map.put(15, 15);
864 const index_size = hash_map.capacityIndexSize(len);1057 try map.put(13, 13);
865 const nbytes = @sizeOf(IndexHeader) + index_size * len;1058 try map.put(14, 14);
866 const bytes = try allocator.allocAdvanced(u8, @alignOf(IndexHeader), nbytes, .exact);1059 try map.put(7, 7);
867 @memset(bytes.ptr + @sizeOf(IndexHeader), 0xff, bytes.len - @sizeOf(IndexHeader));1060 i = 0;
868 const result = @ptrCast(*IndexHeader, bytes.ptr);1061 while (i < 16) : (i += 1) {
869 result.* = .{1062 expectEqual(map.get(i).?, i);
870 .max_distance_from_start_index = 0,1063 }
871 .indexes_len = len,1064}
872 };1065
873 return result;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);
874 }1158 }
8751159
876 fn free(header: *IndexHeader, allocator: *Allocator) void {1160 i = 0;
877 const index_size = hash_map.capacityIndexSize(header.indexes_len);1161 while (i < 20) : (i += 1) {
878 const ptr = @ptrCast([*]u8, header);1162 var n = try map.getOrPutValue(i, 1);
879 const slice = ptr[0 .. @sizeOf(IndexHeader) + header.indexes_len * index_size];
880 allocator.free(slice);
881 }1163 }
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" {
885 var map = AutoHashMap(i32, i32).init(std.testing.allocator);1175 var map = AutoHashMap(i32, i32).init(std.testing.allocator);
886 defer map.deinit();1176 defer map.deinit();
8871177
...@@ -925,85 +1215,10 @@ test "basic hash map usage" {...@@ -925,85 +1215,10 @@ test "basic hash map usage" {
925 map.removeAssertDiscard(3);1215 map.removeAssertDiscard(3);
926}1216}
9271217
928test "iterator hash map" {1218test "std.hash_map clone" {
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" {
1003 var original = AutoHashMap(i32, i32).init(std.testing.allocator);1219 var original = AutoHashMap(i32, i32).init(std.testing.allocator);
1004 defer original.deinit();1220 defer original.deinit();
10051221
1006 // put more than `linear_scan_max` so we can test that the index header is properly cloned
1007 var i: u8 = 0;1222 var i: u8 = 0;
1008 while (i < 10) : (i += 1) {1223 while (i < 10) : (i += 1) {
1009 try original.putNoClobber(i, i * 10);1224 try original.putNoClobber(i, i * 10);
...@@ -1017,69 +1232,3 @@ test "clone" {...@@ -1017,69 +1232,3 @@ test "clone" {
1017 testing.expect(copy.get(i).? == i * 10);1232 testing.expect(copy.get(i).? == i * 10);
1018 }1233 }
1019}1234}
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 {...@@ -325,7 +325,8 @@ pub fn GeneralPurposeAllocator(comptime config: Config) type {
325 break;325 break;
326 }326 }
327 }327 }
328 for (self.large_allocations.items()) |*large_alloc| {328 var it = self.large_allocations.iterator();
329 while (it.next()) |large_alloc| {
329 log.err("Memory leak detected: {}", .{large_alloc.value.getStackTrace()});330 log.err("Memory leak detected: {}", .{large_alloc.value.getStackTrace()});
330 leaks = true;331 leaks = true;
331 }332 }
...@@ -584,7 +585,7 @@ pub fn GeneralPurposeAllocator(comptime config: Config) type {...@@ -584,7 +585,7 @@ pub fn GeneralPurposeAllocator(comptime config: Config) type {
584 if (new_aligned_size > largest_bucket_object_size) {585 if (new_aligned_size > largest_bucket_object_size) {
585 try self.large_allocations.ensureCapacity(586 try self.large_allocations.ensureCapacity(
586 self.backing_allocator,587 self.backing_allocator,
587 self.large_allocations.entries.items.len + 1,588 self.large_allocations.count() + 1,
588 );589 );
589590
590 const slice = try self.backing_allocator.allocFn(self.backing_allocator, len, ptr_align, len_align, ret_addr);591 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 {...@@ -123,9 +123,9 @@ pub const Headers = struct {
123123
124 pub fn deinit(self: *Self) void {124 pub fn deinit(self: *Self) void {
125 {125 {
126 for (self.index.items()) |*entry| {126 var it = self.index.iterator();
127 const dex = &entry.value;127 while (it.next()) |entry| {
128 dex.deinit(self.allocator);128 entry.value.deinit(self.allocator);
129 self.allocator.free(entry.key);129 self.allocator.free(entry.key);
130 }130 }
131 self.index.deinit(self.allocator);131 self.index.deinit(self.allocator);
...@@ -333,7 +333,8 @@ pub const Headers = struct {...@@ -333,7 +333,8 @@ pub const Headers = struct {
333333
334 fn rebuildIndex(self: *Self) void {334 fn rebuildIndex(self: *Self) void {
335 // clear out the indexes335 // clear out the indexes
336 for (self.index.items()) |*entry| {336 var it = self.index.iterator();
337 while (it.next()) |entry| {
337 entry.value.shrinkRetainingCapacity(0);338 entry.value.shrinkRetainingCapacity(0);
338 }339 }
339 // fill up indexes again; we know capacity is fine from before340 // fill up indexes again; we know capacity is fine from before
lib/std/std.zig+7
...@@ -3,11 +3,15 @@...@@ -3,11 +3,15 @@
3// This file is part of [zig](https://ziglang.org/), which is MIT licensed.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 copies4// The MIT license requires this copyright notice to be included in all copies
5// and substantial portions of the software.5// and substantial portions of the software.
6pub const ArrayHashMap = array_hash_map.ArrayHashMap;
7pub const ArrayHashMapUnmanaged = array_hash_map.ArrayHashMapUnmanaged;
6pub const ArrayList = @import("array_list.zig").ArrayList;8pub const ArrayList = @import("array_list.zig").ArrayList;
7pub const ArrayListAligned = @import("array_list.zig").ArrayListAligned;9pub const ArrayListAligned = @import("array_list.zig").ArrayListAligned;
8pub const ArrayListAlignedUnmanaged = @import("array_list.zig").ArrayListAlignedUnmanaged;10pub const ArrayListAlignedUnmanaged = @import("array_list.zig").ArrayListAlignedUnmanaged;
9pub const ArrayListSentineled = @import("array_list_sentineled.zig").ArrayListSentineled;11pub const ArrayListSentineled = @import("array_list_sentineled.zig").ArrayListSentineled;
10pub const ArrayListUnmanaged = @import("array_list.zig").ArrayListUnmanaged;12pub const ArrayListUnmanaged = @import("array_list.zig").ArrayListUnmanaged;
13pub const AutoArrayHashMap = array_hash_map.AutoArrayHashMap;
14pub const AutoArrayHashMapUnmanaged = array_hash_map.AutoArrayHashMapUnmanaged;
11pub const AutoHashMap = hash_map.AutoHashMap;15pub const AutoHashMap = hash_map.AutoHashMap;
12pub const AutoHashMapUnmanaged = hash_map.AutoHashMapUnmanaged;16pub const AutoHashMapUnmanaged = hash_map.AutoHashMapUnmanaged;
13pub const BloomFilter = @import("bloom_filter.zig").BloomFilter;17pub const BloomFilter = @import("bloom_filter.zig").BloomFilter;
...@@ -32,10 +36,13 @@ pub const SinglyLinkedList = @import("linked_list.zig").SinglyLinkedList;...@@ -32,10 +36,13 @@ pub const SinglyLinkedList = @import("linked_list.zig").SinglyLinkedList;
32pub const SpinLock = @import("spinlock.zig").SpinLock;36pub const SpinLock = @import("spinlock.zig").SpinLock;
33pub const StringHashMap = hash_map.StringHashMap;37pub const StringHashMap = hash_map.StringHashMap;
34pub const StringHashMapUnmanaged = hash_map.StringHashMapUnmanaged;38pub const StringHashMapUnmanaged = hash_map.StringHashMapUnmanaged;
39pub const StringArrayHashMap = array_hash_map.StringArrayHashMap;
40pub const StringArrayHashMapUnmanaged = array_hash_map.StringArrayHashMapUnmanaged;
35pub const TailQueue = @import("linked_list.zig").TailQueue;41pub const TailQueue = @import("linked_list.zig").TailQueue;
36pub const Target = @import("target.zig").Target;42pub const Target = @import("target.zig").Target;
37pub const Thread = @import("thread.zig").Thread;43pub const Thread = @import("thread.zig").Thread;
3844
45pub const array_hash_map = @import("array_hash_map.zig");
39pub const atomic = @import("atomic.zig");46pub const atomic = @import("atomic.zig");
40pub const base64 = @import("base64.zig");47pub const base64 = @import("base64.zig");
41pub const build = @import("build.zig");48pub const build = @import("build.zig");
src-self-hosted/Module.zig+14-13
...@@ -36,17 +36,17 @@ bin_file_path: []const u8,...@@ -36,17 +36,17 @@ bin_file_path: []const u8,
36/// It's rare for a decl to be exported, so we save memory by having a sparse map of36/// It's rare for a decl to be exported, so we save memory by having a sparse map of
37/// Decl pointers to details about them being exported.37/// Decl pointers to details about them being exported.
38/// The Export memory is owned by the `export_owners` table; the slice itself is owned by this table.38/// 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) = .{},
40/// We track which export is associated with the given symbol name for quick40/// We track which export is associated with the given symbol name for quick
41/// detection of symbol collisions.41/// detection of symbol collisions.
42symbol_exports: std.StringHashMapUnmanaged(*Export) = .{},42symbol_exports: std.StringArrayHashMapUnmanaged(*Export) = .{},
43/// This models the Decls that perform exports, so that `decl_exports` can be updated when a Decl43/// This models the Decls that perform exports, so that `decl_exports` can be updated when a Decl
44/// is modified. Note that the key of this table is not the Decl being exported, but the Decl that44/// is modified. Note that the key of this table is not the Decl being exported, but the Decl that
45/// is performing the export of another Decl.45/// is performing the export of another Decl.
46/// This table owns the Export memory.46/// This table owns the Export memory.
47export_owners: std.AutoHashMapUnmanaged(*Decl, []*Export) = .{},47export_owners: std.AutoArrayHashMapUnmanaged(*Decl, []*Export) = .{},
48/// Maps fully qualified namespaced names to the Decl struct for them.48/// 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
51link_error_flags: link.File.ErrorFlags = .{},51link_error_flags: link.File.ErrorFlags = .{},
5252
...@@ -57,13 +57,13 @@ work_queue: std.fifo.LinearFifo(WorkItem, .Dynamic),...@@ -57,13 +57,13 @@ work_queue: std.fifo.LinearFifo(WorkItem, .Dynamic),
57/// The ErrorMsg memory is owned by the decl, using Module's allocator.57/// The ErrorMsg memory is owned by the decl, using Module's allocator.
58/// Note that a Decl can succeed but the Fn it represents can fail. In this case,58/// Note that a Decl can succeed but the Fn it represents can fail. In this case,
59/// a Decl can have a failed_decls entry but have analysis status of success.59/// 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) = .{},
61/// Using a map here for consistency with the other fields here.61/// Using a map here for consistency with the other fields here.
62/// The ErrorMsg memory is owned by the `Scope`, using Module's allocator.62/// The ErrorMsg memory is owned by the `Scope`, using Module's allocator.
63failed_files: std.AutoHashMapUnmanaged(*Scope, *ErrorMsg) = .{},63failed_files: std.AutoArrayHashMapUnmanaged(*Scope, *ErrorMsg) = .{},
64/// Using a map here for consistency with the other fields here.64/// Using a map here for consistency with the other fields here.
65/// The ErrorMsg memory is owned by the `Export`, using Module's allocator.65/// 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
68/// Incrementing integer used to compare against the corresponding Decl68/// Incrementing integer used to compare against the corresponding Decl
69/// field to determine whether a Decl's status applies to an ongoing update, or a69/// field to determine whether a Decl's status applies to an ongoing update, or a
...@@ -201,9 +201,9 @@ pub const Decl = struct {...@@ -201,9 +201,9 @@ pub const Decl = struct {
201 /// typed_value may need to be regenerated.201 /// typed_value may need to be regenerated.
202 dependencies: DepsTable = .{},202 dependencies: DepsTable = .{},
203203
204 /// The reason this is not `std.AutoHashMapUnmanaged` is a workaround for204 /// The reason this is not `std.AutoArrayHashMapUnmanaged` is a workaround for
205 /// stage1 compiler giving me: `error: struct 'Module.Decl' depends on itself`205 /// 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
208 pub fn destroy(self: *Decl, gpa: *Allocator) void {208 pub fn destroy(self: *Decl, gpa: *Allocator) void {
209 gpa.free(mem.spanZ(self.name));209 gpa.free(mem.spanZ(self.name));
...@@ -933,7 +933,8 @@ pub fn deinit(self: *Module) void {...@@ -933,7 +933,8 @@ pub fn deinit(self: *Module) void {
933 self.symbol_exports.deinit(gpa);933 self.symbol_exports.deinit(gpa);
934 self.root_scope.destroy(gpa);934 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| {
937 gpa.free(entry.key);938 gpa.free(entry.key);
938 }939 }
939 self.global_error_set.deinit(gpa);940 self.global_error_set.deinit(gpa);
...@@ -1756,7 +1757,7 @@ fn analyzeRootSrcFile(self: *Module, root_scope: *Scope.File) !void {...@@ -1756,7 +1757,7 @@ fn analyzeRootSrcFile(self: *Module, root_scope: *Scope.File) !void {
17561757
1757 // Keep track of the decls that we expect to see in this file so that1758 // Keep track of the decls that we expect to see in this file so that
1758 // we know which ones have been deleted.1759 // 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);
1760 defer deleted_decls.deinit();1761 defer deleted_decls.deinit();
1761 try deleted_decls.ensureCapacity(root_scope.decls.items.len);1762 try deleted_decls.ensureCapacity(root_scope.decls.items.len);
1762 for (root_scope.decls.items) |file_decl| {1763 for (root_scope.decls.items) |file_decl| {
...@@ -1877,7 +1878,7 @@ fn analyzeRootZIRModule(self: *Module, root_scope: *Scope.ZIRModule) !void {...@@ -1877,7 +1878,7 @@ fn analyzeRootZIRModule(self: *Module, root_scope: *Scope.ZIRModule) !void {
18771878
1878 // Keep track of the decls that we expect to see in this file so that1879 // Keep track of the decls that we expect to see in this file so that
1879 // we know which ones have been deleted.1880 // 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);
1881 defer deleted_decls.deinit();1882 defer deleted_decls.deinit();
1882 try deleted_decls.ensureCapacity(self.decl_table.items().len);1883 try deleted_decls.ensureCapacity(self.decl_table.items().len);
1883 for (self.decl_table.items()) |entry| {1884 for (self.decl_table.items()) |entry| {
...@@ -2087,7 +2088,7 @@ pub fn getErrorValue(self: *Module, name: []const u8) !std.StringHashMapUnmanage...@@ -2087,7 +2088,7 @@ pub fn getErrorValue(self: *Module, name: []const u8) !std.StringHashMapUnmanage
2087 errdefer self.global_error_set.removeAssertDiscard(name);2088 errdefer self.global_error_set.removeAssertDiscard(name);
20882089
2089 gop.entry.key = try self.gpa.dupe(u8, name);2090 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);
2091 return gop.entry.*;2092 return gop.entry.*;
2092}2093}
20932094
src-self-hosted/codegen.zig+4-4
...@@ -359,7 +359,7 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type {...@@ -359,7 +359,7 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type {
359 };359 };
360360
361 const Branch = struct {361 const Branch = struct {
362 inst_table: std.AutoHashMapUnmanaged(*ir.Inst, MCValue) = .{},362 inst_table: std.AutoArrayHashMapUnmanaged(*ir.Inst, MCValue) = .{},
363363
364 fn deinit(self: *Branch, gpa: *Allocator) void {364 fn deinit(self: *Branch, gpa: *Allocator) void {
365 self.inst_table.deinit(gpa);365 self.inst_table.deinit(gpa);
...@@ -750,7 +750,7 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type {...@@ -750,7 +750,7 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type {
750 const ptr_bits = arch.ptrBitWidth();750 const ptr_bits = arch.ptrBitWidth();
751 const ptr_bytes: u64 = @divExact(ptr_bits, 8);751 const ptr_bytes: u64 = @divExact(ptr_bits, 8);
752 if (abi_size <= ptr_bytes) {752 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);
754 if (self.allocReg(inst)) |reg| {754 if (self.allocReg(inst)) |reg| {
755 return MCValue{ .register = registerAlias(reg, abi_size) };755 return MCValue{ .register = registerAlias(reg, abi_size) };
756 }756 }
...@@ -788,7 +788,7 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type {...@@ -788,7 +788,7 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type {
788 /// `reg_owner` is the instruction that gets associated with the register in the register table.788 /// `reg_owner` is the instruction that gets associated with the register in the register table.
789 /// This can have a side effect of spilling instructions to the stack to free up a register.789 /// This can have a side effect of spilling instructions to the stack to free up a register.
790 fn copyToNewRegister(self: *Self, reg_owner: *ir.Inst, mcv: MCValue) !MCValue {790 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
793 const reg = self.allocReg(reg_owner) orelse b: {793 const reg = self.allocReg(reg_owner) orelse b: {
794 // We'll take over the first register. Move the instruction that was previously794 // 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 {...@@ -1247,7 +1247,7 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type {
1247 if (inst.base.isUnused())1247 if (inst.base.isUnused())
1248 return MCValue.dead;1248 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
1252 const result = self.args[self.arg_index];1252 const result = self.args[self.arg_index];
1253 self.arg_index += 1;1253 self.arg_index += 1;
src-self-hosted/codegen/c.zig+2-1
...@@ -110,7 +110,8 @@ const Context = struct {...@@ -110,7 +110,8 @@ const Context = struct {
110 }110 }
111111
112 fn deinit(self: *Context) void {112 fn deinit(self: *Context) void {
113 for (self.inst_map.items()) |kv| {113 var it = self.inst_map.iterator();
114 while (it.next()) |kv| {
114 self.file.base.allocator.free(kv.value);115 self.file.base.allocator.free(kv.value);
115 }116 }
116 self.inst_map.deinit();117 self.inst_map.deinit();
src-self-hosted/link.zig+1-1
...@@ -47,7 +47,7 @@ pub const File = struct {...@@ -47,7 +47,7 @@ pub const File = struct {
47 };47 };
4848
49 /// For DWARF .debug_info.49 /// 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
52 /// For DWARF .debug_info.52 /// For DWARF .debug_info.
53 pub const DbgInfoTypeReloc = struct {53 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 {...@@ -1629,7 +1629,8 @@ pub fn updateDecl(self: *Elf, module: *Module, decl: *Module.Decl) !void {
16291629
1630 var dbg_info_type_relocs: File.DbgInfoTypeRelocsTable = .{};1630 var dbg_info_type_relocs: File.DbgInfoTypeRelocsTable = .{};
1631 defer {1631 defer {
1632 for (dbg_info_type_relocs.items()) |*entry| {1632 var it = dbg_info_type_relocs.iterator();
1633 while (it.next()) |entry| {
1633 entry.value.relocs.deinit(self.base.allocator);1634 entry.value.relocs.deinit(self.base.allocator);
1634 }1635 }
1635 dbg_info_type_relocs.deinit(self.base.allocator);1636 dbg_info_type_relocs.deinit(self.base.allocator);
...@@ -1917,7 +1918,8 @@ pub fn updateDecl(self: *Elf, module: *Module, decl: *Module.Decl) !void {...@@ -1917,7 +1918,8 @@ pub fn updateDecl(self: *Elf, module: *Module, decl: *Module.Decl) !void {
1917 // Now we emit the .debug_info types of the Decl. These will count towards the size of1918 // Now we emit the .debug_info types of the Decl. These will count towards the size of
1918 // the buffer, so we have to do it before computing the offset, and we can't perform the actual1919 // the buffer, so we have to do it before computing the offset, and we can't perform the actual
1919 // relocations yet.1920 // relocations yet.
1920 for (dbg_info_type_relocs.items()) |*entry| {1921 var it = dbg_info_type_relocs.iterator();
1922 while (it.next()) |entry| {
1921 entry.value.off = @intCast(u32, dbg_info_buffer.items.len);1923 entry.value.off = @intCast(u32, dbg_info_buffer.items.len);
1922 try self.addDbgInfoType(entry.key, &dbg_info_buffer);1924 try self.addDbgInfoType(entry.key, &dbg_info_buffer);
1923 }1925 }
...@@ -1925,7 +1927,8 @@ pub fn updateDecl(self: *Elf, module: *Module, decl: *Module.Decl) !void {...@@ -1925,7 +1927,8 @@ pub fn updateDecl(self: *Elf, module: *Module, decl: *Module.Decl) !void {
1925 try self.updateDeclDebugInfoAllocation(text_block, @intCast(u32, dbg_info_buffer.items.len));1927 try self.updateDeclDebugInfoAllocation(text_block, @intCast(u32, dbg_info_buffer.items.len));
19261928
1927 // Now that we have the offset assigned we can finally perform type relocations.1929 // 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| {
1929 for (entry.value.relocs.items) |off| {1932 for (entry.value.relocs.items) |off| {
1930 mem.writeInt(1933 mem.writeInt(
1931 u32,1934 u32,
src-self-hosted/liveness.zig+26-15
...@@ -15,7 +15,7 @@ pub fn analyze(...@@ -15,7 +15,7 @@ pub fn analyze(
1515
16 var table = std.AutoHashMap(*ir.Inst, void).init(gpa);16 var table = std.AutoHashMap(*ir.Inst, void).init(gpa);
17 defer table.deinit();17 defer table.deinit();
18 try table.ensureCapacity(body.instructions.len);18 try table.ensureCapacity(@intCast(u32, body.instructions.len));
19 try analyzeWithTable(arena, &table, null, body);19 try analyzeWithTable(arena, &table, null, body);
20}20}
2121
...@@ -84,8 +84,11 @@ fn analyzeInst(...@@ -84,8 +84,11 @@ fn analyzeInst(
84 try analyzeWithTable(arena, table, &then_table, inst.then_body);84 try analyzeWithTable(arena, table, &then_table, inst.then_body);
8585
86 // Reset the table back to its state from before the branch.86 // Reset the table back to its state from before the branch.
87 for (then_table.items()) |entry| {87 {
88 table.removeAssertDiscard(entry.key);88 var it = then_table.iterator();
89 while (it.next()) |entry| {
90 table.removeAssertDiscard(entry.key);
91 }
89 }92 }
9093
91 var else_table = std.AutoHashMap(*ir.Inst, void).init(table.allocator);94 var else_table = std.AutoHashMap(*ir.Inst, void).init(table.allocator);
...@@ -97,28 +100,36 @@ fn analyzeInst(...@@ -97,28 +100,36 @@ fn analyzeInst(
97 var else_entry_deaths = std.ArrayList(*ir.Inst).init(table.allocator);100 var else_entry_deaths = std.ArrayList(*ir.Inst).init(table.allocator);
98 defer else_entry_deaths.deinit();101 defer else_entry_deaths.deinit();
99102
100 for (else_table.items()) |entry| {103 {
101 const else_death = entry.key;104 var it = else_table.iterator();
102 if (!then_table.contains(else_death)) {105 while (it.next()) |entry| {
103 try then_entry_deaths.append(else_death);106 const else_death = entry.key;
107 if (!then_table.contains(else_death)) {
108 try then_entry_deaths.append(else_death);
109 }
104 }110 }
105 }111 }
106 // This loop is the same, except it's for the then branch, and it additionally112 // This loop is the same, except it's for the then branch, and it additionally
107 // has to put its items back into the table to undo the reset.113 // has to put its items back into the table to undo the reset.
108 for (then_table.items()) |entry| {114 {
109 const then_death = entry.key;115 var it = then_table.iterator();
110 if (!else_table.contains(then_death)) {116 while (it.next()) |entry| {
111 try else_entry_deaths.append(then_death);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, {});
112 }122 }
113 _ = try table.put(then_death, {});
114 }123 }
115 // Now we have to correctly populate new_set.124 // Now we have to correctly populate new_set.
116 if (new_set) |ns| {125 if (new_set) |ns| {
117 try ns.ensureCapacity(ns.items().len + then_table.items().len + else_table.items().len);126 try ns.ensureCapacity(@intCast(u32, ns.count() + then_table.count() + else_table.count()));
118 for (then_table.items()) |entry| {127 var it = then_table.iterator();
128 while (it.next()) |entry| {
119 _ = ns.putAssumeCapacity(entry.key, {});129 _ = ns.putAssumeCapacity(entry.key, {});
120 }130 }
121 for (else_table.items()) |entry| {131 it = else_table.iterator();
132 while (it.next()) |entry| {
122 _ = ns.putAssumeCapacity(entry.key, {});133 _ = ns.putAssumeCapacity(entry.key, {});
123 }134 }
124 }135 }
src-self-hosted/translate_c.zig+6-19
...@@ -19,23 +19,9 @@ pub const Error = error{OutOfMemory};...@@ -19,23 +19,9 @@ pub const Error = error{OutOfMemory};
19const TypeError = Error || error{UnsupportedType};19const TypeError = Error || error{UnsupportedType};
20const TransError = TypeError || error{UnsupportedTranslation};20const 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 {24const SymbolTable = std.StringArrayHashMap(*ast.Node);
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);
39const AliasList = std.ArrayList(struct {25const AliasList = std.ArrayList(struct {
40 alias: []const u8,26 alias: []const u8,
41 name: []const u8,27 name: []const u8,
...@@ -285,7 +271,7 @@ pub const Context = struct {...@@ -285,7 +271,7 @@ pub const Context = struct {
285 /// a list of names that we found by visiting all the top level decls without271 /// a list of names that we found by visiting all the top level decls without
286 /// translating them. The other maps are updated as we translate; this one is updated272 /// translating them. The other maps are updated as we translate; this one is updated
287 /// up front in a pre-processing step.273 /// up front in a pre-processing step.
288 global_names: std.StringHashMap(void),274 global_names: std.StringArrayHashMap(void),
289275
290 fn getMangle(c: *Context) u32 {276 fn getMangle(c: *Context) u32 {
291 c.mangle_count += 1;277 c.mangle_count += 1;
...@@ -380,7 +366,7 @@ pub fn translate(...@@ -380,7 +366,7 @@ pub fn translate(
380 .alias_list = AliasList.init(gpa),366 .alias_list = AliasList.init(gpa),
381 .global_scope = try arena.allocator.create(Scope.Root),367 .global_scope = try arena.allocator.create(Scope.Root),
382 .clang_context = ZigClangASTUnit_getASTContext(ast_unit).?,368 .clang_context = ZigClangASTUnit_getASTContext(ast_unit).?,
383 .global_names = std.StringHashMap(void).init(gpa),369 .global_names = std.StringArrayHashMap(void).init(gpa),
384 .token_ids = .{},370 .token_ids = .{},
385 .token_locs = .{},371 .token_locs = .{},
386 .errors = .{},372 .errors = .{},
...@@ -6424,7 +6410,8 @@ fn getFnProto(c: *Context, ref: *ast.Node) ?*ast.Node.FnProto {...@@ -6424,7 +6410,8 @@ fn getFnProto(c: *Context, ref: *ast.Node) ?*ast.Node.FnProto {
6424}6410}
64256411
6426fn addMacros(c: *Context) !void {6412fn 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| {
6428 if (getFnProto(c, kv.value)) |proto_node| {6415 if (getFnProto(c, kv.value)) |proto_node| {
6429 // If a macro aliases a global variable which is a function pointer, we conclude that6416 // If a macro aliases a global variable which is a function pointer, we conclude that
6430 // the macro is intended to represent a function that assumes the function pointer6417 // 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 {...@@ -238,7 +238,7 @@ pub const Type = extern union {
238 }238 }
239 }239 }
240240
241 pub fn hash(self: Type) u32 {241 pub fn hash(self: Type) u64 {
242 var hasher = std.hash.Wyhash.init(0);242 var hasher = std.hash.Wyhash.init(0);
243 const zig_type_tag = self.zigTypeTag();243 const zig_type_tag = self.zigTypeTag();
244 std.hash.autoHash(&hasher, zig_type_tag);244 std.hash.autoHash(&hasher, zig_type_tag);
...@@ -303,7 +303,7 @@ pub const Type = extern union {...@@ -303,7 +303,7 @@ pub const Type = extern union {
303 // TODO implement more type hashing303 // TODO implement more type hashing
304 },304 },
305 }305 }
306 return @truncate(u32, hasher.final());306 return hasher.final();
307 }307 }
308308
309 pub fn copy(self: Type, allocator: *Allocator) error{OutOfMemory}!Type {309 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 {...@@ -358,7 +358,8 @@ pub const Value = extern union {
358 .error_set => {358 .error_set => {
359 const error_set = val.cast(Payload.ErrorSet).?;359 const error_set = val.cast(Payload.ErrorSet).?;
360 try out_stream.writeAll("error{");360 try out_stream.writeAll("error{");
361 for (error_set.fields.items()) |entry| {361 var it = error_set.fields.iterator();
362 while (it.next()) |entry| {
362 try out_stream.print("{},", .{entry.value});363 try out_stream.print("{},", .{entry.value});
363 }364 }
364 return out_stream.writeAll("}");365 return out_stream.writeAll("}");
src-self-hosted/zir.zig+3-3
...@@ -1049,7 +1049,7 @@ pub const Module = struct {...@@ -1049,7 +1049,7 @@ pub const Module = struct {
1049 defer write.loop_table.deinit();1049 defer write.loop_table.deinit();
10501050
1051 // First, build a map of *Inst to @ or % indexes1051 // 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
1054 for (self.decls) |decl, decl_i| {1054 for (self.decls) |decl, decl_i| {
1055 try write.inst_table.putNoClobber(decl.inst, .{ .inst = decl.inst, .index = null, .name = decl.name });1055 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 {...@@ -1685,7 +1685,7 @@ pub fn emit(allocator: *Allocator, old_module: IrModule) !Module {
1685 .arena = std.heap.ArenaAllocator.init(allocator),1685 .arena = std.heap.ArenaAllocator.init(allocator),
1686 .old_module = &old_module,1686 .old_module = &old_module,
1687 .next_auto_name = 0,1687 .next_auto_name = 0,
1688 .names = std.StringHashMap(void).init(allocator),1688 .names = std.StringArrayHashMap(void).init(allocator),
1689 .primitive_table = std.AutoHashMap(Inst.Primitive.Builtin, *Decl).init(allocator),1689 .primitive_table = std.AutoHashMap(Inst.Primitive.Builtin, *Decl).init(allocator),
1690 .indent = 0,1690 .indent = 0,
1691 .block_table = std.AutoHashMap(*ir.Inst.Block, *Inst.Block).init(allocator),1691 .block_table = std.AutoHashMap(*ir.Inst.Block, *Inst.Block).init(allocator),
...@@ -1758,7 +1758,7 @@ const EmitZIR = struct {...@@ -1758,7 +1758,7 @@ const EmitZIR = struct {
1758 arena: std.heap.ArenaAllocator,1758 arena: std.heap.ArenaAllocator,
1759 old_module: *const IrModule,1759 old_module: *const IrModule,
1760 decls: std.ArrayListUnmanaged(*Decl),1760 decls: std.ArrayListUnmanaged(*Decl),
1761 names: std.StringHashMap(void),1761 names: std.StringArrayHashMap(void),
1762 next_auto_name: usize,1762 next_auto_name: usize,
1763 primitive_table: std.AutoHashMap(Inst.Primitive.Builtin, *Decl),1763 primitive_table: std.AutoHashMap(Inst.Primitive.Builtin, *Decl),
1764 indent: usize,1764 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...@@ -812,7 +812,7 @@ fn analyzeInstErrorSet(mod: *Module, scope: *Scope, inst: *zir.Inst.ErrorSet) In
812 .fields = .{},812 .fields = .{},
813 .decl = undefined, // populated below813 .decl = undefined, // populated below
814 };814 };
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
817 for (inst.positionals.fields) |field_name| {817 for (inst.positionals.fields) |field_name| {
818 const entry = try mod.getErrorValue(field_name);818 const entry = try mod.getErrorValue(field_name);