intervals 2.1.0

A generic interval type with support for open/closed bounds.
Documentation
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
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
//! Generalised types for intervals and partitions thereof.
//!
//! `intervals` is oriented towards static-typing. The bounds are all
//! unique types, all operations between instances are exhaustively
//! implemented, and formatting is provided for ease-of-use.
extern crate num_traits;

#[cfg_attr(feature = "serde", macro_use)]
#[cfg(feature = "serde")]
extern crate serde_crate;

use num_traits::{Zero, One, Unsigned};

mod private {
    pub trait Sealed {}
}

pub mod bounds;
pub mod partitions;

pub type Result<T, L, R> = std::result::Result<T, bounds::ValidationError<L, R>>;
pub type IntervalResult<L, R = L> = Result<Interval<L, R>, L, R>;

/// Generalised type representing an interval between two points: a and b.
///
/// # Examples
/// ```
/// # #[macro_use] extern crate intervals;
/// # use intervals::{Interval, bounds};
/// let x = Interval::closed_unchecked(-1.0, 0.0);
/// let y = Interval::closed_unchecked(0.0, 1.0);
///
/// assert!(x.contains(-0.5));
/// assert!(y.contains(0.5));
///
/// assert_eq!(x.intersect(y).unwrap(), Interval::degenerate(0.0));
/// ```
#[derive(Debug, Clone, Copy)]
#[cfg_attr(
    feature = "serde",
    derive(Serialize, Deserialize),
    serde(crate = "serde_crate")
)]
pub struct Interval<L: bounds::Bound, R: bounds::Bound<Value = L::Value>> {
    /// The left-hand bound.
    pub left: L,

    /// The right-hand bound.
    pub right: R,
}

/// Alias for an unbounded interval.
pub type Unbounded<V> = Interval<bounds::NoBound<V>, bounds::NoBound<V>>;

/// Alias for a bounded open interval.
pub type Open<V> = Interval<bounds::Open<V>, bounds::Open<V>>;

/// Alias for a left-open interval, unbounded on the right.
pub type LeftOpen<V> = Interval<bounds::Open<V>, bounds::NoBound<V>>;

/// Alias for a right-open interval, unbounded on the left.
pub type RightOpen<V> = Interval<bounds::NoBound<V>, bounds::Open<V>>;

/// Alias for a bounded closed interval.
pub type Closed<V> = Interval<bounds::Closed<V>, bounds::Closed<V>>;

/// Alias for a left-closed interval, unbounded on the right.
pub type LeftClosed<V> = Interval<bounds::Closed<V>, bounds::NoBound<V>>;

/// Alias for a right-closed interval, unbounded on the left.
pub type RightClosed<V> = Interval<bounds::NoBound<V>, bounds::Closed<V>>;

/// Alias for a left-closed, right-open interval.
pub type LCRO<V> = Interval<bounds::Closed<V>, bounds::Open<V>>;

/// Alias for a left-open, right-closed interval.
pub type LORC<V> = Interval<bounds::Open<V>, bounds::Closed<V>>;

///////////////////////////////////////////////////////////////////////////////
// Core
///////////////////////////////////////////////////////////////////////////////
impl<L, R, LL, RR> PartialEq<Interval<LL, RR>> for Interval<L, R>
where
    L: bounds::Bound + PartialEq<LL>,
    R: bounds::Bound<Value = L::Value> + PartialEq<RR>,
    LL: bounds::Bound,
    RR: bounds::Bound<Value = LL::Value>,
{
    fn eq(&self, rhs: &Interval<LL, RR>) -> bool {
        self.left == rhs.left && self.right == rhs.right
    }
}

impl<L, R> Interval<L, R>
where
    L: bounds::Bound,
    R: bounds::Bound<Value = L::Value>,

    bounds::Validator: bounds::ValidateBounds<L, R>,
{
    /// Construct an interval with bound validation.
    pub fn new(left: L, right: R) -> IntervalResult<L, R> {
        bounds::validate(left, right).map(|(left, right)| Interval { left, right, })
    }
}

impl<L, R> Interval<L, R>
where
    L: bounds::Bound,
    R: bounds::Bound<Value = L::Value>,
{
    /// Construct an interval w/o bound validation.
    pub fn new_unchecked(left: L, right: R) -> Self { Interval { left, right, } }
}

impl<L: bounds::Bound> Interval<L, bounds::NoBound<L::Value>> {
    /// Construct a left-bounded interval, unbounded on the right.
    pub fn left_bounded(left: L) -> Self {
        Interval {
            left,
            right: bounds::NoBound::new(),
        }
    }
}

impl<V: PartialOrd> LeftOpen<V> {
    /// Construct a left-open interval, unbounded on the right.
    pub fn left_open(left: V) -> Self {
        Interval {
            left: bounds::Open(left),
            right: bounds::NoBound::new(),
        }
    }
}

impl<V: PartialOrd> LeftClosed<V> {
    /// Construct a left-closed interval, unbounded on the right.
    pub fn left_closed(left: V) -> Self {
        Interval {
            left: bounds::Closed(left),
            right: bounds::NoBound::new(),
        }
    }
}

impl<R: bounds::Bound> Interval<bounds::NoBound<R::Value>, R> {
    /// Construct a right-bounded interval, unbounded on the left.
    pub fn right_bounded(right: R) -> Self {
        Interval {
            left: bounds::NoBound::new(),
            right,
        }
    }
}

impl<V: PartialOrd> RightOpen<V> {
    /// Construct a right-open interval, unbounded on the left.
    pub fn right_open(right: V) -> Self {
        Interval {
            left: bounds::NoBound::new(),
            right: bounds::Open(right),
        }
    }
}

impl<V: PartialOrd> RightClosed<V> {
    /// Construct a right-closed interval, unbounded on the left.
    pub fn right_closed(right: V) -> Self {
        Interval {
            left: bounds::NoBound::new(),
            right: bounds::Closed(right),
        }
    }
}

impl<V: PartialOrd> LORC<V> {
    /// Construct a left-open, right-closed interval with bound validation.
    pub fn lorc(left: V, right: V) -> Result<Self, bounds::Open<V>, bounds::Closed<V>> {
        Interval::new(bounds::Open(left), bounds::Closed(right))
    }

    /// Construct a left-open, right-closed interval w/o bound validation.
    pub fn lorc_unchecked(left: V, right: V) -> Self {
        Interval::new_unchecked(bounds::Open(left), bounds::Closed(right))
    }
}

impl<V: PartialOrd> LCRO<V> {
    /// Construct a left-closed, right-open interval with bound validation.
    pub fn lcro(left: V, right: V) -> Result<Self, bounds::Closed<V>, bounds::Open<V>> {
        Interval::new(bounds::Closed(left), bounds::Open(right))
    }

    /// Construct a left-closed, right-open interval w/o bound validation.
    pub fn lcro_unchecked(left: V, right: V) -> Self {
        Interval::new_unchecked(bounds::Closed(left), bounds::Open(right))
    }
}

impl<V: PartialOrd> Unbounded<V> {
    /// Construct a totally unbounded interval.
    pub fn unbounded() -> Self {
        Interval {
            left: bounds::NoBound::new(),
            right: bounds::NoBound::new(),
        }
    }
}

impl<V: PartialOrd> Open<V> {
    /// Construct a bounded open interval with bound validation.
    pub fn open(left: V, right: V) -> IntervalResult<bounds::Open<V>, bounds::Open<V>> {
        Interval::new(bounds::Open(left), bounds::Open(right))
    }

    /// Construct a bounded open interval w/o bound validation.
    pub fn open_unchecked(left: V, right: V) -> Self {
        Interval::new_unchecked(bounds::Open(left), bounds::Open(right))
    }
}

impl<V: PartialOrd> Closed<V> {
    /// Construct a bounded closed interval with bound validation.
    pub fn closed(left: V, right: V) -> IntervalResult<bounds::Closed<V>, bounds::Closed<V>> {
        Interval::new(bounds::Closed(left), bounds::Closed(right))
    }

    /// Construct a bounded closed interval w/o bound validation.
    pub fn closed_unchecked(left: V, right: V) -> Self {
        Interval::new_unchecked(bounds::Closed(left), bounds::Closed(right))
    }
}

impl<V: PartialOrd + Clone> Closed<V> {
    /// Construct a degenerate interval: [a, a].
    pub fn degenerate(value: V) -> Self {
        Interval::new_unchecked(bounds::Closed(value.clone()), bounds::Closed(value))
    }
}

impl<V: Zero + One + PartialOrd> Closed<V> {
    /// Construct a unit interval: [0, 1].
    pub fn unit() -> Self {
        Interval::closed_unchecked(V::zero(), V::one())
    }
}

impl<V: PartialOrd> Closed<V> {
    /// Construct a uniform partition over the interval.
    pub fn linspace(self, n_partitions: usize) -> partitions::Uniform<V> {
        partitions::Uniform {
            size: n_partitions,
            left: self.left.0,
            right: self.right.0,
        }
    }
}

///////////////////////////////////////////////////////////////////////////////
// Ops
///////////////////////////////////////////////////////////////////////////////
/// Type alias to simplify intersection return types.
pub type IntersectionOf<L, R, LL, RR> = Interval<
    <L as bounds::Pinch<LL>>::Left,
    <R as bounds::Pinch<RR>>::Right,
>;

impl<L, R> Interval<L, R>
where
    L: bounds::Bound,
    R: bounds::Bound<Value = L::Value>,

    L::Value: PartialOrd,
{
    pub fn intersect<LL, RR>(self, other: Interval<LL, RR>) -> Option<IntersectionOf<L, R, LL, RR>>
    where
        L: bounds::Pinch<LL>,
        R: bounds::Pinch<RR>,

        LL: bounds::Bound,
        RR: bounds::Bound<Value = LL::Value>,

        bounds::Validator: bounds::ValidateBounds<L::Left, R::Right>,
    {
        let left = self.left.pinch_left(other.left);
        let right = self.right.pinch_right(other.right);

        Interval::new(left, right).ok()
    }
}

/// Type alias to simplify union-closure return types.
pub type UnionClosureOf<L, R, LL, RR> = Interval<
    <<L as bounds::Unroll<LL>>::Left as bounds::Bound>::WithLimit,
    <<R as bounds::Unroll<RR>>::Right as bounds::Bound>::WithLimit
>;

impl<L, R> Interval<L, R>
where
    L: bounds::Bound,
    R: bounds::Bound<Value = L::Value>,

    L::Value: PartialOrd,
{
    pub fn union_closure<LL, RR>(self, other: Interval<LL, RR>) -> UnionClosureOf<L, R, LL, RR>
    where
        L: bounds::Unroll<LL>,
        R: bounds::Unroll<RR>,

        LL: bounds::Bound,
        RR: bounds::Bound<Value = LL::Value>,
    {
        use bounds::Bound;

        let left = self.left.unroll_left(other.left).with_limit_point();
        let right = self.right.unroll_right(other.right).with_limit_point();

        Interval::new_unchecked(left, right)
    }
}

impl<L, R> Interval<L, R>
where
    L: bounds::Bound,
    R: bounds::Bound<Value = L::Value>,
{
    /// Returns true if the interval contains `val`.
    ///
    /// __Note__: see [Contains] for more details.
    pub fn contains(&self, val: L::Value) -> bool
    where
        Self: Contains<L, R>
    {
        Contains::<L, R>::contains(self, val)
    }

    /// Returns true if the interval is degenerate.
    ///
    /// A degenerate interval is bounded, where the upper and lower bounds are equal.
    ///
    /// # Examples
    /// ```
    /// # extern crate intervals;
    /// # use intervals::Interval;
    /// assert!(Interval::closed_unchecked(0.0, 0.0).is_degenerate());
    /// assert!(!Interval::open_unchecked(0.0, 0.0).is_degenerate());
    /// ```
    pub fn is_degenerate<'a>(&'a self) -> bool
    where
        &'a L::Value: PartialEq
    {
        match (self.left.value(), self.right.value()) {
            (Some(left), Some(right)) if self.left.is_closed() && self.right.is_closed()
                => left == right,
            _ => false,
        }
    }
}

///////////////////////////////////////////////////////////////////////////////
// Formatting
///////////////////////////////////////////////////////////////////////////////
impl<L, R> std::fmt::Display for Interval<L, R>
where
    L: bounds::BoundDisplay,
    R: bounds::BoundDisplay<Value = L::Value>,
{
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        self.left.fmt_left(f)
            .and_then(|_| write!(f, ", "))
            .and_then(|_| self.right.fmt_right(f))
    }
}

///////////////////////////////////////////////////////////////////////////////
// Boundedness
///////////////////////////////////////////////////////////////////////////////
/// Marker trait for bounded intervals.
///
/// A bounded interval is defined as any such interval for which the left/right
/// limits are open or closed.
pub trait Bounded: private::Sealed {}

macro_rules! impl_bounded {
    (V: $($trait:ident),* => $l:ty, $r:ty) => {
        impl<V: PartialOrd + $($trait),*> private::Sealed for Interval<$l, $r> {}
        impl<V: PartialOrd + $($trait),*> Bounded for Interval<$l, $r> {}
    };
    ($l:ty, $r:ty) => {
        impl<V: PartialOrd> private::Sealed for Interval<$l, $r> {}
        impl<V: PartialOrd> Bounded for Interval<$l, $r> {}
    };
}

impl_bounded!(V: Unsigned => bounds::NoBound<V>, bounds::Open<V>);
impl_bounded!(V: Unsigned => bounds::NoBound<V>, bounds::Closed<V>);

impl_bounded!(bounds::Closed<V>, bounds::Closed<V>);
impl_bounded!(bounds::Closed<V>, bounds::Open<V>);
impl_bounded!(bounds::Open<V>, bounds::Closed<V>);
impl_bounded!(bounds::Open<V>, bounds::Open<V>);

///////////////////////////////////////////////////////////////////////////////
// Containment
///////////////////////////////////////////////////////////////////////////////
/// Trait for intervals which can assert containment of their values.
pub trait Contains<L: bounds::Bound, R: bounds::Bound<Value = L::Value>> {
    /// Returns true if the interval contains `val`.
    ///
    /// # Examples
    /// ```
    /// # use intervals::{Interval, bounds};
    /// assert!(!Interval::unit().contains(-0.5));
    /// assert!(Interval::unit().contains(0.0));
    /// assert!(Interval::unit().contains(0.5));
    /// assert!(Interval::unit().contains(1.0));
    /// ```
    fn contains(&self, val: L::Value) -> bool;
}

impl<V: PartialOrd> Contains<bounds::NoBound<V>, bounds::NoBound<V>> for Unbounded<V> {
    fn contains(&self, _: V) -> bool { true }
}

impl<V: PartialOrd> Contains<bounds::Open<V>, bounds::Open<V>> for Open<V> {
    fn contains(&self, val: V) -> bool {
        val > self.left.0 && val < self.right.0
    }
}

impl<V: PartialOrd> Contains<bounds::Open<V>, bounds::NoBound<V>> for LeftOpen<V> {
    fn contains(&self, val: V) -> bool {
        val > self.left.0
    }
}

impl<V: PartialOrd> Contains<bounds::NoBound<V>, bounds::Open<V>> for RightOpen<V> {
    fn contains(&self, val: V) -> bool {
        val < self.right.0
    }
}

impl<V: PartialOrd> Contains<bounds::Closed<V>, bounds::Closed<V>> for Closed<V> {
    fn contains(&self, val: V) -> bool {
        val >= self.left.0 && val <= self.right.0
    }
}

impl<V: PartialOrd> Contains<bounds::Closed<V>, bounds::NoBound<V>> for LeftClosed<V> {
    fn contains(&self, val: V) -> bool {
        val >= self.left.0
    }
}

impl<V: PartialOrd> Contains<bounds::NoBound<V>, bounds::Closed<V>> for Closed<V> {
    fn contains(&self, val: V) -> bool {
        val <= self.right.0
    }
}

impl<V: PartialOrd> Contains<bounds::Closed<V>, bounds::Open<V>> for LCRO<V> {
    fn contains(&self, val: V) -> bool {
        val >= self.left.0 && val < self.right.0
    }
}

impl<V: PartialOrd> Contains<bounds::Open<V>, bounds::Closed<V>> for LORC<V> {
    fn contains(&self, val: V) -> bool {
        val > self.left.0 && val <= self.right.0
    }
}

impl<V: PartialOrd> Contains<bounds::NoBound<V>, bounds::OpenOrClosed<V>> for Interval<
    bounds::NoBound<V>, bounds::OpenOrClosed<V>
> {
    fn contains(&self, val: V) -> bool {
        match self.right {
            bounds::OpenOrClosed::Open(ref r) => val < *r,
            bounds::OpenOrClosed::Closed(ref r) => val <= *r,
        }
    }
}

impl<V: PartialOrd> Contains<bounds::Open<V>, bounds::OpenOrClosed<V>> for Interval<
    bounds::Open<V>, bounds::OpenOrClosed<V>
> {
    fn contains(&self, val: V) -> bool {
        val > self.left.0 && match &self.right {
            bounds::OpenOrClosed::Open(ref r) => val > *r,
            bounds::OpenOrClosed::Closed(ref r) => val <= *r,
        }
    }
}

impl<V: PartialOrd> Contains<bounds::Closed<V>, bounds::OpenOrClosed<V>> for Interval<
    bounds::Closed<V>, bounds::OpenOrClosed<V>
> {
    fn contains(&self, val: V) -> bool {
        val >= self.left.0 && match &self.right {
            bounds::OpenOrClosed::Open(ref r) => val > *r,
            bounds::OpenOrClosed::Closed(ref r) => val <= *r,
        }
    }
}

impl<V: PartialOrd> Contains<bounds::OpenOrClosed<V>, bounds::NoBound<V>> for Interval<
    bounds::OpenOrClosed<V>, bounds::NoBound<V>
> {
    fn contains(&self, val: V) -> bool {
        match self.left {
            bounds::OpenOrClosed::Open(ref l) => val > *l,
            bounds::OpenOrClosed::Closed(ref l) => val >= *l,
        }
    }
}

impl<V: PartialOrd> Contains<bounds::OpenOrClosed<V>, bounds::Open<V>> for Interval<
    bounds::OpenOrClosed<V>, bounds::Open<V>
> {
    fn contains(&self, val: V) -> bool {
        val < self.right.0 && match self.left {
            bounds::OpenOrClosed::Open(ref l) => val > *l,
            bounds::OpenOrClosed::Closed(ref l) => val >= *l,
        }
    }
}

impl<V: PartialOrd> Contains<bounds::OpenOrClosed<V>, bounds::Closed<V>> for Interval<
    bounds::OpenOrClosed<V>, bounds::Closed<V>
> {
    fn contains(&self, val: V) -> bool {
        val <= self.right.0 && match self.left {
            bounds::OpenOrClosed::Open(ref l) => val > *l,
            bounds::OpenOrClosed::Closed(ref l) => val >= *l,
        }
    }
}