diff --git a/src/main.rs b/src/main.rs index 96e1d75..7aebfbc 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,7 +1,43 @@ mod world; +use world::*; fn main() { println!("Hello, dev!"); + + let mut ma: MagicContainer = MagicContainer::new(); + + ma.add(2); + ma.add(3); + ma.add(4); + ma.add(5); + ma.add(6); + + ma.debug_log(); + + if let Err(a) = ma.rm(1) { + println!("ERROR: {:?}", a); + return; + } + + ma.debug_log(); + + if let Err(a) = ma.rm(0) { + println!("ERROR: {:?}", a); + return; + } + + ma.debug_log(); + + + ma.add(8); + + ma.debug_log(); + + ma.add(9); + ma.add(10); + ma.add(11); + + ma.debug_log(); } diff --git a/src/world/grid/mod.rs b/src/world/grid.rs similarity index 100% rename from src/world/grid/mod.rs rename to src/world/grid.rs diff --git a/src/world/m_container.rs b/src/world/m_container.rs new file mode 100644 index 0000000..10a40fe --- /dev/null +++ b/src/world/m_container.rs @@ -0,0 +1,103 @@ +use std::fmt::Debug; + + +#[derive(Debug)] +pub enum MCError { + BrocenLen, + EntityNotExisit, +} + + + +pub struct MagicContainer{ + data_index: Vec, + id: Vec, + data: Vec, +} + +impl MagicContainer{ + pub fn new() -> Self { + MagicContainer { + data_index: Vec::new(), + id: Vec::new(), + data: Vec::new(), + } + } +} + +impl MagicContainer { + pub fn add(&mut self, value: T){ + let l = self.data_index.len(); + if l == self.data.len() { + self.data_index.push(l); + self.id.push(l); + self.data.push(value); + } else if l > self.data.len() { + self.data.push(value); + } else { + panic!("data_index < data"); + } + } + pub fn add_and_id(&mut self, value: T) -> usize{ + let l = self.data_index.len(); + if l == self.data.len() { + self.data_index.push(l); + self.id.push(l); + self.data.push(value); + } else if l > self.data.len() { + self.data.push(value); + } else { + panic!("data_index < data"); + } + + self.id[self.data.len() -1] + } + pub fn rm(&mut self, entity_id: usize) -> Result<(), MCError>{ + let index = self.data_index[entity_id]; + let last = self.data.len() - 1; + + if self.data.len() > self.data_index.len() { + return Err(MCError::BrocenLen); + } + if index > last { + return Err(MCError::EntityNotExisit); + } + + self.id.swap(last, index); + self.data.swap(last, index); + self.data_index[self.id[index]] = index; + self.data_index[self.id[last]] = last; + self.data.pop(); + + Ok(()) + } + pub fn get(&self, entity_id: usize) -> Option<&T> { + let index = *self.data_index.get(entity_id)?; + + if index >= self.data.len() { + return None; + } + + Some(&self.data[index]) + } +} + +impl MagicContainer{ + pub fn debug(&self) { + println!("data_index: {:?}", self.data_index); + println!("id: {:?}", self.id); + println!("data: {:?}", self.data); + } + pub fn debug_log(&self){ + println!("--- access test ---"); + + for id in 0..self.data_index.len() { + match self.get(id) { + Some(v) => println!("ID {} => {:?}", id, v), + None => println!("ID {} => ", id), + } + } + + println!("-------------------"); + } +} diff --git a/src/world/mod.rs b/src/world/mod.rs index 3493b74..9f25f74 100644 --- a/src/world/mod.rs +++ b/src/world/mod.rs @@ -6,10 +6,13 @@ pub use grid::TorusGrid; mod agent; pub use agent::Agent; +mod m_container; +pub use m_container::*; + pub struct World{ grid: TorusGrid, - agents: Vec>>, - a_grid: TorusGrid>>>, + agents: MagicContainer, + a_grid: TorusGrid>, size: (usize, usize), } @@ -17,7 +20,7 @@ impl World { pub fn new(s: (usize, usize)) -> Self { World { grid: TorusGrid::new(false, s), - agents: Vec::new(), + agents: MagicContainer::new(), a_grid: TorusGrid::new(None, s), size: s, } @@ -28,15 +31,11 @@ impl World { pub fn add_agent(&mut self, ag: Agent) -> Result<(), &str> { let pos = ag.get_pos(); let p = self.a_grid.get_mut_wrap(pos); - // if *p != None { - // return Err("w"); - // } if p.is_some() { return Err("ERROR: pos занято"); } - let rec_ag = Arc::new(Mutex::new(ag)); - self.agents.push(Arc::clone(&rec_ag)); - *p = Some(Arc::clone(&rec_ag)); + let id = self.agents.add_and_id(ag); + *p = Some(id); Ok(()) } pub fn get_grid(&self) -> &TorusGrid{ @@ -45,8 +44,9 @@ impl World { pub fn get_mut_grid(&mut self) -> &mut TorusGrid{ &mut self.grid } - pub fn get_a_grid(&self) -> &TorusGrid>>>{ + pub fn get_a_grid(&self) -> &TorusGrid>{ &self.a_grid } + // pub fn }