Skip to content

Commit

Permalink
Merge pull request #2 from amirhnajafiz/1-generic-comparing-function
Browse files Browse the repository at this point in the history
1 generic comparing function
  • Loading branch information
amirhnajafiz authored Oct 18, 2022
2 parents 5b55403 + f540ad6 commit 0840b9f
Show file tree
Hide file tree
Showing 13 changed files with 41 additions and 37 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,7 @@

# Dependency directories (remove the comment below to include it)
# vendor/

.idea/

.DS_Store
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<p align="center">
<img src="https://img.shields.io/badge/Golang-1.19-66ADD8?style=for-the-badge&logo=go" alt="go version" />
<img src="https://img.shields.io/badge/Version-0.1.2-DD1199?style=for-the-badge&logo=github" alt="version" />
<img src="https://img.shields.io/badge/Version-0.1.3-DD1199?style=for-the-badge&logo=github" alt="version" />
<img src="https://img.shields.io/badge/Load_Test-1M-442266?style=for-the-badge&logo=k6" alt="version" />
<br />
</p>
Expand Down Expand Up @@ -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)
Expand All @@ -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++ {
Expand All @@ -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
})
```

Expand Down
4 changes: 2 additions & 2 deletions example/int/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions example/struct/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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++ {
Expand Down
8 changes: 4 additions & 4 deletions example/update/complex/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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 {
Expand Down
8 changes: 4 additions & 4 deletions example/update/simple/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand Down
6 changes: 3 additions & 3 deletions heap.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions load-test/push-pops/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions load-test/push/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 4 additions & 4 deletions load-test/update/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
})
}

Expand Down
2 changes: 1 addition & 1 deletion queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions test/heap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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{
Expand Down
4 changes: 2 additions & 2 deletions type.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 0840b9f

Please sign in to comment.