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
use std::fmt::Debug;
use std::ops::Sub;
use num_traits::Bounded;
use polars_arrow::index::IdxSize;
pub(super) fn join_asof_forward_with_tolerance<T: PartialOrd + Copy + Debug + Sub<Output = T>>(
left: &[T],
right: &[T],
tolerance: T,
) -> Vec<Option<IdxSize>> {
if right.is_empty() {
return vec![None; left.len()];
}
if left.is_empty() {
return vec![];
}
let mut out = Vec::with_capacity(left.len());
let mut offset = 0 as IdxSize;
for &val_l in left {
loop {
match right.get(offset as usize) {
Some(&val_r) => {
if val_r >= val_l {
let dist = val_r - val_l;
let value = if dist > tolerance { None } else { Some(offset) };
out.push(value);
break;
}
offset += 1;
}
None => {
out.extend(std::iter::repeat(None).take(left.len() - out.len()));
return out;
}
}
}
}
out
}
pub(super) fn join_asof_backward_with_tolerance<T>(
left: &[T],
right: &[T],
tolerance: T,
) -> Vec<Option<IdxSize>>
where
T: PartialOrd + Copy + Debug + Sub<Output = T>,
{
if right.is_empty() {
return vec![None; left.len()];
}
if left.is_empty() {
return vec![];
}
let mut out = Vec::with_capacity(left.len());
let mut offset = 0 as IdxSize;
// left array could start lower than right;
// left: [-1, 0, 1, 2],
// right: [1, 2, 3]
// first values should be None, until left has caught up
let mut left_caught_up = false;
// init with left so that the distance starts at 0
let mut previous_right = left[0];
let mut dist;
for &val_l in left {
loop {
dist = val_l - previous_right;
match right.get(offset as usize) {
Some(&val_r) => {
// we fill nulls until left value is larger than right
if !left_caught_up {
if val_l < val_r {
out.push(None);
break;
} else {
left_caught_up = true;
}
}
// right is larger than left.
// we take the last value before that
if val_r > val_l {
let value = if dist > tolerance {
None
} else {
Some(offset - 1)
};
out.push(value);
break;
}
// right still smaller or equal to left
// continue looping the right side
else {
previous_right = val_r;
offset += 1;
}
}
// we depleted the right array
// we cannot fill the remainder of the value, because we need to check tolerances
None => {
// if we have previous value, continue with that one
let val = if left_caught_up && dist <= tolerance {
Some(offset - 1)
}
// else null
else {
None
};
out.push(val);
break;
}
}
}
}
out
}
pub(super) fn join_asof_backward<T: PartialOrd + Copy + Debug>(
left: &[T],
right: &[T],
) -> Vec<Option<IdxSize>> {
let mut out = Vec::with_capacity(left.len());
let mut offset = 0 as IdxSize;
// left array could start lower than right;
// left: [-1, 0, 1, 2],
// right: [1, 2, 3]
// first values should be None, until left has caught up
let mut left_caught_up = false;
for &val_l in left {
loop {
match right.get(offset as usize) {
Some(&val_r) => {
// we fill nulls until left value is larger than right
if !left_caught_up {
if val_l < val_r {
out.push(None);
break;
} else {
left_caught_up = true;
}
}
// right is larger than left.
// we take the last value before that
if val_r > val_l {
out.push(Some(offset - 1));
break;
}
// right still smaller or equal to left
// continue looping the right side
else {
offset += 1;
}
}
// we depleted the right array
None => {
// if we have previous value, continue with that one
let val = if left_caught_up {
Some(offset - 1)
}
// else all null
else {
None
};
out.extend(std::iter::repeat(val).take(left.len() - out.len()));
return out;
}
}
}
}
out
}
pub(super) fn join_asof_nearest<T: PartialOrd + Copy + Debug + Sub<Output = T> + Bounded>(
left: &[T],
right: &[T],
) -> Vec<Option<IdxSize>> {
let mut out = Vec::with_capacity(left.len());
let mut offset = 0 as IdxSize;
let max_value = <T as num_traits::Bounded>::max_value();
let mut dist: T = max_value;
for &val_l in left {
loop {
match right.get(offset as usize) {
Some(&val_r) => {
// This is (val_r - val_l).abs(), but works on strings/dates
let dist_curr = if val_r > val_l {
val_r - val_l
} else {
val_l - val_r
};
if dist_curr <= dist {
// candidate for match
dist = dist_curr;
offset += 1;
} else {
// distance has increased, we're now farther away, so previous element was closest
out.push(Some(offset - 1));
// reset distance
dist = max_value;
// The next left-item may match on the same item, so we need to rewind the offset
offset -= 1;
break;
}
}
None => {
if offset > 1 {
// we've reached the end with no matches, so the last item is the nearest for all remaining
out.extend(
std::iter::repeat(Some(offset - 1)).take(left.len() - out.len()),
);
} else {
// this is only hit when the right frame is empty
out.extend(std::iter::repeat(None).take(left.len() - out.len()));
}
return out;
}
}
}
}
out
}
pub(super) fn join_asof_forward<T: PartialOrd + Copy + Debug>(
left: &[T],
right: &[T],
) -> Vec<Option<IdxSize>> {
let mut out = Vec::with_capacity(left.len());
let mut offset = 0 as IdxSize;
for &val_l in left {
loop {
match right.get(offset as usize) {
Some(&val_r) => {
if val_r >= val_l {
out.push(Some(offset));
break;
}
offset += 1;
}
None => {
out.extend(std::iter::repeat(None).take(left.len() - out.len()));
return out;
}
}
}
}
out
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_asof_backward() {
let a = [-1, 2, 3, 3, 3, 4];
let b = [1, 2, 3, 3];
let tuples = join_asof_backward(&a, &b);
assert_eq!(tuples.len(), a.len());
assert_eq!(tuples, &[None, Some(1), Some(3), Some(3), Some(3), Some(3)]);
let b = [1, 2, 4, 5];
let tuples = join_asof_backward(&a, &b);
assert_eq!(tuples, &[None, Some(1), Some(1), Some(1), Some(1), Some(2)]);
let a = [2, 4, 4, 4];
let b = [1, 2, 3, 3];
let tuples = join_asof_backward(&a, &b);
assert_eq!(tuples, &[Some(1), Some(3), Some(3), Some(3)]);
}
#[test]
fn test_asof_backward_tolerance() {
let a = [-1, 20, 25, 30, 30, 40];
let b = [10, 20, 30, 30];
let tuples = join_asof_backward_with_tolerance(&a, &b, 4);
assert_eq!(tuples, &[None, Some(1), None, Some(3), Some(3), None]);
}
#[test]
fn test_asof_forward_tolerance() {
let a = [-1, 20, 25, 30, 30, 40, 52];
let b = [10, 20, 33, 55];
let tuples = join_asof_forward_with_tolerance(&a, &b, 4);
assert_eq!(
tuples,
&[None, Some(1), None, Some(2), Some(2), None, Some(3)]
);
}
#[test]
fn test_asof_forward() {
let a = [-1, 1, 2, 4, 6];
let b = [1, 2, 4, 5];
let tuples = join_asof_forward(&a, &b);
assert_eq!(tuples.len(), a.len());
assert_eq!(tuples, &[Some(0), Some(0), Some(1), Some(2), None]);
}
}