@@ -298,6 +298,23 @@ pub fn ArraySetUnmanaged(comptime E: type) type {
298298 self .unmanaged = interSet .unmanaged ;
299299 }
300300
301+ /// isDisjoint returns true if the intersection between two sets is the null set.
302+ /// Otherwise returns false.
303+ pub fn isDisjoint (self : Self , other : Self ) bool {
304+ // Optimization: Find the smaller of the two, and iterate over the smaller set
305+ const smaller = if (self .cardinality () <= other .cardinality ()) self else other ;
306+ const larger = if (self .cardinality () <= other .cardinality ()) other else self ;
307+
308+ var iter = smaller .iterator ();
309+ while (iter .next ()) | el | {
310+ if (larger .contains (el .key_ptr .* )) {
311+ return false ;
312+ }
313+ }
314+ return true ;
315+ }
316+
317+ /// Returns true if this Set is empty otherwise false.
301318 pub fn isEmpty (self : Self ) bool {
302319 return self .unmanaged .count () == 0 ;
303320 }
@@ -614,6 +631,28 @@ test "comprehensive usage" {
614631 // supersetOf
615632}
616633
634+ test "isDisjoint" {
635+ var a = ArraySetUnmanaged (u32 ).init ();
636+ defer a .deinit (testing .allocator );
637+ _ = try a .appendSlice (testing .allocator , &.{ 20 , 30 , 40 });
638+
639+ var b = ArraySetUnmanaged (u32 ).init ();
640+ defer b .deinit (testing .allocator );
641+ _ = try b .appendSlice (testing .allocator , &.{ 202 , 303 , 403 });
642+
643+ // Test the true case.
644+ try expect (a .isDisjoint (b ));
645+ try expect (b .isDisjoint (a ));
646+
647+ // Test the false case.
648+ var c = ArraySetUnmanaged (u32 ).init ();
649+ defer c .deinit (testing .allocator );
650+ _ = try c .appendSlice (testing .allocator , &.{ 20 , 30 , 400 });
651+
652+ try expect (! a .isDisjoint (c ));
653+ try expect (! c .isDisjoint (a ));
654+ }
655+
617656test "clone" {
618657
619658 // clone
0 commit comments