pub struct TopologicalSort<T> { /* private fields */ }Expand description
A data structure for topological sorting.
See the crate-level documentation for examples.
Implementations§
Source§impl<T> TopologicalSort<T>
impl<T> TopologicalSort<T>
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates a new empty TopologicalSort.
See the crate-level documentation for examples.
Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the number of remaining items in the TopologicalSort.
This counts all remaining items, including those that are not yet ready to pop.
Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Returns true if the TopologicalSort contains no remaining items.
Sourcepub fn add_dependency<P, S>(&mut self, prec: P, succ: S) -> bool
pub fn add_dependency<P, S>(&mut self, prec: P, succ: S) -> bool
Registers a dependency from prec to succ.
This means that succ depends on prec, so prec must be popped or removed
before succ becomes ready.
Returns true if this dependency link was newly added, or false if it was
already present.
use topological_sort::TopologicalSort;
let mut ts = TopologicalSort::new();
assert!(ts.add_dependency("compile", "link"));
assert_eq!(ts.pop(), Some("compile"));
assert_eq!(ts.pop(), Some("link"));Sourcepub fn add_link(&mut self, link: DependencyLink<T>) -> bool
pub fn add_link(&mut self, link: DependencyLink<T>) -> bool
Registers a dependency link.
This means that link.succ depends on link.prec, so link.prec must be
popped or removed before link.succ becomes ready.
Returns true if this dependency link was newly added, or false if it was
already present.
use topological_sort::{DependencyLink, TopologicalSort};
let mut ts = TopologicalSort::new();
assert!(ts.add_link(DependencyLink {
prec: "compile",
succ: "link"
}));
assert_eq!(ts.pop(), Some("compile"));
assert_eq!(ts.pop(), Some("link"));Sourcepub fn insert<U>(&mut self, item: U) -> boolwhere
U: Into<T>,
pub fn insert<U>(&mut self, item: U) -> boolwhere
U: Into<T>,
Inserts an item, without adding any dependencies from or to it.
Returns true if the item was not already present, or false otherwise.
use topological_sort::TopologicalSort;
let mut ts = TopologicalSort::new();
assert!(ts.insert("standalone"));
assert!(!ts.insert("standalone"));
assert_eq!(ts.pop(), Some("standalone"));Sourcepub fn pop(&mut self) -> Option<T>
pub fn pop(&mut self) -> Option<T>
Removes one item that does not depend on any other remaining item and returns it, or
None if there is no such item.
If pop returns None and len is not 0, the remaining items contain a cycle.
use topological_sort::TopologicalSort;
let mut ts = TopologicalSort::new();
ts.add_dependency("a", "b");
assert_eq!(ts.pop(), Some("a"));
assert_eq!(ts.pop(), Some("b"));
assert_eq!(ts.pop(), None);Sourcepub fn pop_iter(&mut self) -> PopIter<'_, T> ⓘ
pub fn pop_iter(&mut self) -> PopIter<'_, T> ⓘ
Returns an iterator that repeatedly calls pop.
Each call to Iterator::next removes one item from the sort.
The iterator ends when the sort becomes empty or when no item can be popped because the remaining items contain a cycle.
use topological_sort::TopologicalSort;
let mut ts = TopologicalSort::new();
ts.add_dependency(1, 2);
ts.add_dependency(2, 3);
let mut it = ts.pop_iter();
assert_eq!(Some(1), it.next());
assert_eq!(Some(2), it.next());
drop(it);
assert_eq!(Some(3), ts.pop());Sourcepub fn pop_all(&mut self) -> Vec<T>
👎Deprecated since 0.3.0: Use pop_batch instead, which returns an arbitrary collection containing all ready items.
pub fn pop_all(&mut self) -> Vec<T>
Use pop_batch instead, which returns an arbitrary collection containing all ready items.
Removes all items that do not depend on any other remaining item at the time of the call and returns them, or an empty vector if there are no such items.
The returned items are in arbitrary order.
If pop_all returns an empty vector and the sort is not empty, the remaining items contain a cycle.
Sourcepub fn pop_batch<R>(&mut self) -> R
pub fn pop_batch<R>(&mut self) -> R
Removes all items that do not depend on any other remaining item at the time of the call and returns them, or an empty collection if there are no such items.
Unlike pop_iter, this removes only the current batch of ready items. If
removing those items makes more items ready, they are returned by the next call to
pop_batch, not the current one.
The returned items are in arbitrary order.
If pop_batch returns an empty collection and the sort is not empty, the remaining items contain
a cycle.
use topological_sort::TopologicalSort;
let mut ts = TopologicalSort::<i32>::new();
ts.add_dependency(1, 3);
ts.add_dependency(2, 3);
let mut ready = ts.pop_batch::<Vec<_>>();
ready.sort_unstable();
assert_eq!(ready, [1, 2]);
assert_eq!(ts.pop_batch::<Vec<_>>(), [3]);Sourcepub fn peek(&self) -> Option<&T>
pub fn peek(&self) -> Option<&T>
Returns a reference to one item that does not depend on any other remaining item, or
None if there is no such item.
use topological_sort::TopologicalSort;
let mut ts = TopologicalSort::new();
ts.add_dependency("a", "b");
assert_eq!(ts.peek(), Some(&"a"));
assert_eq!(ts.len(), 2);
assert_eq!(ts.pop(), Some("a"));Sourcepub fn peek_all(&self) -> Vec<&T>
👎Deprecated since 0.3.0: Use peek_batch instead, which returns an iterator over all ready items.
pub fn peek_all(&self) -> Vec<&T>
Use peek_batch instead, which returns an iterator over all ready items.
Returns a vector of references to all items that do not depend on any other remaining item at the time of the call.
The returned items are in arbitrary order.
Sourcepub fn peek_batch(&self) -> PeekBatch<'_, T> ⓘ
pub fn peek_batch(&self) -> PeekBatch<'_, T> ⓘ
Returns an iterator over references to all items that do not depend on any other remaining item at the time of the call.
The iterator yields no items if there are no such items. This inspects only the current batch of ready items.
The returned items are in arbitrary order.
use topological_sort::TopologicalSort;
let mut ts = TopologicalSort::<i32>::new();
ts.add_dependency(1, 3);
ts.add_dependency(2, 3);
let mut ready = ts.peek_batch().copied().collect::<Vec<_>>();
ready.sort_unstable();
assert_eq!(ready, [1, 2]);
assert_eq!(ts.len(), 3);Sourcepub fn items(&self) -> Items<'_, T> ⓘ
pub fn items(&self) -> Items<'_, T> ⓘ
Returns an iterator visiting all remaining items in arbitrary order.
This includes items that are not yet ready because they are blocked by unresolved dependencies or cycles.
Sourcepub fn into_items(self) -> IntoItems<T> ⓘ
pub fn into_items(self) -> IntoItems<T> ⓘ
Returns a consuming iterator visiting all remaining items in arbitrary order.
This includes items that are not yet ready because they are blocked by unresolved dependencies or cycles.
Sourcepub fn remove<Q>(&mut self, item: &Q) -> Option<T>
pub fn remove<Q>(&mut self, item: &Q) -> Option<T>
Removes the specified item if it does not depend on any other remaining item and returns it.
Returns None if the item is not present or if it still depends on another remaining item.
Removing the item also removes its outgoing dependency links, which may make some successor items ready.
use topological_sort::TopologicalSort;
let mut ts = TopologicalSort::new();
ts.add_dependency("a", "b");
assert_eq!(ts.remove("b"), None);
assert_eq!(ts.remove("a"), Some("a"));
assert_eq!(ts.remove("b"), Some("b"));Trait Implementations§
Source§impl<T: Clone> Clone for TopologicalSort<T>
impl<T: Clone> Clone for TopologicalSort<T>
Source§fn clone(&self) -> TopologicalSort<T>
fn clone(&self) -> TopologicalSort<T>
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl<T> Debug for TopologicalSort<T>where
T: Debug,
impl<T> Debug for TopologicalSort<T>where
T: Debug,
Source§impl<T> Default for TopologicalSort<T>
impl<T> Default for TopologicalSort<T>
Source§fn default() -> TopologicalSort<T>
fn default() -> TopologicalSort<T>
Source§impl<T> Extend<DependencyLink<T>> for TopologicalSort<T>
impl<T> Extend<DependencyLink<T>> for TopologicalSort<T>
Source§fn extend<I>(&mut self, iter: I)where
I: IntoIterator<Item = DependencyLink<T>>,
fn extend<I>(&mut self, iter: I)where
I: IntoIterator<Item = DependencyLink<T>>,
Source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one)Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one)