Commit 4157e301 authored by Ken Leiter (Civ ARL/CISD) kleiter's avatar Ken Leiter (Civ ARL/CISD) kleiter

Merge branch 'XdmfArraySmartPointer'

Conflicts:
	Xdmf.py
	XdmfPython.cpp
parents 6cde700a 8d06285b
......@@ -61,24 +61,81 @@ namespace boost {
%include XdmfTopology.hpp
%include XdmfTopologyType.hpp
%template(CharVector) std::vector<char>;
%template(ShortVector) std::vector<short>;
%template(IntVector) std::vector<int>;
%template(LongVector) std::vector<long>;
%template(FloatVector) std::vector<float>;
%template(DoubleVector) std::vector<double>;
%template(UCharVector) std::vector<unsigned char>;
%template(UShortVector) std::vector<unsigned short>;
%template(UIntVector) std::vector<unsigned int>;
// Provide accessors from python lists to XdmfArrays
%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>;
void copyValueAsChar(int index, char value) {
$self->copyValues(index, &value);
}
void copyValueAsShort(int index, short value) {
$self->copyValues(index, &value);
}
void copyValueAsInt(int index, int value) {
$self->copyValues(index, &value);
}
void copyValueAsLong(int index, long value) {
$self->copyValues(index, &value);
}
void copyValueAsFloat(int index, float value) {
$self->copyValues(index, &value);
}
void copyValueAsDouble(int index, double value) {
$self->copyValues(index, &value);
}
void copyValueAsUChar(int index, unsigned char value) {
$self->copyValues(index, &value);
}
void copyValueAsUShort(int index, unsigned short value) {
$self->copyValues(index, &value);
}
void copyValueAsUInt(int index, unsigned int value) {
$self->copyValues(index, &value);
}
};
%extend boost::shared_ptr<XdmfArray> {
%pythoncode {
def copyValuesAsChar(self, startIndex, values):
for i in range(0, len(values)):
self.copyValueAsChar(i+startIndex, values[i])
def copyValuesAsShort(self, startIndex, values):
for i in range(0, len(values)):
self.copyValueAsShort(i+startIndex, values[i])
def copyValuesAsInt(self, startIndex, values):
for i in range(0, len(values)):
self.copyValueAsInt(i+startIndex, values[i])
def copyValuesAsLong(self, startIndex, values):
for i in range(0, len(values)):
self.copyValueAsLong(i+startIndex, values[i])
def copyValuesAsFloat(self, startIndex, values):
for i in range(0, len(values)):
self.copyValueAsFloat(i+startIndex, values[i])
def copyValuesAsDouble(self, startIndex, values):
for i in range(0, len(values)):
self.copyValueAsDouble(i+startIndex, values[i])
def copyValuesAsUChar(self, startIndex, values):
for i in range(0, len(values)):
self.copyValueAsUChar(i+startIndex, values[i])
def copyValuesAsUShort(self, startIndex, values):
for i in range(0, len(values)):
self.copyValueAsUShort(i+startIndex, values[i])
def copyValuesAsUInt(self, startIndex, values):
for i in range(0, len(values)):
self.copyValueAsUInt(i+startIndex, values[i])
};
};
This diff is collapsed.
......@@ -9,159 +9,210 @@
#include "XdmfVisitor.hpp"
#include <sstream>
class XdmfArrayGetPrecision : public boost::static_visitor <int> {
class XdmfArrayClear : public boost::static_visitor <void> {
public:
XdmfArrayGetPrecision()
XdmfArrayClear()
{
}
int operator()(const std::vector<char> & array) const
template<typename T> void operator()(const boost::shared_ptr<std::vector<T> > & array) const
{
return 1;
return array->clear();
}
};
class XdmfArrayCopyArrayValues : public boost::static_visitor <void> {
public:
int operator()(const std::vector<short> & array) const
XdmfArrayCopyArrayValues(int startIndex, int valuesStartIndex, int numValues, int arrayStride, int valuesStride) :
mStartIndex(startIndex),
mValuesStartIndex(valuesStartIndex),
mNumValues(numValues),
mArrayStride(arrayStride),
mValuesStride(valuesStride)
{
return 2;
}
int operator()(const std::vector<int> & array) const
template<typename T, typename U> void operator()(const boost::shared_ptr<std::vector<T> > & array, const boost::shared_ptr<std::vector<U> > & arrayToCopy) const
{
return 4;
int size = mStartIndex + mNumValues;
if(mArrayStride > 1)
{
size = mStartIndex + mNumValues * mArrayStride - 1;
}
if(array->size() < size)
{
array->resize(size);
}
for(int i=0; i<mNumValues; ++i)
{
array->operator[](mStartIndex + i*mArrayStride) = (T)arrayToCopy->operator[](mValuesStartIndex + i*mValuesStride);
}
}
int operator()(const std::vector<long> & array) const
private:
int mStartIndex;
int mValuesStartIndex;
int mNumValues;
int mArrayStride;
int mValuesStride;
};
class XdmfArrayGetHDF5Type : public boost::static_visitor <hid_t> {
public:
XdmfArrayGetHDF5Type()
{
return 8;
}
int operator()(const std::vector<float> & array) const
hid_t operator()(const boost::shared_ptr<std::vector<char> > & array) const
{
return 4;
return H5T_NATIVE_CHAR;
}
int operator()(const std::vector<double> & array) const
hid_t operator()(const boost::shared_ptr<std::vector<short> > & array) const
{
return 8;
return H5T_NATIVE_SHORT;
}
int operator()(const std::vector<unsigned char> & array) const
hid_t operator()(const boost::shared_ptr<std::vector<int> > & array) const
{
return 1;
return H5T_NATIVE_INT;
}
int operator()(const std::vector<unsigned short> & array) const
hid_t operator()(const boost::shared_ptr<std::vector<long> > & array) const
{
return 2;
return H5T_NATIVE_LONG;
}
int operator()(const std::vector<unsigned int> & array) const
hid_t operator()(const boost::shared_ptr<std::vector<float> > & array) const
{
return 4;
return H5T_NATIVE_FLOAT;
}
hid_t operator()(const boost::shared_ptr<std::vector<double> > & array) const
{
return H5T_NATIVE_DOUBLE;
}
hid_t operator()(const boost::shared_ptr<std::vector<unsigned char> > & array) const
{
return H5T_NATIVE_UCHAR;
}
hid_t operator()(const boost::shared_ptr<std::vector<unsigned short> > & array) const
{
return H5T_NATIVE_USHORT;
}
hid_t operator()(const boost::shared_ptr<std::vector<unsigned int> > & array) const
{
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,62 +223,64 @@ 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();
}
};
class XdmfArrayGetValuesPointer : public boost::static_visitor <const void* const> {
public:
XdmfArrayGetValuesPointer()
{
}
template<typename T> const void* const operator()(const boost::shared_ptr<std::vector<T> > & array) const
{
return array.size();
return &array->operator[](0);
}
};
class XdmfArrayGetValues : public boost::static_visitor <std::string> {
class XdmfArrayGetValuesString : public boost::static_visitor <std::string> {
public:
XdmfArrayGetValues()
XdmfArrayGetValuesString()
{
}
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();
}
};
class XdmfArrayGetValuesPointer : public boost::static_visitor <void*> {
class XdmfArrayNewArray : public boost::static_visitor <void> {
public:
XdmfArrayGetValuesPointer()
{
}
template<typename T> void* operator()(std::vector<T> & array) const
XdmfArrayNewArray()
{
return &array[0];
}
};
class XdmfArrayGetValuesPointerConst : public boost::static_visitor <const void* const> {
public:
XdmfArrayGetValuesPointerConst()
template<typename T> void operator()(boost::shared_ptr<std::vector<T> > & array) const
{
}
template<typename T> const void* const operator()(const std::vector<T> & array) const
{
return &array[0];
boost::shared_ptr<std::vector<T> > newArray(new std::vector<T>());
array = newArray;
}
};
......@@ -242,6 +295,28 @@ XdmfArray::~XdmfArray()
std::cout << "Deleted Array " << this << std::endl;
}
void XdmfArray::copyValues(int startIndex, boost::shared_ptr<XdmfArray> values, int valuesStartIndex, int numValues, int arrayStride, int valuesStride)
{
if(!mInitialized)
{
// Copy the values variant in order to get the type (only taking smart pointer so no worries about large copies)
mArray = values->getVariant();
// Reinitialize variant array to contain new array with same type.
boost::apply_visitor( XdmfArrayNewArray(), mArray);
//boost::shared_ptr<std::vector<double> > newArray(new std::vector<double>());
//mArray = newArray;
//std::cout << boost::get<boost::shared_ptr<std::vector<double> > >(mArray)->operator[](0) << std::endl;
//std::cout << this->getValuesString() << std::endl;
}
boost::apply_visitor( XdmfArrayCopyArrayValues(startIndex, valuesStartIndex, numValues, arrayStride, valuesStride), mArray, values->getVariant());
}
void XdmfArray::clear()
{
return boost::apply_visitor( XdmfArrayClear(), mArray);
}
hid_t XdmfArray::getHDF5Type() const
{
return boost::apply_visitor( XdmfArrayGetHDF5Type(), mArray);
......@@ -262,19 +337,14 @@ std::string XdmfArray::getType() const
return boost::apply_visitor( XdmfArrayGetType(), mArray);
}
std::string XdmfArray::getValues() const
{
return boost::apply_visitor( XdmfArrayGetValues(), mArray);
}
void* XdmfArray::getValuesPointer()
const void* const XdmfArray::getValuesPointer() const
{
return boost::apply_visitor( XdmfArrayGetValuesPointer(), mArray);
}
const void* const XdmfArray::getValuesPointer() const
std::string XdmfArray::getValuesString() const
{
return boost::apply_visitor( XdmfArrayGetValuesPointerConst(), mArray);
return boost::apply_visitor( XdmfArrayGetValuesString(), mArray);
}
std::string XdmfArray::printSelf() const
......
......@@ -15,10 +15,10 @@
#include <vector>
template<typename T>
class XdmfArraySetValues : public boost::static_visitor <void> {
class XdmfArrayCopyValues : public boost::static_visitor <void> {
public:
XdmfArraySetValues(int startIndex, T* valuesPointer, int numValues, int arrayStride = 1, int valuesStride = 1) :
XdmfArrayCopyValues(int startIndex, T* valuesPointer, int numValues = 1, int arrayStride = 1, int valuesStride = 1) :
mStartIndex(startIndex),
mValuesPointer(valuesPointer),
mNumValues(numValues),
......@@ -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];
}
}
......@@ -53,11 +53,88 @@ private:
int mValuesStride;
};
template <typename T>
class XdmfArrayGetValues : public boost::static_visitor <boost::shared_ptr<std::vector<T> > > {
public:
XdmfArrayGetValues()
{
}
boost::shared_ptr<std::vector<T> > operator()(const boost::shared_ptr<std::vector<T> > array) const
{
return array;
}
template <typename U>
boost::shared_ptr<std::vector<T> > operator()(const boost::shared_ptr<std::vector<U> > array) const
{
return boost::shared_ptr<std::vector<T> >();
}
};
template <typename T>
class XdmfArrayGetValuesConst : public boost::static_visitor <const boost::shared_ptr<const std::vector<T> > > {
public:
XdmfArrayGetValuesConst()
{
}
const boost::shared_ptr<const std::vector<T> > operator()(const boost::shared_ptr<const std::vector<T> > array) const
{
return array;
}
template <typename U>
const boost::shared_ptr<const std::vector<T> > operator()(const boost::shared_ptr<const std::vector<U> > array) const
{
return boost::shared_ptr<std::vector<T> >();
}
};
class XdmfArray : public XdmfItem {
public:
XdmfNewMacro(XdmfArray);
/**
* Copy values from an XdmfArray into this array.
*
* @param startIndex the index in this array to begin insertion.
* @param values a shared pointer to an XdmfArray to copy into this array.
* @param numValues the number of values to copy into this array.
* @param arrayStride number of values to stride in this array between each copy.
* @param valuesStride number of values to stride in the XdmfArray between each copy.
*/
void copyValues(int startIndex, boost::shared_ptr<XdmfArray> values, int valuesStartIndex= 0, int numValues = 1, int arrayStride = 1, int valuesStride = 1);
/**
* Copy values from an array 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.
* @param numValues the number of values to copy into this array.
* @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.
*/