range_set_blaze/map_from_iter.rs
1use alloc::rc::Rc;
2
3use crate::union_iter_map::UnionIterMap;
4use crate::{Integer, RangeMapBlaze};
5use core::ops::RangeInclusive;
6
7// We create a RangeMapBlaze from an iterator of integers or integer ranges by
8// 1. turning them into a UnionIterMap (internally, it collects into intervals and sorts by start).
9// 2. Turning the SortedDisjointMap into a BTreeMap.
10impl<'a, T, V> FromIterator<(T, &'a V)> for RangeMapBlaze<T, V>
11where
12 T: Integer,
13 V: Eq + Clone + 'a,
14{
15 /// Create a [`RangeMapBlaze`] from an iterator of integers. Duplicates and out-of-order elements are fine.
16 ///
17 /// In case of overlapping keys, the last (right-most) value overrides the previous ones.
18 ///
19 /// *For more about constructors and performance, see [`RangeMapBlaze` Constructors](struct.RangeMapBlaze.html#rangemapblaze-constructors).*
20 ///
21 /// # Examples
22 ///
23 /// ```
24 /// use range_set_blaze::RangeMapBlaze;
25 ///
26 /// let a0 = RangeMapBlaze::from_iter([(1, &"c"), (3, &"a"), (2, &"a"), (1, &"a"), (100, &"b")]);
27 /// let a1: RangeMapBlaze<i32, &str> = [ (1, &"c"), (3, &"a"), (2, &"a"), (1, &"a"), (100, &"b")].into_iter().collect();
28 /// assert!(a0 == a1 && a0.to_string() == r#"(1..=3, "a"), (100..=100, "b")"#);
29 /// ```
30 fn from_iter<I>(iter: I) -> Self
31 where
32 I: IntoIterator<Item = (T, &'a V)>,
33 {
34 iter.into_iter().map(|(x, r)| (x..=x, r)).collect()
35 }
36}
37
38impl<'a, T, V> FromIterator<(RangeInclusive<T>, &'a V)> for RangeMapBlaze<T, V>
39where
40 T: Integer,
41 V: Eq + Clone,
42{
43 /// Create a [`RangeMapBlaze`] from an iterator of inclusive ranges, `start..=end`.
44 /// Overlapping, out-of-order, and empty ranges are fine.
45 ///
46 /// In case of overlapping keys, the last (right-most) value overrides the previous ones.
47 ///
48 /// *For more about constructors and performance, see [`RangeMapBlaze` Constructors](struct.RangeMapBlaze.html#rangemapblaze-constructors).*
49 ///
50 /// # Examples
51 ///
52 /// ```
53 /// use range_set_blaze::prelude::*;
54 ///
55 /// #[allow(clippy::reversed_empty_ranges)]
56 /// let a0 = RangeMapBlaze::from_iter([(1..=0, &"d"), (-10..=-5, &"c"), (2..=2, &"b"), (1..=2, &"a")]);
57 /// #[allow(clippy::reversed_empty_ranges)]
58 /// let a1: RangeMapBlaze<i32, &str> = [(1..=0, &"d"), (-10..=-5, &"c"), (2..=2, &"b"), (1..=2, &"a")].into_iter().collect();
59 /// assert!(a0 == a1 && a0.to_string() == r#"(-10..=-5, "c"), (1..=2, "a")"#);
60 /// ```
61 fn from_iter<I>(iter: I) -> Self
62 where
63 I: IntoIterator<Item = (RangeInclusive<T>, &'a V)>,
64 {
65 let iter = iter.into_iter();
66 let union_iter_map: UnionIterMap<T, &V, _> = iter.collect();
67 Self::from_sorted_disjoint_map(union_iter_map)
68 }
69}
70
71impl<T: Integer, V: Eq + Clone> FromIterator<(RangeInclusive<T>, V)> for RangeMapBlaze<T, V> {
72 /// Create a [`RangeMapBlaze`] from an iterator of inclusive ranges, `start..=end`.
73 /// Overlapping, out-of-order, and empty ranges are fine.
74 ///
75 /// In case of overlapping ranges, the last (right-most) value overrides the previous ones.
76 ///
77 /// *For more about constructors and performance, see [`RangeMapBlaze` Constructors](struct.RangeMapBlaze.html#rangemapblaze-constructors).*
78 ///
79 /// # Examples
80 ///
81 /// ```
82 /// use range_set_blaze::RangeMapBlaze;
83 ///
84 /// #[allow(clippy::reversed_empty_ranges)]
85 /// let a0 = RangeMapBlaze::from_iter([(2..=2, "b"), (1..=2, "a"), (-10..=-5, "c"), (1..=0, "d")]);
86 /// #[allow(clippy::reversed_empty_ranges)]
87 /// let a1: RangeMapBlaze<i32, &str> = [(2..=2, "b"), (1..=2, "a"), (-10..=-5, "c"), (1..=0, "d")].into_iter().collect();
88 /// assert!(a0 == a1 && a0.to_string() == r#"(-10..=-5, "c"), (1..=2, "a")"#);
89 /// ```
90 fn from_iter<I>(iter: I) -> Self
91 where
92 I: IntoIterator<Item = (RangeInclusive<T>, V)>,
93 {
94 let union_iter_map = iter
95 .into_iter()
96 .map(|(r, v)| (r, Rc::new(v)))
97 .collect::<UnionIterMap<T, Rc<V>, _>>();
98 Self::from_sorted_disjoint_map(union_iter_map)
99 }
100}
101
102impl<T: Integer, V: Eq + Clone> FromIterator<(T, V)> for RangeMapBlaze<T, V> {
103 /// Create a [`RangeMapBlaze`] from an iterator of pairs (integer, value).
104 /// Overlapping, out-of-order, and empty ranges are fine.
105 ///
106 /// In case of overlapping keys, the last (right-most) value overrides the previous ones.
107 ///
108 /// *For more about constructors and performance, see [`RangeMapBlaze` Constructors](struct.RangeMapBlaze.html#rangemapblaze-constructors).*
109 ///
110 /// # Examples
111 ///
112 /// ```
113 /// use range_set_blaze::prelude::*;
114 ///
115 /// #[allow(clippy::reversed_empty_ranges)]
116 /// let a0 = RangeMapBlaze::from_iter([(1, "c"), (3, "a"), (2, "a"), (1, "a"), (100, "b")]);
117 /// let a1: RangeMapBlaze<i32, &str> = [(1, "c"), (3, "a"), (2, "a"), (1, "a"), (100, "b")].into_iter().collect();
118 /// assert!(a0 == a1 && a0.to_string() == r#"(1..=3, "a"), (100..=100, "b")"#);
119 /// ```
120 fn from_iter<I>(iter: I) -> Self
121 where
122 I: IntoIterator<Item = (T, V)>,
123 {
124 iter.into_iter().map(|(k, v)| (k..=k, v)).collect()
125 }
126}
127
128impl<'a, T, V> FromIterator<&'a (T, &'a V)> for RangeMapBlaze<T, V>
129where
130 T: Integer,
131 V: Eq + Clone + 'a,
132{
133 /// Create a [`RangeMapBlaze`] from an iterator of integers. Duplicates and out-of-order elements are fine.
134 ///
135 /// In case of overlapping keys, the last (right-most) value overrides the previous ones.
136 ///
137 /// *For more about constructors and performance, see [`RangeMapBlaze` Constructors](struct.RangeMapBlaze.html#rangemapblaze-constructors).*
138 ///
139 /// # Examples
140 ///
141 /// ```
142 /// use range_set_blaze::RangeMapBlaze;
143 ///
144 /// let v = vec![(1, &"c"), (100, &"b"), (1, &"a"), (2, &"a"), (3, &"a")];
145 /// let a0 = RangeMapBlaze::from_iter(&v);
146 /// let a1: RangeMapBlaze<i32, &str> = (&v).into_iter().collect();
147 /// assert!(a0 == a1 && a0.to_string() == r#"(1..=3, "a"), (100..=100, "b")"#);
148 /// ```
149 fn from_iter<I>(iter: I) -> Self
150 where
151 I: IntoIterator<Item = &'a (T, &'a V)>,
152 {
153 iter.into_iter().map(|&(x, r)| (x..=x, r)).collect()
154 }
155}
156
157impl<'a, T, V> FromIterator<&'a (RangeInclusive<T>, &'a V)> for RangeMapBlaze<T, V>
158where
159 T: Integer,
160 V: Eq + Clone,
161{
162 /// Create a [`RangeMapBlaze`] from an iterator of inclusive ranges, `start..=end`.
163 /// Overlapping, out-of-order, and empty ranges are fine.
164 ///
165 /// In case of overlapping ranges, the last (right-most) value overrides the previous ones.
166 ///
167 /// /// *For more about constructors and performance, see [`RangeMapBlaze` Constructors](struct.RangeMapBlaze.html#rangemapblaze-constructors).*
168 ///
169 /// # Examples
170 ///
171 /// ```
172 /// use range_set_blaze::prelude::*;
173 ///
174 /// let v = vec![(1..=0, &"d"), (-10..=-5, &"c"), (2..=2, &"b"), (1..=2, &"a")];
175 /// let a0: RangeMapBlaze<i32, &str> = RangeMapBlaze::from_iter(&v);
176 /// let a1: RangeMapBlaze<i32, &str> = (&v).into_iter().collect();
177 /// assert!(a0 == a1 && a0.to_string() == r#"(-10..=-5, "c"), (1..=2, "a")"#);
178 /// ```
179 fn from_iter<I>(iter: I) -> Self
180 where
181 I: IntoIterator<Item = &'a (RangeInclusive<T>, &'a V)>,
182 {
183 iter.into_iter().map(|(r, v)| (r.clone(), *v)).collect()
184 }
185}
186
187impl<'a, T: Integer, V: Eq + Clone> FromIterator<&'a (RangeInclusive<T>, V)>
188 for RangeMapBlaze<T, V>
189{
190 /// Create a [`RangeMapBlaze`] from an iterator of inclusive ranges, `start..=end`.
191 /// Overlapping, out-of-order, and empty ranges are fine.
192 ///
193 /// In case of overlapping ranges, the last (right-most) value overrides the previous ones.
194 ///
195 /// /// *For more about constructors and performance, see [`RangeMapBlaze` Constructors](struct.RangeMapBlaze.html#rangemapblaze-constructors).*
196 ///
197 /// # Examples
198 ///
199 /// ```
200 /// use range_set_blaze::prelude::*;
201 ///
202 /// #[allow(clippy::reversed_empty_ranges)]
203 /// let vec_range = vec![(1..=0, "d"), (-10..=-5, "c"), (2..=2, "b"), (1..=2, "a") ];
204 /// let a0 = RangeMapBlaze::from_iter(&vec_range);
205 /// let a1: RangeMapBlaze<i32, &str> = vec_range.iter().collect();
206 /// assert!(a0 == a1 && a0.to_string() == r#"(-10..=-5, "c"), (1..=2, "a")"#);
207 /// ```
208 fn from_iter<I>(iter: I) -> Self
209 where
210 I: IntoIterator<Item = &'a (RangeInclusive<T>, V)>,
211 {
212 iter.into_iter().map(|(r, v)| (r.clone(), v)).collect()
213 }
214}
215
216impl<'a, T: Integer, V: Eq + Clone> FromIterator<&'a (T, V)> for RangeMapBlaze<T, V> {
217 /// Create a [`RangeMapBlaze`] from an iterator of inclusive ranges, `start..=end`.
218 /// Overlapping, out-of-order, and empty ranges are fine.
219 ///
220 /// In case of overlapping keys, the last (right-most) value overrides the previous ones.
221 ///
222 /// *For more about constructors and performance, see [`RangeMapBlaze` Constructors](struct.RangeMapBlaze.html#rangemapblaze-constructors).*
223 ///
224 /// # Examples
225 ///
226 /// ```
227 /// use range_set_blaze::prelude::*;
228 ///
229 /// let v = vec![(2, "b"), (2, "a"), (1, "a")];
230 /// let a0 = RangeMapBlaze::from_iter(&v);
231 /// let a1: RangeMapBlaze<i32, &str> = v.iter().collect();
232 /// assert!(a0 == a1 && a0.to_string() == r#"(1..=2, "a")"#);
233 /// ```
234 fn from_iter<I>(iter: I) -> Self
235 where
236 I: IntoIterator<Item = &'a (T, V)>,
237 {
238 iter.into_iter()
239 .map(|(k, v)| {
240 let k = *k;
241 (k..=k, v)
242 })
243 .collect()
244 }
245}