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
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
use crate::bound_pair::BoundPair;
use either::Either;
use std::cmp::Ordering;

/// Interval enum capable of general interval representation
///
/// Where applicable, using lower bound `a` and upper bound `b`.  An Interval taxonomy was pulled from [proofwiki](https://proofwiki.org/wiki/Definition:Real_Interval_Types).
///
/// * Closed -> `[a, b]`
/// * Open -> `(a,b)`
/// * LeftHalfOpen -> `(a, b]`
/// * RightHalfOpen -> `[a, b)`
/// * UnboundedClosedRight -> `(-inf, a]`
/// * UnboundedOpenRight -> `(-inf, a)`
/// * UnboundedClosedLeft -> `[a, inf)`
/// * UnboundedOpenLeft -> `(a, inf)`
/// * Singeleton -> `[a]`
/// * Unbounded -> `(-inf, inf)`
/// * Empty
///
/// # Examples
///
/// ```
/// use intervals_general::bound_pair::BoundPair;
/// use intervals_general::interval::Interval;
/// # fn main() -> std::result::Result<(), String> {
/// let bounds = BoundPair::new(1.0, 2.0).ok_or("invalid BoundPair")?;
/// let right_half_open = Interval::RightHalfOpen { bound_pair: bounds }; // [1.0, 2.0)
/// # Ok(())
/// # }
/// ```
#[derive(Debug, Copy, Clone, PartialEq)]
pub enum Interval<T> {
    Closed { bound_pair: BoundPair<T> },
    Open { bound_pair: BoundPair<T> },
    LeftHalfOpen { bound_pair: BoundPair<T> },
    RightHalfOpen { bound_pair: BoundPair<T> },
    UnboundedClosedRight { right: T },
    UnboundedOpenRight { right: T },
    UnboundedClosedLeft { left: T },
    UnboundedOpenLeft { left: T },
    Singleton { at: T },
    Unbounded,
    Empty,
}

// Internally used to simplify matching functions on Intervals
enum Bound<T> {
    None,
    Unbounded,
    Open(T),
    Closed(T),
}

type TwoIntervalIter<T> =
    std::iter::Chain<std::iter::Once<Interval<T>>, std::iter::Once<Interval<T>>>;
type OneIntervalIter<T> = std::iter::Once<Interval<T>>;

impl<T> Interval<T>
where
    T: Copy,
    T: std::cmp::PartialOrd,
{
    /// Verify whether self contains the specified interval
    ///
    /// Interval I1.contains(I2) if and only if:
    ///
    /// * The left bound of I1 is bounded and less than or equal to the left
    ///   bound of I2 OR
    /// * the left bound of I1 is unbounded and the left bound of I2 is
    ///   unbounded
    ///
    /// AND
    ///
    /// * The right bound of I1 is bounded and greater than or equal to the
    ///   right bound of I2 OR
    /// * The right bound of I1 isunbounded and the left bound of I2 is
    ///   unbounded
    ///
    /// Additionally:
    ///
    /// * The Empty interval does not contain the Empty interval
    ///
    /// # Examples
    ///
    /// ```
    /// use intervals_general::bound_pair::BoundPair;
    /// use intervals_general::interval::Interval;
    /// # fn main() -> std::result::Result<(), String> {
    /// let right_half_open = Interval::RightHalfOpen {
    ///     bound_pair: BoundPair::new(1.0, 5.0).ok_or("invalid BoundPair")?,
    /// };
    /// let contained_interval = Interval::Open {
    ///     bound_pair: BoundPair::new(1.0, 2.0).ok_or("invalid BoundPair")?,
    /// };
    /// let non_contained_interval = Interval::Closed {
    ///     bound_pair: BoundPair::new(4.0, 5.0).ok_or("invalid BoundPair")?,
    /// };
    /// assert_eq!(right_half_open.contains(&contained_interval), true);
    /// assert_eq!(right_half_open.contains(&non_contained_interval), false);
    /// # Ok(())
    /// # }
    /// ```
    pub fn contains(&self, other: &Interval<T>) -> bool {
        let self_right_bound = self.to_right_bound();
        let other_right_bound = other.to_right_bound();
        let self_left_bound = self.to_left_bound();
        let other_left_bound = other.to_left_bound();

        let left_contained = match (self_left_bound, other_left_bound) {
            // The Empty Interval contains no other Intervals (even Empty)
            (Bound::None, _) => false,
            // The Empty interval is contained in all non-Empty Intervals
            (_, Bound::None) => true,
            // If self left interval is unbounded, it will contain any other left bound
            (Bound::Unbounded, _) => true,
            // Given self left interval is not unbounded and right is unbounded, self cannot contain
            // other
            (_, Bound::Unbounded) => false,
            (Bound::Closed(ref self_val), Bound::Closed(ref other_val))
            | (Bound::Closed(ref self_val), Bound::Open(ref other_val))
            | (Bound::Open(ref self_val), Bound::Open(ref other_val)) => self_val <= other_val,
            (Bound::Open(ref self_val), Bound::Closed(ref other_val)) => self_val < other_val,
        };

        let right_contained = match (self_right_bound, other_right_bound) {
            // The Empty interval does not contain the Empty interval
            (Bound::None, _) => false,
            (_, Bound::None) => false,
            // If self left interval is unbounded, it will contain any other left bound
            (Bound::Unbounded, _) => true,
            // Given self left interval is not unbounded and right is unbounded, self cannot contain
            // other
            (_, Bound::Unbounded) => false,
            (Bound::Closed(ref self_val), Bound::Closed(ref other_val))
            | (Bound::Closed(ref self_val), Bound::Open(ref other_val))
            | (Bound::Open(ref self_val), Bound::Open(ref other_val)) => self_val >= other_val,
            (Bound::Open(ref self_val), Bound::Closed(ref other_val)) => self_val > other_val,
        };

        left_contained && right_contained
    }

    /// Intersect an with the specified Interval
    ///
    /// Take the intersection of self with the specified Interval.
    ///
    /// # Examples
    ///
    /// ```
    /// use intervals_general::bound_pair::BoundPair;
    /// use intervals_general::interval::Interval;
    ///
    /// # fn main() -> std::result::Result<(), String> {
    /// let i1 = Interval::RightHalfOpen {
    ///     bound_pair: BoundPair::new(1, 5).ok_or("invalid BoundPair")?,
    /// };
    /// let i2 = Interval::Open {
    ///     bound_pair: BoundPair::new(-1, 2).ok_or("invalid BoundPair")?,
    /// };
    ///
    /// assert_eq!(
    ///     i1.intersect(&i2),
    ///     Interval::RightHalfOpen {
    ///         bound_pair: BoundPair::new(1, 2).ok_or("invalid BoundPair")?
    ///     }
    /// );
    /// # Ok(())
    /// # }
    /// ```
    pub fn intersect(&self, other: &Interval<T>) -> Interval<T> {
        let left_cmp_partial = self.left_partial_cmp(&other);
        let right_cmp_partial = self.right_partial_cmp(&other);
        if left_cmp_partial.is_none() || right_cmp_partial.is_none() {
            return Interval::Empty;
        }

        let left_bound = if left_cmp_partial != Some(Ordering::Less) {
            self.to_left_bound()
        } else {
            other.to_left_bound()
        };
        let right_bound = if right_cmp_partial != Some(Ordering::Greater) {
            self.to_right_bound()
        } else {
            other.to_right_bound()
        };

        match (left_bound, right_bound) {
            (Bound::None, _) => Interval::Empty,
            (_, Bound::None) => Interval::Empty,
            (Bound::Closed(left), Bound::Closed(right)) => {
                if left > right {
                    Interval::Empty
                } else {
                    Interval::Closed {
                        bound_pair: BoundPair { left, right },
                    }
                }
            }
            (Bound::Open(left), Bound::Open(right)) => {
                if left >= right {
                    Interval::Empty
                } else {
                    Interval::Open {
                        bound_pair: BoundPair { left, right },
                    }
                }
            }
            (Bound::Open(left), Bound::Closed(right)) => {
                if left >= right {
                    Interval::Empty
                } else {
                    Interval::LeftHalfOpen {
                        bound_pair: BoundPair { left, right },
                    }
                }
            }
            (Bound::Closed(left), Bound::Open(right)) => {
                if left >= right {
                    Interval::Empty
                } else {
                    Interval::RightHalfOpen {
                        bound_pair: BoundPair { left, right },
                    }
                }
            }
            (Bound::Unbounded, Bound::Closed(right)) => Interval::UnboundedClosedRight { right },
            (Bound::Unbounded, Bound::Open(right)) => Interval::UnboundedOpenRight { right },
            (Bound::Closed(left), Bound::Unbounded) => Interval::UnboundedClosedLeft { left },
            (Bound::Open(left), Bound::Unbounded) => Interval::UnboundedOpenLeft { left },
            (Bound::Unbounded, Bound::Unbounded) => Interval::Unbounded,
        }
    }

    fn to_left_bound(&self) -> Bound<T> {
        match self {
            Interval::Empty => Bound::None,
            Interval::Singleton { ref at } => Bound::Closed(*at),
            // The cases where left bound of self is open -inf
            Interval::Unbounded
            | Interval::UnboundedClosedRight { .. }
            | Interval::UnboundedOpenRight { .. } => Bound::Unbounded,
            // The cases where left bound of self is Closed and Bounded
            Interval::Closed {
                bound_pair: BoundPair { ref left, .. },
            }
            | Interval::RightHalfOpen {
                bound_pair: BoundPair { ref left, .. },
            }
            | Interval::UnboundedClosedLeft { ref left, .. } => Bound::Closed(*left),
            // The cases where left bound of self is Open and Bounded
            Interval::Open {
                bound_pair: BoundPair { ref left, .. },
            }
            | Interval::LeftHalfOpen {
                bound_pair: BoundPair { ref left, .. },
            }
            | Interval::UnboundedOpenLeft { ref left, .. } => Bound::Open(*left),
        }
    }

    fn to_right_bound(&self) -> Bound<T> {
        match self {
            Interval::Empty => Bound::None,
            Interval::Singleton { ref at } => Bound::Closed(*at),
            // The cases where right bound of self is open +inf
            Interval::Unbounded
            | Interval::UnboundedClosedLeft { .. }
            | Interval::UnboundedOpenLeft { .. } => Bound::Unbounded,
            // The cases where right bound of self is Closed and Bounded
            Interval::Closed {
                bound_pair: BoundPair { ref right, .. },
            }
            | Interval::LeftHalfOpen {
                bound_pair: BoundPair { ref right, .. },
            }
            | Interval::UnboundedClosedRight { ref right, .. } => Bound::Closed(*right),
            // The cases where right bound of self is Open and Bounded
            Interval::Open {
                bound_pair: BoundPair { ref right, .. },
            }
            | Interval::RightHalfOpen {
                bound_pair: BoundPair { ref right, .. },
            }
            | Interval::UnboundedOpenRight { ref right, .. } => Bound::Open(*right),
        }
    }

    /// The PartialOrd::partial_cmp implementation for left Bounds
    ///
    /// Though Intervals on some generics (e.g. integers) can supply [Ord](https://doc.rust-lang.org/std/cmp/trait.Ord.html) because they form a [total order](https://en.wikipedia.org/wiki/Total_order),
    /// unfortunately our floating point implementations break such properties.
    /// Therefore the best we can do under some generics is satisfy [PartialOrd](https://doc.rust-lang.org/std/cmp/trait.PartialOrd.html).
    ///
    /// # Examples
    ///
    /// ```
    /// use intervals_general::bound_pair::BoundPair;
    /// use intervals_general::interval::Interval;
    /// use std::cmp::Ordering;
    ///
    /// # fn main() -> std::result::Result<(), String> {
    /// let right_half_open = Interval::RightHalfOpen {
    ///     bound_pair: BoundPair::new(1.0, 5.0).ok_or("invalid BoundPair")?,
    /// };
    /// let contained_interval = Interval::Open {
    ///     bound_pair: BoundPair::new(1.0, 2.0).ok_or("invalid BoundPair")?,
    /// };
    ///
    /// assert_eq!(
    ///     contained_interval.left_partial_cmp(&right_half_open),
    ///     Some(Ordering::Greater)
    /// );
    /// # Ok(())
    /// # }
    /// ```
    pub fn left_partial_cmp(&self, other: &Interval<T>) -> Option<Ordering> {
        let self_left_bound = self.to_left_bound();
        let other_left_bound = other.to_left_bound();

        match (self_left_bound, other_left_bound) {
            (Bound::None, _) => None,
            (_, Bound::None) => None,
            // Handle all cases in which one left bound is Unbounded
            (Bound::Unbounded, Bound::Unbounded) => Some(Ordering::Equal),
            (Bound::Unbounded, _) => Some(Ordering::Less),
            (_, Bound::Unbounded) => Some(Ordering::Greater),
            // The cases where left bound of self is Closed and Bounded
            (Bound::Closed(self_val), Bound::Closed(other_val)) => {
                if self_val < other_val {
                    Some(Ordering::Less)
                } else if self_val > other_val {
                    Some(Ordering::Greater)
                } else {
                    Some(Ordering::Equal)
                }
            }
            (Bound::Closed(self_val), Bound::Open(other_val)) => {
                if self_val <= other_val {
                    Some(Ordering::Less)
                } else {
                    Some(Ordering::Greater)
                }
            }
            // The cases where left bound of self is Open and Bounded
            (Bound::Open(self_val), Bound::Closed(other_val)) => {
                if self_val < other_val {
                    Some(Ordering::Less)
                } else {
                    Some(Ordering::Greater)
                }
            }
            (Bound::Open(self_val), Bound::Open(other_val)) => {
                if self_val < other_val {
                    Some(Ordering::Less)
                } else if self_val > other_val {
                    Some(Ordering::Greater)
                } else {
                    Some(Ordering::Equal)
                }
            }
        }
    }

    /// The PartialOrd::partial_cmp implementation for right Bounds
    ///
    /// Though Intervals on some generics (e.g. integers) can supply [Ord](https://doc.rust-lang.org/std/cmp/trait.Ord.html) because they form a [total order](https://en.wikipedia.org/wiki/Total_order),
    /// unfortunately our floating point implementations break such properties.
    /// Therefore the best we can do under some generics is satisfy [PartialOrd](https://doc.rust-lang.org/std/cmp/trait.PartialOrd.html).
    ///
    /// # Examples
    ///
    /// ```
    /// use intervals_general::bound_pair::BoundPair;
    /// use intervals_general::interval::Interval;
    /// use std::cmp::Ordering;
    ///
    /// # fn main() -> std::result::Result<(), String> {
    /// let right_half_open = Interval::RightHalfOpen {
    ///     bound_pair: BoundPair::new(1.0, 5.0).ok_or("invalid BoundPair")?,
    /// };
    /// let contained_interval = Interval::Open {
    ///     bound_pair: BoundPair::new(1.0, 2.0).ok_or("invalid BoundPair")?,
    /// };
    ///
    /// assert_eq!(
    ///     contained_interval.right_partial_cmp(&right_half_open),
    ///     Some(Ordering::Less)
    /// );
    /// # Ok(())
    /// # }
    /// ```
    pub fn right_partial_cmp(&self, other: &Interval<T>) -> Option<Ordering> {
        let self_right_bound = self.to_right_bound();
        let other_right_bound = other.to_right_bound();

        match (self_right_bound, other_right_bound) {
            (Bound::None, _) => None,
            (_, Bound::None) => None,
            // Handle all cases in which one right bound is Unbounded
            (Bound::Unbounded, Bound::Unbounded) => Some(Ordering::Equal),
            (Bound::Unbounded, _) => Some(Ordering::Greater),
            (_, Bound::Unbounded) => Some(Ordering::Less),
            // The cases where right bound of self is Closed and Bounded
            (Bound::Closed(self_val), Bound::Closed(other_val)) => {
                if self_val < other_val {
                    Some(Ordering::Less)
                } else if self_val > other_val {
                    Some(Ordering::Greater)
                } else {
                    Some(Ordering::Equal)
                }
            }
            (Bound::Closed(self_val), Bound::Open(other_val)) => {
                if self_val < other_val {
                    Some(Ordering::Less)
                } else {
                    Some(Ordering::Greater)
                }
            }
            // The cases where right bound of self is Open and Bounded
            (Bound::Open(self_val), Bound::Closed(other_val)) => {
                if self_val <= other_val {
                    Some(Ordering::Less)
                } else {
                    Some(Ordering::Greater)
                }
            }
            (Bound::Open(self_val), Bound::Open(other_val)) => {
                if self_val < other_val {
                    Some(Ordering::Less)
                } else if self_val > other_val {
                    Some(Ordering::Greater)
                } else {
                    Some(Ordering::Equal)
                }
            }
        }
    }

    /// Compute the width of the interval
    ///
    /// Returns right - left bound, so long as finite, else None
    /// TODO How to handle overflow detection? I do not have access to check_sub
    /// due to generic? Presently for interval widths exceeding the Boundary
    /// type representation, panic occurs in debug mode and wrapping occurs
    /// in production mode.
    ///
    /// # Examples
    ///
    /// ```
    /// use intervals_general::bound_pair::BoundPair;
    /// use intervals_general::interval::Interval;
    ///
    /// # fn main() -> std::result::Result<(), String> {
    /// let interval = Interval::RightHalfOpen {
    ///     bound_pair: BoundPair::new(1, 5).ok_or("invalid BoundPair")?,
    /// };
    ///
    /// let width: i32 = interval.width().ok_or("width was None")?;
    /// assert_eq!(width, 4);
    /// # Ok(())
    /// # }
    /// ```
    pub fn width(&self) -> Option<<T as std::ops::Sub>::Output>
    where
        T: std::ops::Sub,
    {
        let self_left_bound = self.to_left_bound();
        let self_right_bound = self.to_right_bound();

        match (self_left_bound, self_right_bound) {
            (Bound::None, _) => None,
            (_, Bound::None) => None,
            (Bound::Unbounded, _) => None,
            (_, Bound::Unbounded) => None,
            (Bound::Closed(left), Bound::Closed(right)) => Some(right - left),
            (Bound::Closed(left), Bound::Open(right)) => Some(right - left),
            (Bound::Open(left), Bound::Closed(right)) => Some(right - left),
            (Bound::Open(left), Bound::Open(right)) => Some(right - left),
        }
    }

    /// Take the complement of the Interval, return one or two Intervals
    ///
    /// The return value is iterable and contains exclusively one or two
    /// Intervals, depending upon result.
    ///
    /// # Example
    ///
    /// ```
    /// use intervals_general::bound_pair::BoundPair;
    /// use intervals_general::interval::Interval;
    ///
    /// # fn main() -> std::result::Result<(), String> {
    /// let mut result_it =
    ///     Interval::Closed {
    ///         bound_pair: BoundPair::new(1, 5).ok_or("invalid BoundPair")?,
    ///     }
    ///     .complement();
    ///
    /// assert_eq!(
    ///     result_it.next(),
    ///     Some(Interval::UnboundedOpenRight { right: 1 })
    /// );
    /// assert_eq!(
    ///     result_it.next(),
    ///     Some(Interval::UnboundedOpenLeft{ left: 5 })
    /// );
    /// assert_eq!(
    ///     result_it.next(),
    ///     None
    /// );
    /// # Ok(())
    /// # }
    /// ```
    pub fn complement(&self) -> Either<OneIntervalIter<T>, TwoIntervalIter<T>> {
        match self {
            // Interval::Closed { bound_pair: bp } => Either::Left(std::iter::once(Interval::Empty)),
            Interval::Closed { bound_pair: bp } => Either::Right(
                std::iter::once(Interval::UnboundedOpenRight { right: bp.left }).chain(
                    std::iter::once(Interval::UnboundedOpenLeft { left: bp.right }),
                ),
            ),
            Interval::Open { bound_pair: bp } => Either::Right(
                std::iter::once(Interval::UnboundedClosedRight { right: bp.left }).chain(
                    std::iter::once(Interval::UnboundedClosedLeft { left: bp.right }),
                ),
            ),
            Interval::LeftHalfOpen { bound_pair: bp } => Either::Right(
                std::iter::once(Interval::UnboundedClosedRight { right: bp.left }).chain(
                    std::iter::once(Interval::UnboundedOpenLeft { left: bp.right }),
                ),
            ),
            Interval::RightHalfOpen { bound_pair: bp } => Either::Right(
                std::iter::once(Interval::UnboundedOpenRight { right: bp.left }).chain(
                    std::iter::once(Interval::UnboundedClosedLeft { left: bp.right }),
                ),
            ),
            Interval::UnboundedClosedRight { right: r } => {
                Either::Left(std::iter::once(Interval::UnboundedOpenLeft { left: *r }))
            }
            Interval::UnboundedOpenRight { right: r } => {
                Either::Left(std::iter::once(Interval::UnboundedClosedLeft { left: *r }))
            }
            Interval::UnboundedClosedLeft { left: l } => {
                Either::Left(std::iter::once(Interval::UnboundedOpenRight { right: *l }))
            }
            Interval::UnboundedOpenLeft { left: l } => {
                Either::Left(std::iter::once(Interval::UnboundedClosedRight {
                    right: *l,
                }))
            }
            Interval::Singleton { at: a } => Either::Right(
                std::iter::once(Interval::UnboundedOpenRight { right: *a })
                    .chain(std::iter::once(Interval::UnboundedOpenLeft { left: *a })),
            ),
            Interval::Unbounded => Either::Left(std::iter::once(Interval::Empty)),
            Interval::Empty => Either::Left(std::iter::once(Interval::Unbounded)),
        }
    }
}

/// Implement the Display trait for Intervals
///
/// Here I uses [Wirth Interval Notation](https://proofwiki.org/wiki/Mathematician:Niklaus_Emil_Wirth).
///
/// # Examples
///
/// ```
/// use intervals_general::bound_pair::BoundPair;
/// use intervals_general::interval::Interval;
///
/// # fn main() -> std::result::Result<(), String> {
/// let bp = BoundPair::new(1, 5).ok_or("invalid BoundPair")?;
///
/// assert_eq!(format!("{}", Interval::Closed { bound_pair: bp }), "[1..5]");
/// assert_eq!(
///     format!("{}", Interval::UnboundedOpenRight { right: 5 }),
///     "(←..5)"
/// );
/// # Ok(())
/// # }
/// ```
impl<T> std::fmt::Display for Interval<T>
where
    T: std::fmt::Debug,
{
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        match self {
            Interval::Closed {
                bound_pair:
                    BoundPair {
                        ref left,
                        ref right,
                    },
            } => write!(f, "[{:?}..{:?}]", left, right),
            Interval::Open {
                bound_pair:
                    BoundPair {
                        ref left,
                        ref right,
                    },
            } => write!(f, "({:?}..{:?})", left, right),
            Interval::LeftHalfOpen {
                bound_pair:
                    BoundPair {
                        ref left,
                        ref right,
                    },
            } => write!(f, "({:?}..{:?}]", left, right),
            Interval::RightHalfOpen {
                bound_pair:
                    BoundPair {
                        ref left,
                        ref right,
                    },
            } => write!(f, "[{:?}..{:?})", left, right),
            Interval::UnboundedClosedRight { ref right } => write!(f, "(←..{:?}]", right),
            Interval::UnboundedOpenRight { ref right } => write!(f, "(←..{:?})", right),
            Interval::UnboundedClosedLeft { ref left } => write!(f, "[{:?}..→)", left),
            Interval::UnboundedOpenLeft { ref left } => write!(f, "({:?}..→)", left),
            Interval::Singleton { ref at } => write!(f, "[{:?}]", at),
            Interval::Unbounded => write!(f, "(←..→)"),
            Interval::Empty => write!(f, "Empty"),
        }
    }
}

#[cfg(test)]
mod tests {
    use crate::bound_pair::BoundPair;
    use crate::interval::Interval;
    use quickcheck::TestResult;
    use quickcheck_macros::quickcheck;

    #[test]
    fn test_bounded_complements() {
        let bp = BoundPair::new(1, 5).unwrap();
        let mut it = Interval::Closed { bound_pair: bp }.complement();
        assert_eq!(it.next(), Some(Interval::UnboundedOpenRight { right: 1 }));
        assert_eq!(it.next(), Some(Interval::UnboundedOpenLeft { left: 5 }));
        assert_eq!(it.next(), None);

        it = Interval::Open { bound_pair: bp }.complement();
        assert_eq!(it.next(), Some(Interval::UnboundedClosedRight { right: 1 }));
        assert_eq!(it.next(), Some(Interval::UnboundedClosedLeft { left: 5 }));
        assert_eq!(it.next(), None);

        it = Interval::LeftHalfOpen { bound_pair: bp }.complement();
        assert_eq!(it.next(), Some(Interval::UnboundedClosedRight { right: 1 }));
        assert_eq!(it.next(), Some(Interval::UnboundedOpenLeft { left: 5 }));
        assert_eq!(it.next(), None);

        it = Interval::RightHalfOpen { bound_pair: bp }.complement();
        assert_eq!(it.next(), Some(Interval::UnboundedOpenRight { right: 1 }));
        assert_eq!(it.next(), Some(Interval::UnboundedClosedLeft { left: 5 }));
        assert_eq!(it.next(), None);
    }

    #[test]
    fn test_unbounded_complements() {
        let mut it = Interval::UnboundedClosedRight { right: 5 }.complement();
        assert_eq!(it.next(), Some(Interval::UnboundedOpenLeft { left: 5 }));
        assert_eq!(it.next(), None);

        it = Interval::UnboundedOpenRight { right: 5 }.complement();
        assert_eq!(it.next(), Some(Interval::UnboundedClosedLeft { left: 5 }));
        assert_eq!(it.next(), None);

        it = Interval::UnboundedClosedLeft { left: 1 }.complement();
        assert_eq!(it.next(), Some(Interval::UnboundedOpenRight { right: 1 }));
        assert_eq!(it.next(), None);

        it = Interval::UnboundedOpenLeft { left: 1 }.complement();
        assert_eq!(it.next(), Some(Interval::UnboundedClosedRight { right: 1 }));
        assert_eq!(it.next(), None);

        let mut it = Interval::Singleton { at: 2.0 }.complement();
        assert_eq!(it.next(), Some(Interval::UnboundedOpenRight { right: 2.0 }));
        assert_eq!(it.next(), Some(Interval::UnboundedOpenLeft { left: 2.0 }));
        assert_eq!(it.next(), None);

        it = Interval::Unbounded.complement();
        assert_eq!(it.next(), Some(Interval::Empty));
        assert_eq!(it.next(), None);

        it = Interval::Empty.complement();
        assert_eq!(it.next(), Some(Interval::Unbounded));
        assert_eq!(it.next(), None);
    }

    #[test]
    fn interval_display() {
        let bp = BoundPair::new(1, 5).ok_or("invalid BoundPair").unwrap();

        assert_eq!(format!("{}", Interval::Closed { bound_pair: bp }), "[1..5]");
        assert_eq!(format!("{}", Interval::Open { bound_pair: bp }), "(1..5)");
        assert_eq!(
            format!("{}", Interval::LeftHalfOpen { bound_pair: bp }),
            "(1..5]"
        );
        assert_eq!(
            format!("{}", Interval::RightHalfOpen { bound_pair: bp }),
            "[1..5)"
        );
        assert_eq!(
            format!("{}", Interval::UnboundedClosedRight { right: 5 }),
            "(←..5]"
        );
        assert_eq!(
            format!("{}", Interval::UnboundedOpenRight { right: 5 }),
            "(←..5)"
        );
        assert_eq!(
            format!("{}", Interval::UnboundedClosedLeft { left: 1 }),
            "[1..→)"
        );
        assert_eq!(
            format!("{}", Interval::UnboundedOpenLeft { left: 1 }),
            "(1..→)"
        );
        assert_eq!(format!("{}", Interval::Singleton { at: 3.0 }), "[3.0]");
        assert_eq!(format!("{}", Interval::Unbounded::<u32> {}), "(←..→)");
        assert_eq!(format!("{}", Interval::Empty::<u32> {}), "Empty");
    }

    #[quickcheck]
    fn intersect_strictly_shrinks_u32(l1: u32, l2: u32, r1: u32, r2: u32) -> TestResult {
        if let (Some(bp1), Some(bp2)) = (BoundPair::new(l1, r1), BoundPair::new(l2, r2)) {
            let i1 = Interval::LeftHalfOpen { bound_pair: bp1 };
            let i2 = Interval::LeftHalfOpen { bound_pair: bp2 };
            let intersection = i1.intersect(&i2);
            if (intersection.width() > i1.width()) | (intersection.width() > i2.width()) {
                TestResult::from_bool(false)
            } else {
                TestResult::from_bool(true)
            }
        } else {
            // Discard invalid randomly generated intervals
            TestResult::discard()
        }
    }

    #[quickcheck]
    fn intersect_strictly_shrinks_f32(l1: f32, l2: f32, r1: f32, r2: f32) -> TestResult {
        if let (Some(bp1), Some(bp2)) = (BoundPair::new(l1, r1), BoundPair::new(l2, r2)) {
            let i1 = Interval::LeftHalfOpen { bound_pair: bp1 };
            let i2 = Interval::LeftHalfOpen { bound_pair: bp2 };
            let intersection = i1.intersect(&i2);
            if (intersection.width() > i1.width()) | (intersection.width() > i2.width()) {
                TestResult::from_bool(false)
            } else {
                TestResult::from_bool(true)
            }
        } else {
            // Discard invalid randomly generated intervals
            TestResult::discard()
        }
    }
}