Skip to content

Commit

Permalink
Add 'ImplicitSkyMapArrayRef' and start implementing mom merge method.
Browse files Browse the repository at this point in the history
  • Loading branch information
fxpineau committed Oct 11, 2024
1 parent f14d7a8 commit c4f57bb
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/nested/map/fits/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub fn write_implicit_skymap_fits<R: Write, T: SkyMapValue>(
// since we already know it.
// In the end, using monomorphisation, the binary code is duplicated so...
let n_cells = values.len() as u64;
let depth = depth_from_n_hash_unsafe(values.len());
let depth = depth_from_n_hash_unsafe(values.len() as u64);
if n_cells != n_hash(depth) {
return Err(FitsError::new_custom(format!(
"Number of cell {} not compatible with an HEALPix depth of {}. Expected: {}.",
Expand Down
46 changes: 46 additions & 0 deletions src/nested/map/mom/impls/zvec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,52 @@ where
}
Self { depth, entries }
}

fn merge<'s, L, R, S, O, M>(lhs: L, rhs: R, split: S, op: O, merge: M) -> Self
where
L: Mom<'s, ZUniqHType = Z, ValueType = V>,
R: Mom<'s, ZUniqHType = Z, ValueType = V>,
S: Fn(
Self::ValueType,
) -> (
Self::ValueType,
Self::ValueType,
Self::ValueType,
Self::ValueType,
),
O: Fn(Option<Self::ValueType>, Option<Self::ValueType>) -> Option<Self::ValuesIt>,
M: Fn(
Self::ValueType,
Self::ValueType,
Self::ValueType,
Self::ValueType,
) -> Result<
Self::ValueType,
(
Self::ValueType,
Self::ValueType,
Self::ValueType,
Self::ValueType,
),
>,
{
struct DHZV<ZZ, VV> {
d: u8,
h: ZZ,
z: ZZ,
v: VV,
}
let zv_to_dhzv = |(z, v)| {
let (d, h) = Self::ZUniqHType::from_zuniq(z);
DHZV { d, h, z, v }
};
let mut stack: Vec<(Z, V)> = Vec::with_capacity(lhs.len() + rhs.len());
let mut it_left = lhs.owned_entries();
let mut it_right = rhs.owned_entries();
let mut left = it_left.next().map(zv_to_dhzv);
let mut right = it_right.next().map(zv_to_dhzv);
todo!()
}
}

impl<Z, V> MomVecImpl<Z, V>
Expand Down
17 changes: 9 additions & 8 deletions src/nested/map/mom/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,18 +231,19 @@ pub trait Mom<'a>: Sized {
M: Fn(Self::ValueType, Self::ValueType, Self::ValueType, Self::ValueType) -> Result<Self::ValueType, (Self::ValueType, Self::ValueType, Self::ValueType, Self::ValueType)>,
Self::ValueType: 's;

fn merge<L, R, S, O, M>(lhs: L, rhs: R, split: S, op: O, merge: M) -> Self
fn merge<'s, L, R, S, O, M>(lhs: L, rhs: R, split: S, op: O, merge: M) -> Self
where
L: Mom<'s, ZUniqHType = Self::ZUniqHType, ValueType = Self::ValueType>,
R: Mom<'s, ZUniqHType = Self::ZUniqHType, ValueType = Self::ValueType>,
// Split a parent cell into for siblings
S: Fn(Self::ValueType) -> (Self::ValueType, Self::ValueType, Self::ValueType, Self::ValueType),
// Merge the values of a same cell from both MOMs
O: Fn(Self::ValueType, Self::ValueType) -> Self::ValuesIt,
// Performs a post-merge operation?
M: Fn(Self::ValueType, Self::ValueType, Self::ValueType, Self::ValueType) -> Self::ValueType,
{
// get both owned iterators
todo!()
}
// or decide what to do is one of the MOM has a value and the other does not
// (the (None, None) must never be called).
// This allow for various types of JOIN (inner, left, right, full).
O: Fn(Option<Self::ValueType>, Option<Self::ValueType>) -> Option<Self::ValuesIt>,
// Possibly performs a post-merge operation.
M: Fn(Self::ValueType, Self::ValueType, Self::ValueType, Self::ValueType) -> Result<Self::ValueType, (Self::ValueType, Self::ValueType, Self::ValueType, Self::ValueType)>;


}
Expand Down
75 changes: 71 additions & 4 deletions src/nested/map/skymap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,14 +172,14 @@ impl<'a, H: HHash, V: SkyMapValue + 'a> SkyMap<'a> for ImplicitSkyMapArray<H, V>
self.depth
}

fn len(&self) -> usize {
self.values.len()
}

fn is_implicit(&self) -> bool {
true
}

fn len(&self) -> usize {
self.values.len()
}

fn get(&self, hash: Self::HashType) -> &Self::ValueType {
&self.values.deref()[hash.as_()]
}
Expand Down Expand Up @@ -207,6 +207,73 @@ impl<'a, H: HHash, V: SkyMapValue + 'a> SkyMap<'a> for ImplicitSkyMapArray<H, V>
}
}

pub struct ImplicitSkyMapArrayRef<'a, H: HHash, V: SkyMapValue> {
depth: u8,
values: &'a [V],
_htype: PhantomData<H>,
}
impl<'a, H: HHash, V: SkyMapValue + 'a> ImplicitSkyMapArrayRef<'a, H, V> {
/// WARNING: we assume that the coherency between the depth and the number of elements in the
///array has already been tested.
pub fn new(depth: u8, values: &'a [V]) -> Self {
assert_eq!(
n_hash(depth) as usize,
values.deref().len(),
"Wrong implicit skymap size. Epecgted: {}. Actual: {}.",
n_hash(depth),
values.len()
);
Self {
depth,
values,
_htype: PhantomData,
}
}
}
impl<'a, H: HHash, V: SkyMapValue + Clone + 'a> SkyMap<'a> for ImplicitSkyMapArrayRef<'a, H, V> {
type HashType = H;
type ValueType = V;
type ValuesIt = Iter<'a, Self::ValueType>;
type EntriesIt = Map<Enumerate<Self::ValuesIt>, fn((usize, &V)) -> (H, &V)>;
type OwnedEntriesIt = Map<Enumerate<Self::ValuesIt>, fn((usize, &V)) -> (H, V)>;

fn depth(&self) -> u8 {
self.depth
}

fn is_implicit(&self) -> bool {
true
}

fn len(&self) -> usize {
self.values.len()
}

fn get(&self, hash: Self::HashType) -> &Self::ValueType {
&self.values.deref()[hash.as_()]
}

fn values(&'a self) -> Self::ValuesIt {
self.values.deref().iter()
}

fn entries(&'a self) -> Self::EntriesIt {
self
.values
.iter()
.enumerate()
.map(move |(h, v)| (H::from_usize(h), v))
}

fn owned_entries(self) -> Self::OwnedEntriesIt {
self
.values
.iter()
.enumerate()
.map(move |(h, v)| (H::from_usize(h), v.clone()))
}
}

pub enum SkyMapEnum {
ImplicitU64U8(ImplicitSkyMapArray<u64, u8>),
ImplicitU64I16(ImplicitSkyMapArray<u64, i16>),
Expand Down

0 comments on commit c4f57bb

Please sign in to comment.