ubalgorithms

package
v0.0.25 Latest Latest
Warning

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

Go to latest
Published: Sep 13, 2025 License: MIT Imports: 4 Imported by: 0

README

Data Structures and Algorithms

Least recently used cache (LRUCache)

    // Create a cache with 10 items
    cache := algorithms.NewLRUCache[string, string](10)

    for x := 1; x <= 11; x++ {
        key := fmt.Sprintf("key%d", x)
        value := fmt.Sprintf("value %d", x)
        cache.Put(key, value)
    }

    // Try getting the last item
    item, found := cache.Get("key11")
    if found {
        fmt.Printf("Got value: %s\n", item)
    }

    // The first item ought to have rolled off
    item, found = cache.Get("key1")

    if found {
        fmt.Printf("Ooops, we shouldn't be here.\n")
    } else {
        fmt.Printf("Cache miss.\n")
    }

PriorityQueue

A queue which returns the higher priority first (those with smaller priority values).

    // Generate a priority queue with an initial capacity of 10
    //
    // The first type parameter is the value, the second is the
    // priority.
    queue := algorithms.NewPriorityQueue[string, int](10)

    // Push some items with their priority. Smaller values have
    // higher priority.
    queue.Push("Three", 3)
    queue.Push("One", 1)
    queue.Push("Two", 2)

    // Should print:
    // One
    // Two
    // Three
    for queue.Size() > 0 {
        value, err := queue.Pop()
        if err != nil {
            panic(err)
        }
        fmt.Println(*value)
    }

Documentation

Overview

The algorithms package contains useful data structures for re-use

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type LRUCache

type LRUCache[K comparable, V any] struct {
	// contains filtered or unexported fields
}

LRUCache represents the cache structure.

func NewLRUCache

func NewLRUCache[K comparable, V any](capacity int) *LRUCache[K, V]

NewLRUCache initializes a new LRUCache with a given capacity.

func (*LRUCache[K, V]) Get

func (this *LRUCache[K, V]) Get(key K) (V, bool)

Get retrieves the value of the key if it exists in the cache, otherwise returns zero value and false.

func (*LRUCache[K, V]) Put

func (this *LRUCache[K, V]) Put(key K, value V)

Put inserts or updates the value of the key. If the cache exceeds capacity, it removes the least recently used item.

func (*LRUCache[K, V]) Remove

func (this *LRUCache[K, V]) Remove(key K) bool

Remove removes a node from the cache by key if it exists.

type PriorityQueue

type PriorityQueue[T comparable] struct {
	// contains filtered or unexported fields
}

func NewMaxCostPriorityQueue

func NewMaxCostPriorityQueue[T comparable](capacity int) *PriorityQueue[T]

func NewMinCostPriorityQueue

func NewMinCostPriorityQueue[T comparable](capacity int) *PriorityQueue[T]

func (*PriorityQueue[T]) Len

func (q *PriorityQueue[T]) Len() int

Get the current size of the priority queue

func (*PriorityQueue[T]) Less

func (q *PriorityQueue[T]) Less(i, j int) bool

func (*PriorityQueue[T]) Peek

func (q *PriorityQueue[T]) Peek() (*T, error)

Retrieve the next item without popping it

func (*PriorityQueue[T]) Pop

func (q *PriorityQueue[T]) Pop() any

func (*PriorityQueue[T]) PopItem

func (pq *PriorityQueue[T]) PopItem() T

Pop the next item

func (*PriorityQueue[T]) Push

func (q *PriorityQueue[T]) Push(x any)

func (*PriorityQueue[T]) PushItem

func (q *PriorityQueue[T]) PushItem(value T, priority int)

PushItem a new item onto the priority queue

func (*PriorityQueue[T]) String

func (q *PriorityQueue[T]) String() string

func (PriorityQueue[T]) Swap

func (q PriorityQueue[T]) Swap(i, j int)

Jump to

Keyboard shortcuts

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