From 802c82b0720ff2b7c59ba96effbdd1eed7491df4 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Sat, 4 Nov 2023 15:46:30 -0700 Subject: [PATCH] std.ArrayHashMap: add init function for when you have keys and values array --- lib/std/array_hash_map.zig | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/lib/std/array_hash_map.zig b/lib/std/array_hash_map.zig index 75a86f63f6..bf5f6581ac 100644 --- a/lib/std/array_hash_map.zig +++ b/lib/std/array_hash_map.zig @@ -574,6 +574,19 @@ pub fn ArrayHashMapUnmanaged( }; } + pub fn init(allocator: Allocator, key_list: []const K, value_list: []const V) !Self { + var self: Self = .{}; + try self.entries.resize(allocator, key_list.len); + errdefer self.entries.deinit(allocator); + @memcpy(self.keys(), key_list); + if (@sizeOf(V) != 0) { + assert(key_list.len == value_list.len); + @memcpy(self.values(), value_list); + } + try self.reIndex(allocator); + return self; + } + /// Frees the backing allocation and leaves the map in an undefined state. /// Note that this does not free keys or values. You must take care of that /// before calling this function, if it is needed.