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 {
163163 self.mask = ~self.mask;
164164 }
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
166176 /// Performs a union of two bit sets, and stores the
167177 /// result in the first one. Bits in the result are
168178 /// set if the corresponding bits were set in either input.
......@@ -521,6 +531,23 @@ pub fn Array(comptime MaskIntType: type, comptime size: usize) type {
521531 }
522532 }
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
524551 /// Performs a union of two bit sets, and stores the
525552 /// result in the first one. Bits in the result are
526553 /// set if the corresponding bits were set in either input.
......@@ -896,8 +923,13 @@ pub const Dynamic = struct {
896923
897924 /// Set all bits to 1.
898925 pub fn setAll(self: *Self) void {
899 const masks_len = numMasks(self.bit_length);
900 @memset(self.masks[0..masks_len], std.math.maxInt(MaskInt));
926 const num_masks = numMasks(self.bit_length);
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 }
901933 }
902934
903935 /// Flips a specific bit in the bit set
......@@ -1207,6 +1239,16 @@ pub const DynamicManaged = struct {
12071239 self.unmanaged.setIntersection(other.unmanaged);
12081240 }
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
12101252 /// Finds the index of the first set bit.
12111253 /// If no bits are set, returns null.
12121254 pub fn findFirstSet(self: Self) ?usize {
......@@ -1625,6 +1667,12 @@ fn testBitSet(a: anytype, b: anytype, len: usize) !void {
16251667 try testing.expect(!a.isSet(len - 1));
16261668 }
16271669 }
1670
1671 a.unsetAll();
1672 try testing.expectEqual(0, a.count());
1673
1674 a.setAll();
1675 try testing.expectEqual(len, a.count());
16281676}
16291677
16301678fn fillEven(set: anytype, len: usize) void {