use super::core::{Tensor, TensorStorage};
use std::ops::Index;
impl<T: Clone> Index<&[usize]> for Tensor<T> {
type Output = T;
fn index(&self, index: &[usize]) -> &Self::Output {
match &self.storage {
TensorStorage::Cpu(arr) => {
if index.len() != arr.ndim() {
panic!(
"Index dimension mismatch: expected {} dimensions, got {}",
arr.ndim(),
index.len()
);
}
arr.get(index).expect("Index out of bounds")
}
#[cfg(feature = "gpu")]
TensorStorage::Gpu(_) => {
panic!("Direct indexing not supported for GPU tensors. Use .get() or convert to CPU first.")
}
}
}
}
impl<T: Clone> Index<usize> for Tensor<T> {
type Output = T;
fn index(&self, index: usize) -> &Self::Output {
match &self.storage {
TensorStorage::Cpu(arr) => {
if arr.ndim() != 1 {
panic!(
"Single index only supported for 1D tensors, but tensor has {} dimensions",
arr.ndim()
);
}
arr.get([index]).expect("Index out of bounds")
}
#[cfg(feature = "gpu")]
TensorStorage::Gpu(_) => {
panic!("Direct indexing not supported for GPU tensors. Use .get() or convert to CPU first.")
}
}
}
}