2 Commits

Author SHA1 Message Date
29ba6bfd4d fix!: no mixed receiver types (#23)
All checks were successful
CI / Check PR Title (push) Has been skipped
CI / Makefile Lint (push) Successful in 50s
CI / Go Lint (push) Successful in 54s
CI / Markdown Lint (push) Successful in 50s
CI / Unit Tests (push) Successful in 47s
CI / Mutation Tests (push) Successful in 1m31s
CI / Fuzz Tests (push) Successful in 1m21s
## Description

Currently, `bucket` and `Table` have mixed receiver types: some are pointer receviers, and others are value receivers.

As per the Go Wiki, [you can have value and pointer receivers, just don't mix them](https://go.dev/doc/faq#methods_on_values_or_pointers).

## Changes

- Replace all value receivers in `bucket` and `Table` with pointer receivers.

### Design Decisions

This decision was made due to the advice on the Go wiki.

## Checklist

- [x] Tests pass
- [x] Docs updated

Reviewed-on: #23
Co-authored-by: M.V. Hutz <git@maximhutz.me>
Co-committed-by: M.V. Hutz <git@maximhutz.me>
2026-04-16 03:27:48 +00:00
7cc1657403 refactor!: shorter constructors, bucketsubtable (#22)
All checks were successful
CI / Check PR Title (push) Has been skipped
CI / Makefile Lint (push) Successful in 47s
CI / Go Lint (push) Successful in 51s
CI / Markdown Lint (push) Successful in 46s
CI / Unit Tests (push) Successful in 47s
CI / Fuzz Tests (push) Successful in 1m19s
CI / Mutation Tests (push) Successful in 1m36s
## Description

Currently, the name of `bucket` is a bit confusing, because it is considered a 'table' in literature (as well as the whole hash table). A `bucket` is better described as a 'subtable', which is used by the total hash table to perform cuckoo hashing.

In addition, the constructors `NewTable`, `NewTableBy`, and `NewCustomTable` were given shorter names, because the package name `cuckoo` already implies that `New*` would create a hash table with cuckoo hashing. This package has one use-case, and so it unambiguous what constructors produce.

## Changes

- `NewTable` -> `New`
- `NewTableBy` -> `NewBy`
- `NewCustomTable` -> `NewCustom`
- `bucket` -> `subtable`

### Design Decisions

- I would have renamed `Table` and `subtable` to map equivalents, but 'submap' implies that a certain subsection of the map is contained within it, which isn't quite right.
- I chose not to go with `Map` and `table`, because of the split naming convention.

## Checklist

- [x] Tests pass
- [x] Docs updated

Reviewed-on: #22
Co-authored-by: M.V. Hutz <git@maximhutz.me>
Co-committed-by: M.V. Hutz <git@maximhutz.me>
2026-04-16 03:15:39 +00:00
3 changed files with 10 additions and 7 deletions

View File

@@ -114,6 +114,9 @@ linters:
# Reports uses of functions with replacement inside the testing package.
- usetesting
# Reports mixed receiver types in structs/interfaces.
- recvcheck
settings:
revive:
rules:

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

@@ -97,7 +97,7 @@ func (t *Table[K, V]) shrink() error {
// 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) {
func (t *Table[K, V]) Get(key K) (value V, ok bool) {
if item, ok := t.tableA.get(key); ok {
return item, true
}
@@ -111,13 +111,13 @@ func (t Table[K, V]) Get(key K) (value V, ok bool) {
// 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) {
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.
func (t Table[K, V]) Has(key K) (exists bool) {
func (t *Table[K, V]) Has(key K) (exists bool) {
_, exists = t.Get(key)
return
}