ENH: Add Smart Pointers to XdmfArray implementation. Modify some method names...

ENH: Add Smart Pointers to XdmfArray implementation.  Modify some method names to be more clear about whether copying is being
performed when setting values in the array.  Modify tests to use new names and regenerate python wrappers.
parent 369f62ea
......@@ -72,13 +72,13 @@ namespace boost {
%template(UIntVector) std::vector<unsigned int>;
%extend XdmfArray {
%template(setValues) setValues<char>;
%template(setValues) setValues<short>;
%template(setValues) setValues<int>;
%template(setValues) setValues<long>;
%template(setValues) setValues<float>;
%template(setValues) setValues<double>;
%template(setValues) setValues<unsigned char>;
%template(setValues) setValues<unsigned short>;
%template(setValues) setValues<unsigned int>;
%template(copyValues) copyValues<char>;
%template(copyValues) copyValues<short>;
%template(copyValues) copyValues<int>;
%template(copyValues) copyValues<long>;
%template(copyValues) copyValues<float>;
%template(copyValues) copyValues<double>;
%template(copyValues) copyValues<unsigned char>;
%template(copyValues) copyValues<unsigned short>;
%template(copyValues) copyValues<unsigned int>;
};
......@@ -137,10 +137,10 @@ class XdmfArrayPtr(_object):
def getPrecision(self): return _Xdmf.XdmfArrayPtr_getPrecision(self)
def getSize(self): return _Xdmf.XdmfArrayPtr_getSize(self)
def getValues(self): return _Xdmf.XdmfArrayPtr_getValues(self)
def getValuesPointer(self): return _Xdmf.XdmfArrayPtr_getValuesPointer(self)
def getValuesPointer(self, *args): return _Xdmf.XdmfArrayPtr_getValuesPointer(self, *args)
def printSelf(self): return _Xdmf.XdmfArrayPtr_printSelf(self)
def write(self, *args): return _Xdmf.XdmfArrayPtr_write(self, *args)
def setValues(self, *args): return _Xdmf.XdmfArrayPtr_setValues(self, *args)
def copyValues(self, *args): return _Xdmf.XdmfArrayPtr_copyValues(self, *args)
XdmfArrayPtr_swigregister = _Xdmf.XdmfArrayPtr_swigregister
XdmfArrayPtr_swigregister(XdmfArrayPtr)
......@@ -535,10 +535,10 @@ class XdmfArray(XdmfItem):
def getPrecision(self): return _Xdmf.XdmfArray_getPrecision(self)
def getSize(self): return _Xdmf.XdmfArray_getSize(self)
def getValues(self): return _Xdmf.XdmfArray_getValues(self)
def getValuesPointer(self): return _Xdmf.XdmfArray_getValuesPointer(self)
def getValuesPointer(self, *args): return _Xdmf.XdmfArray_getValuesPointer(self, *args)
def printSelf(self): return _Xdmf.XdmfArray_printSelf(self)
def write(self, *args): return _Xdmf.XdmfArray_write(self, *args)
def setValues(self, *args): return _Xdmf.XdmfArray_setValues(self, *args)
def copyValues(self, *args): return _Xdmf.XdmfArray_copyValues(self, *args)
XdmfArray_swigregister = _Xdmf.XdmfArray_swigregister
XdmfArray_swigregister(XdmfArray)
......
......@@ -9,159 +9,159 @@
#include "XdmfVisitor.hpp"
#include <sstream>
class XdmfArrayGetPrecision : public boost::static_visitor <int> {
class XdmfArrayGetHDF5Type : public boost::static_visitor <hid_t> {
public:
XdmfArrayGetPrecision()
XdmfArrayGetHDF5Type()
{
}
int operator()(const std::vector<char> & array) const
hid_t operator()(const boost::shared_ptr<std::vector<char> > array) const
{
return 1;
return H5T_NATIVE_CHAR;
}
int operator()(const std::vector<short> & array) const
hid_t operator()(const boost::shared_ptr<std::vector<short> > array) const
{
return 2;
return H5T_NATIVE_SHORT;
}
int operator()(const std::vector<int> & array) const
hid_t operator()(const boost::shared_ptr<std::vector<int> > array) const
{
return 4;
return H5T_NATIVE_INT;
}
int operator()(const std::vector<long> & array) const
hid_t operator()(const boost::shared_ptr<std::vector<long> > array) const
{
return 8;
return H5T_NATIVE_LONG;
}
int operator()(const std::vector<float> & array) const
hid_t operator()(const boost::shared_ptr<std::vector<float> > array) const
{
return 4;
return H5T_NATIVE_FLOAT;
}
int operator()(const std::vector<double> & array) const
hid_t operator()(const boost::shared_ptr<std::vector<double> > array) const
{
return 8;
return H5T_NATIVE_DOUBLE;
}
int operator()(const std::vector<unsigned char> & array) const
hid_t operator()(const boost::shared_ptr<std::vector<unsigned char> > array) const
{
return 1;
return H5T_NATIVE_UCHAR;
}
int operator()(const std::vector<unsigned short> & array) const
hid_t operator()(const boost::shared_ptr<std::vector<unsigned short> > array) const
{
return 2;
return H5T_NATIVE_USHORT;
}
int operator()(const std::vector<unsigned int> & array) const
hid_t operator()(const boost::shared_ptr<std::vector<unsigned int> > array) const
{
return 4;
return H5T_NATIVE_UINT;
}
};
class XdmfArrayGetType : public boost::static_visitor <std::string> {
class XdmfArrayGetPrecision : public boost::static_visitor <int> {
public:
XdmfArrayGetType()
XdmfArrayGetPrecision()
{
}
std::string operator()(const std::vector<char> & array) const
int operator()(const boost::shared_ptr<std::vector<char> > array) const
{
return "Char";
return 1;
}
std::string operator()(const std::vector<short> & array) const
int operator()(const boost::shared_ptr<std::vector<short> > array) const
{
return "Short";
return 2;
}
std::string operator()(const std::vector<int> & array) const
int operator()(const boost::shared_ptr<std::vector<int> > array) const
{
return "Int";
return 4;
}
std::string operator()(const std::vector<long> & array) const
int operator()(const boost::shared_ptr<std::vector<long> > array) const
{
return "Int";
return 8;
}
std::string operator()(const std::vector<float> & array) const
int operator()(const boost::shared_ptr<std::vector<float> > array) const
{
return "Float";
return 4;
}
std::string operator()(const std::vector<double> & array) const
int operator()(const boost::shared_ptr<std::vector<double> > array) const
{
return "Float";
return 8;
}
std::string operator()(const std::vector<unsigned char> & array) const
int operator()(const boost::shared_ptr<std::vector<unsigned char> > array) const
{
return "UChar";
return 1;
}
std::string operator()(const std::vector<unsigned short> & array) const
int operator()(const boost::shared_ptr<std::vector<unsigned short> > array) const
{
return "UShort";
return 2;
}
std::string operator()(const std::vector<unsigned int> & array) const
int operator()(const boost::shared_ptr<std::vector<unsigned int> > array) const
{
return "UInt";
return 4;
}
};
class XdmfArrayGetHDF5Type : public boost::static_visitor <hid_t> {
class XdmfArrayGetType : public boost::static_visitor <std::string> {
public:
XdmfArrayGetHDF5Type()
XdmfArrayGetType()
{
}
hid_t operator()(const std::vector<char> & array) const
std::string operator()(const boost::shared_ptr<std::vector<char> > array) const
{
return H5T_NATIVE_CHAR;
return "Char";
}
hid_t operator()(const std::vector<short> & array) const
std::string operator()(const boost::shared_ptr<std::vector<short> > array) const
{
return H5T_NATIVE_SHORT;
return "Short";
}
hid_t operator()(const std::vector<int> & array) const
std::string operator()(const boost::shared_ptr<std::vector<int> > array) const
{
return H5T_NATIVE_INT;
return "Int";
}
hid_t operator()(const std::vector<long> & array) const
std::string operator()(const boost::shared_ptr<std::vector<long> > array) const
{
return H5T_NATIVE_LONG;
return "Int";
}
hid_t operator()(const std::vector<float> & array) const
std::string operator()(const boost::shared_ptr<std::vector<float> > array) const
{
return H5T_NATIVE_FLOAT;
return "Float";
}
hid_t operator()(const std::vector<double> & array) const
std::string operator()(const boost::shared_ptr<std::vector<double> > array) const
{
return H5T_NATIVE_DOUBLE;
return "Float";
}
hid_t operator()(const std::vector<unsigned char> & array) const
std::string operator()(const boost::shared_ptr<std::vector<unsigned char> > array) const
{
return H5T_NATIVE_UCHAR;
return "UChar";
}
hid_t operator()(const std::vector<unsigned short> & array) const
std::string operator()(const boost::shared_ptr<std::vector<unsigned short> > array) const
{
return H5T_NATIVE_USHORT;
return "UShort";
}
hid_t operator()(const std::vector<unsigned int> & array) const
std::string operator()(const boost::shared_ptr<std::vector<unsigned int> > array) const
{
return H5T_NATIVE_UINT;
return "UInt";
}
};
......@@ -172,9 +172,9 @@ public:
{
}
template<typename T> int operator()(const std::vector<T> & array) const
template<typename T> int operator()(const boost::shared_ptr<std::vector<T> > array) const
{
return array.size();
return array->size();
}
};
......@@ -185,22 +185,22 @@ public:
{
}
std::string operator()(const std::vector<char> & array) const
std::string operator()(const boost::shared_ptr<std::vector<char> > array) const
{
std::stringstream toReturn;
for(int i=0; i<array.size(); ++i)
for(int i=0; i<array->size(); ++i)
{
toReturn << (int)(array[i]) << " ";
toReturn << (int)(array->operator[](i)) << " ";
}
return toReturn.str();
}
template<typename T> std::string operator()(const std::vector<T> & array) const
template<typename T> std::string operator()(const boost::shared_ptr<std::vector<T> > array) const
{
std::stringstream toReturn;
for(int i=0; i<array.size(); ++i)
for(int i=0; i<array->size(); ++i)
{
toReturn << array[i] << " ";
toReturn << array->operator[](i) << " ";
}
return toReturn.str();
}
......@@ -212,9 +212,9 @@ public:
{
}
template<typename T> void* operator()(std::vector<T> & array) const
template<typename T> void* operator()(boost::shared_ptr<std::vector<T> > array) const
{
return &array[0];
return &array->operator[](0);
}
};
......@@ -225,9 +225,9 @@ public:
{
}
template<typename T> const void* const operator()(const std::vector<T> & array) const
template<typename T> const void* const operator()(const boost::shared_ptr<std::vector<T> > array) const
{
return &array[0];
return &array->operator[](0);
}
};
......
......@@ -27,20 +27,20 @@ public:
{
}
template<typename U> void operator()(std::vector<U> & array) const
template<typename U> void operator()(boost::shared_ptr<std::vector<U> > array) const
{
int size = mStartIndex + mNumValues;
if(mArrayStride > 1)
{
size = mStartIndex + mNumValues * mArrayStride - 1;
}
if(array.size() < size)
if(array->size() < size)
{
array.resize(size);
array->resize(size);
}
for(int i=0; i<mNumValues; ++i)
{
array[mStartIndex + i*mArrayStride] = (U)mValuesPointer[i*mValuesStride];
array->operator[](mStartIndex + i*mArrayStride) = (U)mValuesPointer[i*mValuesStride];
}
}
......@@ -58,6 +58,17 @@ public:
XdmfNewMacro(XdmfArray);
/**
* Copy the values from a vector into this array
*
* @param array the vector to copy into this array.
*/
template<typename T> void copyValues(std::vector<T> & array)
{
boost::shared_ptr<std::vector<T> > newArray(new std::vector<T>(array));
mArray = newArray;
}
/**
* Get the data type of this array.
*
......@@ -108,10 +119,8 @@ public:
*/
virtual const void* const getValuesPointer() const;
virtual std::string printSelf() const;
/**
* Copy the values from a pointer into this array.
* Insert the values from a pointer into this array.
*
* @param startIndex the index in this array to begin insertion.
* @param valuesPointer a pointer to the values to copy into this array.
......@@ -119,23 +128,27 @@ public:
* @param arrayStride number of values to stride in this array between each copy.
* @param valuesStride number of values to stride in the pointer between each copy.
*/
template<typename T> void setValues(int startIndex, T* valuesPointer, int numValues = 1, int arrayStride = 1, int valuesStride = 1)
template<typename T> void insertValues(int startIndex, T* valuesPointer, int numValues = 1, int arrayStride = 1, int valuesStride = 1)
{
if(!mInitialized)
{
// Set type of variant to type of pointer
mArray = std::vector<T>();
boost::shared_ptr<std::vector<T> > newArray(new std::vector<T>());
mArray = newArray;
mInitialized = true;
}
boost::apply_visitor( XdmfArraySetValues<T>(startIndex, valuesPointer, numValues, arrayStride, valuesStride), mArray);
}
boost::apply_visitor( XdmfArraySetValues<T>(startIndex, valuesPointer, numValues, arrayStride, valuesStride), mArray);
}
virtual std::string printSelf() const;
/**
* Copy the values from a vector into this array
* Sets the values of this array to the values stored in the vector. No copy is made. This array shares ownership with
* other references to the smart pointer.
*
* @param array the vector to copy into this array.
* @param a smart pointer to a vector to store in this array.
*/
template<typename T> void setValues(std::vector<T> & array)
template<typename T> void setValues(boost::shared_ptr<std::vector<T> > array)
{
mArray = array;
}
......@@ -151,15 +164,15 @@ private:
bool mInitialized;
boost::variant<std::vector<char>,
std::vector<short>,
std::vector<int>,
std::vector<long>,
std::vector<float>,
std::vector<double>,
std::vector<unsigned char>,
std::vector<unsigned short>,
std::vector<unsigned int> > mArray;
boost::variant<boost::shared_ptr<std::vector<char> >,
boost::shared_ptr<std::vector<short> >,
boost::shared_ptr<std::vector<int> >,
boost::shared_ptr<std::vector<long> >,
boost::shared_ptr<std::vector<float> >,
boost::shared_ptr<std::vector<double> >,
boost::shared_ptr<std::vector<unsigned char> >,
boost::shared_ptr<std::vector<unsigned short> >,
boost::shared_ptr<std::vector<unsigned int> > > mArray;
};
#endif /* XDMFARRAY_HPP_ */
This diff is collapsed.
......@@ -3,53 +3,59 @@
int main(int argc, char* argv[])
{
/**
* Test simple copy from array
*/
double values[] = {1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.8, 9.9};
boost::shared_ptr<XdmfArray> array = XdmfArray::New();
std::vector<char> values;
values.push_back(1);
values.push_back(2);
values.push_back(3);
values.push_back(4);
array->setValues(0, &values[0], 4, 1, 1);
std::cout << array->getValues() << std::endl;
std::cout << array->getType() << std::endl;
boost::shared_ptr<XdmfArray> array2 = XdmfArray::New();
array->insertValues(0, &values[0], 9);
assert(array->getSize() == 9);
assert(array->getType().compare("Float") == 0);
assert(array->getPrecision() == 8);
assert(array->getValues().compare("1.1 2.2 3.3 4.4 5.5 6.6 7.7 8.8 9.9 ") == 0);
// Now modify original array
values[0] = 0;
assert(array->getValues().compare("1.1 2.2 3.3 4.4 5.5 6.6 7.7 8.8 9.9 ") == 0);
/**
* Test simple copy from vector
*/
std::vector<int> values2;
values2.push_back(100);
values2.push_back(200);
array2->setValues(0, &values2[0], 2, 1, 1);
std::cout << array2->getValues() << std::endl;
std::cout << array2->getType() << std::endl;
array2->setValues(2, &values[0], 2, 1, 1);
std::cout << array2->getValues() << std::endl;
std::cout << array2->getType() << std::endl;
boost::shared_ptr<XdmfArray> array2 = XdmfArray::New();
array2->copyValues(values2);
assert(array2->getSize() == 2);
assert(array2->getType().compare("Int") == 0);
assert(array2->getPrecision() == 4);
assert(array2->getValues().compare("100 200 ") == 0);
// Now modify original array
values2.push_back(300);
assert(array2->getSize() == 2);
assert(array2->getType().compare("Int") == 0);
assert(array2->getPrecision() == 4);
assert(array2->getValues().compare("100 200 ") == 0);
/**
* Test shared vector assignment
*/
boost::shared_ptr<std::vector<char> > values3(new std::vector<char>());
values3->push_back(-2);
values3->push_back(-1);
values3->push_back(0);
values3->push_back(1);
values3->push_back(2);
boost::shared_ptr<XdmfArray> array3 = XdmfArray::New();
double values3[] = {0.1, 0.1, 1.1, 1.1, 0.1, 1.1, 3.1, 0.1, 2.1, 0.1, 1.1, 1.1, 1.1, 1.1, 1.1, 3.1, 2.1, 2.1,
0.1, 0.1, -1.1, 1.1, 0.1, -1.1, 3.1, 0.1, -2.1, 0.1, 1.1, -1.1, 1.1, 1.1, -1.1, 3.1, 2.1, -2.1};
array3->setValues(0, &values3[0], 36);
array3->setValues(values3);
assert(array3->getSize() == 5);
assert(array3->getType().compare("Char") == 0);
assert(array3->getPrecision() == 1);
assert(array3->getValues().compare("-2 -1 0 1 2 ") == 0);
// Now modify original array
values3->push_back(8);
assert(array3->getSize() == 6);
std::cout << array3->getValues() << std::endl;
std::cout << array3->getType() << std::endl;
const double* const array3Pointer = (const double* const)array3->getValuesPointer();
for(int i=0; i<array3->getSize(); ++i)
{
assert(array3Pointer[i] == values3[i]);
}
boost::shared_ptr<XdmfArray> array4 = XdmfArray::New();
std::vector<long> values4;
for(int i=1000; i<1100; ++i)
{
values4.push_back(i);
}
array4->setValues(values4);
const long* const array4Pointer = (const long* const)array4->getValuesPointer();
for(int i=0; i<array4->getSize(); ++i)
{
assert(array4Pointer[i] == values4[i]);
}
assert(array3->getValues().compare("-2 -1 0 1 2 8 ") == 0);
return 0;
}
......@@ -18,12 +18,12 @@ int main(int argc, char* argv[])
double points[] = {0.1, 0.1, 1.1, 1.1, 0.1, 1.1, 3.1, 0.1, 2.1, 0.1, 1.1, 1.1, 1.1, 1.1, 1.1, 3.1, 2.1, 2.1,
0.1, 0.1, -1.1, 1.1, 0.1, -1.1, 3.1, 0.1, -2.1, 0.1, 1.1, -1.1, 1.1, 1.1, -1.1, 3.1, 2.1, -2.1};
grid->getGeometry()->setGeometryType(XdmfGeometryType::XYZ());
grid->getGeometry()->getArray()->setValues(0, &points[0], 36);
grid->getGeometry()->getArray()->insertValues(0, &points[0], 36);
// Set Topology
int connectivity[] = {0, 1, 7, 6, 3, 4, 10, 9, 1, 2, 8, 7, 4, 5, 11, 10};
grid->getTopology()->setTopologyType(XdmfTopologyType::Hexahedron());
grid->getTopology()->getArray()->setValues(0, &connectivity[0], 16);
grid->getTopology()->getArray()->insertValues(0, &connectivity[0], 16);
// Add Node Attribute
boost::shared_ptr<XdmfAttribute> nodalAttribute = XdmfAttribute::New();
......@@ -31,7 +31,7 @@ int main(int argc, char* argv[])
nodalAttribute->setName("Nodal Attribute");
nodalAttribute->setAttributeType(XdmfAttributeType::Scalar());
nodalAttribute->setAttributeCenter(XdmfAttributeCenter::Node());
nodalAttribute->getArray()->setValues(0, &nodeValues[0], 12);
nodalAttribute->getArray()->insertValues(0, &nodeValues[0], 12);
// Add Cell Attribute
boost::shared_ptr<XdmfAttribute> cellAttribute = XdmfAttribute::New();
......@@ -39,7 +39,7 @@ int main(int argc, char* argv[])
cellAttribute->setName("Cell Attribute");
cellAttribute->setAttributeType(XdmfAttributeType::Scalar());
cellAttribute->setAttributeCenter(XdmfAttributeCenter::Cell());
cellAttribute->getArray()->setValues(0, &cellValues[0], 2);
cellAttribute->getArray()->insertValues(0, &cellValues[0], 2);
grid->insert(nodalAttribute);
grid->insert(cellAttribute);
......
......@@ -4,10 +4,10 @@ if __name__ == "__main__":
values1 = [0, 1, 2, 3]
array1 = XdmfArray.New()
array1.setValues(IntVector(values1))
array1.copyValues(IntVector(values1))
print array1.getValues()
print array1.getType()
array1.setValues(IntVector(values1[0:2]))
array1.copyValues(IntVector(values1[0:2]))
print array1.getValues()
print array1.getType()
......@@ -11,25 +11,25 @@ if __name__ == "__main__":
0.1, 0.1, -1.1, 1.1, 0.1, -1.1, 3.1, 0.1, -2.1, 0.1, 1.1, -1.1, 1.1, 1.1, -1.1, 3.1, 2.1, -2.1]
grid.getGeometry().setGeometryType(XdmfGeometryType.XYZ())
grid.getGeometry().getArray().setValues(DoubleVector(points))
grid.getGeometry().getArray().copyValues(DoubleVector(points))
connectivity = [0, 1, 7, 6, 3, 4, 10, 9, 1, 2, 8, 7, 4, 5, 11, 10]
grid.getTopology().setTopologyType(XdmfTopologyType.Hexahedron())
grid.getTopology().getArray().setValues(IntVector(connectivity))
grid.getTopology().getArray().copyValues(IntVector(connectivity))
nodalAttribute = XdmfAttribute.New()
nodalAttribute.setName("Nodal Attribute")
nodalAttribute.setAttributeType(XdmfAttributeType.Scalar())
nodalAttribute.setAttributeCenter(XdmfAttributeCenter.Node())
nodeVals = [100, 200, 300, 300, 400, 500, 300, 400, 500, 500, 600, 700]
nodalAttribute.getArray().setValues(DoubleVector(nodeVals))
nodalAttribute.getArray().copyValues(DoubleVector(nodeVals))
cellAttribute = XdmfAttribute.New()
cellAttribute.setName("Cell Attribute")
cellAttribute.setAttributeType(XdmfAttributeType.Scalar())
cellAttribute.setAttributeCenter(XdmfAttributeCenter.Cell())
cellVals = [100, 200]
cellAttribute.getArray().setValues(DoubleVector(cellVals))
cellAttribute.getArray().copyValues(DoubleVector(cellVals))
grid.insert(nodalAttribute)
grid.insert(cellAttribute)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment