authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-11-04 15:46:30-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-11-04 15:46:30-07:00
log802c82b0720ff2b7c59ba96effbdd1eed7491df4
treecf11620fbc06adc825df3abdb03c4e38215cd8f4
parent8bd01d2d9bbda1a2e044223db4b2272e7c5020a0

std.ArrayHashMap: add init function

for when you have keys and values array

1 files changed, 13 insertions(+), 0 deletions(-)

lib/std/array_hash_map.zig+13
...@@ -574,6 +574,19 @@ pub fn ArrayHashMapUnmanaged(...@@ -574,6 +574,19 @@ pub fn ArrayHashMapUnmanaged(
574 };574 };
575 }575 }
576576
577 pub fn init(allocator: Allocator, key_list: []const K, value_list: []const V) !Self {
578 var self: Self = .{};
579 try self.entries.resize(allocator, key_list.len);
580 errdefer self.entries.deinit(allocator);
581 @memcpy(self.keys(), key_list);
582 if (@sizeOf(V) != 0) {
583 assert(key_list.len == value_list.len);
584 @memcpy(self.values(), value_list);
585 }
586 try self.reIndex(allocator);
587 return self;
588 }
589
577 /// Frees the backing allocation and leaves the map in an undefined state.590 /// Frees the backing allocation and leaves the map in an undefined state.
578 /// Note that this does not free keys or values. You must take care of that591 /// Note that this does not free keys or values. You must take care of that
579 /// before calling this function, if it is needed.592 /// before calling this function, if it is needed.