2018-04-10 21:32:42 -07:00
|
|
|
const std = @import("index.zig");
|
|
|
|
const HashMap = std.HashMap;
|
|
|
|
const mem = std.mem;
|
2017-04-03 22:52:20 -07:00
|
|
|
const Allocator = mem.Allocator;
|
2018-04-10 21:32:42 -07:00
|
|
|
const assert = std.debug.assert;
|
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.
|
|
|
|
pub const BufMap = struct {
|
|
|
|
hash_map: BufMapHashMap,
|
|
|
|
|
|
|
|
const BufMapHashMap = HashMap([]const u8, []const u8, mem.hash_slice_u8, mem.eql_slice_u8);
|
|
|
|
|
2018-01-25 01:10:11 -08:00
|
|
|
pub fn init(allocator: &Allocator) BufMap {
|
2017-04-03 22:52:20 -07:00
|
|
|
var self = BufMap {
|
2017-04-06 02:34:04 -07:00
|
|
|
.hash_map = BufMapHashMap.init(allocator),
|
2017-04-03 22:52:20 -07:00
|
|
|
};
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2018-01-25 01:10:11 -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) {
|
|
|
|
const entry = it.next() ?? break;
|
|
|
|
self.free(entry.key);
|
|
|
|
self.free(entry.value);
|
|
|
|
}
|
|
|
|
|
|
|
|
self.hash_map.deinit();
|
|
|
|
}
|
|
|
|
|
2018-01-31 19:48:40 -08:00
|
|
|
pub fn set(self: &BufMap, key: []const u8, value: []const u8) !void {
|
2018-04-10 21:32:42 -07: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-03 22:52:20 -07:00
|
|
|
}
|
|
|
|
|
2018-01-25 01:10:11 -08:00
|
|
|
pub fn get(self: &BufMap, key: []const u8) ?[]const u8 {
|
2017-12-12 13:03:20 -08:00
|
|
|
const entry = self.hash_map.get(key) ?? return null;
|
|
|
|
return entry.value;
|
|
|
|
}
|
|
|
|
|
2018-01-25 01:10:11 -08:00
|
|
|
pub fn delete(self: &BufMap, key: []const u8) void {
|
2017-04-03 22:52:20 -07:00
|
|
|
const entry = self.hash_map.remove(key) ?? return;
|
|
|
|
self.free(entry.key);
|
|
|
|
self.free(entry.value);
|
|
|
|
}
|
|
|
|
|
2018-01-25 01:10:11 -08:00
|
|
|
pub fn count(self: &const BufMap) usize {
|
2017-04-03 22:52:20 -07:00
|
|
|
return self.hash_map.size;
|
|
|
|
}
|
|
|
|
|
2018-01-25 01:10:11 -08: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-01-25 01:10:11 -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-01-31 19:48:40 -08:00
|
|
|
fn copy(self: &BufMap, value: []const u8) ![]const 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" {
|
2018-04-10 21:32:42 -07:00
|
|
|
var direct_allocator = std.heap.DirectAllocator.init();
|
2018-04-05 14:26:06 -07:00
|
|
|
defer direct_allocator.deinit();
|
|
|
|
|
|
|
|
var bufmap = BufMap.init(&direct_allocator.allocator);
|
|
|
|
defer bufmap.deinit();
|
|
|
|
|
|
|
|
try bufmap.set("x", "1");
|
|
|
|
assert(mem.eql(u8, ??bufmap.get("x"), "1"));
|
|
|
|
assert(1 == bufmap.count());
|
|
|
|
|
|
|
|
try bufmap.set("x", "2");
|
|
|
|
assert(mem.eql(u8, ??bufmap.get("x"), "2"));
|
|
|
|
assert(1 == bufmap.count());
|
|
|
|
|
|
|
|
try bufmap.set("x", "3");
|
|
|
|
assert(mem.eql(u8, ??bufmap.get("x"), "3"));
|
|
|
|
assert(1 == bufmap.count());
|
|
|
|
|
|
|
|
bufmap.delete("x");
|
|
|
|
assert(0 == bufmap.count());
|
|
|
|
}
|