Skip to content

Commit

Permalink
remove tree extension
Browse files Browse the repository at this point in the history
  • Loading branch information
kaikalii committed Jun 17, 2024
1 parent 5495309 commit 941fd4d
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 296 deletions.
75 changes: 33 additions & 42 deletions src/algorithm/dyadic/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -462,33 +462,28 @@ impl Value {
impl Value {
/// Use this value as counts to `keep` another
pub fn keep(self, kept: Self, env: &Uiua) -> UiuaResult<Self> {
self.into_nums_with(
let counts = self.as_nums(
env,
"Keep amount must be a positive real number \
or list of natural numbers",
false,
|counts, shape| {
Ok(if shape.len() == 0 {
match kept {
Value::Num(a) => a.keep_scalar_real(counts[0], env)?.into(),
Value::Byte(a) => {
a.convert::<f64>().keep_scalar_real(counts[0], env)?.into()
}
Value::Complex(a) => a.keep_scalar_real(counts[0], env)?.into(),
Value::Char(a) => a.keep_scalar_real(counts[0], env)?.into(),
Value::Box(a) => a.keep_scalar_real(counts[0], env)?.into(),
}
} else {
match kept {
Value::Num(a) => a.keep_list(counts, env)?.into(),
Value::Byte(a) => a.keep_list(counts, env)?.into(),
Value::Complex(a) => a.keep_list(counts, env)?.into(),
Value::Char(a) => a.keep_list(counts, env)?.into(),
Value::Box(a) => a.keep_list(counts, env)?.into(),
}
})
},
)
)?;
Ok(if self.rank() == 0 {
match kept {
Value::Num(a) => a.keep_scalar_real(counts[0], env)?.into(),
Value::Byte(a) => a.convert::<f64>().keep_scalar_real(counts[0], env)?.into(),
Value::Complex(a) => a.keep_scalar_real(counts[0], env)?.into(),
Value::Char(a) => a.keep_scalar_real(counts[0], env)?.into(),
Value::Box(a) => a.keep_scalar_real(counts[0], env)?.into(),
}
} else {
match kept {
Value::Num(a) => a.keep_list(&counts, env)?.into(),
Value::Byte(a) => a.keep_list(&counts, env)?.into(),
Value::Complex(a) => a.keep_list(&counts, env)?.into(),
Value::Char(a) => a.keep_list(&counts, env)?.into(),
Value::Box(a) => a.keep_list(&counts, env)?.into(),
}
})
}
pub(crate) fn unkeep(self, env: &Uiua) -> UiuaResult<(Self, Self)> {
self.generic_into(
Expand All @@ -500,26 +495,22 @@ impl Value {
)
}
pub(crate) fn undo_keep(self, kept: Self, into: Self, env: &Uiua) -> UiuaResult<Self> {
self.into_nums_with_other(
kept,
let counts = self.as_nums(
env,
"Keep amount must be a natural number \
"Keep amount must be a positive real number \
or list of natural numbers",
false,
|counts, shape, kept| {
if shape.len() == 0 {
return Err(env.error("Cannot invert scalar keep"));
}
kept.generic_bin_into(
into,
|a, b| a.undo_keep(counts, b, env).map(Into::into),
|a, b| a.undo_keep(counts, b, env).map(Into::into),
|a, b| a.undo_keep(counts, b, env).map(Into::into),
|a, b| a.undo_keep(counts, b, env).map(Into::into),
|a, b| a.undo_keep(counts, b, env).map(Into::into),
|a, b| env.error(format!("Cannot unkeep {a} array with {b} array")),
)
},
)?;
if self.rank() == 0 {
return Err(env.error("Cannot invert scalar keep"));
}
kept.generic_bin_into(
into,
|a, b| a.undo_keep(&counts, b, env).map(Into::into),
|a, b| a.undo_keep(&counts, b, env).map(Into::into),
|a, b| a.undo_keep(&counts, b, env).map(Into::into),
|a, b| a.undo_keep(&counts, b, env).map(Into::into),
|a, b| a.undo_keep(&counts, b, env).map(Into::into),
|a, b| env.error(format!("Cannot unkeep {a} array with {b} array")),
)
}
}
Expand Down
60 changes: 24 additions & 36 deletions src/algorithm/dyadic/structure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,24 +79,18 @@ impl Value {
}
/// Use this array as an index to pick from another
pub fn pick(self, from: Self, env: &Uiua) -> UiuaResult<Self> {
self.into_ints_with(
env,
"Index must be an array of integers",
true,
|index_data, index_shape| {
Ok(match from {
Value::Num(a) => Value::Num(a.pick(index_shape, index_data, env)?),
Value::Byte(a) => op_bytes_retry_fill(
a,
|a| a.pick(index_shape, index_data, env).map(Into::into),
|a| a.pick(index_shape, index_data, env).map(Into::into),
)?,
Value::Complex(a) => Value::Complex(a.pick(index_shape, index_data, env)?),
Value::Char(a) => Value::Char(a.pick(index_shape, index_data, env)?),
Value::Box(a) => Value::Box(a.pick(index_shape, index_data, env)?),
})
},
)
let (index_shape, index_data) = self.as_shaped_indices(env)?;
Ok(match from {
Value::Num(a) => Value::Num(a.pick(index_shape, &index_data, env)?),
Value::Byte(a) => op_bytes_retry_fill(
a,
|a| a.pick(index_shape, &index_data, env).map(Into::into),
|a| a.pick(index_shape, &index_data, env).map(Into::into),
)?,
Value::Complex(a) => Value::Complex(a.pick(index_shape, &index_data, env)?),
Value::Char(a) => Value::Char(a.pick(index_shape, &index_data, env)?),
Value::Box(a) => Value::Box(a.pick(index_shape, &index_data, env)?),
})
}
pub(crate) fn undo_pick(self, index: Self, into: Self, env: &Uiua) -> UiuaResult<Self> {
let (idx_shape, index_data) = index.as_shaped_indices(env)?;
Expand Down Expand Up @@ -787,24 +781,18 @@ impl<T: ArrayValue> Array<T> {
impl Value {
/// Use this value to `select` from another
pub fn select(self, from: &Self, env: &Uiua) -> UiuaResult<Self> {
self.into_ints_with(
env,
"Index must be an array of integers",
true,
|indices_data, indices_shape| {
Ok(match from {
Value::Num(a) => a.select(indices_shape, indices_data, env)?.into(),
Value::Byte(a) => op_bytes_ref_retry_fill(
a,
|a| Ok(a.select(indices_shape, indices_data, env)?.into()),
|a| Ok(a.select(indices_shape, indices_data, env)?.into()),
)?,
Value::Complex(a) => a.select(indices_shape, indices_data, env)?.into(),
Value::Char(a) => a.select(indices_shape, indices_data, env)?.into(),
Value::Box(a) => a.select(indices_shape, indices_data, env)?.into(),
})
},
)
let (indices_shape, indices_data) = self.as_shaped_indices(env)?;
Ok(match from {
Value::Num(a) => a.select(indices_shape, &indices_data, env)?.into(),
Value::Byte(a) => op_bytes_ref_retry_fill(
a,
|a| Ok(a.select(indices_shape, &indices_data, env)?.into()),
|a| Ok(a.select(indices_shape, &indices_data, env)?.into()),
)?,
Value::Complex(a) => a.select(indices_shape, &indices_data, env)?.into(),
Value::Char(a) => a.select(indices_shape, &indices_data, env)?.into(),
Value::Box(a) => a.select(indices_shape, &indices_data, env)?.into(),
})
}
pub(crate) fn undo_select(self, index: Self, into: Self, env: &Uiua) -> UiuaResult<Self> {
let (idx_shape, ind) = index.as_shaped_indices(env)?;
Expand Down
Loading

0 comments on commit 941fd4d

Please sign in to comment.