forked from Hyperledger-TWGC/tape
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
BlockCollector detects repeated block (Hyperledger-TWGC#172)
- Loading branch information
Showing
9 changed files
with
278 additions
and
267 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package bitmap | ||
|
||
import "github.com/pkg/errors" | ||
|
||
type BitMap struct { | ||
count int // number of bits set | ||
capability int // total number of bits | ||
bits []uint64 | ||
} | ||
|
||
// Has determine whether the specified position is set | ||
func (b *BitMap) Has(num int) bool { | ||
if num >= b.capability { | ||
return false | ||
} | ||
c, bit := num/64, uint(num%64) | ||
return (c < len(b.bits)) && (b.bits[c]&(1<<bit) != 0) | ||
} | ||
|
||
// Set set the specified position | ||
// If the position has been set or exceeds the maximum number of bits, set is a no-op. | ||
func (b *BitMap) Set(num int) { | ||
if b.Has(num) { | ||
return | ||
} | ||
if b.capability <= num { | ||
return | ||
} | ||
|
||
c, bit := num/64, uint(num%64) | ||
b.bits[c] |= 1 << bit | ||
b.count++ | ||
return | ||
} | ||
|
||
func (b *BitMap) Count() int { | ||
return b.count | ||
} | ||
|
||
func (b *BitMap) Cap() int { | ||
return b.capability | ||
} | ||
|
||
func (b *BitMap) BitsLen() int { | ||
return len(b.bits) | ||
} | ||
|
||
// NewBitsMap create a new BitsMap | ||
func NewBitMap(cap int) (BitMap, error) { | ||
if cap < 1 { | ||
return BitMap{}, errors.New("cap should not be less than 1") | ||
} | ||
bitsLen := cap / 64 | ||
if cap%64 > 0 { | ||
bitsLen++ | ||
} | ||
|
||
return BitMap{bits: make([]uint64, bitsLen), capability: cap}, nil | ||
} |
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,13 @@ | ||
package bitmap_test | ||
|
||
import ( | ||
"testing" | ||
|
||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
func TestBitmap(t *testing.T) { | ||
RegisterFailHandler(Fail) | ||
RunSpecs(t, "Bitmap Suite") | ||
} |
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,91 @@ | ||
package bitmap_test | ||
|
||
import ( | ||
"tape/pkg/infra/bitmap" | ||
|
||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
var _ = Describe("Bitmap", func() { | ||
|
||
Context("New BitsMap", func() { | ||
It("the environment is properly set", func() { | ||
b, err := bitmap.NewBitMap(4) | ||
Expect(err).To(BeNil()) | ||
Expect(b.Cap()).To(Equal(4)) | ||
Expect(b.Count()).To(Equal(0)) | ||
Expect(b.BitsLen()).To(Equal(1)) | ||
|
||
b, err = bitmap.NewBitMap(65) | ||
Expect(err).To(BeNil()) | ||
Expect(b.Cap()).To(Equal(65)) | ||
Expect(b.Count()).To(Equal(0)) | ||
Expect(b.BitsLen()).To(Equal(2)) | ||
}) | ||
|
||
It("should error which cap is less than 1", func() { | ||
_, err := bitmap.NewBitMap(0) | ||
Expect(err).NotTo(BeNil()) | ||
|
||
_, err = bitmap.NewBitMap(-1) | ||
Expect(err).NotTo(BeNil()) | ||
}) | ||
}) | ||
|
||
Context("Operate BitsMap", func() { | ||
It("the len of bits is just one ", func() { | ||
b, err := bitmap.NewBitMap(4) | ||
Expect(err).To(BeNil()) | ||
b.Set(0) | ||
Expect(b.Count()).To(Equal(1)) | ||
b.Set(2) | ||
Expect(b.Count()).To(Equal(2)) | ||
ok := b.Has(0) | ||
Expect(ok).To(BeTrue()) | ||
ok = b.Has(2) | ||
Expect(ok).To(BeTrue()) | ||
ok = b.Has(1) | ||
Expect(ok).To(BeFalse()) | ||
ok = b.Has(4) | ||
Expect(ok).To(BeFalse()) | ||
|
||
b.Set(4) | ||
Expect(b.Count()).To(Equal(2)) | ||
b.Set(2) | ||
Expect(b.Count()).To(Equal(2)) | ||
}) | ||
|
||
It("the len of bits is more than one", func() { | ||
b, err := bitmap.NewBitMap(80) | ||
Expect(err).To(BeNil()) | ||
b.Set(0) | ||
Expect(b.Count()).To(Equal(1)) | ||
b.Set(2) | ||
Expect(b.Count()).To(Equal(2)) | ||
b.Set(70) | ||
Expect(b.Count()).To(Equal(3)) | ||
b.Set(79) | ||
Expect(b.Count()).To(Equal(4)) | ||
ok := b.Has(0) | ||
Expect(ok).To(BeTrue()) | ||
ok = b.Has(2) | ||
Expect(ok).To(BeTrue()) | ||
ok = b.Has(70) | ||
Expect(ok).To(BeTrue()) | ||
ok = b.Has(79) | ||
Expect(ok).To(BeTrue()) | ||
ok = b.Has(1) | ||
Expect(ok).To(BeFalse()) | ||
ok = b.Has(3) | ||
Expect(ok).To(BeFalse()) | ||
ok = b.Has(69) | ||
Expect(ok).To(BeFalse()) | ||
|
||
b.Set(80) | ||
Expect(b.Count()).To(Equal(4)) | ||
b.Set(2) | ||
Expect(b.Count()).To(Equal(4)) | ||
}) | ||
}) | ||
}) |
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.