Thu, 01 May 2025 08:40:33 -0500
remove quadratic
|
97
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
1 | /*! |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
2 | Bounded and minimizable/maximizable mappings. |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
3 | */ |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
4 | |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
5 | use crate::instance::Instance; |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
6 | use crate::loc::Loc; |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
7 | use crate::mapping::RealMapping; |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
8 | use crate::sets::{Cube, Set}; |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
9 | use crate::types::{Float, Num}; |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
10 | |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
11 | /// Trait for globally analysing a property `A` of a [`Mapping`]. |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
12 | /// |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
13 | /// Typically `A` is an [`Aggregator`][super::aggregator::Aggregator] such as |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
14 | /// [`Bounds`][super::aggregator::Bounds]. |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
15 | pub trait GlobalAnalysis<F: Num, A> { |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
16 | /// Perform global analysis of the property `A` of `Self`. |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
17 | /// |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
18 | /// As an example, in the case of `A` being [`Bounds`][super::aggregator::Bounds], |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
19 | /// this function will return global upper and lower bounds for the mapping |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
20 | /// represented by `self`. |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
21 | fn global_analysis(&self) -> A; |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
22 | } |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
23 | |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
24 | // default impl<F, A, N, L> GlobalAnalysis<F, A, N> for L |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
25 | // where L : LocalAnalysis<F, A, N> { |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
26 | // #[inline] |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
27 | // fn global_analysis(&self) -> Bounds<F> { |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
28 | // self.local_analysis(&self.support_hint()) |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
29 | // } |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
30 | // } |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
31 | |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
32 | /// Trait for locally analysing a property `A` of a [`Mapping`] (implementing [`Support`]) |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
33 | /// within a [`Cube`]. |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
34 | /// |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
35 | /// Typically `A` is an [`Aggregator`][super::aggregator::Aggregator] such as |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
36 | /// [`Bounds`][super::aggregator::Bounds]. |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
37 | pub trait LocalAnalysis<F: Num, A, const N: usize>: GlobalAnalysis<F, A> { |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
38 | /// Perform local analysis of the property `A` of `Self`. |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
39 | /// |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
40 | /// As an example, in the case of `A` being [`Bounds`][super::aggregator::Bounds], |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
41 | /// this function will return upper and lower bounds within `cube` for the mapping |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
42 | /// represented by `self`. |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
43 | fn local_analysis(&self, cube: &Cube<F, N>) -> A; |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
44 | } |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
45 | |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
46 | /// Trait for determining the upper and lower bounds of an float-valued [`Mapping`]. |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
47 | /// |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
48 | /// This is a blanket-implemented alias for [`GlobalAnalysis`]`<F, Bounds<F>>` |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
49 | /// [`Mapping`] is not a supertrait to allow flexibility in the implementation of either |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
50 | /// reference or non-reference arguments. |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
51 | pub trait Bounded<F: Float>: GlobalAnalysis<F, Bounds<F>> { |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
52 | /// Return lower and upper bounds for the values of of `self`. |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
53 | #[inline] |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
54 | fn bounds(&self) -> Bounds<F> { |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
55 | self.global_analysis() |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
56 | } |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
57 | } |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
58 | |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
59 | impl<F: Float, T: GlobalAnalysis<F, Bounds<F>>> Bounded<F> for T {} |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
60 | |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
61 | /// A [`RealMapping`] that provides rough bounds as well as minimisation and maximisation. |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
62 | pub trait MinMaxMapping<F: Float, const N: usize>: RealMapping<F, N> + Bounded<F> { |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
63 | /// Maximise the mapping within stated value `tolerance`. |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
64 | /// |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
65 | /// At most `max_steps` refinement steps are taken. |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
66 | /// Returns the approximate maximiser and the corresponding function value. |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
67 | fn maximise(&mut self, tolerance: F, max_steps: usize) -> (Loc<F, N>, F); |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
68 | |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
69 | /// Maximise the mapping within stated value `tolerance` subject to a lower bound. |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
70 | /// |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
71 | /// At most `max_steps` refinement steps are taken. |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
72 | /// Returns the approximate maximiser and the corresponding function value when one is found |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
73 | /// above the `bound` threshold, otherwise `None`. |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
74 | fn maximise_above( |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
75 | &mut self, |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
76 | bound: F, |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
77 | tolerance: F, |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
78 | max_steps: usize, |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
79 | ) -> Option<(Loc<F, N>, F)>; |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
80 | |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
81 | /// Minimise the mapping within stated value `tolerance`. |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
82 | /// |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
83 | /// At most `max_steps` refinement steps are taken. |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
84 | /// Returns the approximate minimiser and the corresponding function value. |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
85 | fn minimise(&mut self, tolerance: F, max_steps: usize) -> (Loc<F, N>, F); |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
86 | |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
87 | /// Minimise the mapping within stated value `tolerance` subject to a lower bound. |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
88 | /// |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
89 | /// At most `max_steps` refinement steps are taken. |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
90 | /// Returns the approximate minimiser and the corresponding function value when one is found |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
91 | /// above the `bound` threshold, otherwise `None`. |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
92 | fn minimise_below( |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
93 | &mut self, |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
94 | bound: F, |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
95 | tolerance: F, |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
96 | max_steps: usize, |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
97 | ) -> Option<(Loc<F, N>, F)>; |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
98 | |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
99 | /// Verify that the mapping has a given upper `bound` within indicated `tolerance`. |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
100 | /// |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
101 | /// At most `max_steps` refinement steps are taken. |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
102 | fn has_upper_bound(&mut self, bound: F, tolerance: F, max_steps: usize) -> bool; |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
103 | |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
104 | /// Verify that the mapping has a given lower `bound` within indicated `tolerance`. |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
105 | /// |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
106 | /// At most `max_steps` refinement steps are taken. |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
107 | fn has_lower_bound(&mut self, bound: F, tolerance: F, max_steps: usize) -> bool; |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
108 | } |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
109 | |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
110 | /// Upper and lower bounds on an `F`-valued function. |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
111 | #[derive(Copy, Clone, Debug)] |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
112 | pub struct Bounds<F>( |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
113 | /// Lower bound |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
114 | pub F, |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
115 | /// Upper bound |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
116 | pub F, |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
117 | ); |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
118 | |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
119 | impl<F: Copy> Bounds<F> { |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
120 | /// Returns the lower bound |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
121 | #[inline] |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
122 | pub fn lower(&self) -> F { |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
123 | self.0 |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
124 | } |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
125 | |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
126 | /// Returns the upper bound |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
127 | #[inline] |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
128 | pub fn upper(&self) -> F { |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
129 | self.1 |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
130 | } |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
131 | } |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
132 | |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
133 | impl<F: Float> Bounds<F> { |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
134 | /// Returns a uniform bound. |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
135 | /// |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
136 | /// This is maximum over the absolute values of the upper and lower bound. |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
137 | #[inline] |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
138 | pub fn uniform(&self) -> F { |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
139 | let &Bounds(lower, upper) = self; |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
140 | lower.abs().max(upper.abs()) |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
141 | } |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
142 | |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
143 | /// Construct a bounds, making sure `lower` bound is less than `upper` |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
144 | #[inline] |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
145 | pub fn corrected(lower: F, upper: F) -> Self { |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
146 | if lower <= upper { |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
147 | Bounds(lower, upper) |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
148 | } else { |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
149 | Bounds(upper, lower) |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
150 | } |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
151 | } |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
152 | |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
153 | /// Refine the lower bound |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
154 | #[inline] |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
155 | pub fn refine_lower(&self, lower: F) -> Self { |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
156 | let &Bounds(l, u) = self; |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
157 | debug_assert!(l <= u); |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
158 | Bounds(l.max(lower), u.max(lower)) |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
159 | } |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
160 | |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
161 | /// Refine the lower bound |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
162 | #[inline] |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
163 | pub fn refine_upper(&self, upper: F) -> Self { |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
164 | let &Bounds(l, u) = self; |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
165 | debug_assert!(l <= u); |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
166 | Bounds(l.min(upper), u.min(upper)) |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
167 | } |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
168 | } |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
169 | |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
170 | impl<'a, F: Float> std::ops::Add<Self> for Bounds<F> { |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
171 | type Output = Self; |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
172 | #[inline] |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
173 | fn add(self, Bounds(l2, u2): Self) -> Self::Output { |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
174 | let Bounds(l1, u1) = self; |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
175 | debug_assert!(l1 <= u1 && l2 <= u2); |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
176 | Bounds(l1 + l2, u1 + u2) |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
177 | } |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
178 | } |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
179 | |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
180 | impl<'a, F: Float> std::ops::Mul<Self> for Bounds<F> { |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
181 | type Output = Self; |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
182 | #[inline] |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
183 | fn mul(self, Bounds(l2, u2): Self) -> Self::Output { |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
184 | let Bounds(l1, u1) = self; |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
185 | debug_assert!(l1 <= u1 && l2 <= u2); |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
186 | let a = l1 * l2; |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
187 | let b = u1 * u2; |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
188 | // The order may flip when negative numbers are involved, so need min/max |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
189 | Bounds(a.min(b), a.max(b)) |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
190 | } |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
191 | } |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
192 | |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
193 | impl<F: Float> std::iter::Product for Bounds<F> { |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
194 | #[inline] |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
195 | fn product<I>(mut iter: I) -> Self |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
196 | where |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
197 | I: Iterator<Item = Self>, |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
198 | { |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
199 | match iter.next() { |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
200 | None => Bounds(F::ZERO, F::ZERO), |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
201 | Some(init) => iter.fold(init, |a, b| a * b), |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
202 | } |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
203 | } |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
204 | } |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
205 | |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
206 | impl<F: Float> Set<F> for Bounds<F> { |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
207 | fn contains<I: Instance<F>>(&self, item: I) -> bool { |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
208 | let v = item.own(); |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
209 | let &Bounds(l, u) = self; |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
210 | debug_assert!(l <= u); |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
211 | l <= v && v <= u |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
212 | } |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
213 | } |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
214 | |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
215 | impl<F: Float> Bounds<F> { |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
216 | /// Calculate a common bound (glb, lub) for two bounds. |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
217 | #[inline] |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
218 | pub fn common(&self, &Bounds(l2, u2): &Self) -> Self { |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
219 | let &Bounds(l1, u1) = self; |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
220 | debug_assert!(l1 <= u1 && l2 <= u2); |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
221 | Bounds(l1.min(l2), u1.max(u2)) |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
222 | } |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
223 | |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
224 | /// Indicates whether `Self` is a superset of the argument bound. |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
225 | #[inline] |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
226 | pub fn superset(&self, &Bounds(l2, u2): &Self) -> bool { |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
227 | let &Bounds(l1, u1) = self; |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
228 | debug_assert!(l1 <= u1 && l2 <= u2); |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
229 | l1 <= l2 && u2 <= u1 |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
230 | } |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
231 | |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
232 | /// Returns the greatest bound contained by both argument bounds, if one exists. |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
233 | #[inline] |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
234 | pub fn glb(&self, &Bounds(l2, u2): &Self) -> Option<Self> { |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
235 | let &Bounds(l1, u1) = self; |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
236 | debug_assert!(l1 <= u1 && l2 <= u2); |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
237 | let l = l1.max(l2); |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
238 | let u = u1.min(u2); |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
239 | debug_assert!(l <= u); |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
240 | if l < u { |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
241 | Some(Bounds(l, u)) |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
242 | } else { |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
243 | None |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
244 | } |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
245 | } |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
246 | } |