跳到主要内容

btag

简介

按位标记工具 迁移自 M11 heyc MaskUtil.gs, 参考自 M11 chendh3 MaskUtil.cs

btag

简介

btag 二值标志器 使用一个数字数组(本库称为掩码)表达位索引的激活(1)与关闭(0)状态

每个数字能表达32个索引的状态,

存储说明

[3]表示索引 0,1 为激活态  
["00000000000000000000000000000011"]
[2147483648] 表示索引 31 为激活态,其余皆为关闭态  
["10000000000000000000000000000000"]
[4294967295] 表示索引 0~31 全为激活态  
["11111111111111111111111111111111"]
[0, 1] 表示索引 32 位为激活态,其余皆为关闭态  
["00000000000000000000000000000000", "00000000000000000000000000000001"]

压缩0的概念

  用负数n 表示它本身是0,且它后面还有 |n|0  
如:
原始[0, 0, 0, 0, 0, 9] 压缩为 [-4, 9]
原始[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255] 压缩为 [1, -9, 255]
原始[1, 0, 0, 0, 0, 0, 0, 0, 0, 0] 压缩为 [1, -8];

组件接口

btag.gs

函数原型函数作用
bool is_on_nz(array mask, int idx)判断掩码(明确没有压缩0的)中某个索引是否激活
bool is_on(array zmask, int idx)判断掩码中某个索引是否激活
bool is_on_all_nz(array mask, array idxes)判断掩码(明确没有压缩0的)中某些索引是否全部激活
bool is_on_all(array zmask, array idxes)判断掩码中某些索引是否全部激活
bool is_on_any_nz(array mask, array idxes)判断掩码(明确没有压缩0的)中某些索引是否存在某个激活
bool is_on_any(array zmask, array idxes)判断掩码中某些索引是否存在某个激活
array get_on_ids_nz(array mask, array idxes)获取掩码(明确没有压缩0的)中指定索引中已激活的索引数组
array get_on_ids(array zmask, array idxes)获取掩码中指定索引中已激活的索引数组
void set_on_nz(array mask, int idx)设置掩码(明确没有压缩0的)中指定索引为激活状态
void set_on(array mask, int idx)设置掩码中指定索引为激活状态
void set_on_array_nz(array mask, array idxes)设置掩码(明确没有压缩0的)中指定索引数组为激活状态
void set_on_array(array mask, array idxes)设置掩码中指定索引数组为激活状态
void set_off_nz(array mask, int idx)设置掩码(明确没有压缩0的)中指定索引为关闭状态
void set_off(array mask, int idx)设置掩码中指定索引为关闭状态
void set_off_array_nz(array mask, array idxes)设置掩码(明确没有压缩0的)中指定索引数组为关闭状态
void set_off_array(array mask, array idxes)设置掩码中指定索引数组为关闭状态
bool zip_zeros(array mask)自引用压缩0,用负数n 表示它本身是0,且它后面还有
bool unzip_zeros(array mask)对经过0压缩处理的数组进行自引用解压缩
array decode_nz(array mask)将掩码(明确没有压缩0的)解码成01数组
array decode(array mask)将掩码(可能有压缩0)解码成01数组
array get_all_on_ids_nz(array mask)获取掩码(明确没有压缩0的)中所有已激活的索引数组
array get_all_on_ids(array mask)获取掩码(可能有压缩0)中所有已激活的索引数组

样例

import pkg.btag;
import pkg.gtest;

public void test()
{
// is_on
test_equal(btag.is_on([], 0), false, "空数组测试1");
test_equal(btag.is_on([], 1), false, "空数组测试2");
test_equal(btag.is_on([], 65537), false, "空数组测试3");
test_equal(btag.is_on([1], 0), true, "索引0激活");
test_equal(btag.is_on([2], 1), true, "索引1激活");
test_equal(btag.is_on([2, -9], 1), true, "带后压缩索引1激活");
test_equal(btag.is_on([0, 0, 1], 64), true, "无压缩索引64激活");
test_equal(btag.is_on([-1, 1], 64), true, "带前压缩索引64激活");

// is_on_all
test_equal(btag.is_on_all([3], [0, 1]), true, "数组全激活");
test_equal(btag.is_on_all([3], [0, 1, 2]), false, "数组未全激活");

// is_on_any
test_equal(btag.is_on_any([3], [0, 1, 2, 3]), true, "数组中有某个激活");
test_equal(btag.is_on_any([3], [2, 3]), false, "数组中有某个激活");

// set_on
array arr = [INT32_MAX];
btag.set_on(arr, 32);
test_equal(btag.is_on(arr, 32), true);

// set_off
arr = [INT32_MAX, 1];
btag.set_off(arr, 32);
test_equal(btag.is_on(arr, 32), false);

// zip_zeros
array az1 = [];
btag.zip_zeros(az1);
test_equal(az1.length(), 0, "压0, 空数组");

array az2 = [0, 0, 9];
btag.zip_zeros(az2);
test_equal(az2.length(), 2, "压0测试2");
test_equal(az2[0], -1, "压0测试2, 压缩了2个0");
test_equal(az2[1], 9, "压0测试2, 正数保持不变");

array az3 = [9, 0, 0, 0, 0, 0, 128];
btag.zip_zeros(az3);
test_equal(az3.length(), 3, "压0测试3");
test_equal(az3[0], 9, "压0测试3, 正数不变");
test_equal(az3[1], -4, "压0测试3, 压缩了5个0");
test_equal(az3[2], 128, "压0测试3, 正数不变");

// unzip_zeros
array auz1 = [];
btag.unzip_zeros(auz1);
test_equal(auz1.length(), 0, "解0, 空数组");

array auz2 = [-2];
btag.unzip_zeros(auz2);
test_equal(auz2.length(), 3, "解0, 3个0");
test_equal(auz2[0], 0, "解0, 3个0");
test_equal(auz2[1], 0, "解0, 3个0");
test_equal(auz2[2], 0, "解0, 3个0");

array auz3 = [256, -2, 1];
btag.unzip_zeros(auz3);
test_equal(auz3.length(), 5, "解0, 中间3个0");
test_equal(auz3[0], 256, "解0, 正数不变");
test_equal(auz3[1], 0, "解0, 中间3个0");
test_equal(auz3[2], 0, "解0, 中间3个0");
test_equal(auz3[3], 0, "解0, 中间3个0");
test_equal(auz3[4], 1, "解0, 正数不变");

// decode
test_equal(btag.decode([1]), [[1]], "没有压缩0的掩码");
test_equal(btag.decode([10]), [[1, 0, 1, 0]], "没有压缩0的掩码");
test_equal(btag.decode([10, -2, 1]), [[1, 0, 1, 0], [0], [0], [0], [1]], "有压缩0的掩码");

// get_all_on_ids
gtest.test_equal(btag.get_all_on_ids([1]), [0], "没有压缩0的掩码");
gtest.test_equal(btag.get_all_on_ids([10]), [1, 3], "没有压缩0的掩码");
gtest.test_equal(btag.get_all_on_ids([1, 1]), [0, 32], "没有压缩0的掩码");
gtest.test_equal(btag.get_all_on_ids([10, -2, 1]), [1, 3, 128], "有压缩0的掩码");
}

test();