aboutsummaryrefslogtreecommitdiff
path: root/std/buf_set.zig
diff options
context:
space:
mode:
authortgschultz <tgschultz@gmail.com>2018-05-11 21:36:02 -0500
committerGitHub <noreply@github.com>2018-05-11 21:36:02 -0500
commit8c1872543c8cf76215cc4bf3ced4637bb1065a4e (patch)
tree72dfebb643ab61579e3fb8dd58cd4610ffe876fa /std/buf_set.zig
parent7186e92c86982950d0aa7c0c2deef9ef96bc1264 (diff)
parent6e821078f625a03eb8b7794c983da0f7793366ab (diff)
downloadzig-8c1872543c8cf76215cc4bf3ced4637bb1065a4e.tar.gz
zig-8c1872543c8cf76215cc4bf3ced4637bb1065a4e.zip
Merge pull request #1 from zig-lang/master
Sync with zig-lang/zig master
Diffstat (limited to 'std/buf_set.zig')
-rw-r--r--std/buf_set.zig28
1 files changed, 23 insertions, 5 deletions
diff --git a/std/buf_set.zig b/std/buf_set.zig
index 618b985c41..1badb5bf18 100644
--- a/std/buf_set.zig
+++ b/std/buf_set.zig
@@ -1,6 +1,8 @@
+const std = @import("index.zig");
const HashMap = @import("hash_map.zig").HashMap;
const mem = @import("mem.zig");
const Allocator = mem.Allocator;
+const assert = std.debug.assert;
pub const BufSet = struct {
hash_map: BufSetHashMap,
@@ -14,10 +16,10 @@ pub const BufSet = struct {
return self;
}
- pub fn deinit(self: &BufSet) void {
+ pub fn deinit(self: &const BufSet) void {
var it = self.hash_map.iterator();
while (true) {
- const entry = it.next() ?? break;
+ const entry = it.next() ?? break;
self.free(entry.key);
}
@@ -38,7 +40,7 @@ pub const BufSet = struct {
}
pub fn count(self: &const BufSet) usize {
- return self.hash_map.size;
+ return self.hash_map.count();
}
pub fn iterator(self: &const BufSet) BufSetHashMap.Iterator {
@@ -49,14 +51,30 @@ pub const BufSet = struct {
return self.hash_map.allocator;
}
- fn free(self: &BufSet, value: []const u8) void {
+ fn free(self: &const BufSet, value: []const u8) void {
self.hash_map.allocator.free(value);
}
- fn copy(self: &BufSet, value: []const u8) ![]const u8 {
+ 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 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");
+}