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
use polars_utils::total_ord::TotalOrd;
use super::*;
pub(super) struct SortedBuf<'a, T: NativeType> {
// slice over which the window slides
slice: &'a [T],
last_start: usize,
last_end: usize,
// values within the window that we keep sorted
buf: Vec<T>,
}
impl<'a, T: NativeType> SortedBuf<'a, T> {
pub(super) fn new(slice: &'a [T], start: usize, end: usize) -> Self {
let mut buf = slice[start..end].to_vec();
buf.sort_by(TotalOrd::tot_cmp);
Self {
slice,
last_start: start,
last_end: end,
buf,
}
}
/// Update the window position by setting the `start` index and the `end` index.
///
/// # Safety
/// The caller must ensure that `start` and `end` are within bounds of `self.slice`
///
pub(super) unsafe fn update(&mut self, start: usize, end: usize) -> &[T] {
// swap the whole buffer
if start >= self.last_end {
self.buf.clear();
let new_window = self.slice.get_unchecked(start..end);
self.buf.extend_from_slice(new_window);
self.buf.sort_by(TotalOrd::tot_cmp);
} else {
// remove elements that should leave the window
for idx in self.last_start..start {
// SAFETY:
// we are in bounds
let val = self.slice.get_unchecked(idx);
// SAFETY:
// value is present in buf
let remove_idx = self
.buf
.binary_search_by(|a| a.tot_cmp(val))
.unwrap_unchecked();
// this is O(n) but we need a sorted window
self.buf.remove(remove_idx);
}
// insert elements that enter the window, but insert them sorted
for idx in self.last_end..end {
// SAFETY:
// we are in bounds
let val = *self.slice.get_unchecked(idx);
let insertion_idx = self
.buf
.binary_search_by(|a| a.tot_cmp(&val))
.unwrap_or_else(|insertion_idx| insertion_idx);
// this is O(n) but we need a sorted window
self.buf.insert(insertion_idx, val);
}
}
self.last_start = start;
self.last_end = end;
&self.buf
}
}
pub(super) struct SortedBufNulls<'a, T: NativeType> {
// slice over which the window slides
slice: &'a [T],
validity: &'a Bitmap,
last_start: usize,
last_end: usize,
// values within the window that we keep sorted
buf: Vec<Option<T>>,
pub null_count: usize,
}
impl<'a, T: NativeType> SortedBufNulls<'a, T> {
unsafe fn fill_and_sort_buf(&mut self, start: usize, end: usize) {
self.null_count = 0;
let iter = (start..end).map(|idx| {
if self.validity.get_bit_unchecked(idx) {
Some(*self.slice.get_unchecked(idx))
} else {
self.null_count += 1;
None
}
});
self.buf.clear();
self.buf.extend(iter);
self.buf.sort_by(TotalOrd::tot_cmp);
}
pub(super) unsafe fn new(
slice: &'a [T],
validity: &'a Bitmap,
start: usize,
end: usize,
) -> Self {
let buf = Vec::with_capacity(end - start);
// sort_opt_buf(&mut buf);
let mut out = Self {
slice,
validity,
last_start: start,
last_end: end,
buf,
null_count: 0,
};
out.fill_and_sort_buf(start, end);
out
}
/// Update the window position by setting the `start` index and the `end` index.
///
/// # Safety
/// The caller must ensure that `start` and `end` are within bounds of `self.slice`
///
pub(super) unsafe fn update(&mut self, start: usize, end: usize) -> (&[Option<T>], usize) {
// swap the whole buffer
if start >= self.last_end {
self.fill_and_sort_buf(start, end);
} else {
// remove elements that should leave the window
for idx in self.last_start..start {
// SAFETY:
// we are in bounds
let val = if self.validity.get_bit_unchecked(idx) {
Some(*self.slice.get_unchecked(idx))
} else {
self.null_count -= 1;
None
};
// SAFETY:
// value is present in buf
let remove_idx = self
.buf
.binary_search_by(|a| a.tot_cmp(&val))
.unwrap_unchecked();
// this is O(n) but we need a sorted window
self.buf.remove(remove_idx);
}
// insert elements that enter the window, but insert them sorted
for idx in self.last_end..end {
// SAFETY:
// we are in bounds
let val = if self.validity.get_bit_unchecked(idx) {
Some(*self.slice.get_unchecked(idx))
} else {
self.null_count += 1;
None
};
let insertion_idx = self
.buf
.binary_search_by(|a| a.tot_cmp(&val))
.unwrap_or_else(|insertion_idx| insertion_idx);
// this is O(n) but we need a sorted window
self.buf.insert(insertion_idx, val);
}
}
self.last_start = start;
self.last_end = end;
(&self.buf, self.null_count)
}
pub(super) fn is_valid(&self, min_periods: usize) -> bool {
((self.last_end - self.last_start) - self.null_count) >= min_periods
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_sorted_buf() {
unsafe {
let values = &[1, 3, 4, 6, 2, -1, 9];
let mut sorted_window = SortedBuf::new(values, 0, 3);
let window = sorted_window.update(1, 4);
assert_eq!(window, &[3, 4, 6]);
let window = sorted_window.update(2, 5);
assert_eq!(window, &[2, 4, 6]);
let window = sorted_window.update(3, 6);
assert_eq!(window, &[-1, 2, 6]);
let window = sorted_window.update(3, 7);
assert_eq!(window, &[-1, 2, 6, 9]);
let window = sorted_window.update(4, 7);
assert_eq!(window, &[-1, 2, 9]);
}
}
}