#data-structures #map #data #library

nightly range_bounds_map

This crate provides [RangeBoundsMap] and [RangeBoundsSet], Data Structures for storing non-overlapping intervals based of [BTreeMap]

14 unstable releases (3 breaking)

0.4.0 Apr 24, 2023
0.3.2 Apr 19, 2023
0.2.2 Apr 10, 2023
0.1.1 Jan 5, 2023
0.0.6 Dec 29, 2022

#156 in Data structures

Download history 34/week @ 2023-02-02 33/week @ 2023-02-09 40/week @ 2023-02-16 1/week @ 2023-03-02 5/week @ 2023-03-16 10/week @ 2023-03-23 2/week @ 2023-03-30 72/week @ 2023-04-06 59/week @ 2023-04-13 63/week @ 2023-04-20 12/week @ 2023-04-27 36/week @ 2023-05-04 4/week @ 2023-05-11 10/week @ 2023-05-18

64 downloads per month


2.5K SLoC


License Docs Maintained Crates.io


This crate provides RangeBoundsMap and RangeBoundsSet, Data Structures for storing non-overlapping intervals based of BTreeMap.

Example using Ranges

use range_bounds_map::RangeBoundsMap;

let mut map = RangeBoundsMap::new();

map.insert_strict(0..5, true);
map.insert_strict(5..10, false);

assert_eq!(map.overlaps(&(-2..12)), true);
assert_eq!(map.contains_point(&20), false);
assert_eq!(map.contains_point(&5), true);

Example using a custom RangeBounds type

use std::ops::{Bound, RangeBounds};

use range_bounds_map::RangeBoundsMap;

enum Reservation {
	// Start, End (Inclusive-Inclusive)
	Finite(u8, u8),
	// Start (Exclusive)

// First, we need to implement RangeBounds
impl RangeBounds<u8> for Reservation {
	fn start_bound(&self) -> Bound<&u8> {
		match self {
			Reservation::Finite(start, _) => {
			Reservation::Infinite(start) => {
	fn end_bound(&self) -> Bound<&u8> {
		match self {
			Reservation::Finite(_, end) => Bound::Included(end),
			Reservation::Infinite(_) => Bound::Unbounded,

// Next we can create a custom typed RangeBoundsMap
let reservation_map = RangeBoundsMap::try_from([
	(Reservation::Finite(10, 20), "Ferris".to_string()),
	(Reservation::Infinite(20), "Corro".to_string()),

for (reservation, name) in reservation_map.overlapping(&(16..17))
		"{name} has reserved {reservation:?} inside the range 16..17"

for (reservation, name) in reservation_map.iter() {
	println!("{name} has reserved {reservation:?}");


Key Definitions:

Invalid Ranges

Within this crate, not all ranges are considered valid ranges. The definition of the validity of a range used within this crate is that a range is only valid if it contains at least one value of the underlying domain.

For example, 4..6 is considered valid as it contains the values 4 and 5, however, 4..4 is considered invalid as it contains no values. Another example of invalid range are those whose start values are greater than their end values. such as 5..2 or 100..=40.

Here are a few examples of ranges and whether they are valid:

range valid
0..=0 YES
0..0 NO
0..1 YES
9..8 NO
(0.4)..=(-0.2) NO
..(-3) YES
0.0003.. YES
.. YES
400..=400 YES


Two ranges are "overlapping" if there exists a point that is contained within both ranges.


Two ranges are "touching" if they do not overlap and there exists no value between them. For example, 2..4 and 4..6 are touching but 2..4 and 6..8 are not, neither are 2..6 and 4..8.


When a range "merges" other ranges it absorbs them to become larger.

Further Reading

See Wikipedia's article on mathematical Intervals: https://en.wikipedia.org/wiki/Interval_(mathematics)


  • I had to create a new trait: [TryFromBounds] rather than using TryFrom<(Bound, Bound)> (relys on upstream to impl, see this thread)


I originally came up with the StartBound: Ord bodge on my own, however, I later stumbled across rangemap which also used a StartBound: Ord bodge. rangemap then became my main source of inspiration.

Later I then undid the Ord bodge and switched to my own full-code port of BTreeMap, inspired and forked from copse, for it's increased flexibility.


The aim for this library was to become a more generic superset of rangemap, following from this issue and this pull request in which I changed rangemap's RangeMap to use RangeBoundss as keys before I realized it might be easier and simpler to just write it all from scratch.

Similar Crates

Here are some relevant crates I found whilst searching around the topic area:


~38K SLoC