nodit/nodit/
map.rs

1//! A module containing [`NoditMap`].
2
3use alloc::vec::Vec;
4use core::marker::PhantomData;
5
6use btree_monstrousity::btree_map::{
7	IntoIter as BTreeMapIntoIter, SearchBoundCustom,
8};
9use btree_monstrousity::BTreeMap;
10use itertools::Itertools;
11
12use crate::utils::{
13	cut_interval, invalid_interval_panic, overlapping_comp, starts_comp,
14	touching_end_comp, touching_start_comp,
15};
16use crate::{DiscreteFinite, InclusiveInterval, Interval};
17
18/// An ordered map of non-overlapping intervals based on [`BTreeMap`].
19///
20/// `I` is the generic type parameter for the [`Ord`] type the `K`
21/// type is a interval over.
22///
23/// `K` is the generic type parameter for the interval type stored as the
24/// keys in the map.
25///
26/// `V` is the generic type parameter for the values associated with the
27/// keys in the map.
28///
29/// Phrasing it another way: `I` is the point type, `K` is the interval type, and `V` is the value type.
30///
31/// # Examples
32/// ```
33/// use nodit::interval::ie;
34/// use nodit::NoditMap;
35///
36/// // Make a map of intervals to booleans
37/// let mut map = NoditMap::from_slice_strict([
38/// 	(ie(4, 8), false),
39/// 	(ie(8, 18), true),
40/// 	(ie(20, 100), false),
41/// ])
42/// .unwrap();
43///
44/// // Change a value in the map
45/// *map.get_at_point_mut(7).unwrap() = true;
46///
47/// if map.contains_point(99) {
48/// 	println!("Map contains value at 99 :)");
49/// }
50///
51/// // Iterate over the entries in the map
52/// for (interval, value) in map.iter() {
53/// 	println!("{interval:?}, {value:?}");
54/// }
55/// ```
56///
57/// [`BTreeMap`]: https://doc.rust-lang.org/std/collections/struct.BTreeMap.html
58#[derive(Debug, Clone, PartialEq, Eq)]
59pub struct NoditMap<I, K, V> {
60	pub(crate) inner: BTreeMap<K, V>,
61	phantom: PhantomData<I>,
62}
63
64/// The error returned when inserting a interval that overlaps another interval when
65/// it should not have. Contains the value that was not inserted.
66#[derive(PartialEq, Debug)]
67pub struct OverlapError<V> {
68	/// The value which was not inserted, because of the overlap error.
69	pub value: V,
70}
71
72/// The marker trait for valid point types, a blanket implementation is provided for all types
73/// which implement this traits' super-traits so you shouln't need to implement this yourself.
74pub trait PointType: Ord + Copy + DiscreteFinite {}
75impl<I> PointType for I where I: Ord + Copy + DiscreteFinite {}
76
77/// The marker trait for valid interval types, a blanket implementation is provided for all types
78/// which implement this traits' super-traits so you shouln't need to implement this yourself.
79pub trait IntervalType<I>: InclusiveInterval<I> {}
80impl<I, K> IntervalType<I> for K
81where
82	I: PointType,
83	K: InclusiveInterval<I> + Copy + From<Interval<I>>,
84{
85}
86
87impl<I, K, V> NoditMap<I, K, V>
88where
89	I: PointType,
90	K: IntervalType<I>,
91{
92	/// Returns `true` if the given interval overlaps any of the
93	/// intervals in the map, and `false` if not.
94	///
95	/// # Panics
96	///
97	/// Panics if the given interval is an invalid interval. See [`Invalid
98	/// Intervals`](https://docs.rs/nodit/latest/nodit/index.html#invalid-intervals)
99	/// for more details.
100	///
101	/// # Examples
102	/// ```
103	/// use nodit::interval::{ie, ii};
104	/// use nodit::NoditMap;
105	///
106	/// let mut map = NoditMap::new();
107	///
108	/// map.insert_strict(ie(5, 10), false);
109	///
110	/// assert_eq!(map.overlaps(ii(1, 3)), false);
111	/// assert_eq!(map.overlaps(ie(4, 5)), false);
112	///
113	/// assert_eq!(map.overlaps(ii(4, 5)), true);
114	/// assert_eq!(map.overlaps(ie(4, 6)), true);
115	/// ```
116	pub fn overlaps<Q>(&self, interval: Q) -> bool
117	where
118		Q: IntervalType<I>,
119	{
120		invalid_interval_panic(interval);
121
122		self.overlapping(interval).next().is_some()
123	}
124
125	/// Returns an iterator over every entry in the map that overlaps
126	/// the given interval in ascending order.
127	///
128	/// # Panics
129	///
130	/// Panics if the given interval is an invalid interval. See [`Invalid
131	/// Intervals`](https://docs.rs/nodit/latest/nodit/index.html#invalid-intervals)
132	/// for more details.
133	///
134	/// # Examples
135	/// ```
136	/// use nodit::interval::ie;
137	/// use nodit::NoditMap;
138	///
139	/// let map = NoditMap::from_slice_strict([
140	/// 	(ie(1, 4), false),
141	/// 	(ie(4, 8), true),
142	/// 	(ie(8, 100), false),
143	/// ])
144	/// .unwrap();
145	///
146	/// let mut overlapping = map.overlapping(ie(2, 8));
147	///
148	/// assert_eq!(
149	/// 	overlapping.collect::<Vec<_>>(),
150	/// 	[(&ie(1, 4), &false), (&ie(4, 8), &true)]
151	/// );
152	/// ```
153	pub fn overlapping<Q>(
154		&self,
155		interval: Q,
156	) -> impl DoubleEndedIterator<Item = (&K, &V)>
157	where
158		Q: IntervalType<I>,
159	{
160		invalid_interval_panic(interval);
161
162		self.inner.range(
163			overlapping_comp(interval.start()),
164			SearchBoundCustom::Included,
165			overlapping_comp(interval.end()),
166			SearchBoundCustom::Included,
167		)
168	}
169
170	/// Returns an mutable iterator over every entry in the map that
171	/// overlaps the given interval in ascending order.
172	///
173	/// # Panics
174	///
175	/// Panics if the given interval is an invalid interval. See [`Invalid
176	/// Intervals`](https://docs.rs/nodit/latest/nodit/index.html#invalid-intervals)
177	/// for more details.
178	///
179	/// # Examples
180	/// ```
181	/// use nodit::interval::ie;
182	/// use nodit::NoditMap;
183	///
184	/// let mut map = NoditMap::from_slice_strict([
185	/// 	(ie(1, 4), false),
186	/// 	(ie(4, 8), true),
187	/// 	(ie(8, 100), false),
188	/// ])
189	/// .unwrap();
190	///
191	/// for (interval, value) in map.overlapping_mut(ie(3, 7)) {
192	/// 	if *interval == ie(4, 8) {
193	/// 		*value = false
194	/// 	} else {
195	/// 		*value = true
196	/// 	}
197	/// }
198	/// ```
199	pub fn overlapping_mut<Q>(
200		&mut self,
201		interval: Q,
202	) -> impl DoubleEndedIterator<Item = (&K, &mut V)>
203	where
204		Q: IntervalType<I>,
205	{
206		invalid_interval_panic(interval);
207
208		self.inner.range_mut(
209			overlapping_comp(interval.start()),
210			SearchBoundCustom::Included,
211			overlapping_comp(interval.end()),
212			SearchBoundCustom::Included,
213		)
214	}
215
216	/// Returns a reference to the value corresponding to the interval in
217	/// the map that overlaps the given point, if any.
218	///
219	/// # Examples
220	/// ```
221	/// use nodit::interval::ie;
222	/// use nodit::NoditMap;
223	///
224	/// let map = NoditMap::from_slice_strict([
225	/// 	(ie(1, 4), false),
226	/// 	(ie(4, 8), true),
227	/// 	(ie(8, 100), false),
228	/// ])
229	/// .unwrap();
230	///
231	/// assert_eq!(map.get_at_point(3), Some(&false));
232	/// assert_eq!(map.get_at_point(4), Some(&true));
233	/// assert_eq!(map.get_at_point(101), None);
234	/// ```
235	pub fn get_at_point(&self, point: I) -> Option<&V> {
236		self.get_key_value_at_point(point)
237			.map(|(_, value)| value)
238			.ok()
239	}
240
241	/// Returns a mutable reference to the value corresponding to the
242	/// interval that overlaps the given point, if any.
243	///
244	/// # Examples
245	/// ```
246	/// use nodit::interval::ie;
247	/// use nodit::NoditMap;
248	/// let mut map =
249	/// 	NoditMap::from_slice_strict([(ie(1, 4), false)]).unwrap();
250	///
251	/// if let Some(x) = map.get_at_point_mut(2) {
252	/// 	*x = true;
253	/// }
254	///
255	/// assert_eq!(map.get_at_point(1), Some(&true));
256	/// ```
257	pub fn get_at_point_mut(&mut self, point: I) -> Option<&mut V> {
258		self.inner.get_mut(overlapping_comp(point))
259	}
260
261	/// Returns `true` if the map contains a interval that overlaps the
262	/// given point, and `false` if not.
263	///
264	/// # Examples
265	/// ```
266	/// use nodit::interval::ie;
267	/// use nodit::NoditMap;
268	///
269	/// let map = NoditMap::from_slice_strict([
270	/// 	(ie(1, 4), false),
271	/// 	(ie(4, 8), true),
272	/// 	(ie(8, 100), false),
273	/// ])
274	/// .unwrap();
275	///
276	/// assert_eq!(map.contains_point(3), true);
277	/// assert_eq!(map.contains_point(4), true);
278	/// assert_eq!(map.contains_point(101), false);
279	/// ```
280	pub fn contains_point(&self, point: I) -> bool {
281		self.get_key_value_at_point(point).is_ok()
282	}
283
284	/// Returns the entry corresponding to the interval that
285	/// overlaps the given point, if any.
286	///
287	/// If there is no interval that overlaps the given point the
288	/// maximally-sized gap at the given point is returned.
289	///
290	/// # Examples
291	/// ```
292	/// use nodit::interval::{ie, iu};
293	/// use nodit::NoditMap;
294	///
295	/// let map = NoditMap::from_slice_strict([
296	/// 	(ie(1, 4), false),
297	/// 	(ie(4, 6), true),
298	/// 	(ie(8, 100), false),
299	/// ])
300	/// .unwrap();
301	///
302	/// assert_eq!(
303	/// 	map.get_key_value_at_point(3),
304	/// 	Ok((&ie(1, 4), &false))
305	/// );
306	/// assert_eq!(map.get_key_value_at_point(5), Ok((&ie(4, 6), &true)));
307	/// assert_eq!(map.get_key_value_at_point(7), Err(ie(6, 8)));
308	/// assert_eq!(map.get_key_value_at_point(101), Err(iu(100)));
309	/// ```
310	pub fn get_key_value_at_point(&self, point: I) -> Result<(&K, &V), K> {
311		self.inner
312			.get_key_value(overlapping_comp(point))
313			.ok_or_else(|| K::from(self.get_gap_at_raw(point)))
314	}
315	fn get_gap_at_raw(&self, point: I) -> Interval<I> {
316		let lower = self
317			.inner
318			.upper_bound(overlapping_comp(point), SearchBoundCustom::Included);
319		let upper = self
320			.inner
321			.lower_bound(overlapping_comp(point), SearchBoundCustom::Included);
322
323		Interval {
324			start: lower
325				.key()
326				.map_or(I::MIN, |lower| lower.end().up().unwrap()),
327			end: upper
328				.key()
329				.map_or(I::MAX, |upper| upper.start().down().unwrap()),
330		}
331	}
332
333	/// Removes every entry in the map which overlaps the given interval
334	/// and returns them in an iterator in ascending order.
335	///
336	/// # Panics
337	///
338	/// Panics if the given interval is an invalid interval. See [`Invalid
339	/// Intervals`](https://docs.rs/nodit/latest/nodit/index.html#invalid-intervals)
340	/// for more details.
341	///
342	/// # Examples
343	/// ```
344	/// use nodit::interval::ie;
345	/// use nodit::NoditMap;
346	///
347	/// let mut map = NoditMap::from_slice_strict([
348	/// 	(ie(1, 4), false),
349	/// 	(ie(4, 8), true),
350	/// 	(ie(8, 100), false),
351	/// ])
352	/// .unwrap();
353	///
354	/// let mut removed = map.remove_overlapping(ie(2, 8));
355	///
356	/// assert_eq!(
357	/// 	removed.collect::<Vec<_>>(),
358	/// 	[(ie(1, 4), false), (ie(4, 8), true)]
359	/// );
360	///
361	/// assert_eq!(
362	/// 	map.into_iter().collect::<Vec<_>>(),
363	/// 	[(ie(8, 100), false)]
364	/// );
365	/// ```
366	pub fn remove_overlapping<'a, Q>(
367		&'a mut self,
368		interval: Q,
369	) -> impl Iterator<Item = (K, V)>
370	where
371		Q: IntervalType<I> + 'a,
372	{
373		invalid_interval_panic(interval);
374
375		let mut result = Vec::new();
376
377		let mut cursor = self.inner.lower_bound_mut(
378			overlapping_comp(interval.start()),
379			SearchBoundCustom::Included,
380		);
381
382		while cursor
383			.key()
384			.is_some_and(|inner_interval| interval.overlaps(inner_interval))
385		{
386			result.push(cursor.remove_current().unwrap());
387		}
388
389		return result.into_iter();
390	}
391
392	/// Cuts a given interval out of the map and returns an iterator of the full or
393	/// partial intervals with their values that were cut in ascending order.
394	///
395	/// `V` must implement `Clone` as if you try to cut out the center
396	/// of a interval in the map it will split into two different entries
397	/// using `Clone`. Or if you partially cut a interval then
398	/// `V` must be cloned to be returned in the iterator.
399	///
400	/// # Panics
401	///
402	/// Panics if the given interval is an invalid interval. See [`Invalid
403	/// Intervals`](https://docs.rs/nodit/latest/nodit/index.html#invalid-intervals)
404	/// for more details.
405	///
406	/// # Examples
407	/// ```
408	/// use nodit::interval::{ie, ii};
409	/// use nodit::NoditMap;
410	///
411	/// let mut base = NoditMap::from_slice_strict([
412	/// 	(ie(1, 4), false),
413	/// 	(ie(4, 8), true),
414	/// 	(ie(8, 100), false),
415	/// ])
416	/// .unwrap();
417	///
418	/// let after_cut = NoditMap::from_slice_strict([
419	/// 	(ie(1, 2), false),
420	/// 	(ie(40, 100), false),
421	/// ])
422	/// .unwrap();
423	///
424	/// assert_eq!(
425	/// 	base.cut(ie(2, 40)).collect::<Vec<_>>(),
426	/// 	[(ie(2, 4), false), (ie(4, 8), true), (ie(8, 40), false),]
427	/// );
428	/// assert_eq!(base, after_cut);
429	/// ```
430	pub fn cut<'a, Q>(&'a mut self, interval: Q) -> impl Iterator<Item = (K, V)>
431	where
432		Q: IntervalType<I> + 'a,
433		V: Clone,
434	{
435		invalid_interval_panic(interval);
436
437		let mut result = Vec::new();
438
439		let mut cursor = self.inner.lower_bound_mut(
440			overlapping_comp(interval.start()),
441			SearchBoundCustom::Included,
442		);
443
444		while let Some(key) = cursor.key() {
445			if !key.overlaps(&interval) {
446				break;
447			}
448
449			let (key, value) = cursor.remove_current().unwrap();
450
451			let cut_result = cut_interval(key, interval);
452
453			if let Some(before_cut) = cut_result.before_cut {
454				cursor.insert_before(K::from(before_cut), value.clone());
455			}
456			if let Some(after_cut) = cut_result.after_cut {
457				cursor.insert_before(K::from(after_cut), value.clone());
458			}
459
460			result.push((K::from(cut_result.inside_cut.unwrap()), value));
461		}
462
463		result.into_iter()
464	}
465
466	/// Returns an iterator of all the gaps in the map that overlap the given
467	/// `interval` in ascending order.
468	///
469	/// See [`NoditMap::gaps_trimmed()`] if you require the returned
470	/// gaps to be trimmed to be fully contained within given `interval`.
471	///
472	/// # Panics
473	///
474	/// Panics if the given interval is an invalid interval. See [`Invalid
475	/// Intervals`](https://docs.rs/nodit/latest/nodit/index.html#invalid-intervals)
476	/// for more details.
477	///
478	/// # Examples
479	/// ```
480	/// use nodit::interval::{ie, ii, iu};
481	/// use nodit::NoditMap;
482	///
483	/// let map = NoditMap::from_slice_strict([
484	/// 	(ie(1, 3), false),
485	/// 	(ie(5, 7), true),
486	/// 	(ie(9, 100), false),
487	/// ])
488	/// .unwrap();
489	///
490	/// let mut gaps = map.gaps_untrimmed(ii(4, 120));
491	///
492	/// assert_eq!(
493	/// 	gaps.collect::<Vec<_>>(),
494	/// 	[ie(3, 5), ie(7, 9), iu(100)]
495	/// );
496	/// ```
497	pub fn gaps_untrimmed<'a, Q>(
498		&'a self,
499		interval: Q,
500	) -> impl Iterator<Item = K> + '_
501	where
502		Q: IntervalType<I> + 'a,
503	{
504		invalid_interval_panic(interval);
505
506		// If the start or end point of interval is not
507		// contained within a interval in the map then we need to
508		// generate the gaps.
509		let start_gap =
510			(!self.inner.contains_key(overlapping_comp(interval.start())))
511				.then(|| self.get_gap_at_raw(interval.start()));
512		let end_gap =
513			(!self.inner.contains_key(overlapping_comp(interval.end())))
514				.then(|| self.get_gap_at_raw(interval.end()));
515
516		let (start_gap, end_gap) = match (start_gap, end_gap) {
517			(Some(start_gap), Some(end_gap)) => {
518				if start_gap.start() == end_gap.start() {
519					//it's the same gap
520					(Some(start_gap), None)
521				} else {
522					//it's different gaps
523					(Some(start_gap), Some(end_gap))
524				}
525			}
526			(Some(start_gap), None) => (Some(start_gap), None),
527			(None, Some(end_gap)) => (None, Some(end_gap)),
528			(None, None) => (None, None),
529		};
530
531		let overlapping = self
532			.overlapping(interval)
533			.map(|(key, _)| (key.start(), key.end()));
534
535		let inner_gaps = overlapping
536			.tuple_windows()
537			.map(|(first, second)| {
538				K::from(Interval {
539					start: first.1.up().unwrap(),
540					end: second.0.down().unwrap(),
541				})
542			})
543			.filter(|interval| interval.is_valid());
544
545		//possibly add the trimmed start and end gaps
546		start_gap
547			.map(K::from)
548			.into_iter()
549			.chain(inner_gaps)
550			.chain(end_gap.map(K::from))
551	}
552
553	/// Returns an iterator of all the gaps in the map that overlap the given
554	/// `interval` that are also trimmed so they are all fully contained within the
555	/// given `interval`, in ascending order.
556	///
557	/// See [`NoditMap::gaps_untrimmed()`] if you do not want the
558	/// returned gaps to be trimmed.
559	///
560	/// # Panics
561	///
562	/// Panics if the given interval is an invalid interval. See [`Invalid
563	/// Intervals`](https://docs.rs/nodit/latest/nodit/index.html#invalid-intervals)
564	/// for more details.
565	///
566	/// # Examples
567	/// ```
568	/// use nodit::interval::{ie, ii, iu};
569	/// use nodit::NoditMap;
570	///
571	/// let map = NoditMap::from_slice_strict([
572	/// 	(ie(1, 3), false),
573	/// 	(ie(5, 7), true),
574	/// 	(ie(9, 100), false),
575	/// ])
576	/// .unwrap();
577	///
578	/// let mut gaps = map.gaps_trimmed(ii(4, 120));
579	///
580	/// assert_eq!(
581	/// 	gaps.collect::<Vec<_>>(),
582	/// 	[ie(4, 5), ie(7, 9), ii(100, 120)]
583	/// );
584	/// ```
585	pub fn gaps_trimmed<'a, Q>(
586		&'a self,
587		interval: Q,
588	) -> impl Iterator<Item = K> + '_
589	where
590		Q: IntervalType<I> + 'a,
591	{
592		invalid_interval_panic(interval);
593
594		// If the start or end point of interval is not
595		// contained within a interval in the map then we need to
596		// generate the gaps.
597		let start_gap =
598			(!self.inner.contains_key(overlapping_comp(interval.start())))
599				.then(|| self.get_gap_at_raw(interval.start()));
600		let end_gap =
601			(!self.inner.contains_key(overlapping_comp(interval.end())))
602				.then(|| self.get_gap_at_raw(interval.end()));
603
604		let (trimmed_start_gap, trimmed_end_gap) = match (start_gap, end_gap) {
605			(Some(mut start_gap), Some(mut end_gap)) => {
606				if start_gap.start() == end_gap.start() {
607					//it's the same gap
608					start_gap.start = interval.start();
609					start_gap.end = interval.end();
610
611					(Some(start_gap), None)
612				} else {
613					//it's different gaps
614					start_gap.start = interval.start();
615					end_gap.end = interval.end();
616
617					(Some(start_gap), Some(end_gap))
618				}
619			}
620			(Some(mut start_gap), None) => {
621				start_gap.start = interval.start();
622				(Some(start_gap), None)
623			}
624			(None, Some(mut end_gap)) => {
625				end_gap.end = interval.end();
626				(None, Some(end_gap))
627			}
628			(None, None) => (None, None),
629		};
630
631		let overlapping = self
632			.overlapping(interval)
633			.map(|(key, _)| (key.start(), key.end()));
634
635		let inner_gaps = overlapping
636			.tuple_windows()
637			.map(|(first, second)| {
638				K::from(Interval {
639					start: first.1.up().unwrap(),
640					end: second.0.down().unwrap(),
641				})
642			})
643			.filter(|interval| interval.is_valid());
644
645		//possibly add the trimmed start and end gaps
646		trimmed_start_gap
647			.map(K::from)
648			.into_iter()
649			.chain(inner_gaps)
650			.chain(trimmed_end_gap.map(K::from))
651	}
652
653	/// Returns `true` if the map covers every point in the given
654	/// interval, and `false` if it does not.
655	///
656	/// # Panics
657	///
658	/// Panics if the given interval is an invalid interval. See [`Invalid
659	/// Intervals`](https://docs.rs/nodit/latest/nodit/index.html#invalid-intervals)
660	/// for more details.
661	///
662	/// # Examples
663	/// ```
664	/// use nodit::interval::ie;
665	/// use nodit::NoditMap;
666	///
667	/// let map = NoditMap::from_slice_strict([
668	/// 	(ie(1, 3), false),
669	/// 	(ie(5, 8), true),
670	/// 	(ie(8, 100), false),
671	/// ])
672	/// .unwrap();
673	///
674	/// assert_eq!(map.contains_interval(ie(1, 3)), true);
675	/// assert_eq!(map.contains_interval(ie(2, 6)), false);
676	/// assert_eq!(map.contains_interval(ie(6, 100)), true);
677	/// ```
678	pub fn contains_interval<Q>(&self, interval: Q) -> bool
679	where
680		Q: IntervalType<I>,
681	{
682		invalid_interval_panic(interval);
683
684		// Soooo clean and mathematical!
685		self.gaps_untrimmed(interval).next().is_none()
686	}
687
688	/// Adds a new entry to the map without modifying other entries.
689	///
690	/// If the given interval overlaps one or more intervals already in the
691	/// map, then an [`OverlapError`] is returned and the map is not
692	/// updated.
693	///
694	/// # Panics
695	///
696	/// Panics if the given interval is an invalid interval. See [`Invalid
697	/// Intervals`](https://docs.rs/nodit/latest/nodit/index.html#invalid-intervals)
698	/// for more details.
699	///
700	/// # Examples
701	/// ```
702	/// use nodit::interval::ie;
703	/// use nodit::{NoditMap, OverlapError};
704	///
705	/// let mut map = NoditMap::new();
706	///
707	/// assert_eq!(map.insert_strict(ie(5, 10), 9), Ok(()));
708	/// assert_eq!(
709	/// 	map.insert_strict(ie(5, 10), 2),
710	/// 	Err(OverlapError { value: 2 })
711	/// );
712	/// assert_eq!(map.len(), 1);
713	/// ```
714	pub fn insert_strict(
715		&mut self,
716		interval: K,
717		value: V,
718	) -> Result<(), OverlapError<V>> {
719		invalid_interval_panic(interval);
720
721		if self.overlaps(interval) {
722			return Err(OverlapError { value });
723		}
724
725		self.insert_unchecked(interval, value);
726
727		Ok(())
728	}
729	fn insert_unchecked(&mut self, interval: K, value: V) {
730		self.inner.insert(interval, value, starts_comp());
731	}
732
733	fn insert_merge_with_comps<G1, G2, R1, R2>(
734		&mut self,
735		interval: K,
736		value: V,
737		get_start: G1,
738		get_end: G2,
739		remove_start: R1,
740		remove_end: R2,
741	) -> K
742	where
743		G1: FnOnce(&Self, &V) -> Option<K>,
744		G2: FnOnce(&Self, &V) -> Option<K>,
745		R1: FnOnce(&mut Self, &V),
746		R2: FnOnce(&mut Self, &V),
747	{
748		invalid_interval_panic(interval);
749
750		let matching_start = get_start(self, &value);
751		let matching_end = get_end(self, &value);
752
753		let returning = match (matching_start, matching_end) {
754			(Some(matching_start), Some(matching_end)) => K::from(Interval {
755				start: matching_start.start(),
756				end: matching_end.end(),
757			}),
758			(Some(matching_start), None) => K::from(Interval {
759				start: matching_start.start(),
760				end: interval.end(),
761			}),
762			(None, Some(matching_end)) => K::from(Interval {
763				start: interval.start(),
764				end: matching_end.end(),
765			}),
766			(None, None) => interval,
767		};
768
769		let _ = self.remove_overlapping(interval);
770
771		remove_start(self, &value);
772		remove_end(self, &value);
773
774		self.insert_unchecked(returning, value);
775
776		returning
777	}
778
779	/// Adds a new entry to the map and merges into other intervals in
780	/// the map which touch it.
781	///
782	/// The value of the merged-together interval is set to the value given for
783	/// this insertion.
784	///
785	/// If successful then the newly inserted (possibly merged) interval is
786	/// returned.
787	///
788	/// If the given interval overlaps one or more intervals already in the
789	/// map, then an [`OverlapError`] is returned and the map is not
790	/// updated.
791	///
792	/// # Panics
793	///
794	/// Panics if the given interval is an invalid interval. See [`Invalid
795	/// Intervals`](https://docs.rs/nodit/latest/nodit/index.html#invalid-intervals)
796	/// for more details.
797	///
798	/// # Examples
799	/// ```
800	/// use nodit::interval::ie;
801	/// use nodit::{NoditMap, OverlapError};
802	///
803	/// let mut map = NoditMap::from_slice_strict([
804	/// 	(ie(1, 4), false),
805	/// 	(ie(6, 8), true),
806	/// ])
807	/// .unwrap();
808	///
809	/// // Touching
810	/// assert_eq!(
811	/// 	map.insert_merge_touching(ie(4, 6), true),
812	/// 	Ok(ie(1, 8))
813	/// );
814	///
815	/// // Overlapping
816	/// assert_eq!(
817	/// 	map.insert_merge_touching(ie(4, 8), false),
818	/// 	Err(OverlapError { value: false }),
819	/// );
820	///
821	/// // Neither Touching or Overlapping
822	/// assert_eq!(
823	/// 	map.insert_merge_touching(ie(10, 16), false),
824	/// 	Ok(ie(10, 16))
825	/// );
826	///
827	/// assert_eq!(
828	/// 	map.into_iter().collect::<Vec<_>>(),
829	/// 	[(ie(1, 8), true), (ie(10, 16), false)]
830	/// );
831	/// ```
832	pub fn insert_merge_touching(
833		&mut self,
834		interval: K,
835		value: V,
836	) -> Result<K, OverlapError<V>> {
837		invalid_interval_panic(interval);
838
839		if self.overlaps(interval) {
840			return Err(OverlapError { value });
841		}
842
843		Ok(self.insert_merge_with_comps(
844			interval,
845			value,
846			|selfy, _| {
847				selfy
848					.inner
849					.get_key_value(touching_start_comp(interval.start()))
850					.map(|(key, _)| key)
851					.copied()
852			},
853			|selfy, _| {
854				selfy
855					.inner
856					.get_key_value(touching_end_comp(interval.end()))
857					.map(|(key, _)| key)
858					.copied()
859			},
860			|selfy, _| {
861				selfy.inner.remove(touching_start_comp(interval.start()));
862			},
863			|selfy, _| {
864				selfy.inner.remove(touching_end_comp(interval.end()));
865			},
866		))
867	}
868
869	/// Adds a new entry to the map and merges into other intervals in
870	/// the map which touch it if the touching intervals' values are
871	/// equal to the value being inserted.
872	///
873	/// If successful then the newly inserted (possibly merged) interval is
874	/// returned.
875	///
876	/// If the given interval overlaps one or more intervals already in the
877	/// map, then an [`OverlapError`] is returned and the map is not
878	/// updated.
879	///
880	/// # Panics
881	///
882	/// Panics if the given interval is an invalid interval. See [`Invalid
883	/// Intervals`](https://docs.rs/nodit/latest/nodit/index.html#invalid-intervals)
884	/// for more details.
885	///
886	/// # Examples
887	/// ```
888	/// use nodit::interval::ie;
889	/// use nodit::{NoditMap, OverlapError};
890	///
891	/// let mut map = NoditMap::from_slice_strict([
892	/// 	(ie(1, 4), false),
893	/// 	(ie(6, 8), true),
894	/// ])
895	/// .unwrap();
896	///
897	/// // Touching
898	/// assert_eq!(
899	/// 	map.insert_merge_touching_if_values_equal(ie(4, 6), true),
900	/// 	Ok(ie(4, 8))
901	/// );
902	///
903	/// // Overlapping
904	/// assert_eq!(
905	/// 	map.insert_merge_touching_if_values_equal(ie(4, 8), false),
906	/// 	Err(OverlapError { value: false }),
907	/// );
908	///
909	/// // Neither Touching or Overlapping
910	/// assert_eq!(
911	/// 	map.insert_merge_touching_if_values_equal(ie(10, 16), false),
912	/// 	Ok(ie(10, 16))
913	/// );
914	///
915	/// assert_eq!(
916	/// 	map.into_iter().collect::<Vec<_>>(),
917	/// 	[(ie(1, 4), false), (ie(4, 8), true), (ie(10, 16), false)]
918	/// );
919	/// ```
920	pub fn insert_merge_touching_if_values_equal(
921		&mut self,
922		interval: K,
923		value: V,
924	) -> Result<K, OverlapError<V>>
925	where
926		V: Eq,
927	{
928		invalid_interval_panic(interval);
929
930		if self.overlaps(interval) {
931			return Err(OverlapError { value });
932		}
933
934		let get_start = |selfy: &Self, value: &V| {
935			selfy
936				.inner
937				.get_key_value(touching_start_comp(interval.start()))
938				.filter(|(_, start_touching_value)| {
939					*start_touching_value == value
940				})
941				.map(|(key, _)| key)
942				.copied()
943		};
944		let get_end = |selfy: &Self, value: &V| {
945			selfy
946				.inner
947				.get_key_value(touching_end_comp(interval.end()))
948				.filter(|(_, start_touching_value)| {
949					*start_touching_value == value
950				})
951				.map(|(key, _)| key)
952				.copied()
953		};
954
955		Ok(self.insert_merge_with_comps(
956			interval,
957			value,
958			get_start,
959			get_end,
960			|selfy, value| {
961				if get_start(selfy, value).is_some() {
962					selfy.inner.remove(touching_start_comp(interval.start()));
963				}
964			},
965			|selfy, value| {
966				if get_end(selfy, value).is_some() {
967					selfy.inner.remove(touching_end_comp(interval.end()));
968				}
969			},
970		))
971	}
972
973	/// Adds a new entry to the map and merges into other intervals in
974	/// the map which overlap it.
975	///
976	/// The value of the merged-together interval is set to the value given for
977	/// this insertion.
978	///
979	/// The newly inserted (possibly merged) interval is returned.
980	///
981	/// # Panics
982	///
983	/// Panics if the given interval is an invalid interval. See [`Invalid
984	/// Intervals`](https://docs.rs/nodit/latest/nodit/index.html#invalid-intervals)
985	/// for more details.
986	///
987	/// # Examples
988	/// ```
989	/// use nodit::interval::ie;
990	/// use nodit::{NoditMap, OverlapError};
991	///
992	/// let mut map = NoditMap::from_slice_strict([
993	/// 	(ie(1, 4), false),
994	/// 	(ie(6, 8), true),
995	/// ])
996	/// .unwrap();
997	///
998	/// // Touching
999	/// assert_eq!(
1000	/// 	map.insert_merge_overlapping(ie(4, 6), true),
1001	/// 	ie(4, 6)
1002	/// );
1003	///
1004	/// // Overlapping
1005	/// assert_eq!(
1006	/// 	map.insert_merge_overlapping(ie(4, 8), false),
1007	/// 	ie(4, 8)
1008	/// );
1009	///
1010	/// // Neither Touching or Overlapping
1011	/// assert_eq!(
1012	/// 	map.insert_merge_overlapping(ie(10, 16), false),
1013	/// 	ie(10, 16)
1014	/// );
1015	///
1016	/// assert_eq!(
1017	/// 	map.into_iter().collect::<Vec<_>>(),
1018	/// 	[(ie(1, 4), false), (ie(4, 8), false), (ie(10, 16), false)]
1019	/// );
1020	/// ```
1021	pub fn insert_merge_overlapping(&mut self, interval: K, value: V) -> K {
1022		invalid_interval_panic(interval);
1023
1024		self.insert_merge_with_comps(
1025			interval,
1026			value,
1027			|selfy, _| {
1028				selfy
1029					.inner
1030					.get_key_value(overlapping_comp(interval.start()))
1031					.map(|(key, _)| key)
1032					.copied()
1033			},
1034			|selfy, _| {
1035				selfy
1036					.inner
1037					.get_key_value(overlapping_comp(interval.end()))
1038					.map(|(key, _)| key)
1039					.copied()
1040			},
1041			|_, _| {},
1042			|_, _| {},
1043		)
1044	}
1045
1046	/// Adds a new entry to the map and merges into other intervals in
1047	/// the map which touch or overlap it.
1048	///
1049	/// The value of the merged-together interval is set to the value given for
1050	/// this insertion.
1051	///
1052	/// The newly inserted (possibly merged) interval is returned.
1053	///
1054	/// # Panics
1055	///
1056	/// Panics if the given interval is an invalid interval. See [`Invalid
1057	/// Intervals`](https://docs.rs/nodit/latest/nodit/index.html#invalid-intervals)
1058	/// for more details.
1059	///
1060	/// # Examples
1061	/// ```
1062	/// use nodit::interval::ie;
1063	/// use nodit::{NoditMap, OverlapError};
1064	///
1065	/// let mut map = NoditMap::from_slice_strict([
1066	/// 	(ie(1, 4), false),
1067	/// 	(ie(6, 8), true),
1068	/// ])
1069	/// .unwrap();
1070	///
1071	/// // Touching
1072	/// assert_eq!(
1073	/// 	map.insert_merge_touching_or_overlapping(ie(4, 6), true),
1074	/// 	ie(1, 8)
1075	/// );
1076	///
1077	/// // Overlapping
1078	/// assert_eq!(
1079	/// 	map.insert_merge_touching_or_overlapping(ie(4, 8), false),
1080	/// 	ie(1, 8)
1081	/// );
1082	///
1083	/// // Neither Touching or Overlapping
1084	/// assert_eq!(
1085	/// 	map.insert_merge_touching_or_overlapping(ie(10, 16), false),
1086	/// 	ie(10, 16)
1087	/// );
1088	///
1089	/// assert_eq!(
1090	/// 	map.into_iter().collect::<Vec<_>>(),
1091	/// 	[(ie(1, 8), false), (ie(10, 16), false)]
1092	/// );
1093	/// ```
1094	pub fn insert_merge_touching_or_overlapping(
1095		&mut self,
1096		interval: K,
1097		value: V,
1098	) -> K {
1099		invalid_interval_panic(interval);
1100
1101		self.insert_merge_with_comps(
1102			interval,
1103			value,
1104			|selfy, _| {
1105				selfy
1106					.inner
1107					.get_key_value(touching_start_comp(interval.start()))
1108					.map(|(key, _)| key)
1109					.or(selfy
1110						.inner
1111						.get_key_value(overlapping_comp(interval.start()))
1112						.map(|(key, _)| key))
1113					.copied()
1114			},
1115			|selfy, _| {
1116				selfy
1117					.inner
1118					.get_key_value(touching_end_comp(interval.end()))
1119					.map(|(key, _)| key)
1120					.or(selfy
1121						.inner
1122						.get_key_value(overlapping_comp(interval.end()))
1123						.map(|(key, _)| key))
1124					.copied()
1125			},
1126			|selfy, _| {
1127				selfy.inner.remove(touching_start_comp(interval.start()));
1128			},
1129			|selfy, _| {
1130				selfy.inner.remove(touching_end_comp(interval.end()));
1131			},
1132		)
1133	}
1134
1135	/// Adds a new entry to the map and overwrites any other intervals
1136	/// that overlap the new interval.
1137	///
1138	/// Returns an iterator over the full or partial cut entries in
1139	/// ascending order.
1140	///
1141	/// This is equivalent to using [`NoditMap::cut()`]
1142	/// followed by [`NoditMap::insert_strict()`]. Hence the
1143	/// same `V: Clone` trait bound applies.
1144	///
1145	/// # Panics
1146	///
1147	/// Panics if the given interval is an invalid interval. See [`Invalid
1148	/// Intervals`](https://docs.rs/nodit/latest/nodit/index.html#invalid-intervals)
1149	/// for more details.
1150	///
1151	/// # Examples
1152	/// ```
1153	/// use nodit::interval::ie;
1154	/// use nodit::NoditMap;
1155	///
1156	/// let mut map =
1157	/// 	NoditMap::from_slice_strict([(ie(2, 8), false)]).unwrap();
1158	///
1159	/// map.insert_overwrite(ie(4, 6), true);
1160	///
1161	/// assert_eq!(
1162	/// 	map.into_iter().collect::<Vec<_>>(),
1163	/// 	[(ie(2, 4), false), (ie(4, 6), true), (ie(6, 8), false)]
1164	/// );
1165	/// ```
1166	pub fn insert_overwrite(
1167		&mut self,
1168		interval: K,
1169		value: V,
1170	) -> impl Iterator<Item = (K, V)>
1171	where
1172		V: Clone,
1173	{
1174		invalid_interval_panic(interval);
1175
1176		let cut = self.cut(interval);
1177		self.insert_unchecked(interval, value);
1178		cut
1179	}
1180
1181	/// Allocates a `NoditMap` and moves the given entries from
1182	/// the given slice into the map using
1183	/// [`NoditMap::insert_strict()`].
1184	///
1185	/// May return an `Err` while inserting. See
1186	/// [`NoditMap::insert_strict()`] for details.
1187	///
1188	/// # Panics
1189	///
1190	/// Panics if the given interval is an invalid interval. See [`Invalid
1191	/// Intervals`](https://docs.rs/nodit/latest/nodit/index.html#invalid-intervals)
1192	/// for more details.
1193	///
1194	/// # Examples
1195	/// ```
1196	/// use nodit::interval::ie;
1197	/// use nodit::NoditMap;
1198	///
1199	/// let map = NoditMap::from_slice_strict([
1200	/// 	(ie(1, 4), false),
1201	/// 	(ie(4, 8), true),
1202	/// 	(ie(8, 100), false),
1203	/// ])
1204	/// .unwrap();
1205	/// ```
1206	pub fn from_slice_strict<const N: usize>(
1207		slice: [(K, V); N],
1208	) -> Result<NoditMap<I, K, V>, OverlapError<V>> {
1209		NoditMap::from_iter_strict(slice.into_iter())
1210	}
1211
1212	/// Collects a `NoditMap` from an iterator of (interval,
1213	/// value) tuples using [`NoditMap::insert_strict()`].
1214	///
1215	/// May return an `Err` while inserting. See
1216	/// [`NoditMap::insert_strict()`] for details.
1217	///
1218	/// # Panics
1219	///
1220	/// Panics if the given interval is an invalid interval. See [`Invalid
1221	/// Intervals`](https://docs.rs/nodit/latest/nodit/index.html#invalid-intervals)
1222	/// for more details.
1223	///
1224	/// # Examples
1225	/// ```
1226	/// use nodit::interval::ie;
1227	/// use nodit::NoditMap;
1228	///
1229	/// let slice =
1230	/// 	[(ie(1, 4), false), (ie(4, 8), true), (ie(8, 100), false)];
1231	///
1232	/// let map: NoditMap<_, _, _> = NoditMap::from_iter_strict(
1233	/// 	slice
1234	/// 		.into_iter()
1235	/// 		.filter(|(interval, _)| interval.start() > 2),
1236	/// )
1237	/// .unwrap();
1238	/// ```
1239	pub fn from_iter_strict(
1240		iter: impl Iterator<Item = (K, V)>,
1241	) -> Result<NoditMap<I, K, V>, OverlapError<V>> {
1242		let mut map = NoditMap::new();
1243		for (interval, value) in iter {
1244			map.insert_strict(interval, value)?;
1245		}
1246		Ok(map)
1247	}
1248}
1249
1250impl<I, K, V> NoditMap<I, K, V> {
1251	/// Makes a new, empty [`NoditMap`].
1252	///
1253	/// # Examples
1254	/// ```
1255	/// use nodit::{Interval, NoditMap};
1256	///
1257	/// let map: NoditMap<i8, Interval<i8>, bool> = NoditMap::new();
1258	/// ```
1259	pub fn new() -> Self {
1260		Self::default()
1261	}
1262
1263	/// Returns the number of intervals in the map.
1264	///
1265	/// # Examples
1266	/// ```
1267	/// use nodit::interval::ie;
1268	/// use nodit::NoditMap;
1269	///
1270	/// let mut map = NoditMap::new();
1271	///
1272	/// assert_eq!(map.len(), 0);
1273	/// map.insert_strict(ie(0, 1), false).unwrap();
1274	/// assert_eq!(map.len(), 1);
1275	/// ```
1276	pub fn len(&self) -> usize {
1277		self.inner.len()
1278	}
1279
1280	/// Returns `true` if the map contains no intervals, and
1281	/// `false` if it does.
1282	///
1283	/// # Examples
1284	/// ```
1285	/// use nodit::interval::ie;
1286	/// use nodit::NoditMap;
1287	///
1288	/// let mut map = NoditMap::new();
1289	///
1290	/// assert_eq!(map.is_empty(), true);
1291	/// map.insert_strict(ie(0, 1), false).unwrap();
1292	/// assert_eq!(map.is_empty(), false);
1293	/// ```
1294	pub fn is_empty(&self) -> bool {
1295		self.inner.is_empty()
1296	}
1297
1298	/// Returns an iterator over every entry in the map in ascending
1299	/// order.
1300	///
1301	/// # Examples
1302	/// ```
1303	/// use nodit::interval::ie;
1304	/// use nodit::NoditMap;
1305	///
1306	/// let map = NoditMap::from_slice_strict([
1307	/// 	(ie(1, 4), false),
1308	/// 	(ie(4, 8), true),
1309	/// 	(ie(8, 100), false),
1310	/// ])
1311	/// .unwrap();
1312	///
1313	/// let mut iter = map.iter();
1314	///
1315	/// assert_eq!(iter.next(), Some((&ie(1, 4), &false)));
1316	/// assert_eq!(iter.next(), Some((&ie(4, 8), &true)));
1317	/// assert_eq!(iter.next(), Some((&ie(8, 100), &false)));
1318	/// assert_eq!(iter.next(), None);
1319	/// ```
1320	pub fn iter(&self) -> impl DoubleEndedIterator<Item = (&K, &V)> {
1321		self.inner.iter()
1322	}
1323
1324	/// Returns an mutable iterator over every entry in the map in
1325	/// ascending order.
1326	///
1327	/// # Examples
1328	/// ```
1329	/// use nodit::interval::ie;
1330	/// use nodit::NoditMap;
1331	///
1332	/// let mut map = NoditMap::from_slice_strict([
1333	/// 	(ie(1, 4), false),
1334	/// 	(ie(4, 8), true),
1335	/// 	(ie(8, 100), false),
1336	/// ])
1337	/// .unwrap();
1338	///
1339	/// for (interval, value) in map.iter_mut() {
1340	/// 	if *interval == ie(4, 8) {
1341	/// 		*value = false
1342	/// 	} else {
1343	/// 		*value = true
1344	/// 	}
1345	/// }
1346	/// ```
1347	pub fn iter_mut(
1348		&mut self,
1349	) -> impl DoubleEndedIterator<Item = (&K, &mut V)> {
1350		self.inner.iter_mut()
1351	}
1352
1353	/// Returns the first entry in the map, if any.
1354	///
1355	/// # Examples
1356	/// ```
1357	/// use nodit::interval::ie;
1358	/// use nodit::NoditMap;
1359	///
1360	/// let map = NoditMap::from_slice_strict([
1361	/// 	(ie(1, 4), false),
1362	/// 	(ie(4, 8), true),
1363	/// 	(ie(8, 100), false),
1364	/// ])
1365	/// .unwrap();
1366	///
1367	/// assert_eq!(map.first_key_value(), Some((&ie(1, 4), &false)));
1368	/// ```
1369	pub fn first_key_value(&self) -> Option<(&K, &V)> {
1370		self.inner.first_key_value()
1371	}
1372
1373	/// Returns the last entry in the map, if any.
1374	///
1375	/// # Examples
1376	/// ```
1377	/// use nodit::NoditMap;
1378	/// use nodit::interval::ie;
1379	///
1380	/// let map = NoditMap::from_slice_strict([
1381	/// 	(ie(1, 4), false),
1382	/// 	(ie(4, 8), true),
1383	/// 	(ie(8, 100), false),
1384	/// ])
1385	/// .unwrap();
1386	///
1387	/// assert_eq!(
1388	/// 	map.last_key_value(),
1389	/// 	Some((&ie(8, 100), &false))
1390	/// );
1391	pub fn last_key_value(&self) -> Option<(&K, &V)> {
1392		self.inner.last_key_value()
1393	}
1394}
1395
1396// Trait Impls ==========================
1397
1398impl<I, K, V> IntoIterator for NoditMap<I, K, V> {
1399	type Item = (K, V);
1400	type IntoIter = IntoIter<I, K, V>;
1401	fn into_iter(self) -> Self::IntoIter {
1402		IntoIter {
1403			inner: self.inner.into_iter(),
1404			phantom: PhantomData,
1405		}
1406	}
1407}
1408/// An owning iterator over the entries of a [`NoditMap`].
1409///
1410/// This `struct` is created by the [`into_iter`] method on
1411/// [`NoditMap`] (provided by the [`IntoIterator`] trait). See
1412/// its documentation for more.
1413///
1414/// [`into_iter`]: IntoIterator::into_iter
1415/// [`IntoIterator`]: core::iter::IntoIterator
1416pub struct IntoIter<I, K, V> {
1417	inner: BTreeMapIntoIter<K, V>,
1418	phantom: PhantomData<I>,
1419}
1420impl<I, K, V> Iterator for IntoIter<I, K, V> {
1421	type Item = (K, V);
1422	fn next(&mut self) -> Option<Self::Item> {
1423		self.inner.next()
1424	}
1425}
1426
1427impl<I, K, V> Default for NoditMap<I, K, V> {
1428	fn default() -> Self {
1429		NoditMap {
1430			inner: BTreeMap::default(),
1431			phantom: PhantomData,
1432		}
1433	}
1434}
1435
1436#[cfg(feature = "serde")]
1437mod serde {
1438	use core::marker::PhantomData;
1439
1440	use serde::de::{SeqAccess, Visitor};
1441	use serde::ser::SerializeSeq;
1442	use serde::{Deserialize, Deserializer, Serialize, Serializer};
1443
1444	use crate::{IntervalType, NoditMap, PointType};
1445
1446	impl<I, K, V> Serialize for NoditMap<I, K, V>
1447	where
1448		K: Serialize,
1449		V: Serialize,
1450	{
1451		fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
1452		where
1453			S: Serializer,
1454		{
1455			let mut seq = serializer.serialize_seq(Some(self.len()))?;
1456			for (interval, value) in self.iter() {
1457				seq.serialize_element(&(interval, value))?;
1458			}
1459			seq.end()
1460		}
1461	}
1462
1463	impl<'de, I, K, V> Deserialize<'de> for NoditMap<I, K, V>
1464	where
1465		I: PointType,
1466		K: IntervalType<I> + Deserialize<'de>,
1467		V: Deserialize<'de>,
1468	{
1469		fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
1470		where
1471			D: Deserializer<'de>,
1472		{
1473			deserializer.deserialize_seq(NoditMapVisitor {
1474				i: PhantomData,
1475				k: PhantomData,
1476				v: PhantomData,
1477			})
1478		}
1479	}
1480
1481	struct NoditMapVisitor<I, K, V> {
1482		i: PhantomData<I>,
1483		k: PhantomData<K>,
1484		v: PhantomData<V>,
1485	}
1486
1487	impl<'de, I, K, V> Visitor<'de> for NoditMapVisitor<I, K, V>
1488	where
1489		I: PointType,
1490		K: IntervalType<I> + Deserialize<'de>,
1491		V: Deserialize<'de>,
1492	{
1493		type Value = NoditMap<I, K, V>;
1494
1495		fn expecting(
1496			&self,
1497			formatter: &mut alloc::fmt::Formatter,
1498		) -> alloc::fmt::Result {
1499			formatter.write_str("a NoditMap")
1500		}
1501
1502		fn visit_seq<A>(self, mut access: A) -> Result<Self::Value, A::Error>
1503		where
1504			A: SeqAccess<'de>,
1505		{
1506			let mut map = NoditMap::new();
1507			while let Some((interval, value)) = access.next_element()? {
1508				map.insert_strict(interval, value)
1509					.or(Err(serde::de::Error::custom("intervals overlap")))?;
1510			}
1511			Ok(map)
1512		}
1513	}
1514}
1515
1516#[cfg(test)]
1517mod tests {
1518	extern crate std;
1519
1520	use std::dbg;
1521
1522	use pretty_assertions::assert_eq;
1523
1524	use super::*;
1525	use crate::interval::{ee, ei, ie, ii, iu, ue, ui, uu};
1526	use crate::utils::{config, contains_point, Config, CutResult};
1527
1528	//only every other number to allow mathematical_overlapping_definition
1529	//to test between bounds in finite using smaller intervalled finite
1530	pub(crate) const NUMBERS: &[i8] = &[2, 4, 6, 8, 10];
1531	//go a bit around on either side to compensate for Unbounded
1532	pub(crate) const NUMBERS_DOMAIN: &[i8] =
1533		&[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11];
1534
1535	fn basic() -> NoditMap<i8, Interval<i8>, bool> {
1536		NoditMap::from_slice_strict([
1537			(ui(4), false),
1538			(ee(5, 7), true),
1539			(ii(7, 7), false),
1540			(ie(14, 16), true),
1541		])
1542		.unwrap()
1543	}
1544	fn basic_slice() -> [(Interval<i8>, bool); 4] {
1545		[
1546			(ui(4), false),
1547			(ee(5, 7), true),
1548			(ii(7, 7), false),
1549			(ie(14, 16), true),
1550		]
1551	}
1552
1553	#[test]
1554	fn insert_strict_tests() {
1555		assert_insert_strict(
1556			basic(),
1557			(ii(0, 4), false),
1558			Err(OverlapError { value: false }),
1559			basic_slice(),
1560		);
1561		assert_insert_strict(
1562			basic(),
1563			(ii(5, 6), false),
1564			Err(OverlapError { value: false }),
1565			basic_slice(),
1566		);
1567		assert_insert_strict(
1568			basic(),
1569			(ii(4, 5), true),
1570			Err(OverlapError { value: true }),
1571			basic_slice(),
1572		);
1573		assert_insert_strict(
1574			basic(),
1575			(ei(4, 5), true),
1576			Ok(()),
1577			[
1578				(ui(4), false),
1579				(ei(4, 5), true),
1580				(ee(5, 7), true),
1581				(ii(7, 7), false),
1582				(ie(14, 16), true),
1583			],
1584		);
1585	}
1586	fn assert_insert_strict<const N: usize>(
1587		mut before: NoditMap<i8, Interval<i8>, bool>,
1588		to_insert: (Interval<i8>, bool),
1589		result: Result<(), OverlapError<bool>>,
1590		after: [(Interval<i8>, bool); N],
1591	) {
1592		assert_eq!(before.insert_strict(to_insert.0, to_insert.1), result);
1593		assert_eq!(before, NoditMap::from_slice_strict(after).unwrap())
1594	}
1595
1596	#[test]
1597	fn overlapping_tests() {
1598		//case zero
1599		for overlap_interval in all_valid_test_bounds() {
1600			//you can't overlap nothing
1601			assert!(
1602				NoditMap::<i8, Interval<i8>, ()>::new()
1603					.overlapping(overlap_interval)
1604					.next()
1605					.is_none()
1606			);
1607		}
1608
1609		//case one
1610		for overlap_interval in all_valid_test_bounds() {
1611			for inside_interval in all_valid_test_bounds() {
1612				let mut map = NoditMap::new();
1613				map.insert_strict(inside_interval, ()).unwrap();
1614
1615				let mut expected_overlapping = Vec::new();
1616				if overlap_interval.overlaps(&inside_interval) {
1617					expected_overlapping.push(inside_interval);
1618				}
1619
1620				let overlapping = map
1621					.overlapping(overlap_interval)
1622					.map(|(key, _)| key)
1623					.copied()
1624					.collect::<Vec<_>>();
1625
1626				if overlapping != expected_overlapping {
1627					dbg!(overlap_interval, inside_interval);
1628					dbg!(overlapping, expected_overlapping);
1629					panic!(
1630						"Discrepancy in .overlapping() with single inside interval detected!"
1631					);
1632				}
1633			}
1634		}
1635
1636		//case two
1637		for overlap_interval in all_valid_test_bounds() {
1638			for (inside_interval1, inside_interval2) in
1639				all_non_overlapping_test_bound_entries()
1640			{
1641				let mut map = NoditMap::new();
1642				map.insert_strict(inside_interval1, ()).unwrap();
1643				map.insert_strict(inside_interval2, ()).unwrap();
1644
1645				let mut expected_overlapping = Vec::new();
1646				if overlap_interval.overlaps(&inside_interval1) {
1647					expected_overlapping.push(inside_interval1);
1648				}
1649				if overlap_interval.overlaps(&inside_interval2) {
1650					expected_overlapping.push(inside_interval2);
1651				}
1652				//make our expected_overlapping the correct order
1653				if expected_overlapping.len() > 1
1654					&& expected_overlapping[0].start()
1655						> expected_overlapping[1].start()
1656				{
1657					expected_overlapping.swap(0, 1);
1658				}
1659
1660				let overlapping = map
1661					.overlapping(overlap_interval)
1662					.map(|(key, _)| key)
1663					.copied()
1664					.collect::<Vec<_>>();
1665
1666				if overlapping != expected_overlapping {
1667					dbg!(overlap_interval, inside_interval1, inside_interval2);
1668					dbg!(overlapping, expected_overlapping);
1669					panic!(
1670						"Discrepancy in .overlapping() with two inside intervals detected!"
1671					);
1672				}
1673			}
1674		}
1675	}
1676
1677	#[test]
1678	fn remove_overlapping_tests() {
1679		assert_remove_overlapping(basic(), ii(5, 5), [], basic_slice());
1680		assert_remove_overlapping(
1681			basic(),
1682			uu(),
1683			[
1684				(ui(4), false),
1685				(ee(5, 7), true),
1686				(ii(7, 7), false),
1687				(ie(14, 16), true),
1688			],
1689			[],
1690		);
1691		assert_remove_overlapping(
1692			basic(),
1693			ii(6, 7),
1694			[(ee(5, 7), true), (ii(7, 7), false)],
1695			[(ui(4), false), (ie(14, 16), true)],
1696		);
1697		assert_remove_overlapping(
1698			basic(),
1699			iu(6),
1700			[(ee(5, 7), true), (ii(7, 7), false), (ie(14, 16), true)],
1701			[(ui(4), false)],
1702		);
1703	}
1704	fn assert_remove_overlapping<const N: usize, const Y: usize>(
1705		mut before: NoditMap<i8, Interval<i8>, bool>,
1706		to_remove: Interval<i8>,
1707		result: [(Interval<i8>, bool); N],
1708		after: [(Interval<i8>, bool); Y],
1709	) {
1710		assert_eq!(
1711			before.remove_overlapping(to_remove).collect::<Vec<_>>(),
1712			result
1713		);
1714		assert_eq!(before, NoditMap::from_slice_strict(after).unwrap())
1715	}
1716
1717	#[test]
1718	fn cut_tests() {
1719		assert_cut(
1720			basic(),
1721			ii(50, 60),
1722			[],
1723			[
1724				(ui(4), false),
1725				(ee(5, 7), true),
1726				(ii(7, 7), false),
1727				(ie(14, 16), true),
1728			],
1729		);
1730		assert_cut(
1731			basic(),
1732			uu(),
1733			[
1734				(ui(4), false),
1735				(ee(5, 7), true),
1736				(ii(7, 7), false),
1737				(ie(14, 16), true),
1738			],
1739			[],
1740		);
1741		assert_cut(
1742			basic(),
1743			ui(6),
1744			[(ui(4), false), (ei(5, 6), true)],
1745			[(ii(7, 7), false), (ie(14, 16), true)],
1746		);
1747		assert_cut(
1748			basic(),
1749			iu(6),
1750			[(ie(6, 7), true), (ii(7, 7), false), (ie(14, 16), true)],
1751			[(ui(4), false)],
1752		);
1753	}
1754	fn assert_cut<const N: usize, const Y: usize>(
1755		mut before: NoditMap<i8, Interval<i8>, bool>,
1756		to_cut: Interval<i8>,
1757		result: [(Interval<i8>, bool); Y],
1758		after: [(Interval<i8>, bool); N],
1759	) {
1760		assert_eq!(before.cut(to_cut).collect::<Vec<_>>(), result);
1761		assert_eq!(before, NoditMap::from_slice_strict(after).unwrap());
1762	}
1763
1764	#[test]
1765	fn gaps_untrimmed_tests() {
1766		assert_gaps_untrimmed(basic(), ii(50, 60), [iu(16)]);
1767		assert_gaps_untrimmed(basic(), iu(50), [iu(16)]);
1768		assert_gaps_untrimmed(basic(), ee(3, 16), [ei(4, 5), ee(7, 14)]);
1769		assert_gaps_untrimmed(
1770			basic(),
1771			ei(3, 16),
1772			[ei(4, 5), ee(7, 14), iu(16)],
1773		);
1774		assert_gaps_untrimmed(basic(), ue(5), []);
1775		assert_gaps_untrimmed(basic(), ui(3), []);
1776		assert_gaps_untrimmed(basic(), ii(5, 5), [ii(5, 5)]);
1777		assert_gaps_untrimmed(basic(), ii(6, 6), []);
1778		assert_gaps_untrimmed(basic(), ii(7, 7), []);
1779		assert_gaps_untrimmed(basic(), ii(8, 8), [ii(8, 13)]);
1780
1781		assert_gaps_untrimmed(
1782			basic(),
1783			ii(i8::MIN, i8::MAX),
1784			[ei(4, 5), ee(7, 14), ii(16, i8::MAX)],
1785		);
1786		assert_eq!(
1787			NoditMap::from_slice_strict([(ii(i8::MIN, i8::MAX), false)])
1788				.unwrap()
1789				.gaps_trimmed(uu())
1790				.collect::<Vec<_>>(),
1791			[]
1792		);
1793	}
1794	fn assert_gaps_untrimmed<const N: usize>(
1795		map: NoditMap<i8, Interval<i8>, bool>,
1796		interval: Interval<i8>,
1797		result: [Interval<i8>; N],
1798	) {
1799		assert_eq!(map.gaps_untrimmed(interval).collect::<Vec<_>>(), result);
1800	}
1801
1802	#[test]
1803	fn gaps_trimmed_tests() {
1804		assert_gaps_trimmed(basic(), ii(50, 60), [ii(50, 60)]);
1805		assert_gaps_trimmed(basic(), iu(50), [iu(50)]);
1806		assert_gaps_trimmed(basic(), ee(3, 16), [ei(4, 5), ee(7, 14)]);
1807		assert_gaps_trimmed(
1808			basic(),
1809			ei(3, 16),
1810			[ei(4, 5), ee(7, 14), ii(16, 16)],
1811		);
1812		assert_gaps_trimmed(basic(), ue(5), []);
1813		assert_gaps_trimmed(basic(), ui(3), []);
1814		assert_gaps_trimmed(basic(), ii(5, 5), [ii(5, 5)]);
1815		assert_gaps_trimmed(basic(), ii(6, 6), []);
1816		assert_gaps_trimmed(basic(), ii(7, 7), []);
1817		assert_gaps_trimmed(basic(), ii(8, 8), [ii(8, 8)]);
1818
1819		assert_gaps_trimmed(
1820			basic(),
1821			ii(i8::MIN, i8::MAX),
1822			[ei(4, 5), ee(7, 14), ii(16, i8::MAX)],
1823		);
1824		assert_eq!(
1825			NoditMap::from_slice_strict([(ii(i8::MIN, i8::MAX), false)])
1826				.unwrap()
1827				.gaps_trimmed(uu())
1828				.collect::<Vec<_>>(),
1829			[]
1830		);
1831	}
1832	fn assert_gaps_trimmed<const N: usize>(
1833		map: NoditMap<i8, Interval<i8>, bool>,
1834		interval: Interval<i8>,
1835		result: [Interval<i8>; N],
1836	) {
1837		assert_eq!(map.gaps_trimmed(interval).collect::<Vec<_>>(), result);
1838	}
1839
1840	#[test]
1841	fn insert_merge_touching_tests() {
1842		assert_insert_merge_touching(
1843			basic(),
1844			(ii(0, 4), false),
1845			Err(OverlapError { value: false }),
1846			[
1847				(ui(4), false),
1848				(ee(5, 7), true),
1849				(ii(7, 7), false),
1850				(ie(14, 16), true),
1851			],
1852		);
1853		assert_insert_merge_touching(
1854			basic(),
1855			(ee(7, 10), false),
1856			Ok(ie(7, 10)),
1857			[
1858				(ui(4), false),
1859				(ee(5, 7), true),
1860				(ie(7, 10), false),
1861				(ie(14, 16), true),
1862			],
1863		);
1864		assert_insert_merge_touching(
1865			basic(),
1866			(ee(7, 11), true),
1867			Ok(ie(7, 11)),
1868			[
1869				(ui(4), false),
1870				(ee(5, 7), true),
1871				(ie(7, 11), true),
1872				(ie(14, 16), true),
1873			],
1874		);
1875		assert_insert_merge_touching(
1876			basic(),
1877			(ee(7, 14), false),
1878			Ok(ie(7, 16)),
1879			[(ui(4), false), (ee(5, 7), true), (ie(7, 16), false)],
1880		);
1881	}
1882	fn assert_insert_merge_touching<const N: usize>(
1883		mut before: NoditMap<i8, Interval<i8>, bool>,
1884		to_insert: (Interval<i8>, bool),
1885		result: Result<Interval<i8>, OverlapError<bool>>,
1886		after: [(Interval<i8>, bool); N],
1887	) {
1888		assert_eq!(
1889			before.insert_merge_touching(to_insert.0, to_insert.1),
1890			result
1891		);
1892		assert_eq!(before, NoditMap::from_slice_strict(after).unwrap())
1893	}
1894	#[test]
1895	fn insert_merge_touching_if_values_equal_tests() {
1896		assert_insert_merge_touching_if_values_equal(
1897			basic(),
1898			(ii(0, 4), false),
1899			Err(OverlapError { value: false }),
1900			basic_slice(),
1901		);
1902		dbg!("hererere");
1903		assert_insert_merge_touching_if_values_equal(
1904			basic(),
1905			(ee(7, 10), false),
1906			Ok(ie(7, 10)),
1907			[
1908				(ui(4), false),
1909				(ee(5, 7), true),
1910				(ie(7, 10), false),
1911				(ie(14, 16), true),
1912			],
1913		);
1914		assert_insert_merge_touching_if_values_equal(
1915			basic(),
1916			(ee(7, 11), true),
1917			Ok(ee(7, 11)),
1918			[
1919				(ui(4), false),
1920				(ee(5, 7), true),
1921				(ii(7, 7), false),
1922				(ee(7, 11), true),
1923				(ie(14, 16), true),
1924			],
1925		);
1926		assert_insert_merge_touching_if_values_equal(
1927			basic(),
1928			(ee(7, 14), false),
1929			Ok(ie(7, 14)),
1930			[
1931				(ui(4), false),
1932				(ee(5, 7), true),
1933				(ie(7, 14), false),
1934				(ie(14, 16), true),
1935			],
1936		);
1937	}
1938	fn assert_insert_merge_touching_if_values_equal<const N: usize>(
1939		mut before: NoditMap<i8, Interval<i8>, bool>,
1940		to_insert: (Interval<i8>, bool),
1941		result: Result<Interval<i8>, OverlapError<bool>>,
1942		after: [(Interval<i8>, bool); N],
1943	) {
1944		assert_eq!(
1945			before.insert_merge_touching_if_values_equal(
1946				to_insert.0,
1947				to_insert.1
1948			),
1949			result
1950		);
1951		assert_eq!(before, NoditMap::from_slice_strict(after).unwrap())
1952	}
1953
1954	#[test]
1955	fn insert_merge_overlapping_tests() {
1956		assert_insert_merge_overlapping(
1957			basic(),
1958			(ii(0, 2), true),
1959			ui(4),
1960			[
1961				(ui(4), true),
1962				(ee(5, 7), true),
1963				(ii(7, 7), false),
1964				(ie(14, 16), true),
1965			],
1966		);
1967		assert_insert_merge_overlapping(
1968			basic(),
1969			(ie(14, 16), false),
1970			ie(14, 16),
1971			[
1972				(ui(4), false),
1973				(ee(5, 7), true),
1974				(ii(7, 7), false),
1975				(ie(14, 16), false),
1976			],
1977		);
1978		assert_insert_merge_overlapping(
1979			basic(),
1980			(ii(6, 11), false),
1981			ei(5, 11),
1982			[(ui(4), false), (ei(5, 11), false), (ie(14, 16), true)],
1983		);
1984		assert_insert_merge_overlapping(
1985			basic(),
1986			(ii(15, 18), true),
1987			ii(14, 18),
1988			[
1989				(ui(4), false),
1990				(ee(5, 7), true),
1991				(ii(7, 7), false),
1992				(ii(14, 18), true),
1993			],
1994		);
1995		assert_insert_merge_overlapping(
1996			basic(),
1997			(uu(), false),
1998			uu(),
1999			[(uu(), false)],
2000		);
2001	}
2002	fn assert_insert_merge_overlapping<const N: usize>(
2003		mut before: NoditMap<i8, Interval<i8>, bool>,
2004		to_insert: (Interval<i8>, bool),
2005		result: Interval<i8>,
2006		after: [(Interval<i8>, bool); N],
2007	) {
2008		assert_eq!(
2009			before.insert_merge_overlapping(to_insert.0, to_insert.1),
2010			result
2011		);
2012		assert_eq!(before, NoditMap::from_slice_strict(after).unwrap())
2013	}
2014
2015	#[test]
2016	fn insert_merge_touching_or_overlapping_tests() {
2017		assert_insert_merge_touching_or_overlapping(
2018			NoditMap::from_slice_strict([(ie(1, 4), false)]).unwrap(),
2019			(ie(0, 1), true),
2020			ie(0, 4),
2021			[(ie(0, 4), true)],
2022		);
2023
2024		//copied from insert_merge_overlapping_tests
2025		assert_insert_merge_touching_or_overlapping(
2026			basic(),
2027			(ii(0, 2), true),
2028			ui(4),
2029			[
2030				(ui(4), true),
2031				(ee(5, 7), true),
2032				(ii(7, 7), false),
2033				(ie(14, 16), true),
2034			],
2035		);
2036		assert_insert_merge_touching_or_overlapping(
2037			basic(),
2038			(ie(14, 16), false),
2039			ie(14, 16),
2040			[
2041				(ui(4), false),
2042				(ee(5, 7), true),
2043				(ii(7, 7), false),
2044				(ie(14, 16), false),
2045			],
2046		);
2047		assert_insert_merge_touching_or_overlapping(
2048			basic(),
2049			(ii(6, 11), false),
2050			ei(5, 11),
2051			[(ui(4), false), (ei(5, 11), false), (ie(14, 16), true)],
2052		);
2053		assert_insert_merge_touching_or_overlapping(
2054			basic(),
2055			(ii(15, 18), true),
2056			ii(14, 18),
2057			[
2058				(ui(4), false),
2059				(ee(5, 7), true),
2060				(ii(7, 7), false),
2061				(ii(14, 18), true),
2062			],
2063		);
2064		assert_insert_merge_touching_or_overlapping(
2065			basic(),
2066			(uu(), false),
2067			uu(),
2068			[(uu(), false)],
2069		);
2070		//the only difference from the insert_merge_overlapping
2071		assert_insert_merge_touching_or_overlapping(
2072			basic(),
2073			(ii(7, 14), false),
2074			ee(5, 16),
2075			[(ui(4), false), (ee(5, 16), false)],
2076		);
2077	}
2078	fn assert_insert_merge_touching_or_overlapping<const N: usize>(
2079		mut before: NoditMap<i8, Interval<i8>, bool>,
2080		to_insert: (Interval<i8>, bool),
2081		result: Interval<i8>,
2082		after: [(Interval<i8>, bool); N],
2083	) {
2084		assert_eq!(
2085			before
2086				.insert_merge_touching_or_overlapping(to_insert.0, to_insert.1),
2087			result
2088		);
2089		assert_eq!(before, NoditMap::from_slice_strict(after).unwrap())
2090	}
2091
2092	#[test]
2093	fn config_tests() {
2094		assert_eq!(config(ie(1, 4), ie(6, 8)), Config::LeftFirstNonOverlapping);
2095		assert_eq!(config(ie(1, 4), ie(2, 8)), Config::LeftFirstPartialOverlap);
2096		assert_eq!(config(ie(1, 4), ie(2, 3)), Config::LeftContainsRight);
2097
2098		assert_eq!(
2099			config(ie(6, 8), ie(1, 4)),
2100			Config::RightFirstNonOverlapping
2101		);
2102		assert_eq!(
2103			config(ie(2, 8), ie(1, 4)),
2104			Config::RightFirstPartialOverlap
2105		);
2106		assert_eq!(config(ie(2, 3), ie(1, 4)), Config::RightContainsLeft);
2107	}
2108
2109	#[test]
2110	fn overlaps_tests() {
2111		for interval1 in all_valid_test_bounds() {
2112			for interval2 in all_valid_test_bounds() {
2113				let our_answer = interval1.overlaps(&interval2);
2114
2115				let mathematical_definition_of_overlap =
2116					NUMBERS_DOMAIN.iter().any(|x| {
2117						contains_point(interval1, *x)
2118							&& contains_point(interval2, *x)
2119					});
2120
2121				if our_answer != mathematical_definition_of_overlap {
2122					dbg!(interval1, interval2);
2123					dbg!(mathematical_definition_of_overlap, our_answer);
2124					panic!("Discrepancy in overlaps() detected!");
2125				}
2126			}
2127		}
2128	}
2129
2130	#[test]
2131	fn cut_interval_tests() {
2132		for base in all_valid_test_bounds() {
2133			for cut in all_valid_test_bounds() {
2134				let cut_result @ CutResult {
2135					before_cut: b,
2136					inside_cut: i,
2137					after_cut: a,
2138				} = cut_interval(base, cut);
2139
2140				let mut on_left = true;
2141
2142				// The definition of a cut is: A && NOT B
2143				for x in NUMBERS_DOMAIN {
2144					let base_contains = contains_point(base, *x);
2145					let cut_contains = contains_point(cut, *x);
2146
2147					if cut_contains {
2148						on_left = false;
2149					}
2150
2151					let invariant = match (base_contains, cut_contains) {
2152						(false, _) => !con(b, x) && !con(i, x) && !con(a, x),
2153						(true, false) => {
2154							if on_left {
2155								con(b, x) && !con(i, x) && !con(a, x)
2156							} else {
2157								!con(b, x) && !con(i, x) && con(a, x)
2158							}
2159						}
2160						(true, true) => !con(b, x) && con(i, x) && !con(a, x),
2161					};
2162
2163					if !invariant {
2164						dbg!(base_contains);
2165						dbg!(cut_contains);
2166
2167						dbg!(on_left);
2168
2169						dbg!(base);
2170						dbg!(cut);
2171						dbg!(cut_result);
2172
2173						dbg!(x);
2174
2175						panic!("Invariant Broken!");
2176					}
2177				}
2178			}
2179		}
2180	}
2181	fn con(x: Option<Interval<i8>>, point: &i8) -> bool {
2182		match x {
2183			Some(y) => contains_point(y, *point),
2184			None => false,
2185		}
2186	}
2187	#[test]
2188	fn cut_interval_should_return_valid_intervals() {
2189		let result: CutResult<i8> = cut_interval(ie(3, 8), ie(5, 8));
2190		if let Some(x) = result.before_cut {
2191			assert!(x.is_valid());
2192		}
2193		if let Some(x) = result.inside_cut {
2194			assert!(x.is_valid());
2195		}
2196		if let Some(x) = result.after_cut {
2197			assert!(x.is_valid());
2198		}
2199
2200		let result = cut_interval(ie(3, 8), ie(3, 5));
2201		if let Some(x) = result.before_cut {
2202			assert!(x.is_valid());
2203		}
2204		if let Some(x) = result.inside_cut {
2205			assert!(x.is_valid());
2206		}
2207		if let Some(x) = result.after_cut {
2208			assert!(x.is_valid());
2209		}
2210	}
2211
2212	#[test]
2213	fn test_intersection() {
2214		let input = Interval { start: 5, end: 10 };
2215		assert_eq!(
2216			input.intersection(&Interval { start: 8, end: 13 }),
2217			Some(Interval { start: 8, end: 10 })
2218		);
2219		assert_eq!(
2220			input.intersection(&Interval { start: 10, end: 13 }),
2221			Some(Interval { start: 10, end: 10 })
2222		);
2223		assert_eq!(input.intersection(&Interval { start: 11, end: 13 }), None);
2224	}
2225
2226	#[test]
2227	fn test_translate() {
2228		let input = Interval { start: 5, end: 10 };
2229		assert_eq!(input.translate(3), Interval { start: 8, end: 13 });
2230		assert_eq!(input.translate(-2), Interval { start: 3, end: 8 });
2231	}
2232
2233	// Test Helper Functions
2234	//======================
2235	fn all_non_overlapping_test_bound_entries()
2236	-> Vec<(Interval<i8>, Interval<i8>)> {
2237		let mut output = Vec::new();
2238		for test_bounds1 in all_valid_test_bounds() {
2239			for test_bounds2 in all_valid_test_bounds() {
2240				if !test_bounds1.overlaps(&test_bounds2) {
2241					output.push((test_bounds1, test_bounds2));
2242				}
2243			}
2244		}
2245
2246		output
2247	}
2248
2249	fn all_valid_test_bounds() -> Vec<Interval<i8>> {
2250		let mut output = Vec::new();
2251		for i in NUMBERS {
2252			for j in NUMBERS {
2253				if i <= j {
2254					output.push(Interval { start: *i, end: *j });
2255				}
2256			}
2257		}
2258		output
2259	}
2260}