Skip to content

Commit

Permalink
checkpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
ripytide committed Jan 24, 2024
1 parent 3003fae commit 1554280
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 143 deletions.
161 changes: 29 additions & 132 deletions src/nodit/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -402,16 +402,16 @@ where

let mut result = Vec::new();

let mut leftmost_cursor = self.inner.lower_bound_mut(
let mut cursor = self.inner.lower_bound_mut(
overlapping_comp(interval.start()),
SearchBoundCustom::Included,
);

while leftmost_cursor
while cursor
.key()
.is_some_and(|inner_interval| overlaps(*inner_interval, interval))
{
result.push(leftmost_cursor.remove_current().unwrap());
result.push(cursor.remove_current().unwrap());
}

return result.into_iter();
Expand Down Expand Up @@ -462,136 +462,33 @@ where
{
invalid_interval_panic(interval);

let left_overlapping = self
.inner
.get_key_value(overlapping_comp(interval.start()))
.map(|(key, _)| key)
.copied();
let right_overlapping = self
.inner
.get_key_value(overlapping_comp(interval.end()))
.map(|(key, _)| key)
.copied();

match (left_overlapping, right_overlapping) {
(Some(left), Some(right)) if left.start() == right.start() => {
Either::Left(self.cut_single_overlapping(interval, left))
}
_ => Either::Right(self.cut_non_single_overlapping(
interval,
left_overlapping,
right_overlapping,
)),
}

//todo re-use when let_chains become stable
//
// if let Some(left) = left_overlapping
// && let Some(right) = right_overlapping
// && left.start() == right.start()
// {
// } else {
// }
}
fn cut_single_overlapping<Q>(
&mut self,
interval: Q,
single_overlapping_interval: K,
) -> impl Iterator<Item = (K, V)>
where
Q: IntervalType<I>,
V: Clone,
{
invalid_interval_panic(interval);

let cut_result = cut_interval(single_overlapping_interval, interval);

let returning_before_cut = cut_result.before_cut.map(K::from);
let returning_after_cut = cut_result.after_cut.map(K::from);
let mut result = Vec::new();

let value = self
.inner
.remove(overlapping_comp(interval.start()))
.unwrap();
let mut cursor = self.inner.lower_bound_mut(
overlapping_comp(interval.start()),
SearchBoundCustom::Included,
);

if let Some(before) = returning_before_cut {
self.insert_unchecked(before, value.clone());
}
if let Some(after) = returning_after_cut {
self.insert_unchecked(after, value.clone());
}
while let Some(key) = cursor.key() {
if !overlaps(*key, interval) {
break;
}

once((cut_result.inside_cut.map(K::from).unwrap(), value))
}
fn cut_non_single_overlapping<'a, Q>(
&'a mut self,
interval: Q,
left_overlapping: Option<K>,
right_overlapping: Option<K>,
) -> impl Iterator<Item = (K, V)>
where
Q: IntervalType<I> + 'a,
V: Clone,
{
invalid_interval_panic(interval);
let (key, value) = cursor.remove_current().unwrap();

let (returning_before_cut, keeping_before) = match left_overlapping {
Some(before) => {
let cut_result = cut_interval(before, interval);
let cut_result = cut_interval(key, interval);

(
cut_result.before_cut.map(K::from),
cut_result.inside_cut.map(K::from),
)
if let Some(before_cut) = cut_result.before_cut {
cursor.insert_before(K::from(before_cut), value.clone());
}
None => (None, None),
};
let (returning_after_cut, keeping_after) = match right_overlapping {
Some(after) => {
let cut_result = cut_interval(after, interval);

(
cut_result.after_cut.map(K::from),
cut_result.inside_cut.map(K::from),
)
if let Some(after_cut) = cut_result.after_cut {
cursor.insert_after(K::from(after_cut), value.clone());
}
None => (None, None),
};

let before_value =
self.inner.remove(overlapping_comp(interval.start()));
let after_value = self.inner.remove(overlapping_comp(interval.end()));

if let Some(returning_before_cut) = returning_before_cut {
self.insert_unchecked(
returning_before_cut,
before_value.as_ref().cloned().unwrap(),
);
result.push((key, value));
}
if let Some(returning_after_cut) = returning_after_cut {
self.insert_unchecked(
returning_after_cut,
after_value.as_ref().cloned().unwrap(),
);
}

let keeping_before_entry = keeping_before
.map(|keeping_before| (keeping_before, before_value.unwrap()));
let keeping_after_entry = keeping_after
.map(|keeping_after| (keeping_after, after_value.unwrap()));

return keeping_before_entry
.into_iter()
.chain(self.remove_overlapping(interval).map(|(key, value)| {
(
K::from(Interval {
start: key.start(),
end: key.end(),
}),
value,
)
}))
.chain(keeping_after_entry);
result.into_iter()
}

/// Returns an iterator of all the gaps in the map that overlap the given
Expand Down Expand Up @@ -674,11 +571,11 @@ where
.filter(|interval| interval.is_valid());

//possibly add the trimmed start and end gaps
return start_gap
start_gap
.map(K::from)
.into_iter()
.chain(inner_gaps)
.chain(end_gap.map(K::from));
.chain(end_gap.map(K::from))
}

/// Returns an iterator of all the gaps in the map that overlap the given
Expand Down Expand Up @@ -774,11 +671,11 @@ where
.filter(|interval| interval.is_valid());

//possibly add the trimmed start and end gaps
return trimmed_start_gap
trimmed_start_gap
.map(K::from)
.into_iter()
.chain(inner_gaps)
.chain(trimmed_end_gap.map(K::from));
.chain(trimmed_end_gap.map(K::from))
}

/// Returns `true` if the map covers every point in the given
Expand Down Expand Up @@ -855,7 +752,7 @@ where

self.insert_unchecked(interval, value);

return Ok(());
Ok(())
}
fn insert_unchecked(&mut self, interval: K, value: V) {
self.inner.insert(interval, value, starts_comp());
Expand Down Expand Up @@ -904,7 +801,7 @@ where

self.insert_unchecked(returning, value);

return returning;
returning
}

/// Adds a new entry to the map and merges into other intervals in
Expand Down Expand Up @@ -1374,7 +1271,7 @@ where
for (interval, value) in iter {
map.insert_strict(interval, value)?;
}
return Ok(map);
Ok(map)
}
}

Expand Down Expand Up @@ -2359,7 +2256,7 @@ mod tests {
}
}

return output;
output
}

fn all_valid_test_bounds() -> Vec<Interval<i8>> {
Expand All @@ -2371,6 +2268,6 @@ mod tests {
}
}
}
return output;
output
}
}
74 changes: 63 additions & 11 deletions src/zosdit/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
//temporary variables before the comp calls
//remove overlapping_mut and replace with overlapping_start_comp and overlapping_end_comp
use alloc::boxed::Box;
use alloc::vec::Vec;
use core::cmp::Ordering;
use core::fmt;
use core::marker::PhantomData;
Expand All @@ -17,7 +18,8 @@ use serde::{Deserialize, Deserializer, Serialize, Serializer};
use smallvec::SmallVec;

use crate::utils::{
exclusive_comp_generator, inclusive_comp_generator, invalid_interval_panic,
cut_interval, exclusive_comp_generator, inclusive_comp_generator,
invalid_interval_panic, overlaps,
};
use crate::{IntervalType, PointType};

Expand Down Expand Up @@ -171,13 +173,16 @@ where
/// assert_eq!(map.get_last_value_at_point(10), None);
/// ```
pub fn get_last_value_at_point(&self, point: I) -> Option<&V> {
self.inner
.lower_bound(
exclusive_comp_generator(point, Ordering::Greater),
SearchBoundCustom::Included,
)
.value()
.and_then(|x| x.last())
let mut cursor = self.inner.lower_bound(
exclusive_comp_generator(point, Ordering::Greater),
SearchBoundCustom::Included,
);

if cursor.key().is_none() {
cursor.move_prev()
}

cursor.value().and_then(|x| x.last())
}

/// Appends the value to the `SmallVec` corresponding to the interval.
Expand Down Expand Up @@ -355,9 +360,33 @@ where
{
invalid_interval_panic(interval);

todo!();
let mut result = Vec::new();

let mut cursor = self.inner.upper_bound_mut(
exclusive_comp_generator(interval.start(), Ordering::Less),
SearchBoundCustom::Included,
);

while let Some(key) = cursor.key() {
if !overlaps(*key, interval) {
break;
}

let (key, value_store) = cursor.remove_current().unwrap();

let cut_result = cut_interval(key, interval);

[].into_iter()
if let Some(before_cut) = cut_result.before_cut {
cursor.insert_before(K::from(before_cut), value_store.clone());
}
if let Some(after_cut) = cut_result.after_cut {
cursor.insert_after(K::from(after_cut), value_store.clone());
}

result.extend(value_store.into_iter().map(|value| (key, value)));
}

result.into_iter()
}

/// The same as [`NoditMap::overlapping()`] except it flattens the `SmallVec`s of values into
Expand Down Expand Up @@ -504,7 +533,7 @@ where
for (interval, value) in iter {
map.insert_strict_back(interval, value)?;
}
return Ok(map);
Ok(map)
}
}

Expand Down Expand Up @@ -666,13 +695,36 @@ mod tests {
assert_eq!(map.len(), 2);
}

#[test]
fn get_last_value_at_point_tests() {
let mut map = ZosditMap::new();

map.insert_strict_back(ii(0_u8, 4), -1_i8).unwrap();
map.insert_strict_back(ii(4_u8, 8), -2_i8).unwrap();
map.insert_strict_back(ii(8_u8, u8::MAX), -3_i8).unwrap();

assert_eq!(map.get_last_value_at_point(0_u8), Some(&-1));
assert_eq!(map.get_last_value_at_point(2_u8), Some(&-1));
assert_eq!(map.get_last_value_at_point(4_u8), Some(&-2));
assert_eq!(map.get_last_value_at_point(6_u8), Some(&-2));
assert_eq!(map.get_last_value_at_point(8_u8), Some(&-3));
assert_eq!(map.get_last_value_at_point(10_u8), Some(&-3));
assert_eq!(map.get_last_value_at_point(u8::MAX), Some(&-3));
}

#[test]
fn cut_tests() {
let mut map = ZosditMap::new();

map.insert_strict_back(ii(0_u8, 0), -8_i8).unwrap();
map.insert_strict_back(ii(0_u8, u8::MAX), -4_i8).unwrap();

let mut cursor = map.inner.upper_bound_mut(
exclusive_comp_generator(0, Ordering::Less),
SearchBoundCustom::Included,
);
dbg!(cursor.key_value());

assert_eq!(
map.iter().collect::<Vec<_>>(),
vec![(&ii(0, 0), &-8), (&ii(0, u8::MAX), &-4)]
Expand Down

0 comments on commit 1554280

Please sign in to comment.