tea-core 0.5.1

Core data structures and traits for tevec.
Documentation
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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
use std::marker::PhantomData;

use tea_dtype::{Cast, IsNone};
use tea_error::{TResult, tbail};

use super::super::iter::{OptIter, TIter};
use super::super::iter_traits::TIterator;
use super::super::uninit::UninitRefMut;
use super::own::{Vec1, Vec1Collect};
use crate::prelude::{ToTrustIter, TrustedLen, WriteTrustIter};

/// A trait representing a view into a vector-like structure.
///
/// This trait provides methods for accessing and manipulating data in a vector-like structure
/// without necessarily owning the data. It allows for operations such as slicing, indexing,
/// and iterating over the elements.
///
/// The trait is generic over the type `T`, which represents the type of elements stored in the vector.
///
/// Implementations of this trait should provide efficient access to the underlying data
/// and support various operations that are common for vector-like structures.
///
/// # Type Parameters
///
/// * `T`: The type of elements stored in the vector-like structure.
///
/// # Associated Types
///
/// * `SliceOutput<'a>`: The type returned by the `slice` method. This type must implement `ToOwned`
///   to allow conversion to an owned type if needed.
///
/// Implementations of this trait might include views into contiguous memory arrays,
/// database columns, or other data structures that can be logically viewed as a vector.
pub trait Vec1View<T>: TIter<T> {
    type SliceOutput<'a>: ToOwned
    where
        Self: 'a,
        // this constraint is needed for ndarray backend
        T: 'a;

    /// Returns the name of the backend implementation.
    ///
    /// This method is useful for debugging and logging purposes, allowing
    /// identification of which specific backend is being used at runtime.
    ///
    /// Additionally, this method can be used to implement backend-specific
    /// optimizations or behaviors in certain methods. By checking the backend
    /// name, code can be written to take advantage of specific backend
    /// capabilities or work around limitations.
    ///
    /// # Returns
    ///
    /// A static string slice containing the name of the backend.
    fn get_backend_name(&self) -> &'static str;

    /// Attempts to create a slice of the vector from the given start and end indices.
    ///
    /// # Arguments
    ///
    /// * `start` - The starting index of the slice.
    /// * `end` - The ending index of the slice (exclusive).
    ///
    /// # Returns
    ///
    /// Returns a `TResult` containing the slice if successful, or an error if slicing is not supported for this backend.
    ///
    /// # Note
    ///
    /// This default implementation returns an error, indicating that slicing is not supported.
    /// Backends that support slicing should override this method.
    #[inline]
    fn slice<'a>(&'a self, _start: usize, _end: usize) -> TResult<Self::SliceOutput<'a>>
    where
        T: 'a,
    {
        tbail!("slice is not supported for this backend")
    }

    #[inline]
    /// Creates an unsafe slice of the vector from the given start and end indices.
    ///
    /// # Safety
    ///
    /// The caller must ensure that:
    /// - `start` is less than or equal to `end`.
    /// - `end` is less than or equal to the length of the array.
    /// - The memory range from `start` to `end` is valid and properly initialized.
    ///
    /// # Arguments
    ///
    /// * `start` - The starting index of the slice.
    /// * `end` - The ending index of the slice (exclusive).
    ///
    /// # Returns
    ///
    /// Returns a `TResult<Self::SliceOutput<'a>>` representing the portion of the array from `start` to `end`.
    /// The actual type returned depends on the specific implementation of `Vec1View`.
    unsafe fn uslice<'a>(&'a self, start: usize, end: usize) -> TResult<Self::SliceOutput<'a>>
    where
        T: 'a,
    {
        self.slice(start, end)
    }

    /// Get the value at the index
    ///
    /// # Safety
    ///
    /// The index should be less than the length of the array
    unsafe fn uget(&self, index: usize) -> T;
    /// Attempts to return a reference to the underlying slice of the vector.
    ///
    /// # Returns
    ///
    /// - `Some(&[T])` if the vector's data is contiguous in memory and can be represented as a slice.
    /// - `None` if the vector's data cannot be represented as a contiguous slice.
    ///
    /// # Note
    ///
    /// This method is useful for backends that store data in a contiguous memory layout.
    /// It allows for efficient access to the underlying data without copying.
    /// The default implementation returns `None`, indicating that the data is not
    /// available as a contiguous slice. Backends that can provide this should override this method.
    #[inline(always)]
    fn try_as_slice(&self) -> Option<&[T]> {
        None
    }

    /// Creates an iterator that casts each element of the vector to a new type.
    ///
    /// # Type Parameters
    ///
    /// - `U`: The type to cast each element to.
    ///
    /// # Returns
    ///
    /// An iterator over the vector's elements, with each element cast to type `U`.
    ///
    /// # Note
    ///
    /// This method relies on the `Cast` trait being implemented for the conversion from `T` to `U`.
    #[inline]
    fn iter_cast<'a, U>(&'a self) -> impl TIterator<Item = U>
    where
        T: 'a + Cast<U>,
    {
        self.titer().map(|v| v.cast())
    }

    /// Creates an iterator that optionally casts each element of the vector to a new type.
    ///
    /// # Type Parameters
    ///
    /// - `U`: The type to cast each element to.
    ///
    /// # Returns
    ///
    /// An iterator over the vector's elements, with each element optionally cast to type `U`.
    ///
    /// # Note
    ///
    /// This method is useful when dealing with vectors that may contain null or invalid values.
    /// It uses the `IsNone` trait to determine if an element should be considered as `None`.
    #[inline]
    fn opt_iter_cast<'a, U>(&'a self) -> impl TIterator<Item = Option<U>>
    where
        T: IsNone + 'a,
        <T as IsNone>::Inner: Cast<U>,
    {
        self.titer().map(|v| v.to_opt().map(Cast::<U>::cast))
    }

    /// Creates an `OptIter` for the vector.
    ///
    /// # Returns
    ///
    /// An `OptIter` instance that allows iterating over the vector's elements as `Option` values.
    ///
    /// # Note
    ///
    /// This method is useful for treating the vector's elements as optional values,
    /// which is particularly helpful when dealing with data that may contain null or invalid entries.
    #[inline]
    fn opt(&self) -> OptIter<Self, T>
    where
        T: IsNone,
        Self: Sized,
    {
        OptIter {
            view: self,
            item: PhantomData,
        }
    }

    /// Creates an iterator that converts each element of the vector to an `Option`.
    ///
    /// # Returns
    ///
    /// An iterator over the vector's elements, with each element converted to `Option<T::Inner>`.
    ///
    /// # Note
    ///
    /// This method is useful for explicitly handling potential null or invalid values in the vector.
    /// It relies on the `IsNone` trait to determine how to convert each element to an `Option`.
    #[inline]
    fn to_opt_iter<'a>(&'a self) -> impl TIterator<Item = Option<T::Inner>>
    where
        T: IsNone + 'a,
    {
        self.titer().map(|v| v.to_opt())
    }

    /// Retrieves the value at the specified index as an `Option`, if it's valid.
    ///
    /// # Safety
    ///
    /// The caller must ensure that the index is less than the length of the array.
    ///
    /// # Arguments
    ///
    /// * `index`: The index of the element to retrieve.
    ///
    /// # Returns
    ///
    /// - `Some(T::Inner)` if the value at the index is valid.
    /// - `None` if the value is invalid or represents a null value.
    ///
    /// # Note
    ///
    /// This method is unsafe because it doesn't perform bounds checking. It's the caller's
    /// responsibility to ensure that the index is valid.
    #[inline]
    unsafe fn uvget(&self, index: usize) -> Option<T::Inner>
    where
        T: IsNone,
    {
        unsafe { self.uget(index).to_opt() }
    }

    /// Safely retrieves the value at the specified index.
    ///
    /// # Arguments
    ///
    /// * `index`: The index of the element to retrieve.
    ///
    /// # Returns
    ///
    /// - `Ok(T)` if the index is within bounds.
    /// - `Err(TError)` if the index is out of bounds.
    ///
    /// # Note
    ///
    /// This method performs bounds checking and is safe to use.
    #[inline]
    fn get(&self, index: usize) -> TResult<T> {
        if index < self.len() {
            Ok(unsafe { self.uget(index) })
        } else {
            tbail!(oob(index, self.len()))
        }
    }

    /// Safely retrieves the value at the specified index as an `Option`.
    ///
    /// # Arguments
    ///
    /// * `index`: The index of the element to retrieve.
    ///
    /// # Returns
    ///
    /// - `Some(T::Inner)` if the index is within bounds and the value is valid.
    /// - `None` if the index is out of bounds or the value is invalid.
    ///
    /// # Note
    ///
    /// This method combines bounds checking with null value handling.
    #[inline]
    fn vget(&self, index: usize) -> Option<T::Inner>
    where
        T: IsNone,
    {
        if index < self.len() {
            unsafe { self.uvget(index) }
        } else {
            None
        }
    }

    /// Applies a custom function to rolling windows of the vector.
    ///
    /// # Type Parameters
    ///
    /// - `U`: The type returned by the custom function for each window.
    /// - `F`: The type of the custom function.
    ///
    /// # Arguments
    ///
    /// * `window`: The size of the rolling window.
    /// * `f`: The custom function to apply to each window.
    ///
    /// # Returns
    ///
    /// An iterator over the results of applying the custom function to each window.
    ///
    /// # Note
    ///
    /// This method creates an iterator that doesn't collect results, making it memory-efficient
    /// for large datasets or when further processing of the results is needed.
    #[inline]
    fn rolling_custom_iter<'a, U, F>(&'a self, window: usize, mut f: F) -> impl TrustedLen<Item = U>
    where
        F: FnMut(Self::SliceOutput<'a>) -> U,
        T: 'a,
    {
        (1..self.len() + 1)
            .zip(std::iter::repeat_n(0, window - 1).chain(0..self.len()))
            .map(move |(end, start)| f(self.slice(start, end).unwrap()))
            .to_trust(self.len())
    }

    /// Applies a custom function to rolling windows of the vector and collects the results.
    ///
    /// # Type Parameters
    ///
    /// - `O`: The output vector type.
    /// - `OT`: The type of elements in the output vector.
    /// - `F`: The type of the custom function.
    ///
    /// # Arguments
    ///
    /// * `window`: The size of the rolling window.
    /// * `f`: The custom function to apply to each window.
    /// * `out`: An optional pre-allocated output buffer.
    ///
    /// # Returns
    ///
    /// - `Some(O)` if `out` is `None`, containing the collected results.
    /// - `None` if `out` is `Some`, in which case the results are written to the provided buffer.
    ///
    /// # Note
    ///
    /// This method allows for efficient in-place computation when an output buffer is provided.
    #[inline]
    fn rolling_custom<'a, O: Vec1<OT>, OT: Clone, F>(
        &'a self,
        window: usize,
        f: F,
        out: Option<O::UninitRefMut<'_>>,
    ) -> Option<O>
    where
        F: FnMut(Self::SliceOutput<'a>) -> OT,
        Self: 'a,
        T: 'a,
    {
        let iter = self.rolling_custom_iter(window, f);
        if let Some(mut out) = out {
            iter.write(&mut out).unwrap();
            None
        } else {
            Some(iter.collect_trusted_vec1())
        }
    }

    /// Applies a custom function to rolling windows of the vector and writes the results to a provided buffer.
    ///
    /// # Type Parameters
    ///
    /// - `O`: The output vector type.
    /// - `OT`: The type of elements in the output vector.
    /// - `F`: The type of the custom function.
    ///
    /// # Arguments
    ///
    /// * `window`: The size of the rolling window.
    /// * `f`: The custom function to apply to each window.
    /// * `out`: A mutable reference to an uninitialized output buffer.
    ///
    /// # Note
    ///
    /// This method is more efficient than `rolling_custom` when you have a pre-allocated buffer,
    /// but it may panic with certain backends (e.g., Polars). Use `rolling_custom` for a safer alternative.
    #[inline]
    fn rolling_custom_to<'a, O: Vec1<OT>, OT, F>(
        &'a self,
        window: usize,
        mut f: F,
        mut out: O::UninitRefMut<'_>,
    ) where
        F: FnMut(Self::SliceOutput<'a>) -> OT,
        Self: 'a,
        T: 'a,
    {
        let len = self.len();
        let window = window.min(len);
        if window == 0 {
            return;
        }
        // within the first window
        for i in 0..window - 1 {
            unsafe {
                let slice = self.uslice(0, i + 1).unwrap();
                out.uset(i, f(slice))
            }
        }
        // other windows
        for (start, end) in (window - 1..len).enumerate() {
            unsafe {
                let slice = self.uslice(start, end + 1).unwrap();
                out.uset(end, f(slice))
            }
        }
    }

    /// Applies a custom function to rolling windows of two vectors simultaneously.
    ///
    /// # Type Parameters
    ///
    /// - `O`: The output vector type.
    /// - `OT`: The type of elements in the output vector.
    /// - `V2`: The type of the second input vector.
    /// - `T2`: The type of elements in the second input vector.
    /// - `F`: The type of the custom function.
    ///
    /// # Arguments
    ///
    /// * `other`: A reference to the second input vector.
    /// * `window`: The size of the rolling window.
    /// * `f`: The custom function to apply to each pair of windows.
    /// * `out`: An optional pre-allocated output buffer.
    ///
    /// # Returns
    ///
    /// - `Some(O)` if `out` is `None`, containing the collected results.
    /// - `None` if `out` is `Some`, in which case the results are written to the provided buffer.
    ///
    /// # Note
    ///
    /// This method is useful for operations that need to consider two vectors simultaneously,
    /// such as computing rolling correlations or differences between two time series.
    #[inline]
    fn rolling2_custom<O: Vec1<OT>, OT: Clone, V2, T2, F>(
        &self,
        other: &V2,
        window: usize,
        mut f: F,
        out: Option<O::UninitRefMut<'_>>,
    ) -> Option<O>
    where
        V2: Vec1View<T2>,
        F: FnMut(Self::SliceOutput<'_>, V2::SliceOutput<'_>) -> OT,
    {
        let iter = (1..self.len() + 1)
            .zip(std::iter::repeat_n(0, window - 1).chain(0..self.len()))
            .map(|(end, start)| unsafe {
                f(
                    self.uslice(start, end).unwrap(),
                    other.uslice(start, end).unwrap(),
                )
            });
        if let Some(mut out) = out {
            // TODO: maybe we should return a result here?
            iter.write(&mut out).unwrap();
            None
        } else {
            Some(iter.collect_trusted_vec1())
        }
    }

    /// Applies a rolling function that considers both the removal and addition of elements in the window.
    ///
    /// # Type Parameters
    ///
    /// - `O`: The output vector type.
    /// - `OT`: The type of elements in the output vector.
    /// - `F`: The type of the rolling function.
    ///
    /// # Arguments
    ///
    /// * `window`: The size of the rolling window.
    /// * `f`: The function to apply. It takes an `Option<T>` (the element being removed, if any)
    ///   and a `T` (the element being added).
    /// * `out`: An optional pre-allocated output buffer.
    ///
    /// # Returns
    ///
    /// - `Some(O)` if `out` is `None`, containing the collected results.
    /// - `None` if `out` is `Some`, in which case the results are written to the provided buffer.
    ///
    /// # Note
    ///
    /// This method is particularly useful for implementing efficient rolling calculations
    /// where you can update the result based on the elements entering and leaving the window,
    /// rather than recomputing from scratch for each window.
    #[inline]
    fn rolling_apply<O: Vec1<OT>, OT, F>(
        &self,
        window: usize,
        mut f: F,
        out: Option<O::UninitRefMut<'_>>,
    ) -> Option<O>
    where
        T: Clone,
        F: FnMut(Option<T>, T) -> OT,
    {
        if let Some(out) = out {
            self.rolling_apply_to::<O, _, _>(window, f, out);
            None
        } else {
            assert!(window > 0, "window must be greater than 0");
            let remove_value_iter =
                std::iter::repeat_n(None, window - 1).chain(self.titer().map(Some));
            Some(
                remove_value_iter
                    .zip(self.titer())
                    .map(move |(v_remove, v)| f(v_remove, v))
                    .collect_trusted_vec1(),
            )
        }
    }

    /// Applies a rolling function that considers both the removal and addition of elements in the window,
    /// writing results to a provided buffer.
    ///
    /// # Type Parameters
    ///
    /// - `O`: The output vector type.
    /// - `OT`: The type of elements in the output vector.
    /// - `F`: The type of the rolling function.
    ///
    /// # Arguments
    ///
    /// * `window`: The size of the rolling window.
    /// * `f`: The function to apply. It takes an `Option<T>` (the element being removed, if any)
    ///   and a `T` (the element being added).
    /// * `out`: A mutable reference to an uninitialized buffer to store the results.
    ///
    /// # Behavior
    ///
    /// This method applies a rolling function to the vector, considering both elements
    /// entering and leaving the window. It writes the results directly to the provided
    /// output buffer.
    ///
    /// # Safety
    ///
    /// This method uses unsafe operations for performance reasons. It assumes that:
    /// - The `window` size is valid (greater than 0 and not larger than the vector's length).
    /// - The `out` buffer has sufficient capacity to store the results.
    ///
    /// # Note
    ///
    /// This method is more efficient than `rolling_apply` when you have a pre-allocated
    /// buffer. However, it may not be supported by all backends (e.g., it will panic
    /// in the Polars backend). For broader compatibility, use `rolling_apply` instead.
    #[inline]
    fn rolling_apply_to<O: Vec1<OT>, OT, F>(
        &self,
        window: usize,
        mut f: F,
        mut out: O::UninitRefMut<'_>,
    ) where
        T: Clone,
        F: FnMut(Option<T>, T) -> OT,
    {
        let len = self.len();
        let window = window.min(len);
        if window == 0 {
            return;
        }
        // within the first window
        for i in 0..window - 1 {
            unsafe {
                // no value should be removed in the first window
                out.uset(i, f(None, self.uget(i)))
            }
        }
        // other windows
        for (start, end) in (window - 1..len).enumerate() {
            unsafe {
                // new valid value
                let (v_rm, v) = (self.uget(start), self.uget(end));
                out.uset(end, f(Some(v_rm), v))
            }
        }
    }

    /// Applies a rolling function to two vectors simultaneously, considering both
    /// the removal and addition of elements in the window.
    ///
    /// # Type Parameters
    ///
    /// - `O`: The output vector type.
    /// - `OT`: The type of elements in the output vector.
    /// - `V2`: The type of the second input vector.
    /// - `T2`: The type of elements in the second input vector.
    /// - `F`: The type of the rolling function.
    ///
    /// # Arguments
    ///
    /// * `other`: A reference to the second input vector.
    /// * `window`: The size of the rolling window.
    /// * `f`: The function to apply. It takes an `Option<(T, T2)>` (the elements being removed, if any)
    ///   and a `(T, T2)` (the elements being added).
    /// * `out`: An optional mutable reference to an uninitialized buffer to store the results.
    ///
    /// # Returns
    ///
    /// - `Some(O)` if `out` is `None`, containing the collected results.
    /// - `None` if `out` is `Some`, in which case the results are written to the provided buffer.
    ///
    /// # Note
    ///
    /// This method is particularly useful for implementing efficient rolling calculations
    /// that depend on two input vectors simultaneously.
    #[inline]
    fn rolling2_apply<O: Vec1<OT>, OT, V2: Vec1View<T2>, T2, F>(
        &self,
        other: &V2,
        window: usize,
        mut f: F,
        out: Option<O::UninitRefMut<'_>>,
    ) -> Option<O>
    where
        T: Clone,
        T2: Clone,
        F: FnMut(Option<(T, T2)>, (T, T2)) -> OT,
    {
        if let Some(out) = out {
            self.rolling2_apply_to::<O, _, _, _, _>(other, window, f, out);
            None
        } else {
            assert!(window > 0, "window must be greater than 0");
            let remove_value_iter = std::iter::repeat_n(None, window - 1)
                .chain(self.titer().zip(other.titer()).map(Some));
            Some(
                remove_value_iter
                    .zip(self.titer().zip(other.titer()))
                    .map(move |(v_remove, v)| f(v_remove, v))
                    .collect_trusted_vec1(),
            )
        }
    }

    /// Applies a rolling function to two vectors simultaneously, writing results to a provided buffer.
    ///
    /// # Type Parameters
    ///
    /// - `O`: The output vector type.
    /// - `OT`: The type of elements in the output vector.
    /// - `V2`: The type of the second input vector.
    /// - `T2`: The type of elements in the second input vector.
    /// - `F`: The type of the rolling function.
    ///
    /// # Arguments
    ///
    /// * `other`: A reference to the second input vector.
    /// * `window`: The size of the rolling window.
    /// * `f`: The function to apply. It takes an `Option<(T, T2)>` (the elements being removed, if any)
    ///   and a `(T, T2)` (the elements being added).
    /// * `out`: A mutable reference to an uninitialized buffer to store the results.
    ///
    /// # Safety
    ///
    /// This method uses unsafe operations for performance reasons. It assumes that:
    /// - The `window` size is valid (greater than 0 and not larger than the vector's length).
    /// - The `out` buffer has sufficient capacity to store the results.
    /// - Both input vectors have the same length.
    ///
    /// # Note
    ///
    /// This method is more efficient than `rolling2_apply` when you have a pre-allocated
    /// buffer. However, it may not be supported by all backends (e.g., it will panic
    /// in the Polars backend). For broader compatibility, use `rolling2_apply` instead.
    #[inline]
    fn rolling2_apply_to<O: Vec1<OT>, OT, V2: Vec1View<T2>, T2, F>(
        &self,
        other: &V2,
        window: usize,
        mut f: F,
        mut out: O::UninitRefMut<'_>,
    ) where
        F: FnMut(Option<(T, T2)>, (T, T2)) -> OT,
    {
        let len = self.len();
        let window = window.min(len);
        if window == 0 {
            return;
        }
        // within the first window
        for i in 0..window - 1 {
            unsafe {
                // no value should be removed in the first window
                out.uset(i, f(None, (self.uget(i), other.uget(i))))
            }
        }
        // other windows
        for (start, end) in (window - 1..len).enumerate() {
            unsafe {
                // new valid value
                let (v1_rm, v1) = (self.uget(start), self.uget(end));
                let (v2_rm, v2) = (other.uget(start), other.uget(end));
                out.uset(end, f(Some((v1_rm, v2_rm)), (v1, v2)))
            }
        }
    }

    /// Applies a rolling function that considers the index of elements in the window.
    ///
    /// # Type Parameters
    ///
    /// - `O`: The output vector type.
    /// - `OT`: The type of elements in the output vector.
    /// - `F`: The type of the rolling function.
    ///
    /// # Arguments
    ///
    /// * `window`: The size of the rolling window.
    /// * `f`: The function to apply. It takes `Option<usize>` (the start index),
    ///   `usize` (the end index), and `T` (the current element).
    /// * `out`: An optional mutable reference to an uninitialized buffer to store the results.
    ///
    /// # Returns
    ///
    /// - `Some(O)` if `out` is `None`, containing the collected results.
    /// - `None` if `out` is `Some`, in which case the results are written to the provided buffer.
    ///
    /// # Note
    ///
    /// This method is useful when the rolling calculation needs to consider the
    /// position of elements within the vector.
    #[inline]
    fn rolling_apply_idx<O: Vec1<OT>, OT, F>(
        &self,
        window: usize,
        mut f: F,
        out: Option<O::UninitRefMut<'_>>,
    ) -> Option<O>
    where
        // start, end, value
        F: FnMut(Option<usize>, usize, T) -> OT,
    {
        if let Some(out) = out {
            self.rolling_apply_idx_to::<O, _, _>(window, f, out);
            None
        } else {
            assert!(window > 0, "window must be greater than 0");
            let start_iter = std::iter::repeat_n(None, window - 1).chain((0..self.len()).map(Some)); // this is longer than expect, but start_iter will stop earlier
            Some(
                self.titer()
                    .zip(start_iter)
                    .enumerate()
                    .map(move |(end, (v, start))| f(start, end, v))
                    .collect_trusted_vec1(),
            )
        }
    }

    /// Applies a rolling function that considers the index of elements in the window,
    /// writing results to a provided buffer.
    ///
    /// # Type Parameters
    ///
    /// - `O`: The output vector type.
    /// - `OT`: The type of elements in the output vector.
    /// - `F`: The type of the rolling function.
    ///
    /// # Arguments
    ///
    /// * `window`: The size of the rolling window.
    /// * `f`: The function to apply. It takes `Option<usize>` (the start index),
    ///   `usize` (the end index), and `T` (the current element).
    /// * `out`: A mutable reference to an uninitialized buffer to store the results.
    ///
    /// # Safety
    ///
    /// This method uses unsafe operations for performance reasons. It assumes that:
    /// - The `window` size is valid (greater than 0 and not larger than the vector's length).
    /// - The `out` buffer has sufficient capacity to store the results.
    ///
    /// # Note
    ///
    /// This method is more efficient than `rolling_apply_idx` when you have a pre-allocated
    /// buffer. However, it may not be supported by all backends (e.g., it will panic
    /// in the Polars backend). For broader compatibility, use `rolling_apply_idx` instead.
    #[inline]
    fn rolling_apply_idx_to<O: Vec1<OT>, OT, F>(
        &self,
        window: usize,
        mut f: F,
        mut out: O::UninitRefMut<'_>,
    ) where
        // start, end, value
        F: FnMut(Option<usize>, usize, T) -> OT,
    {
        let len = self.len();
        let window = window.min(len);
        if window == 0 {
            return;
        }
        // within the first window
        for i in 0..window - 1 {
            unsafe {
                // no value should be removed in the first window
                out.uset(i, f(None, i, self.uget(i)))
            }
        }
        // other windows
        for (start, end) in (window - 1..len).enumerate() {
            unsafe { out.uset(end, f(Some(start), end, self.uget(end))) }
        }
    }

    /// Applies a rolling function to two vectors simultaneously, considering the index of elements in the window.
    ///
    /// # Type Parameters
    ///
    /// - `O`: The output vector type.
    /// - `OT`: The type of elements in the output vector.
    /// - `V2`: The type of the second input vector.
    /// - `T2`: The type of elements in the second input vector.
    /// - `F`: The type of the rolling function.
    ///
    /// # Arguments
    ///
    /// * `other`: A reference to the second input vector.
    /// * `window`: The size of the rolling window.
    /// * `f`: The function to apply. It takes `Option<usize>` (the start index),
    ///   `usize` (the end index), and `(T, T2)` (the current elements from both vectors).
    /// * `out`: An optional mutable reference to an uninitialized buffer to store the results.
    ///
    /// # Returns
    ///
    /// - `Some(O)` if `out` is `None`, containing the collected results.
    /// - `None` if `out` is `Some`, in which case the results are written to the provided buffer.
    ///
    /// # Note
    ///
    /// This method is useful when the rolling calculation needs to consider both
    /// the position of elements and values from two input vectors simultaneously.
    #[inline]
    fn rolling2_apply_idx<O: Vec1<OT>, OT, V2: Vec1View<T2>, T2, F>(
        &self,
        other: &V2,
        window: usize,
        mut f: F,
        out: Option<O::UninitRefMut<'_>>,
    ) -> Option<O>
    where
        // start, end, value
        F: FnMut(Option<usize>, usize, (T, T2)) -> OT,
    {
        if let Some(out) = out {
            self.rolling2_apply_idx_to::<O, _, _, _, _>(other, window, f, out);
            None
        } else {
            assert!(window > 0, "window must be greater than 0");
            let start_iter = std::iter::repeat_n(None, window - 1).chain((0..self.len()).map(Some)); // this is longer than expect, but start_iter will stop earlier
            Some(
                self.titer()
                    .zip(other.titer())
                    .zip(start_iter)
                    .enumerate()
                    .map(move |(end, ((v, v2), start))| f(start, end, (v, v2)))
                    .collect_trusted_vec1(),
            )
        }
    }

    /// Applies a rolling function to two vectors simultaneously, considering the index of elements in the window,
    /// writing results to a provided buffer.
    ///
    /// # Type Parameters
    ///
    /// - `O`: The output vector type.
    /// - `OT`: The type of elements in the output vector.
    /// - `V2`: The type of the second input vector.
    /// - `T2`: The type of elements in the second input vector.
    /// - `F`: The type of the rolling function.
    ///
    /// # Arguments
    ///
    /// * `other`: A reference to the second input vector.
    /// * `window`: The size of the rolling window.
    /// * `f`: The function to apply. It takes `Option<usize>` (the start index of the window),
    ///   `usize` (the end index of the window), and `(T, T2)` (the current elements from both vectors).
    /// * `out`: A mutable reference to an uninitialized buffer to store the results.
    ///
    /// # Behavior
    ///
    /// This method applies a rolling function to two vectors simultaneously, considering both
    /// the position of elements and values from both input vectors. It writes the results directly
    /// to the provided output buffer.
    ///
    /// # Safety
    ///
    /// This method uses unsafe operations for performance reasons. It assumes that:
    /// - The `window` size is valid (greater than 0 and not larger than the vector's length).
    /// - The `out` buffer has sufficient capacity to store the results.
    /// - Both input vectors have the same length.
    ///
    /// # Note
    ///
    /// This method is more efficient than `rolling2_apply_idx` when you have a pre-allocated
    /// buffer. However, it may not be supported by all backends (e.g., it will panic
    /// in the Polars backend). For broader compatibility, use `rolling2_apply_idx` instead.
    #[inline]
    fn rolling2_apply_idx_to<O: Vec1<OT>, OT, V2: Vec1View<T2>, T2, F>(
        &self,
        other: &V2,
        window: usize,
        mut f: F,
        mut out: O::UninitRefMut<'_>,
    ) where
        F: FnMut(Option<usize>, usize, (T, T2)) -> OT,
    {
        let len = self.len();
        let window = window.min(len);
        if window == 0 {
            return;
        }
        // within the first window
        for i in 0..window - 1 {
            unsafe {
                // no value should be removed in the first window
                out.uset(i, f(None, i, (self.uget(i), other.uget(i))))
            }
        }
        // other windows
        for (start, end) in (window - 1..len).enumerate() {
            unsafe { out.uset(end, f(Some(start), end, (self.uget(end), other.uget(end)))) }
        }
    }
}