Compare commits
3 Commits
5c39182958
...
v0.3.0
| Author | SHA1 | Date | |
|---|---|---|---|
| 29ba6bfd4d | |||
| 7cc1657403 | |||
| 42c5b5f8f4 |
@@ -114,6 +114,9 @@ linters:
|
|||||||
# Reports uses of functions with replacement inside the testing package.
|
# Reports uses of functions with replacement inside the testing package.
|
||||||
- usetesting
|
- usetesting
|
||||||
|
|
||||||
|
# Reports mixed receiver types in structs/interfaces.
|
||||||
|
- recvcheck
|
||||||
|
|
||||||
settings:
|
settings:
|
||||||
revive:
|
revive:
|
||||||
rules:
|
rules:
|
||||||
@@ -198,7 +201,7 @@ linters:
|
|||||||
|
|
||||||
# warns when initialism, variable or package naming conventions are not followed.
|
# warns when initialism, variable or package naming conventions are not followed.
|
||||||
- name: var-naming
|
- name: var-naming
|
||||||
|
|
||||||
misspell:
|
misspell:
|
||||||
# Correct spellings using locale preferences for US or UK.
|
# Correct spellings using locale preferences for US or UK.
|
||||||
# Setting locale to US will correct the British spelling of 'colour' to 'color'.
|
# Setting locale to US will correct the British spelling of 'colour' to 'color'.
|
||||||
|
|||||||
@@ -73,16 +73,16 @@ func FuzzInsertLookup(f *testing.F) {
|
|||||||
|
|
||||||
delete(expected, step.key)
|
delete(expected, step.key)
|
||||||
|
|
||||||
_, err = actual.Get(step.key)
|
_, ok := actual.Get(step.key)
|
||||||
assert.Error(err)
|
assert.False(ok)
|
||||||
} else {
|
} else {
|
||||||
err := actual.Put(step.key, step.value)
|
err := actual.Put(step.key, step.value)
|
||||||
assert.NoError(err)
|
assert.NoError(err)
|
||||||
|
|
||||||
expected[step.key] = step.value
|
expected[step.key] = step.value
|
||||||
|
|
||||||
found, err := actual.Get(step.key)
|
found, ok := actual.Get(step.key)
|
||||||
assert.NoError(err)
|
assert.True(ok)
|
||||||
assert.Equal(step.value, found)
|
assert.Equal(step.value, found)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -108,12 +108,12 @@ func TestGetMany(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for i := range 2_000 {
|
for i := range 2_000 {
|
||||||
value, err := table.Get(i)
|
value, ok := table.Get(i)
|
||||||
if i < 1_000 {
|
if i < 1_000 {
|
||||||
assert.NoError(err)
|
assert.True(ok)
|
||||||
assert.Equal(value, true)
|
assert.Equal(value, true)
|
||||||
} else {
|
} else {
|
||||||
assert.Error(err)
|
assert.False(ok)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,19 +14,19 @@ func Example_basic() {
|
|||||||
fmt.Println("Put error:", err)
|
fmt.Println("Put error:", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if item, err := table.Get(1); err != nil {
|
if item, ok := table.Get(1); !ok {
|
||||||
fmt.Println("Error:", err)
|
fmt.Println("Not Found 1!")
|
||||||
} else {
|
} else {
|
||||||
fmt.Println("Found 1:", item)
|
fmt.Println("Found 1:", item)
|
||||||
}
|
}
|
||||||
|
|
||||||
if item, err := table.Get(0); err != nil {
|
if item, ok := table.Get(0); !ok {
|
||||||
fmt.Println("Error:", err)
|
fmt.Println("Not Found 0!")
|
||||||
} else {
|
} else {
|
||||||
fmt.Println("Found 0:", item)
|
fmt.Println("Found 0:", item)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Output:
|
// Output:
|
||||||
// Found 1: Hello, World!
|
// Found 1: Hello, World!
|
||||||
// Error: key '0' not found
|
// Not Found 0!
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -19,11 +19,11 @@ type subtable[K, V any] struct {
|
|||||||
|
|
||||||
// location determines where in the subtable a certain key would be placed. If
|
// location determines where in the subtable a certain key would be placed. If
|
||||||
// the capacity is 0, this will panic.
|
// the capacity is 0, this will panic.
|
||||||
func (t subtable[K, V]) location(key K) uint64 {
|
func (t *subtable[K, V]) location(key K) uint64 {
|
||||||
return t.hash(key) % t.capacity
|
return t.hash(key) % t.capacity
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t subtable[K, V]) get(key K) (value V, found bool) {
|
func (t *subtable[K, V]) get(key K) (value V, found bool) {
|
||||||
if t.capacity == 0 {
|
if t.capacity == 0 {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -54,7 +54,7 @@ func (t *subtable[K, V]) resize(capacity uint64) {
|
|||||||
t.size = 0
|
t.size = 0
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t subtable[K, V]) update(key K, value V) (updated bool) {
|
func (t *subtable[K, V]) update(key K, value V) (updated bool) {
|
||||||
if t.capacity == 0 {
|
if t.capacity == 0 {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
23
table.go
23
table.go
@@ -95,24 +95,31 @@ func (t *Table[K, V]) shrink() error {
|
|||||||
return t.resize(t.tableA.capacity / t.growthFactor)
|
return t.resize(t.tableA.capacity / t.growthFactor)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get fetches the value for a key in the [Table]. Returns an error if no value
|
// Get fetches the value for a key in the [Table]. Matches the comma-ok pattern
|
||||||
// is found.
|
// of a builtin map; see [Table.Find] for plain indexing.
|
||||||
func (t *Table[K, V]) Get(key K) (value V, err error) {
|
func (t *Table[K, V]) Get(key K) (value V, ok bool) {
|
||||||
if item, ok := t.tableA.get(key); ok {
|
if item, ok := t.tableA.get(key); ok {
|
||||||
return item, nil
|
return item, true
|
||||||
}
|
}
|
||||||
|
|
||||||
if item, ok := t.tableB.get(key); ok {
|
if item, ok := t.tableB.get(key); ok {
|
||||||
return item, nil
|
return item, true
|
||||||
}
|
}
|
||||||
|
|
||||||
return value, fmt.Errorf("key '%v' not found", key)
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Find fetches the value of a key. Matches direct indexing of a builtin map;
|
||||||
|
// see [Table.Get] for a comma-ok pattern.
|
||||||
|
func (t *Table[K, V]) Find(key K) (value V) {
|
||||||
|
value, _ = t.Get(key)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Has returns true if a key has a value in the table.
|
// Has returns true if a key has a value in the table.
|
||||||
func (t *Table[K, V]) Has(key K) (exists bool) {
|
func (t *Table[K, V]) Has(key K) (exists bool) {
|
||||||
_, err := t.Get(key)
|
_, exists = t.Get(key)
|
||||||
return err == nil
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Put sets the value for a key. Returns error if its value cannot be set.
|
// Put sets the value for a key. Returns error if its value cannot be set.
|
||||||
|
|||||||
Reference in New Issue
Block a user