diff --git a/.gitignore b/.gitignore index 66fd13c..118d10b 100644 --- a/.gitignore +++ b/.gitignore @@ -13,3 +13,7 @@ # Dependency directories (remove the comment below to include it) # vendor/ + +.idea/ + +.DS_Store \ No newline at end of file diff --git a/README.md b/README.md index f8790cb..e47addf 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@

go version -version +version version

@@ -39,8 +39,8 @@ import ( ) func main() { - h := pyramid.NewHeap[int](func(a any, b any) bool { - return a.(int) > b.(int) + h := pyramid.NewHeap[int](func(a int, b int) bool { + return a > b }) h.Push(2) @@ -64,8 +64,8 @@ type Data struct { } func main() { - h := pyramid.NewHeap[Data](func(a any, b any) bool { - return a.(Data).Priority < b.(Data).Priority + h := pyramid.NewHeap[Data](func(a Data, b Data) bool { + return a.Priority < b.Priority }) for i := 0; i < 10; i++ { @@ -92,8 +92,8 @@ for i := 2; i < 100; i++ { h.Push(i) } -h.Update(special, 0, func(a any, b any) bool { - return a.(int) == b.(int) +h.Update(special, 0, func(a int, b int) bool { + return a == b }) ``` diff --git a/example/int/main.go b/example/int/main.go index 00242c2..f4e1800 100644 --- a/example/int/main.go +++ b/example/int/main.go @@ -7,8 +7,8 @@ import ( ) func main() { - h := pyramid.NewHeap[int](func(a any, b any) bool { - return a.(int) > b.(int) + h := pyramid.NewHeap[int](func(a int, b int) bool { + return a > b }) h.Push(2) diff --git a/example/struct/main.go b/example/struct/main.go index a677385..63b559a 100644 --- a/example/struct/main.go +++ b/example/struct/main.go @@ -12,8 +12,8 @@ type Data struct { } func main() { - h := pyramid.NewHeap[Data](func(a any, b any) bool { - return a.(Data).Priority < b.(Data).Priority + h := pyramid.NewHeap[Data](func(a Data, b Data) bool { + return a.Priority < b.Priority }) for i := 0; i < 10; i++ { diff --git a/example/update/complex/main.go b/example/update/complex/main.go index 10c9d36..a11b1f9 100644 --- a/example/update/complex/main.go +++ b/example/update/complex/main.go @@ -12,8 +12,8 @@ type Data struct { } func main() { - h := pyramid.NewHeap[*Data](func(a any, b any) bool { - return a.(*Data).Priority < b.(*Data).Priority + h := pyramid.NewHeap[*Data](func(a *Data, b *Data) bool { + return a.Priority < b.Priority }) special := &Data{ @@ -29,8 +29,8 @@ func main() { special.Priority = 0 - h.Update(special, special, func(a any, b any) bool { - return a.(*Data).Data == b.(*Data).Data + h.Update(special, special, func(a *Data, b *Data) bool { + return a.Data == b.Data }) for h.Length() > 0 { diff --git a/example/update/simple/main.go b/example/update/simple/main.go index 7b9d2c5..5c8713e 100644 --- a/example/update/simple/main.go +++ b/example/update/simple/main.go @@ -7,8 +7,8 @@ import ( ) func main() { - h := pyramid.NewHeap[int](func(a any, b any) bool { - return a.(int) < b.(int) + h := pyramid.NewHeap[int](func(a int, b int) bool { + return a < b }) special := 675 @@ -19,8 +19,8 @@ func main() { h.Push(i) } - h.Update(special, 0, func(a any, b any) bool { - return a.(int) == b.(int) + h.Update(special, 0, func(a int, b int) bool { + return a == b }) for h.Length() > 0 { diff --git a/heap.go b/heap.go index a52d41e..9912f50 100644 --- a/heap.go +++ b/heap.go @@ -31,7 +31,7 @@ func (h *Heap[T]) Length() int { // Update // the queue list. -func (h *Heap[T]) Update(old any, new any, ef equalFunction) { +func (h *Heap[T]) Update(old any, new any, ef equalFunction[T]) { index := h.find(old, ef) if index == -1 { return @@ -40,7 +40,7 @@ func (h *Heap[T]) Update(old any, new any, ef equalFunction) { h.queue.update(h.queue.list[index], new.(T)) } -func (h *Heap[T]) find(object any, equalFunction equalFunction) int { +func (h *Heap[T]) find(object any, equalFunction equalFunction[T]) int { for index, obj := range h.queue.list { if equalFunction(obj.value, object.(T)) { return index @@ -52,7 +52,7 @@ func (h *Heap[T]) find(object any, equalFunction equalFunction) int { // NewHeap // creates a new heap of any type. -func NewHeap[T any](compareFunction compareFunction) Heap[T] { +func NewHeap[T any](compareFunction compareFunction[T]) Heap[T] { var pq Heap[T] // setting the compare function diff --git a/load-test/push-pops/main.go b/load-test/push-pops/main.go index 6f85e95..526da3a 100644 --- a/load-test/push-pops/main.go +++ b/load-test/push-pops/main.go @@ -17,8 +17,8 @@ func main() { flag.Parse() - h := pyramid.NewHeap[int](func(a any, b any) bool { - return a.(int) < b.(int) + h := pyramid.NewHeap[int](func(a int, b int) bool { + return a < b }) fmt.Printf("testing: %d numbers\n", *flNumberOfPop+*flNumberOfPush) diff --git a/load-test/push/main.go b/load-test/push/main.go index 60e4419..b7dbb1d 100644 --- a/load-test/push/main.go +++ b/load-test/push/main.go @@ -16,8 +16,8 @@ func main() { flag.Parse() - h := pyramid.NewHeap[int](func(a any, b any) bool { - return a.(int) < b.(int) + h := pyramid.NewHeap[int](func(a int, b int) bool { + return a < b }) fmt.Printf("testing: %d numbers\n", *flNumberOfPush) diff --git a/load-test/update/main.go b/load-test/update/main.go index 37a51b8..3e11211 100644 --- a/load-test/update/main.go +++ b/load-test/update/main.go @@ -24,8 +24,8 @@ func main() { var items []*Data - h := pyramid.NewHeap[*Data](func(a any, b any) bool { - return a.(*Data).value < b.(*Data).value + h := pyramid.NewHeap[*Data](func(a *Data, b *Data) bool { + return a.value < b.value }) fmt.Printf("testing: %d numbers\n", *flNumberOfPush) @@ -48,8 +48,8 @@ func main() { value: rand.Int() % 100000, } - h.Update(items[index], newItem, func(a any, b any) bool { - return a.(*Data).id == b.(*Data).id + h.Update(items[index], newItem, func(a *Data, b *Data) bool { + return a.id == b.id }) } diff --git a/queue.go b/queue.go index 77ee922..af8d6f9 100644 --- a/queue.go +++ b/queue.go @@ -6,7 +6,7 @@ import "container/heap" // is a list of items. type Queue[T any] struct { list []*item[T] - compareFunction compareFunction + compareFunction compareFunction[T] } // Len diff --git a/test/heap_test.go b/test/heap_test.go index 52057a3..6c89aca 100644 --- a/test/heap_test.go +++ b/test/heap_test.go @@ -10,8 +10,8 @@ import ( // testing heap data structure with int64 type. func TestHeap(t *testing.T) { // creating a new heap - h := pyramid.NewHeap[int64](func(a any, b any) bool { - return a.(int64) < b.(int64) + h := pyramid.NewHeap[int64](func(a int64, b int64) bool { + return a < b }) // push some data into heap @@ -37,8 +37,8 @@ func TestValidType(t *testing.T) { value int } - h := pyramid.NewHeap[item](func(a any, b any) bool { - return a.(item).value < b.(item).value + h := pyramid.NewHeap[item](func(a item, b item) bool { + return a.value < b.value }) h.Push(item{ diff --git a/type.go b/type.go index 69b5f4a..9680967 100644 --- a/type.go +++ b/type.go @@ -10,8 +10,8 @@ type item[T any] struct { // compareFunction // is used to compare items in priority queue. -type compareFunction func(any, any) bool +type compareFunction[T any] func(T, T) bool // equalFunction // is used to update elements in priority queue. -type equalFunction func(any, any) bool +type equalFunction[T any] func(T, T) bool