design.rs
1.4 kB · rust · 64 lines
1// DESIGN23#[derive(Clone, Copy, Debug)]4pub struct Design {5 pub name: &'static str,6 pub dimension: usize,7 pub invert: bool,8}910pub const MENGER: Design = Design {11 name: "menger",12 dimension: 3,13 invert: false,14};1516pub const CARPET: Design = Design {17 name: "carpet",18 dimension: 2,19 invert: false,20};2122pub const VICSEK: Design = Design {23 name: "vicsek",24 dimension: 2,25 invert: true,26};2728pub const DESIGNS: [Design; 3] = [MENGER, CARPET, VICSEK];2930impl Design {31 pub fn hit(&self, digit: u64) -> bool {32 (digit == 1) != self.invert33 }3435 pub fn corners(&self) -> Vec<Vec<u64>> {36 let mut out = Vec::new();37 let total = 3u64.pow(self.dimension as u32);38 for code in 0..total {39 let mut vector = Vec::with_capacity(self.dimension);40 let mut rest = code;41 for _ in 0..self.dimension {42 vector.push(rest % 3);43 rest /= 3;44 }45 vector.reverse();46 if vector.iter().filter(|d| self.hit(**d)).count() <= 1 {47 out.push(vector);48 }49 }50 out51 }5253 pub fn fill(&self) -> u64 {54 self.corners().len() as u6455 }5657 pub fn zero_filled(&self) -> bool {58 !self.invert || self.dimension < 259 }6061 pub fn named(name: &str) -> Option<Design> {62 DESIGNS.iter().find(|d| d.name == name).copied()63 }64}