1 //! Implements a map from integer indices to data.
2 //! Rather than storing data for every index, internally, this maps entire ranges to the data.
3 //! To this end, the APIs all work on ranges, not on individual integers. Ranges are split as
4 //! necessary (e.g. when [0,5) is first associated with X, and then [1,2) is mutated).
5 //! Users must not depend on whether a range is coalesced or not, even though this is observable
6 //! via the iteration APIs.
7 use std::collections::BTreeMap;
10 #[derive(Clone, Debug)]
11 pub struct RangeMap<T> {
12 map: BTreeMap<Range, T>,
15 // The derived `Ord` impl sorts first by the first field, then, if the fields are the same,
16 // by the second field.
17 // This is exactly what we need for our purposes, since a range query on a BTReeSet/BTreeMap will give us all
18 // `MemoryRange`s whose `start` is <= than the one we're looking for, but not > the end of the range we're checking.
19 // At the same time the `end` is irrelevant for the sorting and range searching, but used for the check.
20 // This kind of search breaks, if `end < start`, so don't do that!
21 #[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Debug)]
24 end: u64, // Invariant: end > start
28 fn range(offset: u64, len: u64) -> ops::Range<Range> {
30 // We select all elements that are within
31 // the range given by the offset into the allocation and the length.
32 // This is sound if all ranges that intersect with the argument range, are in the
33 // resulting range of ranges.
35 // lowest range to include `offset`
40 // lowest (valid) range not to include `offset+len`
42 end: offset + len + 1,
47 /// Tests if all of [offset, offset+len) are contained in this range.
48 fn overlaps(&self, offset: u64, len: u64) -> bool {
50 offset < self.end && offset + len >= self.start
55 pub fn new() -> RangeMap<T> {
56 RangeMap { map: BTreeMap::new() }
59 fn iter_with_range<'a>(
63 ) -> impl Iterator<Item = (&'a Range, &'a T)> + 'a {
65 self.map.range(Range::range(offset, len)).filter_map(
68 if range.overlaps(offset, len) {
77 pub fn iter<'a>(&'a self, offset: u64, len: u64) -> impl Iterator<Item = &'a T> + 'a {
78 self.iter_with_range(offset, len).map(|(_, data)| data)
81 fn split_entry_at(&mut self, offset: u64)
85 let range = match self.iter_with_range(offset, 1).next() {
86 Some((&range, _)) => range,
90 range.start <= offset && range.end > offset,
91 "We got a range that doesn't even contain what we asked for."
93 // There is an entry overlapping this position, see if we have to split it
94 if range.start < offset {
95 let data = self.map.remove(&range).unwrap();
96 let old = self.map.insert(
103 assert!(old.is_none());
104 let old = self.map.insert(
111 assert!(old.is_none());
115 pub fn iter_mut_all<'a>(&'a mut self) -> impl Iterator<Item = &'a mut T> + 'a {
116 self.map.values_mut()
119 /// Provide mutable iteration over everything in the given range. As a side-effect,
120 /// this will split entries in the map that are only partially hit by the given range,
121 /// to make sure that when they are mutated, the effect is constrained to the given range.
122 pub fn iter_mut_with_gaps<'a>(
126 ) -> impl Iterator<Item = &'a mut T> + 'a
131 // Preparation: Split first and last entry as needed.
132 self.split_entry_at(offset);
133 self.split_entry_at(offset + len);
134 // Now we can provide a mutable iterator
135 self.map.range_mut(Range::range(offset, len)).filter_map(
136 move |(&range, data)| {
137 if range.overlaps(offset, len) {
139 offset <= range.start && offset + len >= range.end,
140 "The splitting went wrong"
151 /// Provide a mutable iterator over everything in the given range, with the same side-effects as
152 /// iter_mut_with_gaps. Furthermore, if there are gaps between ranges, fill them with the given default.
153 /// This is also how you insert.
154 pub fn iter_mut<'a>(&'a mut self, offset: u64, len: u64) -> impl Iterator<Item = &'a mut T> + 'a
158 // Do a first iteration to collect the gaps
159 let mut gaps = Vec::new();
160 let mut last_end = offset;
161 for (range, _) in self.iter_with_range(offset, len) {
162 if last_end < range.start {
168 last_end = range.end;
170 if last_end < offset + len {
177 // Add default for all gaps
179 let old = self.map.insert(gap, Default::default());
180 assert!(old.is_none());
183 // Now provide mutable iteration
184 self.iter_mut_with_gaps(offset, len)
187 pub fn retain<F>(&mut self, mut f: F)
189 F: FnMut(&T) -> bool,
191 let mut remove = Vec::new();
192 for (range, data) in self.map.iter() {
198 for range in remove {
199 self.map.remove(&range);
208 /// Query the map at every offset in the range and collect the results.
209 fn to_vec<T: Copy>(map: &RangeMap<T>, offset: u64, len: u64) -> Vec<T> {
210 (offset..offset + len)
212 .map(|i| *map.iter(i, 1).next().unwrap())
218 let mut map = RangeMap::<i32>::new();
220 for x in map.iter_mut(10, 1) {
224 assert_eq!(to_vec(&map, 10, 1), vec![42]);
229 let mut map = RangeMap::<i32>::new();
230 for x in map.iter_mut(11, 1) {
233 for x in map.iter_mut(15, 1) {
237 // Now request a range that needs three gaps filled
238 for x in map.iter_mut(10, 10) {
245 to_vec(&map, 10, 10),
246 vec![23, 42, 23, 23, 23, 42, 23, 23, 23, 23]
248 assert_eq!(to_vec(&map, 13, 5), vec![23, 23, 42, 23, 23]);