From 9a3baf56099ea8811ba51b6d609623e8ed638fc0 Mon Sep 17 00:00:00 2001 From: Vitalie Ciubotaru Date: Mon, 15 Jul 2019 00:56:48 +0900 Subject: [PATCH] bf: add setall(), clearall(), string() and str2bf() --- vlib/bf/bf.v | 36 ++++++++++++++++++++ vlib/bf/bf_test.v | 83 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 119 insertions(+) diff --git a/vlib/bf/bf.v b/vlib/bf/bf.v index 0f0d2c3462..94c527afbc 100644 --- a/vlib/bf/bf.v +++ b/vlib/bf/bf.v @@ -64,6 +64,29 @@ fn cleartail(instance BitField) { // public functions +pub fn str2bf(input string) BitField { + mut output := new(input.len) + for i := 0; i < input.len; i++ { + if input[i] != 48 { + output.setbit(i) + } + } + return output +} + +pub fn (input BitField) string() string { + mut output := '' + for i := 0; i < input.size; i++ { + if input.getbit(i) == 1 { + output = output + '1' + } + else { + output = output + '0' + } + } + return output +} + pub fn new(size int) BitField { output := BitField{ size: size @@ -93,6 +116,19 @@ pub fn (instance mut BitField) clearbit(bitnr int) { bitclear(instance, bitnr) } +pub fn (instance mut BitField) setall() { + for i := 0; i < bitnslots(instance.size); i++ { + instance.field[i] = u32(-1) + } + cleartail(instance) +} + +pub fn (instance mut BitField) clearall() { + for i := 0; i < bitnslots(instance.size); i++ { + instance.field[i] = u32(0) + } +} + pub fn (instance mut BitField) togglebit(bitnr int) { if bitnr >= instance.size {return} bittoggle(instance, bitnr) diff --git a/vlib/bf/bf_test.v b/vlib/bf/bf_test.v index ab39dfb815..719c14ac96 100644 --- a/vlib/bf/bf_test.v +++ b/vlib/bf/bf_test.v @@ -119,3 +119,86 @@ fn test_hamming() { } assert count == bf.hamming(input1, input2) } + +fn test_bf_str2bf() { + rand.seed(time.now().uni) + len := 80 + mut input := '' + for i := 0; i < len; i++ { + if rand.next(2) == 1 { + input = input + '1' + } + else { + input = input + '0' + } + } + output := bf.str2bf(input) + mut result := 1 + for i := 0; i < len; i++ { + if input[i] != output.getbit(i) + 48 { + result = 0 + } + } + assert result == 1 +} + +fn test_bf_bf2str() { + rand.seed(time.now().uni) + len := 80 + mut input := bf.new(len) + for i := 0; i < len; i++ { + if rand.next(2) == 1 { + input.setbit(i) + } + } + mut check := '' + for i := 0; i < len; i++ { + if input.getbit(i) == 1 { + check = check + '1' + } + else { + check = check + '0' + } + } + output := input.string() + mut result := 1 + for i := 0; i < len; i++ { + if check[i] != output[i] { + result = 0 + } + } + assert result == 1 +} + +fn test_bf_setall() { + rand.seed(time.now().uni) + len := 80 + mut input := bf.new(len) + input.setall() + mut result := 1 + for i := 0; i < len; i++ { + if input.getbit(i) != 1 { + result = 0 + } + } + assert result == 1 +} + +fn test_bf_clearall() { + rand.seed(time.now().uni) + len := 80 + mut input := bf.new(len) + for i := 0; i < len; i++ { + if rand.next(2) == 1 { + input.setbit(i) + } + } + input.clearall() + mut result := 1 + for i := 0; i < len; i++ { + if input.getbit(i) != 0 { + result = 0 + } + } + assert result == 1 +}