-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
14 changed files
with
383 additions
and
114 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
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
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
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,52 @@ | ||
package dhcpd | ||
|
||
const bitsPerWord = 64 | ||
|
||
// bitSet is a sparse bitSet. A nil *bitSet is an empty bitSet. | ||
type bitSet struct { | ||
words map[uint64]uint64 | ||
} | ||
|
||
// newBitSet returns a new bitset. | ||
func newBitSet() (s *bitSet) { | ||
return &bitSet{ | ||
words: map[uint64]uint64{}, | ||
} | ||
} | ||
|
||
// isSet returns true if the bit n is set. | ||
func (s *bitSet) isSet(n uint64) (ok bool) { | ||
if s == nil { | ||
return false | ||
} | ||
|
||
wordIdx := n / bitsPerWord | ||
bitIdx := n % bitsPerWord | ||
|
||
var word uint64 | ||
word, ok = s.words[wordIdx] | ||
if !ok { | ||
return false | ||
} | ||
|
||
return word&(1<<bitIdx) != 0 | ||
} | ||
|
||
// set sets or unsets a bit. | ||
func (s *bitSet) set(n uint64, ok bool) { | ||
if s == nil { | ||
return | ||
} | ||
|
||
wordIdx := n / bitsPerWord | ||
bitIdx := n % bitsPerWord | ||
|
||
word := s.words[wordIdx] | ||
if ok { | ||
word |= 1 << bitIdx | ||
} else { | ||
word &^= 1 << bitIdx | ||
} | ||
|
||
s.words[wordIdx] = word | ||
} |
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,90 @@ | ||
package dhcpd | ||
|
||
import ( | ||
"math" | ||
"testing" | ||
"testing/quick" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestBitSet(t *testing.T) { | ||
t.Run("nil", func(t *testing.T) { | ||
var s *bitSet | ||
|
||
ok := s.isSet(0) | ||
assert.False(t, ok) | ||
|
||
assert.NotPanics(t, func() { | ||
s.set(0, true) | ||
}) | ||
|
||
ok = s.isSet(0) | ||
assert.False(t, ok) | ||
|
||
assert.NotPanics(t, func() { | ||
s.set(0, false) | ||
}) | ||
|
||
ok = s.isSet(0) | ||
assert.False(t, ok) | ||
}) | ||
|
||
t.Run("non_nil", func(t *testing.T) { | ||
s := newBitSet() | ||
|
||
ok := s.isSet(0) | ||
assert.False(t, ok) | ||
|
||
s.set(0, true) | ||
|
||
ok = s.isSet(0) | ||
assert.True(t, ok) | ||
|
||
s.set(0, false) | ||
|
||
ok = s.isSet(0) | ||
assert.False(t, ok) | ||
}) | ||
|
||
t.Run("non_nil_long", func(t *testing.T) { | ||
s := newBitSet() | ||
|
||
s.set(0, true) | ||
s.set(math.MaxUint64, true) | ||
assert.Len(t, s.words, 2) | ||
|
||
ok := s.isSet(0) | ||
assert.True(t, ok) | ||
|
||
ok = s.isSet(math.MaxUint64) | ||
assert.True(t, ok) | ||
}) | ||
|
||
t.Run("quick", func(t *testing.T) { | ||
m := map[uint64]struct{}{} | ||
s := newBitSet() | ||
|
||
mapFunc := func(setNew, checkOld, delOld uint64) (ok bool) { | ||
m[setNew] = struct{}{} | ||
delete(m, delOld) | ||
_, ok = m[checkOld] | ||
|
||
return ok | ||
} | ||
|
||
setFunc := func(setNew, checkOld, delOld uint64) (ok bool) { | ||
s.set(setNew, true) | ||
s.set(delOld, false) | ||
ok = s.isSet(checkOld) | ||
|
||
return ok | ||
} | ||
|
||
err := quick.CheckEqual(mapFunc, setFunc, &quick.Config{ | ||
MaxCount: 10_000, | ||
MaxCountScale: 10, | ||
}) | ||
assert.NoError(t, err) | ||
}) | ||
} |
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
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
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
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
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
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
Large diffs are not rendered by default.
Oops, something went wrong.
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
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