9 releases

0.0.11 Dec 13, 2023
0.0.10 Jul 16, 2023
0.0.9 Apr 16, 2022
0.0.8 Oct 8, 2021
0.0.4 Apr 30, 2018

#117 in Data structures

Download history 36108/week @ 2024-07-20 38029/week @ 2024-07-27 25894/week @ 2024-08-03 50057/week @ 2024-08-10 44035/week @ 2024-08-17 21415/week @ 2024-08-24 21428/week @ 2024-08-31 25064/week @ 2024-09-07 29929/week @ 2024-09-14 30988/week @ 2024-09-21 61395/week @ 2024-09-28 160086/week @ 2024-10-05 99401/week @ 2024-10-12 31573/week @ 2024-10-19 51988/week @ 2024-10-26 202579/week @ 2024-11-02

390,302 downloads per month
Used in 6 crates (3 directly)

Apache-2.0

56KB
916 lines

range-set

Store collections of PrimInt values as inclusive ranges using generic SmallVec-backed storage.

Documentation

A generic smallvec::Array parameter allows choosing how many ranges will fit on the stack before spilling over onto the heap:

let mut s = range_set![0..=2; 1];
println!("s: {:?}", s);
assert!(!s.spilled());

assert!(s.insert_range (8..=10).is_none());
println!("s: {:?}", s);
assert!(s.spilled());
let v : Vec <u32> = s.iter().collect();
assert_eq!(v, vec![0,1,2,8,9,10]);

assert_eq!(s.insert_range (3..=12), Some (range_set![8..=10; 1]));
s.shrink_to_fit();
println!("s: {:?}", s);
assert!(!s.spilled());
let v : Vec <u32> = s.iter().collect();
assert_eq!(v, vec![0,1,2,3,4,5,6,7,8,9,10,11,12]);

This is most useful with large blocks of not-quite contiguous data that should be traversed in-order.

Usage

  use range_set::{range_set, RangeSet};
  let mut s = RangeSet::<[_; 2]>::from_ranges ([1..=100, 500..=1000]);
  s.insert (200);
  s.insert_range (400..=499);
  assert_eq!(s, range_set![1..=100, 200..=200, 400..=1000]);

See ./examples/example.rs and documentation for more examples.

On-stack sizes

The top-level report_sizes function will report byte sizes for various combinations of integer types and array sizes. A program calling this function can be found in ./examples/example.rs. Example output:

RangeSet report sizes...
  size of RangeSet <[RangeInclusive <u8>; 1]>: 32
  size of RangeSet <[RangeInclusive <u16>; 1]>: 32
  size of RangeSet <[RangeInclusive <u32>; 1]>: 32
  size of RangeSet <[RangeInclusive <u64>; 1]>: 32
  size of RangeSet <[RangeInclusive <usize>; 1]>: 32
  size of RangeSet <[RangeInclusive <u8>; 2]>: 32
  size of RangeSet <[RangeInclusive <u16>; 2]>: 32
  size of RangeSet <[RangeInclusive <u32>; 2]>: 32
  size of RangeSet <[RangeInclusive <u64>; 2]>: 48
  size of RangeSet <[RangeInclusive <usize>; 2]>: 48
  size of RangeSet <[RangeInclusive <u8>; 4]>: 32
  size of RangeSet <[RangeInclusive <u16>; 4]>: 32
  size of RangeSet <[RangeInclusive <u32>; 4]>: 48
  size of RangeSet <[RangeInclusive <u64>; 4]>: 80
  size of RangeSet <[RangeInclusive <usize>; 4]>: 80
  size of RangeSet <[RangeInclusive <u8>; 8]>: 32
  size of RangeSet <[RangeInclusive <u16>; 8]>: 48
  size of RangeSet <[RangeInclusive <u32>; 8]>: 80
  size of RangeSet <[RangeInclusive <u64>; 8]>: 144
  size of RangeSet <[RangeInclusive <usize>; 8]>: 144
  size of RangeSet <[RangeInclusive <u8>; 16]>: 48
  size of RangeSet <[RangeInclusive <u16>; 16]>: 80
  size of RangeSet <[RangeInclusive <u32>; 16]>: 144
  size of RangeSet <[RangeInclusive <u64>; 16]>: 272
  size of RangeSet <[RangeInclusive <usize>; 16]>: 272
...RangeSet report sizes

Storing u8 (byte) ranges is not a good idea since the minimum size (to store a single range on the stack) is 32 bytes which is enough to store the full range of 256 values as individual bits (32 * 8 = 256).

Operations

Since the ranges are stored in sorted order, binary search is used when inserting and removing values.

Dependencies

~135–375KB