2018-04-11 04:32:42 +00:00
|
|
|
const std = @import("index.zig");
|
|
|
|
const HashMap = std.HashMap;
|
|
|
|
const mem = std.mem;
|
2017-04-04 05:52:20 +00:00
|
|
|
const Allocator = mem.Allocator;
|
2018-04-11 04:32:42 +00:00
|
|
|
const assert = std.debug.assert;
|
2017-04-04 05:52:20 +00:00
|
|
|
|
|
|
|
/// BufMap copies keys and values before they go into the map, and
|
|
|
|
/// frees them when they get removed.
|
2018-10-15 13:51:15 +00:00
|
|
|
pub const BufMap = struct.{
|
2017-04-04 05:52:20 +00:00
|
|
|
hash_map: BufMapHashMap,
|
|
|
|
|
|
|
|
const BufMapHashMap = HashMap([]const u8, []const u8, mem.hash_slice_u8, mem.eql_slice_u8);
|
|
|
|
|
2018-05-31 14:56:59 +00:00
|
|
|
pub fn init(allocator: *Allocator) BufMap {
|
2018-10-15 13:51:15 +00:00
|
|
|
var self = BufMap.{ .hash_map = BufMapHashMap.init(allocator) };
|
2017-04-04 05:52:20 +00:00
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2018-05-31 14:56:59 +00:00
|
|
|
pub fn deinit(self: *const BufMap) void {
|
2017-04-06 09:34:04 +00:00
|
|
|
var it = self.hash_map.iterator();
|
2017-04-04 05:52:20 +00:00
|
|
|
while (true) {
|
2018-06-10 05:13:51 +00:00
|
|
|
const entry = it.next() orelse break;
|
2017-04-04 05:52:20 +00:00
|
|
|
self.free(entry.key);
|
|
|
|
self.free(entry.value);
|
|
|
|
}
|
|
|
|
|
|
|
|
self.hash_map.deinit();
|
|
|
|
}
|
|
|
|
|
2018-05-31 14:56:59 +00:00
|
|
|
pub fn set(self: *BufMap, key: []const u8, value: []const u8) !void {
|
2018-04-11 04:32:42 +00:00
|
|
|
self.delete(key);
|
|
|
|
const key_copy = try self.copy(key);
|
|
|
|
errdefer self.free(key_copy);
|
|
|
|
const value_copy = try self.copy(value);
|
|
|
|
errdefer self.free(value_copy);
|
|
|
|
_ = try self.hash_map.put(key_copy, value_copy);
|
2017-04-04 05:52:20 +00:00
|
|
|
}
|
|
|
|
|
2018-05-31 14:56:59 +00:00
|
|
|
pub fn get(self: *const BufMap, key: []const u8) ?[]const u8 {
|
2018-06-10 05:13:51 +00:00
|
|
|
const entry = self.hash_map.get(key) orelse return null;
|
2017-12-12 21:03:20 +00:00
|
|
|
return entry.value;
|
|
|
|
}
|
|
|
|
|
2018-05-31 14:56:59 +00:00
|
|
|
pub fn delete(self: *BufMap, key: []const u8) void {
|
2018-06-10 05:13:51 +00:00
|
|
|
const entry = self.hash_map.remove(key) orelse return;
|
2017-04-04 05:52:20 +00:00
|
|
|
self.free(entry.key);
|
|
|
|
self.free(entry.value);
|
|
|
|
}
|
|
|
|
|
2018-05-31 14:56:59 +00:00
|
|
|
pub fn count(self: *const BufMap) usize {
|
2018-05-03 13:54:33 +00:00
|
|
|
return self.hash_map.count();
|
2017-04-04 05:52:20 +00:00
|
|
|
}
|
|
|
|
|
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-05-31 14:56:59 +00:00
|
|
|
fn free(self: *const 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-05-31 14:56:59 +00:00
|
|
|
fn copy(self: *const BufMap, value: []const u8) ![]const u8 {
|
2018-04-11 04:32:42 +00:00
|
|
|
return mem.dupe(self.hash_map.allocator, u8, value);
|
2017-04-04 05:52:20 +00:00
|
|
|
}
|
|
|
|
};
|
2018-04-05 21:26:06 +00:00
|
|
|
|
|
|
|
test "BufMap" {
|
2018-04-11 04:32:42 +00:00
|
|
|
var direct_allocator = std.heap.DirectAllocator.init();
|
2018-04-05 21:26:06 +00:00
|
|
|
defer direct_allocator.deinit();
|
|
|
|
|
|
|
|
var bufmap = BufMap.init(&direct_allocator.allocator);
|
|
|
|
defer bufmap.deinit();
|
|
|
|
|
|
|
|
try bufmap.set("x", "1");
|
2018-06-10 03:42:14 +00:00
|
|
|
assert(mem.eql(u8, bufmap.get("x").?, "1"));
|
2018-04-05 21:26:06 +00:00
|
|
|
assert(1 == bufmap.count());
|
|
|
|
|
|
|
|
try bufmap.set("x", "2");
|
2018-06-10 03:42:14 +00:00
|
|
|
assert(mem.eql(u8, bufmap.get("x").?, "2"));
|
2018-04-05 21:26:06 +00:00
|
|
|
assert(1 == bufmap.count());
|
|
|
|
|
|
|
|
try bufmap.set("x", "3");
|
2018-06-10 03:42:14 +00:00
|
|
|
assert(mem.eql(u8, bufmap.get("x").?, "3"));
|
2018-04-05 21:26:06 +00:00
|
|
|
assert(1 == bufmap.count());
|
|
|
|
|
|
|
|
bufmap.delete("x");
|
|
|
|
assert(0 == bufmap.count());
|
2018-05-04 21:48:14 +00:00
|
|
|
}
|