ds: lua data structures and algorithms

ds is a small-ish lua library which fills many of the data structure and method gaps (needed "batteries") in Lua's standard library. It's only dependency is lib/metaty which it uses for defining it's records and lib/fmt which it uses to define the logging interface.

Mod ds

ds: data structures and algorithms.

Types: PlainStyler TypoSafe Slc Imm Set bt BiMap Deq Error

Functions

Record PlainStyler

Record TypoSafe

A typo-safe table, typically used in libraries for storing constants.

Adding keys is always allowed but getting non-existant keys is an error.

Record Slc

A slice of anything with start and end indexes.
Note: This object does not hold a reference to the object being sliced.

Fields:

Methods

Record Imm

Immutable table

Record Set

Methods

Mod bt

indexed table as Binary Tree.

These functions treat an indexed table as a binary tree

where root is at index=1

Functions

Record BiMap

Bidirectional Map. Maps both key -> value and value -> key. Must use :remove (instead of bm[k] = nil to handle deletions.

Note that pairs() will return BOTH directions (in an unspecified order)

Methods

Record Deq

Deq() -> Deq, a deque datastructure. Use as a first in/out (fifo) with deq:push(v) and deq() (pop).

Main methods:

  pushLeft()                  -- push on left side
  pushRight() push()          -- push on right side
  popRight()                  -- pop on right side
  popLeft()   pop()  __call() -- pop on left side

Fields:

Methods

Record Error

Error message, traceback and cause NOTE: you should only use this for printing/logging/etc.

Fields:

Methods

Mod ds.time

Working with and representing time.

Types: DateTime Duration Epoch Tz

Functions

Record DateTime

Represents a date time. The core fields are documented below, with methods to convert to more "typical" reprsentations.

Fields:

Methods

Record Duration

Represents a Duration of time.

Fields:

Methods

Record Epoch

Represents an Epoch: seconds and nano-seconds since 1970-01-01 at 12:00 pm.

Fields:

Record Tz

Usage: Tz:of(-6) for a -6 hour offset.

Fields:

Methods

Record Iter

Methods

Record LL

Fields: Methods

Mod ds.path

working with paths

Call directly to convert a list|str to a list of path components.

Functions

Mod ds.utf8

utf8 stream decoding.

Get the length by decodelen(firstbyte), then decode the whole character

with decode(dat)

Functions

Mod ds.heap

Binary Heap implementation

Types: Heap

Record Heap

Heap(t, cmp) binary heap using a table.
A binary heap is a binary tree where the value of the parent always satisfies cmp(parent, child) == true add and push take O(log n), making it very useful for priority queues and similar problems.

Fields:

Methods

Mod ds.log

Simple logging library, set i.e. LOGLEVEL=INFO to enable logging.

This module has the functions trace info warn err crit with the signature:

function(fmt, ... [, data])

To enable logging the user should set a global (or env var) LOGLEVEL

to oneof: C/CRIT/1 E/ERROR/2 W/WARN/3 I/INFO/4 T/TRACE/5

This module also sets (if not already set) the global LOGFN to ds.logFn

which logs to stderr. This fn is called with signature

function(level, srcloc, fmt, ...)

Types: LogTable

Functions

Record LogTable

used in tests

Fields:

Record ds.Grid

Fields: Methods

Mod ds.load

load lua modules with custom or default environment in a sandboxed

environment. This is extremely useful for configurations written in lua or

writing your own config-like language.

The default environment (ds.load.ENV) has safe default functions which

cannot access state and missing unsafe functions like getmetatable or the

debug module.

loading

To perform the load, call this module with:


  (path, env={}, envMeta=ds.load.ENV) -> ok, result

inputs:

outputs:

Throws an error if the path is not valid lua code.

Functions

Record ds.IFile

Fields: Methods