Compare commits
6 Commits
v0.0.1
...
e2ba398a62
| Author | SHA1 | Date | |
|---|---|---|---|
|
e2ba398a62
|
|||
|
c1314f8a3c
|
|||
|
74ed81761c
|
|||
|
d4acdda95b
|
|||
| 2bfaede2d4 | |||
| ece10585a5 |
@@ -6,8 +6,8 @@ on:
|
|||||||
pull_request:
|
pull_request:
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
lint:
|
lint-go:
|
||||||
name: Golang Lint
|
name: Go Lint
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v4
|
- uses: actions/checkout@v4
|
||||||
@@ -23,7 +23,23 @@ jobs:
|
|||||||
with:
|
with:
|
||||||
version: latest
|
version: latest
|
||||||
|
|
||||||
unit-test:
|
lint-makefile:
|
||||||
|
name: Makefile Lint
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v4
|
||||||
|
|
||||||
|
- uses: actions/setup-go@v5
|
||||||
|
with:
|
||||||
|
go-version-file: go.mod
|
||||||
|
|
||||||
|
- name: Install gremlins
|
||||||
|
run: go install github.com/checkmake/checkmake/cmd/checkmake@latest
|
||||||
|
|
||||||
|
- name: Run mutation tests
|
||||||
|
run: make lint-makefile
|
||||||
|
|
||||||
|
test-unit:
|
||||||
name: Unit Tests
|
name: Unit Tests
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
@@ -34,9 +50,9 @@ jobs:
|
|||||||
go-version-file: go.mod
|
go-version-file: go.mod
|
||||||
|
|
||||||
- name: Run unit tests
|
- name: Run unit tests
|
||||||
run: go test ./... -cover -v
|
run: make test-unit
|
||||||
|
|
||||||
fuzz-test:
|
test-fuzz:
|
||||||
name: Fuzz Tests
|
name: Fuzz Tests
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
@@ -47,12 +63,9 @@ jobs:
|
|||||||
go-version-file: go.mod
|
go-version-file: go.mod
|
||||||
|
|
||||||
- name: Run fuzz tests
|
- name: Run fuzz tests
|
||||||
run: |
|
run: make test-fuzz
|
||||||
for func in $(grep -r --include='*_test.go' -oh 'func Fuzz\w*' . | sed 's/func //'); do
|
|
||||||
go test ./... -fuzz="^${func}$" -fuzztime=30s
|
|
||||||
done
|
|
||||||
|
|
||||||
mutation-test:
|
test-mutation:
|
||||||
name: Mutation Tests
|
name: Mutation Tests
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
@@ -66,4 +79,4 @@ jobs:
|
|||||||
run: go install github.com/go-gremlins/gremlins/cmd/gremlins@latest
|
run: go install github.com/go-gremlins/gremlins/cmd/gremlins@latest
|
||||||
|
|
||||||
- name: Run mutation tests
|
- name: Run mutation tests
|
||||||
run: gremlins unleash
|
run: make test-mutation
|
||||||
|
|||||||
42
Makefile
42
Makefile
@@ -1,12 +1,42 @@
|
|||||||
unit:
|
.PHONY: all help install clean test-unit test-mutation test-fuzz test docs lint-go lint-makefile lint
|
||||||
|
|
||||||
|
help: ## Show this help
|
||||||
|
@grep -E '^[a-zA-Z_-]+:.*##' $(MAKEFILE_LIST) | awk -F ':.*## ' '{printf " %-15s %s\n", $$1, $$2}'
|
||||||
|
|
||||||
|
install: ## Install dev tools
|
||||||
|
go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest
|
||||||
|
go install github.com/checkmake/checkmake/cmd/checkmake@latest
|
||||||
|
go install github.com/go-gremlins/gremlins/cmd/gremlins@latest
|
||||||
|
go install golang.org/x/tools/cmd/godoc@latest
|
||||||
|
go install golang.org/x/vuln/cmd/govulncheck@latest
|
||||||
|
|
||||||
|
test-unit: ## Run unit tests with coverage
|
||||||
go test ./... -cover -v
|
go test ./... -cover -v
|
||||||
|
|
||||||
mutation:
|
test-mutation: ## Run mutation tests with gremlins
|
||||||
gremlins unleash
|
gremlins unleash
|
||||||
|
|
||||||
fuzz:
|
test-fuzz: ## Run all fuzz tests for 30s each
|
||||||
go test ./... -fuzz=$(FN)
|
@for func in $$(grep -r --include='*_test.go' -oh 'func Fuzz\w*' . | sed 's/func //'); do \
|
||||||
|
echo "Fuzzing $$func..."; \
|
||||||
|
go test ./... -fuzz="^$$func$$" -fuzztime=30s; \
|
||||||
|
done
|
||||||
|
|
||||||
docs:
|
test: test-unit test-mutation test-fuzz ## Run all tests
|
||||||
@echo ">>> Visit: http://localhost:6060/pkg/git.maximhutz.com/tools/dsa/"
|
|
||||||
|
lint-go: ## Lint Go code
|
||||||
|
golangci-lint run ./...
|
||||||
|
|
||||||
|
lint-makefile: ## Lint the Makefile
|
||||||
|
checkmake Makefile
|
||||||
|
|
||||||
|
lint: lint-go lint-makefile ## Lint all code
|
||||||
|
|
||||||
|
docs: ## Serve godoc locally
|
||||||
|
@echo ">>> Visit: http://localhost:6060/pkg/$$(go list -m)"
|
||||||
godoc -http=:6060
|
godoc -http=:6060
|
||||||
|
|
||||||
|
clean: ## Clean build and test caches
|
||||||
|
go clean -cache -testcache
|
||||||
|
|
||||||
|
all: lint test ## Run all checks and tests
|
||||||
12
bucket.go
12
bucket.go
@@ -26,6 +26,18 @@ func (b bucket[K, V]) get(key K) (value V, found bool) {
|
|||||||
return slot.value, slot.occupied && b.compare(slot.key, key)
|
return slot.value, slot.occupied && b.compare(slot.key, key)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (b *bucket[K, V]) drop(key K) (occupied bool) {
|
||||||
|
slot := &b.slots[b.location(key)]
|
||||||
|
|
||||||
|
if slot.occupied && b.compare(slot.key, key) {
|
||||||
|
slot.occupied = false
|
||||||
|
b.size--
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
func (b *bucket[K, V]) resize(capacity uint64) {
|
func (b *bucket[K, V]) resize(capacity uint64) {
|
||||||
b.slots = make([]slot[K, V], capacity)
|
b.slots = make([]slot[K, V], capacity)
|
||||||
b.capacity = capacity
|
b.capacity = capacity
|
||||||
|
|||||||
@@ -25,6 +25,6 @@ func TestLoad(t *testing.T) {
|
|||||||
for i := range 16 {
|
for i := range 16 {
|
||||||
err := table.Put(i, true)
|
err := table.Put(i, true)
|
||||||
assert.NoError(err)
|
assert.NoError(err)
|
||||||
assert.Equal(float64(table.Size())/float64(table.Capacity()), table.load())
|
assert.Equal(float64(table.Size())/float64(table.TotalCapacity()), table.load())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -64,7 +64,7 @@ func TestStartingCapacity(t *testing.T) {
|
|||||||
assert := assert.New(t)
|
assert := assert.New(t)
|
||||||
table := cuckoo.NewTable[int, bool](cuckoo.Capacity(64))
|
table := cuckoo.NewTable[int, bool](cuckoo.Capacity(64))
|
||||||
|
|
||||||
assert.Equal(uint64(128), table.Capacity())
|
assert.Equal(uint64(128), table.TotalCapacity())
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestResizeCapacity(t *testing.T) {
|
func TestResizeCapacity(t *testing.T) {
|
||||||
@@ -74,12 +74,12 @@ func TestResizeCapacity(t *testing.T) {
|
|||||||
cuckoo.GrowthFactor(2),
|
cuckoo.GrowthFactor(2),
|
||||||
)
|
)
|
||||||
|
|
||||||
for table.Capacity() == 16 {
|
for table.TotalCapacity() == 16 {
|
||||||
err := table.Put(rand.Int(), true)
|
err := table.Put(rand.Int(), true)
|
||||||
assert.NoError(err)
|
assert.NoError(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
assert.Equal(uint64(32), table.Capacity())
|
assert.Equal(uint64(32), table.TotalCapacity())
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestPutMany(t *testing.T) {
|
func TestPutMany(t *testing.T) {
|
||||||
@@ -128,3 +128,16 @@ func TestRemove(t *testing.T) {
|
|||||||
|
|
||||||
assert.True(table.Has(0))
|
assert.True(table.Has(0))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestDropItem(t *testing.T) {
|
||||||
|
assert := assert.New(t)
|
||||||
|
key, value := 0, true
|
||||||
|
table := cuckoo.NewTable[int, bool]()
|
||||||
|
(table.Put(key, value))
|
||||||
|
|
||||||
|
err := table.Drop(key)
|
||||||
|
|
||||||
|
assert.NoError(err)
|
||||||
|
assert.Equal(0, table.Size())
|
||||||
|
assert.False(table.Has(key))
|
||||||
|
}
|
||||||
|
|||||||
31
table.go
31
table.go
@@ -16,9 +16,9 @@ type Table[K, V any] struct {
|
|||||||
minLoadFactor float64
|
minLoadFactor float64
|
||||||
}
|
}
|
||||||
|
|
||||||
// Capacity returns the number of slots allocated for the [Table]. To get the
|
// TotalCapacity returns the number of slots allocated for the [Table]. To get the
|
||||||
// number of slots filled, look at [Table.Size].
|
// number of slots filled, look at [Table.Size].
|
||||||
func (t Table[K, V]) Capacity() uint64 {
|
func (t Table[K, V]) TotalCapacity() uint64 {
|
||||||
return t.bucketA.capacity + t.bucketB.capacity
|
return t.bucketA.capacity + t.bucketB.capacity
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -32,21 +32,21 @@ func log2(n uint64) (m int) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (t Table[K, V]) maxEvictions() int {
|
func (t Table[K, V]) maxEvictions() int {
|
||||||
return 3 * log2(t.Capacity())
|
return 3 * log2(t.TotalCapacity())
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t Table[K, V]) load() float64 {
|
func (t Table[K, V]) load() float64 {
|
||||||
return float64(t.Size()) / float64(t.Capacity())
|
return float64(t.Size()) / float64(t.TotalCapacity())
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *Table[K, V]) resize() error {
|
func (t *Table[K, V]) resize(capacity uint64) error {
|
||||||
entries := make([]entry[K, V], 0, t.Size())
|
entries := make([]entry[K, V], 0, t.Size())
|
||||||
for k, v := range t.Entries() {
|
for k, v := range t.Entries() {
|
||||||
entries = append(entries, entry[K, V]{k, v})
|
entries = append(entries, entry[K, V]{k, v})
|
||||||
}
|
}
|
||||||
|
|
||||||
t.bucketA.resize(t.growthFactor * t.bucketA.capacity)
|
t.bucketA.resize(capacity)
|
||||||
t.bucketB.resize(t.growthFactor * t.bucketB.capacity)
|
t.bucketB.resize(capacity)
|
||||||
|
|
||||||
for _, entry := range entries {
|
for _, entry := range entries {
|
||||||
if err := t.Put(entry.key, entry.value); err != nil {
|
if err := t.Put(entry.key, entry.value); err != nil {
|
||||||
@@ -99,10 +99,10 @@ func (t *Table[K, V]) Put(key K, value V) (err error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if t.load() < t.minLoadFactor {
|
if t.load() < t.minLoadFactor {
|
||||||
return fmt.Errorf("bad hash: resize on load %d/%d = %f", t.Size(), t.Capacity(), t.load())
|
return fmt.Errorf("bad hash: resize on load %d/%d = %f", t.Size(), t.TotalCapacity(), t.load())
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := t.resize(); err != nil {
|
if err := t.resize(t.growthFactor * t.bucketA.capacity); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -111,10 +111,15 @@ func (t *Table[K, V]) Put(key K, value V) (err error) {
|
|||||||
|
|
||||||
// Drop removes a value for a key in the table. Returns an error if its value
|
// Drop removes a value for a key in the table. Returns an error if its value
|
||||||
// cannot be removed.
|
// cannot be removed.
|
||||||
//
|
func (t *Table[K, V]) Drop(key K) (err error) {
|
||||||
// Deprecated: Do not use.
|
t.bucketA.drop(key)
|
||||||
func (t Table[K, V]) Drop(_ K) {
|
t.bucketB.drop(key)
|
||||||
panic("Not implemented")
|
|
||||||
|
if t.load() < t.minLoadFactor {
|
||||||
|
return t.resize(t.bucketA.capacity / t.growthFactor)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Entries returns an unordered sequence of all key-value pairs in the table.
|
// Entries returns an unordered sequence of all key-value pairs in the table.
|
||||||
|
|||||||
Reference in New Issue
Block a user