structstd.bit_set.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.

Types

TypeMaskInt[src]

The integer type used to represent a mask in this bit set

Source Code

Source code
pub const MaskInt = usize

TypeShiftInt[src]

The integer type used to shift a mask in this bit set

Source Code

Source code
pub const ShiftInt = std.math.Log2Int(MaskInt)

Type FunctionIterator[src]

Parameters

Source Code

Source code
pub fn Iterator(comptime options: IteratorOptions) type {
    return BitSetIterator(MaskInt, options);
}

Fields

allocator: Allocator
unmanaged: DynamicBitSetUnmanaged = .{}

Functions

FunctioninitEmpty[src]

pub fn initEmpty(allocator: Allocator, bit_length: usize) !Self

Creates a bit set with no elements present.

Parameters

allocator: Allocator
bit_length: usize

Source Code

Source code
pub fn initEmpty(allocator: Allocator, bit_length: usize) !Self {
    return Self{
        .unmanaged = try DynamicBitSetUnmanaged.initEmpty(allocator, bit_length),
        .allocator = allocator,
    };
}

FunctioninitFull[src]

pub fn initFull(allocator: Allocator, bit_length: usize) !Self

Creates a bit set with all elements present.

Parameters

allocator: Allocator
bit_length: usize

Source Code

Source code
pub fn initFull(allocator: Allocator, bit_length: usize) !Self {
    return Self{
        .unmanaged = try DynamicBitSetUnmanaged.initFull(allocator, bit_length),
        .allocator = allocator,
    };
}

Functionresize[src]

pub fn resize(self: *@This(), new_len: usize, fill: bool) !void

Resizes to a new length. If the new length is larger than the old length, fills any added bits with fill.

Parameters

self: *@This()
new_len: usize
fill: bool

Source Code

Source code
pub fn resize(self: *@This(), new_len: usize, fill: bool) !void {
    try self.unmanaged.resize(self.allocator, new_len, fill);
}

Functiondeinit[src]

pub fn deinit(self: *Self) void

Deinitializes the array and releases its memory. The passed allocator must be the same one used for init* or resize in the past.

Parameters

self: *Self

Source Code

Source code
pub fn deinit(self: *Self) void {
    self.unmanaged.deinit(self.allocator);
}

Functionclone[src]

pub fn clone(self: *const Self, new_allocator: Allocator) !Self

Creates a duplicate of this bit set, using the new allocator.

Parameters

self: *const Self
new_allocator: Allocator

Source Code

Source code
pub fn clone(self: *const Self, new_allocator: Allocator) !Self {
    return Self{
        .unmanaged = try self.unmanaged.clone(new_allocator),
        .allocator = new_allocator,
    };
}

Functioncapacity[src]

pub inline fn capacity(self: Self) usize

Returns the number of bits in this bit set

Parameters

self: Self

Source Code

Source code
pub inline fn capacity(self: Self) usize {
    return self.unmanaged.capacity();
}

FunctionisSet[src]

pub fn isSet(self: Self, index: usize) bool

Returns true if the bit at the specified index is present in the set, false otherwise.

Parameters

self: Self
index: usize

Source Code

Source code
pub fn isSet(self: Self, index: usize) bool {
    return self.unmanaged.isSet(index);
}

Functioncount[src]

pub fn count(self: Self) usize

Returns the total number of set bits in this bit set.

Parameters

self: Self

Source Code

Source code
pub fn count(self: Self) usize {
    return self.unmanaged.count();
}

FunctionsetValue[src]

pub fn setValue(self: *Self, index: usize, value: bool) void

Changes the value of the specified bit of the bit set to match the passed boolean.

Parameters

self: *Self
index: usize
value: bool

Source Code

Source code
pub fn setValue(self: *Self, index: usize, value: bool) void {
    self.unmanaged.setValue(index, value);
}

Functionset[src]

pub fn set(self: *Self, index: usize) void

Adds a specific bit to the bit set

Parameters

self: *Self
index: usize

Source Code

Source code
pub fn set(self: *Self, index: usize) void {
    self.unmanaged.set(index);
}

FunctionsetRangeValue[src]

pub fn setRangeValue(self: *Self, range: Range, value: bool) void

Changes the value of all bits in the specified range to match the passed boolean.

Parameters

self: *Self
range: Range
value: bool

Source Code

Source code
pub fn setRangeValue(self: *Self, range: Range, value: bool) void {
    self.unmanaged.setRangeValue(range, value);
}

Functionunset[src]

pub fn unset(self: *Self, index: usize) void

Removes a specific bit from the bit set

Parameters

self: *Self
index: usize

Source Code

Source code
pub fn unset(self: *Self, index: usize) void {
    self.unmanaged.unset(index);
}

Functiontoggle[src]

pub fn toggle(self: *Self, index: usize) void

Flips a specific bit in the bit set

Parameters

self: *Self
index: usize

Source Code

Source code
pub fn toggle(self: *Self, index: usize) void {
    self.unmanaged.toggle(index);
}

FunctiontoggleSet[src]

pub fn toggleSet(self: *Self, toggles: Self) void

Flips all bits in this bit set which are present in the toggles bit set. Both sets must have the same bit_length.

Parameters

self: *Self
toggles: Self

Source Code

Source code
pub fn toggleSet(self: *Self, toggles: Self) void {
    self.unmanaged.toggleSet(toggles.unmanaged);
}

FunctiontoggleAll[src]

pub fn toggleAll(self: *Self) void

Flips every bit in the bit set.

Parameters

self: *Self

Source Code

Source code
pub fn toggleAll(self: *Self) void {
    self.unmanaged.toggleAll();
}

FunctionsetUnion[src]

pub fn setUnion(self: *Self, other: Self) void

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.

Parameters

self: *Self
other: Self

Source Code

Source code
pub fn setUnion(self: *Self, other: Self) void {
    self.unmanaged.setUnion(other.unmanaged);
}

FunctionsetIntersection[src]

pub fn setIntersection(self: *Self, other: Self) void

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.

Parameters

self: *Self
other: Self

Source Code

Source code
pub fn setIntersection(self: *Self, other: Self) void {
    self.unmanaged.setIntersection(other.unmanaged);
}

FunctionfindFirstSet[src]

pub fn findFirstSet(self: Self) ?usize

Finds the index of the first set bit. If no bits are set, returns null.

Parameters

self: Self

Source Code

Source code
pub fn findFirstSet(self: Self) ?usize {
    return self.unmanaged.findFirstSet();
}

FunctiontoggleFirstSet[src]

pub fn toggleFirstSet(self: *Self) ?usize

Finds the index of the first set bit, and unsets it. If no bits are set, returns null.

Parameters

self: *Self

Source Code

Source code
pub fn toggleFirstSet(self: *Self) ?usize {
    return self.unmanaged.toggleFirstSet();
}

Functioneql[src]

pub fn eql(self: Self, other: Self) bool

Returns true iff every corresponding bit in both bit sets are the same.

Parameters

self: Self
other: Self

Source Code

Source code
pub fn eql(self: Self, other: Self) bool {
    return self.unmanaged.eql(other.unmanaged);
}

Functioniterator[src]

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.

Parameters

self: *const Self

Source Code

Source code
pub fn iterator(self: *const Self, comptime options: IteratorOptions) Iterator(options) {
    return self.unmanaged.iterator(options);
}

Source Code

Source code
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;
}