authorgravatar for jjhellst@gmail.comJanne Hellsten <jjhellst@gmail.com> 2026-09-25 16:52:06+03:00
committergravatar for squeek502@hotmail.comRyan Liptak <squeek502@hotmail.com> 2026-09-26 05:07:07+02:00
log880d7add00fecbd043ff91c8f31a3d92265361cb
tree201340f1ed429c636068ba53c36473bf89845727
parent08e1138080b40027595798572be1aad2600019f2

std.bit_set: add setAll/unsetAll to all bitsets, fix padding bug in Dynamic.setAll

- Add imperative setAll/unsetAll methods to all bitset types. Previously setAll/unsetAll were declared only for Dynamic. - Fix a padding bug in Dynamic.setAll the last mask set to all ones. - Add tests for previously untested setAll, unsetAll

1 files changed, 50 insertions(+), 2 deletions(-)

lib/std/bit_set.zig+50-2
...@@ -163,6 +163,16 @@ pub fn Integer(comptime size: u16) type {...@@ -163,6 +163,16 @@ pub fn Integer(comptime size: u16) type {
163 self.mask = ~self.mask;163 self.mask = ~self.mask;
164 }164 }
165165
166 /// Set all bits to 0.
167 pub fn unsetAll(self: *Self) void {
168 self.mask = 0;
169 }
170
171 /// Set all bits to 1.
172 pub fn setAll(self: *Self) void {
173 self.mask = ~self.mask;
174 }
175
166 /// Performs a union of two bit sets, and stores the176 /// Performs a union of two bit sets, and stores the
167 /// result in the first one. Bits in the result are177 /// result in the first one. Bits in the result are
168 /// set if the corresponding bits were set in either input.178 /// set if the corresponding bits were set in either input.
...@@ -521,6 +531,23 @@ pub fn Array(comptime MaskIntType: type, comptime size: usize) type {...@@ -521,6 +531,23 @@ pub fn Array(comptime MaskIntType: type, comptime size: usize) type {
521 }531 }
522 }532 }
523533
534 /// Set all bits to 0.
535 pub fn unsetAll(self: *Self) void {
536 for (&self.masks) |*mask| {
537 mask.* = 0;
538 }
539 }
540
541 /// Set all bits to 1.
542 pub fn setAll(self: *Self) void {
543 for (&self.masks) |*mask| {
544 mask.* = ~@as(MaskInt, 0);
545 }
546 if (num_masks > 0) {
547 self.masks[num_masks - 1] &= last_item_mask;
548 }
549 }
550
524 /// Performs a union of two bit sets, and stores the551 /// Performs a union of two bit sets, and stores the
525 /// result in the first one. Bits in the result are552 /// result in the first one. Bits in the result are
526 /// set if the corresponding bits were set in either input.553 /// set if the corresponding bits were set in either input.
...@@ -896,8 +923,13 @@ pub const Dynamic = struct {...@@ -896,8 +923,13 @@ pub const Dynamic = struct {
896923
897 /// Set all bits to 1.924 /// Set all bits to 1.
898 pub fn setAll(self: *Self) void {925 pub fn setAll(self: *Self) void {
899 const masks_len = numMasks(self.bit_length);926 const num_masks = numMasks(self.bit_length);
900 @memset(self.masks[0..masks_len], std.math.maxInt(MaskInt));927 @memset(self.masks[0..num_masks], std.math.maxInt(MaskInt));
928 if (num_masks > 0) {
929 const padding_bits = num_masks * @bitSizeOf(MaskInt) - self.bit_length;
930 const last_item_mask = (~@as(MaskInt, 0)) >> @as(ShiftInt, @intCast(padding_bits));
931 self.masks[num_masks - 1] = last_item_mask;
932 }
901 }933 }
902934
903 /// Flips a specific bit in the bit set935 /// Flips a specific bit in the bit set
...@@ -1207,6 +1239,16 @@ pub const DynamicManaged = struct {...@@ -1207,6 +1239,16 @@ pub const DynamicManaged = struct {
1207 self.unmanaged.setIntersection(other.unmanaged);1239 self.unmanaged.setIntersection(other.unmanaged);
1208 }1240 }
12091241
1242 /// Set all bits to 0.
1243 pub fn unsetAll(self: *Self) void {
1244 self.unmanaged.unsetAll();
1245 }
1246
1247 /// Set all bits to 1.
1248 pub fn setAll(self: *Self) void {
1249 self.unmanaged.setAll();
1250 }
1251
1210 /// Finds the index of the first set bit.1252 /// Finds the index of the first set bit.
1211 /// If no bits are set, returns null.1253 /// If no bits are set, returns null.
1212 pub fn findFirstSet(self: Self) ?usize {1254 pub fn findFirstSet(self: Self) ?usize {
...@@ -1625,6 +1667,12 @@ fn testBitSet(a: anytype, b: anytype, len: usize) !void {...@@ -1625,6 +1667,12 @@ fn testBitSet(a: anytype, b: anytype, len: usize) !void {
1625 try testing.expect(!a.isSet(len - 1));1667 try testing.expect(!a.isSet(len - 1));
1626 }1668 }
1627 }1669 }
1670
1671 a.unsetAll();
1672 try testing.expectEqual(0, a.count());
1673
1674 a.setAll();
1675 try testing.expectEqual(len, a.count());
1628}1676}
16291677
1630fn fillEven(set: anytype, len: usize) void {1678fn fillEven(set: anytype, len: usize) void {