Compare commits
7 Commits
v0.1.0
...
2fc74169d5
| Author | SHA1 | Date | |
|---|---|---|---|
|
2fc74169d5
|
|||
|
a815b49b66
|
|||
|
b499fa9c05
|
|||
|
9e936ebadb
|
|||
|
d07f76207b
|
|||
| 41b09ae680 | |||
| e00e6fcb1b |
7
.claude/settings.json
Normal file
7
.claude/settings.json
Normal file
@@ -0,0 +1,7 @@
|
||||
{
|
||||
"permissions": {
|
||||
"allow": [
|
||||
"Bash(curl -s -w \"\\\\n---HTTP_STATUS:%{http_code}---\" https://raw.githubusercontent.com/kubernetes/kubernetes/master/.markdownlint.yaml)"
|
||||
]
|
||||
}
|
||||
}
|
||||
@@ -39,6 +39,14 @@ jobs:
|
||||
- name: Run mutation tests
|
||||
run: make lint-makefile
|
||||
|
||||
lint-markdown:
|
||||
name: Markdown Lint
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
|
||||
- uses: DavidAnson/markdownlint-cli2-action@v19
|
||||
|
||||
test-unit:
|
||||
name: Unit Tests
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
17
.markdownlint.yml
Normal file
17
.markdownlint.yml
Normal file
@@ -0,0 +1,17 @@
|
||||
default: true
|
||||
heading-style:
|
||||
style: atx
|
||||
ul-indent:
|
||||
indent: 2
|
||||
line-length: false
|
||||
no-duplicate-heading:
|
||||
siblings_only: true
|
||||
no-inline-html:
|
||||
allowed_elements:
|
||||
- br
|
||||
- details
|
||||
- summary
|
||||
- img
|
||||
- picture
|
||||
- source
|
||||
first-line-heading: true
|
||||
7
Makefile
7
Makefile
@@ -1,4 +1,4 @@
|
||||
.PHONY: all help install clean test-unit test-mutation test-fuzz test docs lint-go lint-makefile lint
|
||||
.PHONY: all help install clean test-unit test-mutation test-fuzz test docs lint-go lint-makefile lint-markdown lint
|
||||
|
||||
help: ## Show this help
|
||||
@grep -E '^[a-zA-Z_-]+:.*##' $(MAKEFILE_LIST) | awk -F ':.*## ' '{printf " %-15s %s\n", $$1, $$2}'
|
||||
@@ -32,7 +32,10 @@ lint-go: ## Lint Go code
|
||||
lint-makefile: ## Lint the Makefile
|
||||
checkmake Makefile
|
||||
|
||||
lint: lint-go lint-makefile ## Lint all code
|
||||
lint-markdown: ## Lint Markdown files
|
||||
docker run --rm -v $(CURDIR):/workdir davidanson/markdownlint-cli2 "**/*.md"
|
||||
|
||||
lint: lint-go lint-makefile lint-markdown ## Lint all code
|
||||
|
||||
docs: ## Serve godoc locally
|
||||
@echo ">>> Visit: http://localhost:6060/pkg/$$(go list -m)"
|
||||
|
||||
@@ -1,3 +1,3 @@
|
||||
# go-cuckoo
|
||||
# <img height="30" src="assets/logo.svg" alt="Go Cuckoo, by `mvhutz`."> Go Cuckoo
|
||||
|
||||
A hash table that uses cuckoo hashing to achieve a worst-case O(1) lookup time.
|
||||
A hash table that uses cuckoo hashing to achieve a worst-case O(1) lookup time. Read more about it in [the package documentation](https://pkg.go.dev/git.maximhutz.com/tools/go-cuckoo).
|
||||
|
||||
1
assets/logo.svg
Normal file
1
assets/logo.svg
Normal file
@@ -0,0 +1 @@
|
||||
<svg width="16" height="16" xmlns="http://www.w3.org/2000/svg" shape-rendering="crispEdges"><path fill="#2a1512" d="M3,6h1v1h-1v-1M2,7h1v1h-1v-1M12,7h1v1h-1v-1M1,8h1v1h-1v-1M4,8h1v2h-1v-2M14,8h1v1h-1v-1M11,9h1v2h-2v-1h1v-1M5,10h1v1h-1v-1M1,13h2v1h1v1h-2v-1h-1v-1M13,13h2v1h-1v1h-1v1h-1v-2h1v-1M4,15h1v1h-1v-1"/><path fill="#38231f" d="M4,6h1v1h-1v-1M11,6h1v1h-1v-1M13,7h1v1h-1v-1M1,9h1v2h-1v-2M14,9h1v1h-1v-1M14,11h1v2h-1v-2M1,12h1v1h-1v-1M5,15h3v1h-3v-1M10,15h2v1h-2v-1"/><path fill="#3f3f74" d="M7,3h2v1h-2v-1M6,4h1v1h-1v-1M9,4h1v1h-1v-1M5,5h1v5h-1v-5M10,5h1v5h-1v-5M6,10h4v1h-4v-1"/><path fill="#6262ab" d="M9,5h1v1h-1v-1M6,9h1v1h-1v-1M9,9h1v1h-1v-1"/><path fill="#663931" d="M11,8h1v1h-1v-1M2,9h1v1h-1v-1M14,10h1v1h-1v-1M1,11h1v1h-1v-1M3,11h2v1h-2v-1M13,11h1v1h-1v-1M2,12h1v1h-1v-1M5,12h1v1h-1v-1M10,12h3v2h-1v-1h-2v-1M3,13h1v1h-1v-1M8,13h2v1h-2v-1M4,14h3v1h-3v-1M10,14h2v1h-2v-1M8,15h2v1h-2v-1"/><path fill="#8d8dcb" d="M9,6h1v3h-1v1h-2v-1h1v-1h1v-2M6,8h1v1h-1v-1"/><path fill="#8f563b" d="M2,8h1v1h-1v-1M12,9h1v1h-1v-1M2,10h1v1h-1v-1M13,10h1v1h-1v-1M7,11h1v1h-1v-1M11,11h2v1h-2v-1M4,12h1v1h1v-1h1v2h-3v-2M13,12h1v1h-1v-1M10,13h2v1h-2v-1M7,14h3v1h-3v-1"/><path fill="#a4a4d5" d="M8,4h1v4h-1v1h-1v-1h-1v-2h1v-1h1v-1"/><path fill="#ab764a" d="M4,7h1v1h-1v-1M3,8h1v2h-1v-2M12,10h1v1h-1v-1M2,11h1v1h-1v-1M8,11h1v1h-1v-1M3,12h1v1h-1v-1M7,12h1v2h-1v-2"/><path fill="#cacaea" d="M7,4h1v1h-1v-1M6,5h1v1h-1v-1"/><path fill="#d9a066" d="M3,7h1v1h-1v-1M11,7h1v1h-1v-1M12,8h2v2h-1v-1h-1v-1M3,10h2v1h-2v-1M5,11h2v1h-2v-1M9,11h2v1h-1v1h-2v-1h1v-1"/></svg>
|
||||
|
After Width: | Height: | Size: 1.5 KiB |
BIN
assets/logo128x128.png
Normal file
BIN
assets/logo128x128.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 963 B |
BIN
assets/logo16x16.png
Normal file
BIN
assets/logo16x16.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 418 B |
@@ -1,7 +1,10 @@
|
||||
package cuckoo_test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"maps"
|
||||
"math"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
@@ -26,6 +29,8 @@ type fuzzStep struct {
|
||||
|
||||
type fuzzScenario struct {
|
||||
seedA, seedB uint32
|
||||
capacity, growthFactor uint8
|
||||
load float64
|
||||
steps []fuzzStep
|
||||
}
|
||||
|
||||
@@ -40,14 +45,33 @@ func FuzzInsertLookup(f *testing.F) {
|
||||
return
|
||||
}
|
||||
|
||||
if scenario.seedA == scenario.seedB {
|
||||
return
|
||||
seedA, seedB := scenario.seedA, scenario.seedB
|
||||
growthFactor := max(2, int(scenario.growthFactor))
|
||||
capacity := int(scenario.capacity)
|
||||
minimumLoad := math.Abs(math.Mod(scenario.load, 1.0))
|
||||
|
||||
// If they are the same number, the hashes will clash, always causing an
|
||||
// error.
|
||||
if seedA == seedB {
|
||||
t.Skip()
|
||||
}
|
||||
|
||||
// If the load is too high, the hashs will not be able to allocate
|
||||
// properly.
|
||||
if minimumLoad > 0.20 {
|
||||
t.Skip()
|
||||
}
|
||||
|
||||
fmt.Fprintf(os.Stderr, "seedA=%d seedB=%d capacity=%d growthFactor=%d minimumLoad=%f\n",
|
||||
seedA, seedB, capacity, growthFactor, minimumLoad)
|
||||
|
||||
actual := cuckoo.NewCustomTable[uint32, uint32](
|
||||
offsetHash(scenario.seedA),
|
||||
offsetHash(scenario.seedB),
|
||||
offsetHash(seedA),
|
||||
offsetHash(seedB),
|
||||
func(a, b uint32) bool { return a == b },
|
||||
cuckoo.Capacity(capacity),
|
||||
cuckoo.GrowthFactor(growthFactor),
|
||||
cuckoo.MinimumLoad(minimumLoad),
|
||||
)
|
||||
|
||||
expected := map[uint32]uint32{}
|
||||
|
||||
@@ -118,18 +118,6 @@ func TestGetMany(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestRemove(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
table := cuckoo.NewTable[int, bool]()
|
||||
|
||||
assert.False(table.Has(0))
|
||||
|
||||
err := table.Put(0, true)
|
||||
assert.NoError(err)
|
||||
|
||||
assert.True(table.Has(0))
|
||||
}
|
||||
|
||||
func TestDropExistingItem(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
key, value := 0, true
|
||||
|
||||
19
settings.go
19
settings.go
@@ -1,5 +1,7 @@
|
||||
package cuckoo
|
||||
|
||||
import "fmt"
|
||||
|
||||
// DefaultCapacity is the initial capacity of a [Table]. It is inspired from
|
||||
// Java's [HashMap] implementation, which also uses 16.
|
||||
//
|
||||
@@ -27,19 +29,34 @@ type settings struct {
|
||||
type Option func(*settings)
|
||||
|
||||
// Capacity modifies the starting capacity of each bucket of the [Table]. The
|
||||
// value must be greater than 0.
|
||||
// value must be non-negative.
|
||||
func Capacity(value int) Option {
|
||||
if value < 0 {
|
||||
panic(fmt.Sprintf("go-cuckoo: Capacity must be non-negative, got %d", value))
|
||||
}
|
||||
|
||||
return func(s *settings) { s.bucketSize = uint64(value) }
|
||||
}
|
||||
|
||||
// MinimumLoad modifies the [DefaultMinimumLoad] of the [Table]. The value must
|
||||
// be between 0.00 and 1.00.
|
||||
//
|
||||
// The higher the minimum load, the more likely that a [Table.Put] will not
|
||||
// succeed. Minimum loads above 20% are not tested.
|
||||
func MinimumLoad(value float64) Option {
|
||||
if value < 0.00 || value > 1.00 {
|
||||
panic(fmt.Sprintf("go-cuckoo: MinimumLoad must be between 0.00 and 1.00, got %f", value))
|
||||
}
|
||||
|
||||
return func(s *settings) { s.minLoadFactor = value }
|
||||
}
|
||||
|
||||
// GrowthFactor controls how much the capacity of the [Table] multiplies when
|
||||
// it must resize. The value must be greater than 1.
|
||||
func GrowthFactor(value int) Option {
|
||||
if value < 2 {
|
||||
panic(fmt.Sprintf("go-cuckoo: GrowthFactor must be greater than 1, got %d", value))
|
||||
}
|
||||
|
||||
return func(s *settings) { s.growthFactor = uint64(value) }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user