2019-03-02 21:46:04 +00:00
|
|
|
const std = @import("std.zig");
|
2019-09-03 21:53:05 +00:00
|
|
|
const StringHashMap = std.StringHashMap;
|
2018-04-11 04:32:42 +00:00
|
|
|
const mem = std.mem;
|
2017-04-04 05:52:20 +00:00
|
|
|
const Allocator = mem.Allocator;
|
2019-02-08 23:18:47 +00:00
|
|
|
const testing = std.testing;
|
2017-04-04 05:52:20 +00:00
|
|
|
|
2023-04-22 04:21:17 +00:00
|
|
|
/// BufMap copies keys and values before they go into the map and
|
2017-04-04 05:52:20 +00:00
|
|
|
/// frees them when they get removed.
|
2018-11-13 13:08:37 +00:00
|
|
|
pub const BufMap = struct {
|
2017-04-04 05:52:20 +00:00
|
|
|
hash_map: BufMapHashMap,
|
|
|
|
|
2022-02-05 06:35:22 +00:00
|
|
|
const BufMapHashMap = StringHashMap([]const u8);
|
2017-04-04 05:52:20 +00:00
|
|
|
|
2021-06-03 20:39:26 +00:00
|
|
|
/// Create a BufMap backed by a specific allocator.
|
|
|
|
/// That allocator will be used for both backing allocations
|
|
|
|
/// and string deduplication.
|
2021-10-28 23:37:25 +00:00
|
|
|
pub fn init(allocator: Allocator) BufMap {
|
2023-11-10 05:27:17 +00:00
|
|
|
return .{ .hash_map = BufMapHashMap.init(allocator) };
|
2017-04-04 05:52:20 +00:00
|
|
|
}
|
|
|
|
|
2021-06-03 20:39:26 +00:00
|
|
|
/// Free the backing storage of the map, as well as all
|
|
|
|
/// of the stored keys and values.
|
2018-12-13 22:13:10 +00:00
|
|
|
pub fn deinit(self: *BufMap) void {
|
2017-04-06 09:34:04 +00:00
|
|
|
var it = self.hash_map.iterator();
|
2021-06-03 20:39:26 +00:00
|
|
|
while (it.next()) |entry| {
|
|
|
|
self.free(entry.key_ptr.*);
|
|
|
|
self.free(entry.value_ptr.*);
|
2017-04-04 05:52:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
self.hash_map.deinit();
|
|
|
|
}
|
|
|
|
|
2021-06-03 20:39:26 +00:00
|
|
|
/// Same as `put` but the key and value become owned by the BufMap rather
|
2018-12-13 22:13:10 +00:00
|
|
|
/// than being copied.
|
2021-06-03 20:39:26 +00:00
|
|
|
/// If `putMove` fails, the ownership of key and value does not transfer.
|
|
|
|
pub fn putMove(self: *BufMap, key: []u8, value: []u8) !void {
|
2018-12-13 22:13:10 +00:00
|
|
|
const get_or_put = try self.hash_map.getOrPut(key);
|
|
|
|
if (get_or_put.found_existing) {
|
2021-06-03 20:39:26 +00:00
|
|
|
self.free(get_or_put.key_ptr.*);
|
|
|
|
self.free(get_or_put.value_ptr.*);
|
|
|
|
get_or_put.key_ptr.* = key;
|
2018-12-13 22:13:10 +00:00
|
|
|
}
|
2021-06-03 20:39:26 +00:00
|
|
|
get_or_put.value_ptr.* = value;
|
2018-12-13 22:13:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// `key` and `value` are copied into the BufMap.
|
2021-06-03 20:39:26 +00:00
|
|
|
pub fn put(self: *BufMap, key: []const u8, value: []const u8) !void {
|
2018-04-11 04:32:42 +00:00
|
|
|
const value_copy = try self.copy(value);
|
|
|
|
errdefer self.free(value_copy);
|
2018-12-13 22:13:10 +00:00
|
|
|
const get_or_put = try self.hash_map.getOrPut(key);
|
2020-01-30 04:17:15 +00:00
|
|
|
if (get_or_put.found_existing) {
|
2021-06-03 20:39:26 +00:00
|
|
|
self.free(get_or_put.value_ptr.*);
|
2020-01-30 04:17:15 +00:00
|
|
|
} else {
|
2021-06-03 20:39:26 +00:00
|
|
|
get_or_put.key_ptr.* = self.copy(key) catch |err| {
|
2018-12-13 22:13:10 +00:00
|
|
|
_ = self.hash_map.remove(key);
|
|
|
|
return err;
|
|
|
|
};
|
|
|
|
}
|
2021-06-03 20:39:26 +00:00
|
|
|
get_or_put.value_ptr.* = value_copy;
|
2017-04-04 05:52:20 +00:00
|
|
|
}
|
|
|
|
|
2021-06-03 20:39:26 +00:00
|
|
|
/// Find the address of the value associated with a key.
|
|
|
|
/// The returned pointer is invalidated if the map resizes.
|
|
|
|
pub fn getPtr(self: BufMap, key: []const u8) ?*[]const u8 {
|
|
|
|
return self.hash_map.getPtr(key);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Return the map's copy of the value associated with
|
|
|
|
/// a key. The returned string is invalidated if this
|
|
|
|
/// key is removed from the map.
|
2018-12-13 22:13:10 +00:00
|
|
|
pub fn get(self: BufMap, key: []const u8) ?[]const u8 {
|
2020-07-04 01:31:29 +00:00
|
|
|
return self.hash_map.get(key);
|
2017-12-12 21:03:20 +00:00
|
|
|
}
|
|
|
|
|
2021-06-03 20:39:26 +00:00
|
|
|
/// Removes the item from the map and frees its value.
|
|
|
|
/// This invalidates the value returned by get() for this key.
|
|
|
|
pub fn remove(self: *BufMap, key: []const u8) void {
|
|
|
|
const kv = self.hash_map.fetchRemove(key) orelse return;
|
|
|
|
self.free(kv.key);
|
|
|
|
self.free(kv.value);
|
2017-04-04 05:52:20 +00:00
|
|
|
}
|
|
|
|
|
2021-06-03 20:39:26 +00:00
|
|
|
/// Returns the number of KV pairs stored in the map.
|
2022-01-20 10:21:52 +00:00
|
|
|
pub fn count(self: BufMap) BufMapHashMap.Size {
|
2018-05-03 13:54:33 +00:00
|
|
|
return self.hash_map.count();
|
2017-04-04 05:52:20 +00:00
|
|
|
}
|
|
|
|
|
2021-06-03 20:39:26 +00:00
|
|
|
/// Returns an iterator over entries in the map.
|
2018-05-31 14:56:59 +00:00
|
|
|
pub fn iterator(self: *const BufMap) BufMapHashMap.Iterator {
|
2017-04-06 09:34:04 +00:00
|
|
|
return self.hash_map.iterator();
|
2017-04-04 05:52:20 +00:00
|
|
|
}
|
|
|
|
|
2018-12-13 22:13:10 +00:00
|
|
|
fn free(self: BufMap, value: []const u8) void {
|
2018-03-06 21:37:03 +00:00
|
|
|
self.hash_map.allocator.free(value);
|
2017-04-04 05:52:20 +00:00
|
|
|
}
|
|
|
|
|
2018-12-13 22:13:10 +00:00
|
|
|
fn copy(self: BufMap, value: []const u8) ![]u8 {
|
2020-07-04 11:44:28 +00:00
|
|
|
return self.hash_map.allocator.dupe(u8, value);
|
2017-04-04 05:52:20 +00:00
|
|
|
}
|
|
|
|
};
|
2018-04-05 21:26:06 +00:00
|
|
|
|
|
|
|
test "BufMap" {
|
2020-11-29 18:30:37 +00:00
|
|
|
const allocator = std.testing.allocator;
|
|
|
|
var bufmap = BufMap.init(allocator);
|
2018-04-05 21:26:06 +00:00
|
|
|
defer bufmap.deinit();
|
|
|
|
|
2021-06-03 20:39:26 +00:00
|
|
|
try bufmap.put("x", "1");
|
2021-05-04 17:47:26 +00:00
|
|
|
try testing.expect(mem.eql(u8, bufmap.get("x").?, "1"));
|
|
|
|
try testing.expect(1 == bufmap.count());
|
2018-04-05 21:26:06 +00:00
|
|
|
|
2021-06-03 20:39:26 +00:00
|
|
|
try bufmap.put("x", "2");
|
2021-05-04 17:47:26 +00:00
|
|
|
try testing.expect(mem.eql(u8, bufmap.get("x").?, "2"));
|
|
|
|
try testing.expect(1 == bufmap.count());
|
2018-04-05 21:26:06 +00:00
|
|
|
|
2021-06-03 20:39:26 +00:00
|
|
|
try bufmap.put("x", "3");
|
2021-05-04 17:47:26 +00:00
|
|
|
try testing.expect(mem.eql(u8, bufmap.get("x").?, "3"));
|
|
|
|
try testing.expect(1 == bufmap.count());
|
2018-04-05 21:26:06 +00:00
|
|
|
|
2021-06-03 20:39:26 +00:00
|
|
|
bufmap.remove("x");
|
2021-05-04 17:47:26 +00:00
|
|
|
try testing.expect(0 == bufmap.count());
|
2020-11-29 18:30:37 +00:00
|
|
|
|
2021-06-03 20:39:26 +00:00
|
|
|
try bufmap.putMove(try allocator.dupe(u8, "k"), try allocator.dupe(u8, "v1"));
|
|
|
|
try bufmap.putMove(try allocator.dupe(u8, "k"), try allocator.dupe(u8, "v2"));
|
2018-05-04 21:48:14 +00:00
|
|
|
}
|