ranges-ext-0.2.1 has been yanked.
ranges-ext
An efficient range/interval set data structure designed for no_std environments.
- Uses half-open interval semantics:
[start, end)(i.e.,start..end) - Automatic merging of overlapping or adjacent intervals
- Supports querying whether a value falls within any interval
- Supports interval removal, removing intersections from the set; splits existing intervals when necessary
- Uses
heapless::Vecfor fixed-capacity storage with stack allocation support - Supports metadata (kind): Each interval can carry custom metadata
- Supports overwrite control: Specify whether intervals can be overwritten by other intervals
Installation
[]
= "0.2"
This library is #![no_std] and uses heapless::Vec for storage.
Quick Start
use ;
// First, define a struct that implements the RangeInfo trait
// Create RangeSet
let mut set = new;
// Add intervals (intervals with the same kind that are adjacent or overlapping will automatically merge)
set.add?;
set.add?;
set.add?; // Overlaps with the first two intervals, will merge
assert_eq!;
assert_eq!;
assert_eq!;
// Add intervals with different kinds
set.add?;
assert_eq!;
// Query
assert!;
assert!;
assert!;
// Remove intervals (preserves intersections with other intervals)
set.remove?;
assert_eq!;
// Iterator
for info in set.iter
Basic Usage (No Metadata)
If you don't need metadata, you can use a simpler struct:
use ;
let mut set = new;
set.add?;
set.add?;
Custom Capacity
You can specify custom capacity through const generic parameters:
// Create a RangeSet with capacity 16
let mut set: = new;
Core Trait: RangeInfo
API Reference
Constructors
RangeSet<T, C>::new()- Create an empty setRangeSet<T, C>::default()- Create an empty set through the Default trait
Interval Operations
add(info)- Add interval, automatically merge overlapping/adjacent intervals of the same kind. ReturnsResult<(), RangeError>extend(ranges)- Batch add multiple intervals. ReturnsResult<(), RangeError>remove(range)- Remove interval intersections; may trigger interval splitting. ReturnsResult<(), RangeError>
Query Methods
contains(value)- Check if a value is contained by any intervalis_empty()- Check if the set is emptylen()- Return the number of merged intervals
Access Methods
as_slice()- Return a normalized interval slice (sorted, merged, non-overlapping)iter()- Return a normalized interval iterator (zero-copy)
Other Methods
clear()- Clear the set
Advanced Features
Interval Overwrite Control
// Add a non-overwritable interval
set.add?;
// Attempting to add a conflicting interval will return an error
let result = set.add;
assert!; // Returns RangeError::Conflict
// Overwritable intervals can be replaced
set.add?; // Overwritable
set.add?; // Will overwrite overlapping parts
Error Handling
match set.add
Features
no_stdcompatible: Suitable for embedded and bare-metal environments- Zero-copy iteration:
iter()method returns interval references, avoiding unnecessary copying - Smart merging: Adjacent/overlapping intervals with the same kind automatically merge
- Efficient implementation: Uses binary search to optimize insertion and query performance
- Flexible metadata: Supports arbitrary type metadata
- Overwrite control: Precise control over which intervals can be overwritten by other intervals
Examples
See the examples/ directory for more examples:
debug_demo.rs- Basic debugging examplekey_demo.rs- Example using different kindsoverlap_demo.rs- Detailed demonstration of interval overwriting and merging
License
Dual licensed under MIT or Apache-2.0, you may choose either.