| 1 |
// |
| 2 |
// SPDX-License-Identifier: BSD-3-Clause |
| 3 |
// Copyright Contributors to the OpenEXR Project. |
| 4 |
// |
| 5 |
|
| 6 |
// clang-format off |
| 7 |
|
| 8 |
#ifndef _PyImathFixedVArray_h_ |
| 9 |
#define _PyImathFixedVArray_h_ |
| 10 |
|
| 11 |
#define BOOST_BIND_GLOBAL_PLACEHOLDERS |
| 12 |
#include <boost/python.hpp> |
| 13 |
#include <boost/any.hpp> |
| 14 |
#include <vector> |
| 15 |
#include "PyImathFixedArray.h" |
| 16 |
|
| 17 |
namespace PyImath { |
| 18 |
|
| 19 |
template <class T> |
| 20 |
class FixedVArray |
| 21 |
{ |
| 22 |
// This class (at least for now) holds a std::vector of 'T' types. |
| 23 |
// This will give us the 'variable' part of the array. Generally, |
| 24 |
// we will initially support only a very small subset of accessor |
| 25 |
// methods before the semantics are fully defined. Currently, the |
| 26 |
// VArray semantics are defined in the 'varraySemantics.txt' file. |
| 27 |
|
| 28 |
std::vector<T> * _ptr; |
| 29 |
size_t _length; |
| 30 |
size_t _stride; |
| 31 |
bool _writable; |
| 32 |
|
| 33 |
// This handle optionally stores a shared_array to allocated array data |
| 34 |
// so that everything is freed properly on exit. |
| 35 |
boost::any _handle; |
| 36 |
|
| 37 |
boost::shared_array<size_t> _indices; // non-NULL if we're a masked reference |
| 38 |
size_t _unmaskedLength; |
| 39 |
|
| 40 |
public: |
| 41 |
typedef T BaseType; |
| 42 |
|
| 43 |
FixedVArray (std::vector<T>* ptr, Py_ssize_t length, |
| 44 |
Py_ssize_t stride = 1, bool writable = true); |
| 45 |
|
| 46 |
FixedVArray (std::vector<T>* ptr, Py_ssize_t length, |
| 47 |
Py_ssize_t stride, boost::any handle, bool writable = true); |
| 48 |
|
| 49 |
FixedVArray (const std::vector<T>* ptr, Py_ssize_t length, |
| 50 |
Py_ssize_t stride = 1); |
| 51 |
|
| 52 |
FixedVArray (const std::vector<T>* ptr, Py_ssize_t length, |
| 53 |
Py_ssize_t stride, boost::any handle); |
| 54 |
|
| 55 |
explicit FixedVArray (Py_ssize_t length); |
| 56 |
|
| 57 |
FixedVArray (const T& initialValue, Py_ssize_t length); |
| 58 |
|
| 59 |
FixedVArray (FixedVArray<T>& f, const FixedArray<int>& mask); |
| 60 |
|
| 61 |
FixedVArray (const FixedArray<int>& size, const T& initialValue); |
| 62 |
|
| 63 |
FixedVArray (const FixedVArray<T>& other); |
| 64 |
|
| 65 |
const FixedVArray& operator = (const FixedVArray<T>& other); |
| 66 |
|
| 67 |
~FixedVArray(); |
| 68 |
|
| 69 |
// ---------------- |
| 70 |
|
| 71 |
const boost::any& handle() { return _handle; } |
| 72 |
|
| 73 |
Py_ssize_t len() const { return _length; } |
| 74 |
size_t stride() const { return _stride; } |
| 75 |
bool writable() const { return _writable; } |
| 76 |
|
| 77 |
// This method is mainly here for use in confidence tests, but there may |
| 78 |
// be other use-cases where a writable array needs to be made read-only. |
| 79 |
// Note that we do not provide a 'makeWritable' method here, because that |
| 80 |
// type of operation shouldn't be allowed. |
| 81 |
void makeReadOnly() { _writable = false; } |
| 82 |
|
| 83 |
bool isMaskedReference() const { return _indices.get() != 0; } |
| 84 |
size_t unmaskedLength() const { return _unmaskedLength; } |
| 85 |
|
| 86 |
std::vector<T>& operator [] (size_t i); |
| 87 |
const std::vector<T>& operator [] (size_t i) const; |
| 88 |
|
| 89 |
// ---------------- |
| 90 |
|
| 91 |
FixedArray<T> getitem (Py_ssize_t index); |
| 92 |
FixedVArray<T> getslice (PyObject* index) const; |
| 93 |
FixedVArray<T> getslice_mask (const FixedArray<int>& mask); |
| 94 |
|
| 95 |
void setitem_scalar (PyObject* index, const FixedArray<T>& data); |
| 96 |
void setitem_scalar_mask (const FixedArray<int>& mask, const FixedArray<T>& data); |
| 97 |
void setitem_vector (PyObject* index, const FixedVArray<T>& data); |
| 98 |
void setitem_vector_mask (const FixedArray<int>& mask, const FixedVArray<T>& data); |
| 99 |
|
| 100 |
struct SizeHelper |
| 101 |
{ |
| 102 |
SizeHelper(FixedVArray &a) : _a(a) {} |
| 103 |
|
| 104 |
int getitem(Py_ssize_t index) const; |
| 105 |
FixedArray<int> getitem_slice(PyObject* index) const; |
| 106 |
FixedArray<int> getitem_mask(const FixedArray<int>& mask) const; |
| 107 |
|
| 108 |
void setitem_scalar (PyObject* index, size_t size); |
| 109 |
void setitem_scalar_mask (const FixedArray<int>& mask, size_t size); |
| 110 |
void setitem_vector (PyObject* index, const FixedArray<int>& size); |
| 111 |
void setitem_vector_mask (const FixedArray<int>& mask, const FixedArray<int>& size); |
| 112 |
|
| 113 |
private: |
| 114 |
|
| 115 |
FixedVArray &_a; |
| 116 |
}; |
| 117 |
|
| 118 |
boost::shared_ptr<SizeHelper> getSizeHelper(); |
| 119 |
|
| 120 |
friend struct SizeHelper; |
| 121 |
|
| 122 |
// ---------------- |
| 123 |
|
| 124 |
static boost::python::class_<FixedVArray<T> > register_(const char* doc); |
| 125 |
|
| 126 |
// Instantiations of fixed variable arrays must implement this static member. |
| 127 |
static const char* name(); |
| 128 |
|
| 129 |
template <class T2> |
| 130 |
size_t match_dimension (const FixedArray<T2>& mask, |
| 131 |
bool strictComparison = true) const |
| 132 |
{ |
| 133 |
if (len() == mask.len()) |
| 134 |
{ |
| 135 |
return len(); |
| 136 |
} |
| 137 |
|
| 138 |
bool throwExc = false; |
| 139 |
if (strictComparison) |
| 140 |
{ |
| 141 |
throwExc = true; |
| 142 |
} |
| 143 |
else if (_indices) |
| 144 |
{ |
| 145 |
if (_unmaskedLength != (size_t) mask.len()) |
| 146 |
{ |
| 147 |
throwExc = true; |
| 148 |
} |
| 149 |
} |
| 150 |
else |
| 151 |
{ |
| 152 |
throwExc = true; |
| 153 |
} |
| 154 |
|
| 155 |
if (throwExc) |
| 156 |
{ |
| 157 |
throw std::invalid_argument("Dimensions of source do not match destination"); |
| 158 |
} |
| 159 |
|
| 160 |
return len(); |
| 161 |
} |
| 162 |
|
| 163 |
size_t match_dimension (const FixedVArray<T>& other, |
| 164 |
bool strictComparison = true) const |
| 165 |
{ |
| 166 |
if (len() == other.len()) |
| 167 |
{ |
| 168 |
return len(); |
| 169 |
} |
| 170 |
|
| 171 |
bool throwExc = false; |
| 172 |
if (strictComparison) |
| 173 |
{ |
| 174 |
throwExc = true; |
| 175 |
} |
| 176 |
else if (_indices) |
| 177 |
{ |
| 178 |
if (_unmaskedLength != (size_t) other.len()) |
| 179 |
{ |
| 180 |
throwExc = true; |
| 181 |
} |
| 182 |
} |
| 183 |
else |
| 184 |
{ |
| 185 |
throwExc = true; |
| 186 |
} |
| 187 |
|
| 188 |
if (throwExc) |
| 189 |
{ |
| 190 |
throw std::invalid_argument("Dimensions of source do not match destination"); |
| 191 |
} |
| 192 |
|
| 193 |
return len(); |
| 194 |
} |
| 195 |
|
| 196 |
protected: |
| 197 |
size_t raw_ptr_index (size_t i) const; |
| 198 |
|
| 199 |
}; |
| 200 |
|
| 201 |
} // namespace PyImath |
| 202 |
|
| 203 |
#endif // _PyImathFixedVArray_h_ |