Skip to main content

mid_tools/
range_map.rs

1use std::{borrow::Borrow, fmt, ops};
2
3use crate::partition_map::{IntoPartitions, Partition, PartitionBounds, PartitionMap, Partitions};
4
5#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
6#[repr(transparent)]
7pub struct RangeMap<K, V>(PartitionMap<K, Option<V>>);
8
9impl<K: fmt::Debug, V: fmt::Debug> fmt::Debug for RangeMap<K, V> {
10    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
11        self.0
12            .partitions()
13            .fold(&mut f.debug_map(), |d, (k, v)| {
14                if let Some(v) = v {
15                    d.entry(&k, v)
16                } else {
17                    d
18                }
19            })
20            .finish()
21    }
22}
23
24impl<K, V> Default for RangeMap<K, V> {
25    fn default() -> Self { Self::new() }
26}
27
28impl<K, V> RangeMap<K, V> {
29    pub const EMPTY: Self = Self::new();
30
31    #[must_use]
32    #[inline]
33    pub const fn full(value: V) -> Self { Self(PartitionMap::new(Some(value))) }
34
35    #[must_use]
36    #[inline]
37    pub const fn new() -> Self { Self(PartitionMap::new(None)) }
38
39    #[must_use]
40    pub fn ranges(&self) -> Ranges<'_, K, V> { Ranges(self.0.partitions()) }
41
42    #[must_use]
43    pub fn into_ranges(self) -> IntoRanges<K, V> { IntoRanges(self.0.into_partitions()) }
44
45    #[inline]
46    #[must_use]
47    pub fn keys(&self) -> Keys<'_, K, V> { Keys(Ranges(self.0.partitions())) }
48
49    #[inline]
50    #[must_use]
51    pub fn values(&self) -> Values<'_, K, V> { Values(Ranges(self.0.partitions())) }
52}
53
54impl<K: Ord, V> RangeMap<K, V> {
55    #[inline]
56    pub fn contains<Q: ?Sized + Ord>(&self, at: &Q) -> bool
57    where K: Borrow<Q> {
58        self.0.sample(at).is_some()
59    }
60}
61
62impl<K: Copy + Ord, V> RangeMap<&K, V> {
63    #[inline]
64    pub fn copied_keys(self) -> RangeMap<K, V> {
65        let Self(m) = self;
66        RangeMap(m.copied_keys())
67    }
68}
69
70impl<K: Clone + Ord, V: Clone + PartialEq> RangeMap<K, V> {
71    #[inline]
72    pub fn insert<B: PartitionBounds<K>>(&mut self, range: B, value: V) {
73        self.0.set(range, Some(value));
74    }
75
76    #[inline]
77    pub fn remove<B: PartitionBounds<K>>(&mut self, range: B) { self.0.set(range, None); }
78}
79
80impl<K, V> ops::Deref for RangeMap<K, V> {
81    type Target = PartitionMap<K, Option<V>>;
82
83    #[inline]
84    fn deref(&self) -> &Self::Target { &self.0 }
85}
86
87impl<K, V> ops::DerefMut for RangeMap<K, V> {
88    #[inline]
89    fn deref_mut(&mut self) -> &mut Self::Target { &mut self.0 }
90}
91
92impl<K: Clone + Ord, V: Clone + PartialEq, B: PartitionBounds<K>> Extend<(B, V)>
93    for RangeMap<K, V>
94{
95    #[inline]
96    fn extend<I: IntoIterator<Item = (B, V)>>(&mut self, it: I) {
97        self.0.extend(it.into_iter().map(|(k, v)| (k, Some(v))));
98    }
99}
100
101impl<K: Clone + Ord, V: Clone + PartialEq, B: PartitionBounds<K>> FromIterator<(B, V)>
102    for RangeMap<K, V>
103{
104    #[inline]
105    fn from_iter<I: IntoIterator<Item = (B, V)>>(it: I) -> Self {
106        Self(it.into_iter().map(|(k, v)| (k, Some(v))).collect())
107    }
108}
109
110#[derive(Debug, Clone)]
111pub struct Ranges<'a, K, V>(Partitions<'a, K, Option<V>>);
112
113impl<'a, K, V> Iterator for Ranges<'a, K, V> {
114    type Item = (Partition<&'a K>, &'a V);
115
116    fn next(&mut self) -> Option<Self::Item> {
117        loop {
118            let (Partition { start, end }, value) = self.0.next()?;
119
120            if let Some(value) = value {
121                return Some((Partition { start, end }, value));
122            }
123        }
124    }
125}
126
127#[derive(Debug)]
128pub struct IntoRanges<K, V>(IntoPartitions<K, Option<V>>);
129
130impl<K: Clone, V> Iterator for IntoRanges<K, V> {
131    type Item = (Partition<K>, V);
132
133    fn next(&mut self) -> Option<Self::Item> {
134        loop {
135            let (Partition { start, end }, value) = self.0.next()?;
136
137            if let Some(value) = value {
138                return Some((Partition { start, end }, value));
139            }
140        }
141    }
142}
143
144#[derive(Debug, Clone)]
145pub struct Keys<'a, K, V>(Ranges<'a, K, V>);
146
147impl<'a, K, V> Iterator for Keys<'a, K, V> {
148    type Item = Partition<&'a K>;
149
150    #[inline]
151    fn next(&mut self) -> Option<Self::Item> { self.0.next().map(|(k, _)| k) }
152}
153
154#[derive(Debug, Clone)]
155pub struct Values<'a, K, V>(Ranges<'a, K, V>);
156
157impl<'a, K, V> Iterator for Values<'a, K, V> {
158    type Item = &'a V;
159
160    #[inline]
161    fn next(&mut self) -> Option<Self::Item> { self.0.next().map(|(_, v)| v) }
162}