struct AgentCollection {
available_ids: Vec<AgentId>,
agents: HashMap<AgentId, Box<dyn EconomicAgent>>
}
impl AgentCollection {
fn create_agent(&mut self, f: fn(AgentId) -> Box<dyn EconomicAgent>) -> Option<AgentId> {
let id = self.available_ids.pop()?;
let agent = f(id);
self.agents.insert(id, agent);
Some(id)
}
fn destroy_agent(&mut self, agent_id: AgentId) {
self.agents.remove(&agent_id);
self.available_ids.push(agent_id);
}
fn new(size:u32) -> AgentCollection {
AgentCollection{available_ids:(1..size).collect(), agents:HashMap::new()}
}
}
available_ids
starts with a fixed list of ids. As agents are created they take an id from this list. As agents are destroyed, they return an id to this list. The list will never be bigger than when it was first created, so it is not ever necessary to re-allocate.
Assuming I never call shrink_to_fit
or something similar, will this vector remain in the same memory location for the life of my application? Or are there some rules for conserving space baked into the object?