Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add std.DynamicBitSet.setAll & unsetAll #22289

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions lib/std/bit_set.zig
Original file line number Diff line number Diff line change
Expand Up @@ -867,8 +867,13 @@ pub const DynamicBitSetUnmanaged = struct {

/// Set all bits to 1.
pub fn setAll(self: *Self) void {
const masks_len = numMasks(self.bit_length);
@memset(self.masks[0..masks_len], std.math.maxInt(MaskInt));
const num_masks = numMasks(self.bit_length);
@memset(self.masks[0..num_masks], std.math.maxInt(MaskInt));
if (num_masks > 0) {
const padding_bits = num_masks * @bitSizeOf(MaskInt) - self.bit_length;
const last_item_mask = (~@as(MaskInt, 0)) >> @as(ShiftInt, @intCast(padding_bits));
self.masks[num_masks - 1] = last_item_mask;
}
}

/// Flips a specific bit in the bit set
Expand Down Expand Up @@ -1121,6 +1126,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 +1774,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
Loading