Skip to content

Commit

Permalink
add the ZosditMap::remove_last_value_at_point() method
Browse files Browse the repository at this point in the history
  • Loading branch information
ripytide committed Apr 6, 2024
1 parent ad1c61a commit 1f111f7
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 5 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## Unreleased - ReleaseDate

### Added

- Added the `ZosditMap::remove_last_value_at_point()` method similar to the
existing `ZosditMap::get_last_value_at_point()` method.

### Changed
- Some spelling errors corrected

## 0.9.0 - 2024-02-11

### Added
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ off [`BTreeMap`].
Several Discrete Interval Tree data-structures have been implemented, here
is a brief summary of each of them and why you might use them:

| Struct|Abreviation|Use-Case|
| Struct|Abbreviation|Use-Case|
|-----|------|------|
|[`NoditMap`]|Non-Overlapping Discrete Interval Tree Map| General purpose way of associating data with intervals that do not overlap|
|[`NoditSet`]|Non-Overlapping Discrete Interval Tree Set| Useful for when you want to store intervals but don't want/need to associate data with each interval|
Expand Down
4 changes: 2 additions & 2 deletions src/gqdit/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -340,8 +340,8 @@ where
/// Inserts an interval into the structure assigned to the given
/// identifiers.
///
/// How overlapping/touching intervals of the same specificer are
/// stored internally is unspecificed since only the gaps are able to
/// How overlapping/touching intervals of the same specifier are
/// stored internally is unspecified since only the gaps are able to
/// be queried and regardless of how they are stored the gaps will be
/// the same.
///
Expand Down
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
//! Several Discrete Interval Tree data-structures have been implemented, here
//! is a brief summary of each of them and why you might use them:
//!
//! | Struct|Abreviation|Use-Case|
//! | Struct|Abbreviation|Use-Case|
//! |-----|------|------|
//! |[`NoditMap`]|Non-Overlapping Discrete Interval Tree Map| General purpose way of associating data with intervals that do not overlap|
//! |[`NoditSet`]|Non-Overlapping Discrete Interval Tree Set| Useful for when you want to store intervals but don't want/need to associate data with each interval|
Expand Down Expand Up @@ -371,7 +371,7 @@
#![allow(clippy::tabs_in_doc_comments)]
#![allow(clippy::needless_return)]
#![cfg_attr(not(test), no_std)]
#![no_std]

extern crate alloc;

Expand Down
4 changes: 4 additions & 0 deletions src/nodit/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1515,6 +1515,10 @@ mod serde {

#[cfg(test)]
mod tests {
extern crate std;

use std::dbg;

use pretty_assertions::assert_eq;

use super::*;
Expand Down
61 changes: 61 additions & 0 deletions src/zosdit/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,62 @@ where
.and_then(|(_, x)| x.last())
}

/// Removes the last value stored in the `SmallVec` for the interval(s)
/// that contain that point.
///
/// # Examples
/// ```
/// use nodit::interval::ii;
/// use nodit::ZosditMap;
///
/// let mut map = ZosditMap::from_slice_strict_back([
/// (ii(0, 4), -2),
/// (ii(4, 4), -4),
/// (ii(4, 4), -8),
/// (ii(4, 8), -10),
/// ])
/// .unwrap();
///
/// assert_eq!(map.get_last_value_at_point(4), Some(&-10));
/// assert_eq!(map.remove_last_value_at_point(4), Some(-10));
///
/// assert_eq!(map.get_last_value_at_point(4), Some(&-8));
/// assert_eq!(map.remove_last_value_at_point(4), Some(-8));
///
/// assert_eq!(map.get_last_value_at_point(4), Some(&-4));
/// assert_eq!(map.remove_last_value_at_point(4), Some(-4));
///
/// assert_eq!(map.get_last_value_at_point(4), Some(&-2));
/// assert_eq!(map.remove_last_value_at_point(4), Some(-2));
///
/// assert_eq!(map.get_last_value_at_point(4), None);
/// assert_eq!(map.remove_last_value_at_point(4), None);
/// ```
pub fn remove_last_value_at_point(&mut self, point: I) -> Option<V> {
let mut cursor = self.inner.lower_bound_mut(
exclusive_comp_generator(point, Ordering::Greater),
SearchBoundCustom::Included,
);

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

if let Some((key, value)) = cursor.key_value_mut() {
if key.contains_point(point) {
let last = value.pop().unwrap();

if value.is_empty() {
cursor.remove_current();
}

return Some(last);
}
}

None
}

/// Appends the value to the `SmallVec` corresponding to the interval.
///
/// If the given interval non-zero-overlaps one or more intervals already in the
Expand Down Expand Up @@ -665,6 +721,11 @@ mod serde {

#[cfg(test)]
mod tests {
extern crate std;

use alloc::vec;
use std::dbg;

use pretty_assertions::assert_eq;

use super::*;
Expand Down

0 comments on commit 1f111f7

Please sign in to comment.