nd-bitset
v1.0.11
Published
nd-bitset ======================= - simple wrap of std::bitset - only work in nodejs - must be 64 * Integer (IF <= 32, directly use a JS integer is BEST) - suitable for bigsize bit-pool (suggested is 1G)
Downloads
10
Readme
nd-bitset
- simple wrap of std::bitset
- only work in nodejs
- must be 64 * Integer (IF <= 32, directly use a JS integer is BEST)
- suitable for bigsize bit-pool (suggested is 1G)
install
- npm install nd-bitset
splitted
usage
const x = require("nd-bitset");
example
creat new
> var bits = x.creat_bits_1g()
> bits[99999] =true
true
> bits
{
is_any_fls: true,
is_any_tru: true,
are_all_fls: false,
are_all_tru: false,
count_of_0: 1073741823,
count_of_1: 1,
bit_capacity: 1073741824
}
> bits[99999]
true
> bits[1073741823] = true
true
> bits
{
is_any_fls: true,
is_any_tru: true,
are_all_fls: false,
are_all_tru: false,
count_of_0: 1073741822,
count_of_1: 2,
bit_capacity: 1073741824
}
> x.flip_all(bits)
> bits
{
is_any_fls: true,
is_any_tru: true,
are_all_fls: false,
are_all_tru: false,
count_of_0: 2,
count_of_1: 1073741822,
bit_capacity: 1073741824
}
> x.free_bits(bits) // must free.
/* if use it after free, will crash*/
creat on existing buffer
> var ab = new ArrayBuffer(8)
> ab
ArrayBuffer {
[Uint8Contents]: <00 00 00 00 00 00 00 00>,
byteLength: 8
}
> var bits = x.creat_bits_64(ab)
> ab
ArrayBuffer {
[Uint8Contents]: <00 00 00 00 00 00 00 00>,
byteLength: 8
}
> bits
{
is_any_fls: true,
is_any_tru: false,
are_all_fls: true,
are_all_tru: false,
count_of_0: 64,
count_of_1: 0,
bit_capacity: 64
}
> bits[5] = true
true
> ab
ArrayBuffer {
[Uint8Contents]: <20 00 00 00 00 00 00 00>,
byteLength: 8
}
> x.flip_all(bits)
> ab
ArrayBuffer {
[Uint8Contents]: <df ff ff ff ff ff ff ff>,
byteLength: 8
}
>
push /pop (as a circular buffer)
push_one: (bits, v=true)->Void
> var bits = x.creat_bits_1g()
> bits
{
is_any_fls: true,
is_any_tru: false,
are_all_fls: true,
are_all_tru: false,
count_of_0: 1073741824,
count_of_1: 0,
bit_capacity: 1073741824
}
> x.push_one(bits,1)
> bits
{
is_any_fls: true,
is_any_tru: true,
are_all_fls: false,
are_all_tru: false,
count_of_0: 1073741823,
count_of_1: 1,
bit_capacity: 1073741824
}
> bits[1073741823]
true
> bits[1073741822]
false
>
pop_one: (bits)->boolean
> var bits = x.creat_bits_64()
> bits
{
is_any_fls: true,
is_any_tru: false,
are_all_fls: true,
are_all_tru: false,
count_of_0: 64,
count_of_1: 0,
bit_capacity: 64
}
> x.show64(bits)
[ '0000000000000000000000000000000000000000000000000000000000000000' ]
> x.push_one(bits,1)
> x.show64(bits)
[ '1000000000000000000000000000000000000000000000000000000000000000' ]
> bits[0]
false
> bits[63]
true
> x.push_one(bits,1)
> x.show64(bits)
[ '1100000000000000000000000000000000000000000000000000000000000000' ]
// bit(63) ...-> bit(0)
> bits[63]
true
> bits[62]
true
> x.push_one(bits,1)
> x.show64(bits)
[ '1110000000000000000000000000000000000000000000000000000000000000' ]
> x.pop_one(bits)
true
> x.show64(bits)
[ '1100000000000000000000000000000000000000000000000000000000000000' ]
> x.pop_one(bits)
true
> x.show64(bits)
[ '1000000000000000000000000000000000000000000000000000000000000000' ]
> x.pop_one(bits)
true
> x.show64(bits)
[ '0000000000000000000000000000000000000000000000000000000000000000' ]
> bits[0]=true
true
> x.show64(bits)
[ '0000000000000000000000000000000000000000000000000000000000000001' ]
> bits[1]=true
true
> x.show64(bits)
[ '0000000000000000000000000000000000000000000000000000000000000011' ]
> x.lshft_inplace(bits,2)
> x.show64(bits)
[ '0000000000000000000000000000000000000000000000000000000000001100' ]
> x.rshft_inplace(bits,2)
> x.show64(bits)
[ '0000000000000000000000000000000000000000000000000000000000000011' ]
>
x.free_bits(bits)
call as function (range :64-bit-segment)
// can call-as function : (start-64bit-unit-idx, end-64bit-unit-idx, value)
> bits(0,1,0b0000000000000000000000000000000000000000000000000000000000000000n)
> x.show64(bits)
[
'0000000000000000000000000000000000000000000000000000000000000000',
'1111111111111111111111111111111111111111111111111111111111111111'
]
> bits(0,1,3)
> x.show64(bits)
[
'0000000000000000000000000000000000000000000000000000000000000011',
'1111111111111111111111111111111111111111111111111111111111111111'
]
>
simple stringify (just for debug, its slow)
(DO-NOT use this on big-size-bits-larger-than-128M(such as 512M 1G), its very slow, coz js-string is very slow)
var bits = x.creat_bits_128();
x.set_all_to_tru(bits)
> var bits = x.creat_bits_128()
> x.show64(bits)
[
'0000000000000000000000000000000000000000000000000000000000000000',
'0000000000000000000000000000000000000000000000000000000000000000'
]
> x.set_all_to_tru(bits)
> x.show64(bits)
[
'1111111111111111111111111111111111111111111111111111111111111111',
'1111111111111111111111111111111111111111111111111111111111111111'
]
>
x.free_bits(bits)
> var bits = x.creat_bits_128()
> x.show64(bits)
[
'0000000000000000000000000000000000000000000000000000000000000000',
'0000000000000000000000000000000000000000000000000000000000000000'
]
> bits[0]=1
1
> x.show64(bits)
[
'0000000000000000000000000000000000000000000000000000000000000001',
'0000000000000000000000000000000000000000000000000000000000000000'
]
> bits[127]=1
1
> x.show64(bits)
[
'0000000000000000000000000000000000000000000000000000000000000001',
'1000000000000000000000000000000000000000000000000000000000000000'
]
> x.free_bits(bits)
&= |= ^= inplace
var bits0 = x.creat_bits_64()
var bits1 = x.creat_bits_64()
bits0[0]=1
bits1[63]=1
> x.show64(bits0)
[ '0000000000000000000000000000000000000000000000000000000000000001' ]
> x.show64(bits1)
[ '1000000000000000000000000000000000000000000000000000000000000000' ]
>
x.bor_inplace(bits0,bits1) // bits0 = bits0 | bits1
> x.show64(bits0)
[ '1000000000000000000000000000000000000000000000000000000000000001' ]
>
> x.show64(bits1)
[ '1000000000000000000000000000000000000000000000000000000000000000' ]
>
> x.bxor_inplace(bits1,bits0)
> x.show64(bits1)
[ '0000000000000000000000000000000000000000000000000000000000000001' ]
> x.show64(bits0)
[ '1000000000000000000000000000000000000000000000000000000000000001' ]
>
& | ^ NOT-inplace, will creat new
> var bits0 = x.creat_bits_64();
> var bits1 = x.creat_bits_64();
> bits0
{
is_any_fls: true,
is_any_tru: false,
are_all_fls: true,
are_all_tru: false,
count_of_0: 64,
count_of_1: 0,
bit_capacity: 64
}
> x.flip_all(bits1)
> x.show64(bits0)
[ '0000000000000000000000000000000000000000000000000000000000000000' ]
> x.show64(bits1)
[ '1111111111111111111111111111111111111111111111111111111111111111' ]
> var bits2 = x.bor(bits0,bits1)
> bits2
{
is_any_fls: false,
is_any_tru: true,
are_all_fls: false,
are_all_tru: true,
count_of_0: 0,
count_of_1: 64,
bit_capacity: 64
}
> x.show64(bits2)
[ '1111111111111111111111111111111111111111111111111111111111111111' ]
> bits0[25] = 1
1
> x.show64(bits2)
[ '1111111111111111111111111111111111111111111111111111111111111111' ]
> x.show64(bits0)
[ '0000000000000000000000000000000000000010000000000000000000000000' ]
> x.show64(bits1)
[ '1111111111111111111111111111111111111111111111111111111111111111' ]
> bits1[26] = 1
1
> x.show64(bits1)
[ '1111111111111111111111111111111111111111111111111111111111111111' ]
> bits1[26] = 0
0
> x.show64(bits1)
[ '1111111111111111111111111111111111111011111111111111111111111111' ]
> x.show64(bits2)
[ '1111111111111111111111111111111111111111111111111111111111111111' ]
>
METHODS
APIS
{
creat_bits_64: [Function (anonymous)],
creat_bits_128: [Function (anonymous)],
creat_bits_256: [Function (anonymous)],
creat_bits_512: [Function (anonymous)],
creat_bits_1k: [Function (anonymous)],
creat_bits_2k: [Function (anonymous)],
creat_bits_4k: [Function (anonymous)],
creat_bits_8k: [Function (anonymous)],
creat_bits_16k: [Function (anonymous)],
creat_bits_32k: [Function (anonymous)],
creat_bits_64k: [Function (anonymous)],
creat_bits_128k: [Function (anonymous)],
creat_bits_256k: [Function (anonymous)],
creat_bits_512k: [Function (anonymous)],
creat_bits_1m: [Function (anonymous)],
creat_bits_2m: [Function (anonymous)],
creat_bits_4m: [Function (anonymous)],
creat_bits_8m: [Function (anonymous)],
creat_bits_16m: [Function (anonymous)],
creat_bits_32m: [Function (anonymous)],
creat_bits_64m: [Function (anonymous)],
creat_bits_128m: [Function (anonymous)],
creat_bits_256m: [Function (anonymous)],
creat_bits_512m: [Function (anonymous)],
creat_bits_1g: [Function (anonymous)],
creat_bits_2g: [Function (anonymous)],
creat_bits_4g: [Function (anonymous)],
creat_bits_8g: [Function (anonymous)],
creat_bits_16g: [Function (anonymous)],
creat_bits_32g: [Function (anonymous)],
creat_bits_64g: [Function (anonymous)],
creat_bits_128g: [Function (anonymous)],
set_all_to_tru: [Function (anonymous)],
set_all_to_fls: [Function (anonymous)],
flip_all: [Function (anonymous)],
clone: [Function (anonymous)],
free_bits: [Function (anonymous)]
}
LICENSE
- ISC