Improved catching up and refactored
This commit is contained in:
@ -9,6 +9,22 @@ import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
type Bytes []byte
|
||||
|
||||
func (self Bytes) String() string {
|
||||
return string(self)
|
||||
}
|
||||
|
||||
func DeleteFromByteSlice(s [][]byte, hash []byte) [][]byte {
|
||||
for i, h := range s {
|
||||
if bytes.Compare(h, hash) == 0 {
|
||||
return append(s[:i], s[i+1:]...)
|
||||
}
|
||||
}
|
||||
|
||||
return s
|
||||
}
|
||||
|
||||
// Number to bytes
|
||||
//
|
||||
// Returns the number in bytes with the specified base
|
||||
|
@ -124,6 +124,8 @@ func Encode(object interface{}) []byte {
|
||||
} else {
|
||||
buff.Write(Encode(t.Bytes()))
|
||||
}
|
||||
case Bytes:
|
||||
buff.Write(Encode([]byte(t)))
|
||||
case []byte:
|
||||
if len(t) == 1 && t[0] <= 0x7f {
|
||||
buff.Write(t)
|
||||
|
@ -4,9 +4,13 @@ type Settable interface {
|
||||
AsSet() UniqueSet
|
||||
}
|
||||
|
||||
type UniqueSet map[interface{}]struct{}
|
||||
type Stringable interface {
|
||||
String() string
|
||||
}
|
||||
|
||||
func NewSet(v ...interface{}) UniqueSet {
|
||||
type UniqueSet map[string]struct{}
|
||||
|
||||
func NewSet(v ...Stringable) UniqueSet {
|
||||
set := make(UniqueSet)
|
||||
for _, val := range v {
|
||||
set.Insert(val)
|
||||
@ -15,14 +19,14 @@ func NewSet(v ...interface{}) UniqueSet {
|
||||
return set
|
||||
}
|
||||
|
||||
func (self UniqueSet) Insert(k interface{}) UniqueSet {
|
||||
self[k] = struct{}{}
|
||||
func (self UniqueSet) Insert(k Stringable) UniqueSet {
|
||||
self[k.String()] = struct{}{}
|
||||
|
||||
return self
|
||||
}
|
||||
|
||||
func (self UniqueSet) Include(k interface{}) bool {
|
||||
_, ok := self[k]
|
||||
func (self UniqueSet) Include(k Stringable) bool {
|
||||
_, ok := self[k.String()]
|
||||
|
||||
return ok
|
||||
}
|
||||
|
Reference in New Issue
Block a user