1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
use std::{
cmp,
ffi::{c_void, CStr, CString},
fmt::Debug,
hash::Hash,
ops::{BitAnd, Range, RangeInclusive},
ptr,
};
use crate::{
collections::base::{impl_collection, Collection, Span},
errors::ParseError,
};
use super::number_span::NumberSpan;
pub struct FloatSpan {
_inner: ptr::NonNull<meos_sys::Span>,
}
impl Drop for FloatSpan {
fn drop(&mut self) {
unsafe {
libc::free(self._inner.as_ptr().cast::<c_void>());
}
}
}
impl Collection for FloatSpan {
impl_collection!(span, f64);
fn contains(&self, content: &f64) -> bool {
unsafe { meos_sys::contains_span_float(self.inner(), *content) }
}
}
impl Span for FloatSpan {
type SubsetType = Self::Type;
fn inner(&self) -> *const meos_sys::Span {
self._inner.as_ptr()
}
/// Creates a new `FloatSpan` from an inner pointer to a `meos_sys::Span`.
///
/// # Arguments
/// * `inner` - A pointer to the inner `meos_sys::Span`.
///
/// ## Returns
/// * A new `FloatSpan` instance.
fn from_inner(inner: *mut meos_sys::Span) -> Self {
Self {
_inner: ptr::NonNull::new(inner).expect("Null pointers not allowed"),
}
}
/// Returns the lower bound of the span.
///
/// ## Returns
/// * The lower bound as a `f64`.
///
/// ## Example
/// ```
/// # use meos::FloatSpan;
/// # use meos::Span;
///
/// let span: FloatSpan = (12.9..67.8).into();
/// let lower = span.lower();
/// ```
fn lower(&self) -> Self::Type {
unsafe { meos_sys::floatspan_lower(self.inner()) }
}
/// Returns the upper bound of the span.
///
/// ## Returns
/// * The upper bound as a `f64`.
///
/// ## Example
/// ```
/// # use meos::FloatSpan;
/// # use meos::Span;
///
/// let span: FloatSpan = (12.9..67.8).into();;
///
/// assert_eq!(span.upper(), 67.8)
/// ```
fn upper(&self) -> Self::Type {
unsafe { meos_sys::floatspan_upper(self.inner()) }
}
/// Return a new `FloatSpan` with the lower and upper bounds shifted by `delta`.
///
/// # Arguments
/// * `delta` - The value to shift by.
///
/// # Returns
/// A new `FloatSpan` instance.
///
/// # Example
/// ```
/// # use meos::FloatSpan;
/// # use meos::Span;
///
/// let span: FloatSpan = (12.9..67.8).into();
/// let shifted_span = span.shift(5.0);
///
/// assert_eq!(shifted_span, (17.9..72.8).into())
/// ```
fn shift(&self, delta: f64) -> FloatSpan {
self.shift_scale(Some(delta), None)
}
/// Return a new `FloatSpan` with the lower and upper bounds scaled so that the width is `width`.
///
/// # Arguments
/// * `width` - The new width.
///
/// # Returns
/// A new `FloatSpan` instance.
///
/// # Example
/// ```
/// # use meos::FloatSpan;
/// # use meos::Span;
///
/// let span: FloatSpan = (12.9..67.8).into();
/// let scaled_span = span.scale(10.0);
///
/// assert_eq!(scaled_span, (12.9..22.9).into())
/// ```
fn scale(&self, width: f64) -> FloatSpan {
self.shift_scale(None, Some(width))
}
/// Return a new `FloatSpan` with the lower and upper bounds shifted by `delta` and scaled so that the width is `width`.
///
/// # Arguments
/// * `delta` - The value to shift by.
/// * `width` - The new width.
///
/// # Returns
/// A new `FloatSpan` instance.
///
/// # Example
/// ```
/// # use meos::FloatSpan;
/// # use meos::Span;
///
/// let span: FloatSpan = (12.9..67.8).into();
/// let shifted_scaled_span = span.shift_scale(Some(5.0), Some(10.0));
///
/// assert_eq!(shifted_scaled_span, (17.9..27.9).into())
/// ```
fn shift_scale(&self, delta: Option<f64>, width: Option<f64>) -> FloatSpan {
let d = delta.unwrap_or(0.0);
let w = width.unwrap_or(0.0);
let modified = unsafe {
meos_sys::floatspan_shift_scale(self.inner(), d, w, delta.is_some(), width.is_some())
};
FloatSpan::from_inner(modified)
}
fn distance_to_value(&self, other: &Self::Type) -> f64 {
unsafe { meos_sys::distance_span_float(self.inner(), *other) }
}
fn distance_to_span(&self, other: &Self) -> f64 {
unsafe { meos_sys::distance_floatspan_floatspan(self.inner(), other.inner()) }
}
}
impl NumberSpan for FloatSpan {}
impl Clone for FloatSpan {
fn clone(&self) -> Self {
unsafe { Self::from_inner(meos_sys::span_copy(self.inner())) }
}
}
impl Hash for FloatSpan {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
let hash = unsafe { meos_sys::span_hash(self.inner()) };
state.write_u32(hash);
let _ = state.finish();
}
}
impl std::str::FromStr for FloatSpan {
type Err = ParseError;
/// Parses a `FloatSpan` from a string representation.
///
/// ## Arguments
/// * `string` - A string slice containing the representation.
///
/// ## Returns
/// * A `FloatSpan` instance.
///
/// ## Errors
/// * Returns `ParseSpanError` if the string cannot be parsed.
///
/// ## Example
/// ```
/// # use meos::FloatSpan;
/// # use meos::Span;
/// # use std::str::FromStr;
///
/// let span: FloatSpan = "(12.9, 67.8)".parse().expect("Failed to parse span");
/// assert_eq!(span.lower(), 12.9);
/// assert_eq!(span.upper(), 67.8);
/// ```
fn from_str(string: &str) -> Result<Self, Self::Err> {
CString::new(string).map_err(|_| ParseError).map(|string| {
let inner = unsafe { meos_sys::floatspan_in(string.as_ptr()) };
Self::from_inner(inner)
})
}
}
impl cmp::PartialEq for FloatSpan {
/// Checks if two `FloatSpan` instances are equal.
///
/// # Arguments
/// * `other` - Another `FloatSpan` instance.
///
/// ## Returns
/// * `true` if the spans are equal, `false` otherwise.
///
/// ## Example
/// ```
/// # use meos::FloatSpan;
/// # use meos::Span;
/// # use std::str::FromStr;
///
/// let span1: FloatSpan = (12.9..67.8).into();
/// let span2: FloatSpan = (12.9..67.8).into();
/// assert_eq!(span1, span2);
/// ```
fn eq(&self, other: &Self) -> bool {
unsafe { meos_sys::span_eq(self.inner(), other.inner()) }
}
}
impl cmp::Eq for FloatSpan {}
impl From<Range<f64>> for FloatSpan {
fn from(Range { start, end }: Range<f64>) -> Self {
let inner = unsafe { meos_sys::floatspan_make(start, end, true, false) };
Self::from_inner(inner)
}
}
impl From<Range<f32>> for FloatSpan {
fn from(Range { start, end }: Range<f32>) -> Self {
let inner =
unsafe { meos_sys::floatspan_make(f64::from(start), f64::from(end), true, false) };
Self::from_inner(inner)
}
}
impl From<RangeInclusive<f64>> for FloatSpan {
fn from(range: RangeInclusive<f64>) -> Self {
let inner = unsafe { meos_sys::floatspan_make(*range.start(), *range.end(), true, true) };
Self::from_inner(inner)
}
}
impl From<RangeInclusive<f32>> for FloatSpan {
fn from(range: RangeInclusive<f32>) -> Self {
let inner = unsafe {
meos_sys::floatspan_make(
f64::from(*range.start()),
f64::from(*range.end()),
true,
true,
)
};
Self::from_inner(inner)
}
}
impl Debug for FloatSpan {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let out_str = unsafe { meos_sys::floatspan_out(self.inner(), 3) };
// Default of 3 decimal digits
let c_str = unsafe { CStr::from_ptr(out_str) };
let str = c_str.to_str().map_err(|_| std::fmt::Error)?;
let result = f.write_str(str);
unsafe { libc::free(out_str.cast::<c_void>()) };
result
}
}
// Implement BitAnd for intersection with FloatSpan
impl BitAnd for FloatSpan {
type Output = Option<FloatSpan>;
/// Computes the intersection of two `FloatSpan` instances.
///
/// # Arguments
/// * `other` - Another `FloatSpan` instance.
///
/// ## Returns
/// * An `Option<FloatSpan>` containing the intersection, or `None` if there is no intersection.
///
/// ## Example
/// ```
/// # use meos::FloatSpan;
/// # use meos::Span;
/// # use std::str::FromStr;
///
/// let span1: FloatSpan = (12.9..67.8).into();
/// let span2: FloatSpan = (50.0..80.0).into();
/// let intersection = (span1 & span2).unwrap();
///
/// assert_eq!(intersection, (50.0..67.8).into())
/// ```
fn bitand(self, other: FloatSpan) -> Self::Output {
self.intersection(&other)
}
}
impl PartialOrd for FloatSpan {
fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> {
let cmp = unsafe { meos_sys::span_cmp(self.inner(), other.inner()) };
match cmp {
-1 => Some(cmp::Ordering::Less),
0 => Some(cmp::Ordering::Equal),
1 => Some(cmp::Ordering::Greater),
_ => None,
}
}
}
impl Ord for FloatSpan {
fn cmp(&self, other: &Self) -> cmp::Ordering {
self.partial_cmp(other).expect(
"Unreachable since for non-null and same types spans, we only return -1, 0, or 1",
)
}
}