2019-09-08 16:45:51 +02:00
|
|
|
module bitfield
|
2019-07-09 21:11:09 +02:00
|
|
|
|
2019-07-23 18:29:04 +02:00
|
|
|
/*
|
2019-09-08 16:45:51 +02:00
|
|
|
bitfield is a module for
|
2019-07-23 18:29:04 +02:00
|
|
|
manipulating arrays of bits, i.e. series of zeroes and ones spread across an
|
|
|
|
array of storage units (unsigned 32-bit integers).
|
|
|
|
|
|
|
|
BitField structure
|
|
|
|
------------------
|
|
|
|
|
|
|
|
Bit arrays are stored in data structures called 'BitField'. The structure is
|
|
|
|
'opaque', i.e. its internals are not available to the end user. This module
|
|
|
|
provides API (functions and methods) for accessing and modifying bit arrays.
|
|
|
|
*/
|
2019-11-19 01:32:44 +01:00
|
|
|
pub struct BitField {
|
2019-07-09 21:11:09 +02:00
|
|
|
mut:
|
2021-01-12 04:38:43 +01:00
|
|
|
size int
|
2020-12-18 20:47:24 +01:00
|
|
|
// field *u32
|
2019-07-09 21:11:09 +02:00
|
|
|
field []u32
|
|
|
|
}
|
|
|
|
|
2019-09-08 16:45:51 +02:00
|
|
|
// helper functions
|
2019-07-09 21:11:09 +02:00
|
|
|
const (
|
2020-05-22 17:36:09 +02:00
|
|
|
slot_size = 32
|
2019-07-09 21:11:09 +02:00
|
|
|
)
|
|
|
|
|
2020-12-18 20:58:42 +01:00
|
|
|
// from_bytes converts a byte array into a bitfield.
|
|
|
|
// [0x0F, 0x01] => 0000 1111 0000 0001
|
2019-11-13 17:48:00 +01:00
|
|
|
pub fn from_bytes(input []byte) BitField {
|
|
|
|
mut output := new(input.len * 8)
|
|
|
|
for i, b in input {
|
2022-04-15 13:58:56 +02:00
|
|
|
mut ob := u8(0)
|
2020-12-18 20:47:24 +01:00
|
|
|
if b & 0b10000000 > 0 {
|
|
|
|
ob |= 0b00000001
|
|
|
|
}
|
|
|
|
if b & 0b01000000 > 0 {
|
|
|
|
ob |= 0b00000010
|
|
|
|
}
|
|
|
|
if b & 0b00100000 > 0 {
|
|
|
|
ob |= 0b00000100
|
|
|
|
}
|
|
|
|
if b & 0b00010000 > 0 {
|
|
|
|
ob |= 0b00001000
|
|
|
|
}
|
|
|
|
if b & 0b00001000 > 0 {
|
|
|
|
ob |= 0b00010000
|
|
|
|
}
|
|
|
|
if b & 0b00000100 > 0 {
|
|
|
|
ob |= 0b00100000
|
|
|
|
}
|
|
|
|
if b & 0b00000010 > 0 {
|
|
|
|
ob |= 0b01000000
|
|
|
|
}
|
|
|
|
if b & 0b00000001 > 0 {
|
|
|
|
ob |= 0b10000000
|
|
|
|
}
|
|
|
|
output.field[i / 4] |= u32(ob) << ((i % 4) * 8)
|
2019-11-13 17:48:00 +01:00
|
|
|
}
|
|
|
|
return output
|
|
|
|
}
|
|
|
|
|
2020-12-18 20:58:42 +01:00
|
|
|
// from_bytes_lowest_bits_first converts a byte array into a bitfield
|
|
|
|
// [0x0F, 0x01] => 1111 0000 1000 0000
|
|
|
|
pub fn from_bytes_lowest_bits_first(input []byte) BitField {
|
|
|
|
mut output := new(input.len * 8)
|
|
|
|
for i, b in input {
|
|
|
|
output.field[i / 4] |= u32(b) << ((i % 4) * 8)
|
|
|
|
}
|
|
|
|
return output
|
|
|
|
}
|
|
|
|
|
2020-04-04 17:59:26 +02:00
|
|
|
// from_str converts a string of characters ('0' and '1') to a bit
|
2019-07-23 18:29:04 +02:00
|
|
|
// array. Any character different from '0' is treated as '1'.
|
2020-04-04 17:59:26 +02:00
|
|
|
pub fn from_str(input string) BitField {
|
2019-07-14 17:56:48 +02:00
|
|
|
mut output := new(input.len)
|
2020-12-18 20:47:24 +01:00
|
|
|
for i in 0 .. input.len {
|
2020-04-04 17:03:03 +02:00
|
|
|
if input[i] != `0` {
|
2020-05-07 10:21:14 +02:00
|
|
|
output.set_bit(i)
|
2019-07-14 17:56:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return output
|
|
|
|
}
|
|
|
|
|
2020-04-04 17:59:26 +02:00
|
|
|
// str converts the bit array to a string of characters ('0' and '1') and
|
2019-07-23 18:29:04 +02:00
|
|
|
// return the string
|
2020-04-04 17:03:03 +02:00
|
|
|
pub fn (input BitField) str() string {
|
2019-07-14 17:56:48 +02:00
|
|
|
mut output := ''
|
2020-12-18 20:47:24 +01:00
|
|
|
for i in 0 .. input.size {
|
2020-05-07 10:21:14 +02:00
|
|
|
if input.get_bit(i) == 1 {
|
2019-07-14 17:56:48 +02:00
|
|
|
output = output + '1'
|
2020-12-18 20:47:24 +01:00
|
|
|
} else {
|
2019-07-14 17:56:48 +02:00
|
|
|
output = output + '0'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return output
|
|
|
|
}
|
|
|
|
|
2020-04-04 17:59:26 +02:00
|
|
|
// new creates an empty bit array of capable of storing 'size' bits.
|
2019-07-09 21:11:09 +02:00
|
|
|
pub fn new(size int) BitField {
|
|
|
|
output := BitField{
|
2019-09-08 16:45:51 +02:00
|
|
|
size: size
|
2020-12-18 20:47:24 +01:00
|
|
|
// field: *u32(calloc(zbitnslots(size) * slot_size / 8))
|
|
|
|
field: []u32{len: zbitnslots(size)}
|
2019-07-09 21:11:09 +02:00
|
|
|
}
|
|
|
|
return output
|
|
|
|
}
|
2020-12-18 20:47:24 +01:00
|
|
|
|
2020-12-18 21:10:51 +01:00
|
|
|
// frees the memory allocated for the bitfield instance
|
|
|
|
[unsafe]
|
|
|
|
pub fn (instance &BitField) free() {
|
|
|
|
unsafe {
|
|
|
|
instance.field.free()
|
|
|
|
}
|
2019-07-09 21:11:09 +02:00
|
|
|
}
|
2020-12-18 21:10:51 +01:00
|
|
|
|
2020-05-07 10:21:14 +02:00
|
|
|
// get_bit returns the value (0 or 1) of bit number 'bit_nr' (count from 0).
|
|
|
|
pub fn (instance BitField) get_bit(bitnr int) int {
|
2020-04-04 17:59:26 +02:00
|
|
|
if bitnr >= instance.size {
|
|
|
|
return 0
|
|
|
|
}
|
2021-01-24 10:54:27 +01:00
|
|
|
return int((instance.field[bitslot(bitnr)] >> (bitnr % bitfield.slot_size)) & u32(1))
|
2019-07-09 21:11:09 +02:00
|
|
|
}
|
|
|
|
|
2020-05-07 10:21:14 +02:00
|
|
|
// set_bit sets bit number 'bit_nr' to 1 (count from 0).
|
2020-05-17 13:51:18 +02:00
|
|
|
pub fn (mut instance BitField) set_bit(bitnr int) {
|
2020-04-04 17:59:26 +02:00
|
|
|
if bitnr >= instance.size {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
instance.field[bitslot(bitnr)] |= bitmask(bitnr)
|
2019-07-09 21:11:09 +02:00
|
|
|
}
|
|
|
|
|
2020-05-07 10:21:14 +02:00
|
|
|
// clear_bit clears (sets to zero) bit number 'bit_nr' (count from 0).
|
2020-05-17 13:51:18 +02:00
|
|
|
pub fn (mut instance BitField) clear_bit(bitnr int) {
|
2020-04-04 17:59:26 +02:00
|
|
|
if bitnr >= instance.size {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
instance.field[bitslot(bitnr)] &= ~bitmask(bitnr)
|
2019-07-09 21:11:09 +02:00
|
|
|
}
|
|
|
|
|
2021-04-10 16:42:09 +02:00
|
|
|
// extract returns the value converted from a slice of bit numbers
|
|
|
|
// from 'start' by the length of 'len'.
|
|
|
|
// 0101 (1, 2) => 0b10
|
|
|
|
pub fn (instance BitField) extract(start int, len int) u64 {
|
|
|
|
// panic?
|
|
|
|
if start < 0 {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
mut output := u64(0)
|
|
|
|
for i in 0 .. len {
|
|
|
|
output |= u64(instance.get_bit(start + len - i - 1)) << i
|
|
|
|
}
|
|
|
|
return output
|
|
|
|
}
|
|
|
|
|
|
|
|
// insert sets bit numbers from 'start' to 'len' length with
|
|
|
|
// the value converted from the number 'value'.
|
|
|
|
// 0000 (1, 2, 0b10) => 0100
|
|
|
|
pub fn (mut instance BitField) insert<T>(start int, len int, _value T) {
|
|
|
|
// panic?
|
|
|
|
if start < 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
mut value := _value
|
|
|
|
for i in 0 .. len {
|
|
|
|
pos := start + len - i - 1
|
|
|
|
if value & 1 == 1 {
|
|
|
|
instance.set_bit(pos)
|
|
|
|
} else {
|
|
|
|
instance.clear_bit(pos)
|
|
|
|
}
|
|
|
|
value >>= 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// extract returns the value converted from a slice of bit numbers
|
|
|
|
// from 'start' by the length of 'len'.
|
|
|
|
// 0101 (1, 2) => 0b01
|
|
|
|
pub fn (instance BitField) extract_lowest_bits_first(start int, len int) u64 {
|
|
|
|
// panic?
|
|
|
|
if start < 0 {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
mut output := u64(0)
|
|
|
|
for i in 0 .. len {
|
|
|
|
output |= u64(instance.get_bit(start + i)) << i
|
|
|
|
}
|
|
|
|
return output
|
|
|
|
}
|
|
|
|
|
|
|
|
// insert sets bit numbers from 'start' to 'len' length with
|
|
|
|
// the value converted from the number 'value'.
|
|
|
|
// 0000 (1, 2, 0b10) => 0010
|
|
|
|
pub fn (mut instance BitField) insert_lowest_bits_first<T>(start int, len int, _value T) {
|
|
|
|
// panic?
|
|
|
|
if start < 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
mut value := _value
|
|
|
|
for pos in start .. start + len {
|
|
|
|
if value & 1 == 1 {
|
|
|
|
instance.set_bit(pos)
|
|
|
|
} else {
|
|
|
|
instance.clear_bit(pos)
|
|
|
|
}
|
|
|
|
value >>= 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-07 10:21:14 +02:00
|
|
|
// set_all sets all bits in the array to 1.
|
2020-05-17 13:51:18 +02:00
|
|
|
pub fn (mut instance BitField) set_all() {
|
2020-12-18 20:47:24 +01:00
|
|
|
for i in 0 .. zbitnslots(instance.size) {
|
2019-07-14 17:56:48 +02:00
|
|
|
instance.field[i] = u32(-1)
|
|
|
|
}
|
2020-05-07 10:21:14 +02:00
|
|
|
instance.clear_tail()
|
2019-07-14 17:56:48 +02:00
|
|
|
}
|
|
|
|
|
2020-05-07 10:21:14 +02:00
|
|
|
// clear_all clears (sets to zero) all bits in the array.
|
2020-05-17 13:51:18 +02:00
|
|
|
pub fn (mut instance BitField) clear_all() {
|
2020-12-18 20:47:24 +01:00
|
|
|
for i in 0 .. zbitnslots(instance.size) {
|
2019-07-14 17:56:48 +02:00
|
|
|
instance.field[i] = u32(0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-07 10:21:14 +02:00
|
|
|
// toggle_bit changes the value (from 0 to 1 or from 1 to 0) of bit
|
2020-04-04 17:59:26 +02:00
|
|
|
// number 'bit_nr'.
|
2020-05-17 13:51:18 +02:00
|
|
|
pub fn (mut instance BitField) toggle_bit(bitnr int) {
|
2020-04-04 17:59:26 +02:00
|
|
|
if bitnr >= instance.size {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
instance.field[bitslot(bitnr)] ^= bitmask(bitnr)
|
2019-07-09 21:11:09 +02:00
|
|
|
}
|
|
|
|
|
2020-05-07 10:21:14 +02:00
|
|
|
// bf_and performs logical AND operation on every pair of bits from 'input1' and
|
2020-04-04 17:59:26 +02:00
|
|
|
// 'input2' and returns the result as a new array. If inputs differ in size,
|
2019-07-23 18:29:04 +02:00
|
|
|
// the tail of the longer one is ignored.
|
2020-05-07 10:21:14 +02:00
|
|
|
pub fn bf_and(input1 BitField, input2 BitField) BitField {
|
2019-07-09 21:11:09 +02:00
|
|
|
size := min(input1.size, input2.size)
|
2020-07-01 00:53:53 +02:00
|
|
|
bitnslots := zbitnslots(size)
|
2019-07-09 21:11:09 +02:00
|
|
|
mut output := new(size)
|
2020-12-18 20:47:24 +01:00
|
|
|
for i in 0 .. bitnslots {
|
2019-07-09 21:11:09 +02:00
|
|
|
output.field[i] = input1.field[i] & input2.field[i]
|
|
|
|
}
|
2020-05-07 10:21:14 +02:00
|
|
|
output.clear_tail()
|
2019-07-09 21:11:09 +02:00
|
|
|
return output
|
|
|
|
}
|
|
|
|
|
2020-05-07 10:21:14 +02:00
|
|
|
// bf_not toggles all bits in a bit array and returns the result as a new array.
|
|
|
|
pub fn bf_not(input BitField) BitField {
|
2019-07-09 21:11:09 +02:00
|
|
|
size := input.size
|
2020-07-01 00:53:53 +02:00
|
|
|
bitnslots := zbitnslots(size)
|
2019-07-09 21:11:09 +02:00
|
|
|
mut output := new(size)
|
2020-12-18 20:47:24 +01:00
|
|
|
for i in 0 .. bitnslots {
|
2019-07-09 21:11:09 +02:00
|
|
|
output.field[i] = ~input.field[i]
|
|
|
|
}
|
2020-05-07 10:21:14 +02:00
|
|
|
output.clear_tail()
|
2019-07-09 21:11:09 +02:00
|
|
|
return output
|
|
|
|
}
|
|
|
|
|
2020-05-07 10:21:14 +02:00
|
|
|
// bf_or performs logical OR operation on every pair of bits from 'input1' and
|
2020-04-04 17:59:26 +02:00
|
|
|
// 'input2' and returns the result as a new array. If inputs differ in size,
|
|
|
|
// the tail of the longer one is ignored.
|
2020-05-07 10:21:14 +02:00
|
|
|
pub fn bf_or(input1 BitField, input2 BitField) BitField {
|
2019-07-09 21:11:09 +02:00
|
|
|
size := min(input1.size, input2.size)
|
2020-07-01 00:53:53 +02:00
|
|
|
bitnslots := zbitnslots(size)
|
2019-07-09 21:11:09 +02:00
|
|
|
mut output := new(size)
|
2020-12-18 20:47:24 +01:00
|
|
|
for i in 0 .. bitnslots {
|
2019-07-09 21:11:09 +02:00
|
|
|
output.field[i] = input1.field[i] | input2.field[i]
|
|
|
|
}
|
2020-05-07 10:21:14 +02:00
|
|
|
output.clear_tail()
|
2019-07-09 21:11:09 +02:00
|
|
|
return output
|
|
|
|
}
|
|
|
|
|
2020-05-07 10:21:14 +02:00
|
|
|
// bf_xor perform logical XOR operation on every pair of bits from 'input1' and
|
2020-04-04 17:59:26 +02:00
|
|
|
// 'input2' and returns the result as a new array. If inputs differ in size,
|
|
|
|
// the tail of the longer one is ignored.
|
2020-05-07 10:21:14 +02:00
|
|
|
pub fn bf_xor(input1 BitField, input2 BitField) BitField {
|
2019-07-09 21:11:09 +02:00
|
|
|
size := min(input1.size, input2.size)
|
2020-07-01 00:53:53 +02:00
|
|
|
bitnslots := zbitnslots(size)
|
2019-07-09 21:11:09 +02:00
|
|
|
mut output := new(size)
|
2020-12-18 20:47:24 +01:00
|
|
|
for i in 0 .. bitnslots {
|
2019-07-09 21:11:09 +02:00
|
|
|
output.field[i] = input1.field[i] ^ input2.field[i]
|
|
|
|
}
|
2020-05-07 10:21:14 +02:00
|
|
|
output.clear_tail()
|
2019-07-09 21:11:09 +02:00
|
|
|
return output
|
|
|
|
}
|
|
|
|
|
2020-04-04 17:59:26 +02:00
|
|
|
// join concatenates two bit arrays and return the result as a new array.
|
2019-07-12 20:46:37 +02:00
|
|
|
pub fn join(input1 BitField, input2 BitField) BitField {
|
|
|
|
output_size := input1.size + input2.size
|
|
|
|
mut output := new(output_size)
|
|
|
|
// copy the first input to output as is
|
2020-12-18 20:47:24 +01:00
|
|
|
for i in 0 .. zbitnslots(input1.size) {
|
2019-07-12 20:46:37 +02:00
|
|
|
output.field[i] = input1.field[i]
|
|
|
|
}
|
|
|
|
// find offset bit and offset slot
|
2021-01-24 10:54:27 +01:00
|
|
|
offset_bit := input1.size % bitfield.slot_size
|
|
|
|
offset_slot := input1.size / bitfield.slot_size
|
2020-12-18 20:47:24 +01:00
|
|
|
for i in 0 .. zbitnslots(input2.size) {
|
|
|
|
output.field[i + offset_slot] |= u32(input2.field[i] << u32(offset_bit))
|
2019-07-12 20:46:37 +02:00
|
|
|
}
|
|
|
|
/*
|
2020-12-18 20:47:24 +01:00
|
|
|
* If offset_bit is not zero, additional operations are needed.
|
2019-07-12 20:46:37 +02:00
|
|
|
* Number of iterations depends on the nr of slots in output. Two
|
|
|
|
* options:
|
|
|
|
* (a) nr of slots in output is the sum of inputs' slots. In this
|
|
|
|
* case, the nr of bits in the last slot of output is less than the
|
2019-09-14 22:54:14 +02:00
|
|
|
* nr of bits in the second input (i.e. ), OR
|
2019-07-12 20:46:37 +02:00
|
|
|
* (b) nr of slots of output is the sum of inputs' slots less one
|
|
|
|
* (i.e. less iterations needed). In this case, the nr of bits in
|
2019-09-14 22:54:14 +02:00
|
|
|
* the last slot of output is greater than the nr of bits in the second
|
2019-07-12 20:46:37 +02:00
|
|
|
* input.
|
|
|
|
* If offset_bit is zero, no additional copies needed.
|
2020-12-18 20:47:24 +01:00
|
|
|
*/
|
2021-01-24 10:54:27 +01:00
|
|
|
if (output_size - 1) % bitfield.slot_size < (input2.size - 1) % bitfield.slot_size {
|
2020-12-18 20:47:24 +01:00
|
|
|
for i in 0 .. zbitnslots(input2.size) {
|
2021-01-24 10:54:27 +01:00
|
|
|
output.field[i + offset_slot + 1] |= u32(input2.field[i] >> u32(bitfield.slot_size - offset_bit))
|
2019-07-12 20:46:37 +02:00
|
|
|
}
|
2021-01-24 10:54:27 +01:00
|
|
|
} else if (output_size - 1) % bitfield.slot_size > (input2.size - 1) % bitfield.slot_size {
|
2020-12-18 20:47:24 +01:00
|
|
|
for i in 0 .. zbitnslots(input2.size) - 1 {
|
2021-01-24 10:54:27 +01:00
|
|
|
output.field[i + offset_slot + 1] |= u32(input2.field[i] >> u32(bitfield.slot_size - offset_bit))
|
2019-07-12 20:46:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return output
|
|
|
|
}
|
|
|
|
|
2020-05-07 10:21:14 +02:00
|
|
|
// get_size returns the number of bits the array can hold.
|
|
|
|
pub fn (instance BitField) get_size() int {
|
2019-07-09 21:11:09 +02:00
|
|
|
return instance.size
|
|
|
|
}
|
|
|
|
|
2020-04-04 17:59:26 +02:00
|
|
|
// clone creates a copy of a bit array.
|
|
|
|
pub fn (instance BitField) clone() BitField {
|
2020-07-01 00:53:53 +02:00
|
|
|
bitnslots := zbitnslots(instance.size)
|
2020-04-04 17:59:26 +02:00
|
|
|
mut output := new(instance.size)
|
2020-12-18 20:47:24 +01:00
|
|
|
for i in 0 .. bitnslots {
|
2020-04-04 17:59:26 +02:00
|
|
|
output.field[i] = instance.field[i]
|
2019-07-09 21:11:09 +02:00
|
|
|
}
|
|
|
|
return output
|
2019-07-10 22:05:50 +02:00
|
|
|
}
|
|
|
|
|
2021-11-18 19:14:59 +01:00
|
|
|
// == compares 2 bitfields, and returns true when they are equal
|
2021-06-24 06:27:04 +02:00
|
|
|
pub fn (a BitField) == (b BitField) bool {
|
|
|
|
if a.size != b.size {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
for i in 0 .. zbitnslots(a.size) {
|
|
|
|
if a.field[i] != b.field[i] {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2020-05-07 10:21:14 +02:00
|
|
|
// pop_count returns the number of set bits (ones) in the array.
|
|
|
|
pub fn (instance BitField) pop_count() int {
|
2019-07-12 20:46:37 +02:00
|
|
|
size := instance.size
|
2020-07-01 00:53:53 +02:00
|
|
|
bitnslots := zbitnslots(size)
|
2021-01-24 10:54:27 +01:00
|
|
|
tail := size % bitfield.slot_size
|
2019-07-12 20:46:37 +02:00
|
|
|
mut count := 0
|
2020-12-18 20:47:24 +01:00
|
|
|
for i in 0 .. bitnslots - 1 {
|
2021-01-24 10:54:27 +01:00
|
|
|
for j in 0 .. bitfield.slot_size {
|
2019-07-12 20:46:37 +02:00
|
|
|
if u32(instance.field[i] >> u32(j)) & u32(1) == u32(1) {
|
|
|
|
count++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-12-18 20:47:24 +01:00
|
|
|
for j in 0 .. tail {
|
2019-07-12 20:46:37 +02:00
|
|
|
if u32(instance.field[bitnslots - 1] >> u32(j)) & u32(1) == u32(1) {
|
|
|
|
count++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return count
|
|
|
|
}
|
|
|
|
|
2020-04-04 17:59:26 +02:00
|
|
|
// hamming computes the Hamming distance between two bit arrays.
|
2020-05-07 10:21:14 +02:00
|
|
|
pub fn hamming(input1 BitField, input2 BitField) int {
|
|
|
|
input_xored := bf_xor(input1, input2)
|
|
|
|
return input_xored.pop_count()
|
2019-07-12 20:46:37 +02:00
|
|
|
}
|
|
|
|
|
2020-04-04 17:59:26 +02:00
|
|
|
// pos checks if the array contains a sub-array 'needle' and returns its
|
2019-07-23 18:29:04 +02:00
|
|
|
// position if it does, -1 if it does not, and -2 on error.
|
2019-07-15 19:07:07 +02:00
|
|
|
pub fn (haystack BitField) pos(needle BitField) int {
|
|
|
|
heystack_size := haystack.size
|
|
|
|
needle_size := needle.size
|
|
|
|
diff := heystack_size - needle_size
|
|
|
|
// needle longer than haystack; return error code -2
|
|
|
|
if diff < 0 {
|
|
|
|
return -2
|
|
|
|
}
|
|
|
|
for i := 0; i <= diff; i++ {
|
|
|
|
needle_candidate := haystack.slice(i, needle_size + i)
|
2021-06-24 06:27:04 +02:00
|
|
|
if needle_candidate == needle {
|
2019-07-15 19:07:07 +02:00
|
|
|
// needle matches a sub-array of haystack; return starting position of the sub-array
|
|
|
|
return i
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// nothing matched; return -1
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
|
2020-04-04 17:59:26 +02:00
|
|
|
// slice returns a sub-array of bits between 'start_bit_nr' (included) and
|
|
|
|
// 'end_bit_nr' (excluded).
|
2019-07-12 20:46:37 +02:00
|
|
|
pub fn (input BitField) slice(_start int, _end int) BitField {
|
|
|
|
// boundary checks
|
|
|
|
mut start := _start
|
|
|
|
mut end := _end
|
|
|
|
if end > input.size {
|
|
|
|
end = input.size // or panic?
|
|
|
|
}
|
|
|
|
if start > end {
|
|
|
|
start = end // or panic?
|
|
|
|
}
|
|
|
|
mut output := new(end - start)
|
2021-01-24 10:54:27 +01:00
|
|
|
start_offset := start % bitfield.slot_size
|
|
|
|
end_offset := (end - 1) % bitfield.slot_size
|
|
|
|
start_slot := start / bitfield.slot_size
|
|
|
|
end_slot := (end - 1) / bitfield.slot_size
|
2020-07-01 00:53:53 +02:00
|
|
|
output_slots := zbitnslots(end - start)
|
2019-07-12 20:46:37 +02:00
|
|
|
if output_slots > 1 {
|
|
|
|
if start_offset != 0 {
|
2020-12-18 20:47:24 +01:00
|
|
|
for i in 0 .. output_slots - 1 {
|
|
|
|
output.field[i] = u32(input.field[start_slot + i] >> u32(start_offset))
|
2021-01-28 11:23:48 +01:00
|
|
|
output.field[i] = output.field[i] | u32(input.field[start_slot + i + 1] << u32(bitfield.slot_size - start_offset))
|
2019-07-12 20:46:37 +02:00
|
|
|
}
|
2020-12-18 20:47:24 +01:00
|
|
|
} else {
|
|
|
|
for i in 0 .. output_slots - 1 {
|
|
|
|
output.field[i] = u32(input.field[start_slot + i])
|
2019-07-12 20:46:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if start_offset > end_offset {
|
2021-01-24 10:54:27 +01:00
|
|
|
output.field[(end - start - 1) / bitfield.slot_size] = u32(input.field[end_slot - 1] >> u32(start_offset))
|
2019-07-12 20:46:37 +02:00
|
|
|
mut mask := u32((1 << (end_offset + 1)) - 1)
|
|
|
|
mask = input.field[end_slot] & mask
|
2021-01-24 10:54:27 +01:00
|
|
|
mask = u32(mask << u32(bitfield.slot_size - start_offset))
|
|
|
|
output.field[(end - start - 1) / bitfield.slot_size] |= mask
|
2020-12-18 20:47:24 +01:00
|
|
|
} else if start_offset == 0 {
|
2019-07-12 20:46:37 +02:00
|
|
|
mut mask := u32(0)
|
2021-01-24 10:54:27 +01:00
|
|
|
if end_offset == bitfield.slot_size - 1 {
|
2019-07-12 20:46:37 +02:00
|
|
|
mask = u32(-1)
|
2020-12-18 20:47:24 +01:00
|
|
|
} else {
|
2019-07-12 20:46:37 +02:00
|
|
|
mask = u32(u32(1) << u32(end_offset + 1))
|
|
|
|
mask = mask - u32(1)
|
|
|
|
}
|
2021-01-24 10:54:27 +01:00
|
|
|
output.field[(end - start - 1) / bitfield.slot_size] = (input.field[end_slot] & mask)
|
2020-12-18 20:47:24 +01:00
|
|
|
} else {
|
|
|
|
mut mask := u32(((1 << (end_offset - start_offset + 1)) - 1) << start_offset)
|
2019-07-12 20:46:37 +02:00
|
|
|
mask = input.field[end_slot] & mask
|
|
|
|
mask = u32(mask >> u32(start_offset))
|
2021-01-24 10:54:27 +01:00
|
|
|
output.field[(end - start - 1) / bitfield.slot_size] |= mask
|
2019-07-12 20:46:37 +02:00
|
|
|
}
|
|
|
|
return output
|
|
|
|
}
|
2019-07-15 19:07:07 +02:00
|
|
|
|
2020-04-04 17:59:26 +02:00
|
|
|
// reverse reverses the order of bits in the array (swap the first with the
|
|
|
|
// last, the second with the last but one and so on).
|
2019-12-06 21:02:09 +01:00
|
|
|
pub fn (instance BitField) reverse() BitField {
|
2019-07-15 19:07:07 +02:00
|
|
|
size := instance.size
|
2020-07-01 00:53:53 +02:00
|
|
|
bitnslots := zbitnslots(size)
|
2019-07-15 19:07:07 +02:00
|
|
|
mut output := new(size)
|
2020-12-18 20:47:24 +01:00
|
|
|
for i := 0; i < (bitnslots - 1); i++ {
|
2021-01-24 10:54:27 +01:00
|
|
|
for j in 0 .. bitfield.slot_size {
|
2019-07-15 19:07:07 +02:00
|
|
|
if u32(instance.field[i] >> u32(j)) & u32(1) == u32(1) {
|
2021-01-24 10:54:27 +01:00
|
|
|
output.set_bit(size - i * bitfield.slot_size - j - 1)
|
2019-07-15 19:07:07 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-01-24 10:54:27 +01:00
|
|
|
bits_in_last_input_slot := (size - 1) % bitfield.slot_size + 1
|
2020-12-18 20:47:24 +01:00
|
|
|
for j in 0 .. bits_in_last_input_slot {
|
2019-07-15 19:07:07 +02:00
|
|
|
if u32(instance.field[bitnslots - 1] >> u32(j)) & u32(1) == u32(1) {
|
2020-05-07 10:21:14 +02:00
|
|
|
output.set_bit(bits_in_last_input_slot - j - 1)
|
2019-07-15 19:07:07 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return output
|
|
|
|
}
|
|
|
|
|
2020-04-04 17:59:26 +02:00
|
|
|
// resize changes the size of the bit array to 'new_size'.
|
2020-05-17 13:51:18 +02:00
|
|
|
pub fn (mut instance BitField) resize(new_size int) {
|
2020-07-01 00:53:53 +02:00
|
|
|
new_bitnslots := zbitnslots(new_size)
|
2019-07-15 19:07:07 +02:00
|
|
|
old_size := instance.size
|
2020-07-01 00:53:53 +02:00
|
|
|
old_bitnslots := zbitnslots(old_size)
|
2020-12-18 20:47:24 +01:00
|
|
|
mut field := []u32{len: new_bitnslots}
|
2019-07-29 10:37:39 +02:00
|
|
|
for i := 0; i < old_bitnslots && i < new_bitnslots; i++ {
|
2019-07-15 19:07:07 +02:00
|
|
|
field[i] = instance.field[i]
|
|
|
|
}
|
2019-09-08 15:10:59 +02:00
|
|
|
instance.field = field.clone()
|
2019-09-08 17:53:40 +02:00
|
|
|
instance.size = new_size
|
2021-01-24 10:54:27 +01:00
|
|
|
if new_size < old_size && new_size % bitfield.slot_size != 0 {
|
2020-05-07 10:21:14 +02:00
|
|
|
instance.clear_tail()
|
2019-07-15 19:07:07 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-04 17:59:26 +02:00
|
|
|
// rotate circular-shifts the bits by 'offset' positions (move
|
|
|
|
// 'offset' bit to 0, 'offset+1' bit to 1, and so on).
|
2019-07-15 19:07:07 +02:00
|
|
|
pub fn (instance BitField) rotate(offset int) BitField {
|
2020-12-18 20:47:24 +01:00
|
|
|
/*
|
|
|
|
*
|
2019-07-15 19:07:07 +02:00
|
|
|
* This function "cuts" the bitfield into two and swaps them.
|
|
|
|
* If the offset is positive, the cutting point is counted from the
|
|
|
|
* beginning of the bit array, otherwise from the end.
|
2020-12-18 20:47:24 +01:00
|
|
|
*
|
|
|
|
*/
|
2019-07-15 19:07:07 +02:00
|
|
|
size := instance.size
|
|
|
|
// removing extra rotations
|
|
|
|
mut offset_internal := offset % size
|
2020-03-29 10:08:42 +02:00
|
|
|
if offset_internal == 0 {
|
2019-07-15 19:07:07 +02:00
|
|
|
// nothing to shift
|
|
|
|
return instance
|
|
|
|
}
|
|
|
|
if offset_internal < 0 {
|
|
|
|
offset_internal = offset_internal + size
|
|
|
|
}
|
|
|
|
first_chunk := instance.slice(0, offset_internal)
|
|
|
|
second_chunk := instance.slice(offset_internal, size)
|
|
|
|
output := join(second_chunk, first_chunk)
|
|
|
|
return output
|
|
|
|
}
|
2020-04-04 17:59:26 +02:00
|
|
|
|
|
|
|
// Internal functions
|
2020-12-18 21:10:51 +01:00
|
|
|
// clear_tail clears the extra bits that are not part of the bitfield, but yet are allocated
|
2020-05-17 13:51:18 +02:00
|
|
|
fn (mut instance BitField) clear_tail() {
|
2021-01-24 10:54:27 +01:00
|
|
|
tail := instance.size % bitfield.slot_size
|
2020-04-04 17:59:26 +02:00
|
|
|
if tail != 0 {
|
|
|
|
// create a mask for the tail
|
|
|
|
mask := u32((1 << tail) - 1)
|
|
|
|
// clear the extra bits
|
2021-01-23 09:33:22 +01:00
|
|
|
instance.field[zbitnslots(instance.size) - 1] = instance.field[zbitnslots(instance.size) - 1] & mask
|
2020-04-04 17:59:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-18 21:10:51 +01:00
|
|
|
// bitmask is the bitmask needed to access a particular bit at offset bitnr
|
2020-04-04 17:59:26 +02:00
|
|
|
fn bitmask(bitnr int) u32 {
|
2021-01-24 10:54:27 +01:00
|
|
|
return u32(u32(1) << u32(bitnr % bitfield.slot_size))
|
2020-04-04 17:59:26 +02:00
|
|
|
}
|
|
|
|
|
2020-12-18 21:10:51 +01:00
|
|
|
// bitslot is the slot index (i.e. the integer) where a particular bit is located
|
2020-04-04 17:59:26 +02:00
|
|
|
fn bitslot(size int) int {
|
2021-01-24 10:54:27 +01:00
|
|
|
return size / bitfield.slot_size
|
2020-04-04 17:59:26 +02:00
|
|
|
}
|
|
|
|
|
2020-12-18 21:10:51 +01:00
|
|
|
// min returns the minimum of 2 integers; it is here to avoid importing math just for that
|
2020-04-04 17:59:26 +02:00
|
|
|
fn min(input1 int, input2 int) int {
|
|
|
|
if input1 < input2 {
|
|
|
|
return input1
|
2020-12-18 20:47:24 +01:00
|
|
|
} else {
|
2020-04-04 17:59:26 +02:00
|
|
|
return input2
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-18 21:10:51 +01:00
|
|
|
// zbitnslots returns the minimum number of whole integers, needed to represent a bitfield of size length
|
2020-07-01 00:53:53 +02:00
|
|
|
fn zbitnslots(length int) int {
|
2021-01-24 10:54:27 +01:00
|
|
|
return (length - 1) / bitfield.slot_size + 1
|
2020-04-04 17:59:26 +02:00
|
|
|
}
|