pub trait Locatable { fn get_xy(&self) -> (usize, usize); } #[derive(Clone)] pub struct LocatableVec { arr: [Option; COUNT], len: usize, } pub struct LocatableVecIter<'a, T:Locatable + Copy, const COUNT: usize> { vec : &'a LocatableVec, idx: usize, cnt: usize, } impl LocatableVec { pub fn new() -> LocatableVec { LocatableVec{arr: [None; COUNT], len: 0} } pub fn iter(&self) -> LocatableVecIter<'_, T, COUNT> { LocatableVecIter{vec: self, idx: 0, cnt: 0} } pub fn len(&self) -> usize { self.len } pub fn put(&mut self, value : T) { let (x,y) = value.get_xy(); let idx = y * 9 + x; if self.arr[idx].is_none() { self.len += 1; } self.arr[idx] = Some(value); } pub fn remove(&mut self, value : &T) -> bool{ let (x,y) = value.get_xy(); let idx = y * 9 + x; if self.arr[idx].is_some() { self.arr[idx] = None; self.len -= 1; return true; } false } } impl<'a, T:Locatable + Copy, const COUNT: usize> Iterator for LocatableVecIter<'a, T, COUNT> { type Item = &'a T; fn next(&mut self) -> Option { for i in self.idx..self.vec.arr.len() { let pos = &self.vec.arr[i]; if pos.is_some() { self.idx = i + 1; self.cnt += 1; return pos.as_ref(); } } None } fn size_hint(&self) -> (usize, Option) { (self.vec.len - self.cnt, Some(self.vec.arr.len() - self.idx)) } } impl<'a, L:Locatable + Copy, const COUNT: usize> FromIterator for LocatableVec { fn from_iter>(iter: T) -> Self { let mut x = LocatableVec{arr: [None; COUNT], len: 0}; for p in iter { x.put(p); } x } } pub struct FsVec { arr: [T; COUNT], len: usize } pub struct FsVecIter<'a, T:Copy + Sized + Default, const COUNT: usize> { vec : &'a FsVec, idx: usize, } impl FsVec { pub fn new() -> FsVec { FsVec{arr: [Default::default(); COUNT], len: 0} } pub fn len(&self) -> usize { self.len } pub fn append(&mut self, value : T) { self.arr[self.len] = value; self.len += 1; } pub fn iter(&self) -> FsVecIter<'_, T, COUNT> { FsVecIter{vec: self, idx: 0} } } impl<'a, T:Sized + Copy + Default, const COUNT: usize> Iterator for FsVecIter<'a, T, COUNT> { type Item = &'a T; fn next(&mut self) -> Option { if self.idx >= self.vec.len { return None } let result = Some(&self.vec.arr[self.idx]); self.idx += 1; result } fn size_hint(&self) -> (usize, Option) { (self.vec.len() - self.idx, Some(self.vec.len() - self.idx)) } } impl<'a, L:Sized + Copy + Default, const COUNT: usize> FromIterator for FsVec { fn from_iter>(iter: T) -> Self { let mut x = FsVec::new(); for elem in iter { x.append(elem); } x } }