easyheap

package module
v0.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 23, 2023 License: MIT Imports: 1 Imported by: 0

README

Easyheaps

Easyheaps asks you to provide only the comparison function for the items you want to put in the heap. It exposes only Push, Pop and Len. This is sufficient for everyday coding. container/heap packge allows you to do much more so you can switch to that if needed.


type foo struct {
	name string
}

func ExampleReverse() {

    type lexfoo foo
	
	h := easyheap.NewHeap[*lexfoo](
		func(a, b *lexfoo) bool {
			return a.name < b.name
		},
		&lexfoo{"today"},
		&lexfoo{"is"},
		&lexfoo{"the"},
		&lexfoo{"day"},
	)

	for h.Len() > 0 {
		fmt.Println(h.Pop().name)
	}

	// Output:
	//day
	//is
	//the
	//today
}

This package exists because I don't fancy typing this again.

	old := *h
	n := len(old)
	x := old[n-1]
	*h = old[0 : n-1]
	return x

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Heap

type Heap[E any] struct {
	// contains filtered or unexported fields
}

func NewHeap

func NewHeap[E any](less func(E, E) bool, es ...E) Heap[E]

func (Heap[E]) Len

func (e Heap[E]) Len() int

func (*Heap[E]) Pop

func (e *Heap[E]) Pop() E

func (*Heap[E]) Push

func (e *Heap[E]) Push(x E)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL