fyrox_core/
numeric_range.rs

1// Copyright (c) 2019-present Dmitry Stepanov and Fyrox Engine contributors.
2//
3// Permission is hereby granted, free of charge, to any person obtaining a copy
4// of this software and associated documentation files (the "Software"), to deal
5// in the Software without restriction, including without limitation the rights
6// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7// copies of the Software, and to permit persons to whom the Software is
8// furnished to do so, subject to the following conditions:
9//
10// The above copyright notice and this permission notice shall be included in all
11// copies or substantial portions of the Software.
12//
13// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19// SOFTWARE.
20
21use crate::num_traits::Num;
22use rand::{distributions::uniform::SampleUniform, Rng};
23use std::ops::Range;
24
25fn min<T>(a: T, b: T) -> T
26where
27    T: PartialOrd,
28{
29    if a > b {
30        b
31    } else {
32        a
33    }
34}
35
36fn max<T>(a: T, b: T) -> T
37where
38    T: PartialOrd,
39{
40    if a > b {
41        a
42    } else {
43        b
44    }
45}
46
47pub trait RangeExt<T>
48where
49    T: Num + PartialOrd + SampleUniform + Copy,
50{
51    fn random<R: Rng>(&self, rng: &mut R) -> T;
52
53    fn clamp_value(&self, value: &mut T) -> T;
54}
55
56impl<T: Num + PartialOrd + SampleUniform + Copy> RangeExt<T> for Range<T> {
57    #[inline]
58    fn random<R: Rng>(&self, rng: &mut R) -> T {
59        let start = min(self.start, self.end);
60        let end = max(self.start, self.end);
61        if start == end {
62            start
63        } else {
64            rng.gen_range(Range { start, end })
65        }
66    }
67
68    #[inline]
69    fn clamp_value(&self, value: &mut T) -> T {
70        let start = min(self.start, self.end);
71        let end = max(self.start, self.end);
72
73        if *value < start {
74            start
75        } else if *value > end {
76            end
77        } else {
78            *value
79        }
80    }
81}
82
83#[cfg(test)]
84mod test {
85    use rand::thread_rng;
86
87    use super::*;
88
89    #[test]
90    fn test_random() {
91        let mut rng = thread_rng();
92
93        let res = (1..10).random(&mut rng);
94        assert!((1..=10).contains(&res));
95
96        let res = Range { start: 10, end: 1 }.random(&mut rng);
97        assert!((1..=10).contains(&res));
98
99        let res = (1..1).random(&mut rng);
100        assert!(res == 1);
101    }
102
103    #[test]
104    fn test_clamp_value() {
105        let res = (1..10).clamp_value(&mut 5);
106        assert_eq!(res, 5);
107
108        let res = (1..10).clamp_value(&mut 0);
109        assert_eq!(res, 1);
110
111        let res = (1..10).clamp_value(&mut 11);
112        assert_eq!(res, 10);
113    }
114}