| 1 |
// |
| 2 |
// SPDX-License-Identifier: BSD-3-Clause |
| 3 |
// Copyright Contributors to the OpenEXR Project. |
| 4 |
// |
| 5 |
|
| 6 |
// clang-format off |
| 7 |
|
| 8 |
|
| 9 |
#define BOOST_BIND_GLOBAL_PLACEHOLDERS |
| 10 |
#include <boost/python.hpp> |
| 11 |
#include <boost/shared_array.hpp> |
| 12 |
#include <boost/any.hpp> |
| 13 |
#include <ImathVec.h> |
| 14 |
#include "PyImathExport.h" |
| 15 |
#include "PyImathFixedVArray.h" |
| 16 |
|
| 17 |
namespace PyImath { |
| 18 |
|
| 19 |
template <class T> |
| 20 |
FixedVArray<T>::FixedVArray (std::vector<T>* ptr, Py_ssize_t length, |
| 21 |
Py_ssize_t stride, bool writable) |
| 22 |
: _ptr(ptr), _length(length), _stride(stride), _writable (writable), |
| 23 |
_handle(), _unmaskedLength(0) |
| 24 |
{ |
| 25 |
if (length < 0) |
| 26 |
{ |
| 27 |
throw std::invalid_argument("Fixed array length must be non-negative"); |
| 28 |
} |
| 29 |
if (stride <= 0) |
| 30 |
{ |
| 31 |
throw std::invalid_argument("Fixed array stride must be positive"); |
| 32 |
} |
| 33 |
|
| 34 |
// Nothing else to do (pointer given, so we have the data) |
| 35 |
} |
| 36 |
|
| 37 |
template <class T> |
| 38 |
FixedVArray<T>::FixedVArray (std::vector<T>* ptr, Py_ssize_t length, |
| 39 |
Py_ssize_t stride, boost::any handle, bool writable) |
| 40 |
: _ptr(ptr), _length(length), _stride(stride), _writable(writable), |
| 41 |
_handle(handle), _unmaskedLength(0) |
| 42 |
{ |
| 43 |
if (length < 0) |
| 44 |
{ |
| 45 |
throw std::invalid_argument("Fixed array length must be non-negative"); |
| 46 |
} |
| 47 |
if (stride <= 0) |
| 48 |
{ |
| 49 |
throw std::invalid_argument("Fixed array stride must be positive"); |
| 50 |
} |
| 51 |
|
| 52 |
// Nothing else to do (pointer given, so we have the data) |
| 53 |
} |
| 54 |
|
| 55 |
template <class T> |
| 56 |
FixedVArray<T>::FixedVArray (const std::vector<T>* ptr, Py_ssize_t length, |
| 57 |
Py_ssize_t stride) |
| 58 |
: _ptr(const_cast<std::vector<T> *>(ptr)), _length(length), _stride(stride), |
| 59 |
_writable(false), _handle(), _unmaskedLength(0) |
| 60 |
{ |
| 61 |
if (length < 0) |
| 62 |
{ |
| 63 |
throw std::invalid_argument("Fixed array length must be non-negative"); |
| 64 |
} |
| 65 |
if (stride <= 0) |
| 66 |
{ |
| 67 |
throw std::invalid_argument("Fixed array stride must be positive"); |
| 68 |
} |
| 69 |
|
| 70 |
// Nothing else to do (pointer given, so we have the data) |
| 71 |
} |
| 72 |
|
| 73 |
template <class T> |
| 74 |
FixedVArray<T>::FixedVArray (const std::vector<T>* ptr, Py_ssize_t length, |
| 75 |
Py_ssize_t stride, boost::any handle) |
| 76 |
: _ptr(const_cast<std::vector<T> *>(ptr)), _length(length), _stride(stride), |
| 77 |
_writable(false), _handle(handle), _unmaskedLength(0) |
| 78 |
{ |
| 79 |
if (length < 0) |
| 80 |
{ |
| 81 |
throw std::invalid_argument("Fixed array length must be non-negative"); |
| 82 |
} |
| 83 |
if (stride <= 0) |
| 84 |
{ |
| 85 |
throw std::invalid_argument("Fixed array stride must be positive"); |
| 86 |
} |
| 87 |
|
| 88 |
// Nothing else to do (pointer given, so we have the data) |
| 89 |
} |
| 90 |
|
| 91 |
template <class T> |
| 92 |
FixedVArray<T>::FixedVArray(Py_ssize_t length) |
| 93 |
: _ptr(0), _length(length), _stride(1), _writable(true), |
| 94 |
_handle(), _unmaskedLength(0) |
| 95 |
{ |
| 96 |
if (length < 0) |
| 97 |
{ |
| 98 |
throw std::invalid_argument("Fixed array length must be non-negative"); |
| 99 |
} |
| 100 |
|
| 101 |
boost::shared_array<std::vector<T> > a(new std::vector<T>[length]); |
| 102 |
// Initial vectors in the array will be zero-length. |
| 103 |
_handle = a; |
| 104 |
_ptr = a.get(); |
| 105 |
} |
| 106 |
|
| 107 |
template <class T> |
| 108 |
FixedVArray<T>::FixedVArray(const T& initialValue, Py_ssize_t length) |
| 109 |
: _ptr(0), _length(length), _stride(1), _writable(true), |
| 110 |
_handle(), _unmaskedLength(0) |
| 111 |
{ |
| 112 |
if (length < 0) |
| 113 |
{ |
| 114 |
throw std::invalid_argument("Fixed array length must be non-negative"); |
| 115 |
} |
| 116 |
|
| 117 |
boost::shared_array<std::vector<T> > a(new std::vector<T>[length]); |
| 118 |
for (Py_ssize_t i = 0; i < length; ++i) |
| 119 |
{ |
| 120 |
a[i].push_back (initialValue); |
| 121 |
} |
| 122 |
_handle = a; |
| 123 |
_ptr = a.get(); |
| 124 |
} |
| 125 |
|
| 126 |
template <class T> |
| 127 |
FixedVArray<T>::FixedVArray(FixedVArray<T>& other, const FixedArray<int>& mask) |
| 128 |
: _ptr(other._ptr), _stride(other._stride), _writable(other._writable), |
| 129 |
_handle(other._handle) |
| 130 |
{ |
| 131 |
if (other.isMaskedReference()) |
| 132 |
{ |
| 133 |
throw std::invalid_argument |
| 134 |
("Masking an already-masked FixedVArray is not supported yet (SQ27000)"); |
| 135 |
} |
| 136 |
|
| 137 |
size_t len = (size_t) other.match_dimension (mask); |
| 138 |
_unmaskedLength = len; |
| 139 |
|
| 140 |
size_t reduced_len = 0; |
| 141 |
for (size_t i = 0; i < len; ++i) |
| 142 |
{ |
| 143 |
if (mask[i]) |
| 144 |
{ |
| 145 |
reduced_len++; |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
_indices.reset (new size_t[reduced_len]); |
| 150 |
|
| 151 |
for (size_t i = 0, j = 0; i < len; ++i) |
| 152 |
{ |
| 153 |
if (mask[i]) |
| 154 |
{ |
| 155 |
_indices[j] = i; // NOSONAR - suppress SonarCloud warning. |
| 156 |
j++; |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
_length = reduced_len; |
| 161 |
} |
| 162 |
|
| 163 |
template <class T> |
| 164 |
FixedVArray<T>::FixedVArray(const FixedArray<int>& size, const T& initialValue) |
| 165 |
: _ptr(nullptr), _length (size.len()), _stride(1), |
| 166 |
_writable(true), _handle(), _indices(), _unmaskedLength(0) |
| 167 |
{ |
| 168 |
boost::shared_array<std::vector<T> > a(new std::vector<T>[_length]); |
| 169 |
for (size_t i = 0; i < _length; ++i) |
| 170 |
{ |
| 171 |
if (size[i] < 0) |
| 172 |
throw std::invalid_argument("Attempt to create negative FixedVArray element"); |
| 173 |
|
| 174 |
std::vector<T> &v = a[i]; |
| 175 |
|
| 176 |
v.resize (size[i]); |
| 177 |
std::fill (v.begin(), v.end(), initialValue); |
| 178 |
} |
| 179 |
_handle = a; |
| 180 |
_ptr = a.get(); |
| 181 |
} |
| 182 |
|
| 183 |
template <class T> |
| 184 |
FixedVArray<T>::FixedVArray(const FixedVArray<T>& other) |
| 185 |
: _ptr(other._ptr), |
| 186 |
_length(other._length), |
| 187 |
_stride(other._stride), |
| 188 |
_writable (other._writable), |
| 189 |
_handle(other._handle), |
| 190 |
_indices(other._indices), |
| 191 |
_unmaskedLength(other._unmaskedLength) |
| 192 |
{ |
| 193 |
// Nothing. |
| 194 |
} |
| 195 |
|
| 196 |
template <class T> |
| 197 |
const FixedVArray<T> & |
| 198 |
FixedVArray<T>::operator = (const FixedVArray<T>& other) |
| 199 |
{ |
| 200 |
if (&other == this) |
| 201 |
return *this; |
| 202 |
|
| 203 |
_ptr = other._ptr; |
| 204 |
_length = other._length; |
| 205 |
_stride = other._stride; |
| 206 |
_writable = other._writable; |
| 207 |
_handle = other._handle; |
| 208 |
_unmaskedLength = other._unmaskedLength; |
| 209 |
_indices = other._indices; |
| 210 |
|
| 211 |
return *this; |
| 212 |
} |
| 213 |
|
| 214 |
template <class T> |
| 215 |
FixedVArray<T>::~FixedVArray() |
| 216 |
{ |
| 217 |
// Nothing. |
| 218 |
} |
| 219 |
|
| 220 |
|
| 221 |
template <class T> |
| 222 |
std::vector<T>& |
| 223 |
FixedVArray<T>::operator [] (size_t i) |
| 224 |
{ |
| 225 |
if (!_writable) |
| 226 |
throw std::invalid_argument("Fixed V-array is read-only."); |
| 227 |
|
| 228 |
return _ptr[(_indices ? raw_ptr_index(i) : i) * _stride]; |
| 229 |
} |
| 230 |
|
| 231 |
template <class T> |
| 232 |
const std::vector<T>& |
| 233 |
FixedVArray<T>::operator [] (size_t i) const |
| 234 |
{ |
| 235 |
return _ptr[(_indices ? raw_ptr_index(i) : i) * _stride]; |
| 236 |
} |
| 237 |
|
| 238 |
|
| 239 |
namespace { |
| 240 |
|
| 241 |
// |
| 242 |
// Make an index suitable for indexing into an array in c++ |
| 243 |
// from a python index, which can be negative for indexing |
| 244 |
// relative to the end of an array. |
| 245 |
// |
| 246 |
size_t |
| 247 |
canonical_index (Py_ssize_t index, const size_t& totalLength) |
| 248 |
{ |
| 249 |
if (index < 0) |
| 250 |
{ |
| 251 |
index += totalLength; |
| 252 |
} |
| 253 |
if ((size_t) index >= totalLength || index < 0) |
| 254 |
{ |
| 255 |
PyErr_SetString (PyExc_IndexError, "Index out of range"); |
| 256 |
boost::python::throw_error_already_set(); |
| 257 |
} |
| 258 |
return index; // still a 'virtual' index if this is a masked reference array |
| 259 |
} |
| 260 |
|
| 261 |
void |
| 262 |
extract_slice_indices (PyObject* index, size_t& start, size_t& end, |
| 263 |
Py_ssize_t& step, size_t& sliceLength, |
| 264 |
const size_t& totalLength) |
| 265 |
{ |
| 266 |
if (PySlice_Check (index)) |
| 267 |
{ |
| 268 |
#if PY_MAJOR_VERSION > 2 |
| 269 |
PyObject* slice = index; |
| 270 |
#else |
| 271 |
PySliceObject* slice = reinterpret_cast<PySliceObject *>(index); |
| 272 |
#endif |
| 273 |
Py_ssize_t s, e, sl; |
| 274 |
if (PySlice_GetIndicesEx(slice, totalLength, &s, &e, &step, &sl) == -1) |
| 275 |
{ |
| 276 |
boost::python::throw_error_already_set(); |
| 277 |
} |
| 278 |
if (s < 0 || e < -1 || sl < 0) |
| 279 |
{ |
| 280 |
throw std::domain_error |
| 281 |
("Slice extraction produced invalid start, end, or length indices"); |
| 282 |
} |
| 283 |
|
| 284 |
start = s; |
| 285 |
end = e; |
| 286 |
sliceLength = sl; |
| 287 |
} |
| 288 |
else if (PyInt_Check (index)) |
| 289 |
{ |
| 290 |
size_t i = canonical_index (PyInt_AsSsize_t(index), totalLength); |
| 291 |
start = i; |
| 292 |
end = i + 1; |
| 293 |
step = 1; |
| 294 |
sliceLength = 1; |
| 295 |
} |
| 296 |
else |
| 297 |
{ |
| 298 |
PyErr_SetString (PyExc_TypeError, "Object is not a slice"); |
| 299 |
boost::python::throw_error_already_set(); |
| 300 |
} |
| 301 |
} |
| 302 |
|
| 303 |
} // namespace |
| 304 |
|
| 305 |
|
| 306 |
// this must have a call policy of return_internal_reference |
| 307 |
template <class T> |
| 308 |
FixedArray<T> |
| 309 |
FixedVArray<T>::getitem (Py_ssize_t index) |
| 310 |
{ |
| 311 |
const size_t i = canonical_index (index, _length); |
| 312 |
std::vector<T>& data = _ptr[(_indices ? raw_ptr_index(i) : i) * _stride]; |
| 313 |
return FixedArray<T>(data.empty() ? nullptr : &data[0], data.size(), 1, _writable); |
| 314 |
} |
| 315 |
|
| 316 |
template <class T> |
| 317 |
FixedVArray<T> |
| 318 |
FixedVArray<T>::getslice (PyObject* index) const |
| 319 |
{ |
| 320 |
size_t start = 0; |
| 321 |
size_t end = 0; |
| 322 |
size_t sliceLength = 0; |
| 323 |
Py_ssize_t step; |
| 324 |
extract_slice_indices (index, start, end, step, sliceLength, _length); |
| 325 |
|
| 326 |
FixedVArray<T> f(sliceLength); |
| 327 |
|
| 328 |
if (_indices) |
| 329 |
{ |
| 330 |
for (size_t i = 0; i < sliceLength; ++i) |
| 331 |
{ |
| 332 |
f._ptr[i] = _ptr[raw_ptr_index(start + i*step)*_stride]; |
| 333 |
} |
| 334 |
} |
| 335 |
else |
| 336 |
{ |
| 337 |
for (size_t i = 0; i < sliceLength; ++i) |
| 338 |
{ |
| 339 |
f._ptr[i] = _ptr[(start + i*step)*_stride]; |
| 340 |
} |
| 341 |
} |
| 342 |
|
| 343 |
return f; |
| 344 |
} |
| 345 |
|
| 346 |
template <class T> |
| 347 |
FixedVArray<T> |
| 348 |
FixedVArray<T>::getslice_mask (const FixedArray<int>& mask) |
| 349 |
{ |
| 350 |
return FixedVArray<T> (*this, mask); |
| 351 |
} |
| 352 |
|
| 353 |
template <class T> |
| 354 |
void |
| 355 |
FixedVArray<T>::setitem_scalar (PyObject* index, const FixedArray<T>& data) |
| 356 |
{ |
| 357 |
if (!_writable) |
| 358 |
throw std::invalid_argument ("Fixed V-array is read-only."); |
| 359 |
|
| 360 |
size_t start = 0; |
| 361 |
size_t end = 0; |
| 362 |
size_t sliceLength = 0; |
| 363 |
Py_ssize_t step; |
| 364 |
extract_slice_indices (index, start, end, step, sliceLength, _length); |
| 365 |
|
| 366 |
if (_indices) |
| 367 |
{ |
| 368 |
for (size_t i = 0; i < sliceLength; ++i) |
| 369 |
{ |
| 370 |
std::vector<T> &d =_ptr[raw_ptr_index(start + i*step)*_stride]; |
| 371 |
if (data.len() != static_cast<Py_ssize_t>(d.size())) |
| 372 |
throw std::invalid_argument("FixedVArray::setitem: length of data does not match length of array element"); |
| 373 |
|
| 374 |
if (data.isMaskedReference()) |
| 375 |
{ |
| 376 |
for (Py_ssize_t j = 0; j < data.len(); ++j) |
| 377 |
{ |
| 378 |
d[j] = data[j]; |
| 379 |
} |
| 380 |
} |
| 381 |
else |
| 382 |
{ |
| 383 |
for (Py_ssize_t j = 0; j < data.len(); ++j) |
| 384 |
{ |
| 385 |
d[j] = data.direct_index(j); |
| 386 |
} |
| 387 |
} |
| 388 |
} |
| 389 |
} |
| 390 |
else |
| 391 |
{ |
| 392 |
for (size_t i = 0; i < sliceLength; ++i) |
| 393 |
{ |
| 394 |
std::vector<T> &d =_ptr[(start + i*step)*_stride]; |
| 395 |
if (data.len() != static_cast<Py_ssize_t>(d.size())) |
| 396 |
throw std::invalid_argument("FixedVArray::setitem: length of data does not match length of array element"); |
| 397 |
|
| 398 |
if (data.isMaskedReference()) |
| 399 |
{ |
| 400 |
for (Py_ssize_t j = 0; j < data.len(); ++j) |
| 401 |
{ |
| 402 |
d[j] = data[j]; |
| 403 |
} |
| 404 |
} |
| 405 |
else |
| 406 |
{ |
| 407 |
for (Py_ssize_t j = 0; j < data.len(); ++j) |
| 408 |
{ |
| 409 |
d[j] = data.direct_index(j); |
| 410 |
} |
| 411 |
} |
| 412 |
} |
| 413 |
} |
| 414 |
} |
| 415 |
|
| 416 |
template <class T> |
| 417 |
void |
| 418 |
FixedVArray<T>::setitem_scalar_mask (const FixedArray<int>& mask, const FixedArray<T>& data) |
| 419 |
{ |
| 420 |
if (!_writable) |
| 421 |
throw std::invalid_argument ("Fixed V-array is read-only."); |
| 422 |
|
| 423 |
size_t len = match_dimension(mask, false); |
| 424 |
|
| 425 |
if (_indices) |
| 426 |
{ |
| 427 |
for (size_t i = 0; i < len; ++i) |
| 428 |
{ |
| 429 |
// We don't need to actually look at 'mask' because |
| 430 |
// match_dimensions has already forced some expected condition. |
| 431 |
std::vector<T> &d =_ptr[raw_ptr_index(i)*_stride]; |
| 432 |
if (data.len() != static_cast<Py_ssize_t>(d.size())) |
| 433 |
throw std::invalid_argument("FixedVArray::setitem: length of data does not match length of array element"); |
| 434 |
|
| 435 |
if (data.isMaskedReference()) |
| 436 |
{ |
| 437 |
for (Py_ssize_t j = 0; j < data.len(); ++j) |
| 438 |
{ |
| 439 |
d[j] = data[j]; |
| 440 |
} |
| 441 |
} |
| 442 |
else |
| 443 |
{ |
| 444 |
for (Py_ssize_t j = 0; j < data.len(); ++j) |
| 445 |
{ |
| 446 |
d[j] = data.direct_index(j); |
| 447 |
} |
| 448 |
} |
| 449 |
} |
| 450 |
} |
| 451 |
else |
| 452 |
{ |
| 453 |
for (size_t i = 0; i < len; ++i) |
| 454 |
{ |
| 455 |
if (mask[i]) |
| 456 |
{ |
| 457 |
std::vector<T> &d = _ptr[i*_stride]; |
| 458 |
if (data.len() != static_cast<Py_ssize_t>(d.size())) |
| 459 |
throw std::invalid_argument("FixedVArray::setitem: length of data does not match length of array element"); |
| 460 |
|
| 461 |
if (data.isMaskedReference()) |
| 462 |
{ |
| 463 |
for (Py_ssize_t j = 0; j < data.len(); ++j) |
| 464 |
{ |
| 465 |
d[j] = data[j]; |
| 466 |
} |
| 467 |
} |
| 468 |
else |
| 469 |
{ |
| 470 |
for (Py_ssize_t j = 0; j < data.len(); ++j) |
| 471 |
{ |
| 472 |
d[j] = data.direct_index(j); |
| 473 |
} |
| 474 |
} |
| 475 |
} |
| 476 |
} |
| 477 |
} |
| 478 |
} |
| 479 |
template <class T> |
| 480 |
void |
| 481 |
FixedVArray<T>::setitem_vector (PyObject* index, const FixedVArray<T>& data) |
| 482 |
{ |
| 483 |
if (!_writable) |
| 484 |
throw std::invalid_argument ("Fixed V-array is read-only."); |
| 485 |
|
| 486 |
size_t start = 0; |
| 487 |
size_t end = 0; |
| 488 |
size_t sliceLength = 0; |
| 489 |
Py_ssize_t step; |
| 490 |
extract_slice_indices (index, start, end, step, sliceLength, _length); |
| 491 |
|
| 492 |
if ((size_t) data.len() != sliceLength) |
| 493 |
{ |
| 494 |
PyErr_SetString (PyExc_IndexError, |
| 495 |
"Dimensions of source do not match destination"); |
| 496 |
boost::python::throw_error_already_set(); |
| 497 |
} |
| 498 |
|
| 499 |
if (_indices) |
| 500 |
{ |
| 501 |
for (size_t i = 0; i < sliceLength; ++i) |
| 502 |
{ |
| 503 |
_ptr[raw_ptr_index(start + i*step)*_stride] = data[i]; |
| 504 |
} |
| 505 |
} |
| 506 |
else |
| 507 |
{ |
| 508 |
for (size_t i = 0; i < sliceLength; ++i) |
| 509 |
{ |
| 510 |
_ptr[(start + i*step)*_stride] = data[i]; |
| 511 |
} |
| 512 |
} |
| 513 |
} |
| 514 |
|
| 515 |
template <class T> |
| 516 |
void |
| 517 |
FixedVArray<T>::setitem_vector_mask (const FixedArray<int>& mask, |
| 518 |
const FixedVArray<T>& data) |
| 519 |
{ |
| 520 |
if (!_writable) |
| 521 |
throw std::invalid_argument ("Fixed V-array is read-only."); |
| 522 |
|
| 523 |
// This restriction could be removed if there is a compelling use-case. |
| 524 |
if (_indices) |
| 525 |
{ |
| 526 |
throw std::invalid_argument |
| 527 |
("We don't support setting item masks for masked reference arrays"); |
| 528 |
} |
| 529 |
|
| 530 |
size_t len = match_dimension(mask); |
| 531 |
|
| 532 |
if ((size_t) data.len() == len) |
| 533 |
{ |
| 534 |
for (size_t i = 0; i < len; ++i) |
| 535 |
{ |
| 536 |
if (mask[i]) |
| 537 |
{ |
| 538 |
_ptr[i*_stride] = data[i]; |
| 539 |
} |
| 540 |
} |
| 541 |
} |
| 542 |
else |
| 543 |
{ |
| 544 |
size_t count = 0; |
| 545 |
for (size_t i = 0; i < len; ++i) |
| 546 |
{ |
| 547 |
if (mask[i]) |
| 548 |
{ |
| 549 |
count++; |
| 550 |
} |
| 551 |
} |
| 552 |
if ((size_t) data.len() != count) |
| 553 |
{ |
| 554 |
throw std::invalid_argument |
| 555 |
("Dimensions of source data do not match destination " |
| 556 |
"either masked or unmasked"); |
| 557 |
} |
| 558 |
|
| 559 |
Py_ssize_t dataIndex = 0; |
| 560 |
for (size_t i = 0; i < len; ++i) |
| 561 |
{ |
| 562 |
if (mask[i]) |
| 563 |
{ |
| 564 |
_ptr[i*_stride] = data[dataIndex]; |
| 565 |
dataIndex++; |
| 566 |
} |
| 567 |
} |
| 568 |
} |
| 569 |
} |
| 570 |
|
| 571 |
|
| 572 |
template <class T> |
| 573 |
int |
| 574 |
FixedVArray<T>::SizeHelper::getitem (Py_ssize_t index) const |
| 575 |
{ |
| 576 |
size_t i = canonical_index(index, _a._length); |
| 577 |
|
| 578 |
if (_a._indices) |
| 579 |
{ |
| 580 |
return _a._ptr[_a.raw_ptr_index(i)*_a._stride].size(); |
| 581 |
} |
| 582 |
|
| 583 |
return _a._ptr[i*_a._stride].size(); |
| 584 |
} |
| 585 |
|
| 586 |
template <class T> |
| 587 |
FixedArray<int> |
| 588 |
FixedVArray<T>::SizeHelper::getitem_slice (PyObject* index) const |
| 589 |
{ |
| 590 |
size_t start = 0; |
| 591 |
size_t end = 0; |
| 592 |
size_t sliceLength = 0; |
| 593 |
Py_ssize_t step; |
| 594 |
extract_slice_indices (index, start, end, step, sliceLength, _a._length); |
| 595 |
|
| 596 |
FixedArray<int> f(sliceLength); |
| 597 |
|
| 598 |
if (_a._indices) |
| 599 |
{ |
| 600 |
for (size_t i = 0; i < sliceLength; ++i) |
| 601 |
{ |
| 602 |
f.direct_index(i) = _a._ptr[_a.raw_ptr_index(start + i*step)*_a._stride].size(); |
| 603 |
} |
| 604 |
} |
| 605 |
else |
| 606 |
{ |
| 607 |
for (size_t i = 0; i < sliceLength; ++i) |
| 608 |
{ |
| 609 |
f.direct_index(i) = _a._ptr[(start + i*step)*_a._stride].size(); |
| 610 |
} |
| 611 |
} |
| 612 |
|
| 613 |
return f; |
| 614 |
} |
| 615 |
|
| 616 |
template <class T> |
| 617 |
FixedArray<int> |
| 618 |
FixedVArray<T>::SizeHelper::getitem_mask (const FixedArray<int>& mask) const |
| 619 |
{ |
| 620 |
int len = mask.len(); |
| 621 |
if (len != _a.len()) |
| 622 |
{ |
| 623 |
throw std::invalid_argument("Dimensions of mask do not match array"); |
| 624 |
} |
| 625 |
|
| 626 |
int count = 0; |
| 627 |
for (Py_ssize_t i = 0; i < mask.len(); ++i) |
| 628 |
{ |
| 629 |
if (mask[i]) count += 1; |
| 630 |
} |
| 631 |
|
| 632 |
FixedArray<int> f(count); |
| 633 |
|
| 634 |
if (_a._indices) |
| 635 |
{ |
| 636 |
size_t index = 0; |
| 637 |
for (Py_ssize_t i = 0; i < mask.len(); ++i) |
| 638 |
{ |
| 639 |
if (mask[i]) |
| 640 |
{ |
| 641 |
f.direct_index(index) = _a._ptr[_a.raw_ptr_index(i)*_a._stride].size(); |
| 642 |
index += 1; |
| 643 |
} |
| 644 |
} |
| 645 |
} |
| 646 |
else |
| 647 |
{ |
| 648 |
size_t index = 0; |
| 649 |
for (Py_ssize_t i = 0; i < mask.len(); ++i) |
| 650 |
{ |
| 651 |
if (mask[i]) |
| 652 |
{ |
| 653 |
f.direct_index(index) = _a._ptr[i*_a._stride].size(); |
| 654 |
index += 1; |
| 655 |
} |
| 656 |
} |
| 657 |
} |
| 658 |
|
| 659 |
return f; |
| 660 |
} |
| 661 |
|
| 662 |
|
| 663 |
template <class T> |
| 664 |
void |
| 665 |
FixedVArray<T>::SizeHelper::setitem_scalar (PyObject* index, size_t size) |
| 666 |
{ |
| 667 |
if (!_a.writable()) |
| 668 |
throw std::invalid_argument ("Fixed V-array is read-only."); |
| 669 |
|
| 670 |
size_t start = 0; |
| 671 |
size_t end = 0; |
| 672 |
size_t sliceLength = 0; |
| 673 |
Py_ssize_t step; |
| 674 |
extract_slice_indices (index, start, end, step, sliceLength, _a._length); |
| 675 |
|
| 676 |
if (_a._indices) |
| 677 |
{ |
| 678 |
for (size_t i = 0; i < sliceLength; ++i) |
| 679 |
{ |
| 680 |
_a._ptr[_a.raw_ptr_index(start + i*step)*_a._stride].resize(size); |
| 681 |
} |
| 682 |
} |
| 683 |
else |
| 684 |
{ |
| 685 |
for (size_t i = 0; i < sliceLength; ++i) |
| 686 |
{ |
| 687 |
_a._ptr[(start + i*step)*_a._stride].resize(size); |
| 688 |
} |
| 689 |
} |
| 690 |
} |
| 691 |
|
| 692 |
template <class T> |
| 693 |
void |
| 694 |
FixedVArray<T>::SizeHelper::setitem_scalar_mask (const FixedArray<int>& mask, size_t size) |
| 695 |
{ |
| 696 |
if (!_a.writable()) |
| 697 |
throw std::invalid_argument ("Fixed V-array is read-only."); |
| 698 |
|
| 699 |
size_t len = _a.match_dimension(mask, false); |
| 700 |
|
| 701 |
if (_a._indices) |
| 702 |
{ |
| 703 |
for (size_t i = 0; i < len; ++i) |
| 704 |
{ |
| 705 |
// We don't need to actually look at 'mask' because |
| 706 |
// match_dimensions has already forced some expected condition. |
| 707 |
_a._ptr[_a.raw_ptr_index(i)*_a._stride].resize(size); |
| 708 |
} |
| 709 |
} |
| 710 |
else |
| 711 |
{ |
| 712 |
for (size_t i = 0; i < len; ++i) |
| 713 |
{ |
| 714 |
if (mask[i]) |
| 715 |
{ |
| 716 |
_a._ptr[i*_a._stride].resize(size); |
| 717 |
} |
| 718 |
} |
| 719 |
} |
| 720 |
} |
| 721 |
|
| 722 |
template <class T> |
| 723 |
void |
| 724 |
FixedVArray<T>::SizeHelper::setitem_vector(PyObject *index, const FixedArray<int> &size) |
| 725 |
{ |
| 726 |
if (!_a.writable()) |
| 727 |
throw std::invalid_argument ("Fixed V-array is read-only."); |
| 728 |
|
| 729 |
size_t start=0, end=0, slicelength=0; |
| 730 |
Py_ssize_t step; |
| 731 |
extract_slice_indices(index,start,end,step,slicelength,_a._length); |
| 732 |
|
| 733 |
// we have a valid range of indices |
| 734 |
if (size.len() != static_cast<Py_ssize_t>(slicelength)) { |
| 735 |
PyErr_SetString(PyExc_IndexError, "Dimensions of source do not match destination"); |
| 736 |
boost::python::throw_error_already_set(); |
| 737 |
} |
| 738 |
|
| 739 |
if (_a._indices) |
| 740 |
{ |
| 741 |
for (size_t i=0; i<slicelength; ++i) |
| 742 |
_a._ptr[_a.raw_ptr_index(start+i*step)*_a._stride].resize(size[i]); |
| 743 |
} |
| 744 |
else |
| 745 |
{ |
| 746 |
for (size_t i=0; i<slicelength; ++i) |
| 747 |
_a._ptr[(start+i*step)*_a._stride].resize(size[i]); |
| 748 |
} |
| 749 |
} |
| 750 |
|
| 751 |
template <class T> |
| 752 |
void |
| 753 |
FixedVArray<T>::SizeHelper::setitem_vector_mask(const FixedArray<int> &mask, const FixedArray<int> &size) |
| 754 |
{ |
| 755 |
if (!_a.writable()) |
| 756 |
throw std::invalid_argument ("Fixed V-array is read-only."); |
| 757 |
|
| 758 |
// We could relax this but this restriction if there's a good |
| 759 |
// enough reason too. |
| 760 |
|
| 761 |
if (_a._indices) |
| 762 |
{ |
| 763 |
throw std::invalid_argument("We don't support setting item masks for masked reference arrays."); |
| 764 |
} |
| 765 |
|
| 766 |
Py_ssize_t len = _a.match_dimension(mask); |
| 767 |
if (size.len() == len) |
| 768 |
{ |
| 769 |
for (Py_ssize_t i = 0; i < len; ++i) |
| 770 |
if (mask[i]) _a._ptr[i*_a._stride].resize(size[i]); |
| 771 |
} |
| 772 |
else |
| 773 |
{ |
| 774 |
Py_ssize_t count = 0; |
| 775 |
for (Py_ssize_t i = 0; i < len; ++i) |
| 776 |
if (mask[i]) count++; |
| 777 |
|
| 778 |
if (size.len() != count) { |
| 779 |
throw std::invalid_argument("Dimensions of source data do not match destination either masked or unmasked"); |
| 780 |
} |
| 781 |
|
| 782 |
Py_ssize_t sizeIndex = 0; |
| 783 |
for (Py_ssize_t i = 0; i < len; ++i) |
| 784 |
{ |
| 785 |
if (mask[i]) |
| 786 |
{ |
| 787 |
_a._ptr[i*_a._stride].resize(size[sizeIndex]); |
| 788 |
sizeIndex++; |
| 789 |
} |
| 790 |
} |
| 791 |
} |
| 792 |
} |
| 793 |
|
| 794 |
template <class T> |
| 795 |
size_t |
| 796 |
FixedVArray<T>::raw_ptr_index (size_t i) const |
| 797 |
{ |
| 798 |
assert (isMaskedReference()); |
| 799 |
assert (i < _length); |
| 800 |
assert (_indices[i] >= 0 && _indices[i] < _unmaskedLength); |
| 801 |
|
| 802 |
return _indices[i]; |
| 803 |
} |
| 804 |
|
| 805 |
|
| 806 |
template <class T> |
| 807 |
boost::shared_ptr<typename FixedVArray<T>::SizeHelper> |
| 808 |
FixedVArray<T>::getSizeHelper() |
| 809 |
{ |
| 810 |
return boost::shared_ptr<SizeHelper>(new typename FixedVArray<T>::SizeHelper (*this)); |
| 811 |
} |
| 812 |
|
| 813 |
// static |
| 814 |
template <class T> |
| 815 |
boost::python::class_<FixedVArray<T> > |
| 816 |
FixedVArray<T>::register_(const char* doc) |
| 817 |
{ |
| 818 |
boost::python::class_<FixedVArray<T> > fixedVArray_class (name(), doc, |
| 819 |
boost::python::init<Py_ssize_t>("Construct a variable array of the " |
| 820 |
"specified length initialized to the default value for the given type")); |
| 821 |
|
| 822 |
fixedVArray_class |
| 823 |
.def(boost::python::init<const FixedVArray<T> &>("Construct a variable array with the same values as the given array")) |
| 824 |
.def(boost::python::init<const T &, Py_ssize_t>("Construct a variable array of the specified length initialized to the specified default value")) |
| 825 |
.def(boost::python::init<const FixedArray<int> &, const T &>("Construct a variable array with each array size equal to the specified elements and initialized to the specified default value")) |
| 826 |
.def("__getitem__", &FixedVArray<T>::getslice) |
| 827 |
.def("__getitem__", &FixedVArray<T>::getslice_mask) |
| 828 |
.def("__getitem__", &FixedVArray<T>::getitem, boost::python::with_custodian_and_ward_postcall<1,0>()) |
| 829 |
|
| 830 |
.def("__setitem__", &FixedVArray<T>::setitem_scalar) |
| 831 |
.def("__setitem__", &FixedVArray<T>::setitem_scalar_mask) |
| 832 |
.def("__setitem__", &FixedVArray<T>::setitem_vector) |
| 833 |
.def("__setitem__", &FixedVArray<T>::setitem_vector_mask) |
| 834 |
|
| 835 |
.def("__len__", &FixedVArray<T>::len) |
| 836 |
.def("writable", &FixedVArray<T>::writable) |
| 837 |
.def("makeReadOnly",&FixedVArray<T>::makeReadOnly) |
| 838 |
|
| 839 |
.add_property("size", boost::python::make_function(&FixedVArray<T>::getSizeHelper, boost::python::with_custodian_and_ward_postcall<0,1>())) |
| 840 |
; |
| 841 |
|
| 842 |
{ |
| 843 |
boost::python::scope s(fixedVArray_class); |
| 844 |
|
| 845 |
boost::python::class_<typename FixedVArray<T>::SizeHelper,boost::noncopyable> sizeHelper_class ("SizeHelper", boost::python::no_init); |
| 846 |
sizeHelper_class |
| 847 |
.def("__getitem__", &FixedVArray<T>::SizeHelper::getitem) |
| 848 |
.def("__getitem__", &FixedVArray<T>::SizeHelper::getitem_mask) |
| 849 |
.def("__getitem__", &FixedVArray<T>::SizeHelper::getitem_slice) |
| 850 |
|
| 851 |
.def("__setitem__", &FixedVArray<T>::SizeHelper::setitem_scalar) |
| 852 |
.def("__setitem__", &FixedVArray<T>::SizeHelper::setitem_scalar_mask) |
| 853 |
.def("__setitem__", &FixedVArray<T>::SizeHelper::setitem_vector) |
| 854 |
.def("__setitem__", &FixedVArray<T>::SizeHelper::setitem_vector_mask) |
| 855 |
; |
| 856 |
} |
| 857 |
|
| 858 |
boost::python::register_ptr_to_python<boost::shared_ptr<typename FixedVArray<T>::SizeHelper> >(); |
| 859 |
|
| 860 |
return fixedVArray_class; |
| 861 |
} |
| 862 |
|
| 863 |
|
| 864 |
// ---- Explicit Class Instantiation --------------------------------- |
| 865 |
|
| 866 |
template class PYIMATH_EXPORT FixedVArray<int>; |
| 867 |
template class PYIMATH_EXPORT FixedVArray<float>; |
| 868 |
template class PYIMATH_EXPORT FixedVArray<Imath::Vec2<int> >; |
| 869 |
template class PYIMATH_EXPORT FixedVArray<Imath::Vec2<float> >; |
| 870 |
|
| 871 |
} // namespace PyImath |