From ef899f77f07d988bf29a8ff0ff7b15b9be2f509b Mon Sep 17 00:00:00 2001 From: Richard Patel Date: Thu, 8 Sep 2022 11:19:40 +0200 Subject: [PATCH] gossip: move BitVec to bitvec.go --- pkg/gossip/bitvec.go | 64 ++++++++++++++++++++++++++++++++++++++++++++ pkg/gossip/bloom.go | 37 ------------------------- 2 files changed, 64 insertions(+), 37 deletions(-) create mode 100644 pkg/gossip/bitvec.go diff --git a/pkg/gossip/bitvec.go b/pkg/gossip/bitvec.go new file mode 100644 index 0000000..a990256 --- /dev/null +++ b/pkg/gossip/bitvec.go @@ -0,0 +1,64 @@ +package gossip + +func MakeBitVecU8(bits []byte, len int) BitVecU8 { + if len <= 0 { + return BitVecU8{ + Bits: BitVecU8Inner{Value: nil}, + Len: 0, + } + } + return BitVecU8{ + Bits: BitVecU8Inner{Value: &bits}, + Len: uint64(len), + } +} + +func (bv *BitVecU8) Get(pos uint64) bool { + if pos >= bv.Len { + panic("get bit out of bounds") + } + return (*bv.Bits.Value)[pos/8]&(1<<(pos%8)) != 0 +} + +func (bv *BitVecU8) Set(pos uint64, b bool) { + if pos >= bv.Len { + panic("get bit out of bounds") + } + if b { + (*bv.Bits.Value)[pos/8] |= 1 << (pos % 8) + } else { + (*bv.Bits.Value)[pos/8] &= ^uint8(1 << (pos % 8)) + } +} + +func MakeBitVecU64(bits []uint64, len int) BitVecU64 { + if len <= 0 { + return BitVecU64{ + Bits: BitVecU64Inner{Value: nil}, + Len: 0, + } + } + return BitVecU64{ + Bits: BitVecU64Inner{Value: &bits}, + Len: uint64(len), + } +} + +func (bv *BitVecU64) Get(pos uint64) bool { + if pos >= bv.Len { + panic("get bit out of bounds") + } + return (*bv.Bits.Value)[pos/64]&(1<<(pos%64)) != 0 +} + +func (bv *BitVecU64) Set(pos uint64, b bool) { + if pos >= bv.Len { + panic("get bit out of bounds") + } + bits := *bv.Bits.Value + if b { + bits[pos/64] |= 1 << (pos % 64) + } else { + bits[pos/64] &= ^uint64(1 << (pos % 64)) + } +} diff --git a/pkg/gossip/bloom.go b/pkg/gossip/bloom.go index 01578a5..66938af 100644 --- a/pkg/gossip/bloom.go +++ b/pkg/gossip/bloom.go @@ -93,40 +93,3 @@ func FNV1a(slice []byte, hash uint64) uint64 { } return hash } - -func (bv *BitVecU8) Get(pos uint64) bool { - if pos >= bv.Len { - panic("get bit out of bounds") - } - return (*bv.Bits.Value)[pos/8]&(1<<(pos%8)) != 0 -} - -func (bv *BitVecU8) Set(pos uint64, b bool) { - if pos >= bv.Len { - panic("get bit out of bounds") - } - if b { - (*bv.Bits.Value)[pos/8] |= 1 << (pos % 8) - } else { - (*bv.Bits.Value)[pos/8] &= ^uint8(1 << (pos % 8)) - } -} - -func (bv *BitVecU64) Get(pos uint64) bool { - if pos >= bv.Len { - panic("get bit out of bounds") - } - return (*bv.Bits.Value)[pos/64]&(1<<(pos%64)) != 0 -} - -func (bv *BitVecU64) Set(pos uint64, b bool) { - if pos >= bv.Len { - panic("get bit out of bounds") - } - bits := *bv.Bits.Value - if b { - bits[pos/64] |= 1 << (pos % 64) - } else { - bits[pos/64] &= ^uint64(1 << (pos % 64)) - } -}