#[derive(Clone, Debug)]
pub struct Census {
domain: CensusDomain,
}
impl Census {
pub fn new(algebra: &Arc<Algebra>, dimensions: Vec<usize>) -> Result<Self, CensusError> {
Ok(Self {
domain: CensusDomain::new(algebra, dimensions)?,
})
}
pub fn from_dimensions(
algebra: &Arc<Algebra>,
dimensions: &[usize],
) -> Result<Self, CensusError> {
Self::new(algebra, dimensions.to_vec())
}
accessor_methods! {
pub domain() -> &CensusDomain = |this| &this.domain;
}
pub fn run(&self) -> CensusOutcome {
self.run_with(CensusLimits::default(), None)
}
pub fn run_with(
&self,
limits: CensusLimits,
control: Option<&ComputationControl>,
) -> CensusOutcome {
enumerate_state(CensusState::empty(self.domain.clone(), limits), control)
}
pub fn resume(
&self,
checkpoint: &CensusCut,
limits: CensusLimits,
control: Option<&ComputationControl>,
) -> Result<CensusOutcome, CensusResumeError> {
if self.domain != checkpoint.state.domain {
return Err(CensusResumeError::DifferentDomain);
}
if checkpoint.state.limits.retention != limits.retention {
return Err(CensusResumeError::RetentionMismatch {
checkpoint: checkpoint.state.limits.retention,
requested: limits.retention,
});
}
if !checkpoint.verify() {
return Err(CensusResumeError::InvalidCheckpoint);
}
let mut state = checkpoint.state.clone();
state.limits = limits;
Ok(enumerate_state(state, control))
}
pub fn replay(&self, result: &CensusResult) -> bool {
self.domain.eq(result.domain()) && result.verify()
}
}
pub fn census(
algebra: &Arc<Algebra>,
dimensions: Vec<usize>,
limits: CensusLimits,
control: Option<&ComputationControl>,
) -> Result<CensusOutcome, CensusError> {
Ok(Census::new(algebra, dimensions)?.run_with(limits, control))
}
pub fn checked_search_space_size(
algebra: &Arc<Algebra>,
dimensions: &[usize],
) -> Result<u128, CensusError> {
CensusDomain::new(algebra, dimensions.to_vec()).map(|domain| domain.raw_space_size())
}