Skip to content

Commit 23782e7

Browse files
committed
Also protect non-bulk version from stack overflow.
1 parent 78d334b commit 23782e7

File tree

2 files changed

+22
-14
lines changed

2 files changed

+22
-14
lines changed

src/sort.rs

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -200,18 +200,24 @@ where
200200
let (lower_index, upper_index) =
201201
self.dual_partition_mut(sample[lower_index], sample[upper_index]);
202202
if i < lower_index {
203-
self.slice_axis_mut(Axis(0), Slice::from(..lower_index))
204-
.get_from_sorted_mut(i)
203+
maybe_grow(RED_ZONE, STACK_SIZE, || {
204+
self.slice_axis_mut(Axis(0), Slice::from(..lower_index))
205+
.get_from_sorted_mut(i)
206+
})
205207
} else if i == lower_index {
206208
self[i].clone()
207209
} else if i < upper_index {
208-
self.slice_axis_mut(Axis(0), Slice::from(lower_index + 1..upper_index))
209-
.get_from_sorted_mut(i - (lower_index + 1))
210+
maybe_grow(RED_ZONE, STACK_SIZE, || {
211+
self.slice_axis_mut(Axis(0), Slice::from(lower_index + 1..upper_index))
212+
.get_from_sorted_mut(i - (lower_index + 1))
213+
})
210214
} else if i == upper_index {
211215
self[i].clone()
212216
} else {
213-
self.slice_axis_mut(Axis(0), Slice::from(upper_index + 1..))
214-
.get_from_sorted_mut(i - (upper_index + 1))
217+
maybe_grow(RED_ZONE, STACK_SIZE, || {
218+
self.slice_axis_mut(Axis(0), Slice::from(upper_index + 1..))
219+
.get_from_sorted_mut(i - (upper_index + 1))
220+
})
215221
}
216222
} else {
217223
let pivot_index = if sought_rank <= 0.5 {
@@ -221,13 +227,17 @@ where
221227
};
222228
let pivot_index = self.partition_mut(sample[pivot_index]);
223229
if i < pivot_index {
224-
self.slice_axis_mut(Axis(0), Slice::from(..pivot_index))
225-
.get_from_sorted_mut(i)
230+
maybe_grow(RED_ZONE, STACK_SIZE, || {
231+
self.slice_axis_mut(Axis(0), Slice::from(..pivot_index))
232+
.get_from_sorted_mut(i)
233+
})
226234
} else if i == pivot_index {
227235
self[i].clone()
228236
} else {
229-
self.slice_axis_mut(Axis(0), Slice::from(pivot_index + 1..))
230-
.get_from_sorted_mut(i - (pivot_index + 1))
237+
maybe_grow(RED_ZONE, STACK_SIZE, || {
238+
self.slice_axis_mut(Axis(0), Slice::from(pivot_index + 1..))
239+
.get_from_sorted_mut(i - (pivot_index + 1))
240+
})
231241
}
232242
}
233243
}
@@ -363,9 +373,7 @@ where
363373
// Since `!indexes.is_empty()` and indexes must be in-bounds, `array` must
364374
// be non-empty.
365375
let mut values = vec![array[0].clone(); indexes.len()];
366-
maybe_grow(RED_ZONE, STACK_SIZE, || {
367-
_get_many_from_sorted_mut_unchecked(array.view_mut(), &mut indexes.to_owned(), &mut values);
368-
});
376+
_get_many_from_sorted_mut_unchecked(array.view_mut(), &mut indexes.to_owned(), &mut values);
369377

370378
// We convert the vector to a more search-friendly `IndexMap`.
371379
indexes.iter().cloned().zip(values.into_iter()).collect()

tests/sort.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ fn test_dual_partition_mut() {
6767

6868
#[test]
6969
fn test_quantile_mut_with_large_array_of_equal_floats() {
70-
let mut array: Array1<N64> = Array1::ones(100000);
70+
let mut array: Array1<N64> = Array1::ones(100_000);
7171
array.quantile_mut(n64(0.5), &Linear).unwrap();
7272
}
7373

0 commit comments

Comments
 (0)