DoxigAlpha

setRangeValue

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

Function parameters

Parameters

#
self:*Self
value:bool

A bit set with runtime-known size, backed by an allocated slice

Types

#
DynamicBitSetUnmanaged
A bit set with runtime-known size, backed by an allocated slice
DynamicBitSet
A bit set with runtime-known size, backed by an allocated slice
IteratorOptions
Options for configuring an iterator over a bit set
Range
A range of indices within a bitset.

Returns the optimal static bit set type for the specified number

Functions

#
StaticBitSet
Returns the optimal static bit set type for the specified number
IntegerBitSet
A bit set with static size, which is backed by a single integer.
ArrayBitSet
A bit set with static size, which is backed by an array of usize.

Source

Implementation

#
pub fn setRangeValue(self: *Self, range: Range, value: bool) void {
    assert(range.end <= bit_length);
    assert(range.start <= range.end);
    if (range.start == range.end) return;
    if (num_masks == 0) return;

    const start_mask_index = maskIndex(range.start);
    const start_bit = @as(ShiftInt, @truncate(range.start));

    const end_mask_index = maskIndex(range.end);
    const end_bit = @as(ShiftInt, @truncate(range.end));

    if (start_mask_index == end_mask_index) {
        var mask1 = std.math.boolMask(MaskInt, true) << start_bit;
        var mask2 = std.math.boolMask(MaskInt, true) >> (mask_len - 1) - (end_bit - 1);
        self.masks[start_mask_index] &= ~(mask1 & mask2);

        mask1 = std.math.boolMask(MaskInt, value) << start_bit;
        mask2 = std.math.boolMask(MaskInt, value) >> (mask_len - 1) - (end_bit - 1);
        self.masks[start_mask_index] |= mask1 & mask2;
    } else {
        var bulk_mask_index: usize = undefined;
        if (start_bit > 0) {
            self.masks[start_mask_index] =
                (self.masks[start_mask_index] & ~(std.math.boolMask(MaskInt, true) << start_bit)) |
                (std.math.boolMask(MaskInt, value) << start_bit);
            bulk_mask_index = start_mask_index + 1;
        } else {
            bulk_mask_index = start_mask_index;
        }

        while (bulk_mask_index < end_mask_index) : (bulk_mask_index += 1) {
            self.masks[bulk_mask_index] = std.math.boolMask(MaskInt, value);
        }

        if (end_bit > 0) {
            self.masks[end_mask_index] =
                (self.masks[end_mask_index] & (std.math.boolMask(MaskInt, true) << end_bit)) |
                (std.math.boolMask(MaskInt, value) >> ((@bitSizeOf(MaskInt) - 1) - (end_bit - 1)));
        }
    }
}