Skip to content

Commit

Permalink
catch a capacity error in keep
Browse files Browse the repository at this point in the history
  • Loading branch information
kaikalii committed Jun 3, 2024
1 parent 4b158b0 commit a00f179
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions src/algorithm/dyadic/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -517,7 +517,8 @@ impl Value {

impl<T: Clone + Send + Sync> Array<T> {
/// `keep` this array by replicating it as the rows of a new array
pub fn keep_scalar_integer(mut self, count: usize) -> Self {
pub fn keep_scalar_integer(mut self, count: usize, env: &Uiua) -> UiuaResult<Self> {
let elem_count = validate_size::<T>([count, self.data.len()], env)?;
// Scalar kept
if self.rank() == 0 {
self.shape.push(count);
Expand All @@ -528,9 +529,9 @@ impl<T: Clone + Send + Sync> Array<T> {
.extend_from_trusted((0..count).map(|_| value.clone()))
};
self.validate_shape();
return self;
return Ok(self);
}
match count {
Ok(match count {
// Keep nothing
0 => {
self.data = CowSlice::new();
Expand All @@ -541,7 +542,7 @@ impl<T: Clone + Send + Sync> Array<T> {
1 => self,
// Keep ≥2 is a repeat
_ => {
let mut new_data = EcoVec::with_capacity(count * self.data.len());
let mut new_data = EcoVec::with_capacity(elem_count);
for row in self.row_slices() {
for _ in 0..count {
new_data.extend_from_slice(row);
Expand All @@ -552,7 +553,7 @@ impl<T: Clone + Send + Sync> Array<T> {
self.validate_shape();
self
}
}
})
}
}

Expand All @@ -563,9 +564,10 @@ impl<T: ArrayValue> Array<T> {
return Err(env.error("Keep amount cannot be negative"));
}
if count.fract() == 0.0 {
return Ok(self.keep_scalar_integer(count as usize));
return self.keep_scalar_integer(count as usize, env);
}
let new_row_count = (count * self.row_count() as f64).round() as usize;
let new_row_count =
validate_size::<T>([(count * self.row_count() as f64).round() as usize], env)?;
let row_len = self.row_len();
let mut new_data = EcoVec::with_capacity(new_row_count * row_len);
let delta = 1.0 / count;
Expand Down

0 comments on commit a00f179

Please sign in to comment.