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
// -----------------------------------------------------------------------------
// @generated by xtask/codegen (unsigned)
// DO NOT EDIT MANUALLY.
// Changes will be overwritten.
// -----------------------------------------------------------------------------
use crate::res::{OneTwo, ZeroOneTwo};
#[cfg(test)]
mod tests_for_basic;
#[cfg(test)]
mod tests_for_between;
#[cfg(test)]
mod tests_for_checked_minkowski;
#[cfg(test)]
mod tests_for_convex_hull;
#[cfg(test)]
mod tests_for_difference;
#[cfg(test)]
mod tests_for_intersection;
#[cfg(test)]
mod tests_for_saturating_minkowski;
#[cfg(test)]
mod tests_for_symmetric_difference;
#[cfg(test)]
mod tests_for_union;
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash, Ord, PartialOrd)]
pub struct UsizeCO {
start: usize,
end_excl: usize,
}
// ------------------------------------------------------------
// low-level api: construction / accessors / predicates
// ------------------------------------------------------------
mod basic {
use super::*;
impl UsizeCO {
#[inline]
pub const fn try_new(start: usize, end_excl: usize) -> Option<Self> {
if start < end_excl {
Some(Self { start, end_excl })
} else {
None
}
}
#[inline]
pub const unsafe fn new_unchecked(start: usize, end_excl: usize) -> Self {
debug_assert!(start < end_excl);
Self { start, end_excl }
}
/// Construct a `UsizeCO` from a midpoint and a length.
///
/// Returns `None` if the computed interval is invalid or overflows usize.
#[inline]
pub const fn checked_from_midpoint_len(mid: usize, len: usize) -> Option<Self> {
if len == 0 {
return None;
}
// Compute start = mid - floor(len/2)
let Some(start) = mid.checked_sub(len / 2) else {
return None;
};
// Compute end_excl = start + len, return None if overflow
let Some(end_excl) = start.checked_add(len) else {
return None;
};
// # Safety
// This function uses `unsafe { Self::new_unchecked(start, end_excl) }` internally.
// The safety is guaranteed by the following checks:
// 1. `mid.checked_sub(len / 2)` ensures `start` does not underflow `usize`.
// 2. `start.checked_add(len)` ensures `end_excl` does not overflow `usize`.
// 3. Because `len > 0`, we have `start < end_excl`.
// 4. Therefore, the half-open interval invariant `[start, end_excl)` is preserved.
Some(unsafe { Self::new_unchecked(start, end_excl) })
}
/// Saturating version: from midpoint + length, keeps start < end_excl
#[inline]
pub const fn saturating_from_midpoint_len(mid: usize, len: usize) -> Option<Self> {
if len == 0 {
return None;
}
let start = mid.saturating_sub(len / 2);
let end_excl = start.saturating_add(len);
// Use try_new to enforce start < end_excl invariant
Self::try_new(start, end_excl)
}
}
impl UsizeCO {
#[inline]
pub const fn start(self) -> usize {
self.start
}
#[inline]
pub const fn end_excl(self) -> usize {
self.end_excl
}
#[inline]
pub const fn end_incl(self) -> usize {
// usize_low_bound =< start < end_excl
self.end_excl - 1
}
}
impl UsizeCO {
#[inline]
pub const fn len(self) -> usize {
self.end_excl - self.start
}
}
impl UsizeCO {
/// Returns the midpoint of the interval (floor division).
#[inline]
pub const fn midpoint(self) -> usize {
self.start + (self.len() / 2)
}
}
impl UsizeCO {
#[inline]
pub const fn contains(self, x: usize) -> bool {
self.start <= x && x < self.end_excl
}
#[inline]
pub const fn contains_interval(self, other: Self) -> bool {
self.start <= other.start && other.end_excl <= self.end_excl
}
#[inline]
pub const fn intersects(self, other: Self) -> bool {
!(self.end_excl <= other.start || other.end_excl <= self.start)
}
#[inline]
pub const fn is_adjacent(self, other: Self) -> bool {
self.end_excl == other.start || other.end_excl == self.start
}
#[inline]
pub const fn is_contiguous_with(self, other: Self) -> bool {
self.intersects(other) || self.is_adjacent(other)
}
}
impl UsizeCO {
#[inline]
pub const fn iter(self) -> core::ops::Range<usize> {
self.start..self.end_excl
}
#[inline]
pub const fn to_range(self) -> core::ops::Range<usize> {
self.start..self.end_excl
}
}
}
// ------------------------------------------------------------
// interval algebra api: intersection / convex_hull / between / union / difference / symmetric_difference
// ------------------------------------------------------------
mod interval_algebra {
use super::*;
impl UsizeCO {
/// Returns the intersection of two intervals.
///
/// If the intervals do not overlap, returns `None`.
#[inline]
pub const fn intersection(self, other: Self) -> Option<Self> {
let start = if self.start >= other.start {
self.start
} else {
other.start
};
let end_excl = if self.end_excl <= other.end_excl {
self.end_excl
} else {
other.end_excl
};
Self::try_new(start, end_excl)
}
/// Returns the convex hull (smallest interval containing both) of two intervals.
///
/// Always returns a valid `UsizeCO`.
#[inline]
pub const fn convex_hull(self, other: Self) -> Self {
let start = if self.start <= other.start {
self.start
} else {
other.start
};
let end_excl = if self.end_excl >= other.end_excl {
self.end_excl
} else {
other.end_excl
};
Self { start, end_excl }
}
/// Returns the interval strictly between two intervals.
///
/// If the intervals are contiguous or overlap, returns `None`.
#[inline]
pub const fn between(self, other: Self) -> Option<Self> {
let (left, right) = if self.start <= other.start {
(self, other)
} else {
(other, self)
};
Self::try_new(left.end_excl, right.start)
}
/// Returns the union of two intervals.
///
/// - If intervals are contiguous or overlapping, returns `One` containing the merged interval.
/// - Otherwise, returns `Two` containing both intervals in ascending order.
#[inline]
pub const fn union(self, other: Self) -> OneTwo<Self> {
if self.is_contiguous_with(other) {
OneTwo::One(self.convex_hull(other))
} else if self.start <= other.start {
OneTwo::Two(self, other)
} else {
OneTwo::Two(other, self)
}
}
/// Returns the difference of two intervals (self - other).
///
/// - If no overlap, returns `One(self)`.
/// - If partial overlap, returns `One` or `Two` depending on remaining segments.
/// - If fully contained, returns `Zero`.
#[inline]
pub const fn difference(self, other: Self) -> ZeroOneTwo<Self> {
match self.intersection(other) {
None => ZeroOneTwo::One(self),
Some(inter) => {
let left = Self::try_new(self.start, inter.start);
let right = Self::try_new(inter.end_excl, self.end_excl);
match (left, right) {
(None, None) => ZeroOneTwo::Zero,
(Some(x), None) | (None, Some(x)) => ZeroOneTwo::One(x),
(Some(x), Some(y)) => ZeroOneTwo::Two(x, y),
}
}
}
}
/// Returns the symmetric difference of two intervals.
///
/// Equivalent to `(self - other) ∪ (other - self)`.
/// - If intervals do not overlap, returns `Two(self, other)` in order.
/// - If intervals partially overlap, returns remaining non-overlapping segments as `One` or `Two`.
#[inline]
pub const fn symmetric_difference(self, other: Self) -> ZeroOneTwo<Self> {
match self.intersection(other) {
None => {
if self.start <= other.start {
ZeroOneTwo::Two(self, other)
} else {
ZeroOneTwo::Two(other, self)
}
}
Some(inter) => {
let hull = self.convex_hull(other);
let left = Self::try_new(hull.start, inter.start);
let right = Self::try_new(inter.end_excl, hull.end_excl);
match (left, right) {
(None, None) => ZeroOneTwo::Zero,
(Some(x), None) | (None, Some(x)) => ZeroOneTwo::One(x),
(Some(x), Some(y)) => ZeroOneTwo::Two(x, y),
}
}
}
}
}
}
// ------------------------------------------------------------
// Module: Minkowski arithmetic for UsizeCO
// Provides checked and saturating Minkowski operations
// ------------------------------------------------------------
pub mod minkowski {
use super::*;
pub mod checked {
use super::*;
// --------------------------------------------------------
// Interval-to-interval Minkowski operations
// --------------------------------------------------------
impl UsizeCO {
/// Minkowski addition: [a_start, a_end) + [b_start, b_end)
#[inline]
pub const fn checked_minkowski_add(self, other: Self) -> Option<Self> {
let Some(start) = self.start.checked_add(other.start) else {
return None;
};
let Some(end_excl) = self.end_excl.checked_add(other.end_incl()) else {
return None;
};
// SAFETY:
// `checked_add` guarantees both bounds are computed without overflow.
// For half-open intervals, addition preserves ordering:
// self.start <= self.end_excl - 1 and other.start <= other.end_incl(),
// therefore `start < end_excl` still holds for the resulting interval.
Some(unsafe { Self::new_unchecked(start, end_excl) })
}
/// Minkowski subtraction: [a_start, a_end) - [b_start, b_end)
#[inline]
pub const fn checked_minkowski_sub(self, other: Self) -> Option<Self> {
let Some(start) = self.start.checked_sub(other.end_incl()) else {
return None;
};
let Some(end_excl) = self.end_excl.checked_sub(other.start) else {
return None;
};
// SAFETY:
// `checked_sub` guarantees both bounds are computed without underflow.
// Since `self.start <= self.end_excl - 1` and `other.start <= other.end_incl()`,
// we have `self.start - other.end_incl() < self.end_excl - other.start`,
// so the resulting half-open interval remains valid.
Some(unsafe { Self::new_unchecked(start, end_excl) })
}
/// Minkowski multiplication: [a_start, a_end) * [b_start, b_end)
#[inline]
pub const fn checked_minkowski_mul_hull(self, other: Self) -> Option<Self> {
let Some(start) = self.start.checked_mul(other.start) else {
return None;
};
let Some(end_incl) = self.end_incl().checked_mul(other.end_incl()) else {
return None;
};
let Some(end_excl) = end_incl.checked_add(1) else {
return None;
};
// SAFETY:
// For `UsizeCO`, all values are non-negative, so endpoint-wise multiplication
// is monotone. `start` is the minimum product and `end_incl` is the maximum.
// `checked_add(1)` converts the inclusive upper bound back to half-open form,
// and overflow has already been excluded.
Some(unsafe { Self::new_unchecked(start, end_excl) })
}
/// Minkowski division: [a_start, a_end) / [b_start, b_end)
#[inline]
pub const fn checked_minkowski_div_hull(self, other: Self) -> Option<Self> {
if other.start == 0 {
return None;
}
let Some(start) = self.start.checked_div(other.end_incl()) else {
return None;
};
let Some(end_incl) = self.end_incl().checked_div(other.start) else {
return None;
};
let Some(end_excl) = end_incl.checked_add(1) else {
return None;
};
// SAFETY:
// `other.start != 0` excludes division by zero, and all operands are unsigned.
// Over positive integers, division is monotone with respect to the dividend and
// anti-monotone with respect to the divisor, so:
// min = self.start / other.end_incl()
// max = self.end_incl() / other.start
// Thus `start <= end_incl`, and `end_excl = end_incl + 1` is checked.
Some(unsafe { Self::new_unchecked(start, end_excl) })
}
}
// --------------------------------------------------------
// Interval-to-scalar Minkowski operations
// --------------------------------------------------------
impl UsizeCO {
/// Add a scalar to an interval: [start, end) + n
#[inline]
pub const fn checked_minkowski_add_scalar(self, n: usize) -> Option<Self> {
let Some(start) = self.start.checked_add(n) else {
return None;
};
let Some(end_excl) = self.end_excl.checked_add(n) else {
return None;
};
// SAFETY:
// `checked_add` excludes overflow, and adding the same scalar to both bounds
// preserves the half-open interval ordering.
Some(unsafe { Self::new_unchecked(start, end_excl) })
}
/// Subtract a scalar from an interval: [start, end) - n
#[inline]
pub const fn checked_minkowski_sub_scalar(self, n: usize) -> Option<Self> {
let Some(start) = self.start.checked_sub(n) else {
return None;
};
let Some(end_excl) = self.end_excl.checked_sub(n) else {
return None;
};
// SAFETY:
// `checked_sub` excludes underflow, and subtracting the same scalar from both
// bounds preserves the half-open interval ordering.
Some(unsafe { Self::new_unchecked(start, end_excl) })
}
/// Multiply an interval by a scalar: [start, end) * n
#[inline]
pub const fn checked_minkowski_mul_scalar_hull(self, n: usize) -> Option<Self> {
let Some(start) = self.start.checked_mul(n) else {
return None;
};
let Some(end_incl) = self.end_incl().checked_mul(n) else {
return None;
};
let Some(end_excl) = end_incl.checked_add(1) else {
return None;
};
// SAFETY:
// For unsigned integers, multiplication by a scalar is monotone.
// Therefore `start * n` is the lower bound and `end_incl * n` is the upper bound.
// `checked_*` operations exclude overflow, and `end_excl` restores half-open form.
Some(unsafe { Self::new_unchecked(start, end_excl) })
}
/// Divide an interval by a scalar: [start, end) / n
#[inline]
pub const fn checked_minkowski_div_scalar_hull(self, n: usize) -> Option<Self> {
if n == 0 {
return None;
}
let start = self.start / n;
let end_incl = self.end_incl() / n;
let Some(end_excl) = end_incl.checked_add(1) else {
return None;
};
// SAFETY:
// `n != 0` excludes division by zero. For unsigned integers and positive scalar `n`,
// division is monotone, so `self.start / n <= self.end_incl() / n`.
// `checked_add(1)` safely converts the inclusive upper bound to half-open form.
Some(unsafe { Self::new_unchecked(start, end_excl) })
}
}
}
pub mod saturating {
use super::*;
// --------------------------------------------------------
// Interval-to-interval Minkowski operations
// --------------------------------------------------------
impl UsizeCO {
#[inline]
pub const fn saturating_minkowski_add(self, other: Self) -> Option<Self> {
let start = self.start.saturating_add(other.start);
let end_excl = self.end_excl.saturating_add(other.end_incl());
Self::try_new(start, end_excl)
}
#[inline]
pub const fn saturating_minkowski_sub(self, other: Self) -> Option<Self> {
let start = self.start.saturating_sub(other.end_incl());
let end_excl = self.end_excl.saturating_sub(other.start);
Self::try_new(start, end_excl)
}
#[inline]
pub const fn saturating_minkowski_mul_hull(self, other: Self) -> Option<Self> {
let start = self.start.saturating_mul(other.start);
let end_incl = self.end_incl().saturating_mul(other.end_incl());
let end_excl = end_incl.saturating_add(1);
Self::try_new(start, end_excl)
}
#[inline]
pub const fn saturating_minkowski_div_hull(self, other: Self) -> Option<Self> {
if other.start == 0 {
return None;
}
let start = self.start / other.end_incl();
let end_incl = self.end_incl() / other.start;
let end_excl = end_incl.saturating_add(1);
Self::try_new(start, end_excl)
}
}
// --------------------------------------------------------
// Interval-to-scalar Minkowski operations
// --------------------------------------------------------
impl UsizeCO {
/// Saturating add scalar: [start, end) + n
#[inline]
pub const fn saturating_minkowski_add_scalar(self, n: usize) -> Option<Self> {
let start = self.start.saturating_add(n);
let end_excl = self.end_excl.saturating_add(n);
Self::try_new(start, end_excl)
}
/// Saturating sub scalar: [start, end) - n
#[inline]
pub const fn saturating_minkowski_sub_scalar(self, n: usize) -> Option<Self> {
let start = self.start.saturating_sub(n);
let end_excl = self.end_excl.saturating_sub(n);
Self::try_new(start, end_excl)
}
/// Saturating mul scalar: [start, end) * n
#[inline]
pub const fn saturating_minkowski_mul_scalar_hull(self, n: usize) -> Option<Self> {
let start = self.start.saturating_mul(n);
let end_incl = self.end_incl().saturating_mul(n);
let end_excl = end_incl.saturating_add(1);
Self::try_new(start, end_excl)
}
/// Saturating div scalar: [start, end) / n
#[inline]
pub const fn saturating_minkowski_div_scalar_hull(self, n: usize) -> Option<Self> {
if n == 0 {
return None;
}
let start = self.start / n;
let end_incl = self.end_incl() / n;
let end_excl = end_incl.saturating_add(1);
Self::try_new(start, end_excl)
}
}
}
}
crate::traits::impl_co_forwarding!(UsizeCO, usize, usize);