// // SPDX-License-Identifier: BSD-3-Clause // Copyright Contributors to the OpenEXR Project. // // clang-format off #include "PyImathStringArrayRegister.h" #include "PyImathStringArray.h" #include "PyImathExport.h" namespace PyImath { using namespace boost::python; template StringArrayT* StringArrayT::createDefaultArray(size_t length) { return StringArrayT::createUniformArray(T(), length); } template StringArrayT* StringArrayT::createUniformArray(const T& initialValue, size_t length) { typedef boost::shared_array StringTableIndexArrayPtr; typedef boost::shared_ptr > StringTablePtr; BOOST_STATIC_ASSERT(boost::is_pod::value); StringTableIndexArrayPtr indexArray(reinterpret_cast(new char[sizeof(StringTableIndex)*length])); StringTablePtr table(new StringTableT); const StringTableIndex index = table->intern(initialValue); for(size_t i=0; i(*table, indexArray.get(), length, 1, indexArray, boost::any(table)); } template StringArrayT* StringArrayT::createFromRawArray(const T* rawArray, size_t length, bool writable) { typedef boost::shared_array StringTableIndexArrayPtr; typedef boost::shared_ptr > StringTablePtr; BOOST_STATIC_ASSERT(boost::is_pod::value); StringTableIndexArrayPtr indexArray(reinterpret_cast(new char[sizeof(StringTableIndex)*length])); StringTablePtr table(new StringTableT); for(size_t i=0; iintern(rawArray[i]); return new StringArrayT(*table, indexArray.get(), length, 1, indexArray, table, writable); } template StringArrayT::StringArrayT(StringTableT &table, StringTableIndex *ptr, size_t length, size_t stride, boost::any tableHandle, bool writable) : super(ptr,length,stride,writable), _table(table), _tableHandle(tableHandle) { // nothing } template StringArrayT::StringArrayT(StringTableT &table, StringTableIndex *ptr, size_t length, size_t stride, boost::any handle, boost::any tableHandle, bool writable) : super(ptr,length,stride,handle,writable), _table(table), _tableHandle(tableHandle) { // nothing } template StringArrayT::StringArrayT(const StringTableT &table, const StringTableIndex *ptr, size_t length, size_t stride, boost::any tableHandle) : super(ptr,length,stride), _table(const_cast &>(table)), _tableHandle(tableHandle) { // nothing } template StringArrayT::StringArrayT(const StringTableT &table, const StringTableIndex *ptr, size_t length, size_t stride, boost::any handle, boost::any tableHandle) : super(ptr,length,stride,handle), _table(const_cast &>(table)), _tableHandle(tableHandle) { // nothing } template StringArrayT::StringArrayT(StringArrayT& s, const FixedArray& mask) : super(s, mask), _table(s._table), _tableHandle(s._tableHandle) { } template StringArrayT* StringArrayT::getslice_string(PyObject *index) const { typedef boost::shared_array StringTableIndexArrayPtr; typedef boost::shared_ptr > StringTablePtr; BOOST_STATIC_ASSERT(boost::is_pod::value); size_t start=0, end=0, slicelength=0; Py_ssize_t step; extract_slice_indices(index,start,end,step,slicelength); StringTableIndexArrayPtr indexArray(reinterpret_cast(new char[sizeof(StringTableIndex)*slicelength])); StringTablePtr table(new StringTableT); for(size_t i=0; iintern(getitem_string(start+i*step)); return new StringArrayT(*table, indexArray.get(), slicelength, 1, indexArray, boost::any(table)); } template StringArrayT* StringArrayT::getslice_mask_string(const FixedArray& mask) { return new StringArrayT(*this, mask); } template void StringArrayT::setitem_string_scalar(PyObject *index, const T &data) { if (!writable()) throw std::invalid_argument("Fixed string-array is read-only."); size_t start=0, end=0, slicelength=0; Py_ssize_t step; extract_slice_indices(index,start,end,step,slicelength); StringTableIndex di = _table.intern(data); for (size_t i=0; i void StringArrayT::setitem_string_scalar_mask(const FixedArray &mask, const T &data) { if (!writable()) throw std::invalid_argument("Fixed string-array is read-only."); size_t len = match_dimension(mask); StringTableIndex di = _table.intern(data); for (size_t i=0; i void StringArrayT::setitem_string_vector(PyObject *index, const StringArrayT &data) { if (!writable()) throw std::invalid_argument("Fixed string-array is read-only."); size_t start=0, end=0, slicelength=0; Py_ssize_t step; extract_slice_indices(index,start,end,step,slicelength); // we have a valid range of indices if ((size_t) data.len() != slicelength) { PyErr_SetString(PyExc_IndexError, "Dimensions of source do not match destination"); throw_error_already_set(); } for (size_t i=0; i void StringArrayT::setitem_string_vector_mask(const FixedArray &mask, const StringArrayT &data) { if (!writable()) throw std::invalid_argument("Fixed string-array is read-only."); size_t len = match_dimension(mask); if ((size_t) data.len() == len) { for (size_t i=0; i FixedArray operator == (const StringArrayT &a0, const StringArrayT &a1) { size_t len = a0.match_dimension(a1); FixedArray f(len); const StringTableT &t0 = a0.stringTable(); const StringTableT &t1 = a1.stringTable(); for (size_t i=0;i FixedArray operator == (const StringArrayT &a0, const T &v1) { size_t len = a0.len(); FixedArray f(len); const StringTableT &t0 = a0.stringTable(); if (t0.hasString(v1)) { StringTableIndex v1i = t0.lookup(v1); for (size_t i=0;i FixedArray operator == (const T &v1,const StringArrayT &a0) { return a0 == v1; } template FixedArray operator != (const StringArrayT &a0, const StringArrayT &a1) { size_t len = a0.match_dimension(a1); FixedArray f(len); const StringTableT &t0 = a0.stringTable(); const StringTableT &t1 = a1.stringTable(); for (size_t i=0;i FixedArray operator != (const StringArrayT &a0, const T &v1) { size_t len = a0.len(); FixedArray f(len); const StringTableT &t0 = a0.stringTable(); if (t0.hasString(v1)) { StringTableIndex v1i = t0.lookup(v1); for (size_t i=0;i FixedArray operator != (const T &v1,const StringArrayT &a0) { return a0 != v1; } template<> PYIMATH_EXPORT StringTableIndex FixedArrayDefaultValue::value() { return StringTableIndex(0); } template<> PYIMATH_EXPORT const char* FixedArray::name() { return "StringTableArray"; } template class PYIMATH_EXPORT StringArrayT; template class PYIMATH_EXPORT StringArrayT; template FixedArray operator == (const StringArray& a0, const StringArray& a1); template FixedArray operator == (const StringArray& a0, const std::string& v1); template FixedArray operator == (const std::string& a0, const StringArray& v1); template FixedArray operator != (const StringArray& a0, const StringArray& a1); template FixedArray operator != (const StringArray& a0, const std::string& v1); template FixedArray operator != (const std::string& a0, const StringArray& v1); template FixedArray operator == (const WstringArray& a0, const WstringArray& a1); template FixedArray operator == (const WstringArray& a0, const std::wstring& v1); template FixedArray operator == (const std::wstring& a0, const WstringArray& v1); template FixedArray operator != (const WstringArray& a0, const WstringArray& a1); template FixedArray operator != (const WstringArray& a0, const std::wstring& v1); template FixedArray operator != (const std::wstring& a0, const WstringArray& v1); void register_StringArrays() { typedef StringArrayT StringArray; typedef StringArrayT WstringArray; class_ string_array_class = class_("StringArray",no_init); string_array_class .def("__init__", make_constructor(StringArray::createDefaultArray)) .def("__init__", make_constructor(StringArray::createUniformArray)) .def("__getitem__", &StringArray::getslice_string, return_value_policy()) .def("__getitem__", &StringArray::getitem_string) .def("__getitem__", &StringArray::getslice_mask_string, return_value_policy()) .def("__setitem__", &StringArray::setitem_string_scalar) .def("__setitem__", &StringArray::setitem_string_scalar_mask) .def("__setitem__", &StringArray::setitem_string_vector) .def("__setitem__", &StringArray::setitem_string_vector_mask) .def("__len__", &StringArray::len) .def("writable", &StringArray::writable) .def("makeReadOnly",&StringArray::makeReadOnly) .def(self == self) // NOSONAR - suppress SonarCloud bug report. .def(self == other()) .def(other() == self) .def(self != self) // NOSONAR - suppress SonarCloud bug report. .def(self != other()) .def(other() != self) ; class_ wstring_array_class = class_("WstringArray",no_init); wstring_array_class .def("__init__", make_constructor(WstringArray::createDefaultArray)) .def("__init__", make_constructor(WstringArray::createUniformArray)) .def("__getitem__", &WstringArray::getslice_string, return_value_policy()) .def("__getitem__", &WstringArray::getitem_string) .def("__getitem__", &WstringArray::getslice_mask_string, return_value_policy()) .def("__setitem__", &WstringArray::setitem_string_scalar) .def("__setitem__", &WstringArray::setitem_string_scalar_mask) .def("__setitem__", &WstringArray::setitem_string_vector) .def("__setitem__", &WstringArray::setitem_string_vector_mask) .def("__len__",&WstringArray::len) .def(self == self) // NOSONAR - suppress SonarCloud bug report. .def(self == other()) .def(other() == self) .def(self != self) // NOSONAR - suppress SonarCloud bug report. .def(self != other()) .def(other() != self) ; } } // namespace PyImath