First iteration of the immutable AVL tree

This commit is contained in:
Jae Kwon
2014-05-19 20:46:41 -07:00
commit 35b352afb0
8 changed files with 907 additions and 0 deletions
+57
View File
@@ -0,0 +1,57 @@
package merkle
import "bytes"
type String string
type ByteSlice []byte
func (self String) Equals(other Sortable) bool {
if o, ok := other.(String); ok {
return self == o
} else {
return false
}
}
func (self String) Less(other Sortable) bool {
if o, ok := other.(String); ok {
return self < o
} else {
return false
}
}
func (self String) Hash() int {
bytes := []byte(self)
hash := 0
for i, c := range bytes {
hash += (i+1)*int(c)
}
return hash
}
func (self ByteSlice) Equals(other Sortable) bool {
if o, ok := other.(ByteSlice); ok {
return bytes.Equal(self, o)
} else {
return false
}
}
func (self ByteSlice) Less(other Sortable) bool {
if o, ok := other.(ByteSlice); ok {
return bytes.Compare(self, o) < 0 // -1 if a < b
} else {
return false
}
}
func (self ByteSlice) Hash() int {
hash := 0
for i, c := range self {
hash += (i+1)*int(c)
}
return hash
}