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.
[dependencies]
weighted-gss = "0.2.2"
Python 3.8 or later:
python -m pip install "weighted-gss==0.2.2"
Rust
Stacks are supplied and returned bottom-to-top.
use weighted_gss::{Weight, WeightedGss};
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
struct Possibilities(u32);
impl Weight for Possibilities {
fn join(&self, other: &Self) -> Self {
Self(self.0 | other.0)
}
}
let left = WeightedGss::from_stack([0_u32, 1, 2], Possibilities(0b001));
let right = WeightedGss::from_stack([0_u32, 1, 3], Possibilities(0b100));
let stacks = left.merge(&right);
assert_eq!(stacks.top(), None);
assert_eq!(
stacks.tops().collect::<std::collections::BTreeSet<_>>(),
[2, 3].into(),
);
let reduced = stacks.pop_top(&2).push(9);
assert_eq!(
reduced.to_stacks(8).unwrap(),
vec![(vec![0, 1, 9], Possibilities(0b001))],
);
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.
cargo test --all-targets
cargo bench
cargo +nightly fuzz run operation_sequences
See Correctness validation, Benchmark methodology, and the 2026-07-28 benchmark audit.
Python
from dataclasses import dataclass
from weighted_gss import WeightedGSS
@dataclass(frozen=True)
class Possibilities:
bits: int
def join(self, other: "Possibilities") -> "Possibilities":
return Possibilities(self.bits | other.bits)
stacks = WeightedGSS.from_stacks([
([0, 1, 2], Possibilities(0b001)),
([0, 1, 3], Possibilities(0b100)),
])
assert stacks.tops() == {2, 3}
assert stacks.pop_top(2).to_stacks() == [
([0, 1], Possibilities(0b001)),
]
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.
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file weighted_gss-0.2.2.tar.gz.
File metadata
- Download URL: weighted_gss-0.2.2.tar.gz
- Upload date:
- Size: 52.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via:
twine/6.1.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
eb93eab4b73d7661dd8577bbe61b49af237e1c1a4bd3d8f42765157566ce1fc8
|
|
| MD5 |
3f56b64f7e2e4db52219aed827dfaf97
|
|
| BLAKE2b-256 |
03f4ac325bd57716edfeba36d84d3afa437bd695e78cc2efbf8c1ce8fd3eaf2c
|
File details
Details for the file weighted_gss-0.2.2-cp38-abi3-win_amd64.whl.
File metadata
- Download URL: weighted_gss-0.2.2-cp38-abi3-win_amd64.whl
- Upload date:
- Size: 238.0 kB
- Tags: CPython 3.8+, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via:
twine/6.1.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a682a050539e123bdd453e9433e741e04c53fffbfeeab70ed98de60c25f9b7f6
|
|
| MD5 |
73a8b2445d3b4c4f36fb02df13fe2fe4
|
|
| BLAKE2b-256 |
04e9a0fb840440f925c91c4954d0982620530914aefa8b01aea7c8ee74216c22
|
File details
Details for the file weighted_gss-0.2.2-cp38-abi3-musllinux_1_2_x86_64.whl.
File metadata
- Download URL: weighted_gss-0.2.2-cp38-abi3-musllinux_1_2_x86_64.whl
- Upload date:
- Size: 540.2 kB
- Tags: CPython 3.8+, musllinux: musl 1.2+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via:
twine/6.1.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
16c6953aa0fbe451856a2495da826619c9213c31121b89d908c0e11ff386beee
|
|
| MD5 |
4bb0fbb3ea9265abbf4f7c64fa0e7d9f
|
|
| BLAKE2b-256 |
2d694c101d984e71583cace8785e9a7aa154883ba65644a497ff9c87a63f958b
|
File details
Details for the file weighted_gss-0.2.2-cp38-abi3-musllinux_1_2_aarch64.whl.
File metadata
- Download URL: weighted_gss-0.2.2-cp38-abi3-musllinux_1_2_aarch64.whl
- Upload date:
- Size: 494.0 kB
- Tags: CPython 3.8+, musllinux: musl 1.2+ ARM64
- Uploaded using Trusted Publishing? No
- Uploaded via:
twine/6.1.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a3a9c313f5a7dec190a1eed9d6dadb60ab5d2f65cce0a306f6a0e65786f52e61
|
|
| MD5 |
a0be61162f3aab3a154b562f061d4a7f
|
|
| BLAKE2b-256 |
eed0b7ef3af155043f016f1381679eb477e15522140bab4f22f2ae0736f4830a
|
File details
Details for the file weighted_gss-0.2.2-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.
File metadata
- Download URL: weighted_gss-0.2.2-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
- Upload date:
- Size: 332.4 kB
- Tags: CPython 3.8+, manylinux: glibc 2.17+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via:
twine/6.1.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
767547aaa25a8602a1993bdafebfce454f62cc7d8431d8e3db9e7f76aa1840ab
|
|
| MD5 |
3b5c084fd3847a341060a728d29892a5
|
|
| BLAKE2b-256 |
793b84ea7bbdd5b549f31419dc4daadec3de4dba18788d7d58a0e5812d2ebd7d
|
File details
Details for the file weighted_gss-0.2.2-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.
File metadata
- Download URL: weighted_gss-0.2.2-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
- Upload date:
- Size: 317.1 kB
- Tags: CPython 3.8+, manylinux: glibc 2.17+ ARM64
- Uploaded using Trusted Publishing? No
- Uploaded via:
twine/6.1.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0e003f90477b39f213964b48a3431bda025c34ba9d8e93853e6dd294c0b982e4
|
|
| MD5 |
40e738e11bbf9da8f6a0c861047dcf58
|
|
| BLAKE2b-256 |
a7941cceb465b903b7d46e4613a49e7b5db757e75bb2f4d6b4cbe71d4dc53d32
|
File details
Details for the file weighted_gss-0.2.2-cp38-abi3-macosx_11_0_arm64.whl.
File metadata
- Download URL: weighted_gss-0.2.2-cp38-abi3-macosx_11_0_arm64.whl
- Upload date:
- Size: 298.7 kB
- Tags: CPython 3.8+, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? No
- Uploaded via:
twine/6.1.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9ea486c31ea4dca61ab1855bcf6496c96405515484a284987fbdb82fdceb21e9
|
|
| MD5 |
7bac9a93d3e3e4f241d714533696bb72
|
|
| BLAKE2b-256 |
858c697e5c3106ec20313fb1ec11914c2f71109f890b9c1b0d718722dac0ea65
|
File details
Details for the file weighted_gss-0.2.2-cp38-abi3-macosx_10_12_x86_64.whl.
File metadata
- Download URL: weighted_gss-0.2.2-cp38-abi3-macosx_10_12_x86_64.whl
- Upload date:
- Size: 303.0 kB
- Tags: CPython 3.8+, macOS 10.12+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via:
twine/6.1.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8806e47a75ddb1a014a8fa76d72133d29f6fd84a57209f0ffd55c0bbce8db4e5
|
|
| MD5 |
0132c35f5794bc120268bf984b6e8c03
|
|
| BLAKE2b-256 |
dbc9291ee8f9e0a509fd1b177fc5282c29142f97c9d7390f85b1eebf476425f6
|