#include <ATen/TensorIndexing.h>
#include <c10/util/Exception.h>
#include <c10/util/irange.h>
namespace at {
namespace indexing {
const EllipsisIndexType Ellipsis = EllipsisIndexType();
std::ostream& operator<<(std::ostream& stream, const Slice& slice) {
stream << slice.start() << ':' << slice.stop() << ':' << slice.step();
return stream;
}
std::ostream& operator<<(std::ostream& stream, const TensorIndex& tensor_index) {
if (tensor_index.is_none()) {
stream << "None";
} else if (tensor_index.is_ellipsis()) {
stream << "...";
} else if (tensor_index.is_integer()) {
stream << tensor_index.integer();
} else if (tensor_index.is_boolean()) {
stream << std::boolalpha << tensor_index.boolean();
} else if (tensor_index.is_slice()) {
stream << tensor_index.slice();
} else if (tensor_index.is_tensor()) {
stream << tensor_index.tensor();
}
return stream;
}
std::ostream& operator<<(std::ostream& stream, const std::vector<TensorIndex>& tensor_indices) {
stream << '(';
for (const auto i : c10::irange(tensor_indices.size())) {
stream << tensor_indices[i];
if (i < tensor_indices.size() - 1) stream << ", ";
}
stream << ')';
return stream;
}
}
Tensor Tensor::index(ArrayRef<at::indexing::TensorIndex> indices) const {
TORCH_CHECK(!indices.empty(), "Passing an empty index list to Tensor::index() is not valid syntax");
OptionalDeviceGuard device_guard(device_of(*this));
return at::indexing::get_item(*this, indices);
}
Tensor Tensor::index(std::initializer_list<at::indexing::TensorIndex> indices) const {
return index(ArrayRef<at::indexing::TensorIndex>(indices));
}
Tensor & Tensor::index_put_(ArrayRef<at::indexing::TensorIndex> indices, Tensor const & rhs) {
TORCH_CHECK(!indices.empty(), "Passing an empty index list to Tensor::index_put_() is not valid syntax");
OptionalDeviceGuard device_guard(device_of(*this));
at::indexing::set_item(*this, indices, rhs);
return *this;
}
Tensor & Tensor::index_put_(ArrayRef<at::indexing::TensorIndex> indices, const Scalar& v) {
TORCH_CHECK(!indices.empty(), "Passing an empty index list to Tensor::index_put_() is not valid syntax");
OptionalDeviceGuard device_guard(device_of(*this));
at::indexing::set_item(*this, indices, v);
return *this;
}
Tensor & Tensor::index_put_(std::initializer_list<at::indexing::TensorIndex> indices, Tensor const & rhs) {
return index_put_(ArrayRef<at::indexing::TensorIndex>(indices), rhs);
}
Tensor & Tensor::index_put_(std::initializer_list<at::indexing::TensorIndex> indices, const Scalar& v) {
return index_put_(ArrayRef<at::indexing::TensorIndex>(indices), v);
}
}