1use std::ops::RangeInclusive;
2use std::{
3 marker::PhantomData,
4 ops::{Add, Bound, Sub},
5};
6
7use crate::{GetBeginEnd, Mrs};
8
9#[derive(Clone, Copy, Debug)]
19pub struct NumberIncDecCpCmp<T>
20where
21 T: Copy + Clone,
22{
23 min: T,
24 max: T,
25}
26
27#[derive(Clone, Copy, Debug)]
34pub struct AnyIncDecCpCmp<T>
35where
36 T: PartialOrd + Clone + Copy,
37{
38 min: T,
39 max: T,
40}
41impl<T> AnyIncDecCpCmp<T>
42where
43 T: PartialOrd + Clone + Copy,
44{
45 pub fn new(min: T, max: T) -> Self {
46 Self { min, max }
47 }
48
49 pub fn set_min(&mut self, v: T) {
52 self.min = v;
53 }
54
55 pub fn set_max(&mut self, v: T) {
58 self.max = v;
59 }
60}
61
62impl<T> CpCmp<T> for AnyIncDecCpCmp<T>
63where
64 T: PartialOrd + Copy,
65{
66 fn cp(&self, v: &T) -> T {
68 return *v;
69 }
70
71 fn min_ref(&self) -> &T {
73 return &self.min;
74 }
75
76 fn max_ref(&self) -> &T {
78 return &self.max;
79 }
80
81 fn lt(&self, a: &T, b: &T) -> bool {
83 return a < b;
84 }
85
86 fn min(&self) -> T {
88 return self.min;
89 }
90
91 fn max(&self) -> T {
93 return self.max;
94 }
95}
96
97impl<T, V> IncDecCpCmp<T, V> for AnyIncDecCpCmp<T>
98where
99 V: Copy,
100 T: PartialOrd + Copy + Add<V, Output = T> + Sub<V, Output = T>,
101{
102 fn inc(&self, a: &T, b: &V) -> Option<T> {
104 let x = *a;
105 let c = a.add(*b);
106 if x <= c {
107 return Some(c);
108 }
109 return None;
110 }
111
112 fn dec(&self, a: &T, b: &V) -> Option<T> {
114 let x = *a;
115 let c = a.sub(*b);
116 if x >= c {
117 return Some(c);
118 }
119 return None;
120 }
121
122 fn cp_v(&self, v: &V) -> V {
123 return *v;
124 }
125}
126impl<T> NumberIncDecCpCmp<T>
127where
128 T: Clone + Copy,
129 NumberIncDecCpCmp<T>: DefaultValues<T, T>,
130{
131 pub fn defaults() -> Self {
132 return Self::new(
133 NumberIncDecCpCmp::default_min(),
134 NumberIncDecCpCmp::default_max(),
135 );
136 }
137 pub fn new(min: T, max: T) -> Self {
138 Self { min, max }
139 }
140
141 pub fn set_min(&mut self, v: T) {
144 self.min = v;
145 }
146
147 pub fn set_max(&mut self, v: T) {
150 self.max = v;
151 }
152}
153
154pub trait CpCmp<T> {
158 fn cp(&self, v: &T) -> T;
160
161 fn min_ref(&self) -> &T;
162 fn max_ref(&self) -> &T;
163
164 fn lt(&self, a: &T, b: &T) -> bool;
166
167 fn min(&self) -> T;
169
170 fn max(&self) -> T;
172
173 fn gt(&self, a: &T, b: &T) -> bool {
175 return self.lt(b, a);
176 }
177
178 fn eq(&self, a: &T, b: &T) -> bool {
180 return !self.lt(a, b) && !self.lt(b, a);
181 }
182
183 fn ne(&self, a: &T, b: &T) -> bool {
185 return self.lt(a, b) || self.lt(b, a);
186 }
187
188 fn le(&self, a: &T, b: &T) -> bool {
190 return self.lt(a, b) || !self.lt(b, a);
191 }
192
193 fn ge(&self, a: &T, b: &T) -> bool {
195 return self.lt(b, a) || !self.lt(a, b);
196 }
197
198 fn contains(&self, a: &T, b: &T, c: &T) -> bool {
200 return !(self.lt(c, a) || self.lt(b, c));
201 }
202
203 fn cp_tpl_ref(&self, src: (&T, &T)) -> (T, T) {
205 return (self.cp(src.0), self.cp(src.1));
206 }
207
208 fn overlap(&self, a: &T, b: &T, c: &T, d: &T) -> bool {
214 return self.contains(a, b, c)
215 || self.contains(a, b, d)
216 || self.contains(c, d, a)
217 || self.contains(c, d, b);
218 }
219
220 fn is_invalid_set(&self, a: &T, b: &T) -> bool {
222 return self.lt(b, a) || self.lt(a, self.min_ref()) || self.lt(self.max_ref(), b);
223 }
224}
225
226pub trait IncDecCpCmp<T, V>: CpCmp<T> {
270 fn inc(&self, a: &T, b: &V) -> Option<T>;
272
273 fn dec(&self, a: &T, b: &V) -> Option<T>;
275
276 fn cp_v(&self, v: &V) -> V;
277
278 fn rebound_start(&self, start: Bound<&T>, rebound: &V) -> Option<T> {
283 match start {
284 Bound::Included(begin) => Some(self.cp(begin)),
285 Bound::Excluded(begin) => self.inc(begin, rebound),
286 Bound::Unbounded => Some(self.min()),
287 }
288 }
289
290 fn rebound_end(&self, end: Bound<&T>, rebound: &V) -> Option<T> {
295 match end {
296 Bound::Included(end) => Some(self.cp(end)),
297 Bound::Excluded(end) => self.dec(end, rebound),
298 Bound::Unbounded => Some(self.max()),
299 }
300 }
301}
302
303pub trait DefaultValues<T, V>: IncDecCpCmp<T, V> {
307 fn default_step(&self) -> V;
309
310 fn default_rebound(&self) -> V;
312
313 fn default_min() -> T;
315
316 fn default_max() -> T;
318}
319
320macro_rules! impl_inc_dec_cp_cmp_trait_i {
321 ($($t:ty),*) => {
322 $(
323 impl CpCmp<$t> for NumberIncDecCpCmp<$t> {
324 fn min(&self) ->$t { self.min }
325 fn max(&self) ->$t { self.max }
326 fn min_ref(&self) ->&$t { &self.min }
327 fn max_ref(&self) ->&$t { &self.max }
328
329 fn cp(&self,v: &$t) ->$t {
330 return v.clone();
331 }
332
333 fn lt(&self,a:&$t,b: &$t) ->bool {
334 return a<b;
335 }
336 }
337
338 impl IncDecCpCmp<$t,$t> for NumberIncDecCpCmp<$t> {
339 fn dec(&self, a: &$t, b:&$t) ->Option<$t> {
340 if *b<=0 { return None}
341 return a.clone().checked_sub(b.clone());
342 }
343 fn inc(&self, a: &$t, b: &$t) -> Option<$t> {
344 if *b<=0 { return None}
345 return a.clone().checked_add(b.clone())
346 }
347 fn cp_v(&self,v: &$t) ->$t {
348 return *v;
349 }
350 }
351
352 impl DefaultValues<$t,$t> for NumberIncDecCpCmp<$t> {
353 fn default_step(&self) ->$t { return 1}
354 fn default_rebound(&self) ->$t { return 1}
355 fn default_min() ->$t { <$t>::MIN }
356 fn default_max() ->$t { <$t>::MAX }
357 }
358 )*
359 };
360}
361
362macro_rules! impl_inc_dec_cp_cmp_trait_u {
363 ($($t:ty),*) => {
364 $(
365 impl CpCmp<$t> for NumberIncDecCpCmp<$t> {
366 fn min(&self) ->$t { self.min }
367 fn max(&self) ->$t { self.max }
368 fn min_ref(&self) ->&$t { &self.min }
369 fn max_ref(&self) ->&$t { &self.max }
370
371 fn cp(&self,v: &$t) ->$t {
372 return v.clone();
373 }
374
375 fn lt(&self,a:&$t,b: &$t) ->bool {
376 return a<b;
377 }
378 }
379 impl IncDecCpCmp<$t,$t> for NumberIncDecCpCmp<$t> {
380 fn dec(&self, a: &$t, b:&$t) ->Option<$t> {
381 if *b==0 { return None}
382 return a.clone().checked_sub(b.clone());
383 }
384 fn inc(&self, a: &$t, b: &$t) -> Option<$t> {
385 if *b==0 { return None}
386 return a.clone().checked_add(b.clone())
387 }
388 fn cp_v(&self,v: &$t) ->$t {
389 return *v;
390 }
391 }
392
393 impl DefaultValues<$t,$t> for NumberIncDecCpCmp<$t> {
394 fn default_step(&self) ->$t { return 1}
395 fn default_rebound(&self) ->$t { return 1}
396 fn default_min() ->$t { <$t>::MIN }
397 fn default_max() ->$t { <$t>::MAX }
398 }
399 )*
400 };
401}
402
403macro_rules! impl_inc_dec_cp_cmp_trait_f {
404 ($($t:ty),*) => {
405 $(
406
407
408 impl CpCmp<$t> for NumberIncDecCpCmp<$t> {
409
410 fn min(&self) ->$t { self.min }
411 fn max(&self) ->$t { self.max }
412 fn min_ref(&self) ->&$t { &self.min }
413 fn max_ref(&self) ->&$t { &self.max }
414 fn cp(&self,v: &$t) ->$t {
415 return v.clone();
416 }
417 fn lt(&self,a:&$t,b: &$t) ->bool {
418 return a<b;
419 }
420 }
421 impl IncDecCpCmp<$t,$t> for NumberIncDecCpCmp<$t> {
422 fn dec(&self, a: &$t, b:&$t) ->Option<$t> {
423 let res=a - b;
424 if res.is_nan() || res >=*a { None } else { Some(res) }
425 }
426 fn inc(&self, a: &$t, b: &$t) -> Option<$t> {
427 let res=a + b;
428 if res.is_nan() || res <=*a { None } else { Some(res) }
429 }
430 fn cp_v(&self,v: &$t) ->$t {
431 return *v;
432 }
433 }
434
435 impl DefaultValues<$t,$t> for NumberIncDecCpCmp<$t> {
436 fn default_step(&self) ->$t { return 1.0}
437 fn default_rebound(&self) ->$t { return 1.0 }
438 fn default_min() ->$t { <$t>::MIN }
439 fn default_max() ->$t { <$t>::MAX }
440 }
441 )*
442 };
443}
444
445impl_inc_dec_cp_cmp_trait_u!(u8, u16, u32, u64, u128, usize);
446impl_inc_dec_cp_cmp_trait_i!(i8, i16, i32, i64, i128, isize);
447impl_inc_dec_cp_cmp_trait_f!(f32, f64);
448
449pub trait GetBeginEndOption<T, R: GetBeginEnd<T>> {
451 fn factory(&self, opt: Option<(T, T)>) -> Option<R>;
452 fn new_range(&self, src: (T, T)) -> R;
453}
454
455#[derive(Copy, Clone)]
457pub struct MrsFactory<T> {
458 _t: PhantomData<T>,
459}
460
461#[derive(Copy, Clone)]
463pub struct RiFactory<T> {
464 _t: PhantomData<T>,
465}
466
467impl<T> RiFactory<T> {
468 pub fn new() -> Self {
469 return Self { _t: PhantomData };
470 }
471}
472
473impl<T> MrsFactory<T> {
474 pub fn new() -> Self {
475 return Self { _t: PhantomData };
476 }
477}
478
479impl<T> GetBeginEndOption<T, Mrs<T>> for MrsFactory<T> {
480 fn new_range(&self, src: (T, T)) -> Mrs<T> {
481 return Mrs::new(src.0, src.1);
482 }
483 fn factory(&self, opt: Option<(T, T)>) -> Option<Mrs<T>> {
484 match opt {
485 Some((a, z)) => Some(Mrs::new(a, z)),
486 None => None,
487 }
488 }
489}
490
491impl<T> GetBeginEndOption<T, RangeInclusive<T>> for RiFactory<T> {
492 fn new_range(&self, src: (T, T)) -> RangeInclusive<T> {
493 return RangeInclusive::new(src.0, src.1);
494 }
495 fn factory(&self, opt: Option<(T, T)>) -> Option<RangeInclusive<T>> {
496 match opt {
497 Some((a, z)) => Some(RangeInclusive::new(a, z)),
498 None => None,
499 }
500 }
501}