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

cleanup: replace C for loops with range

This commit is contained in:
spaceface777
2020-02-24 17:55:16 +01:00
committed by GitHub
parent 5918946feb
commit ef8c1203b4
50 changed files with 168 additions and 170 deletions

View File

@@ -91,7 +91,7 @@ pub fn from_bytes(input []byte) BitField {
pub fn from_string(input string) BitField {
mut output := new(input.len)
for i := 0; i < input.len; i++ {
for i in 0..input.len {
if input[i] != 48 {
output.setbit(i)
}
@@ -104,7 +104,7 @@ pub fn from_string(input string) BitField {
pub fn (input BitField) string() string {
mut output := ''
for i := 0; i < input.size; i++ {
for i in 0..input.size {
if input.getbit(i) == 1 {
output = output + '1'
}
@@ -157,7 +157,7 @@ pub fn (instance mut BitField) clearbit(bitnr int) {
// setall() sets all bits in the array to 1
pub fn (instance mut BitField) setall() {
for i := 0; i < bitnslots(instance.size); i++ {
for i in 0..bitnslots(instance.size) {
instance.field[i] = u32(-1)
}
cleartail(mut instance)
@@ -166,7 +166,7 @@ pub fn (instance mut BitField) setall() {
// clearall() clears (sets to zero) all bits in the array
pub fn (instance mut BitField) clearall() {
for i := 0; i < bitnslots(instance.size); i++ {
for i in 0..bitnslots(instance.size) {
instance.field[i] = u32(0)
}
}
@@ -251,7 +251,7 @@ 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
for i := 0; i < bitnslots(input1.size); i++ {
for i in 0..bitnslots(input1.size) {
output.field[i] = input1.field[i]
}
@@ -259,7 +259,7 @@ pub fn join(input1 BitField, input2 BitField) BitField {
offset_bit := input1.size % SLOT_SIZE
offset_slot := input1.size / SLOT_SIZE
for i := 0; i < bitnslots(input2.size); i++ {
for i in 0..bitnslots(input2.size) {
output.field[i + offset_slot] |=
u32(input2.field[i] << u32(offset_bit))
}
@@ -278,12 +278,12 @@ pub fn join(input1 BitField, input2 BitField) BitField {
* If offset_bit is zero, no additional copies needed.
*/
if (output_size - 1) % SLOT_SIZE < (input2.size - 1) % SLOT_SIZE {
for i := 0; i < bitnslots(input2.size); i++ {
for i in 0..bitnslots(input2.size) {
output.field[i + offset_slot + 1] |=
u32(input2.field[i] >> u32(SLOT_SIZE - offset_bit))
}
} else if (output_size - 1) % SLOT_SIZE > (input2.size - 1) % SLOT_SIZE {
for i := 0; i < bitnslots(input2.size) - 1; i++ {
for i in 0..bitnslots(input2.size) - 1 {
output.field[i + offset_slot + 1] |=
u32(input2.field[i] >> u32(SLOT_SIZE - offset_bit))
}
@@ -331,7 +331,7 @@ pub fn clone(input BitField) BitField {
pub fn cmp(input1 BitField, input2 BitField) bool {
if input1.size != input2.size {return false}
for i := 0; i < bitnslots(input1.size); i++ {
for i in 0..bitnslots(input1.size) {
if input1.field[i] != input2.field[i] {return false}
}
return true
@@ -344,14 +344,14 @@ pub fn (instance BitField) popcount() int {
bitnslots := bitnslots(size)
tail := size % SLOT_SIZE
mut count := 0
for i := 0; i < bitnslots - 1; i++ {
for j := 0; j < SLOT_SIZE; j++ {
for i in 0..bitnslots - 1 {
for j in 0..SLOT_SIZE {
if u32(instance.field[i] >> u32(j)) & u32(1) == u32(1) {
count++
}
}
}
for j := 0; j < tail; j++ {
for j in 0..tail {
if u32(instance.field[bitnslots - 1] >> u32(j)) & u32(1) == u32(1) {
count++
}
@@ -412,7 +412,7 @@ pub fn (input BitField) slice(_start int, _end int) BitField {
if output_slots > 1 {
if start_offset != 0 {
for i := 0; i < output_slots - 1; i++ {
for i in 0..output_slots - 1 {
output.field[i] =
u32(input.field[start_slot + i] >> u32(start_offset))
output.field[i] = output.field[i] |
@@ -421,7 +421,7 @@ pub fn (input BitField) slice(_start int, _end int) BitField {
}
}
else {
for i := 0; i < output_slots - 1; i++ {
for i in 0..output_slots - 1 {
output.field[i] =
u32(input.field[start_slot + i])
}
@@ -465,14 +465,14 @@ pub fn (instance BitField) reverse() BitField {
bitnslots := bitnslots(size)
mut output := new(size)
for i:= 0; i < (bitnslots - 1); i++ {
for j := 0; j < SLOT_SIZE; j++ {
for j in 0..SLOT_SIZE {
if u32(instance.field[i] >> u32(j)) & u32(1) == u32(1) {
bitset(mut output, size - i * SLOT_SIZE - j - 1)
}
}
}
bits_in_last_input_slot := (size - 1) % SLOT_SIZE + 1
for j := 0; j < bits_in_last_input_slot; j++ {
for j in 0..bits_in_last_input_slot {
if u32(instance.field[bitnslots - 1] >> u32(j)) & u32(1) == u32(1) {
bitset(mut output, bits_in_last_input_slot - j - 1)
}

View File

@@ -49,7 +49,7 @@ fn test_clone_cmp() {
rand.seed(time.now().unix)
len := 80
mut input := bitfield.new(len)
for i := 0; i < len; i++ {
for i in 0..len {
if rand.next(2) == 1 {
input.setbit(i)
}
@@ -63,7 +63,7 @@ fn test_slice_join() {
rand.seed(time.now().unix)
len := 80
mut input := bitfield.new(len)
for i := 0; i < len; i++ {
for i in 0..len {
if rand.next(2) == 1 {
input.setbit(i)
}
@@ -87,7 +87,7 @@ fn test_popcount() {
len := 80
mut count0 := 0
mut input := bitfield.new(len)
for i := 0; i < len; i++ {
for i in 0..len {
if rand.next(2) == 1 {
input.setbit(i)
count0++
@@ -103,7 +103,7 @@ fn test_hamming() {
mut count := 0
mut input1 := bitfield.new(len)
mut input2 := bitfield.new(len)
for i := 0; i < len; i++ {
for i in 0..len {
match rand.next(4) {
0, 1 {
input1.setbit(i)
@@ -129,7 +129,7 @@ fn test_bf_from_bytes() {
input := [byte(0xF0), byte(0x0F), byte(0xF0), byte(0xFF)]
output := bitfield.from_bytes(input)
mut result := 1
for i := 0; i < input.len * 8; i++ {
for i in 0..input.len * 8 {
if (input[i / 8] >> (i % 8)) & 1 != output.getbit(i) {
result = 0
}
@@ -141,7 +141,7 @@ fn test_bf_from_string() {
rand.seed(time.now().unix)
len := 80
mut input := ''
for i := 0; i < len; i++ {
for i in 0..len {
if rand.next(2) == 1 {
input = input + '1'
}
@@ -151,7 +151,7 @@ fn test_bf_from_string() {
}
output := bitfield.from_string(input)
mut result := 1
for i := 0; i < len; i++ {
for i in 0..len {
if input[i] != output.getbit(i) + 48 {
result = 0
}
@@ -163,13 +163,13 @@ fn test_bf_bf2str() {
rand.seed(time.now().unix)
len := 80
mut input := bitfield.new(len)
for i := 0; i < len; i++ {
for i in 0..len {
if rand.next(2) == 1 {
input.setbit(i)
}
}
mut check := ''
for i := 0; i < len; i++ {
for i in 0..len {
if input.getbit(i) == 1 {
check = check + '1'
}
@@ -179,7 +179,7 @@ fn test_bf_bf2str() {
}
output := input.string()
mut result := 1
for i := 0; i < len; i++ {
for i in 0..len {
if check[i] != output[i] {
result = 0
}
@@ -193,7 +193,7 @@ fn test_bf_setall() {
mut input := bitfield.new(len)
input.setall()
mut result := 1
for i := 0; i < len; i++ {
for i in 0..len {
if input.getbit(i) != 1 {
result = 0
}
@@ -205,14 +205,14 @@ fn test_bf_clearall() {
rand.seed(time.now().unix)
len := 80
mut input := bitfield.new(len)
for i := 0; i < len; i++ {
for i in 0..len {
if rand.next(2) == 1 {
input.setbit(i)
}
}
input.clearall()
mut result := 1
for i := 0; i < len; i++ {
for i in 0..len {
if input.getbit(i) != 0 {
result = 0
}
@@ -224,7 +224,7 @@ fn test_bf_reverse() {
rand.seed(time.now().unix)
len := 80
mut input := bitfield.new(len)
for i := 0; i < len; i++ {
for i in 0..len {
if rand.next(2) == 1 {
input.setbit(i)
}
@@ -232,7 +232,7 @@ fn test_bf_reverse() {
check := bitfield.clone(input)
output := input.reverse()
mut result := 1
for i := 0; i < len; i++ {
for i in 0..len {
if output.getbit(i) != check.getbit(len - i - 1) {
result = 0
}
@@ -244,7 +244,7 @@ fn test_bf_resize() {
rand.seed(time.now().unix)
len := 80
mut input := bitfield.new(rand.next(len) + 1)
for i := 0; i < 100; i++ {
for i in 0..100 {
input.resize(rand.next(len) + 1)
input.setbit(input.getsize() - 1)
}
@@ -263,12 +263,12 @@ fn test_bf_pos() {
len := 80
mut result := 1
for i := 1; i < len; i++ { // needle size
for j := 0; j < len - i; j++ { // needle position in the haystack
for j in 0..len - i { // needle position in the haystack
// create the needle
mut needle := bitfield.new(i)
// fill the needle with random values
for k := 0; k < i; k++ {
for k in 0..i {
if rand.next(2) == 1 {
needle.setbit(k)
}