use crate::geometry::*; use crate::constants::*; #[derive(Default, Debug, PartialEq, Eq, Clone)] pub struct Map { cells: [u64; MAP_U64S] } impl Map { pub fn at(&self, p: Point2d) -> Option { if p.y < 0 || p.y as usize >= MAP_SIZE { None } else { let row_data = &MAP_ROW_SIZE[p.y as usize]; if p.x < row_data.x_offset as i8 || p.x as usize >= row_data.x_offset + row_data.len() { None } else { let global_bit = row_data.start_bit + p.x as usize - row_data.x_offset; let integer = global_bit / 64; let bit = global_bit % 64; let mask = 1 << bit; Some(self.cells[integer] & mask != 0) } } } pub fn set(&mut self, p: Point2d) { if p.y < 0 || p.y as usize >= MAP_SIZE { debug_assert!(false, "Tried to set an out of bounds bit, {:?}", p); } else { let row_data = &MAP_ROW_SIZE[p.y as usize]; if p.x < row_data.x_offset as i8 || p.x as usize >= row_data.x_offset + row_data.len() { debug_assert!(false, "Tried to set an out of bounds bit, {:?}", p); } else { let global_bit = row_data.start_bit + p.x as usize - row_data.x_offset; let integer = global_bit / 64; let bit = global_bit % 64; let mask = 1 << bit; self.cells[integer] |= mask; } } } pub fn clear(&mut self, p: Point2d) { if p.y < 0 || p.y as usize >= MAP_SIZE { debug_assert!(false, "Tried to set an out of bounds bit, {:?}", p); } else { let row_data = &MAP_ROW_SIZE[p.y as usize]; if p.x < row_data.x_offset as i8 || p.x as usize >= row_data.x_offset + row_data.len() { debug_assert!(false, "Tried to set an out of bounds bit, {:?}", p); } else { let global_bit = row_data.start_bit + p.x as usize - row_data.x_offset; let integer = global_bit / 64; let bit = global_bit % 64; let mask = !(1 << bit); self.cells[integer] &= mask; } } } } #[cfg(test)] mod test { // TODO: Property test for at, set and clear }