Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use std::fmt::Display;
- use std::fmt::Formatter;
- use std::fmt::Result as FmtResult;
- use std::num::NonZeroU8;
- type V = Option<NonZeroU8>;
- type B4 = [[[V; 4]; 4]; 4];
- type BX4 = Box<B4>;
- #[inline]
- fn nv(v: u8) -> V {
- NonZeroU8::new(v)
- }
- struct Box4 {
- bx: Option<BX4>,
- }
- impl Box4 {
- fn new() -> Self {
- Box4 { bx: None }
- }
- fn init_box(&mut self) {
- self.bx = Some(Box::new([[[None; 4]; 4]; 4]))
- }
- fn drop_box(&mut self) {
- self.bx = None
- }
- fn fast_get_1( &self, x: usize, y: usize, z: usize ) -> V {
- self.bx.as_ref().map( |b|{b[x][y][z]} ).unwrap()
- }
- fn fast_get_2( &self, x: usize, y: usize, z: usize ) -> V {
- match self.bx {
- None => None,
- Some(ref b) => b[x][y][z],
- }
- }
- }
- impl Clone for Box4 {
- fn clone(&self) -> Self {
- match self.bx {
- None => Box4 { bx: None },
- Some(ref v) => Box4 {
- bx: Some(Box::new(*v.clone())),
- },
- }
- }
- }
- // impl Display for Box4 {
- // fn fmt(&self, f: &mut Formatter) -> FmtResult {
- // match self.bx {
- // None => {
- // write!(f, "| None |");
- // }
- // Some(ref l1) => {
- // // write!( f, "| Some |" );
- // for l2 in l1.iter() {
- // write!(f, "{}", "[ ");
- // for l3 in l2.iter() {
- // write!(f, "{}", "[ ");
- // for v in l3.iter() {
- // write!(f, " {:?} ", v);
- // }
- // write!(f, "{}", " ] ");
- // }
- // writeln!(f, "{}", " ]");
- // }
- // }
- // };
- // Ok(())
- // }
- // }
- fn main() {
- // println!("{}", std::mem::size_of::<B4>());
- // println!("{}", std::mem::size_of::<Box4>());
- // let mut t = [ Box4::new(); 128 ];
- let mut tt: Vec<Box4> = Vec::new();
- tt.resize(4, Box4::new());
- tt[2].init_box();
- {
- let bx = tt[2].bx.as_mut().unwrap();
- bx[0][0][1] = nv(1);
- bx[0][1][0] = nv(2);
- bx[1][0][0] = nv(3);
- }
- let box4 = &tt[2];
- println!("{:?}", box4.fast_get_1(0,0,1));
- println!("{:?}", box4.fast_get_2(0,1,0));
- // println!("{}", tt[2]);
- }
Advertisement
Add Comment
Please, Sign In to add comment