refactor: store either map or attrs in perf calc

This commit is contained in:
MaxOhn
2023-12-29 10:28:27 +01:00
parent 1213525246
commit 0adec507c1
7 changed files with 285 additions and 190 deletions
+66
View File
@@ -0,0 +1,66 @@
use crate::Beatmap;
#[derive(Clone, Debug)]
pub(crate) enum MapOrElse<M, E> {
Map(M),
Else(E),
}
impl<M, E> MapOrElse<M, E>
where
M: AsRef<Beatmap>,
{
// /// Take out the `Else` value of `self` and return it.
// ///
// /// If `self` is of variant `Else`, `E::default()` will be left in place.
// pub(crate) fn take(&mut self) -> MapOrElse<MapRef<'_>, E>
// where
// E: Default,
// {
// match self {
// Self::Map(ref map) => MapOrElse::from(map.deref()),
// Self::Else(ref mut other) => MapOrElse::Else(mem::take(other)),
// }
// }
/// Return a mutable reference to `Else`.
///
/// If `self` is of variant `Map`, store `other` in `self`, and return a
/// mutable reference to it.
pub(crate) fn else_or_insert(&mut self, other: E) -> &mut E {
match self {
MapOrElse::Map(_) => {
*self = Self::Else(other);
let Self::Else(ref mut other) = self else {
unreachable!()
};
other
}
MapOrElse::Else(ref mut other) => other,
}
}
}
impl<'map, E> From<&'map Beatmap> for MapOrElse<MapRef<'map>, E> {
fn from(map: &'map Beatmap) -> Self {
Self::Map(MapRef(map))
}
}
/// References don't implement [`Deref`] so we implement a wrapper type.
#[derive(Copy, Clone, Debug)]
pub(crate) struct MapRef<'map>(&'map Beatmap);
impl<'map> MapRef<'map> {
pub(crate) fn into_inner(self) -> &'map Beatmap {
self.0
}
}
impl AsRef<Beatmap> for MapRef<'_> {
fn as_ref(&self) -> &Beatmap {
self.0
}
}
+7 -2
View File
@@ -2,9 +2,14 @@ mod byte_hasher;
mod compact_vec;
mod float_ext;
mod limited_queue;
mod map_or_else;
mod tandem_sort;
pub(crate) use self::{
byte_hasher::ByteHasher, compact_vec::CompactVec, float_ext::FloatExt,
limited_queue::LimitedQueue, tandem_sort::TandemSorter,
byte_hasher::ByteHasher,
compact_vec::CompactVec,
float_ext::FloatExt,
limited_queue::LimitedQueue,
map_or_else::{MapOrElse, MapRef},
tandem_sort::TandemSorter,
};