diff options
author | Am Laher <amir.laher@tixtrack.com> | 2022-06-11 21:51:04 +1200 |
---|---|---|
committer | Am Laher <amir.laher@tixtrack.com> | 2022-06-11 21:51:04 +1200 |
commit | 3bd1361378dc30dd2329baabc0b587401c7d43ce (patch) | |
tree | e6b9e67f4996ea32160905ac9dd5ffaadc7d1ce1 | |
parent | undo v2 module change after comment from @benbjohnson (diff) | |
download | pds-3bd1361378dc30dd2329baabc0b587401c7d43ce.tar.gz pds-3bd1361378dc30dd2329baabc0b587401c7d43ce.tar.xz |
add missing ptr types; tidyup
-rw-r--r-- | immutable.go | 15 | ||||
-rw-r--r-- | immutable_test.go | 72 |
2 files changed, 18 insertions, 69 deletions
diff --git a/immutable.go b/immutable.go index 9666fa6..b189612 100644 --- a/immutable.go +++ b/immutable.go @@ -2215,14 +2215,14 @@ type Hasher[K constraints.Ordered] interface { func NewHasher[K constraints.Ordered](key K) Hasher[K] { // Attempt to use non-reflection based hasher first. switch (any(key)).(type) { - case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, string: + case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, uintptr, string: return &defaultHasher[K]{} } // Fallback to reflection-based hasher otherwise. // This is used when caller wraps a type around a primitive type. switch reflect.TypeOf(key).Kind() { - case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.String: + case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr, reflect.String: return &reflectHasher[K]{} } @@ -2248,7 +2248,7 @@ func (h *reflectHasher[K]) Hash(key K) uint32 { switch reflect.TypeOf(key).Kind() { case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: return hashUint64(uint64(reflect.ValueOf(key).Int())) - case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: + case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr: return hashUint64(reflect.ValueOf(key).Uint()) case reflect.String: var hash uint32 @@ -2267,7 +2267,7 @@ func (h *reflectHasher[K]) Equal(a, b K) bool { switch reflect.TypeOf(a).Kind() { case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: return reflect.ValueOf(a).Int() == reflect.ValueOf(b).Int() - case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: + case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr: return reflect.ValueOf(a).Uint() == reflect.ValueOf(b).Uint() case reflect.String: return reflect.ValueOf(a).String() == reflect.ValueOf(b).String() @@ -2293,7 +2293,6 @@ type defaultHasher[K constraints.Ordered] struct{} func (h *defaultHasher[K]) Hash(key K) uint32 { // Attempt to use non-reflection based hasher first. switch x := (any(key)).(type) { - // int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64: case int: return hashUint64(uint64(x)) case int8: @@ -2339,13 +2338,13 @@ type Comparer[K constraints.Ordered] interface { func NewComparer[K constraints.Ordered](key K) Comparer[K] { // Attempt to use non-reflection based comparer first. switch (any(key)).(type) { - case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, string: + case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, uintptr, string: return &defaultComparer[K]{} } // Fallback to reflection-based comparer otherwise. // This is used when caller wraps a type around a primitive type. switch reflect.TypeOf(key).Kind() { - case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.String: + case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr, reflect.String: return &reflectComparer[K]{} } // If no comparers match then panic. @@ -2381,7 +2380,7 @@ func (c *reflectComparer[K]) Compare(a, b K) int { return 1 } return 0 - case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: + case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr: if i, j := reflect.ValueOf(a).Uint(), reflect.ValueOf(b).Uint(); i < j { return -1 } else if i > j { diff --git a/immutable_test.go b/immutable_test.go index b11b13f..c4b0297 100644 --- a/immutable_test.go +++ b/immutable_test.go @@ -988,37 +988,6 @@ func TestMap_Set(t *testing.T) { t.Fatalf("expected no value: <%v,%v>", v, ok) } }) - /* - t.Run("ByteSliceKeys", func(t *testing.T) { - m := NewMap[[]byte, string](nil) - m = m.Set([]byte("foo"), "bar") - m = m.Set([]byte("baz"), "bat") - m = m.Set([]byte(""), "EMPTY") - if v, ok := m.Get([]byte("foo")); !ok || v != "bar" { - t.Fatalf("unexpected value: <%v,%v>", v, ok) - } else if v, ok := m.Get([]byte("baz")); !ok || v != "bat" { - t.Fatalf("unexpected value: <%v,%v>", v, ok) - } else if v, ok := m.Get([]byte("")); !ok || v != "EMPTY" { - t.Fatalf("unexpected value: <%v,%v>", v, ok) - } - if v, ok := m.Get([]byte("no_such_key")); ok { - t.Fatalf("expected no value: <%v,%v>", v, ok) - } - }) - - t.Run("NoDefaultHasher", func(t *testing.T) { - type T struct{} - var r string - func() { - defer func() { r = recover().(string) }() - m := NewMap[T, string](nil) - m = m.Set(T{}, "bar") - }() - if r != `immutable.NewHasher: must set hasher for immutable.T type` { - t.Fatalf("unexpected panic: %q", r) - } - }) - */ RunRandom(t, "Random", func(t *testing.T, rand *rand.Rand) { m := NewTestMap() @@ -1813,36 +1782,17 @@ func TestSortedMap_Set(t *testing.T) { } }) - /* - t.Run("ByteSliceKeys", func(t *testing.T) { - m := NewSortedMap[int, int](nil) - m = m.Set([]byte("foo"), "bar") - m = m.Set([]byte("baz"), "bat") - m = m.Set([]byte(""), "EMPTY") - if v, ok := m.Get([]byte("foo")); !ok || v != "bar" { - t.Fatalf("unexpected value: <%v,%v>", v, ok) - } else if v, ok := m.Get([]byte("baz")); !ok || v != "bat" { - t.Fatalf("unexpected value: <%v,%v>", v, ok) - } else if v, ok := m.Get([]byte("")); !ok || v != "EMPTY" { - t.Fatalf("unexpected value: <%v,%v>", v, ok) - } - if v, ok := m.Get([]byte("no_such_key")); ok { - t.Fatalf("expected no value: <%v,%v>", v, ok) - } - }) - - t.Run("NoDefaultComparer", func(t *testing.T) { - var r string - func() { - defer func() { r = recover().(string) }() - m := NewSortedMap[int, int](nil) - m = m.Set(float64(100), "bar") - }() - if r != `immutable.NewComparer: must set comparer for float64 type` { - t.Fatalf("unexpected panic: %q", r) - } - }) - */ + t.Run("NoDefaultComparer", func(t *testing.T) { + var r string + func() { + defer func() { r = recover().(string) }() + m := NewSortedMap[float64, string](nil) + m = m.Set(float64(100), "bar") + }() + if r != `immutable.NewComparer: must set comparer for float64 type` { + t.Fatalf("unexpected panic: %q", r) + } + }) RunRandom(t, "Random", func(t *testing.T, rand *rand.Rand) { m := NewTSortedMap() |