Skip to content

Commit

Permalink
Remove orientation_packer and no_orientation_mod feature flags.
Browse files Browse the repository at this point in the history
Closes #32
  • Loading branch information
lgarron committed Aug 25, 2023
1 parent fc40a2a commit dcd2cf2
Show file tree
Hide file tree
Showing 9 changed files with 205 additions and 190 deletions.
8 changes: 2 additions & 6 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,5 @@ jobs:
- run: make build-rust
- run: make lint-rust
- run: make test-twsearch-cpp-wrapper-cli
- run: make test-rs-default
- run: make test-rs-no-default-features
- run: make test-rs-no_orientation_mod
- run: make benchmark-rs-default
- run: make benchmark-rs-no-default-features
- run: make benchmark-rs-no_orientation_mod
- run: make test-rs
- run: make benchmark-rs
40 changes: 6 additions & 34 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ test: \
lint \
test-cpp-cli \
test-twsearch-cpp-wrapper-cli \
test-rs-all \
benchmark-rs-all
test-rs \
benchmark-rs

.PHONY: test-warning
test-warning:
Expand All @@ -31,44 +31,16 @@ test-twsearch-cpp-wrapper-cli:

# Rust testing

.PHONY: test-rs-all
test-rs-all: \
test-rs-default \
test-rs-no-default-features \
test-rs-no_orientation_mod

.PHONY: test-rs-default
test-rs-default:
.PHONY: test-rs
test-rs:
cargo test --quiet

.PHONY: test-rs-no-default-features
test-rs-no-default-features:
cargo test --quiet --no-default-features

.PHONY: test-rs-no_orientation_mod
test-rs-no_orientation_mod:
cargo test --quiet --no-default-features --features no_orientation_mod

BENCHMARK_RS = cargo run --quiet --release --example benchmark

.PHONY: benchmark-rs-all
benchmark-rs-all: \
benchmark-rs-default \
benchmark-rs-no-default-features \
benchmark-rs-no_orientation_mod

.PHONY: benchmark-rs-default
benchmark-rs-default:
.PHONY: benchmark-rs
benchmark-rs:
${BENCHMARK_RS}

.PHONY: benchmark-rs-no-default-features
benchmark-rs-no-default-features:
${BENCHMARK_RS} --no-default-features

.PHONY: benchmark-rs-no_orientation_mod
benchmark-rs-no_orientation_mod:
${BENCHMARK_RS} --no-default-features --features no_orientation_mod

.PHONY: clean
clean:
rm -rf ./.temp ./build ./src/js/generated-wasm/twsearch.* ./*.dwo ./target
Expand Down
5 changes: 0 additions & 5 deletions src/rs/examples/benchmark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,11 @@ use std::time::Instant;

use cubing::{parse_alg, parse_move, puzzles::cube3x3x3_kpuzzle};

#[cfg(not(feature = "no_orientation_mod"))]
use cubing::kpuzzle::{
KPuzzle, KPuzzleDefinition, KPuzzleOrbitDefinition, KPuzzleOrbitName, KState, KStateData,
KStateOrbitData, KTransformationData, KTransformationOrbitData,
};
#[cfg(not(feature = "no_orientation_mod"))]
use std::collections::HashMap;
#[cfg(not(feature = "no_orientation_mod"))]
use std::sync::Arc;

use twsearch::PackedKPuzzle;
Expand All @@ -20,7 +17,6 @@ const PRINT_FINAL_STATE: bool = false;
fn main() {
let num_moves = 10_000_000;
println!("Testing custom puzzle…\n--------");
#[cfg(not(feature = "no_orientation_mod"))]
test_custom_puzzle();
println!("Running timing tests…\n--------");
test_packed(num_moves);
Expand Down Expand Up @@ -196,7 +192,6 @@ fn test_unpacked(num_moves: usize) {
);
}

#[cfg(not(feature = "no_orientation_mod"))]
fn test_custom_puzzle() {
let def = KPuzzleDefinition {
name: "custom".to_owned(),
Expand Down
1 change: 0 additions & 1 deletion src/rs/packed/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,4 @@ mod packed_kstate;
pub use packed_kstate::PackedKState;

mod byte_conversions;
#[cfg(feature = "orientation_packer")]
mod orientation_packer;
2 changes: 0 additions & 2 deletions src/rs/packed/orientation_packer.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#![cfg(feature = "orientation_packer")]

use super::byte_conversions::{u8_to_usize, usize_to_u8, PackedOrientationWithMod};

const NUM_BYTE_VALUES: usize = 0x100;
Expand Down
77 changes: 4 additions & 73 deletions src/rs/packed/packed_kpuzzle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,35 +7,10 @@ use cubing::{
},
};

use super::{byte_conversions::usize_to_u8, PackedKState, PackedKTransformation};

#[cfg(not(feature = "orientation_packer"))]
#[cfg(not(feature = "no_orientation_mod"))]
pub const ORIENTATION_MOD_SHIFT_BITS: usize = 4;
#[cfg(not(feature = "orientation_packer"))]
#[cfg(not(feature = "no_orientation_mod"))]
pub const ORIENTATION_MASK: u8 = 0xF;

#[cfg(feature = "orientation_packer")]
#[cfg(not(feature = "no_orientation_mod"))]
use super::byte_conversions::u8_to_usize;
#[cfg(feature = "orientation_packer")]
#[cfg(not(feature = "no_orientation_mod"))]
use super::orientation_packer::OrientationPacker;
#[cfg(feature = "orientation_packer")]
#[cfg(not(feature = "no_orientation_mod"))]
use crate::packed::orientation_packer::OrientationWithMod;

// https://github.com/cubing/twsearch/issues/25#issue-1862613355
#[cfg(not(feature = "orientation_packer"))]
#[cfg(not(feature = "no_orientation_mod"))]
const MAX_NUM_ORIENTATIONS_INCLUSIVE: usize = 16;
#[cfg(feature = "orientation_packer")]
#[cfg(not(feature = "no_orientation_mod"))]
use super::{byte_conversions::{usize_to_u8, u8_to_usize}, PackedKState, PackedKTransformation, orientation_packer::{OrientationPacker, OrientationWithMod}};

// TODO: allow certain values over 107?
const MAX_NUM_ORIENTATIONS_INCLUSIVE: usize = 107;
#[cfg(feature = "no_orientation_mod")]
const MAX_NUM_ORIENTATIONS_INCLUSIVE: usize = 127;

#[derive(Debug)]
pub struct PackedKPuzzleOrbitInfo {
Expand All @@ -44,11 +19,7 @@ pub struct PackedKPuzzleOrbitInfo {
pub orientations_offset: usize,
pub num_pieces: usize,
pub num_orientations: u8,
#[cfg(feature = "orientation_packer")]
#[cfg(not(feature = "no_orientation_mod"))]
pub orientation_packer: OrientationPacker,
#[cfg(feature = "no_orientation_mod")]
pub unknown_orientation_value: u8,
}

#[derive(Debug)]
Expand Down Expand Up @@ -96,11 +67,7 @@ impl TryFrom<KPuzzle> for PackedKPuzzle {
num_orientations: usize_to_u8(num_orientations),
pieces_or_pemutations_offset: bytes_offset,
orientations_offset: bytes_offset + orbit_definition.num_pieces,
#[cfg(feature = "orientation_packer")]
#[cfg(not(feature = "no_orientation_mod"))]
orientation_packer: OrientationPacker::new(orbit_definition.num_orientations),
#[cfg(feature = "no_orientation_mod")]
unknown_orientation_value: usize_to_u8(2 * num_orientations),
}
});
bytes_offset += orbit_definition.num_pieces * 2;
Expand Down Expand Up @@ -147,26 +114,7 @@ impl PackedKPuzzle {
match &kstate_orbit_data.orientation_mod {
None => usize_to_u8(kstate_orbit_data.orientation[i]),
Some(orientation_mod) => {
#[cfg(not(feature = "orientation_packer"))]
#[cfg(not(feature = "no_orientation_mod"))]
{
if orientation_mod[i] != 0 && std::convert::Into::<usize>::into(orbit_info.num_orientations) % orientation_mod[i] != 0 {
eprintln!(
"`orientation_mod` of {} seen for piece at index {} in orbit {} in the start state for puzzle {}. This must be a factor of `num_orientations` for the orbit ({}). See: https://js.cubing.net/cubing/api/interfaces/kpuzzle.KStateOrbitData.html#orientationMod",
orientation_mod[i],
i,
orbit_info.name,
self.data.kpuzzle.definition().name,
orbit_info.num_orientations
);
panic!("Invalid start state");
};
(usize_to_u8(orientation_mod[i]) << ORIENTATION_MOD_SHIFT_BITS)
+ usize_to_u8(kstate_orbit_data.orientation[i])
}
#[cfg(feature = "orientation_packer")]
#[cfg(not(feature = "no_orientation_mod"))]
{

if orientation_mod[i] != 0 && u8_to_usize(orbit_info.num_orientations) % orientation_mod[i] != 0 {
eprintln!(
"`orientation_mod` of {} seen for piece at index {} in orbit {} in the start state for puzzle {}. This must be a factor of `num_orientations` for the orbit ({}). See: https://js.cubing.net/cubing/api/interfaces/kpuzzle.KStateOrbitData.html#orientationMod",
Expand All @@ -182,24 +130,7 @@ impl PackedKPuzzle {
orientation: kstate_orbit_data.orientation[i],
orientation_mod: orientation_mod[i],
})
}
#[cfg(feature = "no_orientation_mod")]
{
match orientation_mod[i] {
0 => usize_to_u8(kstate_orbit_data.orientation[i]),
1 => orbit_info.unknown_orientation_value,
_ =>{
eprintln!(
"`orientation_mod` of {} seen for piece at index {} in orbit {} in the start state for puzzle {}. Values other than 0 or 1 are not supported for the `no_orientation_mod` feature flag.",
orientation_mod[i],
i,
orbit_info.name,
self.data.kpuzzle.definition().name
);
panic!("Invalid start state");
},
}
}

}
},
);
Expand Down
70 changes: 6 additions & 64 deletions src/rs/packed/packed_kstate.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::{
alloc::{alloc, dealloc},
fmt::Debug,
sync::Arc,
};

use super::{
Expand All @@ -10,14 +11,7 @@ use super::{
};

use cubing::kpuzzle::KPuzzle;
#[cfg(not(feature = "no_orientation_mod"))]
use cubing::kpuzzle::{KState, KStateData};
#[cfg(not(feature = "no_orientation_mod"))]
use std::sync::Arc;

#[cfg(not(feature = "orientation_packer"))]
#[cfg(not(feature = "no_orientation_mod"))]
use super::packed_kpuzzle::{ORIENTATION_MASK, ORIENTATION_MOD_SHIFT_BITS};

pub struct PackedKState {
pub packed_kpuzzle: PackedKPuzzle,
Expand Down Expand Up @@ -109,38 +103,12 @@ impl PackedKState {
let previous_packed_orientation =
self.get_packed_orientation(orbit_info, u8_to_usize(transformation_idx));

#[cfg(not(feature = "orientation_packer"))]
#[cfg(not(feature = "no_orientation_mod"))]
let new_packed_orientation = {
let previous_orientation_mod =
previous_packed_orientation >> ORIENTATION_MOD_SHIFT_BITS;
let (modulus, previous_orientation) = match previous_orientation_mod {
0 => (orbit_info.num_orientations, previous_packed_orientation),
modulus => (modulus, previous_packed_orientation & ORIENTATION_MASK),
};
let new_orientation = (previous_orientation
+ transformation.get_orientation(orbit_info, i))
% modulus;
(previous_orientation_mod << ORIENTATION_MOD_SHIFT_BITS) + new_orientation
};
#[cfg(feature = "orientation_packer")]
#[cfg(not(feature = "no_orientation_mod"))]
let new_packed_orientation = {
orbit_info.orientation_packer.transform(
previous_packed_orientation,
u8_to_usize(transformation.get_orientation(orbit_info, i)),
)
};
// TODO: implement an orientation packer for the `no_orientation_mod` case?
#[cfg(feature = "no_orientation_mod")]
let new_packed_orientation = if previous_packed_orientation
== orbit_info.unknown_orientation_value
{
orbit_info.unknown_orientation_value
} else {
(previous_packed_orientation + transformation.get_orientation(orbit_info, i))
% orbit_info.num_orientations
};
into_state.set_packed_orientation(orbit_info, i, new_packed_orientation);
}
}
Expand All @@ -156,7 +124,6 @@ impl PackedKState {
cityhash::city_hash_64(self.byte_slice())
}

#[cfg(not(feature = "no_orientation_mod"))]
pub fn unpack(&self) -> KState {
let mut state_data = KStateData::new();
for orbit_info in &self.packed_kpuzzle.data.orbit_iteration_info {
Expand All @@ -165,36 +132,11 @@ impl PackedKState {
let mut orientation_mod = Vec::<usize>::new();
for i in 0..orbit_info.num_pieces {
pieces.push(u8_to_usize(self.get_piece_or_permutation(orbit_info, i)));

#[cfg(not(feature = "orientation_packer"))]
#[cfg(not(feature = "no_orientation_mod"))]
{
let packed_orientation = self.get_packed_orientation(orbit_info, i);
orientation.push(u8_to_usize(packed_orientation & ORIENTATION_MASK));
orientation_mod.push(u8_to_usize(
packed_orientation >> ORIENTATION_MOD_SHIFT_BITS,
));
}
#[cfg(feature = "orientation_packer")]
#[cfg(not(feature = "no_orientation_mod"))]
{
let orientation_with_mod = orbit_info
.orientation_packer
.unpack(self.get_packed_orientation(orbit_info, i));
orientation.push(orientation_with_mod.orientation);
orientation_mod.push(orientation_with_mod.orientation_mod);
};
#[cfg(feature = "no_orientation_mod")]
{
let packed_orientation = self.get_packed_orientation(orbit_info, i);
if packed_orientation == orbit_info.unknown_orientation_value {
orientation.push(0);
orientation_mod.push(1);
} else {
orientation.push(u8_to_usize(packed_orientation));
orientation_mod.push(0);
}
}
let orientation_with_mod = orbit_info
.orientation_packer
.unpack(self.get_packed_orientation(orbit_info, i));
orientation.push(orientation_with_mod.orientation);
orientation_mod.push(orientation_with_mod.orientation_mod);
}
let orbit_data = cubing::kpuzzle::KStateOrbitData {
pieces,
Expand Down
6 changes: 1 addition & 5 deletions src/rs/packed/packed_ktransformation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@ pub struct PackedKTransformation {
pub packed_kpuzzle: PackedKPuzzle,
pub bytes: *mut u8,
}
use cubing::kpuzzle::KPuzzle;

#[cfg(not(feature = "no_orientation_mod"))]
use cubing::kpuzzle::{KTransformation, KTransformationOrbitData};
use cubing::kpuzzle::{KPuzzle, KTransformation, KTransformationOrbitData};

impl Drop for PackedKTransformation {
fn drop(&mut self) {
Expand Down Expand Up @@ -114,7 +111,6 @@ impl PackedKTransformation {
cityhash::city_hash_64(self.byte_slice())
}

#[cfg(not(feature = "no_orientation_mod"))]
pub fn unpack(&self) -> KTransformation {
use std::sync::Arc;

Expand Down
Loading

0 comments on commit dcd2cf2

Please sign in to comment.