From e4dd56dc29bd7496e7ba489b047a285278c256ee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pau=20Soler=20Valad=C3=A9s?= Date: Tue, 25 Aug 2026 12:07:41 +0200 Subject: [PATCH 1/4] Changed add to use getOrPut. added an add test (check true or false) --- src/hash_set.zig | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/src/hash_set.zig b/src/hash_set.zig index 435651e..414ae9d 100644 --- a/src/hash_set.zig +++ b/src/hash_set.zig @@ -113,9 +113,8 @@ pub fn HashSetWithContext(comptime E: type, comptime Context: type, comptime max } pub fn add(self: *Self, allocator: Allocator, element: E) Allocator.Error!bool { - const prevCount = self.unmanaged.count(); - try self.unmanaged.put(allocator, element, {}); - return prevCount != self.unmanaged.count(); + const result = try self.unmanaged.getOrPut(allocator, element); + return !result.found_existing; } /// Adds a single element to the set. Asserts that there is enough capacity. @@ -1001,3 +1000,16 @@ test "custom hash function string usage" { _ = try A.add(testing.allocator, "Hello\t"); try expectEqual(7, A.cardinality()); } + +test "add boolean" { + var A: HashSet(u32) = .empty; + defer A.deinit(testing.allocator); + + const five_inserted = try A.add(testing.allocator, 5); + try expectEqual(five_inserted, true); + const six_inserted = try A.add(testing.allocator, 6); + try expectEqual(six_inserted, true); + + const five_again_inserted = try A.add(testing.allocator, 5); + try expectEqual(five_again_inserted, false); +} From a1d0f0061b9898ea85bea1221060759b5433185a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pau=20Soler=20Valad=C3=A9s?= Date: Tue, 25 Aug 2026 12:13:33 +0200 Subject: [PATCH 2/4] Changed add to use getOrPut in array. test added for bool and. Idk but it's commented in root --- src/array_hash_set.zig | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/src/array_hash_set.zig b/src/array_hash_set.zig index 59bc4b7..f1be890 100644 --- a/src/array_hash_set.zig +++ b/src/array_hash_set.zig @@ -94,9 +94,8 @@ pub fn ArraySet(comptime E: type) type { } pub fn add(self: *Self, allocator: Allocator, element: E) Allocator.Error!bool { - const prevCount = self.unmanaged.count(); - try self.unmanaged.put(allocator, element, {}); - return prevCount != self.unmanaged.count(); + const result = try self.unmanaged.getOrPut(allocator, element); + return !result.found_existing; } /// Appends all elements from the provided slice, and may allocate. @@ -830,3 +829,16 @@ test "sizeOf matches" { try expectEqual(expectedByteSize, @sizeOf(std.AutoArrayHashMapUnmanaged(u32, void))); try expectEqual(expectedByteSize, @sizeOf(ArraySet(u32))); } + +test "add boolean" { + var A: ArraySet(u32) = .empty; + defer A.deinit(testing.allocator); + + const five_inserted = try A.add(testing.allocator, 5); + try expectEqual(five_inserted, true); + const six_inserted = try A.add(testing.allocator, 6); + try expectEqual(six_inserted, true); + + const five_again_inserted = try A.add(testing.allocator, 5); + try expectEqual(five_again_inserted, false); +} From efc2ab6f9132068f0a2bed23817832665ec06e43 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pau=20Soler=20Valad=C3=A9s?= Date: Tue, 25 Aug 2026 12:35:33 +0200 Subject: [PATCH 3/4] Use getOrPutAssumeCapacity in addAssumeCapacity + test --- src/hash_set.zig | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/src/hash_set.zig b/src/hash_set.zig index 414ae9d..ea627f6 100644 --- a/src/hash_set.zig +++ b/src/hash_set.zig @@ -121,9 +121,8 @@ pub fn HashSetWithContext(comptime E: type, comptime Context: type, comptime max /// A bool is returned indicating if the element was actually added /// if not already known. pub fn addAssumeCapacity(self: *Self, element: E) bool { - const prevCount = self.unmanaged.count(); - self.unmanaged.putAssumeCapacity(element, {}); - return prevCount != self.unmanaged.count(); + const result = self.unmanaged.getOrPutAssumeCapacity(element); + return !result.found_existing; } /// Appends all elements from the provided set, and may allocate. @@ -1013,3 +1012,17 @@ test "add boolean" { const five_again_inserted = try A.add(testing.allocator, 5); try expectEqual(five_again_inserted, false); } + +test "addAssumeCapacity" { + var A: HashSet(u32) = try .initCapacity(testing.allocator, 3); + defer A.deinit(testing.allocator); + + try expect(A.addAssumeCapacity(1)); + try expect(A.addAssumeCapacity(2)); + try expect(A.addAssumeCapacity(3)); + + try expect(!A.addAssumeCapacity(1)); + + try expectEqual(3, A.cardinality()); + try expect(A.capacity() >= 3); +} From 79de452568a26532c35a7808a8dce19a68f8e00a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pau=20Soler=20Valad=C3=A9s?= Date: Tue, 25 Aug 2026 13:29:46 +0200 Subject: [PATCH 4/4] added assume capacity with getOrPut in arrayhash --- src/array_hash_set.zig | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/array_hash_set.zig b/src/array_hash_set.zig index f1be890..8cf906e 100644 --- a/src/array_hash_set.zig +++ b/src/array_hash_set.zig @@ -98,6 +98,14 @@ pub fn ArraySet(comptime E: type) type { return !result.found_existing; } + /// Adds a single element to the set. Asserts that there is enough capacity. + /// A bool is returned indicating if the element was actually added + /// if not already known. + pub fn addAssumeCapacity(self: *Self, element: E) bool { + const result = self.unmanaged.getOrPutAssumeCapacity(element); + return !result.found_existing; + } + /// Appends all elements from the provided slice, and may allocate. /// appendSlice returns an Allocator.Error or Size which represents how /// many elements added and not previously in the slice. @@ -842,3 +850,15 @@ test "add boolean" { const five_again_inserted = try A.add(testing.allocator, 5); try expectEqual(five_again_inserted, false); } + +test "addAssumeCapacity" { + var A = try ArraySet(u32).initCapacity(testing.allocator, 2); + defer A.deinit(testing.allocator); + + try expectEqual(true, A.addAssumeCapacity(5)); + try expectEqual(true, A.addAssumeCapacity(6)); + try expectEqual(false, A.addAssumeCapacity(5)); + try expectEqual(2, A.cardinality()); + try expect(A.contains(5)); + try expect(A.contains(6)); +}