std

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Aug 5, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func RegisterStdPreload

func RegisterStdPreload(v *vm.VM)

RegisterStdPreload installs the `std` module under package.preload. `require("std")` returns a table of constructors for the data structures defined in this package — stack, queue, deque, set, linked list, heap, hashmap, trie, btree. Each constructor returns a fresh object whose methods are bound to a private Go instance via colon-call.

Types

type BTree

type BTree[T constraints.Ordered] struct {
	// contains filtered or unexported fields
}

func (*BTree[T]) Delete

func (tree *BTree[T]) Delete(key T)

func (*BTree[T]) Insert

func (tree *BTree[T]) Insert(key T)

func (*BTree[T]) Search

func (tree *BTree[T]) Search(key T) bool

type BTreeNode

type BTreeNode[T constraints.Ordered] struct {
	// contains filtered or unexported fields
}

func NewBTreeNode

func NewBTreeNode[T constraints.Ordered](maxKeys int, isLeaf bool) *BTreeNode[T]

func (*BTreeNode[T]) Append

func (node *BTreeNode[T]) Append(key T, child *BTreeNode[T])

func (*BTreeNode[T]) Concat

func (node *BTreeNode[T]) Concat(other *BTreeNode[T], idx int)

Add all of other's keys starting from idx and children starting from idx + 1

func (*BTreeNode[T]) Delete

func (node *BTreeNode[T]) Delete(tree *BTree[T], key T)

func (*BTreeNode[T]) DeleteIthKey

func (node *BTreeNode[T]) DeleteIthKey(i int)

func (*BTreeNode[T]) InsertKeyChild

func (node *BTreeNode[T]) InsertKeyChild(key T, child *BTreeNode[T])

func (*BTreeNode[T]) InsertNonFull

func (node *BTreeNode[T]) InsertNonFull(tree *BTree[T], key T)

func (*BTreeNode[T]) IsFull

func (node *BTreeNode[T]) IsFull(maxKeys int) bool

func (*BTreeNode[T]) Max

func (node *BTreeNode[T]) Max() T

func (*BTreeNode[T]) Merge

func (node *BTreeNode[T]) Merge(idx int)

Transform:

A B C
 / \
a   b

Into:

A C
 |

a B c

func (*BTreeNode[T]) Min

func (node *BTreeNode[T]) Min() T

func (*BTreeNode[T]) Search

func (node *BTreeNode[T]) Search(key T) bool

func (*BTreeNode[T]) Split

func (parent *BTreeNode[T]) Split(idx int, maxKeys int)

Transform:

A B
 |

a b c d

Into:

A c B
 / \

a b d

func (*BTreeNode[T]) Verify

func (node *BTreeNode[T]) Verify(tree *BTree[T])

type Deque

type Deque struct {
	// contains filtered or unexported fields
}

Deque is a double-ended queue over arbitrary Lua values. Backed by a ring buffer so PushFront/PushBack/PopFront/PopBack are all O(1) amortized. The buffer doubles when full and is never shrunk — real-world deques rarely benefit from shrinkage and the bookkeeping makes the iteration paths uglier.

func NewDeque

func NewDeque() *Deque

NewDeque returns an empty deque.

func (*Deque) Back

func (d *Deque) Back() (any, bool)

Back returns the tail element without removing it.

func (*Deque) Clear

func (d *Deque) Clear()

Clear drops every element. Capacity is preserved.

func (*Deque) Empty

func (d *Deque) Empty() bool

Empty reports whether the deque is empty.

func (*Deque) Front

func (d *Deque) Front() (any, bool)

Front returns the head element without removing it.

func (*Deque) PopBack

func (d *Deque) PopBack() (any, bool)

PopBack removes and returns the tail element.

func (*Deque) PopFront

func (d *Deque) PopFront() (any, bool)

PopFront removes and returns the head element.

func (*Deque) PushBack

func (d *Deque) PushBack(v any)

PushBack appends v to the tail.

func (*Deque) PushFront

func (d *Deque) PushFront(v any)

PushFront prepends v to the head.

func (*Deque) Size

func (d *Deque) Size() int

Size returns the number of elements.

type HashMap

type HashMap struct {
	// contains filtered or unexported fields
}

func DefaultHashMap

func DefaultHashMap() *HashMap

func NewHashMap

func NewHashMap(size, capacity uint64) *HashMap

func (*HashMap) Contains

func (hm *HashMap) Contains(key any) bool

func (*HashMap) Get

func (hm *HashMap) Get(key any) any

func (*HashMap) Put

func (hm *HashMap) Put(key, value any)

type Heap

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

func New

func New[T constraints.Ordered]() *Heap[T]

func NewAny

func NewAny[T any](less func(a, b T) bool) (*Heap[T], error)

NewAny gives a new heap object. element can be anything, but must provide less function.

func (*Heap[T]) Empty

func (h *Heap[T]) Empty() bool

Empty returns the heap is empty or not.

func (*Heap[T]) Pop

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

Pop removes the minimum element (according to Less) from the heap. The complexity is O(log n) where n = h.Len().

func (*Heap[T]) Push

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

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

func (*Heap[T]) Size

func (h *Heap[T]) Size() int

Size returns the size of the heap

func (*Heap[T]) Top

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

Top returns the minimum element (according to Less) from the heap. Top panics if the heap is empty.

type LinkedList

type LinkedList struct {
	// contains filtered or unexported fields
}

LinkedList is a doubly-linked list of Lua values. All end-operations are O(1); ToArray is O(n). For random-access use a Deque or a plain Lua table — this type exists for cases where you actually want stable interior references / iteration.

func NewLinkedList

func NewLinkedList() *LinkedList

NewLinkedList returns an empty list.

func (*LinkedList) Back

func (l *LinkedList) Back() (any, bool)

Back returns the tail element without removing it.

func (*LinkedList) Clear

func (l *LinkedList) Clear()

Clear drops every element.

func (*LinkedList) Empty

func (l *LinkedList) Empty() bool

Empty reports whether the list has no elements.

func (*LinkedList) Front

func (l *LinkedList) Front() (any, bool)

Front returns the head element without removing it.

func (*LinkedList) PopBack

func (l *LinkedList) PopBack() (any, bool)

PopBack removes and returns the tail element.

func (*LinkedList) PopFront

func (l *LinkedList) PopFront() (any, bool)

PopFront removes and returns the head element.

func (*LinkedList) PushBack

func (l *LinkedList) PushBack(v any)

PushBack appends v to the tail.

func (*LinkedList) PushFront

func (l *LinkedList) PushFront(v any)

PushFront prepends v to the head.

func (*LinkedList) Size

func (l *LinkedList) Size() int

Size returns the number of elements.

func (*LinkedList) ToArray

func (l *LinkedList) ToArray() []any

ToArray walks the list head→tail and returns a snapshot slice.

type Node

type Node struct {
	// contains filtered or unexported fields
}

Node represents each node in Trie.

func NewNode

func NewNode() *Node

NewNode creates a new Trie node with initialized children map.

func (*Node) Capacity

func (n *Node) Capacity() int

Capacity returns the number of nodes in the Trie

func (*Node) Compact

func (n *Node) Compact() (remove bool)

Compact will remove unecessay nodes, reducing the capacity, returning true if node n itself should be removed.

func (*Node) Find

func (n *Node) Find(s string) bool

Find words at a Trie node.

func (*Node) Insert

func (n *Node) Insert(s ...string)

Insert zero, one or more words at a Trie node.

func (*Node) Remove

func (n *Node) Remove(s ...string)

Remove zero, one or more words lazily from the Trie, no node is actually removed.

func (*Node) Size

func (n *Node) Size() int

Size returns the number of words in the Trie

type Queue

type Queue struct {
	// contains filtered or unexported fields
}

Queue is a FIFO collection over arbitrary Lua values. Backed by a slice with a head index — Push is amortized O(1) and Pop is O(1). When the head wanders past half the underlying buffer the tail half is shifted down so we don't leak unbounded capacity.

func NewQueue

func NewQueue() *Queue

NewQueue returns an empty queue.

func (*Queue) Clear

func (q *Queue) Clear()

Clear drops every element. Capacity is preserved.

func (*Queue) Empty

func (q *Queue) Empty() bool

Empty reports whether the queue has no elements.

func (*Queue) Peek

func (q *Queue) Peek() (any, bool)

Peek returns the front element without removing it.

func (*Queue) Pop

func (q *Queue) Pop() (any, bool)

Pop removes and returns the front element. The second return is false if the queue was empty.

func (*Queue) Push

func (q *Queue) Push(v any)

Push appends v to the back of the queue.

func (*Queue) Size

func (q *Queue) Size() int

Size returns the number of live elements.

type Set

type Set struct {
	// contains filtered or unexported fields
}

Set is an unordered collection of unique Lua values. Membership uses Go's native map, which means key equality follows Go's rules: strings, ints, floats, bools and nil all interoperate naturally, and reference values (tables, closures) compare by identity. Sets do NOT honour Lua's float-vs-int equality (1 == 1.0 in Lua but distinct in a Go map) — callers should pre-normalise if they care.

func NewSet

func NewSet() *Set

NewSet returns an empty set.

func (*Set) Add

func (s *Set) Add(v any) bool

Add inserts v. Returns true if v was newly added, false if already present — handy for "if added then ..." flows.

func (*Set) Clear

func (s *Set) Clear()

Clear drops every element.

func (*Set) Contains

func (s *Set) Contains(v any) bool

Contains reports whether v is in the set.

func (*Set) Empty

func (s *Set) Empty() bool

Empty reports whether the set is empty.

func (*Set) Remove

func (s *Set) Remove(v any) bool

Remove deletes v. Returns true if v was present.

func (*Set) Size

func (s *Set) Size() int

Size returns the number of elements.

func (*Set) Values

func (s *Set) Values() []any

Values returns a snapshot slice of every element. Order is the underlying map iteration order — i.e. random per call.

type Stack

type Stack struct {
	// contains filtered or unexported fields
}

Stack is a LIFO collection over arbitrary Lua values. Backed by a plain slice — Push/Pop/Peek are amortized O(1).

func NewStack

func NewStack() *Stack

NewStack returns an empty stack.

func (*Stack) Clear

func (s *Stack) Clear()

Clear drops every element. Capacity is preserved so subsequent pushes don't immediately re-allocate.

func (*Stack) Empty

func (s *Stack) Empty() bool

Empty reports whether the stack has no elements.

func (*Stack) Peek

func (s *Stack) Peek() (any, bool)

Peek returns the top element without removing it.

func (*Stack) Pop

func (s *Stack) Pop() (any, bool)

Pop removes and returns the top element. The second return is false if the stack was empty.

func (*Stack) Push

func (s *Stack) Push(v any)

Push appends v to the top of the stack.

func (*Stack) Size

func (s *Stack) Size() int

Size returns the number of elements.

Jump to

Keyboard shortcuts

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