apply proper sorting for catch objects

This commit is contained in:
MaxOhn
2024-02-16 01:18:37 +01:00
parent 100d01483c
commit 05b588a2d1
4 changed files with 67 additions and 34 deletions
+11 -2
View File
@@ -8,7 +8,7 @@ use crate::{
hit_object::{HitObject, HitObjectKind, HoldNote, Spinner},
mode::ConvertStatus,
},
util::{float_ext::FloatExt, random::Random, sort},
util::{float_ext::FloatExt, random::Random, sort::TandemSorter},
};
use super::{
@@ -75,12 +75,21 @@ pub fn convert_objects(
palpable_objects.extend(new_objects);
}
sort::csharp(&mut palpable_objects, |a, b| {
// Initializing hyper dashes requires objects to be sorted by C#'s unstable
// sort. After that, we unsort the objects again and then apply a stable
// sort to have the correct order for generating difficulty objects.
// Required e.g. due to map /b/102923.
let mut sorter = TandemSorter::new_unstable(&mut palpable_objects, |a, b| {
a.start_time.total_cmp(&b.start_time)
});
sorter.sort(&mut palpable_objects);
initialize_hyper_dash(cs, &mut palpable_objects);
sorter.unsort(&mut palpable_objects);
palpable_objects.sort_by(|a, b| a.start_time.total_cmp(&b.start_time));
palpable_objects
}
+3 -5
View File
@@ -282,11 +282,9 @@ impl From<BeatmapState> for Beatmap {
slider_tick_rate,
} = state.difficulty.into();
let mut sorter = sort::TandemSorter::new(
&state.hit_objects,
|a, b| a.start_time.total_cmp(&b.start_time),
true,
);
let mut sorter = sort::TandemSorter::new_stable(&state.hit_objects, |a, b| {
a.start_time.total_cmp(&b.start_time)
});
sorter.sort(&mut state.hit_objects);
sorter.sort(&mut state.hit_sounds);
+3 -5
View File
@@ -103,11 +103,9 @@ fn convert(map: &mut Beatmap) {
// We only convert osu! to taiko so we don't need to remove objects
// with the same timestamp that would appear only in mania
let mut sorter = TandemSorter::new(
&map.hit_objects,
|a, b| a.start_time.total_cmp(&b.start_time),
true,
);
let mut sorter = TandemSorter::new_stable(&map.hit_objects, |a, b| {
a.start_time.total_cmp(&b.start_time)
});
sorter.sort(&mut map.hit_objects);
sorter.sort(&mut map.hit_sounds);
+50 -22
View File
@@ -7,27 +7,26 @@ pub struct TandemSorter {
should_reset: bool,
}
macro_rules! new_fn {
( $fn:ident: $sort:expr ) => {
/// Sort indices based on the given slice.
///
/// Note that this does **not** sort the given slice.
pub fn $fn<T>(slice: &[T], cmp: fn(&T, &T) -> Ordering) -> Self {
let mut indices: Box<[usize]> = (0..slice.len()).collect();
$sort(&mut indices, |&i, &j| cmp(&slice[i], &slice[j]));
Self {
indices,
should_reset: false,
}
}
};
}
impl TandemSorter {
/// Sort indices based on the given slice.
///
/// Note that this does **not** sort the given slice.
pub fn new<T>(slice: &[T], cmp: fn(&T, &T) -> Ordering, stable: bool) -> Self {
let mut indices: Box<[usize]> = (0..slice.len()).collect();
let sort_by = |&i: &usize, &j: &usize| cmp(&slice[i], &slice[j]);
if stable {
indices.sort_by(sort_by);
} else {
// When sorting integers, the order of elements with equal values
// does not matter so we can use rust's sort instead of C#'s.
indices.sort_unstable_by(sort_by);
}
Self {
indices,
should_reset: false,
}
}
new_fn!(new_stable: <[_]>::sort_by);
new_fn!(new_unstable: super::csharp);
/// Sort the given slice based on the internal ordering.
pub fn sort<T>(&mut self, slice: &mut [T]) {
@@ -60,6 +59,35 @@ impl TandemSorter {
self.should_reset = true;
}
/// Unsort the given slice based on the internal ordering.
pub fn unsort<T>(mut self, slice: &mut [T]) {
if self.should_reset {
self.toggle_marks();
self.should_reset = false;
}
for i in 0..self.indices.len() {
let i_idx = self.indices[i];
if Self::idx_is_marked(i_idx) {
continue;
}
let mut j = i;
let mut j_idx = i_idx;
while j != j_idx {
self.indices[j] = Self::toggle_mark_idx(j_idx);
self.indices.swap(j, j_idx);
slice.swap(j, j_idx);
j = self.indices[j];
j_idx = self.indices[j];
}
self.indices[j] = Self::toggle_mark_idx(j_idx);
}
}
fn toggle_marks(&mut self) {
for idx in self.indices.iter_mut() {
*idx = Self::toggle_mark_idx(*idx);
@@ -85,12 +113,12 @@ mod tests {
fn sort() {
let mut base = vec![9, 7, 8, 1, 4, 3, 5, 2];
let mut other = "hello World".chars().collect::<Vec<_>>();
let mut sorter = TandemSorter::new(&base, u8::cmp, false);
let mut sorter = TandemSorter::new_stable(&base, u8::cmp);
sorter.sort(&mut base);
assert_eq!(base, vec![1, 2, 3, 4, 5, 7, 8, 9]);
sorter.sort(&mut other);
assert_eq!(other, "lo oWelhrld".chars().collect::<Vec<_>>());
assert_eq!(other.iter().collect::<String>(), "lo oWelhrld");
}
}