Skip to content

Commit

Permalink
Add std.DynamicBitSet.setAll & unsetAll
Browse files Browse the repository at this point in the history
  • Loading branch information
nurpax committed Dec 22, 2024
1 parent 77c63ac commit 595ec94
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions lib/std/bit_set.zig
Original file line number Diff line number Diff line change
Expand Up @@ -868,6 +868,7 @@ pub const DynamicBitSetUnmanaged = struct {
/// Set all bits to 1.
pub fn setAll(self: *Self) void {
const masks_len = numMasks(self.bit_length);
// TODO doesn't seem like this keeps the last bits zero??
@memset(self.masks[0..masks_len], std.math.maxInt(MaskInt));
}

Expand Down Expand Up @@ -1121,6 +1122,16 @@ pub const DynamicBitSet = struct {
self.unmanaged.unset(index);
}

/// Set all bits to 0.
pub fn unsetAll(self: *Self) void {
self.unmanaged.unsetAll();
}

/// Set all bits to 1.
pub fn setAll(self: *Self) void {
self.unmanaged.setAll();
}

/// Flips a specific bit in the bit set
pub fn toggle(self: *Self, index: usize) void {
self.unmanaged.toggle(index);
Expand Down Expand Up @@ -1759,6 +1770,11 @@ test DynamicBitSet {

try testEql(tmp, b, size);
try testBitSet(&a, &b, size);

b.unsetAll();
try testing.expectEqual(0, b.count());
b.setAll();
try testing.expectEqual(size, b.count());
}
}

Expand Down

0 comments on commit 595ec94

Please sign in to comment.