bitset

std.collections.bitset

rec Bitset

pub rec Bitset;

在 u64 字数组中存储 nbits 个位的动态位集

alloc:用于后备存储的分配器 words:指向 u64 字数组的指针 nbits:位的数量 nwords:u64 字的数量

fun init

pub fun init(alloc: *allocator.Allocator, nbits: usize) Result[Bitset, str];

创建一个所有位都已清零的位集

alloc:用于后备存储的分配器 nbits:位的数量 ret:一个新的清零后的 Bitset,或一个错误

fun dnit

pub fun dnit(bs: *Bitset) bool;

释放后备字数组

ret:如果内存成功释放则为 true

fun set

pub fun set(bs: *Bitset, idx: usize);

设置给定索引处的位

idx:位索引

fun clear

pub fun clear(bs: *Bitset, idx: usize);

清除给定索引处的位

idx:位索引

fun get

pub fun get(bs: *Bitset, idx: usize) bool;

测试给定索引处的位

idx:位索引 ret:如果位置位则为 true,如果清零或超出范围则为 false

fun toggle

pub fun toggle(bs: *Bitset, idx: usize);

翻转给定索引处的位

idx:位索引

fun count

pub fun count(bs: *Bitset) usize;

计算置位(1)的数量

ret:置位计数(population count)

fun clear_all

pub fun clear_all(bs: *Bitset);

将所有位清零

fun set_all

pub fun set_all(bs: *Bitset);

将所有位置 1,同时对尾随字进行掩码处理