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 @@...@@ -1,6 +1,8 @@
1const HashMap = @import("hash_map.zig").HashMap;1const std = @import("index.zig");
2const mem = @import("mem.zig");2const HashMap = std.HashMap;
3const mem = std.mem;
3const Allocator = mem.Allocator;4const Allocator = mem.Allocator;
5const assert = std.debug.assert;
46
5/// BufMap copies keys and values before they go into the map, and7/// BufMap copies keys and values before they go into the map, and
6/// frees them when they get removed.8/// frees them when they get removed.
...@@ -28,18 +30,12 @@ pub const BufMap = struct {...@@ -28,18 +30,12 @@ pub const BufMap = struct {
28 }30 }
2931
30 pub fn set(self: &BufMap, key: []const u8, value: []const u8) !void {32 pub fn set(self: &BufMap, key: []const u8, value: []const u8) !void {
31 if (self.hash_map.get(key)) |entry| {33 self.delete(key);
32 const value_copy = try self.copy(value);34 const key_copy = try self.copy(key);
33 errdefer self.free(value_copy);35 errdefer self.free(key_copy);
34 const old_value = ??(try self.hash_map.put(key, value_copy));36 const value_copy = try self.copy(value);
35 self.free(old_value);37 errdefer self.free(value_copy);
36 } else {38 _ = try self.hash_map.put(key_copy, value_copy);
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 }
43 }39 }
4440
45 pub fn get(self: &BufMap, key: []const u8) ?[]const u8 {41 pub fn get(self: &BufMap, key: []const u8) ?[]const u8 {
...@@ -66,17 +62,12 @@ pub const BufMap = struct {...@@ -66,17 +62,12 @@ pub const BufMap = struct {
66 }62 }
6763
68 fn copy(self: &BufMap, value: []const u8) ![]const u8 {64 fn copy(self: &BufMap, value: []const u8) ![]const u8 {
69 const result = try self.hash_map.allocator.alloc(u8, value.len);65 return mem.dupe(self.hash_map.allocator, u8, value);
70 mem.copy(u8, result, value);
71 return result;
72 }66 }
73};67};
7468
75const assert = @import("debug/index.zig").assert;
76const heap = @import("heap.zig");
77
78test "BufMap" {69test "BufMap" {
79 var direct_allocator = heap.DirectAllocator.init();70 var direct_allocator = std.heap.DirectAllocator.init();
80 defer direct_allocator.deinit();71 defer direct_allocator.deinit();
8172
82 var bufmap = BufMap.init(&direct_allocator.allocator);73 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,...@@ -114,6 +114,7 @@ pub fn HashMap(comptime K: type, comptime V: type,
114 }114 }
115115
116 pub fn remove(hm: &Self, key: K) ?&Entry {116 pub fn remove(hm: &Self, key: K) ?&Entry {
117 if (hm.entries.len == 0) return null;
117 hm.incrementModificationCount();118 hm.incrementModificationCount();
118 const start_index = hm.keyToIndex(key);119 const start_index = hm.keyToIndex(key);
119 {var roll_over: usize = 0; while (roll_over <= hm.max_distance_from_start_index) : (roll_over += 1) {120 {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,...@@ -236,7 +237,10 @@ pub fn HashMap(comptime K: type, comptime V: type,
236}237}
237238
238test "basic hash map usage" {239test "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);
240 defer map.deinit();244 defer map.deinit();
241245
242 assert((map.put(1, 11) catch unreachable) == null);246 assert((map.put(1, 11) catch unreachable) == null);