1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
use std::{ borrow::Borrow, cmp::Ordering, collections::Bound, ops::{Range, RangeBounds, RangeFrom, RangeFull, RangeInclusive, RangeTo, RangeToInclusive}, }; use super::bound::{BorrowPartialOrd, EndBound, StartBound}; #[derive(Copy, Clone, Eq, PartialEq, Debug, Hash)] pub struct Interval<T> { pub start: StartBound<T>, pub end: EndBound<T>, } impl<T> Interval<T> { pub const fn new(start: Bound<T>, end: Bound<T>) -> Self { Self { start: StartBound(start), end: EndBound(end), } } pub fn is_empty(&self) -> bool where T: PartialOrd, { !(self.start <= self.end) } pub(super) fn remove(self, other: &Self) -> [Option<Interval<T>>; 2] where T: PartialOrd + Clone, { if self.end < other.start { [Some(self), None] } else if self.start > other.end { [None, Some(self)] } else { [ if self.start < other.start { Some(Interval { start: self.start, end: other.start.clone().into(), }) } else { None }, if self.end > other.end { Some(Interval { start: other.end.clone().into(), end: self.end.into(), }) } else { None }, ] } } fn borrow_contains<Q>(&self, other: &Q) -> bool where T: Borrow<Q>, Q: ?Sized + PartialOrd, { matches!(self.partial_cmp(other), Some(Ordering::Equal)) } } impl<T> RangeBounds<T> for Interval<T> { fn start_bound(&self) -> Bound<&T> { use Bound::*; match self.start.0 { Excluded(ref v) => Excluded(v), Included(ref v) => Included(v), Unbounded => Unbounded, } } fn end_bound(&self) -> Bound<&T> { use Bound::*; match self.end.0 { Excluded(ref v) => Excluded(v), Included(ref v) => Included(v), Unbounded => Unbounded, } } } impl<T, Q> PartialEq<Q> for Interval<T> where T: Borrow<Q>, Q: ?Sized + PartialOrd, { fn eq(&self, other: &Q) -> bool { self.borrow_contains(other) } } impl<T, Q> PartialOrd<Q> for Interval<T> where T: Borrow<Q>, Q: ?Sized + PartialOrd, { fn partial_cmp(&self, other: &Q) -> Option<Ordering> { match self.start.borrow_partial_cmp(other) { Some(Ordering::Less) | Some(Ordering::Equal) => { match self.end.borrow_partial_cmp(other) { Some(Ordering::Greater) | Some(Ordering::Equal) => Some(Ordering::Equal), x => x, } } x => x, } } } impl<T> From<Range<T>> for Interval<T> { fn from(r: Range<T>) -> Self { Self::new(Bound::Included(r.start), Bound::Excluded(r.end)) } } impl<T> From<RangeInclusive<T>> for Interval<T> { fn from(r: RangeInclusive<T>) -> Self { let (start, end) = r.into_inner(); Self::new(Bound::Included(start), Bound::Included(end)) } } impl<T> From<RangeFrom<T>> for Interval<T> { fn from(r: RangeFrom<T>) -> Self { Self::new(Bound::Included(r.start), Bound::Unbounded) } } impl<T> From<RangeTo<T>> for Interval<T> { fn from(r: RangeTo<T>) -> Self { Self::new(Bound::Unbounded, Bound::Excluded(r.end)) } } impl<T> From<RangeToInclusive<T>> for Interval<T> { fn from(r: RangeToInclusive<T>) -> Self { Self::new(Bound::Unbounded, Bound::Included(r.end)) } } impl<T> From<RangeFull> for Interval<T> { fn from(_: RangeFull) -> Self { Self::new(Bound::Unbounded, Bound::Unbounded) } } #[cfg(feature = "proc-macro")] use { proc_macro2::TokenStream, quote::{quote, ToTokens}, }; #[cfg(feature = "proc-macro")] impl<T: ToTokens> ToTokens for Interval<T> { fn to_tokens(&self, tokens: &mut TokenStream) { let s = &self.start; let e = &self.end; tokens.extend(quote!( interval_map::Interval { start: #s, end: #e, })); } } #[cfg(test)] mod tests { use super::*; #[test] fn partial_cmp() { assert_eq!( Interval::new(Bound::Excluded(0), Bound::Excluded(1)).partial_cmp(&0), Some(Ordering::Greater) ); } #[cfg(feature = "proc-macro")] #[test] fn to_tokens() { let i: Interval<_> = (0..1).into(); assert_eq!( i.to_token_stream().to_string(), quote!(interval_map::Interval { start: interval_map::bound::StartBound(std::collections::Bound::Included(0i32)), end: interval_map::bound::EndBound(std::collections::Bound::Excluded(1i32)), }) .to_string() ); } }