15 #pragma warning(disable : 4996)
32 PySlice_Unpack(key.ptr(), &start, &stop, &step);
33 PySliceObject* slice_key =
reinterpret_cast<PySliceObject*
>(key.ptr());
36 if (!py::detail::PyNone_Check(slice_key->start)) {
37 start_opt =
static_cast<int64_t
>(start);
40 if (!py::detail::PyNone_Check(slice_key->stop)) {
41 stop_opt =
static_cast<int64_t
>(stop);
44 if (!py::detail::PyNone_Check(slice_key->step)) {
45 step_opt =
static_cast<int64_t
>(step);
91 if (py::isinstance<py::int_>(item)) {
93 static_cast<int64_t
>(py::reinterpret_borrow<py::int_>(item)));
94 }
else if (py::isinstance<py::slice>(item)) {
95 return ToTensorKey(py::reinterpret_borrow<py::slice>(item));
96 }
else if (py::isinstance<py::list>(item)) {
97 return ToTensorKey(py::reinterpret_borrow<py::list>(item));
98 }
else if (py::isinstance<py::tuple>(item)) {
99 return ToTensorKey(py::reinterpret_borrow<py::tuple>(item));
100 }
else if (py::isinstance<py::array>(item)) {
101 return ToTensorKey(py::reinterpret_borrow<py::array>(item));
102 }
else if (py::isinstance<Tensor>(item)) {
110 "PyHandleToTensorKey has invalid key type {}.",
111 static_cast<std::string
>(py::str(py::type::of(item))));
116 tensor.def(
"__getitem__", [](
const Tensor& tensor,
bool key) {
120 tensor.def(
"__getitem__", [](
const Tensor& tensor,
int key) {
124 tensor.def(
"__getitem__", [](
const Tensor& tensor,
const py::slice& key) {
128 tensor.def(
"__getitem__", [](
const Tensor& tensor,
const py::array& key) {
132 tensor.def(
"__getitem__", [](
const Tensor& tensor,
const Tensor& key) {
141 tensor.def(
"__getitem__", [](
const Tensor& tensor,
const py::list& key) {
148 tensor.def(
"__getitem__", [](
const Tensor& tensor,
const py::tuple& key) {
149 std::vector<TensorKey> tks;
150 for (
const py::handle item : key) {
153 return tensor.GetItem(tks);
158 tensor.def(
"__setitem__", [](
Tensor& tensor,
bool key,
159 const py::handle& value) {
160 return tensor.SetItem(
166 tensor.def(
"__setitem__", [](
Tensor& tensor,
int key,
167 const py::handle& value) {
168 return tensor.SetItem(
174 tensor.def(
"__setitem__", [](
Tensor& tensor,
const py::slice& key,
175 const py::handle& value) {
176 return tensor.SetItem(
182 tensor.def(
"__setitem__", [](
Tensor& tensor,
const py::array& key,
183 const py::handle& value) {
184 return tensor.SetItem(
190 tensor.def(
"__setitem__", [](
Tensor& tensor,
const Tensor& key,
191 const py::handle& value) {
192 return tensor.SetItem(
203 tensor.def(
"__setitem__", [](
Tensor& tensor,
const py::list& key,
204 const py::handle& value) {
205 return tensor.SetItem(
214 tensor.def(
"__setitem__", [](
Tensor& tensor,
const py::tuple& key,
215 const py::handle& value) {
216 std::vector<TensorKey> tks;
217 for (
const py::handle item : key) {
TensorKey is used to represent single index, slice or advanced indexing on a Tensor.
static TensorKey IndexTensor(const Tensor &index_tensor)
static TensorKey Index(int64_t index)
static TensorKey Slice(utility::optional< int64_t > start, utility::optional< int64_t > stop, utility::optional< int64_t > step)
static Tensor Init(const T val, const Device &device=Device("CPU:0"))
Tensor To(Dtype dtype, bool copy=false) const
Tensor PyHandleToTensor(const py::handle &handle, utility::optional< Dtype > dtype, utility::optional< Device > device, bool force_copy)
static void pybind_getitem(py::class_< Tensor > &tensor)
void pybind_core_tensor_accessor(py::class_< Tensor > &t)
static TensorKey PyHandleToTensorKey(const py::handle &item)
static TensorKey ToTensorKey(int key)
static void pybind_setitem(py::class_< Tensor > &tensor)
constexpr utility::nullopt_t None
Tensor PyArrayToTensor(py::array array, bool inplace)
Tensor PyTupleToTensor(const py::tuple &tuple, utility::optional< Dtype > dtype, utility::optional< Device > device)
Generic file read and write utility for python interface.