coll
lib/coll.vklib/coll.vk
Pure-Veko collections: hashed Set, bounded Ring buffer, binary min-Heap, LRU cache, and Memo table.
Built on Veko 1.0's hybrid hashed map and O(1) in-place put. Zero capabilities required.
Set = struct { data map = ... }
Unordered set of scalar keys (str, i64, or .enum), backed by hybrid hashed map.
set-new = fn [] Set
Creates a new empty Set.
use [coll]
mut s = coll/set-new
coll/set-add mut s 'alpha'
coll/set-add mut s 'beta'
if (coll/set-len s) != 2 or (not (coll/set-has? s 'alpha')) [ err 'set' ] else [ .ok ]
Ring = struct { buf block, cap i64, head i64 = ..., count i64 = ... }
Fixed-capacity O(1) FIFO ring buffer.
ring-new = fn [cap i64] Ring
Creates a new bounded Ring with capacity cap > 0.
use [coll]
mut r = coll/ring-new 3
_ = coll/ring-push mut r 10
_ = coll/ring-push mut r 20
v = coll/ring-pop mut r
if v != 10 or (coll/ring-len r) != 1 [ err 'ring' ] else [ .ok ]
ring-push = fn [mut r Ring, val any] i64
Appends val to the tail of r; returns err if full.
ring-pop = fn [mut r Ring] any
Removes and returns the oldest item from r; returns err if empty.
ring-peek = fn [r Ring] any
Returns the oldest item in r without removing it; returns err if empty.
Heap = struct { items block = ... }
Binary min-heap priority queue storing { prio = i64, val = any } entries.
heap-new = fn [] Heap
Creates a new empty min-Heap.
use [coll]
mut h = coll/heap-new
_ = coll/heap-push mut h 30 'c'
_ = coll/heap-push mut h 10 'a'
_ = coll/heap-push mut h 20 'b'
first_v = coll/heap-pop mut h
if first_v != 'a' [ err 'heap' ] else [ .ok ]
heap-push = fn [mut h Heap, prio i64, val any] i64
Pushes val with integer priority prio (smaller prio pops first).
heap-peek = fn [h Heap] any
Returns the lowest-priority value without popping; returns err if empty.
heap-pop = fn [mut h Heap] any
Pops and returns the value with the smallest priority; returns err if empty.
Lru = struct { cap i64, data map = ..., order block[str] = ... }
Bounded Least-Recently-Used cache keyed by str.
lru-new = fn [cap i64] Lru
Creates a new LRU cache holding at most cap > 0 entries.
use [coll]
mut c = coll/lru-new 2
coll/lru-put mut c 'a' 1
coll/lru-put mut c 'b' 2
_ = coll/lru-get mut c 'a'
coll/lru-put mut c 'c' 3
if (coll/lru-has? c 'b') or (not (coll/lru-has? c 'a')) [ err 'lru' ] else [ .ok ]
lru-get = fn [mut c Lru, k str] any
Looks up k in c, marking it most-recently-used; returns err if absent.
lru-put = fn [mut c Lru, k str, val any] i64
Inserts or updates k -> val in c, evicting the least-recently-used key when full.
Memo = struct { cache map = ..., hits i64 = ... }
Simple key-value memoization cache for pure functions.
memo-call = fn [mut m Memo, key str, compute fn[->any]] any
Returns cached value for key, or invokes (compute) once, caches the result, and returns it.