2019-03-02 21:46:04 +00:00
|
|
|
const std = @import("std.zig");
|
2019-09-03 22:53:05 +01:00
|
|
|
const StringHashMap = std.StringHashMap;
|
2017-04-04 06:52:20 +01:00
|
|
|
const mem = @import("mem.zig");
|
|
|
|
const Allocator = mem.Allocator;
|
2019-02-08 23:18:47 +00:00
|
|
|
const testing = std.testing;
|
2017-04-04 06:52:20 +01:00
|
|
|
|
2018-11-13 13:08:37 +00:00
|
|
|
pub const BufSet = struct {
|
2017-04-04 06:52:20 +01:00
|
|
|
hash_map: BufSetHashMap,
|
|
|
|
|
2019-09-03 22:53:05 +01:00
|
|
|
const BufSetHashMap = StringHashMap(void);
|
2017-04-04 06:52:20 +01:00
|
|
|
|
2018-05-31 15:56:59 +01:00
|
|
|
pub fn init(a: *Allocator) BufSet {
|
2018-11-13 13:08:37 +00:00
|
|
|
var self = BufSet{ .hash_map = BufSetHashMap.init(a) };
|
2017-04-04 06:52:20 +01:00
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2018-05-31 15:56:59 +01:00
|
|
|
pub fn deinit(self: *const BufSet) void {
|
2017-04-06 10:34:04 +01:00
|
|
|
var it = self.hash_map.iterator();
|
2017-04-04 06:52:20 +01:00
|
|
|
while (true) {
|
2018-06-10 06:13:51 +01:00
|
|
|
const entry = it.next() orelse break;
|
2017-04-04 06:52:20 +01:00
|
|
|
self.free(entry.key);
|
|
|
|
}
|
|
|
|
|
|
|
|
self.hash_map.deinit();
|
|
|
|
}
|
|
|
|
|
2018-05-31 15:56:59 +01:00
|
|
|
pub fn put(self: *BufSet, key: []const u8) !void {
|
2017-04-04 06:52:20 +01:00
|
|
|
if (self.hash_map.get(key) == null) {
|
2018-01-07 21:51:46 +00:00
|
|
|
const key_copy = try self.copy(key);
|
2018-01-24 04:08:09 +00:00
|
|
|
errdefer self.free(key_copy);
|
2018-01-07 21:51:46 +00:00
|
|
|
_ = try self.hash_map.put(key_copy, {});
|
2017-04-04 06:52:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-25 16:37:54 +00:00
|
|
|
pub fn exists(self: BufSet, key: []const u8) bool {
|
|
|
|
return self.hash_map.get(key) != null;
|
|
|
|
}
|
|
|
|
|
2018-05-31 15:56:59 +01:00
|
|
|
pub fn delete(self: *BufSet, key: []const u8) void {
|
2018-06-10 06:13:51 +01:00
|
|
|
const entry = self.hash_map.remove(key) orelse return;
|
2017-04-04 06:52:20 +01:00
|
|
|
self.free(entry.key);
|
|
|
|
}
|
|
|
|
|
2018-05-31 15:56:59 +01:00
|
|
|
pub fn count(self: *const BufSet) usize {
|
2018-05-03 14:54:33 +01:00
|
|
|
return self.hash_map.count();
|
2017-04-04 06:52:20 +01:00
|
|
|
}
|
|
|
|
|
2018-05-31 15:56:59 +01:00
|
|
|
pub fn iterator(self: *const BufSet) BufSetHashMap.Iterator {
|
2017-04-06 10:34:04 +01:00
|
|
|
return self.hash_map.iterator();
|
2017-04-04 06:52:20 +01:00
|
|
|
}
|
|
|
|
|
2018-05-31 15:56:59 +01:00
|
|
|
pub fn allocator(self: *const BufSet) *Allocator {
|
2017-10-13 14:31:03 +01:00
|
|
|
return self.hash_map.allocator;
|
|
|
|
}
|
|
|
|
|
2018-05-31 15:56:59 +01:00
|
|
|
fn free(self: *const BufSet, value: []const u8) void {
|
2018-03-06 21:37:03 +00:00
|
|
|
self.hash_map.allocator.free(value);
|
2017-04-04 06:52:20 +01:00
|
|
|
}
|
|
|
|
|
2018-05-31 15:56:59 +01:00
|
|
|
fn copy(self: *const BufSet, 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;
|
|
|
|
}
|
|
|
|
};
|
2018-05-04 22:48:14 +01:00
|
|
|
|
|
|
|
test "BufSet" {
|
2020-01-30 04:06:26 +00:00
|
|
|
var bufset = BufSet.init(std.testing.allocator);
|
2018-05-04 22:48:14 +01:00
|
|
|
defer bufset.deinit();
|
|
|
|
|
|
|
|
try bufset.put("x");
|
2019-02-08 23:18:47 +00:00
|
|
|
testing.expect(bufset.count() == 1);
|
2018-05-04 22:48:14 +01:00
|
|
|
bufset.delete("x");
|
2019-02-08 23:18:47 +00:00
|
|
|
testing.expect(bufset.count() == 0);
|
2018-05-04 22:48:14 +01:00
|
|
|
|
|
|
|
try bufset.put("x");
|
|
|
|
try bufset.put("y");
|
|
|
|
try bufset.put("z");
|
|
|
|
}
|