authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-04-11 00:32:42-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-04-11 00:32:42-04:00
log58c6424d4fe64f88e25714d20d5755d31a7775c1
tree5d7aa554aab5a6b9a012dcdb1562851ba1fdb81e
parent19e0ed5d3e303771e672f8cec42adb67a13fa3af

simplify and fix BufMap logic


2 files changed, 17 insertions(+), 22 deletions(-)

std/buf_map.zig+12-21
......@@ -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 const old_value = ??(try self.hash_map.put(key, value_copy));
35 self.free(old_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,17 +62,12 @@ 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};
7468
75const assert = @import("debug/index.zig").assert;
76const heap = @import("heap.zig");
77
7869test "BufMap" {
79 var direct_allocator = heap.DirectAllocator.init();
70 var direct_allocator = std.heap.DirectAllocator.init();
8071 defer direct_allocator.deinit();
8172
8273 var bufmap = BufMap.init(&direct_allocator.allocator);
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);