2018-05-04 14:48:14 -07:00
|
|
|
const std = @import("index.zig");
|
2017-04-03 22:52:20 -07:00
|
|
|
const HashMap = @import("hash_map.zig").HashMap;
|
|
|
|
const mem = @import("mem.zig");
|
|
|
|
const Allocator = mem.Allocator;
|
2018-05-04 14:48:14 -07:00
|
|
|
const assert = std.debug.assert;
|
2017-04-03 22:52:20 -07:00
|
|
|
|
2018-10-15 06:51:15 -07:00
|
|
|
pub const BufSet = struct.{
|
2017-04-03 22:52:20 -07:00
|
|
|
hash_map: BufSetHashMap,
|
|
|
|
|
|
|
|
const BufSetHashMap = HashMap([]const u8, void, mem.hash_slice_u8, mem.eql_slice_u8);
|
|
|
|
|
2018-05-31 07:56:59 -07:00
|
|
|
pub fn init(a: *Allocator) BufSet {
|
2018-10-15 06:51:15 -07:00
|
|
|
var self = BufSet.{ .hash_map = BufSetHashMap.init(a) };
|
2017-04-03 22:52:20 -07:00
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2018-05-31 07:56:59 -07:00
|
|
|
pub fn deinit(self: *const BufSet) 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.hash_map.deinit();
|
|
|
|
}
|
|
|
|
|
2018-05-31 07:56:59 -07:00
|
|
|
pub fn put(self: *BufSet, key: []const u8) !void {
|
2017-04-03 22:52:20 -07:00
|
|
|
if (self.hash_map.get(key) == null) {
|
2018-01-07 13:51:46 -08:00
|
|
|
const key_copy = try self.copy(key);
|
2018-01-23 20:08:09 -08:00
|
|
|
errdefer self.free(key_copy);
|
2018-01-07 13:51:46 -08:00
|
|
|
_ = try self.hash_map.put(key_copy, {});
|
2017-04-03 22:52:20 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-31 07:56:59 -07:00
|
|
|
pub fn delete(self: *BufSet, 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);
|
|
|
|
}
|
|
|
|
|
2018-05-31 07:56:59 -07:00
|
|
|
pub fn count(self: *const BufSet) 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 BufSet) BufSetHashMap.Iterator {
|
2017-04-06 02:34:04 -07:00
|
|
|
return self.hash_map.iterator();
|
2017-04-03 22:52:20 -07:00
|
|
|
}
|
|
|
|
|
2018-05-31 07:56:59 -07:00
|
|
|
pub fn allocator(self: *const BufSet) *Allocator {
|
2017-10-13 06:31:03 -07:00
|
|
|
return self.hash_map.allocator;
|
|
|
|
}
|
|
|
|
|
2018-05-31 07:56:59 -07:00
|
|
|
fn free(self: *const BufSet, 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-05-31 07:56:59 -07:00
|
|
|
fn copy(self: *const BufSet, value: []const u8) ![]const u8 {
|
2018-01-07 13:51:46 -08:00
|
|
|
const result = try self.hash_map.allocator.alloc(u8, value.len);
|
2017-04-03 22:52:20 -07:00
|
|
|
mem.copy(u8, result, value);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
};
|
2018-05-04 14:48:14 -07:00
|
|
|
|
|
|
|
test "BufSet" {
|
|
|
|
var direct_allocator = std.heap.DirectAllocator.init();
|
|
|
|
defer direct_allocator.deinit();
|
|
|
|
|
|
|
|
var bufset = BufSet.init(&direct_allocator.allocator);
|
|
|
|
defer bufset.deinit();
|
|
|
|
|
|
|
|
try bufset.put("x");
|
|
|
|
assert(bufset.count() == 1);
|
|
|
|
bufset.delete("x");
|
|
|
|
assert(bufset.count() == 0);
|
|
|
|
|
|
|
|
try bufset.put("x");
|
|
|
|
try bufset.put("y");
|
|
|
|
try bufset.put("z");
|
|
|
|
}
|