Fix writter

This commit is contained in:
Adrian Woźniak 2024-09-16 12:32:01 +02:00
parent 353e59d66a
commit 6b7681bad6

View File

@ -451,12 +451,22 @@ impl<const X: usize, const Y: usize, const SIZE: usize> AsciiBroad<X, Y, SIZE> {
} }
} }
struct BufWriter<'s>(&'s mut [MazePart]); struct BufWriter<'s>(&'s mut [MazePart], usize);
impl<'s> BufWriter<'s> { impl<'s> BufWriter<'s> {
pub fn new(b: &'s mut [MazePart]) -> Self {
Self(b, 0)
}
pub fn push(&mut self, part: MazePart) { pub fn push(&mut self, part: MazePart) {
self.0[0] = part; self.0[self.1] = part;
self.0 = &mut self.0[1..]; self.1 += 1;
}
pub fn copy(&mut self, src: &[MazePart]) {
src.iter()
.take_while(|p| **p != MazePart::Noop)
.for_each(|p| {
self.push(*p);
});
} }
} }
@ -464,22 +474,12 @@ pub struct BinaryMap<const X: usize, const Y: usize, const SIZE: usize>;
impl<const X: usize, const Y: usize, const SIZE: usize> BinaryMap<X, Y, SIZE> { impl<const X: usize, const Y: usize, const SIZE: usize> BinaryMap<X, Y, SIZE> {
fn format(&self, grid: &Grid<X, Y, SIZE>, buffer: &mut [MazePart]) { fn format(&self, grid: &Grid<X, Y, SIZE>, buffer: &mut [MazePart]) {
let push = |p: MazePart, buffer: &mut [MazePart]| { let mut bw = BufWriter::new(buffer);
buffer[0] = p;
&mut buffer[1..]
};
let copy = |src: &[MazePart], dst: &mut [MazePart]| {
src.iter()
.take_while(|p| **p != MazePart::Noop)
.for_each(|p| {
dst = push(*p, dst);
});
};
buffer = push(MazePart::Wall, buffer); bw.push(MazePart::Wall);
(0..X).into_iter().for_each(|_| { (0..X).into_iter().for_each(|_| {
push(MazePart::Wall, buffer); bw.push(MazePart::Wall);
push(MazePart::Wall, buffer); bw.push(MazePart::Wall);
}); });
let mut top_line = [MazePart::Noop; SIZE]; let mut top_line = [MazePart::Noop; SIZE];
@ -488,34 +488,28 @@ impl<const X: usize, const Y: usize, const SIZE: usize> BinaryMap<X, Y, SIZE> {
for y in 0..Y { for y in 0..Y {
top_line.fill(MazePart::Noop); top_line.fill(MazePart::Noop);
bottom_line.fill(MazePart::Noop); bottom_line.fill(MazePart::Noop);
let tl = &mut top_line; let mut tl = BufWriter::new(&mut top_line);
let bl = &mut bottom_line; let mut bl = BufWriter::new(&mut bottom_line);
push(MazePart::Wall, tl); tl.push(MazePart::Wall);
push(MazePart::Wall, bl); bl.push(MazePart::Wall);
for x in 0..X { for x in 0..X {
push(MazePart::Passage, tl); tl.push(MazePart::Passage);
push( tl.push(if grid.is_carved((x, y), Cell::EAST) {
if grid.is_carved((x, y), Cell::EAST) { MazePart::Passage
MazePart::Passage } else {
} else { MazePart::Wall
MazePart::Wall });
},
tl,
);
push( bl.push(if grid.is_carved((x, y), Cell::SOUTH) {
if grid.is_carved((x, y), Cell::SOUTH) { MazePart::Passage
MazePart::Passage } else {
} else { MazePart::Wall
MazePart::Wall });
},
bl,
);
} }
copy(&top_line, buffer); bw.copy(&top_line);
copy(&bottom_line, buffer); bw.copy(&bottom_line);
} }
} }
} }