intervalx
Generic Interval and IntervalSet algebra crate
-
This crate is pure generic interval algebra, and can be implemented for any type
-
main-types :
intervalx::interval::Interval<T:Ord+IntervalBound>intervalx::set::IntervalSet<T:Ord+IntervalBound>
Key Features
-
Mathematically correct interval types: open, closed, left_open, right_open, point, empty, and unbounded.
-
Generic over numeric types: Works out-of-the-box with standard primitives (i32, i64, u32, u64, f32, f64).
-
Extensible via
IntervalBoundtrait: Allows users to define intervals for custom numeric types, units, or domain-specific numbers. -
Safe empty interval handling: Automatically handles degenerate intervals like
(5,5). -
Unbounded intervals: Represent full numeric ranges using practical finite bounds (MIN/MAX) of the user-implemented type for real-world safety, in other words
[-Inf,+inf] intervalfori32is actually[i32::MIN,i32::MAX]as implemented for default. -
Contains checks: Easily test if a value is inside an interval *with
.contains(&x). -
Algebraic operations: Supports intersect, union, overlaps, and adjacency checks (planned for advanced versions).
-
Beginner-friendly defaults: Use immediately with standard numeric types; no trait implementation required.
-
Advanced usage: Fully generic and type-safe for experts needing custom types or unit-safe intervals.
-
Readable algebraic notation: Intervals are displayed in standard mathematical forms
(a,b),[a,b],(a,b],[a,b).
Rules
-
intervalx::interval::IntervalBoundTrait andstd::cmp::OrdTrait must be implemented for any type that want to useInterval<T> -
default implementation of
IntervalBoundis supplied fori8,i16,i32,i64,i128,u8,u16,u32,u64,u128. -
intervalx::bound::Bound(Open(x))excludes x -
intervalx::bound::Bound(Closed(x))includes x
for better understanding review documentation
Examples
- Basic usage:
use *;
let open_interval=open;//only 6 inside ,represented algebric: (5,7)
println!;
assert!;
let closed_interval=closed;//contains 5 and 6 ,represented algebric: [5,6]
println!;
assert!;
assert!;
let left_open=left_open;//contains only 6 represented algebric: (5,6]
println!;
assert!;
assert!;
let right_open=right_open;//contains only 5,represented algebric: [5,6)
println!;
assert!;
assert!;
let unbounded=unbounded;//contains all the elements as [MIN,MAX],represented algebric: [MIN,MAX]
println!;
for i in -50..=50
let empty_interval:=empty;
println!;
let empty_interval1=open;//(5,5) open is empty interval
println!;
//in fact any Open Interval (one side,or two sides) that the endpoints are equal results in empty interval,
//..
let empty_interval2=left_open;//(5,5] left open is empty interval
println!;
for i in -50..=50
- basic methods
use *;
let a_b=closed;
let c_d=closed;
let a_b_intersects_c_d=a_b.intersection;
println!;
assert!;
let inf_=left_open;
println!;
let open1=open;
let b=open;
let isn=open1.intersection;
println!;
assert!;
println!;
let a = closed;//(0,5)
let b = closed;//[5,8]
let i = a.intersection;
println!;
let a = closed;
let b = closed;
let i=a.intersection;
println!;
let a=open;
let b=closed;
let i=a.intersection;
println!;
let age_interval=closed;
println!;
let ossama_age=HumanAge;
assert!;
let a:=unbounded;
let b=unbounded;
let i=a.intersection;
println!;
let a=right_open;
let b=closed;
println!;
let a=right_open;
let b=closed;
println!;
Advanced Usage (Custom Types)
use ;
use *;
use *;
;