resq-dsa
Version:v0.1.3· License:Apache-2.0· Crate: crates.io · API docs: docs.rs
no_std environments and embedded systems while remaining ergonomic in standard Rust applications. The crate provides space-efficient probabilistic data structures (Bloom filter, Count-Min sketch), graph algorithms (BFS, Dijkstra, A*), a bounded heap for K-nearest-neighbor tracking, a trie for prefix-based search, and Rabin-Karp rolling-hash string matching.
Module Structure
Feature Flags
| Feature | Default | Description |
|---|---|---|
std | Yes | Enables standard library support. Disable for no_std environments (the crate uses alloc internally). |
Installation
With std (default)
no_std environments
std is disabled the crate compiles with #![no_std] and relies only on alloc. You must provide a global allocator in your binary.
Data Structures
Bloom Filter
A space-efficient probabilistic set-membership data structure. It can tell you if an element is possibly in the set or definitely not in the set. False positives are possible; false negatives are not. The filter usesk independent FNV-1a hash functions (seeded variants) to set bits in an m-bit array. Optimal values for k and m are computed automatically from the desired capacity and false-positive rate.
Complexity
| Operation | Time | Space |
|---|---|---|
new | O(m) | O(m) |
add | O(k) | — |
has | O(k) | — |
len | O(1) | — |
is_empty | O(1) | — |
clear | O(m) | — |
capacity and error_rate).
API Reference
| Method | Signature | Description |
|---|---|---|
new | fn new(capacity: usize, error_rate: f64) -> Self | Creates a new Bloom filter sized for capacity elements at the given false-positive error_rate (must be in (0, 1)). Panics if error_rate is out of range or capacity is zero. |
add | fn add(&mut self, item: impl AsRef<[u8]>) | Adds an element to the filter. Accepts &str, String, &[u8], Vec<u8>, etc. |
has | fn has(&self, item: impl AsRef<[u8]>) -> bool | Returns true if the element is possibly in the set, false if it is definitely absent. |
len | const fn len(&self) -> usize | Returns the number of elements that have been added. |
is_empty | const fn is_empty(&self) -> bool | Returns true if no elements have been added. |
clear | fn clear(&mut self) | Resets the filter, removing all elements. |
Example
Count-Min Sketch
A space-efficient probabilistic data structure for frequency estimation. It may overcount but never undercounts. Estimates are withinepsilon * N of the true count with probability 1 - delta, where N is the total count of all increments.
Uses depth independent FNV-1a hash functions mapping elements to width columns. The estimated count for a key is the minimum across all rows.
Complexity
| Operation | Time | Space |
|---|---|---|
new | O(w * d) | O(w * d) |
increment | O(d) | — |
estimate | O(d) | — |
ceil(e / epsilon) (width) and d = ceil(ln(1 / delta)) (depth).
API Reference
| Method | Signature | Description |
|---|---|---|
new | fn new(epsilon: f64, delta: f64) -> Self | Creates a new sketch. epsilon controls error magnitude, delta controls failure probability. Both must be in (0, 1). |
increment | fn increment(&mut self, key: impl AsRef<[u8]>, count: u64) | Increments the count for key by count. Counts are stored as u64 and saturate on overflow. |
estimate | fn estimate(&self, key: impl AsRef<[u8]>) -> u64 | Returns the estimated count for key. Guaranteed to be >= the true count. |
Example
Graph (Weighted Directed)
A weighted directed graph with three pathfinding algorithms: breadth-first search (BFS), Dijkstra’s shortest path, and A* with a user-provided heuristic. Node identifiers can be any type that implementsEq + Hash + Clone (and additionally Ord for Dijkstra and A*). Edge weights are u64.
Complexity
| Operation | Time | Space |
|---|---|---|
new | O(1) | O(1) |
add_edge | O(1) amortized | O(1) |
bfs | O(V + E) | O(V) |
dijkstra | O((V + E) log V) | O(V) |
astar | O((V + E) log V) * | O(V) |
API Reference
| Method | Signature | Description |
|---|---|---|
new | fn new() -> Self | Creates a new empty directed graph. Also implements Default. |
add_edge | fn add_edge(&mut self, from: Id, to: Id, weight: u64) | Adds a directed edge. For undirected graphs, call twice with reversed nodes. |
bfs | fn bfs(&self, start: &Id) -> Vec<Id> | Returns all reachable nodes from start in breadth-first order. Requires Id: Eq + Hash + Clone. |
dijkstra | fn dijkstra(&self, start: &Id, end: &Id) -> Option<(Vec<Id>, u64)> | Finds the shortest path and its cost. Returns None if end is unreachable. Requires Id: Eq + Hash + Clone + Ord. |
astar | fn astar<H: Fn(&Id, &Id) -> u64>(&self, start: &Id, end: &Id, h: H) -> Option<(Vec<Id>, u64)> | A* shortest path with heuristic h(node, goal). The heuristic must be admissible and consistent for correct results. Requires Id: Eq + Hash + Clone + Ord. |
Examples
Bounded Heap
A bounded max-heap that retains only the K entries with the smallest distance values. Useful for K-nearest-neighbor search, top-K tracking, and streaming scenarios where you want to keep only the closest results. The root always holds the entry with the largest distance among the retained items. When the heap is full and a new entry has a smaller distance than the root, the root is evicted.Complexity
| Operation | Time | Space |
|---|---|---|
new | O(1) | O(k) |
insert | O(log k) | — |
peek | O(1) | — |
to_sorted | O(k log k) | O(k) |
len | O(1) | — |
is_empty | O(1) | — |
API Reference
| Method | Signature | Description |
|---|---|---|
new | fn new(limit: usize, dist: D) -> Self | Creates a heap that keeps at most limit items, ordered by the distance function dist: Fn(&T) -> f64. |
insert | fn insert(&mut self, entry: T) | Inserts an entry. If full and the new entry’s distance is less than the current max, the max is evicted. Otherwise the entry is rejected. |
peek | fn peek(&self) -> Option<&T> | Returns a reference to the entry with the maximum distance (the root), or None if empty. |
to_sorted | fn to_sorted(&self) -> Vec<&T> | Returns all entries sorted by distance ascending (nearest first). Allocates a new Vec on each call. |
len | const fn len(&self) -> usize | Returns the current number of entries. |
is_empty | const fn is_empty(&self) -> bool | Returns true if the heap is empty. |
Example
Trie (Prefix Tree)
A prefix tree for efficient string storage, exact lookup, and prefix-based autocomplete. All operations run in O(m) time where m is the length of the input string. Internally, each node stores aHashMap<char, TrieNode>, making the trie Unicode-aware.
Complexity
| Operation | Time | Space |
|---|---|---|
new | O(1) | O(1) |
insert | O(m) | O(m) worst case |
search | O(m) | — |
starts_with | O(m + r) | O(r) |
API Reference
| Method | Signature | Description |
|---|---|---|
new | fn new() -> Self | Creates a new empty trie. Also implements Default. |
insert | fn insert(&mut self, word: &str) | Inserts a word into the trie. |
search | fn search(&self, word: &str) -> bool | Returns true if the exact word exists in the trie. |
starts_with | fn starts_with(&self, prefix: &str) -> Vec<String> | Returns all words that start with the given prefix. Uses DFS with a shared buffer to minimize allocations. |
Example
Rabin-Karp String Search
A rolling-hash string matching algorithm that finds all occurrences of a pattern in a text. Uses a polynomial rolling hash with modular arithmetic (base 31, mod 10^9 + 7). The algorithm is Unicode-aware — it operates onchar boundaries, so multi-byte characters are handled correctly. Indices in the result are character positions, not byte offsets.
Complexity
| Case | Time | Space |
|---|---|---|
| Average | O(n + m) | O(m) |
| Worst | O(n * m) | O(m) |
API Reference
| Function | Signature | Description |
|---|---|---|
rabin_karp | fn rabin_karp(text: &str, pattern: &str) -> Vec<usize> | Returns a vector of starting character indices where pattern occurs in text. Returns an empty vector if there are no matches or the pattern is empty. |
Example
Quick Reference
| Module | Primary Type | Key Methods |
|---|---|---|
bloom | BloomFilter | new, add, has, len, is_empty, clear |
count_min | CountMinSketch | new, increment, estimate |
graph | Graph<Id> | new, add_edge, bfs, dijkstra, astar |
heap | BoundedHeap<T, D> | new, insert, peek, to_sorted, len, is_empty |
trie | Trie | new, insert, search, starts_with |
trie | (free fn) rabin_karp | rabin_karp(text, pattern) |
Contributing
- Fork the repository and create a feature branch.
- Run
cargo testto ensure all tests pass. - All new source files must include the Apache-2.0 license header.
- Keep binary names consistent with the
resq-<name>convention. - Open a pull request against
master.