| 1 |
// |
| 2 |
// SPDX-License-Identifier: BSD-3-Clause |
| 3 |
// Copyright Contributors to the OpenEXR Project. |
| 4 |
// |
| 5 |
|
| 6 |
// clang-format off |
| 7 |
|
| 8 |
#include "PyImathStringArrayRegister.h" |
| 9 |
#include "PyImathStringArray.h" |
| 10 |
#include "PyImathExport.h" |
| 11 |
|
| 12 |
namespace PyImath { |
| 13 |
|
| 14 |
using namespace boost::python; |
| 15 |
|
| 16 |
template<class T> |
| 17 |
StringArrayT<T>* StringArrayT<T>::createDefaultArray(size_t length) |
| 18 |
{ |
| 19 |
return StringArrayT<T>::createUniformArray(T(), length); |
| 20 |
} |
| 21 |
|
| 22 |
template<class T> |
| 23 |
StringArrayT<T>* StringArrayT<T>::createUniformArray(const T& initialValue, size_t length) |
| 24 |
{ |
| 25 |
typedef boost::shared_array<StringTableIndex> StringTableIndexArrayPtr; |
| 26 |
typedef boost::shared_ptr<StringTableT<T> > StringTablePtr; |
| 27 |
|
| 28 |
BOOST_STATIC_ASSERT(boost::is_pod<StringTableIndex>::value); |
| 29 |
|
| 30 |
StringTableIndexArrayPtr indexArray(reinterpret_cast<StringTableIndex*>(new char[sizeof(StringTableIndex)*length])); |
| 31 |
StringTablePtr table(new StringTableT<T>); |
| 32 |
|
| 33 |
const StringTableIndex index = table->intern(initialValue); |
| 34 |
|
| 35 |
for(size_t i=0; i<length; ++i) |
| 36 |
indexArray[i] = index; |
| 37 |
|
| 38 |
return new StringArrayT<T>(*table, indexArray.get(), length, 1, indexArray, boost::any(table)); |
| 39 |
} |
| 40 |
|
| 41 |
template<class T> |
| 42 |
StringArrayT<T>* StringArrayT<T>::createFromRawArray(const T* rawArray, size_t length, bool writable) |
| 43 |
{ |
| 44 |
typedef boost::shared_array<StringTableIndex> StringTableIndexArrayPtr; |
| 45 |
typedef boost::shared_ptr<StringTableT<T> > StringTablePtr; |
| 46 |
|
| 47 |
BOOST_STATIC_ASSERT(boost::is_pod<StringTableIndex>::value); |
| 48 |
|
| 49 |
StringTableIndexArrayPtr indexArray(reinterpret_cast<StringTableIndex*>(new char[sizeof(StringTableIndex)*length])); |
| 50 |
StringTablePtr table(new StringTableT<T>); |
| 51 |
|
| 52 |
for(size_t i=0; i<length; ++i) |
| 53 |
indexArray[i] = table->intern(rawArray[i]); |
| 54 |
|
| 55 |
return new StringArrayT<T>(*table, indexArray.get(), length, 1, indexArray, table, writable); |
| 56 |
} |
| 57 |
|
| 58 |
template<class T> |
| 59 |
StringArrayT<T>::StringArrayT(StringTableT<T> &table, StringTableIndex *ptr, size_t length, |
| 60 |
size_t stride, boost::any tableHandle, bool writable) |
| 61 |
: super(ptr,length,stride,writable), _table(table), _tableHandle(tableHandle) |
| 62 |
{ |
| 63 |
// nothing |
| 64 |
} |
| 65 |
|
| 66 |
template<class T> |
| 67 |
StringArrayT<T>::StringArrayT(StringTableT<T> &table, StringTableIndex *ptr, size_t length, |
| 68 |
size_t stride, boost::any handle, boost::any tableHandle, bool writable) |
| 69 |
: super(ptr,length,stride,handle,writable), _table(table), _tableHandle(tableHandle) |
| 70 |
{ |
| 71 |
// nothing |
| 72 |
} |
| 73 |
|
| 74 |
template<class T> |
| 75 |
StringArrayT<T>::StringArrayT(const StringTableT<T> &table, const StringTableIndex *ptr, |
| 76 |
size_t length, size_t stride, boost::any tableHandle) |
| 77 |
: super(ptr,length,stride), _table(const_cast<StringTableT<T> &>(table)), |
| 78 |
_tableHandle(tableHandle) |
| 79 |
{ |
| 80 |
// nothing |
| 81 |
} |
| 82 |
|
| 83 |
template<class T> |
| 84 |
StringArrayT<T>::StringArrayT(const StringTableT<T> &table, const StringTableIndex *ptr, |
| 85 |
size_t length, size_t stride, boost::any handle, boost::any tableHandle) |
| 86 |
: super(ptr,length,stride,handle), _table(const_cast<StringTableT<T> &>(table)), |
| 87 |
_tableHandle(tableHandle) |
| 88 |
{ |
| 89 |
// nothing |
| 90 |
} |
| 91 |
|
| 92 |
template<class T> |
| 93 |
StringArrayT<T>::StringArrayT(StringArrayT& s, const FixedArray<int>& mask) |
| 94 |
: super(s, mask), |
| 95 |
_table(s._table), |
| 96 |
_tableHandle(s._tableHandle) |
| 97 |
{ |
| 98 |
} |
| 99 |
|
| 100 |
template<class T> |
| 101 |
StringArrayT<T>* |
| 102 |
StringArrayT<T>::getslice_string(PyObject *index) const |
| 103 |
{ |
| 104 |
typedef boost::shared_array<StringTableIndex> StringTableIndexArrayPtr; |
| 105 |
typedef boost::shared_ptr<StringTableT<T> > StringTablePtr; |
| 106 |
|
| 107 |
BOOST_STATIC_ASSERT(boost::is_pod<StringTableIndex>::value); |
| 108 |
|
| 109 |
size_t start=0, end=0, slicelength=0; |
| 110 |
Py_ssize_t step; |
| 111 |
extract_slice_indices(index,start,end,step,slicelength); |
| 112 |
|
| 113 |
StringTableIndexArrayPtr indexArray(reinterpret_cast<StringTableIndex*>(new char[sizeof(StringTableIndex)*slicelength])); |
| 114 |
StringTablePtr table(new StringTableT<T>); |
| 115 |
|
| 116 |
for(size_t i=0; i<slicelength; ++i) |
| 117 |
indexArray[i] = table->intern(getitem_string(start+i*step)); |
| 118 |
|
| 119 |
return new StringArrayT<T>(*table, indexArray.get(), slicelength, 1, indexArray, boost::any(table)); |
| 120 |
} |
| 121 |
|
| 122 |
template<class T> |
| 123 |
StringArrayT<T>* |
| 124 |
StringArrayT<T>::getslice_mask_string(const FixedArray<int>& mask) |
| 125 |
{ |
| 126 |
return new StringArrayT(*this, mask); |
| 127 |
} |
| 128 |
|
| 129 |
template<class T> |
| 130 |
void |
| 131 |
StringArrayT<T>::setitem_string_scalar(PyObject *index, const T &data) |
| 132 |
{ |
| 133 |
if (!writable()) |
| 134 |
throw std::invalid_argument("Fixed string-array is read-only."); |
| 135 |
|
| 136 |
size_t start=0, end=0, slicelength=0; |
| 137 |
Py_ssize_t step; |
| 138 |
extract_slice_indices(index,start,end,step,slicelength); |
| 139 |
StringTableIndex di = _table.intern(data); |
| 140 |
for (size_t i=0; i<slicelength; ++i) { |
| 141 |
(*this)[start+i*step] = di; |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
template<class T> |
| 146 |
void |
| 147 |
StringArrayT<T>::setitem_string_scalar_mask(const FixedArray<int> &mask, const T &data) |
| 148 |
{ |
| 149 |
if (!writable()) |
| 150 |
throw std::invalid_argument("Fixed string-array is read-only."); |
| 151 |
|
| 152 |
size_t len = match_dimension(mask); |
| 153 |
StringTableIndex di = _table.intern(data); |
| 154 |
for (size_t i=0; i<len; ++i) { |
| 155 |
if (mask[i]) (*this)[i] = di; |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
template<class T> |
| 160 |
void |
| 161 |
StringArrayT<T>::setitem_string_vector(PyObject *index, const StringArrayT<T> &data) |
| 162 |
{ |
| 163 |
if (!writable()) |
| 164 |
throw std::invalid_argument("Fixed string-array is read-only."); |
| 165 |
|
| 166 |
size_t start=0, end=0, slicelength=0; |
| 167 |
Py_ssize_t step; |
| 168 |
extract_slice_indices(index,start,end,step,slicelength); |
| 169 |
|
| 170 |
// we have a valid range of indices |
| 171 |
if ((size_t) data.len() != slicelength) { |
| 172 |
PyErr_SetString(PyExc_IndexError, "Dimensions of source do not match destination"); |
| 173 |
throw_error_already_set(); |
| 174 |
} |
| 175 |
for (size_t i=0; i<slicelength; ++i) { |
| 176 |
StringTableIndex di = _table.intern(data._table.lookup(data[i])); |
| 177 |
(*this)[start+i*step] = di; |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
template<class T> |
| 182 |
void |
| 183 |
StringArrayT<T>::setitem_string_vector_mask(const FixedArray<int> &mask, const StringArrayT<T> &data) |
| 184 |
{ |
| 185 |
if (!writable()) |
| 186 |
throw std::invalid_argument("Fixed string-array is read-only."); |
| 187 |
|
| 188 |
size_t len = match_dimension(mask); |
| 189 |
if ((size_t) data.len() == len) { |
| 190 |
for (size_t i=0; i<len; ++i) { |
| 191 |
if (mask[i]) { |
| 192 |
StringTableIndex di = _table.intern(data._table.lookup(data[i])); |
| 193 |
(*this)[i] = di; |
| 194 |
} |
| 195 |
} |
| 196 |
} else { |
| 197 |
size_t count = 0; |
| 198 |
for (size_t i=0; i<len; ++i) { |
| 199 |
if (mask[i]) count += 1; |
| 200 |
} |
| 201 |
|
| 202 |
if ((size_t) data.len() != count) { |
| 203 |
PyErr_SetString(PyExc_IndexError, "Dimensions of source data do not match destination either masked or unmasked"); |
| 204 |
throw_error_already_set(); |
| 205 |
} |
| 206 |
|
| 207 |
size_t dataIndex = 0; |
| 208 |
for (size_t i=0; i<len; ++i) { |
| 209 |
if (mask[i]) { |
| 210 |
StringTableIndex di = _table.intern(data._table.lookup(data[dataIndex])); |
| 211 |
(*this)[i] = di; |
| 212 |
dataIndex += 1; |
| 213 |
} |
| 214 |
} |
| 215 |
} |
| 216 |
} |
| 217 |
|
| 218 |
template<class T> |
| 219 |
FixedArray<int> operator == (const StringArrayT<T> &a0, const StringArrayT<T> &a1) { |
| 220 |
size_t len = a0.match_dimension(a1); |
| 221 |
FixedArray<int> f(len); |
| 222 |
const StringTableT<T> &t0 = a0.stringTable(); |
| 223 |
const StringTableT<T> &t1 = a1.stringTable(); |
| 224 |
for (size_t i=0;i<len;++i) { |
| 225 |
f[i] = t0.lookup(a0[i])==t1.lookup(a1[i]); |
| 226 |
} |
| 227 |
return f; |
| 228 |
} |
| 229 |
|
| 230 |
template<class T> |
| 231 |
FixedArray<int> operator == (const StringArrayT<T> &a0, const T &v1) { |
| 232 |
size_t len = a0.len(); |
| 233 |
FixedArray<int> f(len); |
| 234 |
const StringTableT<T> &t0 = a0.stringTable(); |
| 235 |
if (t0.hasString(v1)) { |
| 236 |
StringTableIndex v1i = t0.lookup(v1); |
| 237 |
for (size_t i=0;i<len;++i) { |
| 238 |
f[i] = a0[i]==v1i; |
| 239 |
} |
| 240 |
} else { |
| 241 |
for (size_t i=0;i<len;++i) { |
| 242 |
f[i] = 0; |
| 243 |
} |
| 244 |
} |
| 245 |
return f; |
| 246 |
} |
| 247 |
|
| 248 |
template<class T> |
| 249 |
FixedArray<int> operator == (const T &v1,const StringArrayT<T> &a0) { |
| 250 |
return a0 == v1; |
| 251 |
} |
| 252 |
|
| 253 |
template<class T> |
| 254 |
FixedArray<int> operator != (const StringArrayT<T> &a0, const StringArrayT<T> &a1) { |
| 255 |
size_t len = a0.match_dimension(a1); |
| 256 |
FixedArray<int> f(len); |
| 257 |
const StringTableT<T> &t0 = a0.stringTable(); |
| 258 |
const StringTableT<T> &t1 = a1.stringTable(); |
| 259 |
for (size_t i=0;i<len;++i) { |
| 260 |
f[i] = t0.lookup(a0[i])!=t1.lookup(a1[i]); |
| 261 |
} |
| 262 |
return f; |
| 263 |
} |
| 264 |
|
| 265 |
template<class T> |
| 266 |
FixedArray<int> operator != (const StringArrayT<T> &a0, const T &v1) { |
| 267 |
size_t len = a0.len(); |
| 268 |
FixedArray<int> f(len); |
| 269 |
const StringTableT<T> &t0 = a0.stringTable(); |
| 270 |
if (t0.hasString(v1)) { |
| 271 |
StringTableIndex v1i = t0.lookup(v1); |
| 272 |
for (size_t i=0;i<len;++i) { |
| 273 |
f[i] = a0[i]!=v1i; |
| 274 |
} |
| 275 |
} else { |
| 276 |
for (size_t i=0;i<len;++i) { |
| 277 |
f[i] = 1; |
| 278 |
} |
| 279 |
} |
| 280 |
return f; |
| 281 |
} |
| 282 |
|
| 283 |
template<class T> |
| 284 |
FixedArray<int> operator != (const T &v1,const StringArrayT<T> &a0) { |
| 285 |
return a0 != v1; |
| 286 |
} |
| 287 |
|
| 288 |
template<> PYIMATH_EXPORT StringTableIndex FixedArrayDefaultValue<StringTableIndex>::value() { return StringTableIndex(0); } |
| 289 |
template<> PYIMATH_EXPORT const char* FixedArray<StringTableIndex>::name() { return "StringTableArray"; } |
| 290 |
|
| 291 |
template class PYIMATH_EXPORT StringArrayT<std::string>; |
| 292 |
template class PYIMATH_EXPORT StringArrayT<std::wstring>; |
| 293 |
|
| 294 |
template FixedArray<int> operator == (const StringArray& a0, const StringArray& a1); |
| 295 |
template FixedArray<int> operator == (const StringArray& a0, const std::string& v1); |
| 296 |
template FixedArray<int> operator == (const std::string& a0, const StringArray& v1); |
| 297 |
template FixedArray<int> operator != (const StringArray& a0, const StringArray& a1); |
| 298 |
template FixedArray<int> operator != (const StringArray& a0, const std::string& v1); |
| 299 |
template FixedArray<int> operator != (const std::string& a0, const StringArray& v1); |
| 300 |
|
| 301 |
template FixedArray<int> operator == (const WstringArray& a0, const WstringArray& a1); |
| 302 |
template FixedArray<int> operator == (const WstringArray& a0, const std::wstring& v1); |
| 303 |
template FixedArray<int> operator == (const std::wstring& a0, const WstringArray& v1); |
| 304 |
template FixedArray<int> operator != (const WstringArray& a0, const WstringArray& a1); |
| 305 |
template FixedArray<int> operator != (const WstringArray& a0, const std::wstring& v1); |
| 306 |
template FixedArray<int> operator != (const std::wstring& a0, const WstringArray& v1); |
| 307 |
|
| 308 |
void register_StringArrays() |
| 309 |
{ |
| 310 |
typedef StringArrayT<std::string> StringArray; |
| 311 |
typedef StringArrayT<std::wstring> WstringArray; |
| 312 |
|
| 313 |
class_<StringArray> string_array_class = |
| 314 |
class_<StringArray>("StringArray",no_init); |
| 315 |
string_array_class |
| 316 |
.def("__init__", make_constructor(StringArray::createDefaultArray)) |
| 317 |
.def("__init__", make_constructor(StringArray::createUniformArray)) |
| 318 |
.def("__getitem__", &StringArray::getslice_string, return_value_policy<manage_new_object>()) |
| 319 |
.def("__getitem__", &StringArray::getitem_string) |
| 320 |
.def("__getitem__", &StringArray::getslice_mask_string, return_value_policy<manage_new_object>()) |
| 321 |
.def("__setitem__", &StringArray::setitem_string_scalar) |
| 322 |
.def("__setitem__", &StringArray::setitem_string_scalar_mask) |
| 323 |
.def("__setitem__", &StringArray::setitem_string_vector) |
| 324 |
.def("__setitem__", &StringArray::setitem_string_vector_mask) |
| 325 |
.def("__len__", &StringArray::len) |
| 326 |
.def("writable", &StringArray::writable) |
| 327 |
.def("makeReadOnly",&StringArray::makeReadOnly) |
| 328 |
.def(self == self) // NOSONAR - suppress SonarCloud bug report. |
| 329 |
.def(self == other<std::string>()) |
| 330 |
.def(other<std::string>() == self) |
| 331 |
.def(self != self) // NOSONAR - suppress SonarCloud bug report. |
| 332 |
.def(self != other<std::string>()) |
| 333 |
.def(other<std::string>() != self) |
| 334 |
; |
| 335 |
|
| 336 |
class_<WstringArray> wstring_array_class = |
| 337 |
class_<WstringArray>("WstringArray",no_init); |
| 338 |
wstring_array_class |
| 339 |
.def("__init__", make_constructor(WstringArray::createDefaultArray)) |
| 340 |
.def("__init__", make_constructor(WstringArray::createUniformArray)) |
| 341 |
.def("__getitem__", &WstringArray::getslice_string, return_value_policy<manage_new_object>()) |
| 342 |
.def("__getitem__", &WstringArray::getitem_string) |
| 343 |
.def("__getitem__", &WstringArray::getslice_mask_string, return_value_policy<manage_new_object>()) |
| 344 |
.def("__setitem__", &WstringArray::setitem_string_scalar) |
| 345 |
.def("__setitem__", &WstringArray::setitem_string_scalar_mask) |
| 346 |
.def("__setitem__", &WstringArray::setitem_string_vector) |
| 347 |
.def("__setitem__", &WstringArray::setitem_string_vector_mask) |
| 348 |
.def("__len__",&WstringArray::len) |
| 349 |
.def(self == self) // NOSONAR - suppress SonarCloud bug report. |
| 350 |
.def(self == other<std::wstring>()) |
| 351 |
.def(other<std::wstring>() == self) |
| 352 |
.def(self != self) // NOSONAR - suppress SonarCloud bug report. |
| 353 |
.def(self != other<std::wstring>()) |
| 354 |
.def(other<std::wstring>() != self) |
| 355 |
; |
| 356 |
} |
| 357 |
|
| 358 |
} // namespace PyImath |