From cc9008c6bb9ebcf8275e999b3225a5b69b975838 Mon Sep 17 00:00:00 2001 From: Martin von Zweigbergk Date: Wed, 23 Dec 2020 09:58:13 -0800 Subject: [PATCH] evolution: create State struct in place I'm about to add more fields to the type and this will help to slightly reduce the boilerplate for initializing and the maps and sets and creating the struct. --- lib/src/evolution.rs | 38 +++++++++++++++++--------------------- 1 file changed, 17 insertions(+), 21 deletions(-) diff --git a/lib/src/evolution.rs b/lib/src/evolution.rs index da71b8e4c..df3172b14 100644 --- a/lib/src/evolution.rs +++ b/lib/src/evolution.rs @@ -28,7 +28,7 @@ use crate::transaction::{MutableRepo, Transaction}; use crate::trees::merge_trees; use crate::view::View; -#[derive(Debug, Clone)] +#[derive(Debug, Clone, Default)] struct State { /// Contains all successors whether they have the same change id or not. successors: HashMap>, @@ -41,10 +41,7 @@ struct State { impl State { fn calculate(store: &StoreWrapper, view: &dyn View) -> State { - let mut successors = HashMap::new(); - let mut obsolete_commits = HashSet::new(); - let mut orphan_commits = HashSet::new(); - let mut divergent_changes = HashMap::new(); + let mut state = State::default(); let mut heads = vec![]; for commit_id in view.heads() { heads.push(store.get_commit(commit_id).unwrap()); @@ -64,18 +61,19 @@ impl State { // children of a commit for commit in &commits { if commit.is_pruned() { - obsolete_commits.insert(commit.id().clone()); + state.obsolete_commits.insert(commit.id().clone()); } for predecessor in commit.predecessors() { if !commits.contains(&predecessor) { continue; } - successors + state + .successors .entry(predecessor.id().clone()) .or_insert_with(HashSet::new) .insert(commit.id().clone()); if predecessor.change_id() == commit.change_id() { - obsolete_commits.insert(predecessor.id().clone()); + state.obsolete_commits.insert(predecessor.id().clone()); } } for parent in commit.parents() { @@ -86,33 +84,31 @@ impl State { } // Find divergent commits for (change_id, commit_ids) in change_to_commits { - let divergent: HashSet = - commit_ids.difference(&obsolete_commits).cloned().collect(); + let divergent: HashSet = commit_ids + .difference(&state.obsolete_commits) + .cloned() + .collect(); if divergent.len() > 1 { - divergent_changes.insert(change_id, divergent); + state.divergent_changes.insert(change_id, divergent); } } // Find orphans by walking to the children of obsolete commits - let mut work: Vec = obsolete_commits.iter().map(ToOwned::to_owned).collect(); + let mut work: Vec = state.obsolete_commits.iter().cloned().collect(); while !work.is_empty() { let commit_id = work.pop().unwrap(); for child in children.get(&commit_id).unwrap() { - if orphan_commits.insert(child.clone()) { + if state.orphan_commits.insert(child.clone()) { work.push(child.clone()); } } } - orphan_commits = orphan_commits - .difference(&obsolete_commits) + state.orphan_commits = state + .orphan_commits + .difference(&state.obsolete_commits) .map(ToOwned::to_owned) .collect(); - State { - successors, - obsolete_commits, - orphan_commits, - divergent_changes, - } + state } fn successors(&self, commit_id: &CommitId) -> HashSet {