https://github.com/0x484558/fairqueue
Spatially distancing fair queue
https://github.com/0x484558/fairqueue
data-structures fairness no-std queue round-robin stack
Last synced: about 1 month ago
JSON representation
Spatially distancing fair queue
- Host: GitHub
- URL: https://github.com/0x484558/fairqueue
- Owner: 0x484558
- License: 0bsd
- Created: 2024-12-20T05:05:34.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2026-01-10T12:00:04.000Z (3 months ago)
- Last Synced: 2026-01-14T01:09:03.075Z (2 months ago)
- Topics: data-structures, fairness, no-std, queue, round-robin, stack
- Language: Rust
- Homepage: https://crates.io/crates/fairqueue
- Size: 44.9 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# FairQueue
FairQueue is a Rust `no_std` (`alloc`) library that implements a fair queue through spatial distancing of similar values. The data structure presented in `fairqueue` crate ensures that groups of items are equitably interleaved with items from other groups and can be scheduled in a round-robin manner.
## Usage
```Rust
use fairqueue::{FairQueue, FairGroup};
#[derive(Debug, PartialEq)]
struct Event {
timestamp: u32,
user_id: &'static str,
}
impl FairGroup for Event {
fn is_same_group(&self, other: &Self) -> bool {
self.user_id == other.user_id
}
}
fn main() {
let mut queue = FairQueue::new();
queue.insert(&Event { timestamp: 1, user_id: "user1" });
queue.insert(&Event { timestamp: 2, user_id: "user2" });
queue.insert(&Event { timestamp: 3, user_id: "user1" });
assert_eq!(queue.pop(), Some(&Event { timestamp: 1, user_id: "user1" }));
assert_eq!(queue.pop(), Some(&Event { timestamp: 2, user_id: "user2" }));
assert_eq!(queue.pop(), Some(&Event { timestamp: 3, user_id: "user1" }));
assert_eq!(queue.pop(), None);
}
```
## API Overview
### `FairQueue`
- `new() -> FairQueue` - Constructs a new, empty instance of the queue.
- `insert(&mut self, value: &V: FairGroup)` - Adds a value to the queue, distancing it from values within the same group.
- `pop(&mut self) -> Option<&V>` - Removes and returns the next item in the queue, adhering to round-robin scheduling.
- `peek(&self) -> Option<&&V>` - Returns a reference to the next item in the queue without removing it.
## License
FairQueue is distributed under the [BSD Zero Clause License](LICENSE).