1use crate::prelude::*;
3use std::fmt;
4
5pub type Interval = crate::Interval<f64>;
6
7#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
9#[cfg_attr(feature = "serialize", derive(Serialize, Deserialize))]
10pub struct Reals;
11
12impl Reals {
13    pub fn bounded(self, lb: f64, ub: f64) -> Interval { Interval::bounded(lb, ub) }
14
15    pub fn lower_bounded(self, lb: f64) -> Interval { Interval::lower_bounded(lb) }
16
17    pub fn upper_bounded(self, ub: f64) -> Interval { Interval::upper_bounded(ub) }
18}
19
20impl Space for Reals {
21    const DIM: usize = 1;
22
23    type Value = f64;
24
25    fn card(&self) -> Card { Card::Infinite }
26
27    fn contains(&self, _: &f64) -> bool { true }
28}
29
30impl OrderedSpace for Reals {}
31
32impl_union_intersect!(Reals, Reals);
33
34impl fmt::Display for Reals {
35    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "\u{211d}") }
36}
37
38#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
40#[cfg_attr(feature = "serialize", derive(Serialize, Deserialize))]
41pub struct NonNegativeReals;
42
43impl Space for NonNegativeReals {
44    const DIM: usize = 1;
45
46    type Value = f64;
47
48    fn card(&self) -> Card { Card::Infinite }
49
50    fn contains(&self, val: &f64) -> bool { *val >= 0.0 }
51}
52
53impl OrderedSpace for NonNegativeReals {
54    fn min(&self) -> Option<f64> { Some(0.0) }
55}
56
57impl_union_intersect!(NonNegativeReals, NonNegativeReals);
58
59impl fmt::Display for NonNegativeReals {
60    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "\u{211d}(>0)") }
61}
62
63#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
65#[cfg_attr(feature = "serialize", derive(Serialize, Deserialize))]
66pub struct PositiveReals;
67
68impl Space for PositiveReals {
69    const DIM: usize = 1;
70
71    type Value = f64;
72
73    fn card(&self) -> Card { Card::Infinite }
74
75    fn contains(&self, val: &f64) -> bool { *val > 0.0 }
76}
77
78impl OrderedSpace for PositiveReals {
79    fn inf(&self) -> Option<f64> { Some(0.0) }
80}
81
82impl_union_intersect!(PositiveReals, PositiveReals);
83
84impl fmt::Display for PositiveReals {
85    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "\u{211d}(≥0)") }
86}
87
88#[cfg(test)]
89mod tests {
90    use super::*;
91
92    #[cfg(feature = "serialize")]
93    extern crate serde_test;
94    #[cfg(feature = "serialize")]
95    use self::serde_test::{assert_tokens, Token};
96
97    #[test]
98    fn test_bounded() {
99        let d = Reals;
100
101        assert_eq!(d.bounded(0.0, 1.0), Interval::bounded(0.0, 1.0));
102    }
103
104    #[test]
105    fn test_card() {
106        let d = Reals;
107
108        assert_eq!(d.card(), Card::Infinite);
109    }
110
111    #[cfg(feature = "serialize")]
112    #[test]
113    fn test_serialisation() {
114        let d = Reals;
115
116        assert_tokens(&d, &[Token::UnitStruct { name: "Reals" }]);
117    }
118}