-
Notifications
You must be signed in to change notification settings - Fork 100
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: inotflying <[email protected]>
- Loading branch information
1 parent
268adeb
commit e181313
Showing
27 changed files
with
352 additions
and
51 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package protocol | ||
|
||
import "math/big" | ||
|
||
// Bitset is a representation of std::bitset<size> being sent over the network, allowing for more than 64 bits | ||
// to be stored in a single integer. A Bitset has a fixed size, which is set at creation time. | ||
type Bitset struct { | ||
size int | ||
int *big.Int | ||
} | ||
|
||
// NewBitset creates a new Bitset with a specific size. The size is the amount of bits that the Bitset can | ||
// store. Attempting to set a bit at an index higher than the size will panic. | ||
func NewBitset(size int) Bitset { | ||
return Bitset{size: size, int: new(big.Int)} | ||
} | ||
|
||
// Set sets a bit at a specific index in the Bitset. If the index is higher than the size of the Bitset, a | ||
// panic will occur. | ||
func (b Bitset) Set(i int) { | ||
if i >= b.size { | ||
panic("index out of bounds") | ||
} | ||
b.int.SetBit(b.int, i, 1) | ||
} | ||
|
||
// Unset unsets a bit at a specific index in the Bitset. If the index is higher than the size of the Bitset, | ||
// a panic will occur. | ||
func (b Bitset) Unset(i int) { | ||
if i >= b.size { | ||
panic("index out of bounds") | ||
} | ||
b.int.SetBit(b.int, i, 0) | ||
} | ||
|
||
// Load returns if a bit at a specific index in the Bitset is set. If the index is higher than the size of the | ||
// Bitset, a panic will occur. | ||
func (b Bitset) Load(i int) bool { | ||
if i >= b.size { | ||
panic("index out of bounds") | ||
} | ||
return b.int.Bit(i) == 1 | ||
} |
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
Oops, something went wrong.