Skip to content

Commit 8188d45

Browse files
committed
rewriting (does not compile)
1 parent 368db99 commit 8188d45

File tree

2 files changed

+26
-26
lines changed

2 files changed

+26
-26
lines changed

src/dfx/src/lib/graph/traverse_filtered.rs

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,31 @@
11
// TODO: Somebody, adopt this code to `pethgraph`.
22
use petgraph::{data::DataMap, visit::{Dfs, IntoNeighbors, VisitMap}};
33

4-
pub struct DfsFiltered<N, VM>
4+
pub struct DfsFiltered<NodeId, VM>
55
// where P: FnMut(&N) -> bool
66
{
7-
base: Dfs<N, VM>,
7+
base: Dfs<NodeId, VM>,
88
// node_filter: P,
99
}
1010

11-
impl<N, VM> DfsFiltered<N, VM> {
12-
pub fn new(base: Dfs<N, VM>) -> Self {
11+
impl<NodeId, VM> DfsFiltered<NodeId, VM> {
12+
pub fn new(base: Dfs<NodeId, VM>) -> Self {
1313
Self {
1414
base
1515
}
1616
}
1717

18-
pub fn traverse<G, P, C>(&mut self, graph: G, predicate: P, call: C)
19-
where C: Fn(&N, &N) -> (),
20-
G: IntoNeighbors<NodeId = N> + DataMap<NodeWeight = N>,
21-
P: Fn(&N) -> bool,
22-
N: Copy + PartialEq,
23-
VM: VisitMap<N>,
18+
pub fn traverse<G, P, C, NodeWeight>(&mut self, graph: G, predicate: P, call: C)
19+
where C: Fn(&NodeId, &NodeId) -> (),
20+
G: IntoNeighbors<NodeId = NodeId> + DataMap<NodeWeight = NodeWeight>,
21+
P: Fn(&NodeId) -> bool,
22+
NodeId: Copy + PartialEq,
23+
VM: VisitMap<NodeId>,
2424
{
2525
while let Some(item) = &self.base.next(graph) {
2626
if predicate(item) {
27-
let parent = self.base.stack.iter().rev().find(
28-
|&entry| if let Some(elt) = graph.node_weight(*entry) {
29-
predicate(elt)
30-
} else {
31-
false
32-
}
33-
);
34-
if let Some(parent) = parent {
27+
let parent = self.base.stack.iter().rev().find(predicate);
28+
if let Some(parent) = &parent {
3529
call(parent, item);
3630
}
3731
}

src/dfx/src/lib/models/canister.rs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -578,26 +578,32 @@ impl CanisterPool {
578578
Some(canisters_to_build) => canisters_to_build,
579579
None => self.canisters.iter().map(|canister| canister.get_name().to_string()).collect(),
580580
};
581+
// let real_canisters_to_build = real_canisters_to_build.iter().collect(); // hack
582+
let source_graph = &self.imports.borrow().graph;
583+
let source_ids = &self.imports.borrow().nodes;
584+
let start: Vec<_> =
585+
real_canisters_to_build.iter().map(|name| &MotokoImport::Canister(name.clone())).collect(); // `clone` is inefficient.
586+
let start = start.into_iter().map(|node| *source_ids.get(node).unwrap()).collect();
581587
// // Transform the graph of file dependencies to graph of canister dependencies.
582588
// // For this do DFS for each of `real_canisters_to_build`.
583-
let source_graph = &self.imports.borrow().graph;
584589
let mut dest_graph: DiGraph<CanisterId, ()> = DiGraph::new();
585590
let mut dest_id_set = HashMap::new();
586-
let dfs = Dfs::from_parts(real_canisters_to_build, HashMap::new()); // TODO: Use `FixedBitSet` instead of `HashMap`?
591+
let dfs = Dfs::from_parts(start, HashSet::new()); // TODO: Use `FixedBitSet` instead of `HashMap`?
587592
let filtered_dfs = DfsFiltered::new(dfs);
588593
filtered_dfs.traverse(
589594
source_graph,
590-
|s| {
591-
if let Some(MotokoImport::Canister(_parent_name)) = source_graph.node_weight(*entry) {
595+
|&s| {
596+
let source_id = source_graph.node_weight(s);
597+
if let Some(MotokoImport::Canister(_parent_name)) = source_id {
592598
true
593599
} else {
594600
false
595601
}
596602
},
597-
|parent, child| {
598-
let parent_id = *dest_id_set.entry(parent).or_insert_with(|| dest_graph.add_node(parent));
599-
let child_id = *dest_id_set.entry(child).or_insert_with(|| dest_graph.add_node(child));
600-
dest_graph.add_edge(parent_id, child_id, ());
603+
|parent_id, child_id| {
604+
let parent_id = *dest_id_set.entry(*parent_id).or_insert_with(|| parent_id);
605+
let child_id = *dest_id_set.entry(*child_id).or_insert_with(|| child_id);
606+
dest_graph.add_edge(*parent_id, *child_id, ());
601607
}
602608
);
603609
// let source_graph = &self.imports.borrow().graph;

0 commit comments

Comments
 (0)