diff --git a/vlib/bitfield/bitfield.v b/vlib/bitfield/bitfield.v index 9e4d135692..e9632f366d 100644 --- a/vlib/bitfield/bitfield.v +++ b/vlib/bitfield/bitfield.v @@ -42,7 +42,7 @@ pub fn from_str(input string) BitField { mut output := new(input.len) for i in 0..input.len { if input[i] != `0` { - output.setbit(i) + output.set_bit(i) } } return output @@ -53,7 +53,7 @@ pub fn from_str(input string) BitField { pub fn (input BitField) str() string { mut output := '' for i in 0..input.size { - if input.getbit(i) == 1 { + if input.get_bit(i) == 1 { output = output + '1' } else { @@ -79,105 +79,105 @@ pub fn del(instance *BitField) { } */ -// getbit returns the value (0 or 1) of bit number 'bit_nr' (count from 0). -pub fn (instance BitField) getbit(bitnr int) int { +// 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 { if bitnr >= instance.size { return 0 } return (instance.field[bitslot(bitnr)] >> (bitnr % SLOT_SIZE)) & u32(1) } -// setbit sets bit number 'bit_nr' to 1 (count from 0). -pub fn (instance mut BitField) setbit(bitnr int) { +// set_bit sets bit number 'bit_nr' to 1 (count from 0). +pub fn (instance mut BitField) set_bit(bitnr int) { if bitnr >= instance.size { return } instance.field[bitslot(bitnr)] |= bitmask(bitnr) } -// clearbit clears (sets to zero) bit number 'bit_nr' (count from 0). -pub fn (instance mut BitField) clearbit(bitnr int) { +// clear_bit clears (sets to zero) bit number 'bit_nr' (count from 0). +pub fn (instance mut BitField) clear_bit(bitnr int) { if bitnr >= instance.size { return } instance.field[bitslot(bitnr)] &= ~bitmask(bitnr) } -// setall sets all bits in the array to 1. -pub fn (instance mut BitField) setall() { +// set_all sets all bits in the array to 1. +pub fn (instance mut BitField) set_all() { for i in 0..bitnslots(instance.size) { instance.field[i] = u32(-1) } - instance.cleartail() + instance.clear_tail() } -// clearall clears (sets to zero) all bits in the array. -pub fn (instance mut BitField) clearall() { +// clear_all clears (sets to zero) all bits in the array. +pub fn (instance mut BitField) clear_all() { for i in 0..bitnslots(instance.size) { instance.field[i] = u32(0) } } -// togglebit 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'. -pub fn (instance mut BitField) togglebit(bitnr int) { +pub fn (instance mut BitField) toggle_bit(bitnr int) { if bitnr >= instance.size { return } instance.field[bitslot(bitnr)] ^= bitmask(bitnr) } -// bfand performs logical AND operation on every pair of bits from 'input1' and +// bf_and performs logical AND operation on every pair of bits from 'input1' and // 'input2' and returns the result as a new array. If inputs differ in size, // the tail of the longer one is ignored. -pub fn bfand(input1 BitField, input2 BitField) BitField { +pub fn bf_and(input1 BitField, input2 BitField) BitField { size := min(input1.size, input2.size) bitnslots := bitnslots(size) mut output := new(size) for i in 0..bitnslots { output.field[i] = input1.field[i] & input2.field[i] } - output.cleartail() + output.clear_tail() return output } -// bfnot toggles all bits in a bit array and returns the result as a new array. -pub fn bfnot(input BitField) BitField { +// bf_not toggles all bits in a bit array and returns the result as a new array. +pub fn bf_not(input BitField) BitField { size := input.size bitnslots := bitnslots(size) mut output := new(size) for i in 0..bitnslots { output.field[i] = ~input.field[i] } - output.cleartail() + output.clear_tail() return output } -// bfor performs logical OR operation on every pair of bits from 'input1' and +// bf_or performs logical OR operation on every pair of bits from 'input1' and // 'input2' and returns the result as a new array. If inputs differ in size, // the tail of the longer one is ignored. -pub fn bfor(input1 BitField, input2 BitField) BitField { +pub fn bf_or(input1 BitField, input2 BitField) BitField { size := min(input1.size, input2.size) bitnslots := bitnslots(size) mut output := new(size) for i in 0..bitnslots { output.field[i] = input1.field[i] | input2.field[i] } - output.cleartail() + output.clear_tail() return output } -// bfxor perform logical XOR operation on every pair of bits from 'input1' and +// bf_xor perform logical XOR operation on every pair of bits from 'input1' and // 'input2' and returns the result as a new array. If inputs differ in size, // the tail of the longer one is ignored. -pub fn bfxor(input1 BitField, input2 BitField) BitField { +pub fn bf_xor(input1 BitField, input2 BitField) BitField { size := min(input1.size, input2.size) bitnslots := bitnslots(size) mut output := new(size) for i in 0..bitnslots { output.field[i] = input1.field[i] ^ input2.field[i] } - output.cleartail() + output.clear_tail() return output } @@ -226,8 +226,8 @@ pub fn join(input1 BitField, input2 BitField) BitField { return output } -// getsize returns the number of bits the array can hold. -pub fn (instance BitField) getsize() int { +// get_size returns the number of bits the array can hold. +pub fn (instance BitField) get_size() int { return instance.size } @@ -251,8 +251,8 @@ pub fn (instance BitField) cmp(input BitField) bool { return true } -// popcount returns the number of set bits (ones) in the array. -pub fn (instance BitField) popcount() int { +// pop_count returns the number of set bits (ones) in the array. +pub fn (instance BitField) pop_count() int { size := instance.size bitnslots := bitnslots(size) tail := size % SLOT_SIZE @@ -273,9 +273,9 @@ pub fn (instance BitField) popcount() int { } // hamming computes the Hamming distance between two bit arrays. -pub fn hamming (input1 BitField, input2 BitField) int { - input_xored := bfxor(input1, input2) - return input_xored.popcount() +pub fn hamming(input1 BitField, input2 BitField) int { + input_xored := bf_xor(input1, input2) + return input_xored.pop_count() } // pos checks if the array contains a sub-array 'needle' and returns its @@ -376,14 +376,14 @@ pub fn (instance BitField) reverse() BitField { for i:= 0; i < (bitnslots - 1); i++ { for j in 0..SLOT_SIZE { if u32(instance.field[i] >> u32(j)) & u32(1) == u32(1) { - output.setbit(size - i * SLOT_SIZE - j - 1) + output.set_bit(size - i * SLOT_SIZE - j - 1) } } } bits_in_last_input_slot := (size - 1) % SLOT_SIZE + 1 for j in 0..bits_in_last_input_slot { if u32(instance.field[bitnslots - 1] >> u32(j)) & u32(1) == u32(1) { - output.setbit(bits_in_last_input_slot - j - 1) + output.set_bit(bits_in_last_input_slot - j - 1) } } return output @@ -401,7 +401,7 @@ pub fn (instance mut BitField) resize(new_size int) { instance.field = field.clone() instance.size = new_size if new_size < old_size && new_size % SLOT_SIZE != 0 { - instance.cleartail() + instance.clear_tail() } } @@ -433,7 +433,7 @@ pub fn (instance BitField) rotate(offset int) BitField { // Internal functions -fn (instance mut BitField) cleartail() { +fn (instance mut BitField) clear_tail() { tail := instance.size % SLOT_SIZE if tail != 0 { // create a mask for the tail diff --git a/vlib/bitfield/bitfield_test.v b/vlib/bitfield/bitfield_test.v index f4deef8af6..71c679418a 100644 --- a/vlib/bitfield/bitfield_test.v +++ b/vlib/bitfield/bitfield_test.v @@ -5,17 +5,17 @@ import time fn test_bf_new_size() { instance := bitfield.new(75) - assert instance.getsize() == 75 + assert instance.get_size() == 75 } fn test_bf_set_clear_toggle_get() { mut instance := bitfield.new(75) - instance.setbit(47) - assert instance.getbit(47) == 1 - instance.clearbit(47) - assert instance.getbit(47) == 0 - instance.togglebit(47) - assert instance.getbit(47) == 1 + instance.set_bit(47) + assert instance.get_bit(47) == 1 + instance.clear_bit(47) + assert instance.get_bit(47) == 0 + instance.toggle_bit(47) + assert instance.get_bit(47) == 1 } fn test_bf_and_not_or_xor() { @@ -26,21 +26,21 @@ fn test_bf_and_not_or_xor() { mut i := 0 for i < len { if rand.next(2) == 1 { - input1.setbit(i) + input1.set_bit(i) } if rand.next(2) == 1{ - input2.setbit(i) + input2.set_bit(i) } i++ } - output1 := bitfield.bfxor(input1, input2) - bfand := bitfield.bfand(input1, input2) - bfor := bitfield.bfor(input1, input2) - bfnot := bitfield.bfnot(bfand) - output2 := bitfield.bfand(bfor, bfnot) + output1 := bitfield.bf_xor(input1, input2) + bf_and := bitfield.bf_and(input1, input2) + bf_or := bitfield.bf_or(input1, input2) + bf_not := bitfield.bf_not(bf_and) + output2 := bitfield.bf_and(bf_or, bf_not) mut result := 1 for i < len { - if output1.getbit(i) != output2.getbit(i) {result = 0} + if output1.get_bit(i) != output2.get_bit(i) {result = 0} } assert result == 1 } @@ -51,11 +51,11 @@ fn test_clone_cmp() { mut input := bitfield.new(len) for i in 0..len { if rand.next(2) == 1 { - input.setbit(i) + input.set_bit(i) } } output := input.clone() - assert output.getsize() == len + assert output.get_size() == len assert input.cmp(output) == true } @@ -65,14 +65,14 @@ fn test_slice_join() { mut input := bitfield.new(len) for i in 0..len { if rand.next(2) == 1 { - input.setbit(i) + input.set_bit(i) } } mut result := 1 for point := 1; point < (len - 1); point++ { // divide a bitfield into two subfields chunk1 := input.slice(0, point) - chunk2 := input.slice(point, input.getsize()) + chunk2 := input.slice(point, input.get_size()) // concatenate them back into one and compare to the original output := bitfield.join(chunk1, chunk2) if !input.cmp(output) { @@ -82,18 +82,18 @@ fn test_slice_join() { assert result == 1 } -fn test_popcount() { +fn test_pop_count() { rand.seed(time.now().unix) len := 80 mut count0 := 0 mut input := bitfield.new(len) for i in 0..len { if rand.next(2) == 1 { - input.setbit(i) + input.set_bit(i) count0++ } } - count1 := input.popcount() + count1 := input.pop_count() assert count0 == count1 } @@ -106,16 +106,16 @@ fn test_hamming() { for i in 0..len { match rand.next(4) { 0, 1 { - input1.setbit(i) + input1.set_bit(i) count++ } 2 { - input2.setbit(i) + input2.set_bit(i) count++ } 3 { - input1.setbit(i) - input2.setbit(i) + input1.set_bit(i) + input2.set_bit(i) } else { @@ -130,7 +130,7 @@ fn test_bf_from_bytes() { output := bitfield.from_bytes(input) mut result := 1 for i in 0..input.len * 8 { - if (input[i / 8] >> (i % 8)) & 1 != output.getbit(i) { + if (input[i / 8] >> (i % 8)) & 1 != output.get_bit(i) { result = 0 } } @@ -152,7 +152,7 @@ fn test_bf_from_str() { output := bitfield.from_str(input) mut result := 1 for i in 0..len { - if input[i] != output.getbit(i) + 48 { + if input[i] != output.get_bit(i) + 48 { result = 0 } } @@ -165,12 +165,12 @@ fn test_bf_bf2str() { mut input := bitfield.new(len) for i in 0..len { if rand.next(2) == 1 { - input.setbit(i) + input.set_bit(i) } } mut check := '' for i in 0..len { - if input.getbit(i) == 1 { + if input.get_bit(i) == 1 { check = check + '1' } else { @@ -187,33 +187,33 @@ fn test_bf_bf2str() { assert result == 1 } -fn test_bf_setall() { - rand.seed(time.now().unix) +fn test_bf_set_all() { + rand.seed(time.now().unix) len := 80 mut input := bitfield.new(len) - input.setall() + input.set_all() mut result := 1 for i in 0..len { - if input.getbit(i) != 1 { + if input.get_bit(i) != 1 { result = 0 } } assert result == 1 } -fn test_bf_clearall() { - rand.seed(time.now().unix) +fn test_bf_clear_all() { + rand.seed(time.now().unix) len := 80 mut input := bitfield.new(len) for i in 0..len { if rand.next(2) == 1 { - input.setbit(i) + input.set_bit(i) } } - input.clearall() + input.clear_all() mut result := 1 for i in 0..len { - if input.getbit(i) != 0 { + if input.get_bit(i) != 0 { result = 0 } } @@ -226,14 +226,14 @@ fn test_bf_reverse() { mut input := bitfield.new(len) for i in 0..len { if rand.next(2) == 1 { - input.setbit(i) + input.set_bit(i) } } check := input.clone() output := input.reverse() mut result := 1 for i in 0..len { - if output.getbit(i) != check.getbit(len - i - 1) { + if output.get_bit(i) != check.get_bit(len - i - 1) { result = 0 } } @@ -246,9 +246,9 @@ fn test_bf_resize() { mut input := bitfield.new(rand.next(len) + 1) for _ in 0..100 { input.resize(rand.next(len) + 1) - input.setbit(input.getsize() - 1) + input.set_bit(input.get_size() - 1) } - assert input.getbit(input.getsize() - 1) == 1 + assert input.get_bit(input.get_size() - 1) == 1 } fn test_bf_pos() { @@ -270,13 +270,13 @@ fn test_bf_pos() { // fill the needle with random values for k in 0..i { if rand.next(2) == 1 { - needle.setbit(k) + needle.set_bit(k) } } // make sure the needle contains at least one set bit, selected randomly r := rand.next(i) - needle.setbit(r) + needle.set_bit(r) // create the haystack, make sure it contains the needle mut haystack := needle.clone() @@ -311,10 +311,10 @@ fn test_bf_rotate() { for i := 1; i < 80 && result == 1; i++ { mut chunk1 := bitfield.new(i) chunk2 := bitfield.new(len - i) - chunk1.setall() + chunk1.set_all() input := bitfield.join(chunk1, chunk2) output := input.rotate(i) - if output.getbit(len - i - 1) != 0 || output.getbit(len - i) != 1 { + if output.get_bit(len - i - 1) != 0 || output.get_bit(len - i) != 1 { result = 0 } } @@ -327,7 +327,7 @@ fn test_bf_printing(){ mut input := bitfield.new(len) for i in 0..len { if rand.next(2) == 0 { - input.setbit(i) + input.set_bit(i) } } // the following should convert the bitfield input into a string automatically