pub struct Extent<N: PrimInt> { /* private fields */ }
Implementations§
source§impl<N: PrimInt> Extent<N>
impl<N: PrimInt> Extent<N>
pub fn lo(&self) -> Option<N>
pub fn hi(&self) -> Option<N>
pub unsafe fn lo_unchecked(&self) -> N
pub unsafe fn hi_unchecked(&self) -> N
sourcepub fn len(&self) -> Option<usize>
pub fn len(&self) -> Option<usize>
Returns the number of values included in this extent, as a usize, if the elements of the extent and the quantity of values between them, inclusively, can all be represented exactly as a usize. Various conditions may make this impossible: if the extent uses a numeric type larger than a usize, for example, or even if it includes the entire range of possible usize values (the quantity of which are a number one greater than the largest representable usize). This function also returns None for any extents involving negatve values of signed types (some such cases could still be represented as usize differences from lo-to-hi, but it is fussy to get the cases all right and this is not the main use-case for this library).
sourcepub fn len_u64(&self) -> Option<u64>
pub fn len_u64(&self) -> Option<u64>
Same as len() but targeting u64 rather than usize. Depending on use-case, some contexts prefer measuring extents against a definite-sized type.
sourcepub fn empty() -> Self
pub fn empty() -> Self
Examples found in repository?
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 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
fn default() -> Self {
Self::empty()
}
}
impl<N: PrimInt> Extent<N> {
pub fn lo(&self) -> Option<N> {
if self.is_empty() {
None
} else {
Some(self.lo)
}
}
pub fn hi(&self) -> Option<N> {
if self.is_empty() {
None
} else {
Some(self.hi)
}
}
pub unsafe fn lo_unchecked(&self) -> N {
self.lo
}
pub unsafe fn hi_unchecked(&self) -> N {
self.hi
}
/// Returns the number of values included in this extent, as a usize, if the
/// elements of the extent _and_ the quantity of values between them,
/// inclusively, can all be represented exactly as a usize. Various
/// conditions may make this impossible: if the extent uses a numeric type
/// larger than a usize, for example, or even if it includes the entire
/// range of possible usize values (the quantity of which are a number one
/// greater than the largest representable usize). This function also
/// returns None for any extents involving negatve values of signed types
/// (some such cases could still be represented as usize _differences_ from
/// lo-to-hi, but it is fussy to get the cases all right and this is not the
/// main use-case for this library).
pub fn len(&self) -> Option<usize> {
if self.is_empty() {
Some(0)
} else if let (Some(lo), Some(hi)) = (self.lo.to_usize(), self.hi.to_usize()) {
let exclusive_range: usize = hi - lo;
if exclusive_range < usize::MAX {
Some(exclusive_range + 1)
} else {
None
}
} else {
None
}
}
/// Same as len() but targeting u64 rather than usize. Depending on
/// use-case, some contexts prefer measuring extents against a
/// definite-sized type.
pub fn len_u64(&self) -> Option<u64> {
if self.is_empty() {
Some(0)
} else if let (Some(lo), Some(hi)) = (self.lo.to_u64(), self.hi.to_u64()) {
let exclusive_range: u64 = hi - lo;
if exclusive_range < u64::MAX {
Some(exclusive_range + 1)
} else {
None
}
} else {
None
}
}
pub fn empty() -> Self {
Self {
lo: N::one(),
hi: N::zero(),
}
}
pub fn is_empty(&self) -> bool {
self.lo > self.hi
}
pub fn new<T: Borrow<N>, U: Borrow<N>>(lo: T, hi: U) -> Self {
let lo: N = *lo.borrow();
let hi: N = *hi.borrow();
Self {
lo: lo.min(hi),
hi: hi.max(lo),
}
}
pub unsafe fn new_unchecked<T: Borrow<N>, U: Borrow<N>>(lo: T, hi: U) -> Self {
let lo: N = *lo.borrow();
let hi: N = *hi.borrow();
if lo > hi {
Self::empty()
} else {
Self { lo, hi }
}
}
pub fn union<S: Borrow<Self>>(&self, other: S) -> Self {
if self.is_empty() {
*other.borrow()
} else if other.borrow().is_empty() {
self.clone()
} else {
let other = *other.borrow();
Self::new(self.lo.min(other.lo), self.hi.max(other.hi))
}
}
pub fn intersect<S: Borrow<Self>>(&self, other: S) -> Self {
if self.is_empty() || other.borrow().is_empty() {
Extent::empty()
} else {
let other = *other.borrow();
Self::new(&self.lo.max(other.lo), &self.hi.min(other.hi))
}
}
pub fn contains<T: Borrow<N>>(&self, n: T) -> bool {
let n = *n.borrow();
self.lo <= n && n <= self.hi
}
pub fn iter(&self) -> ExtentIter<N> {
ExtentIter(*self)
}
}
#[derive(Clone, Debug, Default)]
pub struct ExtentIter<N: PrimInt>(Extent<N>);
impl<N: PrimInt> Iterator for ExtentIter<N> {
type Item = N;
fn next(&mut self) -> Option<Self::Item> {
if self.0.is_empty() {
None
} else {
let v = self.0.lo;
self.0.lo = self.0.lo + N::one();
Some(v)
}
}
}
impl<N: PrimInt> ExtentIter<N> {
pub fn rev(self) -> ExtentRevIter<N> {
ExtentRevIter(self.0)
}
}
pub struct ExtentRevIter<N: PrimInt>(Extent<N>);
impl<N: PrimInt> Iterator for ExtentRevIter<N> {
type Item = N;
fn next(&mut self) -> Option<Self::Item> {
if self.0.is_empty() {
None
} else {
let v = self.0.hi;
self.0.hi = self.0.hi - N::one();
Some(v)
}
}
}
// std::ops::Range is an exclusive range. Extent is inclusive,
// so we subtract one from any nonempty std::ops::Range.
impl<N: PrimInt> From<Range<N>> for Extent<N> {
fn from(r: Range<N>) -> Self {
if r.is_empty() {
Self::empty()
} else {
Self {
lo: r.start,
hi: r.end - N::one(),
}
}
}
}
impl<N: PrimInt> TryFrom<Extent<N>> for Range<N> {
type Error = &'static str;
fn try_from(e: Extent<N>) -> Result<Self, Self::Error> {
if e.is_empty() {
Ok(Range {
start: N::zero(),
end: N::zero(),
})
} else if e.hi == N::max_value() {
Err("Extent.hi is N::max_value(), can't represent as Range")
} else {
Ok(Range {
start: e.lo,
end: e.hi + N::one(),
})
}
}
}
impl<N: PrimInt> From<RangeInclusive<N>> for Extent<N> {
fn from(r: RangeInclusive<N>) -> Self {
if r.is_empty() {
Self::empty()
} else {
Self::new(r.start(), r.end())
}
}
sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Examples found in repository?
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 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
pub fn lo(&self) -> Option<N> {
if self.is_empty() {
None
} else {
Some(self.lo)
}
}
pub fn hi(&self) -> Option<N> {
if self.is_empty() {
None
} else {
Some(self.hi)
}
}
pub unsafe fn lo_unchecked(&self) -> N {
self.lo
}
pub unsafe fn hi_unchecked(&self) -> N {
self.hi
}
/// Returns the number of values included in this extent, as a usize, if the
/// elements of the extent _and_ the quantity of values between them,
/// inclusively, can all be represented exactly as a usize. Various
/// conditions may make this impossible: if the extent uses a numeric type
/// larger than a usize, for example, or even if it includes the entire
/// range of possible usize values (the quantity of which are a number one
/// greater than the largest representable usize). This function also
/// returns None for any extents involving negatve values of signed types
/// (some such cases could still be represented as usize _differences_ from
/// lo-to-hi, but it is fussy to get the cases all right and this is not the
/// main use-case for this library).
pub fn len(&self) -> Option<usize> {
if self.is_empty() {
Some(0)
} else if let (Some(lo), Some(hi)) = (self.lo.to_usize(), self.hi.to_usize()) {
let exclusive_range: usize = hi - lo;
if exclusive_range < usize::MAX {
Some(exclusive_range + 1)
} else {
None
}
} else {
None
}
}
/// Same as len() but targeting u64 rather than usize. Depending on
/// use-case, some contexts prefer measuring extents against a
/// definite-sized type.
pub fn len_u64(&self) -> Option<u64> {
if self.is_empty() {
Some(0)
} else if let (Some(lo), Some(hi)) = (self.lo.to_u64(), self.hi.to_u64()) {
let exclusive_range: u64 = hi - lo;
if exclusive_range < u64::MAX {
Some(exclusive_range + 1)
} else {
None
}
} else {
None
}
}
pub fn empty() -> Self {
Self {
lo: N::one(),
hi: N::zero(),
}
}
pub fn is_empty(&self) -> bool {
self.lo > self.hi
}
pub fn new<T: Borrow<N>, U: Borrow<N>>(lo: T, hi: U) -> Self {
let lo: N = *lo.borrow();
let hi: N = *hi.borrow();
Self {
lo: lo.min(hi),
hi: hi.max(lo),
}
}
pub unsafe fn new_unchecked<T: Borrow<N>, U: Borrow<N>>(lo: T, hi: U) -> Self {
let lo: N = *lo.borrow();
let hi: N = *hi.borrow();
if lo > hi {
Self::empty()
} else {
Self { lo, hi }
}
}
pub fn union<S: Borrow<Self>>(&self, other: S) -> Self {
if self.is_empty() {
*other.borrow()
} else if other.borrow().is_empty() {
self.clone()
} else {
let other = *other.borrow();
Self::new(self.lo.min(other.lo), self.hi.max(other.hi))
}
}
pub fn intersect<S: Borrow<Self>>(&self, other: S) -> Self {
if self.is_empty() || other.borrow().is_empty() {
Extent::empty()
} else {
let other = *other.borrow();
Self::new(&self.lo.max(other.lo), &self.hi.min(other.hi))
}
}
pub fn contains<T: Borrow<N>>(&self, n: T) -> bool {
let n = *n.borrow();
self.lo <= n && n <= self.hi
}
pub fn iter(&self) -> ExtentIter<N> {
ExtentIter(*self)
}
}
#[derive(Clone, Debug, Default)]
pub struct ExtentIter<N: PrimInt>(Extent<N>);
impl<N: PrimInt> Iterator for ExtentIter<N> {
type Item = N;
fn next(&mut self) -> Option<Self::Item> {
if self.0.is_empty() {
None
} else {
let v = self.0.lo;
self.0.lo = self.0.lo + N::one();
Some(v)
}
}
}
impl<N: PrimInt> ExtentIter<N> {
pub fn rev(self) -> ExtentRevIter<N> {
ExtentRevIter(self.0)
}
}
pub struct ExtentRevIter<N: PrimInt>(Extent<N>);
impl<N: PrimInt> Iterator for ExtentRevIter<N> {
type Item = N;
fn next(&mut self) -> Option<Self::Item> {
if self.0.is_empty() {
None
} else {
let v = self.0.hi;
self.0.hi = self.0.hi - N::one();
Some(v)
}
}
}
// std::ops::Range is an exclusive range. Extent is inclusive,
// so we subtract one from any nonempty std::ops::Range.
impl<N: PrimInt> From<Range<N>> for Extent<N> {
fn from(r: Range<N>) -> Self {
if r.is_empty() {
Self::empty()
} else {
Self {
lo: r.start,
hi: r.end - N::one(),
}
}
}
}
impl<N: PrimInt> TryFrom<Extent<N>> for Range<N> {
type Error = &'static str;
fn try_from(e: Extent<N>) -> Result<Self, Self::Error> {
if e.is_empty() {
Ok(Range {
start: N::zero(),
end: N::zero(),
})
} else if e.hi == N::max_value() {
Err("Extent.hi is N::max_value(), can't represent as Range")
} else {
Ok(Range {
start: e.lo,
end: e.hi + N::one(),
})
}
}
sourcepub fn new<T: Borrow<N>, U: Borrow<N>>(lo: T, hi: U) -> Self
pub fn new<T: Borrow<N>, U: Borrow<N>>(lo: T, hi: U) -> Self
Examples found in repository?
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 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
pub fn union<S: Borrow<Self>>(&self, other: S) -> Self {
if self.is_empty() {
*other.borrow()
} else if other.borrow().is_empty() {
self.clone()
} else {
let other = *other.borrow();
Self::new(self.lo.min(other.lo), self.hi.max(other.hi))
}
}
pub fn intersect<S: Borrow<Self>>(&self, other: S) -> Self {
if self.is_empty() || other.borrow().is_empty() {
Extent::empty()
} else {
let other = *other.borrow();
Self::new(&self.lo.max(other.lo), &self.hi.min(other.hi))
}
}
pub fn contains<T: Borrow<N>>(&self, n: T) -> bool {
let n = *n.borrow();
self.lo <= n && n <= self.hi
}
pub fn iter(&self) -> ExtentIter<N> {
ExtentIter(*self)
}
}
#[derive(Clone, Debug, Default)]
pub struct ExtentIter<N: PrimInt>(Extent<N>);
impl<N: PrimInt> Iterator for ExtentIter<N> {
type Item = N;
fn next(&mut self) -> Option<Self::Item> {
if self.0.is_empty() {
None
} else {
let v = self.0.lo;
self.0.lo = self.0.lo + N::one();
Some(v)
}
}
}
impl<N: PrimInt> ExtentIter<N> {
pub fn rev(self) -> ExtentRevIter<N> {
ExtentRevIter(self.0)
}
}
pub struct ExtentRevIter<N: PrimInt>(Extent<N>);
impl<N: PrimInt> Iterator for ExtentRevIter<N> {
type Item = N;
fn next(&mut self) -> Option<Self::Item> {
if self.0.is_empty() {
None
} else {
let v = self.0.hi;
self.0.hi = self.0.hi - N::one();
Some(v)
}
}
}
// std::ops::Range is an exclusive range. Extent is inclusive,
// so we subtract one from any nonempty std::ops::Range.
impl<N: PrimInt> From<Range<N>> for Extent<N> {
fn from(r: Range<N>) -> Self {
if r.is_empty() {
Self::empty()
} else {
Self {
lo: r.start,
hi: r.end - N::one(),
}
}
}
}
impl<N: PrimInt> TryFrom<Extent<N>> for Range<N> {
type Error = &'static str;
fn try_from(e: Extent<N>) -> Result<Self, Self::Error> {
if e.is_empty() {
Ok(Range {
start: N::zero(),
end: N::zero(),
})
} else if e.hi == N::max_value() {
Err("Extent.hi is N::max_value(), can't represent as Range")
} else {
Ok(Range {
start: e.lo,
end: e.hi + N::one(),
})
}
}
}
impl<N: PrimInt> From<RangeInclusive<N>> for Extent<N> {
fn from(r: RangeInclusive<N>) -> Self {
if r.is_empty() {
Self::empty()
} else {
Self::new(r.start(), r.end())
}
}
pub unsafe fn new_unchecked<T: Borrow<N>, U: Borrow<N>>(lo: T, hi: U) -> Self
pub fn union<S: Borrow<Self>>(&self, other: S) -> Self
pub fn intersect<S: Borrow<Self>>(&self, other: S) -> Self
pub fn contains<T: Borrow<N>>(&self, n: T) -> bool
pub fn iter(&self) -> ExtentIter<N> ⓘ
Trait Implementations§
source§impl<N: PrimInt> From<RangeInclusive<N>> for Extent<N>
impl<N: PrimInt> From<RangeInclusive<N>> for Extent<N>
source§fn from(r: RangeInclusive<N>) -> Self
fn from(r: RangeInclusive<N>) -> Self
source§impl<N: Ord + PrimInt> Ord for Extent<N>
impl<N: Ord + PrimInt> Ord for Extent<N>
source§impl<N: PartialEq + PrimInt> PartialEq<Extent<N>> for Extent<N>
impl<N: PartialEq + PrimInt> PartialEq<Extent<N>> for Extent<N>
source§impl<N: PartialOrd + PrimInt> PartialOrd<Extent<N>> for Extent<N>
impl<N: PartialOrd + PrimInt> PartialOrd<Extent<N>> for Extent<N>
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read more