segmap
]
segmap
exposes SegmentMap
, a map data structure whose keys are stored as
ranges. Contiguous and overlapping ranges that map to the same value are
coalesced into a single range.
A correspoinding SegmentSet
structure is also provided.
Segment<T>
TypeSegmentMap
supports all types of input range types in the same map and coerces
them all to a common range type for internal representation. A Segment<T>
is
always represented as increasing, so "backwards" ranges will be flipped for
insertion.
Most methods on SegmentMap
and SegmentSet
accept a generic argument for the
range, which only needs to implement RangeBounds
.
```rust use chrono::offset::TimeZone; use chrono::{Duration, Utc}; use segmap::SegmentMap;
let people = ["Alice", "Bob", "Carol"]; let mut roster = SegmentMap::new();
// Set up initial roster. let startofroster = Utc.ymd(2019, 1, 7); let mut weekstart = startofroster; for _ in 0..3 { for person in people { let nextweek = weekstart + Duration::weeks(1); roster.insert(weekstart..nextweek, person); weekstart = next_week; } }
// Bob is covering Alice's second shift (the fourth shift overall). let fourthshiftstart = startofroster + Duration::weeks(3); let fourthshiftend = fourthshiftstart + Duration::weeks(1); roster.insert(fourthshiftstart..fourthshiftend, "Bob");
// Print out the roster, and observe that // the fourth and fifth shifts have been coalesced // into one range. for (range, &person) in roster.iter() { let start = *range.startvalue().unwrap(); let duration = *range.endvalue().unwrap() - start; println!("{} ({}): {}", start, duration, person); }
// Output: // 2019-01-07UTC (P7D): Alice // 2019-01-14UTC (P7D): Bob // 2019-01-21UTC (P7D): Carol // 2019-01-28UTC (P14D): Bob // 2019-02-11UTC (P7D): Carol // 2019-02-18UTC (P7D): Alice // 2019-02-25UTC (P7D): Bob // 2019-03-04UTC (P7D): Carol ```
This crate can work without the full standard library available
(e.g. when running on bare metal without an operating system)
but relies on the presence of a global allocator —
i.e. it links the core
and alloc
crates, but not std
.
Presently there is no functionality in the crate that require
the standard library. Such functionality will likely be
introduced in the future, and will be gated behind a default-on
std
feature.
See The Rust Programming Language book for general information about operating without the standard library.