Crate rust_intervals

Source
Expand description

This create provides operations for mathematical intervals. Such intervals include all values between two bounds.

This library supports multiple kinds of intervals. Let’s call E the set of valid values in the interval,

IntervalConstructorDescription
[A,B]Interval::new_closed_closedleft-closed, right-closed
[A,B)Interval::new_closed_openleft-closed, right-open
(A,B)Interval::new_open_openleft-open, right-open
(A,B]Interval::new_open_closedleft-open, right-closed
(,B]Interval::new_unbounded_closedleft-unbounded, right-closed
(,B)Interval::new_unbounded_openleft-unbounded, right-open
[A,)Interval::new_closed_unboundedleft-closed, right-unbounded
(A,)Interval::new_open_unboundedleft-open, right-unbounded
(,)Interval::doubly_unboundeddoubly unbounded
emptyInterval::default()empty

Any type can be used for the bounds, though operations on the interval depends on the traits that the bound type implements.

Intervals on floats (like any code using float) can be tricky. For instance, the two intervals [1.0, 100.0) and [1.0, 100.0 - f32:EPSILON) are not considered equivalent, since the machine thinks the two upper bounds have the same value, but one of them is closed and the other is open.

Although this type is mostly intended to be used when T can be ordered, it is in fact possible to define intervals using any type. But only a few functions are then available (like Interval::lower(), Interval::upper(),…)

Given two intervals, and assuming T is orderable, we can compute the following:

       [------ A ------]
              [----- B -------]

       [----------------------]     Convex hull
       [------)                     Difference (A - B)
                       (------]     Difference (B - A)
       [------)        (------]     Symmetric difference (A ^ B)
              [--------]            Intersection (A & B)
                                    Between is empty
       [----------------------]     Union (A | B)

When the two intervals do not overlap, we can compute:

     [---A---]   [----B----]

     [---------------------]    Convex hull
     [-------]                  Difference (A - B)
                 [---------]    Difference (B - A)
     [-------]   [---------]    Symmetric difference (A ^ B)
                                Intersection (A & B) is empty
             (---)              Between
                                Union (A | B) is empty, non contiguous

Macros§

interval
This macro lets you create intervals with a syntax closer to what Postgresql provides. There are multiple variants of this macro:

Structs§

Interval
An interval of values.
IntervalIterator
IntervalSetstd
A sorted list of non-overlapping intervals. There are multiple ways to combine intervals, depending on the chosen policy.
Joiningstd
Separatingstd

Enums§

Pair
The result of the difference between two intervals. This might be a single interval, or two (in which case the first one is always the left-most).
ParseError

Traits§

Bounded
NothingBetween
Trait to compute whether intervals are empty.
Step
Similar to std::iter::Step, but the latter is unstable and cannot be used in this package. It also doesn’t provide support for starting from lowest value valid for the type for instance.