aboutsummaryrefslogtreecommitdiff
path: root/lib/std/buf_set.zig
diff options
context:
space:
mode:
authorAndrew Kelley <andrew@ziglang.org>2019-09-26 01:54:45 -0400
committerGitHub <noreply@github.com>2019-09-26 01:54:45 -0400
commit68bb3945708c43109c48bda3664176307d45b62c (patch)
treeafb9731e10cef9d192560b52cd9ae2cf179775c4 /lib/std/buf_set.zig
parent6128bc728d1e1024a178c16c2149f5b1a167a013 (diff)
parent4637e8f9699af9c3c6cf4df50ef5bb67c7a318a4 (diff)
downloadzig-68bb3945708c43109c48bda3664176307d45b62c.tar.gz
zig-68bb3945708c43109c48bda3664176307d45b62c.zip
Merge pull request #3315 from ziglang/mv-std-lib
Move std/ to lib/std/
Diffstat (limited to 'lib/std/buf_set.zig')
-rw-r--r--lib/std/buf_set.zig79
1 files changed, 79 insertions, 0 deletions
diff --git a/lib/std/buf_set.zig b/lib/std/buf_set.zig
new file mode 100644
index 0000000000..1a321e89c9
--- /dev/null
+++ b/lib/std/buf_set.zig
@@ -0,0 +1,79 @@
+const std = @import("std.zig");
+const StringHashMap = std.StringHashMap;
+const mem = @import("mem.zig");
+const Allocator = mem.Allocator;
+const testing = std.testing;
+
+pub const BufSet = struct {
+ hash_map: BufSetHashMap,
+
+ const BufSetHashMap = StringHashMap(void);
+
+ pub fn init(a: *Allocator) BufSet {
+ var self = BufSet{ .hash_map = BufSetHashMap.init(a) };
+ return self;
+ }
+
+ pub fn deinit(self: *const BufSet) void {
+ var it = self.hash_map.iterator();
+ while (true) {
+ const entry = it.next() orelse break;
+ self.free(entry.key);
+ }
+
+ self.hash_map.deinit();
+ }
+
+ pub fn put(self: *BufSet, key: []const u8) !void {
+ if (self.hash_map.get(key) == null) {
+ const key_copy = try self.copy(key);
+ errdefer self.free(key_copy);
+ _ = try self.hash_map.put(key_copy, {});
+ }
+ }
+
+ pub fn exists(self: BufSet, key: []const u8) bool {
+ return self.hash_map.get(key) != null;
+ }
+
+ pub fn delete(self: *BufSet, key: []const u8) void {
+ const entry = self.hash_map.remove(key) orelse return;
+ self.free(entry.key);
+ }
+
+ pub fn count(self: *const BufSet) usize {
+ return self.hash_map.count();
+ }
+
+ pub fn iterator(self: *const BufSet) BufSetHashMap.Iterator {
+ return self.hash_map.iterator();
+ }
+
+ pub fn allocator(self: *const BufSet) *Allocator {
+ return self.hash_map.allocator;
+ }
+
+ fn free(self: *const BufSet, value: []const u8) void {
+ self.hash_map.allocator.free(value);
+ }
+
+ fn copy(self: *const BufSet, value: []const u8) ![]const u8 {
+ const result = try self.hash_map.allocator.alloc(u8, value.len);
+ mem.copy(u8, result, value);
+ return result;
+ }
+};
+
+test "BufSet" {
+ var bufset = BufSet.init(std.heap.direct_allocator);
+ defer bufset.deinit();
+
+ try bufset.put("x");
+ testing.expect(bufset.count() == 1);
+ bufset.delete("x");
+ testing.expect(bufset.count() == 0);
+
+ try bufset.put("x");
+ try bufset.put("y");
+ try bufset.put("z");
+}