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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
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 IntSpan {
_inner: ptr::NonNull<meos_sys::Span>,
}
impl Drop for IntSpan {
fn drop(&mut self) {
unsafe {
libc::free(self._inner.as_ptr().cast::<c_void>());
}
}
}
impl Collection for IntSpan {
impl_collection!(span, i32);
fn contains(&self, content: &i32) -> bool {
unsafe { meos_sys::contains_span_int(self.inner(), *content) }
}
}
impl Span for IntSpan {
type SubsetType = Self::Type;
fn inner(&self) -> *const meos_sys::Span {
self._inner.as_ptr()
}
/// Creates a new `IntSpan` from an inner pointer to a `meos_sys::Span`.
///
/// # Arguments
/// * `inner` - A pointer to the inner `meos_sys::Span`.
///
/// ## Returns
/// * A new `IntSpan` 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 `i32`.
///
/// ## Example
/// ```
/// # use meos::IntSpan;
/// # use meos::Span;
///
/// let span: IntSpan = (12..67).into();
/// let lower = span.lower();
/// ```
fn lower(&self) -> Self::Type {
unsafe { meos_sys::intspan_lower(self.inner()) }
}
/// Returns the upper bound of the span.
///
/// ## Returns
/// * The upper bound as a `i32`.
///
/// ## Example
/// ```
/// # use meos::IntSpan;
/// # use meos::Span;
///
/// let span: IntSpan = (12..67).into();;
///
/// assert_eq!(span.upper(), 67)
/// ```
fn upper(&self) -> Self::Type {
unsafe { meos_sys::intspan_upper(self.inner()) }
}
/// Return a new `IntSpan` with the lower and upper bounds shifted by `delta`.
///
/// # Arguments
/// * `delta` - The value to shift by.
///
/// # Returns
/// A new `IntSpan` instance.
///
/// # Example
/// ```
/// # use meos::IntSpan;
/// # use meos::Span;
///
/// let span: IntSpan = (12..67).into();
/// let shifted_span = span.shift(5);
///
/// assert_eq!(shifted_span, (17..72).into())
/// ```
fn shift(&self, delta: i32) -> IntSpan {
self.shift_scale(Some(delta), None)
}
/// Return a new `IntSpan` with the lower and upper bounds scaled so that the width is `width`.
///
/// # Arguments
/// * `width` - The new width.
///
/// # Returns
/// A new `IntSpan` instance.
///
/// # Example
/// ```
/// # use meos::IntSpan;
/// # use meos::Span;
///
/// let span: IntSpan = (12..67).into();
/// let scaled_span = span.scale(10);
///
/// assert_eq!(scaled_span, (12..23).into())
/// ```
fn scale(&self, width: i32) -> IntSpan {
self.shift_scale(None, Some(width))
}
/// Return a new `IntSpan` 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 `IntSpan` instance.
///
/// # Example
/// ```
/// # use meos::IntSpan;
/// # use meos::Span;
///
/// let span: IntSpan = (12..67).into();
/// let shifted_scaled_span = span.shift_scale(Some(5), Some(10));
///
/// assert_eq!(shifted_scaled_span, (17..28).into())
/// ```
fn shift_scale(&self, delta: Option<i32>, width: Option<i32>) -> IntSpan {
let d = delta.unwrap_or(0);
let w = width.unwrap_or(0);
let modified = unsafe {
meos_sys::intspan_shift_scale(self.inner(), d, w, delta.is_some(), width.is_some())
};
IntSpan::from_inner(modified)
}
/// Calculates the distance between this `IntSpan` and an int.
///
/// ## Arguments
/// * `value` - An `i32` to calculate the distance to.
///
/// ## Returns
/// An `i32` representing the distance between the span and the value.
///
/// ## Example
/// ```
/// # use meos::IntSpan;
/// # use meos::Span;
///
/// let span: IntSpan = (12..67).into();
/// let distance = span.distance_to_value(&8);
///
/// assert_eq!(distance, 4);
/// ```
fn distance_to_value(&self, value: &i32) -> i32 {
unsafe { meos_sys::distance_span_int(self.inner(), *value) }
}
/// Calculates the distance between this `IntSpan` and another `IntSpan`.
///
/// ## Arguments
/// * `other` - An `IntSpan` to calculate the distance to.
///
/// ## Returns
/// An `i32` representing the distance between the two spans.
///
/// ## Example
/// ```
/// # use meos::IntSpan;
/// # use meos::Span;
///
/// let span1: IntSpan = (12..67).into();
/// let span2: IntSpan = (10..11).into();
/// let distance = span1.distance_to_span(&span2);
///
/// assert_eq!(distance, 2);
/// ```
fn distance_to_span(&self, other: &Self) -> i32 {
unsafe { meos_sys::distance_intspan_intspan(self.inner(), other.inner()) }
}
}
impl NumberSpan for IntSpan {}
impl Clone for IntSpan {
fn clone(&self) -> Self {
unsafe { Self::from_inner(meos_sys::span_copy(self.inner())) }
}
}
impl Hash for IntSpan {
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 IntSpan {
type Err = ParseError;
/// Parses a `IntSpan` from a string representation.
///
/// ## Arguments
/// * `string` - A string slice containing the representation.
///
/// ## Returns
/// * A `IntSpan` instance.
///
/// ## Errors
/// * Returns `ParseSpanError` if the string cannot be parsed.
///
/// ## Example
/// ```
/// # use meos::IntSpan;
/// # use meos::Span;
/// # use std::str::FromStr;
///
/// let span: IntSpan = "(12, 67)".parse().expect("Failed to parse span");
/// assert_eq!(span.lower(), 13);
/// assert_eq!(span.upper(), 67);
/// ```
fn from_str(string: &str) -> Result<Self, Self::Err> {
CString::new(string).map_err(|_| ParseError).map(|string| {
let inner = unsafe { meos_sys::intspan_in(string.as_ptr()) };
Self::from_inner(inner)
})
}
}
impl cmp::PartialEq for IntSpan {
/// Checks if two `IntSpan` instances are equal.
///
/// # Arguments
/// * `other` - Another `IntSpan` instance.
///
/// ## Returns
/// * `true` if the spans are equal, `false` otherwise.
///
/// ## Example
/// ```
/// # use meos::IntSpan;
/// # use meos::Span;
/// # use std::str::FromStr;
///
/// let span1: IntSpan = (12..67).into();
/// let span2: IntSpan = (12..67).into();
/// assert_eq!(span1, span2);
/// ```
fn eq(&self, other: &Self) -> bool {
unsafe { meos_sys::span_eq(self.inner(), other.inner()) }
}
}
impl cmp::Eq for IntSpan {}
impl From<Range<i32>> for IntSpan {
fn from(Range { start, end }: Range<i32>) -> Self {
let inner = unsafe { meos_sys::intspan_make(start, end, true, false) };
Self::from_inner(inner)
}
}
impl From<RangeInclusive<i32>> for IntSpan {
fn from(range: RangeInclusive<i32>) -> Self {
let inner = unsafe { meos_sys::intspan_make(*range.start(), *range.end(), true, true) };
Self::from_inner(inner)
}
}
impl From<RangeInclusive<f32>> for IntSpan {
fn from(range: RangeInclusive<f32>) -> Self {
let inner = unsafe {
meos_sys::intspan_make(*range.start() as i32, *range.end() as i32, true, true)
};
Self::from_inner(inner)
}
}
impl Debug for IntSpan {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let out_str = unsafe { meos_sys::intspan_out(self.inner()) };
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 IntSpan
impl BitAnd for IntSpan {
type Output = Option<IntSpan>;
/// Computes the intersection of two `IntSpan` instances.
///
/// # Arguments
/// * `other` - Another `IntSpan` instance.
///
/// ## Returns
/// * An `Option<IntSpan>` containing the intersection, or `None` if there is no intersection.
///
/// ## Example
/// ```
/// # use meos::IntSpan;
/// # use meos::Span;
/// # use std::str::FromStr;
///
/// let span1: IntSpan = (12..67).into();
/// let span2: IntSpan = (50..90).into();
/// let intersection = (span1 & span2).unwrap();
///
/// assert_eq!(intersection, (50..67).into())
/// ```
fn bitand(self, other: Self) -> Self::Output {
self.intersection(&other)
}
}
impl PartialOrd for IntSpan {
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 IntSpan {
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",
)
}
}