Skip to content

Commit f09fb59

Browse files
committed
validate size in some cases of stencil
1 parent 3fd7c39 commit f09fb59

File tree

1 file changed

+27
-18
lines changed

1 file changed

+27
-18
lines changed

src/algorithm/stencil.rs

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::{collections::VecDeque, mem::take};
33
use ecow::EcoVec;
44

55
use crate::{
6-
algorithm::{pervade::*, FillContext, MultiOutput},
6+
algorithm::{pervade::*, validate_size, FillContext, MultiOutput},
77
cowslice::extend_repeat,
88
val_as_arr, Array, ArrayValue, Boxed, Node, Primitive, Shape, SigNode, Uiua, UiuaResult, Value,
99
};
@@ -78,8 +78,33 @@ where
7878
Default(MultiOutput<Vec<Value>>, EcoVec<T>),
7979
}
8080

81+
// Determine shape stuff
82+
let mut shape_prefix = Shape::SCALAR;
83+
for (d, s) in dims.iter().zip(&arr.shape) {
84+
let total_len = *s + 2 * d.fill * d.stride;
85+
shape_prefix.push((total_len + d.stride).saturating_sub(d.size) / d.stride);
86+
}
87+
let window_shape = Shape::from_iter(
88+
dims.iter()
89+
.map(|d| d.size)
90+
.chain(arr.shape.iter().skip(dims.len()).copied()),
91+
);
92+
if shape_prefix.contains(&0) {
93+
let mut shape = shape_prefix;
94+
shape.extend(window_shape);
95+
env.push(Array::new(shape, EcoVec::new()));
96+
return Ok(());
97+
}
98+
99+
// Initialize action
81100
let mut action: WindowAction<T> = match &f.node {
82-
Node::Prim(Primitive::Identity, _) => WindowAction::Id(EcoVec::new()),
101+
Node::Prim(Primitive::Identity, _) => {
102+
let size = validate_size::<T>(
103+
(shape_prefix.iter().copied()).chain(window_shape.iter().copied()),
104+
env,
105+
)?;
106+
WindowAction::Id(EcoVec::with_capacity(size))
107+
}
83108
Node::Prim(Primitive::Box, _) => WindowAction::Box(EcoVec::new(), EcoVec::new()),
84109
_ => WindowAction::Default(multi_output(f.sig.outputs, Vec::new()), EcoVec::new()),
85110
};
@@ -143,22 +168,6 @@ where
143168
let mut corner = corner_starts.clone();
144169
let mut curr = corner.clone();
145170
let mut offset = vec![0usize; corner.len()];
146-
let mut shape_prefix = Shape::SCALAR;
147-
for (d, s) in dims.iter().zip(&arr.shape) {
148-
let total_len = *s + 2 * d.fill * d.stride;
149-
shape_prefix.push((total_len + d.stride).saturating_sub(d.size) / d.stride);
150-
}
151-
let window_shape = Shape::from_iter(
152-
dims.iter()
153-
.map(|d| d.size)
154-
.chain(arr.shape.iter().skip(dims.len()).copied()),
155-
);
156-
if shape_prefix.contains(&0) {
157-
let mut shape = shape_prefix;
158-
shape.extend(window_shape);
159-
env.push(Array::new(shape, EcoVec::new()));
160-
return Ok(());
161-
}
162171
let cell_shape = Shape::from(&arr.shape[dims.len()..]);
163172
let cell_len = cell_shape.elements();
164173
let fill = fill.unwrap_or_else(T::proxy);

0 commit comments

Comments
 (0)