|
|
- package common
-
- import (
- "bytes"
- "encoding/binary"
- "sort"
- )
-
- var (
- Zero256 = Word256{0}
- One256 = Word256{1}
- )
-
- type Word256 [32]byte
-
- func (w Word256) String() string { return string(w[:]) }
- func (w Word256) Copy() Word256 { return w }
- func (w Word256) Bytes() []byte { return w[:] } // copied.
- func (w Word256) Prefix(n int) []byte { return w[:n] }
- func (w Word256) Postfix(n int) []byte { return w[32-n:] }
- func (w Word256) IsZero() bool {
- accum := byte(0)
- for _, byt := range w {
- accum |= byt
- }
- return accum == 0
- }
- func (w Word256) Compare(other Word256) int {
- return bytes.Compare(w[:], other[:])
- }
-
- func Uint64ToWord256(i uint64) Word256 {
- word := Word256{}
- buf := [8]byte{}
- PutUint64(buf[:], i)
- word[24], word[25], word[26], word[27] = buf[7], buf[6], buf[5], buf[4]
- word[28], word[29], word[30], word[31] = buf[3], buf[2], buf[1], buf[0]
- return word
- }
-
- func RightPadWord256(bz []byte) (word Word256) {
- copy(word[:], bz)
- return
- }
-
- func LeftPadWord256(bz []byte) (word Word256) {
- copy(word[32-len(bz):], bz)
- return
- }
-
- func Uint64FromWord256(word Word256) uint64 {
- buf := [8]byte{}
- buf[0], buf[1], buf[2], buf[3] = word[31], word[30], word[29], word[28]
- buf[4], buf[5], buf[6], buf[7] = word[27], word[26], word[25], word[24]
- return binary.LittleEndian.Uint64(buf[:])
- }
-
- //-------------------------------------
-
- type Tuple256 struct {
- First Word256
- Second Word256
- }
-
- func (tuple Tuple256) Compare(other Tuple256) int {
- firstCompare := tuple.First.Compare(other.First)
- if firstCompare == 0 {
- return tuple.Second.Compare(other.Second)
- } else {
- return firstCompare
- }
- }
-
- func Tuple256Split(t Tuple256) (Word256, Word256) {
- return t.First, t.Second
- }
-
- type Tuple256Slice []Tuple256
-
- func (p Tuple256Slice) Len() int { return len(p) }
- func (p Tuple256Slice) Less(i, j int) bool {
- return p[i].Compare(p[j]) < 0
- }
- func (p Tuple256Slice) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
- func (p Tuple256Slice) Sort() { sort.Sort(p) }
|