| 1 | const std = @import("std.zig"); |
| 2 | const StringHashMap = std.StringHashMap; |
| 3 | const mem = std.mem; |
| 4 | const Allocator = mem.Allocator; |
| 5 | const testing = std.testing; |
| 6 | |
| 7 | /// BufMap copies keys and values before they go into the map and |
| 8 | /// frees them when they get removed. |
| 9 | pub const BufMap = struct { |
| 10 | hash_map: BufMapHashMap, |
| 11 | |
| 12 | const BufMapHashMap = StringHashMap([]const u8); |
| 13 | |
| 14 | /// Create a BufMap backed by a specific allocator. |
| 15 | /// That allocator will be used for both backing allocations |
| 16 | /// and string deduplication. |
| 17 | pub fn init(allocator: Allocator) BufMap { |
| 18 | return .{ .hash_map = BufMapHashMap.init(allocator) }; |
| 19 | } |
| 20 | |
| 21 | /// Free the backing storage of the map, as well as all |
| 22 | /// of the stored keys and values. |
| 23 | pub fn deinit(self: *BufMap) void { |
| 24 | var it = self.hash_map.iterator(); |
| 25 | while (it.next()) |entry| { |
| 26 | self.free(entry.key_ptr.*); |
| 27 | self.free(entry.value_ptr.*); |
| 28 | } |
| 29 | |
| 30 | self.hash_map.deinit(); |
| 31 | } |
| 32 | |
| 33 | /// Same as `put` but the key and value become owned by the BufMap rather |
| 34 | /// than being copied. |
| 35 | /// If `putMove` fails, the ownership of key and value does not transfer. |
| 36 | pub fn putMove(self: *BufMap, key: []u8, value: []u8) !void { |
| 37 | const get_or_put = try self.hash_map.getOrPut(key); |
| 38 | if (get_or_put.found_existing) { |
| 39 | self.free(get_or_put.key_ptr.*); |
| 40 | self.free(get_or_put.value_ptr.*); |
| 41 | get_or_put.key_ptr.* = key; |
| 42 | } |
| 43 | get_or_put.value_ptr.* = value; |
| 44 | } |
| 45 | |
| 46 | /// `key` and `value` are copied into the BufMap. |
| 47 | pub fn put(self: *BufMap, key: []const u8, value: []const u8) !void { |
| 48 | const value_copy = try self.copy(value); |
| 49 | errdefer self.free(value_copy); |
| 50 | const get_or_put = try self.hash_map.getOrPut(key); |
| 51 | if (get_or_put.found_existing) { |
| 52 | self.free(get_or_put.value_ptr.*); |
| 53 | } else { |
| 54 | get_or_put.key_ptr.* = self.copy(key) catch |err| { |
| 55 | _ = self.hash_map.remove(key); |
| 56 | return err; |
| 57 | }; |
| 58 | } |
| 59 | get_or_put.value_ptr.* = value_copy; |
| 60 | } |
| 61 | |
| 62 | /// Find the address of the value associated with a key. |
| 63 | /// The returned pointer is invalidated if the map resizes. |
| 64 | pub fn getPtr(self: BufMap, key: []const u8) ?*[]const u8 { |
| 65 | return self.hash_map.getPtr(key); |
| 66 | } |
| 67 | |
| 68 | /// Return the map's copy of the value associated with |
| 69 | /// a key. The returned string is invalidated if this |
| 70 | /// key is removed from the map. |
| 71 | pub fn get(self: BufMap, key: []const u8) ?[]const u8 { |
| 72 | return self.hash_map.get(key); |
| 73 | } |
| 74 | |
| 75 | /// Removes the item from the map and frees its value. |
| 76 | /// This invalidates the value returned by get() for this key. |
| 77 | pub fn remove(self: *BufMap, key: []const u8) void { |
| 78 | const kv = self.hash_map.fetchRemove(key) orelse return; |
| 79 | self.free(kv.key); |
| 80 | self.free(kv.value); |
| 81 | } |
| 82 | |
| 83 | /// Returns the number of KV pairs stored in the map. |
| 84 | pub fn count(self: BufMap) BufMapHashMap.Size { |
| 85 | return self.hash_map.count(); |
| 86 | } |
| 87 | |
| 88 | /// Returns an iterator over entries in the map. |
| 89 | pub fn iterator(self: *const BufMap) BufMapHashMap.Iterator { |
| 90 | return self.hash_map.iterator(); |
| 91 | } |
| 92 | |
| 93 | fn free(self: BufMap, value: []const u8) void { |
| 94 | self.hash_map.allocator.free(value); |
| 95 | } |
| 96 | |
| 97 | fn copy(self: BufMap, value: []const u8) ![]u8 { |
| 98 | return self.hash_map.allocator.dupe(u8, value); |
| 99 | } |
| 100 | }; |
| 101 | |
| 102 | test "BufMap" { |
| 103 | const allocator = std.testing.allocator; |
| 104 | var bufmap = BufMap.init(allocator); |
| 105 | defer bufmap.deinit(); |
| 106 | |
| 107 | try bufmap.put("x", "1"); |
| 108 | try testing.expect(mem.eql(u8, bufmap.get("x").?, "1")); |
| 109 | try testing.expect(1 == bufmap.count()); |
| 110 | |
| 111 | try bufmap.put("x", "2"); |
| 112 | try testing.expect(mem.eql(u8, bufmap.get("x").?, "2")); |
| 113 | try testing.expect(1 == bufmap.count()); |
| 114 | |
| 115 | try bufmap.put("x", "3"); |
| 116 | try testing.expect(mem.eql(u8, bufmap.get("x").?, "3")); |
| 117 | try testing.expect(1 == bufmap.count()); |
| 118 | |
| 119 | bufmap.remove("x"); |
| 120 | try testing.expect(0 == bufmap.count()); |
| 121 | |
| 122 | try bufmap.putMove(try allocator.dupe(u8, "k"), try allocator.dupe(u8, "v1")); |
| 123 | try bufmap.putMove(try allocator.dupe(u8, "k"), try allocator.dupe(u8, "v2")); |
| 124 | } |