heh
Diffstat (limited to 'src/util.rs')
-rw-r--r--src/util.rs33
1 files changed, 33 insertions, 0 deletions
diff --git a/src/util.rs b/src/util.rs
index 00b8f7a..e64fbf1 100644
--- a/src/util.rs
+++ b/src/util.rs
@@ -480,6 +480,14 @@ pub fn reachable<D: Hash + Copy, N: Debug + Eq + Hash + Copy + Ord, I: Iterator<
map
}
+pub fn ccomponents<I: Iterator<Item = usize>>(graph: impl Fn(usize) -> I, length: usize) -> usize {
+ let mut s = UnionFind::new(length);
+ for n in 0..length {
+ graph(n).map(|y| s.union(n, y)).θ();
+ }
+ (0..length).map(|x| s.find(x)).collect::<HashSet<_>>().len()
+}
+
pub fn dijkstra_h<N: Debug + Eq + Hash + Copy + Ord, I: Iterator<Item = (N, u16)>>(
start: N,
graph: impl Fn(N) -> I,
@@ -2112,6 +2120,31 @@ pub fn parse_digraph<'a, N: Hash + Ord, D>(
});
map
}
+pub fn parse_graph<'a, N: Hash + Ord + Clone>(
+ x: &'a [u8],
+ mut f: impl FnMut(&'a [u8]) -> N + Copy,
+) -> HashMap<N, Vec<N>> {
+ let mut map = HashMap::default();
+ x.行().for_each(|x| {
+ let (node, connections) = match memchr::memmem::find(x, b"--") {
+ Some(index) => (
+ &x[..index - 1],
+ x[index + 2..]
+ .μₙ(b',')
+ .map(<[u8]>::trim_ascii)
+ .map(f)
+ .collect::<Vec<_>>(),
+ ),
+ None => (x, vec![]),
+ };
+
+ // for c in &connections {
+ // map.entry(c.clone()).or_default().push(c);
+ // }
+ map.entry(f(node)).or_insert(connections);
+ });
+ map
+}
pub mod python {