weighted-gss
A persistent weighted graph-structured stack.
WeightedGss<S, W> represents a finite collection of stack alternatives. Each stack carries a weight. When stack operations make alternatives denote the same concrete stack, their weights are joined.
The graph representation is private. The Rust API contains semantic stack operations, bounded concrete-stack inspection, and a linear-prefix fast path without exposing graph nodes or canonical representation machinery.
Installation
The latest release is version 0.2.2.
[]
= "0.2.2"
Python 3.8 or later:
Rust
Stacks are supplied and returned bottom-to-top.
use ;
;
let left = from_stack;
let right = from_stack;
let stacks = left.merge;
assert_eq!;
assert_eq!;
let reduced = stacks.pop_top.push;
assert_eq!;
A weight must implement ordinary equality. join must be associative, commutative, and idempotent.
The exported Rust names are:
Weight
WeightedGss
Gss
LinearPrefix
StackLimitExceeded
linear_prefix
for_each_stack_top_first
The core methods are:
- construction:
new,from_stack,from_stacks,from_stacks_with_weight; - alternatives:
merge; - stack operations:
push,pop,popn; - top selection:
top,tops,has_empty_stack,retain_top,retain_empty,pop_top; - weights:
weights,map_weights,filter_map_weights,joined_weight; - observations:
is_empty,max_depth,to_stacks.
to_stacks(max_stacks) returns canonical (stack, weight) pairs and fails with the opaque StackLimitExceeded error rather than returning more than the requested number of distinct stacks.
weights() iterates stored factored weight regions, not concrete stacks. One weight may cover many stacks, equal weights may appear more than once, and count/order are unspecified. map_weights and filter_map_weights transform those regions without materialising stacks; see Semantics and invariants for the representation-independence condition.
Bounded inspection and linear prefixes
for_each_stack_top_first(&gss, max_stacks, visit) visits canonical distinct stacks as borrowed top-first slices. It completes only when the complete result fits within max_stacks.
linear_prefix(&gss) returns a LinearPrefix when the current value has one homogeneous weight and a directly accessible linear top prefix. The hidden floor may still branch. The view supports indexed reads from the top, pushes, bounded pops, and conversion back into a WeightedGss while retaining the unchanged floor.
Neither operation exposes graph nodes, structural paths, or canonical stack-language IDs.
Validation and performance characteristics
Correctness is checked against an explicit stack-to-weight map through deterministic tests, shrinkable property-based operation sequences, and an oracle-backed cargo-fuzz target. Benchmark baselines implement the same extensional semantics with explicit maps or sets, and the structural benchmark builders are themselves checked for identical outputs.
Representative Criterion point estimates from one clean Apple M1 Pro run are shown below. Input preparation and output destruction are excluded from construction timings. These are measurements of particular public operation traces, not machine-independent guarantees.
| Workload | WeightedGss |
Explicit map | What it shows |
|---|---|---|---|
| Grow a 65,536-stack binary language through 16 rounds of push and merge, preserving two weight classes | 12.5 µs | 26.6 ms | Structural evolution remains compact while the explicit population doubles each round |
| Pop a structurally built 4,096-stack binary language | 104 ns | 235 µs | A shared graph operation need not enumerate the represented stacks |
| Fork a persistent 512-stack value twice | 163 ns | 101 µs | Immutable structure is reused rather than copied |
| Import an already enumerated list of 1,024 weighted stacks | 3.57 ms | 15.1 µs | Conversion from the explicit representation naturally favours the explicit map |
| Materialise 1,024 concrete stacks as owned output | 86.9 µs | 36.5 µs | Complete output remains proportional to the concrete language |
The distinction between structural evolution and flat import or enumeration is fundamental. WeightedGss is intended to preserve and update shared stack structure; it is not a faster hash map for inputs that have already been fully expanded.
See Correctness validation, Benchmark methodology, and the 2026-07-28 benchmark audit.
Python
:
return
=
assert ==
assert ==
Python weights need not be hashable. Exceptions raised by join() are propagated normally. See the Python API.
Semantics
Weights and stack operations have an extensional meaning independent of the private graph representation. Rust 1.85 is the declared minimum version. See Semantics and invariants.
Licensed under either Apache-2.0 or MIT, at your option.