From 5d51c6b9d3d8ff2f2d45b5447c2250fc2a28f153 Mon Sep 17 00:00:00 2001 From: fxpineau Date: Thu, 26 Sep 2024 10:49:35 +0200 Subject: [PATCH] Reorganize the MOM sub-module --- src/nested/map/mom/impls/mod.rs | 3 + src/nested/map/mom/impls/zvec.rs | 89 +++++++++++++++++++++++++++ src/nested/map/{mom.rs => mom/mod.rs} | 78 +---------------------- 3 files changed, 94 insertions(+), 76 deletions(-) create mode 100644 src/nested/map/mom/impls/mod.rs create mode 100644 src/nested/map/mom/impls/zvec.rs rename src/nested/map/{mom.rs => mom/mod.rs} (76%) diff --git a/src/nested/map/mom/impls/mod.rs b/src/nested/map/mom/impls/mod.rs new file mode 100644 index 0000000..b1cf573 --- /dev/null +++ b/src/nested/map/mom/impls/mod.rs @@ -0,0 +1,3 @@ +//! Module containing MOM implementations in sub-modules +pub mod zvec; + diff --git a/src/nested/map/mom/impls/zvec.rs b/src/nested/map/mom/impls/zvec.rs new file mode 100644 index 0000000..41cc4d6 --- /dev/null +++ b/src/nested/map/mom/impls/zvec.rs @@ -0,0 +1,89 @@ +use std::{iter::Map, slice::Iter}; + +use super::super::{super::skymap::SkyMapValue, Mom, ZUniqHashT}; + +/// Implementation of a MOM in an ordered vector of `(zuniq, values)` tuples. +pub struct MomVecImpl +where + Z: ZUniqHashT, + V: SkyMapValue, +{ + depth: u8, + entries: Vec<(Z, V)>, +} +impl<'a, Z, V> Mom<'a> for MomVecImpl +where + Z: ZUniqHashT, + V: 'a + SkyMapValue, +{ + type ZUniqHType = Z; + type ValueType = V; + type ZuniqIt = Map, fn(&'a (Z, V)) -> Z>; + type EntriesIt = Map, fn(&'a (Z, V)) -> (Z, &'a V)>; + + fn depth_max(&self) -> u8 { + self.depth + } + + fn get_cell_containing_unsafe( + &'a self, + hash_at_depth_max: Self::ZUniqHType, + ) -> Option<(Self::ZUniqHType, &'a Self::ValueType)> { + match self + .entries + .binary_search_by(|&(z, _)| z.cmp(&hash_at_depth_max)) + { + Ok(i) => { + let e = &self.entries[i]; + Some((e.0, &e.1)) + } + Err(i) => { + if i > 0 { + // if array len is 0, i will be 0 so we do not enter here. + let e = &self.entries[i - 1]; + if Z::are_overlapping(hash_at_depth_max, e.0) { + return Some((e.0, &e.1)); + } + } + if i < self.entries.len() { + let e = &self.entries[i]; + if Z::are_overlapping(hash_at_depth_max, e.0) { + return Some((e.0, &e.1)); + } + } + None + } + } + } + + fn get_overlapped_cells( + &'a self, + zuniq: Self::ZUniqHType, + ) -> Vec<(Self::ZUniqHType, &'a Self::ValueType)> { + let mut range = match self.entries.binary_search_by(|&(z, _)| z.cmp(&zuniq)) { + Ok(i) => i..i + 1, + Err(i) => i..i, + }; + while range.start - 1 > 0 && Z::are_overlapping(zuniq, self.entries[range.start - 1].0) { + range.start -= 1; + } + while range.end < self.entries.len() && Z::are_overlapping(zuniq, self.entries[range.end].0) { + range.end += 1; + } + range + .into_iter() + .map(|i| { + let (z, v) = &self.entries[i]; + (*z, v) + }) + .collect() + } + + fn zuniqs(&'a self) -> Self::ZuniqIt { + self.entries.iter().map(|&(zuniq, _)| zuniq) + } + + fn entries(&'a self) -> Self::EntriesIt { + self.entries.iter().map(|(z, v)| (*z, v)) + } +} diff --git a/src/nested/map/mom.rs b/src/nested/map/mom/mod.rs similarity index 76% rename from src/nested/map/mom.rs rename to src/nested/map/mom/mod.rs index a43bc67..f4556fa 100644 --- a/src/nested/map/mom.rs +++ b/src/nested/map/mom/mod.rs @@ -8,14 +8,13 @@ use std::{ cmp::Ordering, fmt::{Debug, Display}, mem, - iter::Map, - slice::Iter, }; use num::PrimInt; -use crate::nested::map::skymap::SkyMapValue; +use super::skymap::SkyMapValue; +pub mod impls; /// `ZUniqHHashT` stands for `Z-curve ordered Uniq Healpix Hash Type`. pub trait ZUniqHashT: @@ -174,79 +173,6 @@ pub trait Mom<'a> { } -/// Implementation of a MOM in a simple vector. -pub struct MomVecImpl - where - Z: ZUniqHashT, - V: SkyMapValue -{ - depth: u8, - entries: Vec<(Z, V)>, -} -impl<'a, Z, V> Mom<'a> for MomVecImpl - where - Z: ZUniqHashT, - V: 'a + SkyMapValue -{ - type ZUniqHType = Z; - type ValueType = V; - type ZuniqIt = Map, fn(&'a (Z, V)) -> Z>; - type EntriesIt = Map, fn(&'a (Z, V)) -> (Z, &'a V)>; - - fn depth_max(&self) -> u8 { - self.depth - } - - fn get_cell_containing_unsafe(&'a self, hash_at_depth_max: Self::ZUniqHType) -> Option<(Self::ZUniqHType, &'a Self::ValueType)> { - match self.entries.binary_search_by(|&(z, _)| z.cmp(&hash_at_depth_max)) { - Ok(i) => { - let e = &self.entries[i]; - Some((e.0, &e.1)) - }, - Err(i) => { - if i > 0 { - // if array len is 0, i will be 0 so we do not enter here. - let e = &self.entries[i - 1]; - if Z::are_overlapping(hash_at_depth_max, e.0) { - return Some((e.0, &e.1)); - } - } - if i < self.entries.len() { - let e = &self.entries[i]; - if Z::are_overlapping(hash_at_depth_max, e.0) { - return Some((e.0, &e.1)); - } - } - None - } - } - } - - fn get_overlapped_cells(&'a self, zuniq: Self::ZUniqHType) -> Vec<(Self::ZUniqHType, &'a Self::ValueType)> { - let mut range = match self.entries.binary_search_by(|&(z, _)| z.cmp(&zuniq)) { - Ok(i) => i..i + 1, - Err(i) => i..i, - }; - while range.start - 1 > 0 && Z::are_overlapping(zuniq, self.entries[range.start - 1].0) { - range.start -= 1; - } - while range.end < self.entries.len() && Z::are_overlapping(zuniq, self.entries[range.end].0) { - range.end += 1; - } - range.into_iter().map(|i| { - let (z, v) = &self.entries[i]; - (*z, v) - }).collect() - } - - fn zuniqs(&'a self) -> Self::ZuniqIt { - self.entries.iter().map(|&(zuniq, _)| zuniq) - } - - fn entries(&'a self) -> Self::EntriesIt { - self.entries.iter().map(|(z, v)| (*z, v)) - } -} /*