bitfield: add methods insert and extract (#9612)

This commit is contained in:
れもん 2021-04-10 23:42:09 +09:00 committed by GitHub
parent 38c517c1a2
commit e66de8e824
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 83 additions and 0 deletions

View file

@ -137,6 +137,75 @@ pub fn (mut instance BitField) clear_bit(bitnr int) {
instance.field[bitslot(bitnr)] &= ~bitmask(bitnr)
}
// 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
}
}
// set_all sets all bits in the array to 1.
pub fn (mut instance BitField) set_all() {
for i in 0 .. zbitnslots(instance.size) {