intrvals
intrvals is a generic intervals library with basic
arithmetic (+, -, *)
and sets (union, intersect, complement) operations support.
Its main structure is an Interval which represents
a single-dimensioned (possibly unbounded) span of values instantiated by any type T.
macro syntax
Allows to simplify the defintions of an interval in common inequality and ranges terms.
For the half-open intervals, the inclusive bound is marked with = symbol;
for the closed interval both [a, b] and (=a, =b) defintions are possible.
use ;
let i0: = interval!;
assert_eq!;
let igt2: = interval!;
assert_eq!;
let igt10: = interval!;
assert_eq!;
let i_2to10_incl: = interval!;
assert_eq!;
let i5to20_excl: = interval!;
assert_eq!;
let iuni: = interval!;
assert_eq!;
common functions
use Ordering;
use ;
assert!;
assert!;
assert!;
assert!;
assert!;
let igt10 = interval!;
assert!;
assert!;
assert!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
let i_left_open = interval!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
scalar arithmetic
Add, subtract or multiply the interval bounds with a scalar value of type U
if the underlying type T: {Add, Sub, Mult}<U>:
use ;
// negation changes the sign and flips the bounds
assert_eq!;
assert_eq!;
// full and empy does not change with scalars
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
// however, multiplying by 0 is different
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
// multiplying/dividing by negative flips the bounds
assert_eq!;
assert_eq!;
interval arithmetic
Add, subtract or multiply an Interval<T> with an Interval<U>
to produce another Interval<Z>
if the underlying type T: {Add, Sub, Mult}<U, Output=Z>:
use interval;
let i0 = interval!;
let igt10 = interval!;
let i_2to10_incl = interval!;
let i5to20_excl = interval!;
let iuni = interval!;
assert_eq!;
// adding degenerate does not change the normal one
assert_eq!;
assert_eq!;
assert_eq!;
// subtracting degenerate does not change the normal one
assert_eq!;
// subtracting _from_ degenerate negates the normal one
assert_eq!;
// Interval::Empty is neutral over multiplication
assert_eq!;
// positive (+inf) times positive is positive
assert_eq!;
// positive (+inf) times (negative and positive) is (-inf, +inf)
assert_eq!;
assert_eq!;
// Interval::Full is neutral over multiplication
assert_eq!;
set operations
use ;
let igt2 = interval!;
let igt_e2 = interval!;
let igt10 = interval!;
assert!;
assert!;
assert!;
assert!;
assert!;
assert!;
assert!;
assert!;
assert!;
assert_eq!;
// `.complement` is aliased with `!`
assert_eq!;
assert_eq!;
assert_eq!;
// `.symmetric_difference` is aliased with `^` (falling back to Interval::Empty)
assert_eq!;
assert_eq!;
// `.intersect` is aliased with `&` (falling back to Interval::Empty)
assert!;
assert_eq!;
assert_eq!;
// `.union` is aliased with `|`
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
Features
By default, all the features below are disabled to ensure minimalistic no-dependency library.
serde
Enables the support of serde::{Serialize, Deserialize} for Interval<T>.
arbitrary
Enables the proptest::Arbitrary for Interval<T> along with the property tests
(could be invoked with cargo test prop_test --features=arbitrary).
singleton
This feature is useful if you want to have a dedicated Interval::Singleton
variant (otherwise represented as Interval::Closed).
This allows to create an Interval from a single value
without cloning it.
However, to get the bounds of an Interval, you have to meet
the requirement of T: Clone again.
The table below summarizes these limitations for the Interval<T>
| use case | singleton feature |
no singleton feature |
|---|---|---|
create an Interval from a single point (Singleton::singleton) |
- | T: Clone |
convert an Interval into a pair of Endpoint-s(Bounded::into_bounds) |
T: Clone |
- |
There are two helper traits used to implement the desired feature-gated behaviour:
trait Singletonto create a point-sizedIntervalfrom a single value (when the feature is disabled, create anInterval::Closed((x.clone(), x))instead);trait SingletonBoundsto convert a single value into a pair ofEndpoint-s (when the feature is disabled, this trait has no methods and implemented for anInterval<T>unconditionally).