Skip to content

Commit

Permalink
petgraph works
Browse files Browse the repository at this point in the history
  • Loading branch information
pawarherschel committed Jan 26, 2024
1 parent 490e280 commit df8898b
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,8 @@ graph2_sorted.ron
owner_id.txt
owner_name.txt
sorted_undirected_graph.ron
dots/
graphs/
!dots/boop
dot_edge_no_label.dot
dot_edge_with_label.dot
17 changes: 17 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ async-compat = "0.2.3"
smol = "1.3.0"
serde = { version = "1.0.192", features = ["derive"] }
ron = "0.8.1"
petgraph = "0.6.4"
79 changes: 79 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
extern crate core;

use std::collections::{HashMap, HashSet};
use std::fs;
use std::sync::{Arc, RwLock};
use std::time::Instant;

use async_compat::Compat;
use indicatif::ParallelProgressIterator;
use petgraph::dot::Config;
use petgraph::Graph;
use rayon::prelude::*;
use ron::ser::{to_writer_pretty, PrettyConfig};
use sqlx::SqlitePool;
Expand All @@ -18,6 +21,41 @@ use vrcx_insights::zaphkiel::world_instance::WorldInstance;

#[allow(clippy::too_many_lines)]
fn main() {
// let mut map = HashMap::new();
// map.insert("A", vec!["B", "C", "D"]);
// map.insert("B", vec!["A", "C"]);
// map.insert("C", vec!["A", "E"]);
// map.insert("D", vec!["A"]);
// map.insert("F", vec![]);
//
// let mut deps = UnGraph::new_undirected();
// let mut deps_idxs = HashMap::new();
// for node in map.keys() {
// let idx = deps.add_node(node.to_owned());
// deps_idxs.insert(node.to_owned(), idx);
// }
// for (node, edges) in map {
// for edge in edges {
// let Some(node_idx) = deps_idxs.get(node) else {
// unreachable!()
// };
// let node_idx = node_idx.to_owned();
// deps_idxs.entry(edge).or_insert_with(|| deps.add_node(edge));
// let Some(edge_idx) = deps_idxs.get(edge) else {
// unreachable!()
// };
// let edge_idx = edge_idx.to_owned();
// deps.add_edge(node_idx, edge_idx, ());
// }
// }
//
// println!(
// "{:?}",
// petgraph::dot::Dot::with_config(&deps, &[Config::EdgeNoLabel])
// );
//
// todo!();

let start = Instant::now();

let owner_id: String = std::fs::read_to_string("owner_id.txt").unwrap();
Expand Down Expand Up @@ -206,6 +244,47 @@ fn main() {
.unwrap();
});

let mut petgraph = Graph::new();
let mut dot_idxs = HashMap::new();

time_it! {"converting from hashmap to petgraph" =>
for (node, edges) in graph2_sorted {
// if node == "Kat Sakura" {
// continue;
// }

for (edge, weight) in edges {
// if edge == "Kat Sakura" {
// continue;
// }

dot_idxs
.entry(node.clone())
.or_insert_with(|| petgraph.add_node(node.clone()));
let node_idx = dot_idxs.get(&node).unwrap().to_owned();

dot_idxs
.entry(edge.clone())
.or_insert_with(|| petgraph.add_node(edge.clone()));
let edge_idx = dot_idxs.get(&edge).unwrap().to_owned();

petgraph.add_edge(node_idx, edge_idx, weight);
}
}
}

let dot_edge_no_label = petgraph::dot::Dot::with_config(&petgraph, &[Config::EdgeNoLabel]);
let dot_edge_with_label = petgraph::dot::Dot::new(&petgraph);
time_it! { "writing dots" => {
fs::write("dot_edge_no_label.dot", format!("{dot_edge_no_label:?}")).unwrap();
fs::write(
"dot_edge_with_label.dot",
format!("{dot_edge_with_label:?}"),
)
.unwrap();
}
}

println!("\x07Total run time => {:?}", start.elapsed());
}

Expand Down

0 comments on commit df8898b

Please sign in to comment.