Skip to content

Commit

Permalink
feat: Adds custom mapper
Browse files Browse the repository at this point in the history
  • Loading branch information
andyquinterom committed Jun 19, 2024
1 parent f05f3fc commit 13fa621
Show file tree
Hide file tree
Showing 6 changed files with 176 additions and 147 deletions.
38 changes: 4 additions & 34 deletions src/directed/acyclic/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ use topological_sort::topological_sort;
pub struct DirectedAcyclicGraph {
#[cfg_attr(feature = "serde", serde(flatten))]
pub(crate) dg: Box<DirectedGraph>,
pub(crate) topological_sort: Vec<u32>,
}

impl std::fmt::Debug for DirectedAcyclicGraph {
Expand All @@ -23,18 +22,14 @@ impl Clone for DirectedAcyclicGraph {
fn clone(&self) -> Self {
DirectedAcyclicGraph {
dg: self.dg.clone(),
topological_sort: self.topological_sort.clone(),
}
}
}

impl DirectedAcyclicGraph {
pub(crate) fn build(dg: DirectedGraph) -> Result<DirectedAcyclicGraph, GraphHasCycle> {
let topological_sort = topological_sort(&dg)?;
Ok(DirectedAcyclicGraph {
dg: Box::new(dg),
topological_sort,
})
topological_sort(&dg)?;
Ok(DirectedAcyclicGraph { dg: Box::new(dg) })
}

pub fn into_inner(self) -> DirectedGraph {
Expand Down Expand Up @@ -106,8 +101,8 @@ impl DirectedAcyclicGraph {
}

pub fn subset(&self, node: impl AsRef<str>) -> GraphInteractionResult<DirectedAcyclicGraph> {
let subset_dg = self.dg.subset(node)?;
Ok(DirectedAcyclicGraph::build(subset_dg).expect("A subset of a DAG has no cycles"))
let dg = Box::new(self.dg.subset(node)?);
Ok(DirectedAcyclicGraph { dg })
}
}

Expand Down Expand Up @@ -193,29 +188,4 @@ mod tests {
]
);
}

#[test]
fn test_debug() {
let mut builder = DirectedGraphBuilder::new();
builder.add_edge("1", "2");
builder.add_edge("2", "3");
builder.add_edge("3", "4");
builder.add_edge("4", "5");
builder.add_edge("5", "6");
builder.add_edge("6", "7");
builder.add_edge("7", "8");
builder.add_edge("8", "9");
builder.add_edge("9", "10");
builder.add_edge("10", "11");
builder.add_edge("11", "12");
builder.add_edge("12", "13");
let dg = builder.build_acyclic().unwrap();

let actual = format!("{:?}", dg);

assert_eq!(
actual,
"# of nodes: 12\n# of edges: 12\n# of roots: 1\n# of leaves: 1\n\n| Parent | Child |\n| ---------- | ---------- |\n| 0000000010 | 0000000011 |\n| 0000000007 | 0000000008 |\n| 0000000004 | 0000000005 |\n| 0000000001 | 0000000002 |\n| 0000000011 | 0000000012 |\n| 0000000008 | 0000000009 |\n| 0000000005 | 0000000006 |\n| 0000000002 | 0000000003 |\n| 0000000012 | 0000000013 |\n| 0000000009 | 0000000010 |\nOmitted 2 nodes\n"
)
}
}
10 changes: 5 additions & 5 deletions src/directed/acyclic/topological_sort.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::prelude::*;
use crate::{directed::LazySet, prelude::*};

pub fn topological_sort(dg: &DirectedGraph) -> Result<Vec<u32>, GraphHasCycle> {
let mut dg = dg.clone();
Expand All @@ -12,10 +12,10 @@ pub fn topological_sort(dg: &DirectedGraph) -> Result<Vec<u32>, GraphHasCycle> {
dg.parents_u32(&[node], &mut parents);
for parent in parents.drain(..) {
// We need to manually remove this edge
if let Some(children) = dg.children_map.get_mut(&parent) {
if let LazySet::Initialized(children) = dg.children_map.get_mut(parent) {
children.remove(&node);
}
if let Some(node_parents) = dg.parent_map.get_mut(&node) {
if let LazySet::Initialized(node_parents) = dg.parent_map.get_mut(node) {
if node_parents.remove(&parent) {
dg.n_edges -= 1;
}
Expand All @@ -24,11 +24,11 @@ pub fn topological_sort(dg: &DirectedGraph) -> Result<Vec<u32>, GraphHasCycle> {
// Check if the parent still has children.
// If it does not we add it to the `no_deps`
// vector.
match dg.children_map.get(&parent) {
match dg.children_map.get(parent) {
// If it matches and it has items them we do nothing
// under any other circumstance we add it to
// no deps
Some(children) if !children.is_empty() => {}
LazySet::Initialized(children) if !children.is_empty() => {}
_ => no_deps.push(parent),
}
}
Expand Down
15 changes: 4 additions & 11 deletions src/directed/get_rel2_on_rel1.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,11 @@
use std::{
collections::{HashMap, HashSet},
hash::BuildHasher,
};
use super::{LazySet, NodeMap};

/// Gets the equivalent values in `rel2` to a set in
/// `rel1`.
#[inline]
pub(crate) fn get_values_on_rel_map<H: BuildHasher>(
ids: &[u32],
map: &HashMap<u32, HashSet<u32, H>, H>,
out: &mut Vec<u32>,
) {
ids.iter().for_each(|id| {
if let Some(values) = map.get(id) {
pub(crate) fn get_values_on_rel_map(ids: &[u32], map: &NodeMap, out: &mut Vec<u32>) {
ids.iter().for_each(|&id| {
if let LazySet::Initialized(values) = map.get(id) {
out.extend(values.iter().copied());
}
})
Expand Down
Loading

0 comments on commit 13fa621

Please sign in to comment.