2019-03-02 13:46:04 -08:00
|
|
|
const std = @import("std.zig");
|
2019-09-03 14:53:05 -07:00
|
|
|
const StringHashMap = std.StringHashMap;
|
2018-04-10 21:32:42 -07:00
|
|
|
const mem = std.mem;
|
2017-04-03 22:52:20 -07:00
|
|
|
const Allocator = mem.Allocator;
|
2019-02-08 15:18:47 -08:00
|
|
|
const testing = std.testing;
|
2017-04-03 22:52:20 -07:00
|
|
|
|
|
|
|
/// BufMap copies keys and values before they go into the map, and
|
|
|
|
/// frees them when they get removed.
|
2018-11-13 05:08:37 -08:00
|
|
|
pub const BufMap = struct {
|
2017-04-03 22:52:20 -07:00
|
|
|
hash_map: BufMapHashMap,
|
|
|
|
|
2019-09-03 14:53:05 -07:00
|
|
|
const BufMapHashMap = StringHashMap([]const u8);
|
2017-04-03 22:52:20 -07:00
|
|
|
|
2018-05-31 07:56:59 -07:00
|
|
|
pub fn init(allocator: *Allocator) BufMap {
|
2018-11-13 05:08:37 -08:00
|
|
|
var self = BufMap{ .hash_map = BufMapHashMap.init(allocator) };
|
2017-04-03 22:52:20 -07:00
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2018-12-13 14:13:10 -08:00
|
|
|
pub fn deinit(self: *BufMap) void {
|
2017-04-06 02:34:04 -07:00
|
|
|
var it = self.hash_map.iterator();
|
2017-04-03 22:52:20 -07:00
|
|
|
while (true) {
|
2018-06-09 22:13:51 -07:00
|
|
|
const entry = it.next() orelse break;
|
2017-04-03 22:52:20 -07:00
|
|
|
self.free(entry.key);
|
|
|
|
self.free(entry.value);
|
|
|
|
}
|
|
|
|
|
|
|
|
self.hash_map.deinit();
|
|
|
|
}
|
|
|
|
|
2018-12-13 14:13:10 -08:00
|
|
|
/// Same as `set` but the key and value become owned by the BufMap rather
|
|
|
|
/// than being copied.
|
|
|
|
/// If `setMove` fails, the ownership of key and value does not transfer.
|
|
|
|
pub fn setMove(self: *BufMap, key: []u8, value: []u8) !void {
|
|
|
|
const get_or_put = try self.hash_map.getOrPut(key);
|
|
|
|
if (get_or_put.found_existing) {
|
|
|
|
self.free(get_or_put.kv.key);
|
|
|
|
get_or_put.kv.key = key;
|
|
|
|
}
|
|
|
|
get_or_put.kv.value = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// `key` and `value` are copied into the BufMap.
|
2018-05-31 07:56:59 -07:00
|
|
|
pub fn set(self: *BufMap, key: []const u8, value: []const u8) !void {
|
2018-04-10 21:32:42 -07:00
|
|
|
const value_copy = try self.copy(value);
|
|
|
|
errdefer self.free(value_copy);
|
2018-12-13 14:13:10 -08:00
|
|
|
// Avoid copying key if it already exists
|
|
|
|
const get_or_put = try self.hash_map.getOrPut(key);
|
|
|
|
if (!get_or_put.found_existing) {
|
|
|
|
get_or_put.kv.key = self.copy(key) catch |err| {
|
|
|
|
_ = self.hash_map.remove(key);
|
|
|
|
return err;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
get_or_put.kv.value = value_copy;
|
2017-04-03 22:52:20 -07:00
|
|
|
}
|
|
|
|
|
2018-12-13 14:13:10 -08:00
|
|
|
pub fn get(self: BufMap, key: []const u8) ?[]const u8 {
|
2018-06-09 22:13:51 -07:00
|
|
|
const entry = self.hash_map.get(key) orelse return null;
|
2017-12-12 13:03:20 -08:00
|
|
|
return entry.value;
|
|
|
|
}
|
|
|
|
|
2018-05-31 07:56:59 -07:00
|
|
|
pub fn delete(self: *BufMap, key: []const u8) void {
|
2018-06-09 22:13:51 -07:00
|
|
|
const entry = self.hash_map.remove(key) orelse return;
|
2017-04-03 22:52:20 -07:00
|
|
|
self.free(entry.key);
|
|
|
|
self.free(entry.value);
|
|
|
|
}
|
|
|
|
|
2018-12-13 14:13:10 -08:00
|
|
|
pub fn count(self: BufMap) usize {
|
2018-05-03 06:54:33 -07:00
|
|
|
return self.hash_map.count();
|
2017-04-03 22:52:20 -07:00
|
|
|
}
|
|
|
|
|
2018-05-31 07:56:59 -07:00
|
|
|
pub fn iterator(self: *const BufMap) BufMapHashMap.Iterator {
|
2017-04-06 02:34:04 -07:00
|
|
|
return self.hash_map.iterator();
|
2017-04-03 22:52:20 -07:00
|
|
|
}
|
|
|
|
|
2018-12-13 14:13:10 -08:00
|
|
|
fn free(self: BufMap, value: []const u8) void {
|
2018-03-06 13:37:03 -08:00
|
|
|
self.hash_map.allocator.free(value);
|
2017-04-03 22:52:20 -07:00
|
|
|
}
|
|
|
|
|
2018-12-13 14:13:10 -08:00
|
|
|
fn copy(self: BufMap, value: []const u8) ![]u8 {
|
2018-04-10 21:32:42 -07:00
|
|
|
return mem.dupe(self.hash_map.allocator, u8, value);
|
2017-04-03 22:52:20 -07:00
|
|
|
}
|
|
|
|
};
|
2018-04-05 14:26:06 -07:00
|
|
|
|
|
|
|
test "BufMap" {
|
2020-01-29 20:06:26 -08:00
|
|
|
// TODO: uncomment and fix the leak
|
2019-11-25 14:25:06 -08:00
|
|
|
var bufmap = BufMap.init(std.heap.page_allocator);
|
2018-04-05 14:26:06 -07:00
|
|
|
defer bufmap.deinit();
|
|
|
|
|
|
|
|
try bufmap.set("x", "1");
|
2019-02-08 15:18:47 -08:00
|
|
|
testing.expect(mem.eql(u8, bufmap.get("x").?, "1"));
|
|
|
|
testing.expect(1 == bufmap.count());
|
2018-04-05 14:26:06 -07:00
|
|
|
|
|
|
|
try bufmap.set("x", "2");
|
2019-02-08 15:18:47 -08:00
|
|
|
testing.expect(mem.eql(u8, bufmap.get("x").?, "2"));
|
|
|
|
testing.expect(1 == bufmap.count());
|
2018-04-05 14:26:06 -07:00
|
|
|
|
|
|
|
try bufmap.set("x", "3");
|
2019-02-08 15:18:47 -08:00
|
|
|
testing.expect(mem.eql(u8, bufmap.get("x").?, "3"));
|
|
|
|
testing.expect(1 == bufmap.count());
|
2018-04-05 14:26:06 -07:00
|
|
|
|
|
|
|
bufmap.delete("x");
|
2019-02-08 15:18:47 -08:00
|
|
|
testing.expect(0 == bufmap.count());
|
2018-05-04 14:48:14 -07:00
|
|
|
}
|