dstruct

package
v0.0.0-...-4c3e4d9 Latest Latest
Warning

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

Go to latest
Published: Jan 26, 2023 License: MIT Imports: 3 Imported by: 7

README

Palgo

This module contains data structures

Documentation

Overview

Package dstruct contains data stuctures.

Package dstruct implements various Data STRUCTures.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Heap

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

Heap implemements a heap data structure by wrapping around the standard library's heap.

func HeapFromSlice

func HeapFromSlice[T any](src []T, best utils.Comparator[T]) Heap[T]

HeapFromSlice creates and initializes a heap from a given slice.

func NewHeap

func NewHeap[T any](best utils.Comparator[T]) Heap[T]

NewHeap creates and initializes a heap.

func (Heap[T]) Data

func (h Heap[T]) Data() *[]T

Data returns a pointer to the internal data Run Heap.Fix if you modify it in any way.

func (Heap[T]) Fix

func (h Heap[T]) Fix(i int)

Fix re-establishes the heap ordering after the element at index i has changed its value. Changing the value of the element at index i and then calling Fix is equivalent to, but less expensive than, calling Remove(h, i) followed by a Push of the new value. The complexity is O(log n) where n = h.Len().

func (Heap[T]) Len

func (h Heap[T]) Len() int

Len returns the size of the heap.

func (*Heap[T]) Pop

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

Pop removes and returns the minimum element (according to Less) from the heap. The complexity is O(log n) where n = h.Len(). Pop is equivalent to Remove(h, 0).

func (*Heap[T]) Push

func (h *Heap[T]) Push(t T)

Push pushes the element x onto the heap. The complexity is O(log n) where n = h.Len().

func (Heap[T]) Remove

func (h Heap[T]) Remove(i int) T

Remove removes and returns the element at index i from the heap. The complexity is O(log n) where n = h.Len().

func (Heap[T]) Repair

func (h Heap[T]) Repair()

Repair establishes the heap invariants required by the other routines in this package. Repair is idempotent with respect to the heap invariants and may be called whenever the heap invariants may have been invalidated. The complexity is O(n) where n = h.Len().

type LruCache

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

LruCache is a implements a Least Recently Used cache. It acts as a dictionary of keys and values, with a maximum number of entries. When this maximum is surpassed, the least recently used item is dropped.

func NewLRU

func NewLRU[K comparable, V any](cap int) *LruCache[K, V]

NewLRU creates new lru cache with the specified capacity, measured in number of key-value paires stored.

func (*LruCache[K, V]) Get

func (lru *LruCache[K, V]) Get(key K) (v V, ok bool)

Get looks into the cache to see if the given key is is registered. If so, the value is returned and its epoch updated.

func (LruCache[K, V]) Len

func (lru LruCache[K, V]) Len() int

Len is the number of stored items.

func (LruCache[K, V]) Set

func (lru LruCache[K, V]) Set(k K, v V)

Set checks if an entry was in the cache. If it was, it updates its epoch. Otherwise, it adds it anew.

type Stack

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

Stack data structure. Implements a LIFO queue.

func NewStack

func NewStack[T any](args ...int) Stack[T]

NewStack creates a new stack, with optional arguments for initial size (with default initialization) and initial capacity.

func (*Stack[T]) Data

func (s *Stack[T]) Data() []T

Data reveals the internal storage. The storage is arranged from bottom to top of the stack.

func (*Stack[T]) Invert

func (s *Stack[T]) Invert()

Invert reverses the order of the items within the stack.

func (Stack[T]) IsEmpty

func (s Stack[T]) IsEmpty() bool

IsEmpty indicates when the count of elements in the stack is zero.

func (Stack[T]) Peek

func (s Stack[T]) Peek() T

Peek reveals a copy of the item at the top of the stack.

func (*Stack[T]) Pop

func (s *Stack[T]) Pop()

Pop removes an item to the top of the stack.

func (*Stack[T]) Push

func (s *Stack[T]) Push(t T)

Push inserts an item to the top of the stack.

func (Stack[T]) Size

func (s Stack[T]) Size() int

Size is the count of elements in the stack.

Jump to

Keyboard shortcuts

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