Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add TensorBase::slice_with method #360

Merged
merged 2 commits into from
Sep 17, 2024
Merged

Add TensorBase::slice_with method #360

merged 2 commits into from
Sep 17, 2024

Conversation

robertknight
Copy link
Owner

@robertknight robertknight commented Sep 17, 2024

Add new slicing methods, slice_with and slice_with_mut that infer the rank of the returned views automatically at compile time. This is more ergonomic to use than the existing slice method and avoids the possibility of specifying the wrong dimension count, or changing the rank of the sliced type and forgetting to update the slicing operation.

Eventually this may replace both slice and slice_dyn, but it has been added as a new API initially to allow incremental adoption downstream and also allow some iteration on the details.

This is like `TensorBase::{slice, slice_mut}` but is more ergonomic to use
because the rank of the returned tensor is inferred automatically. This is
achieved via a helper `IndexCount` trait which counts the number of elements in
a tuple that have index types, as opposed to range types.
Using `slice_with` ensures at compile time that the returned view has the
correct rank. Since it requires the input tensor to have a static rank to infer
the outut rank, it also encourages the use of static rank tensors, which is
generally good practice.
@robertknight robertknight marked this pull request as ready for review September 17, 2024 18:28
@robertknight robertknight merged commit 9b5d7fe into main Sep 17, 2024
2 checks passed
@robertknight robertknight deleted the tensor-slice-with branch September 17, 2024 18:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant