Skip to content

Commit

Permalink
Use a lookup table for orientation arithmetic.
Browse files Browse the repository at this point in the history
  • Loading branch information
lgarron committed Aug 22, 2023
1 parent 4364d9a commit ac259e3
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 9 deletions.
20 changes: 19 additions & 1 deletion examples/cpp_port/packed/packed_kpuzzle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ pub struct PackedKPuzzleOrbitData {
pub num_pieces: usize,
pub num_orientations: u8,
pub unknown_orientation_value: u8,
pub table: [u8; 16],
}

#[derive(Debug, Clone)]
Expand All @@ -22,6 +23,8 @@ pub struct PackedKPuzzleData {
pub orbit_iteration_info: Vec<PackedKPuzzleOrbitData>,
}

const X: u8 = 255;

#[derive(Debug, Clone)]
pub struct PackedKPuzzle {
// pub data: Arc<PackedKPuzzleData>, // TODO
Expand All @@ -48,13 +51,28 @@ impl TryFrom<KPuzzle> for PackedKPuzzle {
),
})?;
let unknown_orientation_value = usize_to_u8(2 * orbit_definition.num_orientations);
let num_orientations = usize_to_u8(orbit_definition.num_orientations);
let table: [u8; 16] = match num_orientations {
1 => [0, 0, X, X, X, X, X, X, X, X, X, X, X, X, X, X],
2 => [0, 1, 0, X, 4, 4, X, X, X, X, X, X, X, X, X, X],
3 => [0, 1, 2, 0, 1, X, 6, 6, 6, X, X, X, X, X, X, X],
4 => [0, 1, 2, 3, 0, 1, 2, X, 8, 8, 8, 8, X, X, X, X],
5 => [0, 1, 2, 3, 4, 0, 1, 2, 3, X, 10, 10, 10, 10, 10, X],
_ => {
return Err(InvalidDefinitionError {
description: "`num_orientations` higher than currently supported"
.to_owned(),
})
}
};
orbit_iteration_info.push({
PackedKPuzzleOrbitData {
name: orbit_name.clone(),
num_pieces: orbit_definition.num_pieces,
num_orientations: usize_to_u8(orbit_definition.num_orientations),
num_orientations,
bytes_offset,
unknown_orientation_value,
table,
}
});
bytes_offset += orbit_definition.num_pieces * 2;
Expand Down
12 changes: 4 additions & 8 deletions examples/cpp_port/packed/packed_kstate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,10 @@ impl PackedKState {
get_packed_piece_or_permutation!(self.bytes, orbit_info, transformation_idx);
let previous_piece_orientation =
get_packed_orientation!(self.bytes, orbit_info, transformation_idx);
let new_piece_orientation =
if previous_piece_orientation == orbit_info.unknown_orientation_value {
previous_piece_orientation
} else {
(previous_piece_orientation
+ get_packed_orientation!(transformation.bytes, orbit_info, i))
% orbit_info.num_orientations
};
let new_piece_orientation = orbit_info.table[std::convert::Into::<usize>::into(
previous_piece_orientation
+ get_packed_orientation!(transformation.bytes, orbit_info, i),
)];
set_packed_piece_or_permutation_and_orientation!(
bytes,
orbit_info,
Expand Down

0 comments on commit ac259e3

Please sign in to comment.