authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-04-11 00:33:14-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-04-11 00:33:14-04:00
logf43711e5fbbedafa1c28c933fdca0949427c77cd
tree5d7aa554aab5a6b9a012dcdb1562851ba1fdb81e
parent2ec1cec92d018d25f720c43a7586a12eafd2cbeb
parent58c6424d4fe64f88e25714d20d5755d31a7775c1

Merge branch 'bnoordhuis-fix879'


2 files changed, 39 insertions(+), 18 deletions(-)

std/buf_map.zig+34-17
......@@ -1,6 +1,8 @@
1const HashMap = @import("hash_map.zig").HashMap;
2const mem = @import("mem.zig");
1const std = @import("index.zig");
2const HashMap = std.HashMap;
3const mem = std.mem;
34const Allocator = mem.Allocator;
5const assert = std.debug.assert;
46
57/// BufMap copies keys and values before they go into the map, and
68/// frees them when they get removed.
......@@ -28,18 +30,12 @@ pub const BufMap = struct {
2830 }
2931
3032 pub fn set(self: &BufMap, key: []const u8, value: []const u8) !void {
31 if (self.hash_map.get(key)) |entry| {
32 const value_copy = try self.copy(value);
33 errdefer self.free(value_copy);
34 _ = try self.hash_map.put(key, value_copy);
35 self.free(entry.value);
36 } else {
37 const key_copy = try self.copy(key);
38 errdefer self.free(key_copy);
39 const value_copy = try self.copy(value);
40 errdefer self.free(value_copy);
41 _ = try self.hash_map.put(key_copy, value_copy);
42 }
33 self.delete(key);
34 const key_copy = try self.copy(key);
35 errdefer self.free(key_copy);
36 const value_copy = try self.copy(value);
37 errdefer self.free(value_copy);
38 _ = try self.hash_map.put(key_copy, value_copy);
4339 }
4440
4541 pub fn get(self: &BufMap, key: []const u8) ?[]const u8 {
......@@ -66,8 +62,29 @@ pub const BufMap = struct {
6662 }
6763
6864 fn copy(self: &BufMap, value: []const u8) ![]const u8 {
69 const result = try self.hash_map.allocator.alloc(u8, value.len);
70 mem.copy(u8, result, value);
71 return result;
65 return mem.dupe(self.hash_map.allocator, u8, value);
7266 }
7367};
68
69test "BufMap" {
70 var direct_allocator = std.heap.DirectAllocator.init();
71 defer direct_allocator.deinit();
72
73 var bufmap = BufMap.init(&direct_allocator.allocator);
74 defer bufmap.deinit();
75
76 try bufmap.set("x", "1");
77 assert(mem.eql(u8, ??bufmap.get("x"), "1"));
78 assert(1 == bufmap.count());
79
80 try bufmap.set("x", "2");
81 assert(mem.eql(u8, ??bufmap.get("x"), "2"));
82 assert(1 == bufmap.count());
83
84 try bufmap.set("x", "3");
85 assert(mem.eql(u8, ??bufmap.get("x"), "3"));
86 assert(1 == bufmap.count());
87
88 bufmap.delete("x");
89 assert(0 == bufmap.count());
90}
std/hash_map.zig+5-1
......@@ -114,6 +114,7 @@ pub fn HashMap(comptime K: type, comptime V: type,
114114 }
115115
116116 pub fn remove(hm: &Self, key: K) ?&Entry {
117 if (hm.entries.len == 0) return null;
117118 hm.incrementModificationCount();
118119 const start_index = hm.keyToIndex(key);
119120 {var roll_over: usize = 0; while (roll_over <= hm.max_distance_from_start_index) : (roll_over += 1) {
......@@ -236,7 +237,10 @@ pub fn HashMap(comptime K: type, comptime V: type,
236237}
237238
238239test "basic hash map usage" {
239 var map = HashMap(i32, i32, hash_i32, eql_i32).init(debug.global_allocator);
240 var direct_allocator = std.heap.DirectAllocator.init();
241 defer direct_allocator.deinit();
242
243 var map = HashMap(i32, i32, hash_i32, eql_i32).init(&direct_allocator.allocator);
240244 defer map.deinit();
241245
242246 assert((map.put(1, 11) catch unreachable) == null);