| ... | @@ -1,6 +1,8 @@ | ... | @@ -1,6 +1,8 @@ |
| 1 | const HashMap = @import("hash_map.zig").HashMap; | 1 | const std = @import("index.zig"); |
| 2 | const mem = @import("mem.zig"); | 2 | const HashMap = std.HashMap; |
| | 3 | const mem = std.mem; |
| 3 | const Allocator = mem.Allocator; | 4 | const Allocator = mem.Allocator; |
| | 5 | const assert = std.debug.assert; |
| 4 | | 6 | |
| 5 | /// BufMap copies keys and values before they go into the map, and | 7 | /// 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 | } |
| 29 | | 31 | |
| 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 | _ = try self.hash_map.put(key, value_copy); | 36 | const value_copy = try self.copy(value); |
| 35 | self.free(entry.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 | } |
| 44 | | 40 | |
| 45 | pub fn get(self: &BufMap, key: []const u8) ?[]const u8 { | 41 | pub fn get(self: &BufMap, key: []const u8) ?[]const u8 { |
| ... | @@ -66,8 +62,29 @@ pub const BufMap = struct { | ... | @@ -66,8 +62,29 @@ pub const BufMap = struct { |
| 66 | } | 62 | } |
| 67 | | 63 | |
| 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 | }; |
| | 68 | |
| | 69 | test "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 | } |