m_container: is done
This commit is contained in:
1 parent
25af75b381
commit
580894ed0a
4 files changed
+149
-10
No files matched your search
+36
@@ -1,7 +1,43 @@
|
||||
|
||||
|
||||
mod world;
|
||||
use world::*;
|
||||
|
||||
fn main() {
|
||||
println!("Hello, dev!");
|
||||
|
||||
let mut ma: MagicContainer<i32> = 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();
|
||||
}
|
||||
File renamed without changes.
@@ -0,0 +1,103 @@
|
||||
use std::fmt::Debug;
|
||||
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum MCError {
|
||||
BrocenLen,
|
||||
EntityNotExisit,
|
||||
}
|
||||
|
||||
|
||||
|
||||
pub struct MagicContainer<T>{
|
||||
data_index: Vec<usize>,
|
||||
id: Vec<usize>,
|
||||
data: Vec<T>,
|
||||
}
|
||||
|
||||
impl<T: Clone> MagicContainer<T>{
|
||||
pub fn new() -> Self {
|
||||
MagicContainer {
|
||||
data_index: Vec::new(),
|
||||
id: Vec::new(),
|
||||
data: Vec::new(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> MagicContainer<T> {
|
||||
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<T: Debug> MagicContainer<T>{
|
||||
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 {} => <deleted>", id),
|
||||
}
|
||||
}
|
||||
|
||||
println!("-------------------");
|
||||
}
|
||||
}
|
||||
+10
-10
@@ -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<bool>,
|
||||
agents: Vec<Arc<Mutex<Agent>>>,
|
||||
a_grid: TorusGrid<Option<Arc<Mutex<Agent>>>>,
|
||||
agents: MagicContainer<Agent>,
|
||||
a_grid: TorusGrid<Option<usize>>,
|
||||
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<bool>{
|
||||
@@ -45,8 +44,9 @@ impl World {
|
||||
pub fn get_mut_grid(&mut self) -> &mut TorusGrid<bool>{
|
||||
&mut self.grid
|
||||
}
|
||||
pub fn get_a_grid(&self) -> &TorusGrid<Option<Arc<Mutex<Agent>>>>{
|
||||
pub fn get_a_grid(&self) -> &TorusGrid<Option<usize>>{
|
||||
&self.a_grid
|
||||
}
|
||||
// pub fn
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user