Skip to content

Commit

Permalink
refactor: remove hash
Browse files Browse the repository at this point in the history
  • Loading branch information
siyul-park committed Nov 24, 2023
1 parent 407997b commit 6ff1a90
Show file tree
Hide file tree
Showing 17 changed files with 1 addition and 323 deletions.
9 changes: 0 additions & 9 deletions pkg/primitive/binary.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package primitive
import (
"encoding"
"fmt"
"hash/fnv"
"reflect"

"github.com/pkg/errors"
Expand Down Expand Up @@ -83,14 +82,6 @@ func (o Binary) Compare(v Object) int {
}
}

func (o Binary) Hash() uint32 {
h := fnv.New32()
h.Write([]byte{byte(KindBinary), 0})
h.Write([]byte(o))

return h.Sum32()
}

func (o Binary) Interface() any {
return []byte(o)
}
Expand Down
6 changes: 0 additions & 6 deletions pkg/primitive/binary_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,6 @@ func TestBinary_Compare(t *testing.T) {
assert.Equal(t, 1, v2.Compare(v1))
}

func TestBinary_Hash(t *testing.T) {
assert.NotEqual(t, NewBinary([]byte{0}).Hash(), NewBinary([]byte{1}).Hash())
assert.Equal(t, NewBinary(nil).Hash(), NewBinary(nil).Hash())
assert.Equal(t, NewBinary([]byte{0}).Hash(), NewBinary([]byte{0}).Hash())
}

func TestBinary_Encode(t *testing.T) {
e := NewBinaryEncoder()

Expand Down
14 changes: 0 additions & 14 deletions pkg/primitive/bool.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package primitive

import (
"hash/fnv"
"reflect"

"github.com/pkg/errors"
Expand Down Expand Up @@ -58,19 +57,6 @@ func (o Bool) Compare(v Object) int {
}
}

func (o Bool) Hash() uint32 {
var v byte
if o {
v |= 1
}

h := fnv.New32()
h.Write([]byte{byte(KindBool), 0})
h.Write([]byte{v})

return h.Sum32()
}

func (o Bool) Interface() any {
return bool(o)
}
Expand Down
6 changes: 0 additions & 6 deletions pkg/primitive/bool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,6 @@ func TestBool_Equal(t *testing.T) {
assert.False(t, FALSE.Equal(TRUE))
}

func TestBool_Hash(t *testing.T) {
assert.NotEqual(t, TRUE.Hash(), FALSE.Hash())
assert.Equal(t, TRUE.Hash(), TRUE.Hash())
assert.Equal(t, FALSE.Hash(), FALSE.Hash())
}

func TestBool_Encode(t *testing.T) {
e := NewBoolEncoder()

Expand Down
25 changes: 0 additions & 25 deletions pkg/primitive/float.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package primitive

import (
"encoding/binary"
"hash/fnv"
"math"
"reflect"

"github.com/pkg/errors"
Expand Down Expand Up @@ -68,17 +65,6 @@ func (o Float32) Compare(v Object) int {
}
}

func (o Float32) Hash() uint32 {
var buf [4]byte
binary.BigEndian.PutUint32(buf[:], math.Float32bits(float32(o)))

h := fnv.New32()
h.Write([]byte{byte(KindFloat32), 0})
h.Write(buf[:])

return h.Sum32()
}

func (o Float32) Interface() any {
return float32(o)
}
Expand Down Expand Up @@ -127,17 +113,6 @@ func (o Float64) Compare(v Object) int {
}
}

func (o Float64) Hash() uint32 {
var buf [8]byte
binary.BigEndian.PutUint64(buf[:], math.Float64bits(float64(o)))

h := fnv.New32()
h.Write([]byte{byte(KindFloat64), 0})
h.Write(buf[:])

return h.Sum32()
}

func (o Float64) Interface() any {
return float64(o)
}
Expand Down
14 changes: 0 additions & 14 deletions pkg/primitive/float_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,20 +51,6 @@ func TestFloat_Compare(t *testing.T) {
})
}

func TestFloat_Hash(t *testing.T) {
t.Run("32", func(t *testing.T) {
assert.NotEqual(t, NewFloat32(0).Hash(), NewFloat32(1).Hash())
assert.Equal(t, NewFloat32(0).Hash(), NewFloat32(0).Hash())
assert.Equal(t, NewFloat32(1).Hash(), NewFloat32(1).Hash())
})

t.Run("64", func(t *testing.T) {
assert.NotEqual(t, NewFloat64(0).Hash(), NewFloat64(1).Hash())
assert.Equal(t, NewFloat64(0).Hash(), NewFloat64(0).Hash())
assert.Equal(t, NewFloat64(1).Hash(), NewFloat64(1).Hash())
})
}

func TestFloat_Encode(t *testing.T) {
e := NewFloatEncoder()

Expand Down
52 changes: 0 additions & 52 deletions pkg/primitive/int.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package primitive

import (
"hash/fnv"
"reflect"
"unsafe"

"github.com/pkg/errors"
"github.com/siyul-park/uniflow/internal/encoding"
Expand Down Expand Up @@ -76,16 +74,6 @@ func (o Int) Compare(v Object) int {
}
}

func (o Int) Hash() uint32 {
buf := *(*[unsafe.Sizeof(o)]byte)(unsafe.Pointer(&o))

h := fnv.New32()
h.Write([]byte{byte(KindInt), 0})
h.Write(buf[:])

return h.Sum32()
}

func (o Int) Interface() any {
return int(o)
}
Expand Down Expand Up @@ -134,16 +122,6 @@ func (o Int8) Compare(v Object) int {
}
}

func (o Int8) Hash() uint32 {
buf := *(*[unsafe.Sizeof(o)]byte)(unsafe.Pointer(&o))

h := fnv.New32()
h.Write([]byte{byte(KindInt8), 0})
h.Write(buf[:])

return h.Sum32()
}

func (o Int8) Interface() any {
return int8(o)
}
Expand Down Expand Up @@ -192,16 +170,6 @@ func (o Int16) Compare(v Object) int {
}
}

func (o Int16) Hash() uint32 {
buf := *(*[unsafe.Sizeof(o)]byte)(unsafe.Pointer(&o))

h := fnv.New32()
h.Write([]byte{byte(KindInt16), 0})
h.Write(buf[:])

return h.Sum32()
}

func (o Int16) Interface() any {
return int16(o)
}
Expand Down Expand Up @@ -250,16 +218,6 @@ func (o Int32) Compare(v Object) int {
}
}

func (o Int32) Hash() uint32 {
buf := *(*[unsafe.Sizeof(o)]byte)(unsafe.Pointer(&o))

h := fnv.New32()
h.Write([]byte{byte(KindInt32), 0})
h.Write(buf[:])

return h.Sum32()
}

func (o Int32) Interface() any {
return int32(o)
}
Expand Down Expand Up @@ -308,16 +266,6 @@ func (o Int64) Compare(v Object) int {
}
}

func (o Int64) Hash() uint32 {
buf := *(*[unsafe.Sizeof(o)]byte)(unsafe.Pointer(&o))

h := fnv.New32()
h.Write([]byte{byte(KindInt64), 0})
h.Write(buf[:])

return h.Sum32()
}

func (o Int64) Interface() any {
return int64(o)
}
Expand Down
28 changes: 0 additions & 28 deletions pkg/primitive/int_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,34 +100,6 @@ func TestInt_Compare(t *testing.T) {
})
}

func TestInt_Hash(t *testing.T) {
t.Run("", func(t *testing.T) {
assert.NotEqual(t, NewInt(0).Hash(), NewInt(1).Hash())
assert.Equal(t, NewInt(0).Hash(), NewInt(0).Hash())
assert.Equal(t, NewInt(1).Hash(), NewInt(1).Hash())
})
t.Run("8", func(t *testing.T) {
assert.NotEqual(t, NewInt8(0).Hash(), NewInt8(1).Hash())
assert.Equal(t, NewInt8(0).Hash(), NewInt8(0).Hash())
assert.Equal(t, NewInt8(1).Hash(), NewInt8(1).Hash())
})
t.Run("16", func(t *testing.T) {
assert.NotEqual(t, NewInt16(0).Hash(), NewInt16(1).Hash())
assert.Equal(t, NewInt16(0).Hash(), NewInt16(0).Hash())
assert.Equal(t, NewInt16(1).Hash(), NewInt16(1).Hash())
})
t.Run("32", func(t *testing.T) {
assert.NotEqual(t, NewInt32(0).Hash(), NewInt32(1).Hash())
assert.Equal(t, NewInt32(0).Hash(), NewInt32(0).Hash())
assert.Equal(t, NewInt32(1).Hash(), NewInt32(1).Hash())
})
t.Run("64", func(t *testing.T) {
assert.NotEqual(t, NewInt64(0).Hash(), NewInt64(1).Hash())
assert.Equal(t, NewInt64(0).Hash(), NewInt64(0).Hash())
assert.Equal(t, NewInt64(1).Hash(), NewInt64(1).Hash())
})
}

func TestInt_Encode(t *testing.T) {
e := NewIntEncoder()

Expand Down
29 changes: 0 additions & 29 deletions pkg/primitive/map.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@ package primitive

import (
"fmt"
"hash/fnv"
"reflect"
"strings"
"unsafe"

"github.com/benbjohnson/immutable"
"github.com/iancoleman/strcase"
Expand Down Expand Up @@ -177,33 +175,6 @@ func (o *Map) Compare(v Object) int {
}
}

func (o *Map) Hash() uint32 {
h := fnv.New32()
h.Write([]byte{byte(KindMap), 0})

itr := o.value.Iterator()
for !itr.Done() {
k, v, _ := itr.Next()

if k != nil {
hash := k.Hash()
buf := *(*[unsafe.Sizeof(hash)]byte)(unsafe.Pointer(&hash))
h.Write(buf[:])
} else {
h.Write([]byte{0})
}
if v != nil {
hash := v.Hash()
buf := *(*[unsafe.Sizeof(hash)]byte)(unsafe.Pointer(&hash))
h.Write(buf[:])
} else {
h.Write([]byte{0})
}
}

return h.Sum32()
}

func (o *Map) Interface() any {
var keys []any
var values []any
Expand Down
14 changes: 1 addition & 13 deletions pkg/primitive/map_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,6 @@ func TestMap_Equal(t *testing.T) {
assert.False(t, NewMap(k1, v1).Equal(NewMap(k2, v2)))
}

func TestMap_Hash(t *testing.T) {
k1 := NewString(faker.Word())
k2 := NewString(faker.Word())
v1 := NewString(faker.Word())
v2 := NewString(faker.Word())

assert.NotEqual(t, NewMap(k1, v1).Hash(), NewMap(k2, v2).Hash())
assert.Equal(t, NewMap().Hash(), NewMap().Hash())
assert.Equal(t, NewMap(k1, v1).Hash(), NewMap(k1, v1).Hash())
assert.Equal(t, NewMap(k1, v1, k2, v2).Hash(), NewMap(k2, v2, k1, v1).Hash())
}

func TestMap_GetAndSetAndDelete(t *testing.T) {
k1 := NewString(faker.Word())
v1 := NewString(faker.Word())
Expand Down Expand Up @@ -99,7 +87,7 @@ func TestMap_Encode(t *testing.T) {
K1: v1.String(),
})
assert.NoError(t, err)
assert.Equal(t, NewMap(NewString("k_1"), v1).Hash(), v.Hash())
assert.True(t, NewMap(NewString("k_1"), v1).Compare(v) == 0)
})
}

Expand Down
1 change: 0 additions & 1 deletion pkg/primitive/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ type (
Kind() Kind
Equal(v Object) bool
Compare(v Object) int
Hash() uint32
Interface() any
}

Expand Down
22 changes: 0 additions & 22 deletions pkg/primitive/slice.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ package primitive

import (
"fmt"
"hash/fnv"
"reflect"
"unsafe"

"github.com/benbjohnson/immutable"
"github.com/pkg/errors"
Expand Down Expand Up @@ -120,26 +118,6 @@ func (o *Slice) Compare(v Object) int {
}
}

func (o *Slice) Hash() uint32 {
h := fnv.New32()
h.Write([]byte{byte(KindSlice), 0})

itr := o.value.Iterator()
for !itr.Done() {
_, v := itr.Next()

if v != nil {
hash := v.Hash()
buf := *(*[unsafe.Sizeof(hash)]byte)(unsafe.Pointer(&hash))
h.Write(buf[:])
} else {
h.Write([]byte{0})
}
}

return h.Sum32()
}

func (o *Slice) Interface() any {
var values []any
itr := o.value.Iterator()
Expand Down
Loading

0 comments on commit 6ff1a90

Please sign in to comment.