feat: implement tape peeking
This commit is contained in:
1 parent
31a5c96fbf
commit
7fc4d12eef
1 file changed
+39
+39
@@ -63,6 +63,45 @@ impl Tape {
|
||||
TapeSide::Right => self.right_half[self.head.index] = symbol,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn peek(&self, radius: usize) -> Vec<&TapeSymbol> {
|
||||
let mut fields = Vec::new();
|
||||
match self.head.side {
|
||||
TapeSide::Left => {
|
||||
let left_lower_bound = self.head.index.saturating_sub(radius);
|
||||
let left_upper_bound = self.head.index + radius;
|
||||
let left_overshoot = left_upper_bound.saturating_sub(self.left_half.len() - 1);
|
||||
let right_upper_bound = radius.saturating_sub(self.head.index);
|
||||
let right_overshoot = right_upper_bound.saturating_sub(self.right_half.len() - 1);
|
||||
|
||||
fields.resize(left_overshoot, &TapeSymbol::Zero);
|
||||
let left_side = self.left_half[left_lower_bound..=self.head.index + radius]
|
||||
.iter()
|
||||
.rev();
|
||||
fields.extend(left_side);
|
||||
|
||||
let right_side = self.right_half[..=right_upper_bound].iter();
|
||||
fields.extend(right_side);
|
||||
fields.resize(fields.len() + right_overshoot, &TapeSymbol::Zero);
|
||||
}
|
||||
TapeSide::Right => {
|
||||
let right_lower_bound = self.head.index.saturating_sub(radius);
|
||||
let right_upper_bound = self.head.index + radius;
|
||||
let right_overshoot = right_upper_bound.saturating_sub(self.right_half.len() - 1);
|
||||
let left_upper_bound = radius.saturating_sub(self.head.index);
|
||||
let left_overshoot = left_upper_bound.saturating_sub(self.left_half.len() - 1);
|
||||
|
||||
fields.resize(left_overshoot, &TapeSymbol::Zero);
|
||||
let left_side = self.left_half[..=left_upper_bound].iter().rev();
|
||||
fields.extend(left_side);
|
||||
|
||||
let right_side = self.right_half[right_lower_bound..=self.head.index + radius].iter();
|
||||
fields.extend(right_side);
|
||||
fields.resize(fields.len() + right_overshoot, &TapeSymbol::Zero);
|
||||
}
|
||||
}
|
||||
fields
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Eq, Hash, PartialEq, Clone, Copy)]
|
||||
|
||||
Reference in new issue
Block a user