int-intervals 0.1.2

Closed-open integer interval algebra, canonical interval sets, and overlap-stack structures.
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
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
use core::num::ParseIntError;

use crate::interval::{OneTwo, ZeroOneTwo};

pub(crate) mod forwarding;
pub(crate) use forwarding::impl_co_forwarding;

mod sealed;

/// Built-in integer coordinate type accepted by closed-open intervals.
pub trait IntPrimitive: sealed::Int {
    // ============================================================
    // Constants
    // ============================================================

    fn zero() -> Self;
    fn one() -> Self;

    fn min_value() -> Self;
    fn max_value() -> Self;

    // ============================================================
    // Numeric conversion
    // ============================================================

    fn as_f32(self) -> f32;
    fn as_f64(self) -> f64;

    fn checked_from<T>(value: T) -> Option<Self>
    where
        Self: Sized + TryFrom<T>;

    // ============================================================
    // Checked same-type arithmetic
    // ============================================================

    fn checked_add(self, rhs: Self) -> Option<Self>;
    fn checked_sub(self, rhs: Self) -> Option<Self>;
    fn checked_mul(self, rhs: Self) -> Option<Self>;
    fn checked_div(self, rhs: Self) -> Option<Self>;
    fn checked_rem(self, rhs: Self) -> Option<Self>;

    // ============================================================
    // Checked converted arithmetic
    // ============================================================

    fn checked_add_from<T>(self, rhs: T) -> Option<Self>
    where
        Self: Sized + TryFrom<T>;

    fn checked_sub_from<T>(self, rhs: T) -> Option<Self>
    where
        Self: Sized + TryFrom<T>;

    fn checked_mul_from<T>(self, rhs: T) -> Option<Self>
    where
        Self: Sized + TryFrom<T>;

    fn checked_div_from<T>(self, rhs: T) -> Option<Self>
    where
        Self: Sized + TryFrom<T>;

    fn checked_rem_from<T>(self, rhs: T) -> Option<Self>
    where
        Self: Sized + TryFrom<T>;

    // ============================================================
    // Saturating same-type arithmetic
    // ============================================================

    fn saturating_add(self, rhs: Self) -> Self;
    fn saturating_sub(self, rhs: Self) -> Self;
    fn saturating_mul(self, rhs: Self) -> Self;

    // ============================================================
    // Saturating converted arithmetic
    // ============================================================

    fn saturating_add_from<T>(self, rhs: T) -> Option<Self>
    where
        Self: Sized + TryFrom<T>;

    fn saturating_sub_from<T>(self, rhs: T) -> Option<Self>
    where
        Self: Sized + TryFrom<T>;

    fn saturating_mul_from<T>(self, rhs: T) -> Option<Self>
    where
        Self: Sized + TryFrom<T>;

    // ============================================================
    // Unit stepping
    // ============================================================

    fn checked_next(self) -> Option<Self>;
    fn checked_prev(self) -> Option<Self>;

    fn saturating_next(self) -> Self;
    fn saturating_prev(self) -> Self;

    // ============================================================
    // Parsing
    // ============================================================

    fn parse_decimal(src: &str) -> Result<Self, ParseIntError>;
}

impl<T> IntPrimitive for T
where
    T: sealed::Int,
{
    // ============================================================
    // Constants
    // ============================================================

    #[inline]
    fn zero() -> Self {
        sealed::Int::zero()
    }

    #[inline]
    fn one() -> Self {
        sealed::Int::one()
    }

    #[inline]
    fn min_value() -> Self {
        sealed::Int::min_value()
    }

    #[inline]
    fn max_value() -> Self {
        sealed::Int::max_value()
    }

    // ============================================================
    // Numeric conversion
    // ============================================================

    #[inline]
    fn as_f32(self) -> f32 {
        sealed::Int::as_f32(self)
    }

    #[inline]
    fn as_f64(self) -> f64 {
        sealed::Int::as_f64(self)
    }

    #[inline]
    fn checked_from<U>(value: U) -> Option<Self>
    where
        Self: Sized + TryFrom<U>,
    {
        sealed::Int::checked_from(value)
    }

    // ============================================================
    // Checked same-type arithmetic
    // ============================================================

    #[inline]
    fn checked_add(self, rhs: Self) -> Option<Self> {
        sealed::Int::checked_add(self, rhs)
    }

    #[inline]
    fn checked_sub(self, rhs: Self) -> Option<Self> {
        sealed::Int::checked_sub(self, rhs)
    }

    #[inline]
    fn checked_mul(self, rhs: Self) -> Option<Self> {
        sealed::Int::checked_mul(self, rhs)
    }

    #[inline]
    fn checked_div(self, rhs: Self) -> Option<Self> {
        sealed::Int::checked_div(self, rhs)
    }

    #[inline]
    fn checked_rem(self, rhs: Self) -> Option<Self> {
        sealed::Int::checked_rem(self, rhs)
    }

    // ============================================================
    // Checked converted arithmetic
    // ============================================================

    #[inline]
    fn checked_add_from<U>(self, rhs: U) -> Option<Self>
    where
        Self: Sized + TryFrom<U>,
    {
        sealed::Int::checked_add_from(self, rhs)
    }

    #[inline]
    fn checked_sub_from<U>(self, rhs: U) -> Option<Self>
    where
        Self: Sized + TryFrom<U>,
    {
        sealed::Int::checked_sub_from(self, rhs)
    }

    #[inline]
    fn checked_mul_from<U>(self, rhs: U) -> Option<Self>
    where
        Self: Sized + TryFrom<U>,
    {
        sealed::Int::checked_mul_from(self, rhs)
    }

    #[inline]
    fn checked_div_from<U>(self, rhs: U) -> Option<Self>
    where
        Self: Sized + TryFrom<U>,
    {
        sealed::Int::checked_div_from(self, rhs)
    }

    #[inline]
    fn checked_rem_from<U>(self, rhs: U) -> Option<Self>
    where
        Self: Sized + TryFrom<U>,
    {
        sealed::Int::checked_rem_from(self, rhs)
    }

    // ============================================================
    // Saturating same-type arithmetic
    // ============================================================

    #[inline]
    fn saturating_add(self, rhs: Self) -> Self {
        sealed::Int::saturating_add(self, rhs)
    }

    #[inline]
    fn saturating_sub(self, rhs: Self) -> Self {
        sealed::Int::saturating_sub(self, rhs)
    }

    #[inline]
    fn saturating_mul(self, rhs: Self) -> Self {
        sealed::Int::saturating_mul(self, rhs)
    }

    // ============================================================
    // Saturating converted arithmetic
    // ============================================================

    #[inline]
    fn saturating_add_from<U>(self, rhs: U) -> Option<Self>
    where
        Self: Sized + TryFrom<U>,
    {
        sealed::Int::saturating_add_from(self, rhs)
    }

    #[inline]
    fn saturating_sub_from<U>(self, rhs: U) -> Option<Self>
    where
        Self: Sized + TryFrom<U>,
    {
        sealed::Int::saturating_sub_from(self, rhs)
    }

    #[inline]
    fn saturating_mul_from<U>(self, rhs: U) -> Option<Self>
    where
        Self: Sized + TryFrom<U>,
    {
        sealed::Int::saturating_mul_from(self, rhs)
    }

    // ============================================================
    // Unit stepping
    // ============================================================

    #[inline]
    fn checked_next(self) -> Option<Self> {
        sealed::Int::checked_next(self)
    }

    #[inline]
    fn checked_prev(self) -> Option<Self> {
        sealed::Int::checked_prev(self)
    }

    #[inline]
    fn saturating_next(self) -> Self {
        sealed::Int::saturating_next(self)
    }

    #[inline]
    fn saturating_prev(self) -> Self {
        sealed::Int::saturating_prev(self)
    }

    // ============================================================
    // Parsing
    // ============================================================

    #[inline]
    fn parse_decimal(src: &str) -> Result<Self, ParseIntError> {
        sealed::Int::parse_decimal(src)
    }
}

/// Built-in unsigned integer type used for exact interval measures.
pub trait UnsignedPrimitive: IntPrimitive + sealed::Unsigned {}

impl<T> UnsignedPrimitive for T where T: IntPrimitive + sealed::Unsigned {}

/// Primitive types associated with a closed-open integer interval.
pub trait COPrimitive {
    type CoordType: IntPrimitive;
    type MeasureType: UnsignedPrimitive;
}

/// Construction capability for a valid closed-open interval.
///
/// Implementations must preserve the invariant:
///
/// ```text
/// start < end_excl
/// ```
pub trait COConstruct: COPrimitive + Sized {
    /// Constructs `[start, end_excl)`, returning `None` for an empty or
    /// reversed interval.
    fn try_new(start: Self::CoordType, end_excl: Self::CoordType) -> Option<Self>;

    /// Constructs `[start, end_excl)` without checking the interval invariant.
    ///
    /// # Safety
    ///
    /// The caller must guarantee that:
    ///
    /// ```text
    /// start < end_excl
    /// ```
    unsafe fn new_unchecked(start: Self::CoordType, end_excl: Self::CoordType) -> Self;
}

/// Construction capability based on a midpoint and an exact interval measure.
///
/// `len` is represented by the interval's exact unsigned measure type.
pub trait COMidpointConstruct: COConstruct {
    /// Constructs an interval centered around `mid` with exact length `len`.
    ///
    /// Returns `None` when `len` is zero or when the resulting bounds cannot
    /// be represented by `CoordType`.
    fn checked_from_midpoint_len(mid: Self::CoordType, len: Self::MeasureType) -> Option<Self>;

    /// Constructs an interval centered around `mid` with saturating endpoint
    /// arithmetic.
    ///
    /// Returns `None` when `len` is zero or saturation collapses the result
    /// into an empty interval.
    fn saturating_from_midpoint_len(mid: Self::CoordType, len: Self::MeasureType) -> Option<Self>;
}

/// Construction capability based on a start bound and an exact interval measure.
///
/// `len` is represented by the interval's exact unsigned measure type.
pub trait COStartLenConstruct: COConstruct {
    /// Constructs an interval starting at `start` with exact length `len`.
    ///
    /// The resulting interval is:
    ///
    /// ```text
    /// [start, start + len)
    /// ```
    ///
    /// Returns `None` when `len` is zero or when the resulting exclusive end
    /// bound cannot be represented by `CoordType`.
    fn checked_from_start_len(start: Self::CoordType, len: Self::MeasureType) -> Option<Self>;

    /// Constructs an interval starting at `start` with saturating endpoint
    /// arithmetic.
    ///
    /// The resulting interval starts at `start`, while the exclusive end bound
    /// is clamped to the maximum representable coordinate when needed.
    ///
    /// Returns `None` when `len` is zero or saturation collapses the result
    /// into an empty interval.
    fn saturating_from_start_len(start: Self::CoordType, len: Self::MeasureType) -> Option<Self>;
}

/// Boundary access capability for a closed-open interval.
pub trait COBounds: COPrimitive + Copy + Ord + Eq + core::fmt::Debug {
    /// Returns the inclusive lower bound.
    fn start(self) -> Self::CoordType;

    /// Returns the exclusive upper bound.
    fn end_excl(self) -> Self::CoordType;

    /// Returns the inclusive upper bound.
    ///
    /// This is the greatest coordinate contained in the interval.
    fn end_incl(self) -> Self::CoordType;
}

/// Containment and overlap predicates for closed-open intervals.
pub trait COPredicates: COBounds {
    /// Returns whether `x` is contained in this interval.
    fn contains(self, x: Self::CoordType) -> bool;

    /// Returns whether `other` is fully contained in this interval.
    fn contains_interval(self, other: Self) -> bool;

    /// Returns whether this interval and `other` overlap with positive length.
    fn intersects(self, other: Self) -> bool;

    /// Returns whether this interval and `other` touch at exactly one boundary
    /// without overlapping.
    fn is_adjacent(self, other: Self) -> bool;

    /// Returns whether this interval and `other` overlap or are adjacent.
    fn is_contiguous_with(self, other: Self) -> bool;
}

/// Range projection capability for a closed-open interval.
///
/// The returned range has the same half-open semantics as the interval:
///
/// ```text
/// [start, end_excl) -> start..end_excl
/// ```
pub trait CORange: COBounds + Sized {
    /// Returns the standard-library half-open range represented by this
    /// interval.
    fn to_range(self) -> core::ops::Range<Self::CoordType>;

    /// Returns the standard-library range used to iterate covered coordinates.
    ///
    /// This is equivalent to `self.to_range()`.
    #[inline]
    fn iter(self) -> core::ops::Range<Self::CoordType> {
        self.to_range()
    }
}

/// Algebraic operations for closed-open intervals.
pub trait COAlgebra: COConstruct + COBounds + COPredicates {
    /// Returns the overlapping region of two intervals, if any.
    fn intersection(self, other: Self) -> Option<Self>;

    /// Returns the smallest interval containing both intervals.
    fn convex_hull(self, other: Self) -> Self;

    /// Returns the interval strictly between two separated intervals.
    ///
    /// Returns `None` when the intervals overlap or are adjacent.
    fn between(self, other: Self) -> Option<Self>;

    /// Returns the union of two intervals.
    ///
    /// Contiguous intervals are merged into one interval; otherwise the two
    /// intervals are returned in ascending order.
    fn union(self, other: Self) -> OneTwo<Self>;

    /// Returns `self \ other`.
    ///
    /// The result may contain zero, one, or two residual intervals.
    fn difference(self, other: Self) -> ZeroOneTwo<Self>;

    /// Returns the symmetric difference of two intervals.
    ///
    /// The result contains points covered by exactly one operand and may
    /// contain zero, one, or two intervals.
    fn symmetric_difference(self, other: Self) -> ZeroOneTwo<Self>;
}

/// Exact measure capability for a closed-open interval.
pub trait COMeasure: COPrimitive {
    /// Returns the exact interval length.
    fn len(self) -> Self::MeasureType;
}

/// Representative-position capability for a closed-open interval.
pub trait COMidpoint: COPrimitive {
    /// Returns the midpoint coordinate, using floor rounding where required.
    fn midpoint(self) -> Self::CoordType;
}

/// Exact checked Minkowski operations whose images remain closed-open
/// integer intervals.
pub trait COCheckedMinkowskiLinear: COPrimitive + Sized {
    /// Returns the exact Minkowski sum `self + other`.
    fn checked_minkowski_add(self, other: Self) -> Option<Self>;

    /// Returns the exact Minkowski subtraction `self - other`.
    fn checked_minkowski_sub(self, other: Self) -> Option<Self>;

    /// Returns the exact translation `self + scalar`.
    fn checked_minkowski_add_scalar(self, scalar: Self::CoordType) -> Option<Self>;

    /// Returns the exact translation `self - scalar`.
    fn checked_minkowski_sub_scalar(self, scalar: Self::CoordType) -> Option<Self>;
}

/// Checked interval hulls of non-linear Minkowski images.
///
/// For discrete integer intervals, multiplication and division may produce
/// non-contiguous point sets. These methods return a containing interval hull,
/// not necessarily an exact image.
pub trait COCheckedMinkowskiHull: COPrimitive + Sized {
    /// Returns the interval hull containing every point in `self * other`.
    fn checked_minkowski_mul_hull(self, other: Self) -> Option<Self>;

    /// Returns the interval hull containing every point in `self / other`.
    fn checked_minkowski_div_hull(self, other: Self) -> Option<Self>;

    /// Returns the interval hull containing every point in `self * scalar`.
    fn checked_minkowski_mul_scalar_hull(self, scalar: Self::CoordType) -> Option<Self>;

    /// Returns the interval hull containing every point in `self / scalar`.
    fn checked_minkowski_div_scalar_hull(self, scalar: Self::CoordType) -> Option<Self>;
}

/// Saturating Minkowski operations whose results remain closed-open integer
/// intervals after endpoint arithmetic is clamped to the representable domain.
///
/// These methods apply saturating arithmetic to the interval bounds rather
/// than returning an error on overflow or underflow.
///
/// When saturation clips a bound, the returned interval is the representable
/// saturated result, not necessarily the exact unconstrained mathematical
/// image.
///
/// Returns `None` when saturation collapses the resulting interval into an
/// empty or otherwise invalid closed-open interval.
pub trait COSaturatingMinkowskiLinear: COPrimitive + Sized {
    /// Returns the saturated Minkowski sum `self + other`.
    ///
    /// Both result bounds are computed with saturating addition.
    fn saturating_minkowski_add(self, other: Self) -> Option<Self>;

    /// Returns the saturated Minkowski subtraction `self - other`.
    ///
    /// Both result bounds are computed with saturating subtraction.
    fn saturating_minkowski_sub(self, other: Self) -> Option<Self>;

    /// Returns the saturated translation `self + scalar`.
    ///
    /// Both interval bounds are shifted with saturating addition.
    fn saturating_minkowski_add_scalar(self, scalar: Self::CoordType) -> Option<Self>;

    /// Returns the saturated translation `self - scalar`.
    ///
    /// Both interval bounds are shifted with saturating subtraction.
    fn saturating_minkowski_sub_scalar(self, scalar: Self::CoordType) -> Option<Self>;
}

/// Saturating interval hulls of non-linear Minkowski images.
///
/// For discrete integer intervals, multiplication and division may produce
/// non-contiguous point sets. These methods first compute a containing
/// interval hull and apply saturating endpoint arithmetic where needed.
///
/// When saturation clips a bound, the returned interval is a representable
/// saturated hull rather than the exact unconstrained mathematical image.
///
/// Returns `None` when the operation is undefined, such as division by zero,
/// or when saturation collapses the resulting hull into an empty or otherwise
/// invalid closed-open interval.
pub trait COSaturatingMinkowskiHull: COPrimitive + Sized {
    /// Returns the saturated interval hull of `self * other`.
    ///
    /// Endpoint products are computed with saturating multiplication.
    fn saturating_minkowski_mul_hull(self, other: Self) -> Option<Self>;

    /// Returns the saturated interval hull of `self / other`.
    ///
    /// Returns `None` when the divisor interval contains zero in a position
    /// that makes the interval division undefined.
    fn saturating_minkowski_div_hull(self, other: Self) -> Option<Self>;

    /// Returns the saturated interval hull of `self * scalar`.
    ///
    /// Endpoint products are computed with saturating multiplication.
    fn saturating_minkowski_mul_scalar_hull(self, scalar: Self::CoordType) -> Option<Self>;

    /// Returns the saturated interval hull of `self / scalar`.
    ///
    /// Returns `None` when `scalar` is zero.
    fn saturating_minkowski_div_scalar_hull(self, scalar: Self::CoordType) -> Option<Self>;
}

/// Complete closed-open integer interval capability required by interval sets.
pub trait IntCO: COConstruct + COBounds + COPredicates + COAlgebra + COMeasure {}

impl<T> IntCO for T where T: COConstruct + COBounds + COPredicates + COAlgebra + COMeasure {}

#[cfg(test)]
mod tests_for_int_primitive;