src/bounds.rs

Wed, 22 Apr 2026 23:41:59 -0500

author
Tuomo Valkonen <tuomov@iki.fi>
date
Wed, 22 Apr 2026 23:41:59 -0500
branch
dev
changeset 197
1f301affeae3
parent 126
0ccad3ee8e95
permissions
-rw-r--r--

Fix internal links in documentation

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
125
25b6c8b20d1b Allow MinMaxMappings on any domain
Tuomo Valkonen <tuomov@iki.fi>
parents: 124
diff changeset
5 use crate::instance::{Instance, Space};
25b6c8b20d1b Allow MinMaxMappings on any domain
Tuomo Valkonen <tuomov@iki.fi>
parents: 124
diff changeset
6 use crate::mapping::Mapping;
97
4e80fb049dca MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
7 use crate::sets::{Cube, Set};
4e80fb049dca MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
8 use crate::types::{Float, Num};
4e80fb049dca MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
9
4e80fb049dca MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
10 /// 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
11 ///
197
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 126
diff changeset
12 /// Typically `A` is an [`Aggregator`][super::bisection_tree::Aggregator]
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 126
diff changeset
13 /// such as [`Bounds`].
97
4e80fb049dca MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
14 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
15 /// 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
16 ///
197
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 126
diff changeset
17 /// As an example, in the case of `A` being [`Bounds`],
97
4e80fb049dca MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
18 /// 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
19 /// represented by `self`.
4e80fb049dca MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
20 fn global_analysis(&self) -> A;
4e80fb049dca MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
21 }
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 // 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
24 // 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
25 // #[inline]
4e80fb049dca MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
26 // 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
27 // 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
28 // }
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
197
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 126
diff changeset
31 /// Trait for locally analysing a property `A` of a [`Mapping`] (implementing [`super::bisection_tree::Support`])
97
4e80fb049dca MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
32 /// within a [`Cube`].
4e80fb049dca MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
33 ///
197
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 126
diff changeset
34 /// Typically `A` is an [`Aggregator`][super::bisection_tree::Aggregator] such as [`Bounds`].
97
4e80fb049dca MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
35 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
36 /// 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
37 ///
197
1f301affeae3 Fix internal links in documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 126
diff changeset
38 /// As an example, in the case of `A` being [`Bounds`],
97
4e80fb049dca MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
39 /// 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
40 /// represented by `self`.
124
6aa955ad8122 Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents: 97
diff changeset
41 fn local_analysis(&self, cube: &Cube<N, F>) -> A;
97
4e80fb049dca MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
42 }
4e80fb049dca MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
43
4e80fb049dca MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
44 /// 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
45 ///
4e80fb049dca MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
46 /// 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
47 /// [`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
48 /// reference or non-reference arguments.
4e80fb049dca MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
49 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
50 /// 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
51 #[inline]
4e80fb049dca MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
52 fn bounds(&self) -> Bounds<F> {
4e80fb049dca MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
53 self.global_analysis()
4e80fb049dca MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
54 }
4e80fb049dca MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
55 }
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 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
58
126
0ccad3ee8e95 MinMaxMapping default implementations
Tuomo Valkonen <tuomov@iki.fi>
parents: 125
diff changeset
59 /// A real-valued [`Mapping`] that provides rough bounds as well as minimisation and maximisation.
125
25b6c8b20d1b Allow MinMaxMappings on any domain
Tuomo Valkonen <tuomov@iki.fi>
parents: 124
diff changeset
60 pub trait MinMaxMapping<Domain: Space, F: Float = f64>:
25b6c8b20d1b Allow MinMaxMappings on any domain
Tuomo Valkonen <tuomov@iki.fi>
parents: 124
diff changeset
61 Mapping<Domain, Codomain = F> + Bounded<F>
25b6c8b20d1b Allow MinMaxMappings on any domain
Tuomo Valkonen <tuomov@iki.fi>
parents: 124
diff changeset
62 {
97
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.
125
25b6c8b20d1b Allow MinMaxMappings on any domain
Tuomo Valkonen <tuomov@iki.fi>
parents: 124
diff changeset
67 fn maximise(&mut self, tolerance: F, max_steps: usize) -> (Domain, F);
97
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`.
126
0ccad3ee8e95 MinMaxMapping default implementations
Tuomo Valkonen <tuomov@iki.fi>
parents: 125
diff changeset
74 fn maximise_above(&mut self, bound: F, tolerance: F, max_steps: usize) -> Option<(Domain, F)> {
0ccad3ee8e95 MinMaxMapping default implementations
Tuomo Valkonen <tuomov@iki.fi>
parents: 125
diff changeset
75 let res @ (_, v) = self.maximise(tolerance, max_steps);
0ccad3ee8e95 MinMaxMapping default implementations
Tuomo Valkonen <tuomov@iki.fi>
parents: 125
diff changeset
76 (v > bound).then_some(res)
0ccad3ee8e95 MinMaxMapping default implementations
Tuomo Valkonen <tuomov@iki.fi>
parents: 125
diff changeset
77 }
97
4e80fb049dca MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
78
4e80fb049dca MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
79 /// 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
80 ///
4e80fb049dca MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
81 /// 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
82 /// Returns the approximate minimiser and the corresponding function value.
125
25b6c8b20d1b Allow MinMaxMappings on any domain
Tuomo Valkonen <tuomov@iki.fi>
parents: 124
diff changeset
83 fn minimise(&mut self, tolerance: F, max_steps: usize) -> (Domain, F);
97
4e80fb049dca MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
84
4e80fb049dca MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
85 /// 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
86 ///
4e80fb049dca MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
87 /// 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
88 /// Returns the approximate minimiser and the corresponding function value when one is found
126
0ccad3ee8e95 MinMaxMapping default implementations
Tuomo Valkonen <tuomov@iki.fi>
parents: 125
diff changeset
89 /// below the `bound` threshold, otherwise `None`.
0ccad3ee8e95 MinMaxMapping default implementations
Tuomo Valkonen <tuomov@iki.fi>
parents: 125
diff changeset
90 fn minimise_below(&mut self, bound: F, tolerance: F, max_steps: usize) -> Option<(Domain, F)> {
0ccad3ee8e95 MinMaxMapping default implementations
Tuomo Valkonen <tuomov@iki.fi>
parents: 125
diff changeset
91 let res @ (_, v) = self.minimise(tolerance, max_steps);
0ccad3ee8e95 MinMaxMapping default implementations
Tuomo Valkonen <tuomov@iki.fi>
parents: 125
diff changeset
92 (v < bound).then_some(res)
0ccad3ee8e95 MinMaxMapping default implementations
Tuomo Valkonen <tuomov@iki.fi>
parents: 125
diff changeset
93 }
97
4e80fb049dca MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
94
4e80fb049dca MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
95 /// 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
96 ///
4e80fb049dca MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
97 /// At most `max_steps` refinement steps are taken.
126
0ccad3ee8e95 MinMaxMapping default implementations
Tuomo Valkonen <tuomov@iki.fi>
parents: 125
diff changeset
98 fn has_upper_bound(&mut self, bound: F, tolerance: F, max_steps: usize) -> bool {
0ccad3ee8e95 MinMaxMapping default implementations
Tuomo Valkonen <tuomov@iki.fi>
parents: 125
diff changeset
99 match self.maximise_above(bound, tolerance, max_steps) {
0ccad3ee8e95 MinMaxMapping default implementations
Tuomo Valkonen <tuomov@iki.fi>
parents: 125
diff changeset
100 None => true,
0ccad3ee8e95 MinMaxMapping default implementations
Tuomo Valkonen <tuomov@iki.fi>
parents: 125
diff changeset
101 Some((_, v)) => v <= bound,
0ccad3ee8e95 MinMaxMapping default implementations
Tuomo Valkonen <tuomov@iki.fi>
parents: 125
diff changeset
102 }
0ccad3ee8e95 MinMaxMapping default implementations
Tuomo Valkonen <tuomov@iki.fi>
parents: 125
diff changeset
103 }
97
4e80fb049dca MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
104
4e80fb049dca MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
105 /// 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
106 ///
4e80fb049dca MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
107 /// At most `max_steps` refinement steps are taken.
126
0ccad3ee8e95 MinMaxMapping default implementations
Tuomo Valkonen <tuomov@iki.fi>
parents: 125
diff changeset
108 fn has_lower_bound(&mut self, bound: F, tolerance: F, max_steps: usize) -> bool {
0ccad3ee8e95 MinMaxMapping default implementations
Tuomo Valkonen <tuomov@iki.fi>
parents: 125
diff changeset
109 match self.minimise_below(bound, tolerance, max_steps) {
0ccad3ee8e95 MinMaxMapping default implementations
Tuomo Valkonen <tuomov@iki.fi>
parents: 125
diff changeset
110 None => true,
0ccad3ee8e95 MinMaxMapping default implementations
Tuomo Valkonen <tuomov@iki.fi>
parents: 125
diff changeset
111 Some((_, v)) => v >= bound,
0ccad3ee8e95 MinMaxMapping default implementations
Tuomo Valkonen <tuomov@iki.fi>
parents: 125
diff changeset
112 }
0ccad3ee8e95 MinMaxMapping default implementations
Tuomo Valkonen <tuomov@iki.fi>
parents: 125
diff changeset
113 }
97
4e80fb049dca MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
114 }
4e80fb049dca MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
115
4e80fb049dca MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
116 /// 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
117 #[derive(Copy, Clone, Debug)]
4e80fb049dca MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
118 pub struct Bounds<F>(
4e80fb049dca MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
119 /// Lower bound
4e80fb049dca MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
120 pub F,
4e80fb049dca MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
121 /// Upper bound
4e80fb049dca MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
122 pub F,
4e80fb049dca MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
123 );
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 impl<F: Copy> Bounds<F> {
4e80fb049dca MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
126 /// Returns the lower 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 lower(&self) -> F {
4e80fb049dca MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
129 self.0
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 /// Returns the upper bound
4e80fb049dca MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
133 #[inline]
4e80fb049dca MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
134 pub fn upper(&self) -> F {
4e80fb049dca MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
135 self.1
4e80fb049dca MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
136 }
4e80fb049dca MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
137 }
4e80fb049dca MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
138
4e80fb049dca MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
139 impl<F: Float> Bounds<F> {
4e80fb049dca MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
140 /// Returns a uniform bound.
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 /// 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
143 #[inline]
4e80fb049dca MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
144 pub fn uniform(&self) -> F {
4e80fb049dca MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
145 let &Bounds(lower, upper) = self;
4e80fb049dca MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
146 lower.abs().max(upper.abs())
4e80fb049dca MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
147 }
4e80fb049dca MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
148
4e80fb049dca MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
149 /// 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
150 #[inline]
4e80fb049dca MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
151 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
152 if lower <= upper {
4e80fb049dca MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
153 Bounds(lower, upper)
4e80fb049dca MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
154 } else {
4e80fb049dca MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
155 Bounds(upper, lower)
4e80fb049dca MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
156 }
4e80fb049dca MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
157 }
4e80fb049dca MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
158
4e80fb049dca MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
159 /// Refine the lower bound
4e80fb049dca MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
160 #[inline]
4e80fb049dca MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
161 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
162 let &Bounds(l, u) = self;
4e80fb049dca MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
163 debug_assert!(l <= u);
4e80fb049dca MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
164 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
165 }
4e80fb049dca MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
166
4e80fb049dca MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
167 /// Refine the lower bound
4e80fb049dca MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
168 #[inline]
4e80fb049dca MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
169 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
170 let &Bounds(l, u) = self;
4e80fb049dca MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
171 debug_assert!(l <= u);
4e80fb049dca MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
172 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
173 }
4e80fb049dca MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
174 }
4e80fb049dca MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
175
4e80fb049dca MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
176 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
177 type Output = Self;
4e80fb049dca MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
178 #[inline]
4e80fb049dca MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
179 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
180 let Bounds(l1, u1) = self;
4e80fb049dca MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
181 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
182 Bounds(l1 + l2, u1 + u2)
4e80fb049dca MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
183 }
4e80fb049dca MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
184 }
4e80fb049dca MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
185
4e80fb049dca MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
186 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
187 type Output = Self;
4e80fb049dca MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
188 #[inline]
4e80fb049dca MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
189 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
190 let Bounds(l1, u1) = self;
4e80fb049dca MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
191 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
192 let a = l1 * l2;
4e80fb049dca MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
193 let b = u1 * u2;
4e80fb049dca MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
194 // 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
195 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
196 }
4e80fb049dca MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
197 }
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 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
200 #[inline]
4e80fb049dca MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
201 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
202 where
4e80fb049dca MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
203 I: Iterator<Item = Self>,
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 match iter.next() {
4e80fb049dca MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
206 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
207 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
208 }
4e80fb049dca MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
209 }
4e80fb049dca MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
210 }
4e80fb049dca MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
211
4e80fb049dca MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
212 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
213 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
214 let v = item.own();
4e80fb049dca MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
215 let &Bounds(l, u) = self;
4e80fb049dca MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
216 debug_assert!(l <= u);
4e80fb049dca MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
217 l <= v && v <= u
4e80fb049dca MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
218 }
4e80fb049dca MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
219 }
4e80fb049dca MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
220
4e80fb049dca MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
221 impl<F: Float> Bounds<F> {
4e80fb049dca MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
222 /// 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
223 #[inline]
4e80fb049dca MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
224 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
225 let &Bounds(l1, u1) = self;
4e80fb049dca MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
226 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
227 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
228 }
4e80fb049dca MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
229
4e80fb049dca MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
230 /// 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
231 #[inline]
4e80fb049dca MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
232 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
233 let &Bounds(l1, u1) = self;
4e80fb049dca MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
234 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
235 l1 <= l2 && u2 <= u1
4e80fb049dca MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
236 }
4e80fb049dca MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
237
4e80fb049dca MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
238 /// 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
239 #[inline]
4e80fb049dca MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
240 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
241 let &Bounds(l1, u1) = self;
4e80fb049dca MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
242 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
243 let l = l1.max(l2);
4e80fb049dca MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
244 let u = u1.min(u2);
4e80fb049dca MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
245 debug_assert!(l <= u);
4e80fb049dca MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
246 if l < u {
4e80fb049dca MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
247 Some(Bounds(l, u))
4e80fb049dca MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
248 } else {
4e80fb049dca MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
249 None
4e80fb049dca MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
250 }
4e80fb049dca MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
251 }
4e80fb049dca MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
252 }

mercurial