콘텐츠로 이동

zwire-collections

이 콘텐츠는 아직 번역되지 않았습니다.

Zero-dependency, #![no_std] (alloc) exact incremental filtered + sorted Top-K collection backed by a 2-level Chunked Sorted Block Index (BLOCK_TARGET = 128, split > 256, merge < 32) with deterministic key tie-breaking and O(1) Top-K boundary pruning.

Part of the zwire Rust workspace (native companion to @zwire/core).

In Rust, maintaining an exact filtered + sorted Top-K view over live data streams (orderbooks, Ratatui dashboards, leaderboards, log/metric monitors, game entity views) usually forces a choice between:

  • Full filter + sort on every update (O(N log N)),
  • Heavyweight streaming dataflow engines (dbsp / differential-dataflow), or
  • Approximate streaming sketches (topk Space-Saving) that cannot produce exact live rankings.

zwire-collections fills the gap with:

  • Zero runtime dependencies, #![forbid(unsafe_code)], and #![no_std] + alloc.
  • Single-copy slot storage: T is stored once in an internal slot arena and sorted blocks hold compact u32 slot indices — zero T: Clone required for inserts, updates, deletes, or sorting.
  • Deterministic total order: user comparator ties break deterministically by key (K: Ord).
  • O(1) Top-K boundary pruning: mutations strictly outside the current K-th boundary skip view materialization completely.
  • Exact revision-tracked TopKViewIds: detects both rank/membership changes and in-place payload updates inside the Top-K window without requiring T: PartialEq.
use zwire_collections::Collection;
#[derive(Debug, Clone, PartialEq, Eq)]
struct Order {
id: u64,
price: i64,
active: bool,
}
let mut book = Collection::with_filter(
|o: &Order| o.id,
|a, b| b.price.cmp(&a.price), // highest price first
|o| o.active,
);
let top5 = book.watch_top_k(5);
book.upsert_many([
Order { id: 1, price: 100, active: true },
Order { id: 2, price: 120, active: true },
Order { id: 3, price: 110, active: false },
]);
let changed = book.sync_view(top5);
assert!(changed);
let leaders: Vec<u64> = book.view_items(top5).into_iter().map(|o| o.id).collect();
assert_eq!(leaders, vec![2, 1]);
[dependencies]
zwire-collections = { version = "0.1", default-features = false }