minheap

package
v0.8.0 Latest Latest
Warning

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

Go to latest
Published: Sep 15, 2024 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Example
package main

import (
	"fmt"

	"github.com/joetifa2003/mm-go/allocator"
	"github.com/joetifa2003/mm-go/minheap"
)

func int_less(a, b int) bool { return a < b }

func main() {
	alloc := allocator.NewC()
	defer alloc.Destroy()

	h := minheap.New[int](alloc, int_less)

	// Push some values onto the heap
	h.Push(2)
	h.Push(1)
	h.Push(4)
	h.Push(3)
	h.Push(5)

	// Pop the minimum value from the heap
	fmt.Println(h.Pop())
	fmt.Println(h.Pop())

}
Output:

1
2

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type MinHeap

type MinHeap[T any] struct {
	// contains filtered or unexported fields
}

func New

func New[T any](alloc allocator.Allocator, less func(a, b T) bool) *MinHeap[T]

New creates a new MinHeap.

func (*MinHeap[T]) Free

func (h *MinHeap[T]) Free()

Free frees the heap.

func (*MinHeap[T]) Iter

func (h *MinHeap[T]) Iter() iter.Seq2[int, T]

Iter returns an iterator over the elements of the heap.

func (*MinHeap[T]) Len

func (h *MinHeap[T]) Len() int

Len returns the number of elements in the heap.

func (*MinHeap[T]) Peek

func (h *MinHeap[T]) Peek() T

Peek returns the minimum value from the heap without removing it.

func (*MinHeap[T]) Pop

func (h *MinHeap[T]) Pop() T

Pop removes and returns the minimum value from the heap.

func (*MinHeap[T]) Push

func (h *MinHeap[T]) Push(value T)

Push adds a value to the heap.

func (*MinHeap[T]) Remove

func (h *MinHeap[T]) Remove(f func(T) bool)

Remove the first element that makes f return true

Jump to

Keyboard shortcuts

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