| ... | @@ -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 | } |
| 165 | | 165 | |
| | 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 the | 176 | /// Performs a union of two bit sets, and stores the |
| 167 | /// result in the first one. Bits in the result are | 177 | /// 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 | } |
| 523 | | 533 | |
| | 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 the | 551 | /// Performs a union of two bit sets, and stores the |
| 525 | /// result in the first one. Bits in the result are | 552 | /// 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 { |
| 896 | | 923 | |
| 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 | } |
| 902 | | 934 | |
| 903 | /// Flips a specific bit in the bit set | 935 | /// 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 | } |
| 1209 | | 1241 | |
| | 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 | } |
| 1629 | | 1677 | |
| 1630 | fn fillEven(set: anytype, len: usize) void { | 1678 | fn fillEven(set: anytype, len: usize) void { |