struct DynamicBitSet [src]
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.
Fields
allocator: Allocator
unmanaged: DynamicBitSetUnmanaged = .{}
Members
- capacity (Function)
- clone (Function)
- count (Function)
- deinit (Function)
- eql (Function)
- findFirstSet (Function)
- findLastSet (Function)
- initEmpty (Function)
- initFull (Function)
- isSet (Function)
- iterator (Function)
- Iterator (Type Function)
- MaskInt (Type)
- resize (Function)
- set (Function)
- setIntersection (Function)
- setRangeValue (Function)
- setUnion (Function)
- setValue (Function)
- ShiftInt (Type)
- toggle (Function)
- toggleAll (Function)
- toggleFirstSet (Function)
- toggleSet (Function)
- unset (Function)
Source
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 last set bit.
/// If no bits are set, returns null.
pub fn findLastSet(self: Self) ?usize {
return self.unmanaged.findLastSet();
}
/// 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;
}