diff --git a/README.md b/README.md index f473943..204c88f 100644 --- a/README.md +++ b/README.md @@ -114,13 +114,13 @@ goos: linux goarch: amd64 pkg: github.com/s0rg/grid cpu: AMD Ryzen 5 5500U with Radeon Graphics -BenchmarkGrid/Set-12 1000000000 1.022 ns/op 0 B/op 0 allocs/op -BenchmarkGrid/Get-12 721531257 1.634 ns/op 0 B/op 0 allocs/op -BenchmarkGrid/Neighbours-12 66094719 18.29 ns/op 0 B/op 0 allocs/op -BenchmarkGrid/LineBresenham-12 5609686 215.2 ns/op 0 B/op 0 allocs/op -BenchmarkGrid/CastRay-12 4111668 283.9 ns/op 0 B/op 0 allocs/op -BenchmarkGrid/CastShadow-12 34016 34912 ns/op 0 B/op 0 allocs/op -BenchmarkGrid/LineOfSight-12 11792 100790 ns/op 0 B/op 0 allocs/op -BenchmarkGrid/DijkstraMap-12 1054 1094518 ns/op 20656 B/op 3 allocs/op -BenchmarkGrid/Path-12 120 10212821 ns/op 14209590 B/op 40388 allocs/op +BenchmarkGrid/Set-12 1000000000 0.8379 ns/op 0 B/op 0 allocs/op +BenchmarkGrid/Get-12 781486358 1.521 ns/op 0 B/op 0 allocs/op +BenchmarkGrid/Neighbours-12 61203862 19.25 ns/op 0 B/op 0 allocs/op +BenchmarkGrid/LineBresenham-12 5331663 223.9 ns/op 0 B/op 0 allocs/op +BenchmarkGrid/CastRay-12 4160371 287.7 ns/op 0 B/op 0 allocs/op +BenchmarkGrid/CastShadow-12 33894 34771 ns/op 0 B/op 0 allocs/op +BenchmarkGrid/LineOfSight-12 11482 104325 ns/op 0 B/op 0 allocs/op +BenchmarkGrid/DijkstraMap-12 1011 1105615 ns/op 20656 B/op 3 allocs/op +BenchmarkGrid/Path-12 166 7458693 ns/op 13347421 B/op 13643 allocs/op ``` diff --git a/go.mod b/go.mod index 270b2ff..266a99f 100644 --- a/go.mod +++ b/go.mod @@ -1,11 +1,15 @@ module github.com/s0rg/grid -go 1.20 +go 1.21 require ( github.com/s0rg/array2d v1.1.1 github.com/s0rg/set v1.0.1 github.com/s0rg/vec2d v1.1.1 + github.com/zyedidia/generic v1.2.1 ) -require golang.org/x/exp v0.0.0-20230801115018-d63ba01acd4b // indirect +require ( + github.com/segmentio/fasthash v1.0.3 // indirect + golang.org/x/exp v0.0.0-20230817173708-d852ddb80c63 // indirect +) diff --git a/go.sum b/go.sum index 15b354a..875535a 100644 --- a/go.sum +++ b/go.sum @@ -4,5 +4,9 @@ github.com/s0rg/set v1.0.1 h1:eJ2KSRUwnIZOBtlQScgET80+jkm/ro01pn6YeZzgWp4= github.com/s0rg/set v1.0.1/go.mod h1:aBGSm+F2mCTDjNIRR4GMB04NMqZBhtSh0tOBe/u5z5k= github.com/s0rg/vec2d v1.1.1 h1:asdV+Iev4M5lcFwB46883k8oLku4JgNQHyI/4SHAtMo= github.com/s0rg/vec2d v1.1.1/go.mod h1:/0pgb+2ax3mWfBZ1gb9LMMFawvmGyWpgCjk8a/ImCgg= -golang.org/x/exp v0.0.0-20230801115018-d63ba01acd4b h1:r+vk0EmXNmekl0S0BascoeeoHk/L7wmaW2QF90K+kYI= -golang.org/x/exp v0.0.0-20230801115018-d63ba01acd4b/go.mod h1:FXUEEKJgO7OQYeo8N01OfiKP8RXMtf6e8aTskBGqWdc= +github.com/segmentio/fasthash v1.0.3 h1:EI9+KE1EwvMLBWwjpRDc+fEM+prwxDYbslddQGtrmhM= +github.com/segmentio/fasthash v1.0.3/go.mod h1:waKX8l2N8yckOgmSsXJi7x1ZfdKZ4x7KRMzBtS3oedY= +github.com/zyedidia/generic v1.2.1 h1:Zv5KS/N2m0XZZiuLS82qheRG4X1o5gsWreGb0hR7XDc= +github.com/zyedidia/generic v1.2.1/go.mod h1:ly2RBz4mnz1yeuVbQA/VFwGjK3mnHGRj1JuoG336Bis= +golang.org/x/exp v0.0.0-20230817173708-d852ddb80c63 h1:m64FZMko/V45gv0bNmrNYoDEq8U5YUhetc9cBWKS1TQ= +golang.org/x/exp v0.0.0-20230817173708-d852ddb80c63/go.mod h1:0v4NqG35kSWCMzLaMeX+IQrlSnVE/bqGSyC2cz/9Le8= diff --git a/grid.go b/grid.go index db93da7..4feaea1 100644 --- a/grid.go +++ b/grid.go @@ -1,13 +1,13 @@ package grid import ( - "container/heap" "image" "math" "github.com/s0rg/array2d" "github.com/s0rg/set" "github.com/s0rg/vec2d" + "github.com/zyedidia/generic/heap" ) const one = 1.0 @@ -131,16 +131,18 @@ func (m *Map[T]) Path( var ( road path - queue pqueue last image.Point closed = make(set.Set[image.Point]) ) - heap.Init(&queue) - heap.Push(&queue, road.Fork(src, tdist)) + queue := heap.New[path](func(a, b path) bool { + return a.Cost < b.Cost + }) + + queue.Push(road.Fork(src, tdist)) - for queue.Len() > 0 { - road = heap.Pop(&queue).(path) + for queue.Size() > 0 { + road, _ = queue.Pop() last = road.Last() if !closed.TryAdd(last) { @@ -155,7 +157,7 @@ func (m *Map[T]) Path( var ncost float64 if ncost, ok = cost(p, dist(dst, p), t); ok { - heap.Push(&queue, road.Fork(p, ncost)) + queue.Push(road.Fork(p, ncost)) } return true diff --git a/pqueue.go b/pqueue.go deleted file mode 100644 index 27a677f..0000000 --- a/pqueue.go +++ /dev/null @@ -1,14 +0,0 @@ -package grid - -type pqueue []path - -func (q *pqueue) Len() int { return len(*q) } -func (q *pqueue) Less(i, j int) bool { return (*q)[i].Cost < (*q)[j].Cost } -func (q *pqueue) Swap(i, j int) { (*q)[i], (*q)[j] = (*q)[j], (*q)[i] } -func (q *pqueue) Push(x any) { *q = append(*q, x.(path)) } - -func (q *pqueue) Pop() (x any) { - *q, x = (*q)[:q.Len()-1], (*q)[q.Len()-1] - - return x -}