easyheap

package module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Apr 12, 2025 License: MIT Imports: 3 Imported by: 0

README

Easyheap

Go

package main

import (
	"fmt"

	"github.com/pratikdeoghare/easyheap"
)

func main() {

	type Player struct {
		Name string
	}

	scores := make(map[string]int)

	getPriority := func(x *Player) int {
		return scores[x.Name]
	}

	setPriority := func(x *Player, score int) {
		scores[x.Name] = score
	}

	players := []*Player{
		&Player{"Jan"},
		&Player{"Johnny"},
		&Player{"Janardan"},
		&Player{"Tara"},
		&Player{"Ram"},
		&Player{"Pam"},
	}

	for i, p := range players {
		setPriority(p, -i*200)
	}

	h := easyheap.NewHeap(getPriority, setPriority, players...)

	for h.Len() > 0 {
		x := h.Pop()
		fmt.Println(x)
	}

}

This package exists because I don't fancy typing this all the time.

	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[P cmp.Ordered, E comparable] struct {
	// contains filtered or unexported fields
}

func NewHeap

func NewHeap[P cmp.Ordered, E comparable](getp func(e *E) P, setp func(e *E, p P), es ...*E) Heap[P, E]

func (Heap[P, E]) Fix added in v0.0.2

func (e Heap[P, E]) Fix(x *E, priority P) bool

func (Heap[P, E]) Len

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

func (*Heap[P, E]) Pop

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

func (*Heap[P, E]) Push

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

func (Heap[P, E]) Remove added in v0.0.2

func (e Heap[P, E]) Remove(x *E) (*E, bool)

func (Heap[P, E]) String added in v0.0.2

func (e Heap[P, E]) String() string

Directories

Path Synopsis
cmd
example command

Jump to

Keyboard shortcuts

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