use ::std::collections::HashMap; use ::std::hash::Hash; pub mod std { pub fn overwrite(base: &mut T, other: T) { *base = other; } } pub mod vec { use std::collections::HashMap; use merge::Merge; use super::Key; #[allow(unused)] pub fn unify_by_key(base: &mut Vec, other: Vec) where T::Id: Clone + std::hash::Hash + Eq, { // Create a HashMap for O(1) lookup of base agents by their key let mut base_map: HashMap = HashMap::new(); for (index, agent) in base.iter().enumerate() { base_map.insert(agent.key().clone(), index); } for other_agent in other { if let Some(&index) = base_map.get(other_agent.key()) { // If the base contains an agent with the same Key, merge them if let Some(base_agent) = base.get_mut(index) { base_agent.merge(other_agent); } } else { // Otherwise, append the other agent to the base list base.push(other_agent); } } } } pub fn option(base: &mut Option, other: Option) { if other.is_some() { *base = other; } } #[allow(unused)] pub trait Key { type Id: Eq; fn key(&self) -> &Self::Id; } #[allow(unused)] pub fn hashmap(base: &mut HashMap, other: HashMap) { for (key, value) in other { base.insert(key, value); } }