Skip to content

Commit

Permalink
Reorganize the MOM sub-module
Browse files Browse the repository at this point in the history
  • Loading branch information
fxpineau committed Sep 26, 2024
1 parent 06e6761 commit 5d51c6b
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 76 deletions.
3 changes: 3 additions & 0 deletions src/nested/map/mom/impls/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
//! Module containing MOM implementations in sub-modules
pub mod zvec;

89 changes: 89 additions & 0 deletions src/nested/map/mom/impls/zvec.rs
Original file line number Diff line number Diff line change
@@ -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<Z, V>
where
Z: ZUniqHashT,
V: SkyMapValue,
{
depth: u8,
entries: Vec<(Z, V)>,
}
impl<'a, Z, V> Mom<'a> for MomVecImpl<Z, V>
where
Z: ZUniqHashT,
V: 'a + SkyMapValue,
{
type ZUniqHType = Z;
type ValueType = V;
type ZuniqIt = Map<Iter<'a, (Z, V)>, fn(&'a (Z, V)) -> Z>;
type EntriesIt = Map<Iter<'a, (Z, V)>, 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))
}
}
78 changes: 2 additions & 76 deletions src/nested/map/mom.rs → src/nested/map/mom/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -174,79 +173,6 @@ pub trait Mom<'a> {

}

/// Implementation of a MOM in a simple vector.
pub struct MomVecImpl<Z, V>
where
Z: ZUniqHashT,
V: SkyMapValue
{
depth: u8,
entries: Vec<(Z, V)>,
}
impl<'a, Z, V> Mom<'a> for MomVecImpl<Z, V>
where
Z: ZUniqHashT,
V: 'a + SkyMapValue
{
type ZUniqHType = Z;
type ValueType = V;
type ZuniqIt = Map<Iter<'a, (Z, V)>, fn(&'a (Z, V)) -> Z>;
type EntriesIt = Map<Iter<'a, (Z, V)>, 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))
}
}


/*
Expand Down

0 comments on commit 5d51c6b

Please sign in to comment.