RMap

Struct RMap 

Source
pub struct RMap { /* private fields */ }
Expand description

A collection that manages disjoint, inclusive ranges [start, end].

Implementations§

Source§

impl RMap

Source

pub fn new() -> Self

Creates a new, empty RMap.

Source

pub fn insert(&mut self, value: u64)

Inserts a value into the RMap.

§Behavior
  • Create a new range [value, value] if value is isolated.
  • Extend an existing range if value is adjacent to it (e.g., inserting 5 into [1, 4] results in [1, 5]).
  • Merge two ranges if value bridges them (e.g., inserting 3 into a map with [1, 2] and [4, 5] results in [1, 5]).
  • Do nothing if value is already covered by an existing range.
§Complexity

The time complexity is typically O(log N) due to BTreeMap lookups and insertions, where N is the number of disjoint ranges in the map. In scenarios involving merges, a few extra map operations (removals, insertions) might occur, but the overall complexity remains logarithmic.

§Example
use commonware_storage::rmap::RMap;

let mut map = RMap::new();
map.insert(1); // Map: [1, 1]
assert_eq!(map.next_gap(0), (None, Some(1)));
map.insert(3); // Map: [1, 1], [3, 3]
assert_eq!(map.next_gap(1), (Some(1), Some(3)));
map.insert(2); // Map: [1, 3]
map.insert(0); // Map: [0, 3]
map.insert(5); // Map: [0, 3], [5, 5]
map.insert(4); // Map: [0, 5]
assert_eq!(map.get(&3), Some((0, 5)));
Source

pub fn get(&self, value: &u64) -> Option<(u64, u64)>

Returns the range that contains the given value.

Source

pub fn remove(&mut self, start: u64, end: u64)

Removes a range [start, end] (inclusive) from the RMap.

§Behavior
  • If the removal range completely covers an existing range, the existing range is removed.
  • If the removal range is a sub-range of an existing range, the existing range may be split into two (e.g., removing [3, 4] from [1, 6] results in [1, 2] and [5, 6]).
  • If the removal range overlaps with the start or end of an existing range, the existing range is truncated (e.g., removing [1, 2] from [1, 5] results in [3, 5]).
  • If the removal range covers multiple existing ranges, all such ranges are affected or removed.
  • If start > end, the method does nothing.
  • If the removal range does not overlap with any existing range, the map remains unchanged.
§Complexity

The time complexity is O(M + K log N), where N is the total number of ranges in the map, M is the number of ranges that overlap with the removal range (iterate part), and K is the number of new ranges created or ranges removed (at most 2 additions and M removals).

§Example
use commonware_storage::rmap::RMap;

let mut map = RMap::new();
map.insert(1); map.insert(2); map.insert(3); // Map: [1, 3]
map.insert(5); map.insert(6); map.insert(7); // Map: [1, 3], [5, 7]

map.remove(2, 6); // Results in [1, 1], [7, 7]
assert_eq!(map.get(&1), Some((1, 1)));
assert_eq!(map.get(&2), None);
assert_eq!(map.get(&6), None);
assert_eq!(map.get(&7), Some((7, 7)));
Source

pub fn iter(&self) -> impl Iterator<Item = (&u64, &u64)>

Returns an iterator over the ranges (start, end) in the RMap.

The ranges are yielded in ascending order of their start points. Each tuple represents an inclusive range [start, end].

§Example
use commonware_storage::rmap::RMap;

let mut map = RMap::new();
map.insert(0); map.insert(1); // Map: [0, 1]
map.insert(3); map.insert(4); // Map: [0, 1], [3, 4]

let mut iter = map.iter();
assert_eq!(iter.next(), Some((&0, &1)));
assert_eq!(iter.next(), Some((&3, &4)));
assert_eq!(iter.next(), None);
Source

pub fn next_gap(&self, value: u64) -> (Option<u64>, Option<u64>)

Finds the end of the range containing value and the start of the range succeeding value. This method is useful for identifying gaps around a given point.

§Behavior
  • If value falls within an existing range [r_start, r_end], current_range_end will be Some(r_end).
  • If value falls in a gap between two ranges [..., prev_end] and [next_start, ...], current_range_end will be None and next_range_start will be Some(next_start).
  • If value is before all ranges in the map, current_range_end will be None.
  • If value is after all ranges in the map (or within the last range), next_range_start will be None.
  • If the map is empty, both will be None.
§Arguments
  • value: The u64 value to query around.
§Returns

A tuple (Option<u64>, Option<u64>) where:

  • The first element (current_range_end) is Some(end) of the range that contains value. It’s None if value is before all ranges, the map is empty, or value is not in any range.
  • The second element (next_range_start) is Some(start) of the first range that begins strictly after value. It’s None if no range starts after value or the map is empty.
§Complexity

O(log N) where N is the number of ranges in RMap.

§Example
use commonware_storage::rmap::RMap;

let mut map = RMap::new();
map.insert(1); map.insert(2); // Map: [1, 2]
map.insert(5); map.insert(6); // Map: [1, 2], [5, 6]

assert_eq!(map.next_gap(0), (None, Some(1)));        // Before all ranges
assert_eq!(map.next_gap(1), (Some(2), Some(5)));     // Value is at the start of a range
assert_eq!(map.next_gap(2), (Some(2), Some(5)));     // Value is at the end of a range
assert_eq!(map.next_gap(3), (None, Some(5)));     // Value is in a gap
assert_eq!(map.next_gap(5), (Some(6), None));        // Value is at the start of the last range
assert_eq!(map.next_gap(6), (Some(6), None));        // Value is at the end of the last range
assert_eq!(map.next_gap(7), (None, None));        // After all ranges
Source

pub fn missing_items(&self, start: u64, max: usize) -> Vec<u64>

Returns up to max missing items starting from start.

This method iterates through gaps between existing ranges, collecting missing indices until either max items are found or there are no more gaps to fill.

§Arguments
  • start: The index to start searching from (inclusive).
  • max: The maximum number of missing items to return.
§Returns

A vector containing up to max missing indices from gaps between ranges. The vector may contain fewer than max items if there aren’t enough gaps. If there are no more ranges after the current position, no items are returned.

§Complexity

O(G log N + M) where N is the number of ranges in RMap, G is the number of gaps visited (at most N), and M is the number of missing items returned (at most max). Each gap requires a next_gap call (O(log N)) and collecting items (O(items in gap)).

§Example
use commonware_storage::rmap::RMap;

let mut map = RMap::new();
map.insert(1); map.insert(2); // Map: [1, 2]
map.insert(5); map.insert(6); // Map: [1, 2], [5, 6]
map.insert(10);                // Map: [1, 2], [5, 6], [10, 10]

// Starting from 0, find up to 5 missing items
assert_eq!(map.missing_items(0, 5), vec![0, 3, 4, 7, 8]);

// Starting from 3, find up to 3 missing items
assert_eq!(map.missing_items(3, 3), vec![3, 4, 7]);

// Starting from 7, find up to 10 missing items (only gaps are returned)
assert_eq!(map.missing_items(7, 10), vec![7, 8, 9]);

// Starting from 11, there are no more ranges, so no gaps
assert_eq!(map.missing_items(11, 5), Vec::<u64>::new());

Trait Implementations§

Source§

impl Debug for RMap

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for RMap

Source§

fn default() -> RMap

Returns the “default value” for a type. Read more
Source§

impl PartialEq for RMap

Source§

fn eq(&self, other: &RMap) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl StructuralPartialEq for RMap

Auto Trait Implementations§

§

impl Freeze for RMap

§

impl RefUnwindSafe for RMap

§

impl Send for RMap

§

impl Sync for RMap

§

impl Unpin for RMap

§

impl UnwindSafe for RMap

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> FutureExt for T

Source§

fn with_context(self, otel_cx: Context) -> WithContext<Self>

Attaches the provided Context to this type, returning a WithContext wrapper. Read more
Source§

fn with_current_context(self) -> WithContext<Self>

Attaches the current Context to this type, returning a WithContext wrapper. Read more
Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> ErasedDestructor for T
where T: 'static,

Source§

impl<A, B, T> HttpServerConnExec<A, B> for T
where B: Body,