topological-sort
A data structure for topological sorting.
Examples
Modeling Makefile-style dependencies
This example reproduces a small Makefile. Each call to TopologicalSort::pop_batch
returns the next batch of files that can be built in parallel.
: :
use TopologicalSort;
let mut ts = new;
ts.add_dependency;
ts.add_dependency;
ts.add_dependency;
ts.add_dependency;
// Source inputs with no remaining dependencies are ready first.
let mut first_group = ts.;
first_group.sort;
assert_eq!;
// Building those inputs makes the object file ready.
let mut second_group = ts.;
second_group.sort;
assert_eq!;
// Finally, the executable itself becomes ready.
let mut third_group = ts.;
third_group.sort;
assert_eq!;
assert!;
Detecting circular dependencies
This example consumes a sort by repeatedly popping ready items. If any items remain afterward, the remaining subgraph contains a cycle.
use TopologicalSort;
let mut ts1 = new;
ts1.add_dependency;
ts1.add_dependency;
ts1.add_dependency;
let mut ts2 = new;
ts2.add_dependency;
ts2.add_dependency;
assert!;
assert!;
Processing items one at a time
This example repeatedly calls TopologicalSort::pop to process items as soon as each next
item becomes ready.
use TopologicalSort;
let mut ts = new;
ts.add_dependency;
ts.add_dependency;
while let Some = ts.pop
Using TopologicalSort in a task scheduler
TopologicalSort can serve as the dependency tracker inside a task
scheduler. TopologicalSort::peek_batch returns all tasks whose
prerequisites are satisfied, and TopologicalSort::remove marks a
completed task as done, which may make more tasks ready.
Because TopologicalSort::peek_batch does not remove tasks, a scheduler
also needs to track which ready tasks are already running so it does not
start them twice.
use HashSet;
use TopologicalSort;
type Task = String;
How to use?
Add this to your Cargo.toml:
[]
= "0.3.0"
Minimum supported Rust version (MSRV)
The minimum supported Rust version is Rust 1.88.0.
While a crate is pre-release status (0.x.x) it may have its MSRV bumped in a patch release. Once a crate has reached 1.x, any MSRV bump will be accompanied with a new minor version.
License
Licensed under either of
- Apache License, Version 2.0, (LICENSE-APACHE or https://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or https://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.