2017-04-04 06:52:20 +01:00
|
|
|
const HashMap = @import("hash_map.zig").HashMap;
|
|
|
|
const mem = @import("mem.zig");
|
|
|
|
const Allocator = mem.Allocator;
|
|
|
|
|
|
|
|
/// 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);
|
|
|
|
|
|
|
|
pub fn init(allocator: &Allocator) -> BufMap {
|
|
|
|
var self = BufMap {
|
2017-04-06 10:34:04 +01:00
|
|
|
.hash_map = BufMapHashMap.init(allocator),
|
2017-04-04 06:52:20 +01:00
|
|
|
};
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn deinit(self: &BufMap) {
|
2017-04-06 10:34:04 +01:00
|
|
|
var it = self.hash_map.iterator();
|
2017-04-04 06:52:20 +01:00
|
|
|
while (true) {
|
|
|
|
const entry = it.next() ?? break;
|
|
|
|
self.free(entry.key);
|
|
|
|
self.free(entry.value);
|
|
|
|
}
|
|
|
|
|
|
|
|
self.hash_map.deinit();
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn set(self: &BufMap, key: []const u8, value: []const u8) -> %void {
|
2017-05-03 22:23:11 +01:00
|
|
|
if (self.hash_map.get(key)) |entry| {
|
2018-01-07 21:51:46 +00:00
|
|
|
const value_copy = try self.copy(value);
|
2017-04-04 06:52:20 +01:00
|
|
|
%defer self.free(value_copy);
|
2018-01-07 21:51:46 +00:00
|
|
|
_ = try self.hash_map.put(key, value_copy);
|
2017-04-04 06:52:20 +01:00
|
|
|
self.free(entry.value);
|
|
|
|
} else {
|
2018-01-07 21:51:46 +00:00
|
|
|
const key_copy = try self.copy(key);
|
2017-04-04 06:52:20 +01:00
|
|
|
%defer self.free(key_copy);
|
2018-01-07 21:51:46 +00:00
|
|
|
const value_copy = try self.copy(value);
|
2017-04-04 06:52:20 +01:00
|
|
|
%defer self.free(value_copy);
|
2018-01-07 21:51:46 +00:00
|
|
|
_ = try self.hash_map.put(key_copy, value_copy);
|
2017-04-04 06:52:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-12 21:03:20 +00:00
|
|
|
pub fn get(self: &BufMap, key: []const u8) -> ?[]const u8 {
|
|
|
|
const entry = self.hash_map.get(key) ?? return null;
|
|
|
|
return entry.value;
|
|
|
|
}
|
|
|
|
|
2017-04-04 06:52:20 +01:00
|
|
|
pub fn delete(self: &BufMap, key: []const u8) {
|
|
|
|
const entry = self.hash_map.remove(key) ?? return;
|
|
|
|
self.free(entry.key);
|
|
|
|
self.free(entry.value);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn count(self: &const BufMap) -> usize {
|
|
|
|
return self.hash_map.size;
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn iterator(self: &const BufMap) -> BufMapHashMap.Iterator {
|
2017-04-06 10:34:04 +01:00
|
|
|
return self.hash_map.iterator();
|
2017-04-04 06:52:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn free(self: &BufMap, value: []const u8) {
|
|
|
|
// remove the const
|
2017-05-19 15:39:59 +01:00
|
|
|
const mut_value = @ptrCast(&u8, value.ptr)[0..value.len];
|
2017-04-04 06:52:20 +01:00
|
|
|
self.hash_map.allocator.free(mut_value);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn copy(self: &BufMap, value: []const u8) -> %[]const u8 {
|
2018-01-07 21:51:46 +00:00
|
|
|
const result = try self.hash_map.allocator.alloc(u8, value.len);
|
2017-04-04 06:52:20 +01:00
|
|
|
mem.copy(u8, result, value);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
};
|