Skip to content

newblue/binheap

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

[ What ]

A small binary-heap for golang with a simple interface


<code>
type Heapable struct {
    Priority() int
}

if a.Priority() < b.Priority() {
    fmt.Println(" a is more urgent than b")
}
</code>


[ Install ]

goinstall github.com/bjarneh/binheap


[ Example ]

<code>

type Job struct {
    priority int
    // probably more stuff..
}

func (j *Job) Priority() int {
    return j.priority
}

heap := binheap.New()

heap.Add(&Job{4})
heap.Add(&Job{1})
heap.Add(&Job{10})
.
.

mostUrgent := heap.Remove() // Job{1}

// a slice of Heapable elements can be added as well

var jobs []Heapable = make([]Heapable, 0)

jobs = append(jobs, &Job{8})
jobs = append(jobs, &Job{19})
.
.

heap.AddSlice(jobs)

// heap-sort, slower than sort.Sort (quick-sort)

binheap.Sort(jobs) // any []Heapable slice will do


</code>

About

golang binary heap with a simple interface

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published