From 57fcb117fc3f87ac34e947862fc9f86de218689f Mon Sep 17 00:00:00 2001 From: Alexis Sellier Date: Wed, 28 Dec 2022 17:53:00 +0100 Subject: [PATCH] cob: Remove redundant history indices Signed-off-by: Alexis Sellier --- radicle-cob/src/history.rs | 16 ++++------------ radicle-dag/src/lib.rs | 18 ++++++++++++++++++ 2 files changed, 22 insertions(+), 12 deletions(-) diff --git a/radicle-cob/src/history.rs b/radicle-cob/src/history.rs index 7545a242..adf66ac0 100644 --- a/radicle-cob/src/history.rs +++ b/radicle-cob/src/history.rs @@ -21,7 +21,6 @@ pub use entry::{Clock, Contents, Entry, EntryId, EntryWithClock, Timestamp}; #[derive(Clone, Debug)] pub struct History { graph: Dag, - indices: HashMap, } impl PartialEq for History { @@ -150,30 +149,23 @@ impl History { }, ); for tip in tips { - let tip_ix = self.indices.get(&tip.into()).unwrap(); - self.graph.dependency(new_id, (*tip_ix).into()); + self.graph.dependency(new_id, (*tip).into()); } } } fn create_dag<'a>(root: &'a EntryId, entries: &'a HashMap) -> History { - let mut graph: Dag = Dag::new(); - let mut indices = HashMap::::new(); let root_entry = entries.get(root).unwrap().clone(); - graph.node(*root, root_entry.clone()); - indices.insert(root_entry.id, (*root).into()); + let mut graph: Dag = Dag::root(*root, root_entry.clone()); let mut to_process = vec![root_entry]; while let Some(entry) = to_process.pop() { - let entry_ix = indices[&entry.id]; - for child_id in entry.children() { let child = entries[child_id].clone(); graph.node(*child_id, child.clone()); - indices.insert(child.id, (*child_id).into()); - graph.dependency(*child_id, entry_ix.into()); + graph.dependency(*child_id, entry.id); to_process.push(child.clone()); } } - History { graph, indices } + History { graph } } diff --git a/radicle-dag/src/lib.rs b/radicle-dag/src/lib.rs index 0fce13e4..fe2934b0 100644 --- a/radicle-dag/src/lib.rs +++ b/radicle-dag/src/lib.rs @@ -17,6 +17,16 @@ pub struct Node { pub dependents: HashSet, } +impl Node { + fn new(value: V) -> Self { + Self { + value, + dependencies: HashSet::new(), + dependents: HashSet::new(), + } + } +} + impl Borrow for &Node { fn borrow(&self) -> &V { &self.value @@ -49,6 +59,14 @@ impl Dag { } } + pub fn root(key: K, value: V) -> Self { + Self { + graph: HashMap::from_iter([(key, Node::new(value))]), + tips: HashSet::from_iter([key]), + roots: HashSet::from_iter([key]), + } + } + /// Check whether there are any nodes in the graph. pub fn is_empty(&self) -> bool { self.graph.is_empty()