bitfield: document all functions

pull/7402/head
Delyan Angelov 2020-12-18 22:10:51 +02:00
parent f74ab3a52d
commit 8e6d2df131
No known key found for this signature in database
GPG Key ID: 66886C0F12D595ED
1 changed files with 12 additions and 5 deletions

View File

@ -105,12 +105,14 @@ pub fn new(size int) BitField {
return output return output
} }
/* // frees the memory allocated for the bitfield instance
pub fn del(instance *BitField) { [unsafe]
free(instance.field) pub fn (instance &BitField) free() {
free(instance) unsafe {
instance.field.free()
} }
*/ }
// get_bit returns the value (0 or 1) of bit number 'bit_nr' (count from 0). // 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 { pub fn (instance BitField) get_bit(bitnr int) int {
if bitnr >= instance.size { if bitnr >= instance.size {
@ -449,6 +451,7 @@ pub fn (instance BitField) rotate(offset int) BitField {
} }
// Internal functions // Internal functions
// clear_tail clears the extra bits that are not part of the bitfield, but yet are allocated
fn (mut instance BitField) clear_tail() { fn (mut instance BitField) clear_tail() {
tail := instance.size % slot_size tail := instance.size % slot_size
if tail != 0 { if tail != 0 {
@ -460,14 +463,17 @@ fn (mut instance BitField) clear_tail() {
} }
} }
// bitmask is the bitmask needed to access a particular bit at offset bitnr
fn bitmask(bitnr int) u32 { fn bitmask(bitnr int) u32 {
return u32(u32(1) << u32(bitnr % slot_size)) return u32(u32(1) << u32(bitnr % slot_size))
} }
// bitslot is the slot index (i.e. the integer) where a particular bit is located
fn bitslot(size int) int { fn bitslot(size int) int {
return size / slot_size return size / slot_size
} }
// min returns the minimum of 2 integers; it is here to avoid importing math just for that
fn min(input1 int, input2 int) int { fn min(input1 int, input2 int) int {
if input1 < input2 { if input1 < input2 {
return input1 return input1
@ -476,6 +482,7 @@ fn min(input1 int, input2 int) int {
} }
} }
// zbitnslots returns the minimum number of whole integers, needed to represent a bitfield of size length
fn zbitnslots(length int) int { fn zbitnslots(length int) int {
return (length - 1) / slot_size + 1 return (length - 1) / slot_size + 1
} }