Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions src/hash_set.zig
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,20 @@ pub fn HashSetWithContext(comptime E: type, comptime Context: type, comptime max
return prevCount != self.unmanaged.count();
}

/// Appends an element without checking if it was inside the set.
/// Adding an element that already was in the set will corrupt the data structure.
/// Faster than add, pretty unsafe.
pub fn addAssumeNotIn(self: *Self, allocator: Allocator, element: E) Allocator.Error!void {
try self.unmanaged.putNoClobber(allocator, element, {});
}

/// Appends an element without checking if it was inside the set.
/// Adding an element that already was in the set will corrupt the data structure.
/// Faster than add, pretty unsafe.
pub fn addAssumeNotInAndCapacity(self: *Self, element: E) void {
self.unmanaged.putAssumeCapacityNoClobber(element, {});
}

/// Appends all elements from the provided set, and may allocate.
/// append returns an Allocator.Error or Size which represents how
/// many elements added and not previously in the Set.
Expand Down Expand Up @@ -1001,3 +1015,23 @@ test "custom hash function string usage" {
_ = try A.add(testing.allocator, "Hello\t");
try expectEqual(7, A.cardinality());
}

test "add assume not in" {
var A: HashSet(u32) = .empty;
defer A.deinit(testing.allocator);

try A.addAssumeNotIn(testing.allocator, 1);
try expectEqual(1, A.cardinality());
try expect(A.contains(1));
}

test "add assume not in assume capacity" {
var A: HashSet(u32) = try .initCapacity(testing.allocator, 2);
defer A.deinit(testing.allocator);

A.addAssumeNotInAndCapacity(1);
A.addAssumeNotInAndCapacity(2);
try expectEqual(2, A.cardinality());
try expect(A.contains(1));
try expect(A.contains(2));
}
Loading