heron_rebuild_workflow/branch/
baselines.rs

1use crate::{BranchpointId, IdentId};
2
3/// Keeps track of which branch is baseline for each defined branch
4/// in the workflow.
5#[derive(Debug)]
6pub struct BaselineBranches {
7    vec: Vec<IdentId>,
8}
9
10impl BaselineBranches {
11    /// Create a new `BaselineBranches` with the given capacity.
12    pub fn with_capacity(cap: usize) -> Self {
13        Self {
14            vec: Vec::with_capacity(cap),
15        }
16    }
17
18    /// Set the given `IdentId` as the baseline for the given branchpoint.
19    pub fn add(&mut self, k: BranchpointId, v: IdentId) {
20        let k: usize = k.into();
21        let len = self.vec.len();
22        if k >= len {
23            self.vec.reserve(k + 1);
24            self.vec.append(&mut vec![crate::NULL_IDENT; k + 1 - len]);
25        }
26        let existing_v = &mut self.vec[k];
27        if *existing_v == crate::NULL_IDENT {
28            *existing_v = v;
29        }
30    }
31
32    /// Get the `IdentId` of baseline branch for the given branchpoint.
33    pub fn get(&self, k: BranchpointId) -> IdentId {
34        let k: usize = k.into();
35        self.vec[k]
36    }
37
38    /// Iterate through baseline branch values.
39    // NB the first part of the tuple is equivalent to a BranchpointId.
40    pub fn iter(&self) -> impl Iterator<Item = (usize, &IdentId)> {
41        self.vec.iter().enumerate()
42    }
43}