5 Commits

Author SHA1 Message Date
5c39182958 refactor: HashTable -> Table, table -> subtable
All checks were successful
CI / Check PR Title (pull_request) Successful in 29s
CI / Go Lint (pull_request) Successful in 39s
CI / Makefile Lint (pull_request) Successful in 48s
CI / Markdown Lint (pull_request) Successful in 31s
CI / Unit Tests (pull_request) Successful in 37s
CI / Fuzz Tests (pull_request) Successful in 1m38s
CI / Mutation Tests (pull_request) Successful in 1m19s
2026-04-13 21:11:37 -04:00
2eeff25efd Merge remote-tracking branch 'origin' into refactor/name-bucket-slot-table 2026-04-13 20:54:09 -04:00
6a5b40c097 docs: replaced instances of "bucket" with "table"
- Removed instances of `growthFactor`, as it is unexported.
- Typo in `HashTable.String()`.
2026-04-13 20:49:33 -04:00
395a3560c7 refactor: constructors, update docs
- NewCustomTable -> NewCustom
- NewTableBy -> NewBy
- NewTable -> New
2026-04-04 12:27:53 +02:00
2fd9da973b refactor: bucket -> table, Table -> HashTable 2026-04-04 12:22:42 +02:00
6 changed files with 24 additions and 34 deletions

View File

@@ -114,9 +114,6 @@ linters:
# Reports uses of functions with replacement inside the testing package.
- usetesting
# Reports mixed receiver types in structs/interfaces.
- recvcheck
settings:
revive:
rules:
@@ -201,7 +198,7 @@ linters:
# warns when initialism, variable or package naming conventions are not followed.
- name: var-naming
misspell:
# Correct spellings using locale preferences for US or UK.
# Setting locale to US will correct the British spelling of 'colour' to 'color'.

View File

@@ -73,16 +73,16 @@ func FuzzInsertLookup(f *testing.F) {
delete(expected, step.key)
_, ok := actual.Get(step.key)
assert.False(ok)
_, err = actual.Get(step.key)
assert.Error(err)
} else {
err := actual.Put(step.key, step.value)
assert.NoError(err)
expected[step.key] = step.value
found, ok := actual.Get(step.key)
assert.True(ok)
found, err := actual.Get(step.key)
assert.NoError(err)
assert.Equal(step.value, found)
}

View File

@@ -108,12 +108,12 @@ func TestGetMany(t *testing.T) {
}
for i := range 2_000 {
value, ok := table.Get(i)
value, err := table.Get(i)
if i < 1_000 {
assert.True(ok)
assert.NoError(err)
assert.Equal(value, true)
} else {
assert.False(ok)
assert.Error(err)
}
}
}

View File

@@ -14,19 +14,19 @@ func Example_basic() {
fmt.Println("Put error:", err)
}
if item, ok := table.Get(1); !ok {
fmt.Println("Not Found 1!")
if item, err := table.Get(1); err != nil {
fmt.Println("Error:", err)
} else {
fmt.Println("Found 1:", item)
}
if item, ok := table.Get(0); !ok {
fmt.Println("Not Found 0!")
if item, err := table.Get(0); err != nil {
fmt.Println("Error:", err)
} else {
fmt.Println("Found 0:", item)
}
// Output:
// Found 1: Hello, World!
// Not Found 0!
// Error: key '0' not found
}

View File

@@ -19,11 +19,11 @@ type subtable[K, V any] struct {
// location determines where in the subtable a certain key would be placed. If
// 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
}
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 {
return
}
@@ -54,7 +54,7 @@ func (t *subtable[K, V]) resize(capacity uint64) {
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 {
return
}

View File

@@ -95,31 +95,24 @@ func (t *Table[K, V]) shrink() error {
return t.resize(t.tableA.capacity / t.growthFactor)
}
// Get fetches the value for a key in the [Table]. Matches the comma-ok pattern
// of a builtin map; see [Table.Find] for plain indexing.
func (t *Table[K, V]) Get(key K) (value V, ok bool) {
// Get fetches the value for a key in the [Table]. Returns an error if no value
// is found.
func (t *Table[K, V]) Get(key K) (value V, err error) {
if item, ok := t.tableA.get(key); ok {
return item, true
return item, nil
}
if item, ok := t.tableB.get(key); ok {
return item, true
return item, nil
}
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
return value, fmt.Errorf("key '%v' not found", key)
}
// Has returns true if a key has a value in the table.
func (t *Table[K, V]) Has(key K) (exists bool) {
_, exists = t.Get(key)
return
_, err := t.Get(key)
return err == nil
}
// Put sets the value for a key. Returns error if its value cannot be set.