An open API service indexing awesome lists of open source software.

https://github.com/varugasu/go-heap

Heap implementation based on container/heap using Generics
https://github.com/varugasu/go-heap

data-structures generics go golang heap

Last synced: 2 months ago
JSON representation

Heap implementation based on container/heap using Generics

Awesome Lists containing this project

README

        

# Heap with Generics

A library to work with **Heaps** using **Generics**

The current heap solution in `container/heap` relies on the following interfaces:

```go
package sort

type Interface interface {
Len() int
Less(i, j int) bool
Swap(i, j int)
}
```

```go
package heap

type Interface interface {
sort.Interface
Push(x any)
Pop() any
}
```

But with generics with **do not need** to **implement Interfaces**. Any slice will work as long as we define a `Less` function.

```go
type Less[T any] func(t1, t2 T) bool

func Heapify[T any](t []T, less Less[T])
```

This `Less` function allow us to create **any kind of Heap**, even with **custom algorithms** for complex data structures