-
Notifications
You must be signed in to change notification settings - Fork 121
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
a better fix for #21; track ncs in a bitset which allows for detectin…
…g replays even if ncs arrive out of order
- Loading branch information
Kevin Manley
committed
Jan 8, 2016
1 parent
538147e
commit e837317
Showing
4 changed files
with
260 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
// Package bitset implments a memory efficient bit array of booleans | ||
// Adapted from https://github.com/lazybeaver/bitset | ||
|
||
package auth | ||
|
||
import "fmt" | ||
|
||
type BitSet struct { | ||
bits []uint8 | ||
size uint64 | ||
} | ||
|
||
const ( | ||
bitMaskZero = uint8(0) | ||
bitMaskOnes = uint8((1 << 8) - 1) | ||
) | ||
|
||
var ( | ||
bitMasks = [...]uint8{0x1, 0x2, 0x4, 0x8, 0x10, 0x20, 0x40, 0x80} | ||
) | ||
|
||
func (b *BitSet) getPositionAndMask(index uint64) (uint64, uint8) { | ||
if index < 0 || index >= b.size { | ||
panic(fmt.Errorf("BitSet index (%d) out of bounds (size: %d)", index, b.size)) | ||
} | ||
position := index >> 3 | ||
mask := bitMasks[index%8] | ||
return position, mask | ||
} | ||
|
||
func (b *BitSet) Init(size uint64) { | ||
b.bits = make([]uint8, (size+7)/8) | ||
b.size = size | ||
} | ||
|
||
func (b *BitSet) Size() uint64 { | ||
return b.size | ||
} | ||
|
||
func (b *BitSet) Get(index uint64) bool { | ||
position, mask := b.getPositionAndMask(index) | ||
return (b.bits[position] & mask) != 0 | ||
} | ||
|
||
func (b *BitSet) Set(index uint64) { | ||
position, mask := b.getPositionAndMask(index) | ||
b.bits[position] |= mask | ||
} | ||
|
||
func (b *BitSet) Clear(index uint64) { | ||
position, mask := b.getPositionAndMask(index) | ||
b.bits[position] &^= mask | ||
} | ||
|
||
func (b *BitSet) String() string { | ||
value := make([]byte, b.size) | ||
var i uint64 | ||
for i = 0; i < b.size; i++ { | ||
if b.Get(i) { | ||
value[i] = '1' | ||
} else { | ||
value[i] = '0' | ||
} | ||
} | ||
return string(value) | ||
} | ||
|
||
func NewBitSet(size uint64) *BitSet { | ||
b := &BitSet{} | ||
b.Init(size) | ||
return b | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package auth | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestNew(t *testing.T) { | ||
var size uint64 = 101 | ||
bs := NewBitSet(size) | ||
if bs.Size() != size { | ||
t.Errorf("Unexpected initialization failure") | ||
} | ||
var i uint64 | ||
for i = 0; i < size; i++ { | ||
if bs.Get(i) { | ||
t.Errorf("Newly initialized bitset cannot have true values") | ||
} | ||
} | ||
} | ||
|
||
func TestGet(t *testing.T) { | ||
bs := NewBitSet(2) | ||
bs.Set(0) | ||
bs.Clear(1) | ||
if bs.Get(0) != true { | ||
t.Errorf("Actual: false | Expected: true") | ||
} | ||
if bs.Get(1) != false { | ||
t.Errorf("Actual: true | Expected: false") | ||
} | ||
} | ||
|
||
func TestSet(t *testing.T) { | ||
bs := NewBitSet(10) | ||
bs.Set(2) | ||
bs.Set(3) | ||
bs.Set(5) | ||
bs.Set(7) | ||
actual := bs.String() | ||
expected := "0011010100" | ||
if actual != expected { | ||
t.Errorf("Actual: %s | Expected: %s", actual, expected) | ||
} | ||
} | ||
|
||
func TestClear(t *testing.T) { | ||
bs := NewBitSet(10) | ||
var i uint64 | ||
for i = 0; i < 10; i++ { | ||
bs.Set(i) | ||
} | ||
bs.Clear(0) | ||
bs.Clear(3) | ||
bs.Clear(6) | ||
bs.Clear(9) | ||
actual := bs.String() | ||
expected := "0110110110" | ||
if actual != expected { | ||
t.Errorf("Actual: %s | Expected: %s", actual, expected) | ||
} | ||
} | ||
|
||
func BenchmarkGet(b *testing.B) { | ||
bn := uint64(b.N) | ||
bs := NewBitSet(bn) | ||
var i uint64 | ||
for i = 0; i < bn; i++ { | ||
_ = bs.Get(i) | ||
} | ||
} | ||
|
||
func BenchmarkSet(b *testing.B) { | ||
bn := uint64(b.N) | ||
bs := NewBitSet(bn) | ||
var i uint64 | ||
for i = 0; i < bn; i++ { | ||
bs.Set(i) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.