meos/temporal/
temporal.rs

1use std::{
2    ffi::{CStr, CString},
3    hash::Hash,
4    ptr,
5};
6
7use crate::{
8    collections::{
9        base::*,
10        datetime::{TsTzSpan, TsTzSpanSet},
11    },
12    factory,
13    utils::{create_interval, from_interval, from_meos_timestamp, to_meos_timestamp},
14    BoundingBox, MeosEnum,
15};
16use chrono::{DateTime, TimeDelta, TimeZone, Utc};
17
18use super::{
19    interpolation::TInterpolation, tbool::TBoolTrait, tinstant::TInstant, tsequence::TSequence,
20    tsequence_set::TSequenceSet,
21};
22
23pub trait Temporal: Collection + Hash {
24    type TI: TInstant;
25    type TS: TSequence;
26    type TSS: TSequenceSet;
27    type TBB: BoundingBox;
28    type Enum: MeosEnum;
29    type TBoolType: TBoolTrait;
30
31    fn from_inner_as_temporal(inner: *mut meos_sys::Temporal) -> Self;
32
33    fn inner(&self) -> *const meos_sys::Temporal;
34
35    /// Returns the bounding box of the temporal object.
36    ///
37    /// ## Returns
38    /// The bounding box of the temporal object.
39    fn bounding_box(&self) -> Self::TBB;
40
41    /// Returns the interpolation method of the temporal object.
42    ///
43    /// ## Returns
44    /// The interpolation method.
45    fn interpolation(&self) -> TInterpolation {
46        let string = unsafe { CStr::from_ptr(meos_sys::temporal_interp(self.inner())) };
47        string.to_str().unwrap().parse().unwrap()
48    }
49
50    /// Returns the set of unique values in the temporal object.
51    ///
52    /// ## Returns
53    /// A set of unique values.
54    // fn value_set(&self) -> HashSet<Self::Type>;
55
56    /// Returns the list of values taken by the temporal object.
57    ///
58    /// ## Returns
59    /// A list of values.
60    fn values(&self) -> Vec<Self::Type>;
61
62    /// Returns the starting value of the temporal object.
63    ///
64    /// ## Returns
65    /// The starting value.
66    fn start_value(&self) -> Self::Type;
67
68    /// Returns the ending value of the temporal object.
69    ///
70    /// ## Returns
71    /// The ending value.
72    fn end_value(&self) -> Self::Type;
73
74    /// Returns the value of the temporal object at a specific timestamp.
75    ///
76    /// ## Arguments
77    /// * `timestamp` - The timestamp.
78    ///
79    /// ## Returns
80    /// The value at the given timestamp.
81    fn value_at_timestamp<Tz: TimeZone>(&self, timestamp: DateTime<Tz>) -> Option<Self::Type>;
82
83    /// Returns the time span on which the temporal object is defined.
84    ///
85    /// ## Returns
86    /// The time span.
87    #[doc(alias = "temporal_time")]
88    fn time(&self) -> TsTzSpanSet {
89        TsTzSpanSet::from_inner(unsafe { meos_sys::temporal_time(self.inner()) })
90    }
91
92    /// Returns the time span on which the temporal object is defined.
93    ///
94    /// ## Returns
95    /// The time span.
96    #[doc(alias = "temporal_to_tstzspan")]
97    fn timespan(&self) -> TsTzSpan {
98        unsafe { TsTzSpan::from_inner(meos_sys::temporal_to_tstzspan(self.inner())) }
99    }
100
101    /// Returns the duration of the temporal object.
102    ///
103    /// ## Arguments
104    /// * `ignore_gaps` - Whether to ignore gaps in the temporal value.
105    ///
106    /// ## Returns
107    /// The duration of the temporal object.
108    #[doc(alias = "temporal_duration")]
109    fn duration(&self, ignore_gaps: bool) -> TimeDelta {
110        from_interval(unsafe { meos_sys::temporal_duration(self.inner(), ignore_gaps).read() })
111    }
112
113    /// Returns the number of instants in the temporal object.
114    ///
115    /// ## Returns
116    /// The number of instants.
117    #[doc(alias = "temporal_num_instants")]
118    fn num_instants(&self) -> i32 {
119        unsafe { meos_sys::temporal_num_instants(self.inner()) }
120    }
121
122    /// Returns the first instant in the temporal object.
123    ///
124    /// ## Returns
125    /// The first instant.
126    #[doc(alias = "temporal_start_instant")]
127    fn start_instant(&self) -> Self::TI {
128        <Self::TI as TInstant>::from_inner(unsafe {
129            meos_sys::temporal_start_instant(self.inner())
130        })
131    }
132
133    /// Returns the last instant in the temporal object.
134    ///
135    /// ## Returns
136    /// The last instant.
137    #[doc(alias = "temporal_end_instant")]
138    fn end_instant(&self) -> Self::TI {
139        <Self::TI as TInstant>::from_inner(unsafe { meos_sys::temporal_end_instant(self.inner()) })
140    }
141
142    /// Returns the instant with the minimum value in the temporal object.
143    ///
144    /// ## Returns
145    /// The instant with the minimum value.
146    #[doc(alias = "temporal_min_instant")]
147    fn min_instant(&self) -> Self::TI {
148        <Self::TI as TInstant>::from_inner(unsafe { meos_sys::temporal_min_instant(self.inner()) })
149    }
150
151    /// Returns the instant with the maximum value in the temporal object.
152    ///
153    /// ## Returns
154    /// The instant with the maximum value.
155    #[doc(alias = "temporal_max_instant")]
156    fn max_instant(&self) -> Self::TI {
157        <Self::TI as TInstant>::from_inner(unsafe { meos_sys::temporal_max_instant(self.inner()) })
158    }
159
160    /// Returns the n-th instant in the temporal object.
161    ///
162    /// ## Arguments
163    /// * `n` - The index (0-based).
164    ///
165    /// ## Return
166    /// The n-th instant if exists, None otherwise.
167    #[doc(alias = "temporal_instant_n")]
168    fn instant_n(&self, n: i32) -> Option<Self::TI> {
169        let result = unsafe { meos_sys::temporal_instant_n(self.inner(), n) };
170        if !result.is_null() {
171            Some(<Self::TI as TInstant>::from_inner(result))
172        } else {
173            None
174        }
175    }
176
177    /// Returns the list of instants in the temporal object.
178    ///
179    /// ## Returns
180    /// A list of instants.
181    #[doc(alias = "temporal_instants")]
182    fn instants(&self) -> Vec<Self::TI> {
183        let mut count = 0;
184        unsafe {
185            let instants = meos_sys::temporal_instants(self.inner(), ptr::addr_of_mut!(count));
186
187            Vec::from_raw_parts(instants, count as usize, count as usize)
188                .iter()
189                .map(|&instant| <Self::TI as TInstant>::from_inner(instant))
190                .collect()
191        }
192    }
193
194    /// Returns the number of timestamps in the temporal object.
195    ///
196    /// ## Returns
197    /// The number of timestamps.
198    #[doc(alias = "temporal_num_timestamps")]
199    fn num_timestamps(&self) -> i32 {
200        unsafe { meos_sys::temporal_num_timestamps(self.inner()) }
201    }
202
203    /// Returns the first timestamp in the temporal object.
204    ///
205    /// ## Returns
206    /// The first timestamp.
207    #[doc(alias = "temporal_start_timestampz")]
208    fn start_timestamp(&self) -> DateTime<Utc> {
209        from_meos_timestamp(unsafe { meos_sys::temporal_start_timestamptz(self.inner()) })
210    }
211
212    /// Returns the last timestamp in the temporal object.
213    ///
214    /// ## Returns
215    /// The last timestamp.
216    #[doc(alias = "temporal_end_timestampz")]
217    fn end_timestamp(&self) -> DateTime<Utc> {
218        from_meos_timestamp(unsafe { meos_sys::temporal_end_timestamptz(self.inner()) })
219    }
220
221    /// Returns the n-th timestamp in the temporal object.
222    ///
223    /// ## Arguments
224    /// * `n` - The index (0-based).
225    ///
226    /// ## Returns
227    /// The n-th timestamp if exists, None otherwise.
228    #[doc(alias = "temporal_timestampz_n")]
229    fn timestamp_n(&self, n: i32) -> Option<DateTime<Utc>> {
230        let mut timestamp = 0;
231        unsafe {
232            let success =
233                meos_sys::temporal_timestamptz_n(self.inner(), n, ptr::addr_of_mut!(timestamp));
234            if success {
235                Some(from_meos_timestamp(timestamp))
236            } else {
237                None
238            }
239        }
240    }
241
242    /// Returns the list of timestamps in the temporal object.
243    ///
244    /// ## Returns
245    /// A list of timestamps.
246    #[doc(alias = "temporal_timestamps")]
247    fn timestamps(&self) -> Vec<DateTime<Utc>> {
248        let mut count = 0;
249        let timestamps =
250            unsafe { meos_sys::temporal_timestamps(self.inner(), ptr::addr_of_mut!(count)) };
251        unsafe {
252            Vec::from_raw_parts(timestamps, count as usize, count as usize)
253                .iter()
254                .map(|&timestamp| from_meos_timestamp(timestamp))
255                .collect()
256        }
257    }
258
259    /// Returns the list of segments in the temporal object.
260    ///
261    /// ## Returns
262    /// A list of segments.
263    #[doc(alias = "temporal_segments")]
264    fn segments(&self) -> Vec<Self::TS> {
265        let mut count = 0;
266        let segments =
267            unsafe { meos_sys::temporal_segments(self.inner(), ptr::addr_of_mut!(count)) };
268        unsafe {
269            Vec::from_raw_parts(segments, count as usize, count as usize)
270                .iter()
271                .map(|&segment| <Self::TS as TSequence>::from_inner(segment))
272                .collect()
273        }
274    }
275
276    // ------------------------- Transformations -------------------------------
277
278    /// Returns a new `Temporal` object with the given interpolation.
279    #[doc(alias = "temporal_set_interp")]
280    fn set_interpolation(&self, interpolation: TInterpolation) -> Self {
281        Self::from_inner_as_temporal(unsafe {
282            meos_sys::temporal_set_interp(self.inner(), interpolation as u32)
283        })
284    }
285
286    /// Returns a new `Temporal` with the temporal dimension shifted by `delta`.
287    ///
288    /// ## Arguments
289    /// * `delta` - TimeDelta to shift the temporal dimension.
290    #[doc(alias = "temporal_shift_scale_time")]
291    fn shift_time(&self, delta: TimeDelta) -> Self {
292        self.shift_scale_time(Some(delta), None)
293    }
294
295    /// Returns a new `Temporal` scaled so the temporal dimension has duration `duration`.
296    ///
297    /// ## Arguments
298    /// * `duration` - TimeDelta representing the new temporal duration.
299    #[doc(alias = "temporal_shift_scale_time")]
300    fn scale_time(&self, duration: TimeDelta) -> Self {
301        self.shift_scale_time(None, Some(duration))
302    }
303
304    /// Returns a new `Temporal` with the time dimension shifted and scaled.
305    ///
306    /// ## Arguments
307    /// * `shift` - TimeDelta to shift the time dimension.
308    /// * `duration` - TimeDelta representing the new temporal duration.
309    #[doc(alias = "temporal_shift_scale_time")]
310    fn shift_scale_time(&self, shift: Option<TimeDelta>, duration: Option<TimeDelta>) -> Self {
311        let d = {
312            if let Some(d) = shift {
313                &*Box::new(create_interval(d)) as *const meos_sys::Interval
314            } else {
315                std::ptr::null()
316            }
317        };
318
319        let w = {
320            if let Some(w) = duration {
321                &*Box::new(create_interval(w)) as *const meos_sys::Interval
322            } else {
323                std::ptr::null()
324            }
325        };
326
327        let modified = unsafe { meos_sys::temporal_shift_scale_time(self.inner(), d, w) };
328        Self::from_inner_as_temporal(modified)
329    }
330
331    /// Returns a new `Temporal` downsampled with respect to `duration`.
332    ///
333    /// ## Arguments
334    /// * `duration` - TimeDelta of the temporal tiles.
335    /// * `start` - Start time of the temporal tiles.
336    /// * `interpolation`- Interpolation of the resulting temporal object.
337    #[doc(alias = "temporal_tsample")]
338    fn temporal_sample<Tz: TimeZone>(
339        self,
340        duration: TimeDelta,
341        start: DateTime<Tz>,
342        interpolation: TInterpolation,
343    ) -> Self {
344        let interval = create_interval(duration);
345        Self::from_inner_as_temporal(unsafe {
346            meos_sys::temporal_tsample(
347                self.inner(),
348                ptr::addr_of!(interval),
349                to_meos_timestamp(&start),
350                interpolation as u32,
351            )
352        })
353    }
354
355    /// Returns a new `Temporal` with precision reduced to `duration`.
356    ///
357    /// ## Arguments
358    /// * `duration` - TimeDelta of the temporal tiles.
359    /// * `start` - Start time of the temporal tiles.
360    #[doc(alias = "temporal_tprecision")]
361    fn temporal_precision<Tz: TimeZone>(self, duration: TimeDelta, start: DateTime<Tz>) -> Self {
362        let interval = create_interval(duration);
363        Self::from_inner_as_temporal(unsafe {
364            meos_sys::temporal_tprecision(
365                self.inner(),
366                ptr::addr_of!(interval),
367                to_meos_timestamp(&start),
368            )
369        })
370    }
371
372    /// Converts `self` into a `TInstant`.
373    #[doc(alias = "temporal_to_instant")]
374    fn to_instant(&self) -> Self::TI {
375        TInstant::from_inner(unsafe { meos_sys::temporal_to_tinstant(self.inner()) })
376    }
377
378    /// Converts `self` into a `TSequence`.
379    ///
380    /// ## Arguments
381    /// * `interpolation` - The interpolation type for the sequence.
382    #[doc(alias = "temporal_to_tsequence")]
383    fn to_sequence(&self, interpolation: TInterpolation) -> Self::TS {
384        let c_str = CString::new(interpolation.to_string()).unwrap();
385        TSequence::from_inner(unsafe {
386            meos_sys::temporal_to_tsequence(self.inner(), c_str.as_ptr())
387        })
388    }
389
390    /// Converts `self` into a `TSequenceSet`.
391    ///
392    /// ## Arguments
393    /// * `interpolation` - The interpolation type for the sequence set.
394    #[doc(alias = "temporal_to_tsequenceset")]
395    fn to_sequence_set(&self, interpolation: TInterpolation) -> Self::TSS {
396        let c_str = CString::new(interpolation.to_string()).unwrap();
397        TSequenceSet::from_inner(unsafe {
398            meos_sys::temporal_to_tsequenceset(self.inner(), c_str.as_ptr())
399        })
400    }
401
402    // ------------------------- Modifications ---------------------------------
403
404    /// Appends `instant` to `self`.
405    ///
406    /// ## Arguments
407    /// * `instant` - Instant to append.
408    /// * `max_dist` - Maximum distance for defining a gap.
409    /// * `max_time` - Maximum time for defining a gap.
410    #[doc(alias = "temporal_append_tinstant")]
411    fn append_instant(
412        self,
413        instant: Self::TI,
414        max_dist: Option<f64>,
415        max_time: Option<TimeDelta>,
416    ) -> Self::Enum {
417        let td = create_interval(max_time.unwrap_or_default());
418        let max_time_ptr = if max_time.is_some() {
419            ptr::addr_of!(td)
420        } else {
421            ptr::null()
422        };
423        factory::<Self::Enum>(unsafe {
424            meos_sys::temporal_append_tinstant(
425                self.inner() as *mut _,
426                instant.inner_as_tinstant(),
427                max_dist.unwrap_or_default(),
428                max_time_ptr,
429                false,
430            )
431        })
432    }
433
434    /// Appends `sequence` to `self`.
435    ///
436    /// ## Arguments
437    /// * `sequence` - Sequence to append.
438    #[doc(alias = "temporal_append_tsequence")]
439    fn append_sequence(&self, sequence: Self::TS) -> Self::Enum {
440        factory::<Self::Enum>(unsafe {
441            meos_sys::temporal_append_tsequence(
442                self.inner() as *mut _,
443                sequence.inner_as_tsequence(),
444                false,
445            )
446        })
447    }
448
449    /// Merges `self` with `other`.
450    ///
451    /// ## Arguments
452    /// * `other` - Another temporal object
453    #[doc(alias = "temporal_merge")]
454    fn merge_other(&self, other: Self::Enum) -> Self::Enum {
455        factory::<Self::Enum>(unsafe { meos_sys::temporal_merge(self.inner(), other.inner()) })
456    }
457
458    /// Inserts `other` into `self`.
459    ///
460    /// ## Arguments
461    /// * `other` - Temporal object to insert.
462    /// * `connect` - Whether to connect inserted elements with existing ones.
463    #[doc(alias = "temporal_insert")]
464    fn insert(&self, other: Self::Enum, connect: bool) -> Self::Enum {
465        factory::<Self::Enum>(unsafe {
466            meos_sys::temporal_insert(self.inner(), other.inner(), connect)
467        })
468    }
469
470    /// Updates `self` with `other`.
471    ///
472    /// ## Arguments
473    /// * `other` - Temporal object to update with.
474    /// * `connect` - Whether to connect updated elements with existing ones.
475    #[doc(alias = "temporal_update")]
476    fn update(&self, other: Self::Enum, connect: bool) -> Self::Enum {
477        factory::<Self::Enum>(unsafe {
478            meos_sys::temporal_update(self.inner(), other.inner(), connect)
479        })
480    }
481
482    /// Deletes elements from `self` at `other`.
483    ///
484    /// ## Arguments
485    /// * `other` - Time object specifying the elements to delete.
486    /// * `connect` - Whether to connect the potential gaps generated by the deletions.
487    #[doc(alias = "temporal_delete_timestamptz")]
488    fn delete_at_timestamp<Tz: TimeZone>(&self, other: DateTime<Tz>, connect: bool) -> Self::Enum {
489        factory::<Self::Enum>(unsafe {
490            meos_sys::temporal_delete_timestamptz(self.inner(), to_meos_timestamp(&other), connect)
491        })
492    }
493
494    /// Deletes elements from `self` at `time_span`.
495    ///
496    /// ## Arguments
497    /// * `time_span` - Time span object specifying the elements to delete.
498    /// * `connect` - Whether to connect the potential gaps generated by the deletions.
499    #[doc(alias = "temporal_delete_tstzspan")]
500    fn delete_at_tstz_span(&self, time_span: TsTzSpan, connect: bool) -> Self::Enum {
501        factory::<Self::Enum>(unsafe {
502            meos_sys::temporal_delete_tstzspan(self.inner(), time_span.inner(), connect)
503        })
504    }
505
506    /// Deletes elements from `self` at `time_span_set`.
507    ///
508    /// ## Arguments
509    /// * `time_span_set` - Time span set object specifying the elements to delete.
510    /// * `connect` - Whether to connect the potential gaps generated by the deletions.
511    #[doc(alias = "temporal_delete_tstzspanset")]
512    fn delete_at_tstz_span_set(&self, time_span_set: TsTzSpanSet, connect: bool) -> Self::Enum {
513        factory::<Self::Enum>(unsafe {
514            meos_sys::temporal_delete_tstzspanset(self.inner(), time_span_set.inner(), connect)
515        })
516    }
517
518    // ------------------------- Restrictions ----------------------------------
519
520    /// Returns a new temporal object with values restricted to the time `other`.
521    ///
522    /// ## Arguments
523    /// * `other` - A timestamp to restrict the values to.
524    #[doc(alias = "temporal_at_timestamptz")]
525    fn at_timestamp<Tz: TimeZone>(&self, other: DateTime<Tz>) -> Self::TI {
526        <Self::TI as Temporal>::from_inner_as_temporal(unsafe {
527            meos_sys::temporal_at_timestamptz(self.inner(), to_meos_timestamp(&other))
528        })
529    }
530
531    /// Returns a new temporal object with values restricted to the time `time_span`.
532    ///
533    /// ## Arguments
534    /// * `time_span` - A time span to restrict the values to.
535    #[doc(alias = "temporal_at_tstzspan")]
536    fn at_tstz_span(&self, time_span: TsTzSpan) -> Self {
537        Self::from_inner_as_temporal(unsafe {
538            meos_sys::temporal_at_tstzspan(self.inner(), time_span.inner())
539        })
540    }
541
542    /// Returns a new temporal object with values restricted to the time `time_span_set`.
543    ///
544    /// ## Arguments
545    /// * `time_span_set` - A time span set to restrict the values to.
546    #[doc(alias = "temporal_at_tstzspanset")]
547    fn at_tstz_span_set(&self, time_span_set: TsTzSpanSet) -> Self {
548        Self::from_inner_as_temporal(unsafe {
549            meos_sys::temporal_at_tstzspanset(self.inner(), time_span_set.inner())
550        })
551    }
552
553    /// Returns a new temporal object containing the times `self` is at `value`.
554    fn at_value(&self, value: &Self::Type) -> Option<Self::Enum>;
555
556    /// Returns a new temporal object containing the times `self` is in any of the values of `values`.
557    fn at_values(&self, values: &[Self::Type]) -> Option<Self::Enum>;
558
559    /// Returns a new temporal object with values at `timestamp` removed.
560    ///
561    /// ## Arguments
562    /// * `timestamp` - A timestamp specifying the values to remove.
563    #[doc(alias = "temporal_minus_timestampz")]
564    fn minus_timestamp<Tz: TimeZone>(&self, timestamp: DateTime<Tz>) -> Self::Enum {
565        factory::<Self::Enum>(unsafe {
566            meos_sys::temporal_minus_timestamptz(self.inner(), to_meos_timestamp(&timestamp))
567        })
568    }
569
570    /// Returns a new temporal object with values at any of the values of `timestamps` removed.
571    ///
572    /// ## Arguments
573    /// * `timestamps` - A timestamp specifying the values to remove.
574    #[doc(alias = "temporal_minus_tstzset")]
575    fn minus_timestamp_set<Tz: TimeZone>(&self, timestamps: &[DateTime<Tz>]) -> Self::Enum {
576        let timestamps: Vec<_> = timestamps.iter().map(to_meos_timestamp).collect();
577        let set = unsafe { meos_sys::tstzset_make(timestamps.as_ptr(), timestamps.len() as i32) };
578        factory::<Self::Enum>(unsafe { meos_sys::temporal_minus_tstzset(self.inner(), set) })
579    }
580
581    /// Returns a new temporal object with values at `time_span` removed.
582    ///
583    /// ## Arguments
584    /// * `time_span` - A time span specifying the values to remove.
585    #[doc(alias = "temporal_minus_tstzspan")]
586    fn minus_tstz_span(&self, time_span: TsTzSpan) -> Self::Enum {
587        factory::<Self::Enum>(unsafe {
588            meos_sys::temporal_minus_tstzspan(self.inner(), time_span.inner())
589        })
590    }
591
592    /// Returns a new temporal object with values at `time_span_set` removed.
593    ///
594    /// ## Arguments
595    /// * `time_span_set` - A time span set specifying the values to remove.
596    #[doc(alias = "temporal_minus_tstzspanset")]
597    fn minus_tstz_span_set(&self, time_span_set: TsTzSpanSet) -> Self::Enum {
598        factory::<Self::Enum>(unsafe {
599            meos_sys::temporal_minus_tstzspanset(self.inner(), time_span_set.inner())
600        })
601    }
602
603    /// Returns a new temporal object containing the times `self` is not at `value`.
604    fn minus_value(&self, value: Self::Type) -> Self::Enum;
605
606    /// Returns a new temporal object containing the times `self` is not at `values`.
607    fn minus_values(&self, values: &[Self::Type]) -> Self::Enum;
608
609    // ------------------------- Topological Operations ------------------------
610
611    /// Returns a `TBool` representing whether the bounding box of `self` is adjacent to the bounding box of `other` accross time.
612    ///
613    /// ## Arguments
614    /// * `other` - A time or temporal object to compare.
615    ///
616    /// See also:
617    ///     `Collection.is_adjacent`
618    #[doc(alias = "adjacent_temporal_temporal")]
619    fn is_adjacent(&self, other: Self::Enum) -> bool {
620        unsafe { meos_sys::adjacent_temporal_temporal(self.inner(), other.inner()) }
621    }
622
623    /// Returns a `TBool` representing whether the bounding timespan of `self` is temporally adjacent to the bounding timespan of `other` accross time.
624    ///
625    /// ## Arguments
626    /// * `other` - A time or temporal object to compare.
627    ///
628    /// See also:
629    ///     `Collection.is_adjacent`
630    fn is_temporally_adjacent(&self, other: Self) -> bool {
631        self.timespan().is_adjacent(&other.timespan())
632    }
633
634    /// Returns a `TBool` representing whether the bounding-box of `self` is contained in the bounding-box of `container` accross time.
635    ///
636    /// ## Arguments
637    /// * `container` - A time or temporal object to compare.
638    ///
639    /// See also:
640    ///     `Collection.is_contained_in`
641    #[doc(alias = "contained_temporal_temporal")]
642    fn is_contained_in(&self, other: Self::Enum) -> bool {
643        unsafe { meos_sys::contained_temporal_temporal(self.inner(), other.inner()) }
644    }
645
646    /// Returns a `TBool` representing whether the bounding timespan of `self` is contained in the bounding timespan of `container` accross time.
647    ///
648    /// ## Arguments
649    /// * `container` - A time or temporal object to compare.
650    ///
651    /// See also:
652    ///     `Collection.is_contained_in`
653    fn is_temporally_contained_in(&self, other: Self) -> bool {
654        self.timespan().is_contained_in(&other.timespan())
655    }
656
657    /// Returns a `TBool` representing whether the bounding timespan of `self` contains the bounding timespan of `other` accross time.
658    ///
659    /// ## Arguments
660    /// * `other` - A time or temporal object to compare.
661    #[doc(alias = "contains_temporal_temporal")]
662    fn contains(&self, other: Self::Enum) -> bool {
663        unsafe { meos_sys::contains_temporal_temporal(self.inner(), other.inner()) }
664    }
665
666    /// Returns a `TBool` representing whether the bounding timespan of `self` temporally contains the bounding timespan of `other` accross time.
667    ///
668    /// ## Arguments
669    /// * `other` - A time or temporal object to compare.
670    fn temporally_contains(&self, other: Self) -> bool {
671        other.timespan().is_contained_in(&self.timespan())
672    }
673
674    /// Returns a `TBool` representing whether the bounding timespan of `self` overlaps with the bounding timespan of `other` accross time.
675    ///
676    /// ## Arguments
677    /// * `other` - A time or temporal object to compare.
678    ///
679    /// See also:
680    ///     `Collection.overlaps`
681    #[doc(alias = "overlaps_temporal_temporal")]
682    fn overlaps(&self, other: Self) -> bool {
683        unsafe { meos_sys::overlaps_temporal_temporal(self.inner(), other.inner()) }
684    }
685
686    /// Returns a `TBool` representing whether the bounding timespan of `self` temporally overlaps with the bounding timespan of `other` accross time.
687    ///
688    /// ## Arguments
689    /// * `other` - A time or temporal object to compare.
690    ///
691    /// See also:
692    ///     `TsTzSpan.overlaps`
693    fn temporally_overlaps(&self, other: Self) -> bool {
694        self.timespan().overlaps(&other.timespan())
695    }
696
697    // ------------------------- Position Operations ---------------------------
698    /// Returns whether `self` is before `other`.
699    ///
700    /// ## Arguments
701    /// * `other` - A time or temporal object to compare.
702    ///
703    /// ## Returns
704    /// True if `self` is before `other`, False otherwise.
705    ///
706    /// See also:
707    ///     `TsTzSpan.is_left`
708    #[doc(alias = "before_temporal_temporal")]
709    fn is_before(&self, other: Self::Enum) -> bool {
710        unsafe { meos_sys::before_temporal_temporal(self.inner(), other.inner()) }
711    }
712
713    /// Returns whether `self` is before `other` allowing overlap.
714    ///
715    /// ## Arguments
716    /// * `other` - A time or temporal object to compare.
717    ///
718    /// ## Returns
719    /// True if `self` is before `other` allowing overlap, False otherwise.
720    ///
721    /// See also:
722    ///     `TsTzSpan.is_over_or_left`
723    #[doc(alias = "overbefore_temporal_temporal")]
724    fn is_over_or_before(&self, other: Self::Enum) -> bool {
725        unsafe { meos_sys::overbefore_temporal_temporal(self.inner(), other.inner()) }
726    }
727
728    /// Returns whether `self` is after `other`.
729    ///
730    /// ## Arguments
731    /// * `other` - A time or temporal object to compare.
732    ///
733    /// ## Returns
734    /// True if `self` is after `other`, False otherwise.
735    ///
736    /// See also:
737    ///     `TsTzSpan.is_right`
738    #[doc(alias = "after_temporal_temporal")]
739    fn is_after(&self, other: Self::Enum) -> bool {
740        unsafe { meos_sys::after_temporal_temporal(self.inner(), other.inner()) }
741    }
742
743    /// Returns whether `self` is after `other` allowing overlap.
744    ///
745    /// ## Arguments
746    /// * `other` - A time or temporal object to compare.
747    ///
748    /// ## Returns
749    /// True if `self` is after `other` allowing overlap, False otherwise.
750    ///
751    /// See also:
752    ///     `TsTzSpan.is_over_or_right`
753    #[doc(alias = "overafter_temporal_temporal")]
754    fn is_over_or_after(&self, other: Self::Enum) -> bool {
755        unsafe { meos_sys::overafter_temporal_temporal(self.inner(), other.inner()) }
756    }
757
758    // ------------------------- Similarity Operations -------------------------
759    /// Returns the Frechet distance between `self` and `other`.
760    ///
761    /// ## Arguments
762    /// * `other` - A temporal object to compare.
763    ///
764    /// ## Returns
765    /// A float with the Frechet distance.
766    #[doc(alias = "temporal_frechet_distance")]
767    fn frechet_distance(&self, other: Self) -> f64 {
768        unsafe { meos_sys::temporal_frechet_distance(self.inner(), other.inner()) }
769    }
770
771    /// Returns the Dynamic Time Warp distance between `self` and `other`.
772    ///
773    /// ## Arguments
774    /// * `other` - A temporal object to compare.
775    ///
776    /// ## Returns
777    /// A float with the Dynamic Time Warp distance.
778    #[doc(alias = "temporal_dyntimewarp_distance")]
779    fn dyntimewarp_distance(&self, other: Self) -> f64 {
780        unsafe { meos_sys::temporal_dyntimewarp_distance(self.inner(), other.inner()) }
781    }
782
783    /// Returns the Hausdorff distance between `self` and `other`.
784    ///
785    /// ## Arguments
786    /// * `other` - A temporal object to compare.
787    ///
788    /// ## Returns
789    /// A float with the Hausdorff distance.
790    #[doc(alias = "temporal_hausdorff_distance")]
791    fn hausdorff_distance(&self, other: Self) -> f64 {
792        unsafe { meos_sys::temporal_hausdorff_distance(self.inner(), other.inner()) }
793    }
794
795    // ------------------------- Split Operations ------------------------------
796    /// Splits the temporal object into multiple pieces based on the given duration.
797    ///
798    /// ## Arguments
799    /// * `duration` - Duration of each temporal tile.
800    /// * `start` - Start time for the tiles.
801    ///
802    /// ## Returns
803    /// A list of temporal objects representing the split tiles.
804    #[doc(alias = "temporal_time_split")]
805    fn time_split<Tz: TimeZone>(&self, duration: TimeDelta, start: DateTime<Tz>) -> Vec<Self> {
806        let duration = create_interval(duration);
807        let start = to_meos_timestamp(&start);
808        let mut count = 0;
809        let _buckets = Vec::new().as_mut_ptr();
810        unsafe {
811            let temps = meos_sys::temporal_time_split(
812                self.inner(),
813                ptr::addr_of!(duration),
814                start,
815                _buckets,
816                ptr::addr_of_mut!(count),
817            );
818
819            Vec::from_raw_parts(temps, count as usize, count as usize)
820                .iter()
821                .map(|&t| Temporal::from_inner_as_temporal(t))
822                .collect()
823        }
824    }
825
826    /// Splits the temporal object into `n` equal-duration parts.
827    ///
828    /// ## Arguments
829    /// * `n` - Number of parts to split into.
830    ///
831    /// ## Returns
832    /// A list of temporal objects representing the split parts.
833    fn time_split_n(&self, n: usize) -> Vec<Self> {
834        let start = self.start_timestamp();
835        let duration = (self.end_timestamp() - start) / n as i32;
836        self.time_split(duration, start)
837    }
838
839    /// Extracts the subsequences where the object stays within a certain distance for a specified duration.
840    ///
841    /// ## Arguments
842    /// * `max_distance` - Maximum distance of a stop.
843    /// * `min_duration` - Minimum duration of a stop.
844    ///
845    /// ## Returns
846    /// A sequence set of stops.
847    #[doc(alias = "temporal_stops")]
848    fn stops(&self, max_distance: f64, min_duration: TimeDelta) -> Self::TSS {
849        let interval = create_interval(min_duration);
850        unsafe {
851            <Self::TSS as TSequenceSet>::from_inner(meos_sys::temporal_stops(
852                self.inner(),
853                max_distance,
854                ptr::addr_of!(interval),
855            ))
856        }
857    }
858
859    /// Returns whether the values of `self` are always equal to `other`.
860    ///
861    /// ## Arguments
862    ///
863    /// * `other` - Another temporal instance to compare against.
864    ///
865    /// ## Returns
866    ///
867    /// `true` if the values of `self` are always equal to `other`, `false` otherwise.
868    #[doc(alias = "always_eq_temporal_temporal")]
869    fn always_equal(&self, other: &Self) -> Option<bool> {
870        let result = unsafe { meos_sys::always_eq_temporal_temporal(self.inner(), other.inner()) };
871        if result != -1 {
872            Some(result == 1)
873        } else {
874            None
875        }
876    }
877
878    /// Returns whether the values of `self` are always not equal to `other`.
879    ///
880    /// ## Arguments
881    ///
882    /// * `other` - Another temporal instance to compare against.
883    ///
884    /// ## Returns
885    ///
886    /// `true` if the values of `self` are always not equal to `other`, `false` otherwise.
887    #[doc(alias = "always_ne_temporal_temporal")]
888    fn always_not_equal(&self, other: &Self) -> Option<bool> {
889        let result = unsafe { meos_sys::always_ne_temporal_temporal(self.inner(), other.inner()) };
890        if result != -1 {
891            Some(result == 1)
892        } else {
893            None
894        }
895    }
896
897    /// Returns whether the values of `self` are ever equal to `other`.
898    ///
899    /// ## Arguments
900    ///
901    /// * `other` - Another temporal instance to compare against.
902    ///
903    /// ## Returns
904    ///
905    /// `true` if the values of `self` are ever equal to `other`, `false` otherwise.
906    #[doc(alias = "ever_eq_temporal_temporal")]
907    fn ever_equal(&self, other: &Self) -> Option<bool> {
908        let result = unsafe { meos_sys::ever_eq_temporal_temporal(self.inner(), other.inner()) };
909        if result != -1 {
910            Some(result == 1)
911        } else {
912            None
913        }
914    }
915
916    /// Returns whether the values of `self` are ever not equal to `other`.
917    ///
918    /// ## Arguments
919    ///
920    /// * `other` - Another temporal instance to compare against.
921    ///
922    /// ## Returns
923    ///
924    /// `true` if the values of `self` are ever not equal to `other`, `false` otherwise.
925    #[doc(alias = "ever_ne_temporal_temporal")]
926    fn ever_not_equal(&self, other: &Self) -> Option<bool> {
927        let result = unsafe { meos_sys::ever_ne_temporal_temporal(self.inner(), other.inner()) };
928        if result != -1 {
929            Some(result == 1)
930        } else {
931            None
932        }
933    }
934
935    /// Returns whether the values of `self` are always equal to `value`.
936    ///
937    /// ## Arguments
938    ///
939    /// * `value` - Value to compare against.
940    ///
941    /// ## Returns
942    ///
943    /// `true` if the values of `self` are always equal to `value`, `false` otherwise.
944    fn always_equal_than_value(&self, value: Self::Type) -> Option<bool>;
945
946    /// Returns whether the values of `self` are always not equal to `value`.
947    ///
948    /// ## Arguments
949    ///
950    /// * `value` - Value to compare against.
951    ///
952    /// ## Returns
953    ///
954    /// `true` if the values of `self` are always not equal to `value`, `false` otherwise.
955    fn always_not_equal_than_value(&self, value: Self::Type) -> Option<bool>;
956
957    /// Returns whether the values of `self` are ever equal to `value`.
958    ///
959    /// ## Arguments
960    ///
961    /// * `value` - Value to compare against.
962    ///
963    /// ## Returns
964    ///
965    /// `true` if the values of `self` are ever equal to `value`, `false` otherwise.
966    fn ever_equal_than_value(&self, value: Self::Type) -> Option<bool>;
967
968    /// Returns whether the values of `self` are ever not equal to `value`.
969    ///
970    /// ## Arguments
971    ///
972    /// * `value` - Value to compare against.
973    ///
974    /// ## Returns
975    ///
976    /// `true` if the values of `self` are ever not equal to `value`, `false` otherwise.
977    fn ever_not_equal_than_value(&self, value: Self::Type) -> Option<bool>;
978
979    /// Returns a `TBool` representing whether `self` is equal to `other` accross time.
980    ///
981    /// ## Arguments
982    ///
983    /// * `other` - A reference to another temporal object to compare with.
984    ///
985    /// ## Returns
986    ///
987    /// A temporal boolean indicating if `self` is equal to `other`.
988    fn temporal_equal(&self, other: &Self) -> Self::TBoolType {
989        Self::TBoolType::from_inner_as_temporal(unsafe {
990            meos_sys::teq_temporal_temporal(self.inner(), other.inner())
991        })
992    }
993
994    /// Returns a `TBool` representing whether `self` is not equal to `other` accross time.
995    ///
996    /// ## Arguments
997    ///
998    /// * `other` - A reference to another temporal object to compare with.
999    ///
1000    /// ## Returns
1001    ///
1002    /// A temporal boolean indicating if `self` is not equal to `other`.
1003    fn temporal_not_equal(&self, other: &Self) -> Self::TBoolType {
1004        Self::TBoolType::from_inner_as_temporal(unsafe {
1005            meos_sys::tne_temporal_temporal(self.inner(), other.inner())
1006        })
1007    }
1008
1009    /// Returns a `TBool` representing whether `self` is equal to the given value accross time.
1010    ///
1011    /// ## Arguments
1012    ///
1013    /// * `other` - A reference to a value to compare with.
1014    ///
1015    /// ## Returns
1016    ///
1017    /// A temporal boolean indicating if `self` is equal to the given value.
1018    fn temporal_equal_value(&self, other: &Self::Type) -> Self::TBoolType;
1019
1020    /// Returns a `TBool` representing whether `self` is not equal to the given value accross time.
1021    ///
1022    /// ## Arguments
1023    ///
1024    /// * `other` - A reference to a value to compare with.
1025    ///
1026    /// ## Returns
1027    ///
1028    /// A temporal boolean indicating if `self` is not equal to the given value.
1029    fn temporal_not_equal_value(&self, other: &Self::Type) -> Self::TBoolType;
1030}
1031
1032pub trait OrderedTemporal: Temporal {
1033    /// Returns the minimum value of the temporal object.
1034    ///
1035    /// ## Returns
1036    /// The minimum value.
1037    fn min_value(&self) -> Self::Type;
1038
1039    /// Returns the maximum value of the temporal object.
1040    ///
1041    /// ## Returns
1042    /// The maximum value.
1043    fn max_value(&self) -> Self::Type;
1044
1045    /// Returns a new temporal object containing the times `self` is at its minimum value.
1046    fn at_min(&self) -> Self {
1047        Self::from_inner_as_temporal(unsafe { meos_sys::temporal_at_min(self.inner()) })
1048    }
1049
1050    /// Returns a new temporal object containing the times `self` is at its maximum value.
1051    fn at_max(&self) -> Self {
1052        Self::from_inner_as_temporal(unsafe { meos_sys::temporal_at_max(self.inner()) })
1053    }
1054    /// Returns a new temporal object containing the times `self` is not at its minimum value.
1055    fn minus_min(&self) -> Self {
1056        Self::from_inner_as_temporal(unsafe { meos_sys::temporal_minus_min(self.inner()) })
1057    }
1058
1059    /// Returns a new temporal object containing the times `self` is not at its maximum value.
1060    fn minus_max(&self) -> Self {
1061        Self::from_inner_as_temporal(unsafe { meos_sys::temporal_minus_max(self.inner()) })
1062    }
1063
1064    /// Returns a `TBool` representing whether `self` is greater than `other` accross time.
1065    ///
1066    /// ## Arguments
1067    ///
1068    /// * `other` - A reference to another temporal object to compare with.
1069    ///
1070    /// ## Returns
1071    ///
1072    /// A temporal boolean indicating if `self` is greater than `other`.
1073    fn temporal_greater_than(&self, other: &Self) -> Self::TBoolType {
1074        Self::TBoolType::from_inner_as_temporal(unsafe {
1075            meos_sys::tgt_temporal_temporal(self.inner(), other.inner())
1076        })
1077    }
1078
1079    /// Returns a `TBool` representing whether `self` is greater than or equal to `other` accross time.
1080    ///
1081    /// ## Arguments
1082    ///
1083    /// * `other` - A reference to another temporal object to compare with.
1084    ///
1085    /// ## Returns
1086    ///
1087    /// A temporal boolean indicating if `self` is greater than or equal to `other`.
1088    fn temporal_greater_or_equal_than(&self, other: &Self) -> Self::TBoolType {
1089        Self::TBoolType::from_inner_as_temporal(unsafe {
1090            meos_sys::tge_temporal_temporal(self.inner(), other.inner())
1091        })
1092    }
1093
1094    /// Returns a `TBool` representing whether `self` is less than `other` accross time.
1095    ///
1096    /// ## Arguments
1097    ///
1098    /// * `other` - A reference to another temporal object to compare with.
1099    ///
1100    /// ## Returns
1101    ///
1102    /// A temporal boolean indicating if `self` is less than `other`.
1103    fn temporal_lower_than(&self, other: &Self) -> Self::TBoolType {
1104        Self::TBoolType::from_inner_as_temporal(unsafe {
1105            meos_sys::tlt_temporal_temporal(self.inner(), other.inner())
1106        })
1107    }
1108
1109    /// Returns a `TBool` representing whether `self` is less than or equal to `other` accross time.
1110    ///
1111    /// ## Arguments
1112    ///
1113    /// * `other` - A reference to another temporal object to compare with.
1114    ///
1115    /// ## Returns
1116    ///
1117    /// A temporal boolean indicating if `self` is less than or equal to `other`.
1118    fn temporal_lower_or_equal_than(&self, other: &Self) -> Self::TBoolType {
1119        Self::TBoolType::from_inner_as_temporal(unsafe {
1120            meos_sys::tle_temporal_temporal(self.inner(), other.inner())
1121        })
1122    }
1123
1124    /// Returns a `TBool` representing whether `self` is greater than the given value accross time.
1125    ///
1126    /// ## Arguments
1127    ///
1128    /// * `other` - A reference to a value to compare with.
1129    ///
1130    /// ## Returns
1131    ///
1132    /// A temporal boolean indicating if `self` is greater than the given value.
1133    fn temporal_greater_than_value(&self, other: &Self::Type) -> Self::TBoolType;
1134
1135    /// Returns a `TBool` representing whether `self` is greater than or equal to the given value accross time.
1136    ///
1137    /// ## Arguments
1138    ///
1139    /// * `other` - A reference to a value to compare with.
1140    ///
1141    /// ## Returns
1142    ///
1143    /// A temporal boolean indicating if `self` is greater than or equal to the given value.
1144    fn temporal_greater_or_equal_than_value(&self, other: &Self::Type) -> Self::TBoolType;
1145
1146    /// Returns a `TBool` representing whether `self` is less than the given value accross time.
1147    ///
1148    /// ## Arguments
1149    ///
1150    /// * `other` - A reference to a value to compare with.
1151    ///
1152    /// ## Returns
1153    ///
1154    /// A temporal boolean indicating if `self` is less than the given value.
1155    fn temporal_lower_than_value(&self, other: &Self::Type) -> Self::TBoolType;
1156
1157    /// Returns a `TBool` representing whether `self` is less than or equal to the given value accross time.
1158    ///
1159    /// ## Arguments
1160    ///
1161    /// * `other` - A reference to a value to compare with.
1162    ///
1163    /// ## Returns
1164    ///
1165    /// A temporal boolean indicating if `self` is less than or equal to the given value.
1166    fn temporal_lower_or_equal_than_value(&self, other: &Self::Type) -> Self::TBoolType;
1167
1168    /// Returns whether the values of `self` are always less than `other`.
1169    ///
1170    /// ## Arguments
1171    ///
1172    /// * `other` - Another temporal instance to compare against.
1173    ///
1174    /// ## Returns
1175    ///
1176    /// `true` if the values of `self` are always less than `other`, `false` otherwise.
1177    #[doc(alias = "always_lt_temporal_temporal")]
1178    fn always_less(&self, other: &Self::Enum) -> Option<bool> {
1179        let result = unsafe { meos_sys::always_lt_temporal_temporal(self.inner(), other.inner()) };
1180        if result != -1 {
1181            Some(result == 1)
1182        } else {
1183            None
1184        }
1185    }
1186
1187    /// Returns whether the values of `self` are always less than or equal to `other`.
1188    ///
1189    /// ## Arguments
1190    ///
1191    /// * `other` - Another temporal instance to compare against.
1192    ///
1193    /// ## Returns
1194    ///
1195    /// `true` if the values of `self` are always less than or equal to `other`, `false` otherwise.
1196    #[doc(alias = "always_le_temporal_temporal")]
1197    fn always_less_or_equal(&self, other: &Self::Enum) -> Option<bool> {
1198        let result = unsafe { meos_sys::always_le_temporal_temporal(self.inner(), other.inner()) };
1199        if result != -1 {
1200            Some(result == 1)
1201        } else {
1202            None
1203        }
1204    }
1205
1206    /// Returns whether the values of `self` are always greater than or equal to `other`.
1207    ///
1208    /// ## Arguments
1209    ///
1210    /// * `other` - Another temporal instance to compare against.
1211    ///
1212    /// ## Returns
1213    ///
1214    /// `true` if the values of `self` are always greater than or equal to `other`, `false` otherwise.
1215    #[doc(alias = "always_ge_temporal_temporal")]
1216    fn always_greater_or_equal(&self, other: &Self::Enum) -> Option<bool> {
1217        let result = unsafe { meos_sys::always_ge_temporal_temporal(self.inner(), other.inner()) };
1218        if result != -1 {
1219            Some(result == 1)
1220        } else {
1221            None
1222        }
1223    }
1224
1225    /// Returns whether the values of `self` are always greater than `other`.
1226    ///
1227    /// ## Arguments
1228    ///
1229    /// * `other` - Another temporal instance to compare against.
1230    ///
1231    /// ## Returns
1232    ///
1233    /// `true` if the values of `self` are always greater than `other`, `false` otherwise.
1234    #[doc(alias = "always_gt_temporal_temporal")]
1235    fn always_greater(&self, other: &Self::Enum) -> Option<bool> {
1236        let result = unsafe { meos_sys::always_gt_temporal_temporal(self.inner(), other.inner()) };
1237        if result != -1 {
1238            Some(result == 1)
1239        } else {
1240            None
1241        };
1242        if result != -1 {
1243            Some(result == 1)
1244        } else {
1245            None
1246        }
1247    }
1248    /// Returns whether the values of `self` are ever less than `other`.
1249    ///
1250    /// ## Arguments
1251    ///
1252    /// * `other` - Another temporal instance to compare against.
1253    ///
1254    /// ## Returns
1255    ///
1256    /// `true` if the values of `self` are ever less than `other`, `false` otherwise.
1257    #[doc(alias = "ever_lt_temporal_temporal")]
1258    fn ever_less(&self, other: &Self::Enum) -> Option<bool> {
1259        let result = unsafe { meos_sys::ever_lt_temporal_temporal(self.inner(), other.inner()) };
1260        if result != -1 {
1261            Some(result == 1)
1262        } else {
1263            None
1264        }
1265    }
1266    /// Returns whether the values of `self` are ever less than or equal to `other`.
1267    ///
1268    /// ## Arguments
1269    ///
1270    /// * `other` - Another temporal instance to compare against.
1271    ///
1272    /// ## Returns
1273    ///
1274    /// `true` if the values of `self` are ever less than or equal to `other`, `false` otherwise.
1275    #[doc(alias = "ever_le_temporal_temporal")]
1276    fn ever_less_or_equal(&self, other: &Self::Enum) -> Option<bool> {
1277        let result = unsafe { meos_sys::ever_le_temporal_temporal(self.inner(), other.inner()) };
1278        if result != -1 {
1279            Some(result == 1)
1280        } else {
1281            None
1282        }
1283    }
1284
1285    /// Returns whether the values of `self` are ever greater than or equal to `other`.
1286    ///
1287    /// ## Arguments
1288    ///
1289    /// * `other` - Another temporal instance to compare against.
1290    ///
1291    /// ## Returns
1292    ///
1293    /// `true` if the values of `self` are ever greater than or equal to `other`, `false` otherwise.
1294    #[doc(alias = "ever_ge_temporal_temporal")]
1295    fn ever_greater_or_equal(&self, other: &Self::Enum) -> Option<bool> {
1296        let result = unsafe { meos_sys::ever_ge_temporal_temporal(self.inner(), other.inner()) };
1297        if result != -1 {
1298            Some(result == 1)
1299        } else {
1300            None
1301        }
1302    }
1303
1304    /// Returns whether the values of `self` are ever greater than `other`.
1305    ///
1306    /// ## Arguments
1307    ///
1308    /// * `other` - Another temporal instance to compare against.
1309    ///
1310    /// ## Returns
1311    ///
1312    /// `true` if the values of `self` are ever greater than `other`, `false` otherwise.
1313    #[doc(alias = "ever_gt_temporal_temporal")]
1314    fn ever_greater(&self, other: &Self::Enum) -> Option<bool> {
1315        let result = unsafe { meos_sys::ever_gt_temporal_temporal(self.inner(), other.inner()) };
1316        if result != -1 {
1317            Some(result == 1)
1318        } else {
1319            None
1320        }
1321    }
1322
1323    /// Returns whether the values of `self` are always less than `value`.
1324    ///
1325    /// ## Arguments
1326    ///
1327    /// * `value` - Value to compare against.
1328    ///
1329    /// ## Returns
1330    ///
1331    /// `true` if the values of `self` are always less than `value`, `false` otherwise.
1332    fn always_less_than_value(&self, value: Self::Type) -> Option<bool>;
1333
1334    /// Returns whether the values of `self` are always less than or equal to `value`.
1335    ///
1336    /// ## Arguments
1337    ///
1338    /// * `value` - Value to compare against.
1339    ///
1340    /// ## Returns
1341    ///
1342    /// `true` if the values of `self` are always less than or equal to `value`, `false` otherwise.
1343    fn always_less_or_equal_than_value(&self, value: Self::Type) -> Option<bool>;
1344
1345    /// Returns whether the values of `self` are always greater than or equal to `value`.
1346    ///
1347    /// ## Arguments
1348    ///
1349    /// * `value` - Value to compare against.
1350    ///
1351    /// ## Returns
1352    ///
1353    /// `true` if the values of `self` are always greater than or equal to `value`, `false` otherwise.
1354    fn always_greater_or_equal_than_value(&self, value: Self::Type) -> Option<bool>;
1355
1356    /// Returns whether the values of `self` are always greater than `value`.
1357    ///
1358    /// ## Arguments
1359    ///
1360    /// * `value` - Value to compare against.
1361    ///
1362    /// ## Returns
1363    ///
1364    /// `true` if the values of `self` are always greater than `value`, `false` otherwise.
1365    fn always_greater_than_value(&self, value: Self::Type) -> Option<bool>;
1366
1367    /// Returns whether the values of `self` are ever less than `value`.
1368    ///
1369    /// ## Arguments
1370    ///
1371    /// * `value` - Value to compare against.
1372    ///
1373    /// ## Returns
1374    ///
1375    /// `true` if the values of `self` are ever less than `value`, `false` otherwise.
1376    fn ever_less_than_value(&self, value: Self::Type) -> Option<bool>;
1377
1378    /// Returns whether the values of `self` are ever less than or equal to `value`.
1379    ///
1380    /// ## Arguments
1381    ///
1382    /// * `value` - Value to compare against.
1383    ///
1384    /// ## Returns
1385    ///
1386    /// `true` if the values of `self` are ever less than or equal to `value`, `false` otherwise.
1387    fn ever_less_or_equal_than_value(&self, value: Self::Type) -> Option<bool>;
1388
1389    /// Returns whether the values of `self` are ever greater than or equal to `value`.
1390    ///
1391    /// ## Arguments
1392    ///
1393    /// * `value` - Value to compare against.
1394    ///
1395    /// ## Returns
1396    ///
1397    /// `true` if the values of `self` are ever greater than or equal to `value`, `false` otherwise.
1398    fn ever_greater_or_equal_than_value(&self, value: Self::Type) -> Option<bool>;
1399
1400    /// Returns whether the values of `self` are ever greater than `value`.
1401    ///
1402    /// ## Arguments
1403    ///
1404    /// * `value` - Value to compare against.
1405    ///
1406    /// ## Returns
1407    ///
1408    /// `true` if the values of `self` are ever greater than `value`, `false` otherwise.
1409    fn ever_greater_than_value(&self, value: Self::Type) -> Option<bool>;
1410}
1411
1412/// A trait for simplifying temporal values in various ways.
1413pub trait SimplifiableTemporal: Temporal {
1414    /// Simplifies a temporal value ensuring that consecutive values are at least
1415    /// a certain distance apart.
1416    ///
1417    /// ## Arguments
1418    ///
1419    /// * `distance` - A `f64` representing the minimum distance between two points.
1420    ///
1421    /// ## Returns
1422    ///
1423    /// A simplified instance of the implementing type with the same subtype as the input.
1424    ///
1425    /// ## MEOS Functions
1426    #[doc(alias = "temporal_simplify_min_dist")]
1427    fn simplify_min_distance(&self, distance: f64) -> Self::Enum {
1428        factory::<Self::Enum>(unsafe {
1429            meos_sys::temporal_simplify_min_dist(self.inner(), distance)
1430        })
1431    }
1432
1433    /// Simplifies a temporal value ensuring that consecutive values are at least
1434    /// a certain time apart.
1435    ///
1436    /// ## Arguments
1437    ///
1438    /// * `distance` - A `Duration` indicating the minimum time between two points.
1439    ///
1440    /// ## Returns
1441    ///
1442    /// A simplified instance of the implementing type with the same subtype as the input.
1443    #[doc(alias = "temporal_simplify_min_tdelta")]
1444    fn simplify_min_tdelta(&self, distance: TimeDelta) -> Self::Enum {
1445        let interval = create_interval(distance);
1446        factory::<Self::Enum>(unsafe {
1447            meos_sys::temporal_simplify_min_tdelta(self.inner(), ptr::addr_of!(interval))
1448        })
1449    }
1450
1451    /// Simplifies a temporal value using the Douglas-Peucker line simplification algorithm.
1452    ///
1453    /// ## Arguments
1454    ///
1455    /// * `distance` - A `f64` representing the minimum distance between two points.
1456    /// * `synchronized` - A `bool` indicating if the Synchronized Distance should be used.
1457    ///   If `false`, the spatial-only distance will be used.
1458    ///
1459    /// ## Returns
1460    ///
1461    /// A simplified instance of the implementing type with the same subtype as the input.
1462    #[doc(alias = "temporal_simplify_dp")]
1463    fn simplify_douglas_peucker(&self, distance: f64, synchronized: bool) -> Self::Enum {
1464        factory::<Self::Enum>(unsafe {
1465            meos_sys::temporal_simplify_dp(self.inner(), distance, synchronized)
1466        })
1467    }
1468
1469    /// Simplifies a temporal value using a single-pass Douglas-Peucker line simplification algorithm.
1470    ///
1471    /// ## Arguments
1472    ///
1473    /// * `distance` - A `f64` representing the minimum distance between two points.
1474    /// * `synchronized` - A `bool` indicating if the Synchronized Distance should be used.
1475    ///   If `false`, the spatial-only distance will be used.
1476    ///
1477    /// ## Returns
1478    ///
1479    /// A simplified instance of the implementing type with the same subtype as the input.
1480    #[doc(alias = "temporal_simplify_max_dist")]
1481    fn simplify_max_distance(&self, distance: f64, synchronized: bool) -> Self::Enum {
1482        factory::<Self::Enum>(unsafe {
1483            meos_sys::temporal_simplify_max_dist(self.inner(), distance, synchronized)
1484        })
1485    }
1486}
1487
1488macro_rules! impl_simple_traits_for_temporal {
1489    ($type:ty) => {
1490        paste::paste! {
1491            impl AsRef<$type> for $type {
1492                fn as_ref(&self) -> &$type {
1493                    self
1494                }
1495            }
1496
1497            impl Clone for $type {
1498                fn clone(&self) -> Self {
1499                    Temporal::from_inner_as_temporal(unsafe { meos_sys::temporal_copy(self.inner()) })
1500                }
1501            }
1502            impl PartialEq for $type {
1503                fn eq(&self, other: &Self) -> bool {
1504                    unsafe { meos_sys::temporal_eq(self.inner(), other.inner()) }
1505                }
1506            }
1507
1508            impl Hash for $type {
1509                fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
1510                    let hash = unsafe { meos_sys::temporal_hash(self.inner()) };
1511                    state.write_u32(hash);
1512
1513                    let _ = state.finish();
1514                }
1515            }
1516        }
1517    };
1518    ($type:ty, with_drop) => {
1519        impl_simple_traits_for_temporal!($type);
1520
1521        impl Drop for $type {
1522            fn drop(&mut self) {
1523                unsafe {
1524                    libc::free(self._inner.as_ptr() as *mut c_void);
1525                }
1526            }
1527        }
1528    }
1529}
1530
1531macro_rules! impl_always_and_ever_value_equality_functions {
1532    ($type:ident, $transform_function:expr) => {
1533        paste::paste! {
1534            fn always_equal_than_value(&self, value: Self::Type) -> Option<bool> {
1535                let result = unsafe { meos_sys::[<always_eq_t $type _ $type>](self.inner(), $transform_function(&value)) };
1536                if result != -1 {
1537                    Some(result == 1)
1538                } else {
1539                    None
1540                }
1541            }
1542            fn always_not_equal_than_value(&self, value: Self::Type) -> Option<bool> {
1543                let result = unsafe { meos_sys::[<always_ne_t $type _ $type>](self.inner(), $transform_function(&value)) };
1544                if result != -1 {
1545                    Some(result == 1)
1546                } else {
1547                    None
1548                }
1549            }
1550            fn ever_equal_than_value(&self, value: Self::Type) -> Option<bool> {
1551                let result = unsafe { meos_sys::[<ever_eq_t $type _ $type>](self.inner(), $transform_function(&value)) };
1552                if result != -1 {
1553                    Some(result == 1)
1554                } else {
1555                    None
1556                }
1557            }
1558            fn ever_not_equal_than_value(&self, value: Self::Type) -> Option<bool> {
1559                let result = unsafe { meos_sys::[<ever_ne_t $type _ $type>](self.inner(), $transform_function(&value)) };
1560                if result != -1 {
1561                    Some(result == 1)
1562                } else {
1563                    None
1564                }
1565
1566            }
1567        }
1568    };
1569    ($type:ident) => {
1570        impl_always_and_ever_value_equality_functions!($type, |&x| x);
1571    };
1572}
1573
1574macro_rules! impl_ordered_temporal_functions {
1575
1576    ($type:ident, $transform_function:expr) => {
1577        paste::paste! {
1578            fn always_less_than_value(&self, value: Self::Type) -> Option<bool> {
1579                let result = unsafe { meos_sys::[<always_lt_t $type _ $type>](self.inner(), $transform_function(&value)) };
1580                if result != -1 {
1581                    Some(result == 1)
1582                } else {
1583                    None
1584                }
1585            }
1586            fn always_less_or_equal_than_value(&self, value: Self::Type) -> Option<bool> {
1587                let result = unsafe { meos_sys::[<always_le_t $type _ $type>](self.inner(), $transform_function(&value)) };
1588                if result != -1 {
1589                    Some(result == 1)
1590                } else {
1591                    None
1592                }
1593            }
1594            fn always_greater_or_equal_than_value(&self, value: Self::Type) -> Option<bool> {
1595                let result = unsafe { meos_sys::[<always_ge_t $type _ $type>](self.inner(), $transform_function(&value)) };
1596                if result != -1 {
1597                    Some(result == 1)
1598                } else {
1599                    None
1600                }
1601            }
1602            fn always_greater_than_value(&self, value: Self::Type) -> Option<bool> {
1603                let result = unsafe { meos_sys::[<always_gt_t $type _ $type>](self.inner(), $transform_function(&value)) };
1604                if result != -1 {
1605                    Some(result == 1)
1606                } else {
1607                    None
1608                }
1609            }
1610            fn ever_less_than_value(&self, value: Self::Type) -> Option<bool> {
1611                let result = unsafe { meos_sys::[<ever_lt_t $type _ $type>](self.inner(), $transform_function(&value)) };
1612                if result != -1 {
1613                    Some(result == 1)
1614                } else {
1615                    None
1616                }
1617            }
1618            fn ever_less_or_equal_than_value(&self, value: Self::Type) -> Option<bool> {
1619                let result = unsafe { meos_sys::[<ever_le_t $type _ $type>](self.inner(), $transform_function(&value)) };
1620                if result != -1 {
1621                    Some(result == 1)
1622                } else {
1623                    None
1624                }
1625            }
1626            fn ever_greater_or_equal_than_value(&self, value: Self::Type) -> Option<bool> {
1627                let result = unsafe { meos_sys::[<ever_ge_t $type _ $type>](self.inner(), $transform_function(&value)) };
1628                if result != -1 {
1629                    Some(result == 1)
1630                } else {
1631                    None
1632                }
1633            }
1634            fn ever_greater_than_value(&self, value: Self::Type) -> Option<bool> {
1635                let result = unsafe { meos_sys::[<ever_gt_t $type _ $type>](self.inner(), $transform_function(&value)) };
1636                if result != -1 {
1637                    Some(result == 1)
1638                } else {
1639                    None
1640                }
1641            }
1642
1643            fn temporal_greater_than_value(&self, other: &Self::Type) -> Self::TBoolType {
1644                Self::TBoolType::from_inner_as_temporal(unsafe {
1645                    meos_sys::[<tgt_t $type _ $type>](self.inner(), $transform_function(other))
1646                })
1647            }
1648            fn temporal_greater_or_equal_than_value(&self, other: &Self::Type) -> Self::TBoolType {
1649                Self::TBoolType::from_inner_as_temporal(unsafe {
1650                    meos_sys::[<tge_t $type _ $type>](self.inner(), $transform_function(other))
1651                })
1652            }
1653            fn temporal_lower_than_value(&self, other: &Self::Type) -> Self::TBoolType {
1654                Self::TBoolType::from_inner_as_temporal(unsafe {
1655                    meos_sys::[<tlt_t $type _ $type>](self.inner(), $transform_function(other))
1656                })
1657            }
1658            fn temporal_lower_or_equal_than_value(&self, other: &Self::Type) -> Self::TBoolType {
1659                Self::TBoolType::from_inner_as_temporal(unsafe {
1660                    meos_sys::[<tle_t $type _ $type>](self.inner(), $transform_function(other))
1661                })
1662            }
1663        }
1664    };
1665    ($type:ident) => {
1666        impl_ordered_temporal_functions!($type, |&x| x);
1667    };
1668}
1669
1670pub(crate) use impl_ordered_temporal_functions;
1671
1672pub(crate) use impl_always_and_ever_value_equality_functions;
1673
1674pub(crate) use impl_simple_traits_for_temporal;