Debian Codegraph @

debian/missing-sources/rangemap/src/dense.rs

raw
1 use alloc::{collections::BTreeMap, vec::Vec};
2 use core::ops::{Range, RangeInclusive};
3
4 use super::{RangeInclusiveMap, RangeMap};
5
6 // A simple but infeasibly slow and memory-hungry
7 // version of `RangeInclusiveMap` for testing.
8 //
9 // Only understands `u32` keys, so that we don't
10 // have to be generic over `step`. This is just for
11 // testing, so it's fine.
12 //
13 // Note that even though this mirrors the semantics
14 // of `RangeInclusiveMap`, it can also be used to
15 // test `RangeMap` just fine.
16 #[derive(Eq, PartialEq, Debug)]
17 pub struct DenseU32RangeMap<V> {
18 // Inner B-Tree map. Stores values and their keys
19 // directly rather than as ranges.
20 btm: BTreeMap<u32, V>,
21 }
22
23 impl<V> DenseU32RangeMap<V>
24 where
25 V: PartialEq + Clone,
26 {
27 pub fn new() -> DenseU32RangeMap<V> {
28 DenseU32RangeMap {
29 btm: BTreeMap::new(),
30 }
31 }
32
33 pub fn insert(&mut self, range: RangeInclusive<u32>, value: V) {
34 for k in range {
35 self.btm.insert(k, value.clone());
36 }
37 }
38
39 pub fn iter(&self) -> Iter<'_, V> {
40 Iter {
41 inner: self.btm.iter(),
42 current: None,
43 }
44 }
45
46 pub fn end_exclusive_iter(&self) -> EndExclusiveIter<'_, V> {
47 EndExclusiveIter { inner: self.iter() }
48 }
49
50 // Vecs are easier to use for assertions than iterators,
51 // because you don't have to consume them to compare them.
52 pub fn to_vec(&self) -> Vec<(RangeInclusive<u32>, V)> {
53 self.iter()
54 .map(|(range, value)| (range, value.clone()))
55 .collect()
56 }
57
58 pub fn to_end_exclusive_vec(&self) -> Vec<(Range<u32>, V)> {
59 self.end_exclusive_iter()
60 .map(|(range, value)| (range, value.clone()))
61 .collect()
62 }
63 }
64
65 impl<V> From<RangeMap<u32, V>> for DenseU32RangeMap<V>
66 where
67 V: PartialEq + Clone,
68 {
69 fn from(range_map: RangeMap<u32, V>) -> Self {
70 let mut dense = Self::new();
71 for (range, value) in range_map.iter() {
72 // Convert to inclusive end.
73 // (This is only valid because u32 has
74 // the "successor function".)
75 //
76 // NOTE: Clippy's `range_minus_one` lint is a bit overzealous here,
77 // because we _can't_ pass an open-ended range to `insert`.
78 #[allow(clippy::range_minus_one)]
79 dense.insert(range.start..=(range.end - 1), value.clone());
80 }
81 dense
82 }
83 }
84
85 impl<V> From<RangeInclusiveMap<u32, V>> for DenseU32RangeMap<V>
86 where
87 V: PartialEq + Clone,
88 {
89 fn from(range_inclusive_map: RangeInclusiveMap<u32, V, u32>) -> Self {
90 let mut dense = Self::new();
91 for (range, value) in range_inclusive_map.iter() {
92 dense.insert(range.clone(), value.clone());
93 }
94 dense
95 }
96 }
97
98 pub struct Iter<'a, V> {
99 inner: alloc::collections::btree_map::Iter<'a, u32, V>,
100 // Current range being built for output.
101 // We modify it as we iterate through the underlying
102 // dense map.
103 current: Option<(RangeInclusive<u32>, &'a V)>,
104 }
105
106 // Coalesce items from the underlying dense map as we go.
107 impl<'a, V> Iterator for Iter<'a, V>
108 where
109 V: 'a + PartialEq,
110 {
111 type Item = (RangeInclusive<u32>, &'a V);
112
113 fn next(&mut self) -> Option<(RangeInclusive<u32>, &'a V)> {
114 if let Some(next_dense) = self.inner.next() {
115 if let Some(current) = &mut self.current {
116 // We're already building a range. Can we extend it?
117 if current.0.end() + 1 == *next_dense.0 && *current.1 == *next_dense.1 {
118 // This immediately follows the last item and the value is the same;
119 // we can extend it.
120 *current = (*current.0.start()..=*next_dense.0, current.1);
121 // Recurse until we find a way to flush it.
122 self.next()
123 } else {
124 // There's a gap or the value differs; flush the one we were working on and start a new one.
125 let item_to_yield = current.clone();
126 *current = (*next_dense.0..=*next_dense.0, next_dense.1);
127 Some(item_to_yield)
128 }
129 } else {
130 // We're not building a range yet; start a new one.
131 self.current = Some((*next_dense.0..=*next_dense.0, next_dense.1));
132 // Recurse until we find a way to flush it.
133 self.next()
134 }
135 } else {
136 // We reached the end of the underlying dense map.
137 // Flush the item we were building, if any.
138 self.current.take()
139 }
140 }
141 }
142
143 pub struct EndExclusiveIter<'a, V> {
144 inner: Iter<'a, V>,
145 }
146
147 impl<'a, V> Iterator for EndExclusiveIter<'a, V>
148 where
149 V: 'a + PartialEq,
150 {
151 type Item = (Range<u32>, &'a V);
152
153 fn next(&mut self) -> Option<(Range<u32>, &'a V)> {
154 self.inner
155 .next()
156 .map(|(range, value)| ((*range.start())..(*range.end() + 1), value))
157 }
158 }