Debian Codegraph @

debian/missing-sources/rangemap/README.md

raw symbols SCIP
1 # rangemap
2
3 [![Crate](https://img.shields.io/crates/v/rangemap.svg)](https://crates.io/crates/rangemap)
4 [![Docs](https://docs.rs/rangemap/badge.svg)](https://docs.rs/rangemap)
5 [![Build status](https://github.com/jeffparsons/rangemap/workflows/CI/badge.svg)](https://github.com/jeffparsons/rangemap/actions)
6 [![Rust](https://img.shields.io/badge/rust-1.81.0%2B-blue.svg?maxAge=3600)](https://github.com/jeffparsons/rangemap) <!-- Don't forget to update the GitHub actions config when bumping minimum Rust version. -->
7
8 [`RangeMap`] and [`RangeInclusiveMap`] are map data structures whose keys
9 are stored as ranges. Contiguous and overlapping ranges that map to the same
10 value are coalesced into a single range.
11
12 Corresponding [`RangeSet`] and [`RangeInclusiveSet`] structures are also provided.
13
14
15 ## Different kinds of ranges
16
17 `RangeMap` and `RangeInclusiveMap` correspond to the [`Range`]
18 and [`RangeInclusive`] types from the standard library respectively.
19 For some applications the choice of range type may be obvious,
20 or even be dictated by pre-existing design decisions. For other applications
21 the choice may seem arbitrary, and be guided instead by convenience or
22 aesthetic preference.
23
24 If the choice is not obvious in your case, consider these differences:
25
26 - If your key type `K` represents points on a continuum (e.g. `f64`),
27 and the choice of which of two adjacent ranges "owns" the value
28 where they touch is largely arbitrary, then it may be more natural
29 to work with half-open `Range`s like `0.0..1.0` and `1.0..2.0`. If you
30 were to use closed `RangeInclusive`s here instead, then to represent two such adjacent
31 ranges you would need to subtract some infinitesimal (which may depend,
32 as it does in the case of `f64`, on the specific value of `K`)
33 from the end of the earlier range. (See the last point below for more
34 on this problem.)
35 - If you need to represent ranges that _include_ the maximum
36 value in the key domain (e.g. `255u8`) then you will
37 probably want to use `RangeInclusive`s like `128u8..=255u8`. Sometimes
38 it may be possible to instead work around this by using a wider key
39 type than the values you are actually trying to represent (`K=u16`
40 even though you are only trying to represent ranges covering `u8`)
41 but in these cases the key domain often represents discrete objects
42 rather than points on a continuum, and so `RangeInclusive` may
43 be a more natural way to express these ranges anyway.
44 - If you are using `RangeInclusive`, then it must be possible to define
45 _successor_ and _predecessor_ functions for your key type `K`,
46 because adjacent ranges can not be detected (and thereby coalesced)
47 simply by testing their ends for equality. For key types that represent
48 points on a continuum, defining these functions may be awkward and error-prone.
49 For key types that represent discrete objects, this is usually much
50 more straightforward.
51
52
53 ## Example: use with Chrono
54
55 ```rust
56 use chrono::offset::TimeZone;
57 use chrono::{Duration, Utc};
58 use rangemap::RangeMap;
59
60 fn main() {
61 let people = ["Alice", "Bob", "Carol"];
62 let mut roster = RangeMap::new();
63
64 // Set up initial roster.
65 let start_of_roster = Utc.ymd(2019, 1, 7);
66 let mut week_start = start_of_roster;
67 for _ in 0..3 {
68 for person in &people {
69 let next_week = week_start + Duration::weeks(1);
70 roster.insert(week_start..next_week, person);
71 week_start = next_week;
72 }
73 }
74
75 // Bob is covering Alice's second shift (the fourth shift overall).
76 let fourth_shift_start = start_of_roster + Duration::weeks(3);
77 let fourth_shift_end = fourth_shift_start + Duration::weeks(1);
78 roster.insert(fourth_shift_start..fourth_shift_end, &"Bob");
79
80 for (range, person) in roster.iter() {
81 println!("{} ({}): {}", range.start, range.end - range.start, person);
82 }
83
84 // Output:
85 // 2019-01-07UTC (P7D): Alice
86 // 2019-01-14UTC (P7D): Bob
87 // 2019-01-21UTC (P7D): Carol
88 // 2019-01-28UTC (P14D): Bob
89 // 2019-02-11UTC (P7D): Carol
90 // 2019-02-18UTC (P7D): Alice
91 // 2019-02-25UTC (P7D): Bob
92 // 2019-03-04UTC (P7D): Carol
93 }
94 ```
95
96
97 ## Crate features
98
99 By default this crate has no dependencies on other crates.
100
101 If you enable the **serde1** feature it will introduce a dependency on
102 the _serde_ crate and provide `Serialize` and `Deserialize`
103 implementations for all map and set types in this crate.
104
105 You can enable the **serde1** feature in your _Cargo.toml_ file like so:
106
107 ```toml
108 [dependencies]
109 rangemap = { version = "1", features = ["serde1"] }
110 ```
111
112 You can similarly enable support for _quickcheck_ by enabling
113 the **quickcheck** feature.
114
115 ## Building without the Rust standard library
116
117 This crate can work without the full standard library available
118 (e.g. when running on bare metal without an operating system)
119 but relies on the presence of a global allocator &mdash;
120 i.e. it links the `core` and `alloc` crates, but not `std`.
121
122 Presently there is no functionality in the crate that require
123 the standard library. Such functionality will likely be
124 introduced in the future, and will be gated behind a default-on
125 `std` feature.
126
127 See [The Rust Programming Language](https://doc.rust-lang.org/1.7.0/book/no-stdlib.html)
128 book for general information about operating without the standard library.
129
130
131
132 [`RangeMap`]: https://docs.rs/rangemap/latest/rangemap/map/struct.RangeMap.html
133 [`RangeInclusiveMap`]: https://docs.rs/rangemap/latest/rangemap/inclusive_map/struct.RangeInclusiveMap.html
134 [`RangeSet`]: https://docs.rs/rangemap/latest/rangemap/set/struct.RangeSet.html
135 [`RangeInclusiveSet`]: https://docs.rs/rangemap/latest/rangemap/inclusive_set/struct.RangeInclusiveSet.html
136 [`Range`]: https://doc.rust-lang.org/stable/std/ops/struct.Range.html
137 [`RangeInclusive`]: https://doc.rust-lang.org/stable/std/ops/struct.RangeInclusive.html