https://github.com/freyskeyd/stacktools
Offer structure representation of data structures
https://github.com/freyskeyd/stacktools
priority-queue queue rust stack
Last synced: 2 months ago
JSON representation
Offer structure representation of data structures
- Host: GitHub
- URL: https://github.com/freyskeyd/stacktools
- Owner: Freyskeyd
- Created: 2017-11-02T20:11:34.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2017-11-06T11:59:08.000Z (about 8 years ago)
- Last Synced: 2025-04-06T07:24:45.019Z (9 months ago)
- Topics: priority-queue, queue, rust, stack
- Language: Rust
- Homepage:
- Size: 12.7 KB
- Stars: 3
- Watchers: 0
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Stacktools
> **Offer structure representation of data structures** - This crate helps you to deal with data structures such as
> Queue (FiFo), Stack (LiFo) and Priority queue.
[](https://travis-ci.org/Freyskeyd/stacktools) [][Documentation]
## Install
Just add it to your `Cargo.toml`:
```toml
[dependencies]
stacktools = "0.1"
```
## Examples
```rust
extern crate stacktools;
use stacktools::prelude::*;
use stacktools::Queue;
fn main() {
let mut queue: Queue = Queue::new();
assert!(queue.len() == 0);
queue.push(1);
queue.push(2);
queue.push(3);
assert!(queue.len() == 3);
assert_eq!(Some(1), queue.next());
assert!(queue.len() == 2);
assert_eq!(Some(2), queue.next());
assert!(queue.len() == 1);
assert_eq!(Some(3), queue.next());
assert!(queue.len() == 0);
assert_eq!(None, queue.next());
}
```
With PriorityQueue:
```rust
extern crate stacktools;
use stacktools::prelude::*;
use stacktools::{PriorityQueue, Priority};
use std::cmp::Ordering;
#[derive(Eq, PartialEq, Debug)]
struct PriorityMessage {
priority: Priority,
value: i32,
}
impl Ord for PriorityMessage {
fn cmp(&self, other: &PriorityMessage) -> Ordering {
self.priority.cmp(&other.priority)
}
}
impl PartialOrd for PriorityMessage {
fn partial_cmp(&self, other: &PriorityMessage) -> Option {
Some(self.cmp(other))
}
}
fn main() {
let mut queue: PriorityQueue = PriorityQueue::new();
assert!(queue.len() == 0);
queue.push(PriorityMessage {
priority: Priority::Trivial,
value: 1,
});
queue.push(PriorityMessage {
priority: Priority::Normal,
value: 2,
});
queue.push(PriorityMessage {
priority: Priority::Critical,
value: 3,
});
queue.push(PriorityMessage {
priority: Priority::Critical,
value: 4,
});
assert_eq!(
Some(PriorityMessage {
priority: Priority::Critical,
value: 3,
}),
queue.next()
);
assert!(queue.len() == 3);
assert_eq!(
Some(PriorityMessage {
priority: Priority::Critical,
value: 4,
}),
queue.next()
);
assert!(queue.len() == 2);
assert_eq!(
Some(PriorityMessage {
priority: Priority::Normal,
value: 2,
}),
queue.next()
);
assert!(queue.len() == 1);
assert_eq!(
Some(PriorityMessage {
priority: Priority::Trivial,
value: 1,
}),
queue.next()
);
assert!(queue.len() == 0);
assert_eq!(None, queue.next());
}
```
## License
Licensed under either of
* Apache License, Version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0)
* MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT)
at your option.
### Contribution
Unless you explicitly state otherwise, any contribution intentionally
submitted for inclusion in the work by you, as defined in the Apache-2.0
license, shall be dual licensed as above, without any additional terms or
conditions.
[Documentation]: https://docs.rs/stacktools