ranges-ext
A simple and efficient range/interval set data structure designed for no_std environments.
- Uses half-open range semantics:
[start, end)(i.e.start..end) - Automatically merges overlapping or adjacent ranges
- Checks whether a value is contained in any range
- Removes a range by subtracting the intersection; can split an existing range into two
- Fixed-size capacity using
heapless::Vecfor stack allocation
Installation
[]
= "0.1"
This crate is #![no_std] and uses heapless::Vec for storage.
Usage
use RangeSet;
// Create a new RangeSet with default capacity (128)
let mut set = new;
// Add ranges (automatically normalized/merged)
set.add;
set.add;
set.add; // Overlaps with previous ranges, will be merged
assert_eq!;
// Query
assert!;
assert!;
// Remove intersection (may trigger splitting)
set.clear;
set.add;
set.remove; // Splits the range into two
assert_eq!;
// Iterate over ranges
for range in set.iter
// Batch add multiple ranges
set.extend;
Custom Capacity
You can specify a custom capacity using the const generic parameter:
// Create a RangeSet with capacity for 16 ranges
let mut set: = new;
set.add;
set.add;
API Reference
Constructors
RangeSet<T, M>::new()- Create an empty setRangeSet<T, M>::default()- Create an empty set via Default trait
Range Operations
add(range, meta)- Add a range with metadata and mergeadd_range(range)- Add a range without metadata (only forM = ())extend(ranges)- Batch add multiple ranges (only forM = ())remove_range(range)- Remove range intersection; may trigger splitting
Query Methods
contains(value)- Check if value is contained in any rangeis_empty()- Check if the set is emptylen()- Return the number of merged ranges
Access Methods
elements()- Return slice of internal elements (contains merged ranges and original lists)as_slice()- Return normalized range slice (only ranges, sorted, merged, non-overlapping)iter()- Return iterator of normalized ranges (zero-copy)
Other Methods
clear()- Clear the set
Features
no_stdcompatible: Suitable for embedded and bare-metal environments- Zero-copy iteration:
iter()method returns range references to avoid unnecessary copying - Smart merging: Adjacent/overlapping original ranges with same metadata are automatically merged
- Efficient implementation: Uses binary search to optimize insertion and query performance
- Flexible metadata: Supports any type of metadata, can be
()to indicate no metadata needed
License
Licensed under either MIT or Apache-2.0, at your option.