Skip to content

Commit

Permalink
shrink_to_fit for mutable graphs
Browse files Browse the repository at this point in the history
  • Loading branch information
vigna committed Feb 1, 2025
1 parent de8e1fa commit d6c1849
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/graphs/btree_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,16 @@ impl<L: Clone + 'static> LabeledBTreeGraph<L> {
g.add_arcs(arcs);
g
}

/// Shrink the capacity of the graph to fit its current size.
///
/// # Implementation Notes
///
/// This method just shrinks the capacity of the successor vector, as
/// [`BTreeSet`] does not have a `shrink_to_fit` method.
pub fn shrink_to_fit(&mut self) {
self.succ.shrink_to_fit();
}
}

impl<L: Clone + 'static> SequentialLabeling for LabeledBTreeGraph<L> {
Expand Down Expand Up @@ -314,6 +324,16 @@ impl BTreeGraph {
g.add_arcs(arcs);
g
}

/// Shrink the capacity of the graph to fit its current size.
///
/// # Implementation Notes
///
/// This method just shrinks the capacity of the successor vector, as
/// [`BTreeSet`] does not have a `shrink_to_fit` method.
pub fn shrink_to_fit(&mut self) {
self.0.shrink_to_fit();
}
}

impl<'a> IntoLender for &'a BTreeGraph {
Expand Down
13 changes: 13 additions & 0 deletions src/graphs/vec_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,14 @@ impl<L: Clone + 'static> LabeledVecGraph<L> {
g.add_arcs(arcs);
g
}

/// Shrink the capacity of the graph to fit its current size.
pub fn shrink_to_fit(&mut self) {
self.succ.shrink_to_fit();
for s in self.succ.iter_mut() {
s.shrink_to_fit();
}
}
}

impl<L: Clone + 'static> SequentialLabeling for LabeledVecGraph<L> {
Expand Down Expand Up @@ -388,6 +396,11 @@ impl VecGraph {
g.add_arcs(arcs);
g
}

/// Shrink the capacity of the graph to fit its current size.
pub fn shrink_to_fit(&mut self) {
self.0.shrink_to_fit();
}
}

impl<'a> IntoLender for &'a VecGraph {
Expand Down

0 comments on commit d6c1849

Please sign in to comment.