use allocative::Allocative;
use serde::{Deserialize, Serialize};
#[cfg(feature = "detailed_costs")]
use std::collections::HashMap;
use crate::model::unit::Cost;
#[derive(Serialize, Deserialize, Default, Clone, Debug, Allocative)]
pub struct TraversalCost {
pub objective_cost: Cost,
pub total_cost: Cost,
#[cfg(feature = "detailed_costs")]
pub cost_component: HashMap<String, Cost>,
}
impl TraversalCost {
pub fn insert(&mut self, #[allow(unused_variables)] name: &str, cost: Cost, weight: f64) {
let positive_cost = Cost::enforce_strictly_positive(cost);
self.total_cost += positive_cost;
self.objective_cost += positive_cost * weight;
#[cfg(feature = "detailed_costs")]
{
self.cost_component
.entry(name.to_string())
.and_modify(|c| *c += positive_cost)
.or_insert(positive_cost);
}
}
}