| 1 |
// |
| 2 |
// SPDX-License-Identifier: BSD-3-Clause |
| 3 |
// Copyright Contributors to the OpenEXR Project. |
| 4 |
// |
| 5 |
|
| 6 |
// clang-format off |
| 7 |
|
| 8 |
#ifndef _PyImathStringArray_h_ |
| 9 |
#define _PyImathStringArray_h_ |
| 10 |
|
| 11 |
#include "PyImathFixedArray.h" |
| 12 |
#include "PyImathStringTable.h" |
| 13 |
|
| 14 |
// |
| 15 |
// A fixed lengy array class for string and wide string type in python. |
| 16 |
// The implementation of StringArray is does not follow other FixeArray |
| 17 |
// types. StringArray de-duplicate repeated strings using StringTable |
| 18 |
// internally for compact memory usage. |
| 19 |
// |
| 20 |
namespace PyImath { |
| 21 |
|
| 22 |
template <class T> |
| 23 |
class StringArrayT : public FixedArray<StringTableIndex> |
| 24 |
{ |
| 25 |
public: |
| 26 |
typedef T BaseType; |
| 27 |
typedef FixedArray<StringTableIndex> super; |
| 28 |
|
| 29 |
static StringArrayT<T>* createDefaultArray(size_t length); |
| 30 |
static StringArrayT<T>* createUniformArray(const T& initialValue, size_t length); |
| 31 |
static StringArrayT<T>* createFromRawArray(const T* rawArray, size_t length, |
| 32 |
bool writable = true); |
| 33 |
|
| 34 |
StringArrayT(StringTableT<T> &table, StringTableIndex *ptr, size_t length, |
| 35 |
size_t stride = 1, boost::any tableHandle = boost::any(), |
| 36 |
bool writable = true); |
| 37 |
|
| 38 |
StringArrayT(StringTableT<T> &table, StringTableIndex *ptr, size_t length, |
| 39 |
size_t stride, boost::any handle, boost::any tableHandle = boost::any(), |
| 40 |
bool writable = true); |
| 41 |
|
| 42 |
StringArrayT(const StringTableT<T> &table, const StringTableIndex *ptr, size_t length, |
| 43 |
size_t stride = 1, boost::any tableHandle = boost::any()); |
| 44 |
|
| 45 |
StringArrayT(const StringTableT<T> &table, const StringTableIndex *ptr, size_t length, |
| 46 |
size_t stride, boost::any handle, boost::any tableHandle = boost::any()); |
| 47 |
|
| 48 |
StringArrayT(StringArrayT& s, const FixedArray<int>& mask); |
| 49 |
|
| 50 |
const StringTableT<T> & stringTable() const { return _table; } |
| 51 |
|
| 52 |
T getitem_string(Py_ssize_t index) const {return _table.lookup(getitem(index)); } |
| 53 |
StringArrayT* getslice_string(PyObject *index) const; |
| 54 |
StringArrayT* getslice_mask_string(const FixedArray<int>& mask); |
| 55 |
|
| 56 |
void setitem_string_scalar(PyObject *index, const T &data); |
| 57 |
|
| 58 |
void setitem_string_scalar_mask(const FixedArray<int> &mask, const T &data); |
| 59 |
void setitem_string_vector(PyObject *index, const StringArrayT<T> &data); |
| 60 |
void setitem_string_vector_mask(const FixedArray<int> &mask, const StringArrayT<T> &data); |
| 61 |
|
| 62 |
private: |
| 63 |
typedef StringArrayT<T> this_type; |
| 64 |
|
| 65 |
StringTableT<T> &_table; |
| 66 |
// StringArray can borrow a string table from somewhere else or maintain |
| 67 |
// its own string table. This handle optionally stores a shared pointer to |
| 68 |
// a allocated StringTable class instance |
| 69 |
boost::any _tableHandle; |
| 70 |
}; |
| 71 |
|
| 72 |
template<class T> |
| 73 |
FixedArray<int> operator == (const StringArrayT<T> &a0, const StringArrayT<T> &a1); |
| 74 |
template<class T> |
| 75 |
FixedArray<int> operator == (const StringArrayT<T> &a0, const T &v1); |
| 76 |
template<class T> |
| 77 |
FixedArray<int> operator == (const T &v1,const StringArrayT<T> &a0); |
| 78 |
template<class T> |
| 79 |
FixedArray<int> operator != (const StringArrayT<T> &a0, const StringArrayT<T> &a1); |
| 80 |
template<class T> |
| 81 |
FixedArray<int> operator != (const StringArrayT<T> &a0, const T &v1); |
| 82 |
template<class T> |
| 83 |
FixedArray<int> operator != (const T &v1,const StringArrayT<T> &a0); |
| 84 |
|
| 85 |
typedef StringArrayT<std::string> StringArray; |
| 86 |
typedef StringArrayT<std::wstring> WstringArray; |
| 87 |
|
| 88 |
} // namespace PyImath |
| 89 |
|
| 90 |
#endif |