pqs

package
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: May 24, 2025 License: MIT Imports: 2 Imported by: 0

README

pqs GoDoc Go Report Card

A minimal, generic priority queue in Go using ordered types.

The pqs package provides the simplest form of a priority queue for values that are inherently comparable (e.g., int, float64, string). It does not support key-based lookups or custom structures — items are their own priority.


✨ Features

  • ✅ Minimal: single-type input, no struct wrapping
  • ✅ Generic: works with any cmp.Ordered type
  • ✅ Custom comparator: control min/max or custom logic
  • ❌ No stability guarantees (insertion order not preserved for equal priority)
  • ❌ No key support or item updates

🧱 Example

package main

import (
	"fmt"
	"github.com/byExist/priorityqueues/pqs"
)

func main() {
	q := pqs.New(pqs.MinFirst[int])
	pqs.Enqueue(q, 3)
	pqs.Enqueue(q, 1)
	pqs.Enqueue(q, 2)

	for pqs.Len(q) > 0 {
		item, _ := pqs.Dequeue(q)
		fmt.Println(item)
	}
}

// Output:
// 1
// 2
// 3

📚 Use When

  • You need a fast, generic min/max heap
  • The item itself is the priority
  • You don’t need key lookups, updates, or custom types

🚫 Avoid If

  • You need to update item priority (kmpqs, kpqs)
  • You need key-based identity (kpqs, kmpqs)
  • You want stable ordering among equal-priority items (mpqs, kpqs)

🔍 Alternatives

Package Use case
mpqs custom structs with external priority
kpqs key + internal priority field
kmpqs key + external priority control

Documentation

Overview

Example (StringLengthPriority)
package main

import (
	"fmt"

	"github.com/byExist/priorityqueues/pqs"
)

func main() {
	lengthPriority := func(x, y string) bool {
		return len(x) < len(y)
	}

	pq := pqs.New(lengthPriority)
	pqs.Enqueue(pq, "apple")
	pqs.Enqueue(pq, "kiwi")
	pqs.Enqueue(pq, "banana")

	for pqs.Len(pq) > 0 {
		item, _ := pqs.Dequeue(pq)
		fmt.Println(item)
	}
}
Output:

kiwi
apple
banana

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Clear

func Clear[T cmp.Ordered](pq *PriorityQueue[T])
Example
package main

import (
	"fmt"

	"github.com/byExist/priorityqueues/pqs"
)

func main() {
	pq := pqs.New(pqs.MinFirst[int])
	pqs.Enqueue(pq, 1)
	pqs.Clear(pq)
	fmt.Println(pqs.Len(pq))
}
Output:

0

func Dequeue

func Dequeue[T cmp.Ordered](pq *PriorityQueue[T]) (T, bool)
Example
package main

import (
	"fmt"

	"github.com/byExist/priorityqueues/pqs"
)

func main() {
	pq := pqs.New(pqs.MinFirst[int])
	pqs.Enqueue(pq, 2)
	pqs.Enqueue(pq, 1)
	item, _ := pqs.Dequeue(pq)
	fmt.Println(item)
}
Output:

1

func Enqueue

func Enqueue[T cmp.Ordered](pq *PriorityQueue[T], item T)
Example
package main

import (
	"fmt"

	"github.com/byExist/priorityqueues/pqs"
)

func main() {
	pq := pqs.New(pqs.MinFirst[int])
	pqs.Enqueue(pq, 5)
	item, _ := pqs.Peek(pq)
	fmt.Println(item)
}
Output:

5

func Len

func Len[T cmp.Ordered](pq *PriorityQueue[T]) int
Example
package main

import (
	"fmt"

	"github.com/byExist/priorityqueues/pqs"
)

func main() {
	pq := pqs.New(pqs.MinFirst[int])
	fmt.Println(pqs.Len(pq))
	pqs.Enqueue(pq, 1)
	fmt.Println(pqs.Len(pq))
}
Output:

0
1

func MaxFirst

func MaxFirst[T cmp.Ordered](x, y T) bool
Example
package main

import (
	"fmt"

	"github.com/byExist/priorityqueues/pqs"
)

func main() {
	fmt.Println(pqs.MaxFirst(1, 2))
}
Output:

false

func MinFirst

func MinFirst[T cmp.Ordered](x, y T) bool
Example
package main

import (
	"fmt"

	"github.com/byExist/priorityqueues/pqs"
)

func main() {
	fmt.Println(pqs.MinFirst(1, 2))
}
Output:

true

func Peek

func Peek[T cmp.Ordered](pq *PriorityQueue[T]) (T, bool)
Example
package main

import (
	"fmt"

	"github.com/byExist/priorityqueues/pqs"
)

func main() {
	pq := pqs.New(pqs.MinFirst[int])
	pqs.Enqueue(pq, 7)
	item, _ := pqs.Peek(pq)
	fmt.Println(item)
}
Output:

7

Types

type PriorityQueue

type PriorityQueue[T cmp.Ordered] struct {
	// contains filtered or unexported fields
}

func New

func New[T cmp.Ordered](
	lessFunc func(x, y T) bool,
) *PriorityQueue[T]
Example
package main

import (
	"fmt"

	"github.com/byExist/priorityqueues/pqs"
)

func main() {
	pq := pqs.New(pqs.MinFirst[int])
	pqs.Enqueue(pq, 3)
	pqs.Enqueue(pq, 1)
	item, _ := pqs.Dequeue(pq)
	fmt.Println(item)
}
Output:

1

Jump to

Keyboard shortcuts

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