/////////////////////////////////////////////////////////////////////////// // // Copyright (c) 2009-2012, Industrial Light & Magic, a division of Lucas // Digital Ltd. LLC // // All rights reserved. // // Redistribution and use in source and binary forms, with or without // modification, are permitted provided that the following conditions are // met: // * Redistributions of source code must retain the above copyright // notice, this list of conditions and the following disclaimer. // * Redistributions in binary form must reproduce the above // copyright notice, this list of conditions and the following disclaimer // in the documentation and/or other materials provided with the // distribution. // * Neither the name of Industrial Light & Magic nor the names of // its contributors may be used to endorse or promote products derived // from this software without specific prior written permission. // // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. // /////////////////////////////////////////////////////////////////////////// #include #include #include 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, table); } template StringArrayT* StringArrayT::createFromRawArray(const T* rawArray, 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); for(size_t i=0; iintern(rawArray[i]); return new StringArrayT(*table, indexArray.get(), length, 1, indexArray, table); } template StringArrayT::StringArrayT(StringTableT &table, StringTableIndex *ptr, size_t length, size_t stride, boost::any tableHandle) : super(ptr,length,stride), _table(table), _tableHandle(tableHandle) { // nothing } template StringArrayT::StringArrayT(StringTableT &table, StringTableIndex *ptr, size_t length, size_t stride, boost::any handle, boost::any tableHandle) : super(ptr,length,stride,handle), _table(table), _tableHandle(tableHandle) { // nothing } 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, table); } template void StringArrayT::setitem_string_scalar(PyObject *index, const T &data) { 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) { 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) { 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 (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) { size_t len = match_dimension(mask); if (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("__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(self == self) .def(self == other()) .def(other() == self) .def(self != self) .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("__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) .def(self == other()) .def(other() == self) .def(self != self) .def(self != other()) .def(other() != self) ; } } // namespace PyImath