1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

bitfield: add [inline] for very commonly used simple methods

This commit is contained in:
Delyan Angelov 2023-03-30 11:53:02 +03:00
parent ae6fc64fb3
commit 4ef6e16e3b
No known key found for this signature in database
GPG Key ID: 66886C0F12D595ED

View File

@ -114,6 +114,7 @@ pub fn (instance &BitField) 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).
[inline]
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 {
return 0 return 0
@ -122,6 +123,7 @@ pub fn (instance BitField) get_bit(bitnr int) int {
} }
// set_bit sets bit number 'bit_nr' to 1 (count from 0). // set_bit sets bit number 'bit_nr' to 1 (count from 0).
[inline]
pub fn (mut instance BitField) set_bit(bitnr int) { pub fn (mut instance BitField) set_bit(bitnr int) {
if bitnr >= instance.size { if bitnr >= instance.size {
return return
@ -130,6 +132,7 @@ pub fn (mut instance BitField) set_bit(bitnr int) {
} }
// clear_bit clears (sets to zero) bit number 'bit_nr' (count from 0). // clear_bit clears (sets to zero) bit number 'bit_nr' (count from 0).
[inline]
pub fn (mut instance BitField) clear_bit(bitnr int) { pub fn (mut instance BitField) clear_bit(bitnr int) {
if bitnr >= instance.size { if bitnr >= instance.size {
return return
@ -209,7 +212,7 @@ pub fn (mut instance BitField) insert_lowest_bits_first[T](start int, len int, _
// set_all sets all bits in the array to 1. // set_all sets all bits in the array to 1.
pub fn (mut instance BitField) set_all() { pub fn (mut instance BitField) set_all() {
for i in 0 .. zbitnslots(instance.size) { for i in 0 .. zbitnslots(instance.size) {
instance.field[i] = u32(-1) instance.field[i] = u32(0xFFFF_FFFF)
} }
instance.clear_tail() instance.clear_tail()
} }
@ -223,6 +226,7 @@ pub fn (mut instance BitField) clear_all() {
// toggle_bit changes the value (from 0 to 1 or from 1 to 0) of bit // toggle_bit changes the value (from 0 to 1 or from 1 to 0) of bit
// number 'bit_nr'. // number 'bit_nr'.
[inline]
pub fn (mut instance BitField) toggle_bit(bitnr int) { pub fn (mut instance BitField) toggle_bit(bitnr int) {
if bitnr >= instance.size { if bitnr >= instance.size {
return return
@ -324,6 +328,7 @@ pub fn join(input1 BitField, input2 BitField) BitField {
} }
// get_size returns the number of bits the array can hold. // get_size returns the number of bits the array can hold.
[inline]
pub fn (instance BitField) get_size() int { pub fn (instance BitField) get_size() int {
return instance.size return instance.size
} }
@ -373,6 +378,7 @@ pub fn (instance BitField) pop_count() int {
} }
// hamming computes the Hamming distance between two bit arrays. // hamming computes the Hamming distance between two bit arrays.
[inline]
pub fn hamming(input1 BitField, input2 BitField) int { pub fn hamming(input1 BitField, input2 BitField) int {
input_xored := bf_xor(input1, input2) input_xored := bf_xor(input1, input2)
return input_xored.pop_count() return input_xored.pop_count()
@ -519,6 +525,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 // clear_tail clears the extra bits that are not part of the bitfield, but yet are allocated
[inline]
fn (mut instance BitField) clear_tail() { fn (mut instance BitField) clear_tail() {
tail := instance.size % bitfield.slot_size tail := instance.size % bitfield.slot_size
if tail != 0 { if tail != 0 {
@ -530,16 +537,19 @@ fn (mut instance BitField) clear_tail() {
} }
// bitmask is the bitmask needed to access a particular bit at offset bitnr // bitmask is the bitmask needed to access a particular bit at offset bitnr
[inline]
fn bitmask(bitnr int) u32 { fn bitmask(bitnr int) u32 {
return u32(u32(1) << u32(bitnr % bitfield.slot_size)) return u32(u32(1) << u32(bitnr % bitfield.slot_size))
} }
// bitslot is the slot index (i.e. the integer) where a particular bit is located // bitslot is the slot index (i.e. the integer) where a particular bit is located
[inline]
fn bitslot(size int) int { fn bitslot(size int) int {
return size / bitfield.slot_size return size / bitfield.slot_size
} }
// min returns the minimum of 2 integers; it is here to avoid importing math just for that // min returns the minimum of 2 integers; it is here to avoid importing math just for that
[inline]
fn min(input1 int, input2 int) int { fn min(input1 int, input2 int) int {
if input1 < input2 { if input1 < input2 {
return input1 return input1
@ -549,6 +559,7 @@ fn min(input1 int, input2 int) int {
} }
// zbitnslots returns the minimum number of whole integers, needed to represent a bitfield of size length // zbitnslots returns the minimum number of whole integers, needed to represent a bitfield of size length
[inline]
fn zbitnslots(length int) int { fn zbitnslots(length int) int {
return (length - 1) / bitfield.slot_size + 1 return (length - 1) / bitfield.slot_size + 1
} }