A bit set with runtime-known size, backed by an allocated slice of usize. Thin wrapper around DynamicBitSetUnmanaged which keeps track of the allocator instance.
The integer type used to represent a mask in this bit set
pub const MaskInt = usizeThe integer type used to shift a mask in this bit set
options: IteratorOptionspub fn Iterator(comptime options: IteratorOptions) type {
return BitSetIterator(MaskInt, options);
}allocator: Allocatorunmanaged: DynamicBitSetUnmanaged = .{}Creates a bit set with no elements present.
allocator: Allocatorbit_length: usizepub fn initEmpty(allocator: Allocator, bit_length: usize) !Self {
return Self{
.unmanaged = try DynamicBitSetUnmanaged.initEmpty(allocator, bit_length),
.allocator = allocator,
};
}Creates a bit set with all elements present.
allocator: Allocatorbit_length: usizepub fn initFull(allocator: Allocator, bit_length: usize) !Self {
return Self{
.unmanaged = try DynamicBitSetUnmanaged.initFull(allocator, bit_length),
.allocator = allocator,
};
}pub fn resize(self: *@This(), new_len: usize, fill: bool) !voidResizes to a new length. If the new length is larger
than the old length, fills any added bits with fill.
self: *@This()new_len: usizefill: boolpub fn resize(self: *@This(), new_len: usize, fill: bool) !void {
try self.unmanaged.resize(self.allocator, new_len, fill);
}pub fn deinit(self: *Self) voidDeinitializes the array and releases its memory. The passed allocator must be the same one used for init* or resize in the past.
self: *Selfpub fn deinit(self: *Self) void {
self.unmanaged.deinit(self.allocator);
}Creates a duplicate of this bit set, using the new allocator.
pub inline fn capacity(self: Self) usizeReturns the number of bits in this bit set
self: Selfpub inline fn capacity(self: Self) usize {
return self.unmanaged.capacity();
}pub fn isSet(self: Self, index: usize) boolReturns true if the bit at the specified index is present in the set, false otherwise.
self: Selfindex: usizepub fn isSet(self: Self, index: usize) bool {
return self.unmanaged.isSet(index);
}pub fn count(self: Self) usizeReturns the total number of set bits in this bit set.
self: Selfpub fn count(self: Self) usize {
return self.unmanaged.count();
}pub fn setValue(self: *Self, index: usize, value: bool) voidChanges the value of the specified bit of the bit set to match the passed boolean.
pub fn setValue(self: *Self, index: usize, value: bool) void {
self.unmanaged.setValue(index, value);
}pub fn set(self: *Self, index: usize) voidAdds a specific bit to the bit set
self: *Selfindex: usizepub fn set(self: *Self, index: usize) void {
self.unmanaged.set(index);
}Changes the value of all bits in the specified range to match the passed boolean.
pub fn unset(self: *Self, index: usize) voidRemoves a specific bit from the bit set
self: *Selfindex: usizepub fn unset(self: *Self, index: usize) void {
self.unmanaged.unset(index);
}pub fn toggle(self: *Self, index: usize) voidFlips a specific bit in the bit set
self: *Selfindex: usizepub fn toggle(self: *Self, index: usize) void {
self.unmanaged.toggle(index);
}Flips all bits in this bit set which are present in the toggles bit set. Both sets must have the same bit_length.
pub fn toggleAll(self: *Self) voidFlips every bit in the bit set.
self: *Selfpub fn toggleAll(self: *Self) void {
self.unmanaged.toggleAll();
}Performs a union of two bit sets, and stores the result in the first one. Bits in the result are set if the corresponding bits were set in either input. The two sets must both be the same bit_length.
Performs an intersection of two bit sets, and stores the result in the first one. Bits in the result are set if the corresponding bits were set in both inputs. The two sets must both be the same bit_length.
pub fn findFirstSet(self: Self) ?usizeFinds the index of the first set bit. If no bits are set, returns null.
self: Selfpub fn findFirstSet(self: Self) ?usize {
return self.unmanaged.findFirstSet();
}pub fn toggleFirstSet(self: *Self) ?usizeFinds the index of the first set bit, and unsets it. If no bits are set, returns null.
self: *Selfpub fn toggleFirstSet(self: *Self) ?usize {
return self.unmanaged.toggleFirstSet();
}Returns true iff every corresponding bit in both bit sets are the same.
pub fn iterator(self: *const Self, comptime options: IteratorOptions) Iterator(options)Iterates through the items in the set, according to the options. The default options (.{}) will iterate indices of set bits in ascending order. Modifications to the underlying bit set may or may not be observed by the iterator. Resizing the underlying bit set invalidates the iterator.
self: *const Selfoptions: IteratorOptionspub fn iterator(self: *const Self, comptime options: IteratorOptions) Iterator(options) {
return self.unmanaged.iterator(options);
}pub const DynamicBitSet = struct {
const Self = @This();
/// The integer type used to represent a mask in this bit set
pub const MaskInt = usize;
/// The integer type used to shift a mask in this bit set
pub const ShiftInt = std.math.Log2Int(MaskInt);
allocator: Allocator,
unmanaged: DynamicBitSetUnmanaged = .{},
/// Creates a bit set with no elements present.
pub fn initEmpty(allocator: Allocator, bit_length: usize) !Self {
return Self{
.unmanaged = try DynamicBitSetUnmanaged.initEmpty(allocator, bit_length),
.allocator = allocator,
};
}
/// Creates a bit set with all elements present.
pub fn initFull(allocator: Allocator, bit_length: usize) !Self {
return Self{
.unmanaged = try DynamicBitSetUnmanaged.initFull(allocator, bit_length),
.allocator = allocator,
};
}
/// Resizes to a new length. If the new length is larger
/// than the old length, fills any added bits with `fill`.
pub fn resize(self: *@This(), new_len: usize, fill: bool) !void {
try self.unmanaged.resize(self.allocator, new_len, fill);
}
/// Deinitializes the array and releases its memory.
/// The passed allocator must be the same one used for
/// init* or resize in the past.
pub fn deinit(self: *Self) void {
self.unmanaged.deinit(self.allocator);
}
/// Creates a duplicate of this bit set, using the new allocator.
pub fn clone(self: *const Self, new_allocator: Allocator) !Self {
return Self{
.unmanaged = try self.unmanaged.clone(new_allocator),
.allocator = new_allocator,
};
}
/// Returns the number of bits in this bit set
pub inline fn capacity(self: Self) usize {
return self.unmanaged.capacity();
}
/// Returns true if the bit at the specified index
/// is present in the set, false otherwise.
pub fn isSet(self: Self, index: usize) bool {
return self.unmanaged.isSet(index);
}
/// Returns the total number of set bits in this bit set.
pub fn count(self: Self) usize {
return self.unmanaged.count();
}
/// Changes the value of the specified bit of the bit
/// set to match the passed boolean.
pub fn setValue(self: *Self, index: usize, value: bool) void {
self.unmanaged.setValue(index, value);
}
/// Adds a specific bit to the bit set
pub fn set(self: *Self, index: usize) void {
self.unmanaged.set(index);
}
/// Changes the value of all bits in the specified range to
/// match the passed boolean.
pub fn setRangeValue(self: *Self, range: Range, value: bool) void {
self.unmanaged.setRangeValue(range, value);
}
/// Removes a specific bit from the bit set
pub fn unset(self: *Self, index: usize) void {
self.unmanaged.unset(index);
}
/// Flips a specific bit in the bit set
pub fn toggle(self: *Self, index: usize) void {
self.unmanaged.toggle(index);
}
/// Flips all bits in this bit set which are present
/// in the toggles bit set. Both sets must have the
/// same bit_length.
pub fn toggleSet(self: *Self, toggles: Self) void {
self.unmanaged.toggleSet(toggles.unmanaged);
}
/// Flips every bit in the bit set.
pub fn toggleAll(self: *Self) void {
self.unmanaged.toggleAll();
}
/// Performs a union of two bit sets, and stores the
/// result in the first one. Bits in the result are
/// set if the corresponding bits were set in either input.
/// The two sets must both be the same bit_length.
pub fn setUnion(self: *Self, other: Self) void {
self.unmanaged.setUnion(other.unmanaged);
}
/// Performs an intersection of two bit sets, and stores
/// the result in the first one. Bits in the result are
/// set if the corresponding bits were set in both inputs.
/// The two sets must both be the same bit_length.
pub fn setIntersection(self: *Self, other: Self) void {
self.unmanaged.setIntersection(other.unmanaged);
}
/// Finds the index of the first set bit.
/// If no bits are set, returns null.
pub fn findFirstSet(self: Self) ?usize {
return self.unmanaged.findFirstSet();
}
/// Finds the index of the first set bit, and unsets it.
/// If no bits are set, returns null.
pub fn toggleFirstSet(self: *Self) ?usize {
return self.unmanaged.toggleFirstSet();
}
/// Returns true iff every corresponding bit in both
/// bit sets are the same.
pub fn eql(self: Self, other: Self) bool {
return self.unmanaged.eql(other.unmanaged);
}
/// Iterates through the items in the set, according to the options.
/// The default options (.{}) will iterate indices of set bits in
/// ascending order. Modifications to the underlying bit set may
/// or may not be observed by the iterator. Resizing the underlying
/// bit set invalidates the iterator.
pub fn iterator(self: *const Self, comptime options: IteratorOptions) Iterator(options) {
return self.unmanaged.iterator(options);
}
pub const Iterator = DynamicBitSetUnmanaged.Iterator;
}