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
use std::ffi::{c_void, CStr, CString};
use std::fmt::Debug;
use std::hash::Hash;
use std::ops::{BitAnd, BitOr};
use std::ptr;
use crate::collections::base::SpanSet;
use crate::collections::base::{Collection, Span, impl_collection, impl_iterator};
use crate::errors::ParseError;
use super::int_span::IntSpan;
use super::number_span_set::NumberSpanSet;
pub struct IntSpanSet {
_inner: ptr::NonNull<meos_sys::SpanSet>,
}
impl Drop for IntSpanSet {
fn drop(&mut self) {
unsafe {
libc::free(self._inner.as_ptr().cast::<c_void>());
}
}
}
impl Collection for IntSpanSet {
impl_collection!(spanset, i32);
fn contains(&self, content: &i32) -> bool {
unsafe { meos_sys::contains_spanset_int(self.inner(), *content) }
}
}
impl SpanSet for IntSpanSet {
type SpanType = IntSpan;
type SubsetType = <Self as Collection>::Type;
fn inner(&self) -> *const meos_sys::SpanSet {
self._inner.as_ptr()
}
fn from_inner(inner: *mut meos_sys::SpanSet) -> Self {
Self {
_inner: ptr::NonNull::new(inner).expect("Null pointers not allowed"),
}
}
fn width(&self, ignore_gaps: bool) -> Self::Type {
unsafe { meos_sys::intspanset_width(self.inner(), ignore_gaps) }
}
/// Return a new `IntSpanSet` with the lower and upper bounds shifted by `delta`.
///
/// ## Arguments
/// * `delta` - The value to shift by.
///
/// ## Returns
/// A new `IntSpanSet` instance.
///
/// ## Example
/// ```
/// # use meos::IntSpanSet;
/// # use std::str::FromStr;
/// # use meos::SpanSet;
///
/// let span = IntSpanSet::from_str("{[17, 18), [19, 20)}").unwrap();
/// let shifted_span = span.shift(5);
///
/// let expected_shifted_span =
/// IntSpanSet::from_str("{[22, 23), [24, 25)}").unwrap();
/// assert_eq!(shifted_span, expected_shifted_span);
/// ```
fn shift(&self, delta: i32) -> IntSpanSet {
self.shift_scale(Some(delta), None)
}
/// Return a new `IntSpanSet` with the lower and upper bounds scaled so that the width is `width`.
///
/// ## Arguments
/// * `width` - The new width.
///
/// ## Returns
/// A new `IntSpanSet` instance.
///
/// ## Example
/// ```
/// # use meos::IntSpanSet;
/// # use std::str::FromStr;
/// # use meos::SpanSet;
///
/// let span = IntSpanSet::from_str("{[17, 18), [19, 23)}").unwrap();
/// let scaled_span = span.scale(5);
///
/// let expected_scaled_span =
/// IntSpanSet::from_str("{[17, 18), [19, 23)}").unwrap();
/// assert_eq!(scaled_span, expected_scaled_span);
/// ```
fn scale(&self, width: i32) -> IntSpanSet {
self.shift_scale(None, Some(width))
}
/// Return a new `IntSpanSet` 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 `IntSpanSet` instance.
///
/// ## Example
/// ```
/// # use meos::IntSpanSet;
/// # use std::str::FromStr;
/// # use meos::SpanSet;
///
/// let span = IntSpanSet::from_str("{[17, 18), [19, 20)}").unwrap();
/// let shifted_scaled_span = span.shift_scale(Some(5), Some(2));
///
/// let expected_shifted_scaled_span =
/// IntSpanSet::from_str("{[22, 23), [24, 25)}").unwrap();
/// assert_eq!(shifted_scaled_span, expected_shifted_scaled_span);
/// ```
fn shift_scale(&self, delta: Option<i32>, width: Option<i32>) -> IntSpanSet {
let d = delta.unwrap_or(0);
let w = width.unwrap_or(0);
let modified = unsafe {
meos_sys::intspanset_shift_scale(self.inner(), d, w, delta.is_some(), width.is_some())
};
IntSpanSet::from_inner(modified)
}
/// Calculates the distance between this `IntSpanSet` and an integer (`value`).
///
/// ## Arguments
/// * `value` - An i32 to calculate the distance to.
///
/// ## Returns
/// An `i32` representing the distance between the span set and the value.
///
/// ## Example
/// ```
/// # use meos::IntSpanSet;
/// # use meos::SpanSet;
/// let span_set: IntSpanSet = [(2019..2023).into(), (2029..2030).into()].iter().collect();
/// let distance = span_set.distance_to_value(&2032);
/// assert_eq!(distance, 3);
/// ```
fn distance_to_value(&self, value: &Self::Type) -> i32 {
unsafe { meos_sys::distance_spanset_int(self.inner(), *value) }
}
/// Calculates the distance between this `IntSpanSet` and another `IntSpanSet`.
///
/// ## Arguments
/// * `other` - An `IntSpanSet` to calculate the distance to.
///
/// ## Returns
/// An `i32` representing the distance between the two spansets.
///
/// ## Example
/// ```
/// # use meos::IntSpanSet;
/// # use meos::SpanSet;
/// # use meos::Span;
///
/// let span_set1: IntSpanSet = [(2019..2023).into(), (2029..2030).into()].iter().collect();
/// let span_set2: IntSpanSet = [(2049..2050).into(), (2059..2600).into()].iter().collect();
/// let distance = span_set1.distance_to_span_set(&span_set2);
///
/// assert_eq!(distance, 20);
/// ```
fn distance_to_span_set(&self, other: &Self) -> i32 {
unsafe { meos_sys::distance_intspanset_intspanset(self.inner(), other.inner()) }
}
/// Calculates the distance between this `IntSpanSet` and a `IntSpan`.
///
/// ## Arguments
/// * `other` - A `IntSpan` to calculate the distance to.
///
/// ## Returns
/// A `TimeDelta` representing the distance in seconds between the span set and the span.
///
/// ## Example
/// ```
/// # use meos::IntSpanSet;
/// # use meos::SpanSet;
/// # use meos::Span;
/// # use meos::IntSpan;
///
/// let span_set: IntSpanSet = [(2019..2023).into(), (2029..2030).into()].iter().collect();
/// let span: IntSpan = (2009..2010).into();
/// let distance = span_set.distance_to_span(&span);
/// assert_eq!(distance, 10);
/// ```
fn distance_to_span(&self, span: &Self::SpanType) -> Self::SubsetType {
unsafe { meos_sys::distance_intspanset_intspan(self.inner(), span.inner()) }
}
}
impl NumberSpanSet for IntSpanSet {}
impl Clone for IntSpanSet {
fn clone(&self) -> IntSpanSet {
self.copy()
}
}
impl_iterator!(IntSpanSet);
impl Hash for IntSpanSet {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
let hash = unsafe { meos_sys::spanset_hash(self.inner()) };
state.write_u32(hash);
let _ = state.finish();
}
}
impl std::str::FromStr for IntSpanSet {
type Err = ParseError;
fn from_str(string: &str) -> Result<Self, Self::Err> {
CString::new(string).map_err(|_| ParseError).map(|string| {
let inner = unsafe { meos_sys::intspanset_in(string.as_ptr()) };
Self::from_inner(inner)
})
}
}
impl std::cmp::PartialEq for IntSpanSet {
fn eq(&self, other: &Self) -> bool {
unsafe { meos_sys::spanset_eq(self.inner(), other.inner()) }
}
}
impl Debug for IntSpanSet {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let out_str = unsafe { meos_sys::intspanset_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
}
}
impl BitAnd<IntSpanSet> for IntSpanSet {
type Output = Option<IntSpanSet>;
/// Computes the intersection of two `IntSpanSet`s.
///
/// ## Arguments
///
/// * `other` - Another `IntSpanSet` to intersect with.
///
/// ## Returns
///
/// * `Some(IntSpanSet)` - A new `IntSpanSet` containing the intersection, if it exists.
/// * `None` - If the intersection is empty.
///
/// ## Example
///
/// ```
/// # use meos::IntSpanSet;
/// # use std::str::FromStr;
/// # use meos::SpanSet;
///
/// let span_set1 = IntSpanSet::from_str("{[17, 18), [19, 20)}").unwrap();
/// let span_set2 = IntSpanSet::from_str("{[19, 23), [45, 67)}").unwrap();
///
/// let expected_result = IntSpanSet::from_str("{[19, 20)}").unwrap();
/// assert_eq!((span_set1 & span_set2).unwrap(), expected_result);
/// ```
fn bitand(self, other: IntSpanSet) -> Self::Output {
self.intersection(&other)
}
}
impl BitOr for IntSpanSet {
type Output = Option<IntSpanSet>;
/// Computes the union of two `IntSpanSet`s.
///
/// ## Arguments
///
/// * `other` - Another `IntSpanSet` to union with.
///
/// ## Returns
///
/// * `Some(IntSpanSet)` - A new `IntSpanSet` containing the union.
/// * `None` - If the union is empty.
///
/// ## Example
///
/// ```
/// # use meos::IntSpanSet;
/// # use std::str::FromStr;
/// # use meos::SpanSet;
///
/// let span_set1 = IntSpanSet::from_str("{[17, 18), [19, 20)}").unwrap();
/// let span_set2 = IntSpanSet::from_str("{[19, 23), [45, 67)}").unwrap();
///
/// let expected_result = IntSpanSet::from_str("{[17, 18), [19, 23), [45, 67)}").unwrap();
/// assert_eq!((span_set1 | span_set2).unwrap(), expected_result)
/// ```
fn bitor(self, other: Self) -> Self::Output {
self.union(&other)
}
}