Commit 24ea357e authored by Andrew J. Burns (Cont's avatar Andrew J. Burns (Cont

std::string compatibility

parent b21b735a
......@@ -887,6 +887,8 @@ XdmfTemplate::preallocateSteps(unsigned int numSteps)
//printf("allocating subsection %u\n", allocateIteration);
//*/
//printf("initializing base array\n");
std::vector<unsigned int> preallocatedSizeVector;
preallocatedSizeVector.push_back(preallocatedSize);
tempArray->initialize(mTrackedArrays[i]->getArrayType(), preallocatedSize);
//printf("writing subsection");
tempArray->accept(mHeavyWriter);
......
......@@ -26,7 +26,7 @@
#include <utility>
#include <stack>
#include <math.h>
#include <cstring>
#include <string.h>
#include "XdmfArray.hpp"
#include "XdmfArrayType.hpp"
#include "XdmfArrayReference.hpp"
......@@ -43,6 +43,10 @@ XDMF_CHILDREN_IMPLEMENTATION(XdmfArray,
HeavyDataController,
Name)
template<>
void
XdmfArray::setArrayType<std::string>();
template<>
XDMFCORE_EXPORT
std::string
......@@ -70,7 +74,7 @@ std::string
XdmfArray::getValue<std::string>(const unsigned int index) const
{
std::string * internalRef = NULL;
XdmfArray::GetValue<std::string> visitor = GetValue<std::string>(index);
XdmfArray::GetValue<std::string> visitor = GetValue<std::string>(this, index);
return ApplyVisitor(this,
visitor,
mArray,
......@@ -85,6 +89,63 @@ XdmfArray::initialize<void>(const unsigned int size)
return this->initialize<char>(size);
}
template <>
XDMFCORE_EXPORT
std::string *
XdmfArray::initialize<std::string>(const unsigned int size)
{
// Set type of variant to type of pointer
if (mCapacity.size() == 0)
{
mCapacity.push_back(0);
}
if (size * mStringSize > mCapacity[0] * getArrayType()->getElementSize())
{
if (size >= mTmpReserveSize)
{
if (mArray)
{
void * newArray = calloc(size, mStringSize);
memcpy(newArray, mArray, mCapacity[0] * getArrayType()->getElementSize());
std::swap(mArray, newArray);
free(newArray);
}
else
{
void * newArray = calloc(size, mStringSize);
std::swap(mArray, newArray);
}
if (mArray == NULL)
{
XdmfError::message(XdmfError::FATAL,
"Error: Allocation Failed.");
}
mCapacity[0] = size;
}
else
{
if (mArray)
{
void * newArray = calloc(mTmpReserveSize, mStringSize);
memcpy(newArray, mArray, mCapacity[0] * getArrayType()->getElementSize());
std::swap(mArray, newArray);
free(newArray);
}
else
{
void * newArray = calloc(mTmpReserveSize, mStringSize);
std::swap(mArray, newArray);
}
mCapacity[0] = mTmpReserveSize;
mTmpReserveSize = 0;
}
}
this->setArrayType<std::string>();
this->setIsChanged(true);
return (std::string *)mArray;
}
//These void calls should never be called
template<>
XDMFCORE_EXPORT
void
......@@ -120,7 +181,6 @@ XdmfArray::GetValues<std::string>::operator()<void>(const void * array) const
}
}
shared_ptr<XdmfArray>
XdmfArray::New()
{
......@@ -130,6 +190,7 @@ XdmfArray::New()
XdmfArray::XdmfArray() :
mArrayPointerNumValues(0),
mStringSize(256),
mName(""),
mTmpReserveSize(256),
mReadMode(XdmfArray::Controller),
......@@ -145,6 +206,7 @@ XdmfArray::XdmfArray() :
XdmfArray::XdmfArray(XdmfArray & refArray):
XdmfItem(refArray),
mDimensions(refArray.getDimensions()),
mStringSize(refArray.mStringSize),
mName(refArray.getName()),
mReadMode(refArray.getReadMode()),
mHasArrayOwnership(true),
......@@ -271,7 +333,8 @@ XdmfArray::getDimensionsString() const
if (mHeavyDataControllers.size() > 0)
{
std::vector<unsigned int> dimensions = this->getDimensions();
return GetValuesString<std::string>(dimensions.size()).getValuesString(&dimensions[0],
return GetValuesString<std::string>(this,
dimensions.size()).getValuesString(&dimensions[0],
dimensions.size());
}
else
......@@ -281,7 +344,8 @@ XdmfArray::getDimensionsString() const
}
else
{
return GetValuesString<std::string>(mDimensions.size()).getValuesString(&mDimensions[0],
return GetValuesString<std::string>(this,
mDimensions.size()).getValuesString(&mDimensions[0],
mDimensions.size());
}
}
......@@ -380,7 +444,7 @@ XdmfArray::getValuesString() const
{
if (isInitialized())
{
XdmfArray::GetValuesString<std::string> visitor = GetValuesString<std::string>(getSize());
XdmfArray::GetValuesString<std::string> visitor = GetValuesString<std::string>(this, getSize());
return ApplyVisitor<std::string>(this,
visitor,
mArray,
......@@ -1010,7 +1074,15 @@ XdmfArray::readController()
shared_ptr<XdmfArray> tempArray = XdmfArray::New();
mHeavyDataControllers[0]->read(tempArray.get());
this->insert(mHeavyDataControllers[0]->getArrayOffset(), tempArray, 0, mHeavyDataControllers[0]->getSize(), 1, 1);
mDimensions = mHeavyDataControllers[0]->getDimensions();
std::vector<unsigned int> initDims;
unsigned int initSize = 1;
for (unsigned int i = 0; i < mHeavyDataControllers[0]->getDimensions().size(); ++i)
{
initSize *= mHeavyDataControllers[0]->getDimensions()[i];
}
initSize += mHeavyDataControllers[0]->getArrayOffset();
initDims.push_back(initSize);
mDimensions = initDims;
}
this->setIsChanged(true);
}
......@@ -2328,109 +2400,6 @@ XdmfArraySwapWithXdmfArray(XDMFARRAY * array, XDMFARRAY * swapArray)
((XdmfArray *)array)->swap(pointerToSwap);
}
void
XdmfArraySwapWithArray(XDMFARRAY * array, void ** pointer, int numValues, int arrayType, int * status)
{
XDMF_ERROR_WRAP_START(status)
switch (arrayType) {
case XDMF_ARRAY_TYPE_UINT8: {
std::vector<unsigned char> swapVector((unsigned char *)(*pointer), (unsigned char *)(*pointer) + numValues);
((XdmfArray *)array)->swap(swapVector);
*pointer = new unsigned char[swapVector.size()];
for (unsigned int i = 0; i < swapVector.size(); ++i)
{
((unsigned char *) (*pointer))[i] = swapVector[i];
}
break;
}
case XDMF_ARRAY_TYPE_UINT16: {
std::vector<unsigned short> swapVector((unsigned short *)(*pointer), (unsigned short *)(*pointer) + numValues);
((XdmfArray *)array)->swap(swapVector);
*pointer = new unsigned short[swapVector.size()];
for (unsigned int i = 0; i < swapVector.size(); ++i)
{
((unsigned short *) (*pointer))[i] = swapVector[i];
}
break;
}
case XDMF_ARRAY_TYPE_UINT32: {
std::vector<unsigned int> swapVector((unsigned int *)(*pointer), (unsigned int *)(*pointer) + numValues);
((XdmfArray *)array)->swap(swapVector);
*pointer = new unsigned int[swapVector.size()];
for (unsigned int i = 0; i < swapVector.size(); ++i)
{
((unsigned int *) (*pointer))[i] = swapVector[i];
}
break;
}
case XDMF_ARRAY_TYPE_INT8: {
std::vector<char> swapVector((char *)(*pointer), (char *)(*pointer) + numValues);
((XdmfArray *)array)->swap(swapVector);
*pointer = new char[swapVector.size()];
for (unsigned int i = 0; i < swapVector.size(); ++i)
{
((char *) (*pointer))[i] = swapVector[i];
}
break;
}
case XDMF_ARRAY_TYPE_INT16: {
std::vector<short> swapVector((short *)(*pointer), (short *)(*pointer) + numValues);
((XdmfArray *)array)->swap(swapVector);
*pointer = new short[swapVector.size()];
for (unsigned int i = 0; i < swapVector.size(); ++i)
{
((short *) (*pointer))[i] = swapVector[i];
}
break;
}
case XDMF_ARRAY_TYPE_INT32: {
std::vector<int> swapVector((int *)(*pointer), (int *)(*pointer) + numValues);
((XdmfArray *)array)->swap(swapVector);
*pointer = new int[swapVector.size()];
for (unsigned int i = 0; i < swapVector.size(); ++i)
{
((int *) (*pointer))[i] = swapVector[i];
}
break;
}
case XDMF_ARRAY_TYPE_INT64: {
std::vector<long> swapVector((long *)(*pointer), (long *)(*pointer) + numValues);
((XdmfArray *)array)->swap(swapVector);
*pointer = new long[swapVector.size()];
for (unsigned int i = 0; i < swapVector.size(); ++i)
{
((long *) (*pointer))[i] = swapVector[i];
}
break;
}
case XDMF_ARRAY_TYPE_FLOAT32: {
std::vector<float> swapVector((float *)(*pointer), (float *)(*pointer) + numValues);
((XdmfArray *)array)->swap(swapVector);
*pointer = new float[swapVector.size()];
for (unsigned int i = 0; i < swapVector.size(); ++i)
{
((float *) (*pointer))[i] = swapVector[i];
}
break;
}
case XDMF_ARRAY_TYPE_FLOAT64: {
std::vector<double> swapVector((double *)(*pointer), (double *)(*pointer) + numValues);
((XdmfArray *)array)->swap(swapVector);
*pointer = new double[swapVector.size()];
for (unsigned int i = 0; i < swapVector.size(); ++i)
{
((double *) (*pointer))[i] = swapVector[i];
}
break;
}
default:
XdmfError::message(XdmfError::FATAL,
"Error: Invalid ArrayType.");
break;
}
XDMF_ERROR_WRAP_END(status)
}
// C Wrappers for parent classes are generated by macros
XDMF_ITEM_C_CHILD_WRAPPER(XdmfArray, XDMFARRAY)
......@@ -108,6 +108,11 @@ public:
Reference
};
friend class XdmfHeavyDataWriter;
friend class XdmfHDF5Writer;
friend class XdmfHeavyDataController;
friend class XdmfHDF5Controller;
/**
* Create a new XdmfArray.
*
......@@ -1375,56 +1380,6 @@ public:
template<typename T>
void setValuesInternal(const shared_ptr<std::vector<T> > array);
/**
* Exchange the contents of the vector with the contents of this
* array. No copy is made. The internal arrays are swapped.
*
* Example of use
*
* C++
*
* @dontinclude ExampleXdmfArray.cpp
* @skipline //#initialization
* @until //#initialization
* @skipline //#initinternalvector
* @until //#initinternalvector
* @skipline //#swapvector
* @until //#swapvector
*
* Python: The Python version only supports swapping XdmfArrays
*
* @param array A vector to exchange values with.
* @return bool whether the swap was successful.
*/
template<typename T>
bool swap(std::vector<T> & array);
/**
* Exchange the contents of the vector with the contents of this
* array. No copy is made. The internal arrays are swapped.
*
* Example of use
*
* C++
*
* @dontinclude ExampleXdmfArray.cpp
* @skipline //#initialization
* @until //#initialization
* @skipline //#initinternalvector
* @until //#initinternalvector
* @skipline //#initsharedvector
* @until //#initsharedvector
* @skipline //#swapsharedvector
* @until //#swapsharedvector
*
* Python: The Python version only supports swapping XdmfArrays
*
* @param array A smart pointer to a vector to exchange values with.
* @return bool whether the swap was successful.
*/
template<typename T>
bool swap(const shared_ptr<std::vector<T> > array);
/**
* Exchange the contents of an XdmfArray with the contents of this
* array. No copy is made. The internal arrays are swapped.
......@@ -1472,6 +1427,8 @@ protected:
const std::vector<shared_ptr<XdmfItem> > & childItems,
const XdmfCoreReader * const reader);
unsigned int mStringSize;
private:
XdmfArray(const XdmfArray &); // Not implemented.
......@@ -1525,7 +1482,7 @@ private:
void internalizeArrayPointer();
unsigned int mArrayPointerNumValues;
// unsigned int mStringSize;
unsigned int mTmpReserveSize;
ReadMode mReadMode;
shared_ptr<XdmfArrayReference> mReference;
......@@ -1631,8 +1588,6 @@ XDMFCORE_EXPORT void XdmfArraySetValuesInternal(XDMFARRAY * array, void * pointe
XDMFCORE_EXPORT void XdmfArraySwapWithXdmfArray(XDMFARRAY * array, XDMFARRAY * swapArray);
XDMFCORE_EXPORT void XdmfArraySwapWithArray(XDMFARRAY * array, void ** pointer, int numValues, int arrayType, int * status);
// C Wrappers for parent classes are generated by macros
XDMF_ITEM_C_CHILD_DECLARE(XdmfArray, XDMFARRAY, XDMFCORE)
......@@ -1721,12 +1676,7 @@ Level##_EXPORT void ClassName##SetValuesInternal( CClassName * array,
int arrayType, \
int transferOwnership, \
int * status); \
Level##_EXPORT void ClassName##SwapWithXdmfArray( CClassName * array, XDMFARRAY * swapArray); \
Level##_EXPORT void ClassName##SwapWithArray( CClassName * array, \
void ** pointer, \
int numValues, \
int arrayType, \
int * status);
Level##_EXPORT void ClassName##SwapWithXdmfArray( CClassName * array, XDMFARRAY * swapArray);
#define XDMF_ARRAY_C_CHILD_WRAPPER(ClassName, CClassName) \
......@@ -1996,16 +1946,6 @@ void
ClassName##SwapWithXdmfArray( CClassName * array, XDMFARRAY * swapArray) \
{ \
XdmfArraySwapWithXdmfArray((XDMFARRAY *)((void *)array), swapArray); \
} \
\
void \
ClassName##SwapWithArray( CClassName * array, \
void ** pointer, \
int numValues, \
int arrayType, \
int * status) \
{ \
XdmfArraySwapWithArray((XDMFARRAY *)((void *)array), pointer, numValues, arrayType, status); \
}
#ifdef __cplusplus
......
......@@ -65,7 +65,7 @@ class XdmfArray::XdmfVisitor
operator()(U * array)
{
printf("blank operator\n");
return NULL;
return 0;
}
virtual
......@@ -73,7 +73,46 @@ class XdmfArray::XdmfVisitor
operator()()
{
printf("blank operator\n");
return NULL;
return 0;
}
std::string mName;
};
template <>
class XdmfArray::XdmfVisitor<std::string>
{
public:
// Parameters other than the array pointer will be passed via the constructor
// This includes output parameters
XdmfVisitor()
{
mName = "none";
}
XdmfVisitor(const XdmfVisitor<std::string> & orig)
{
mName = orig.mName;
}
~XdmfVisitor()
{
}
template <typename U>
std::string
operator()(U * array)
{
printf("blank operator\n");
return "";
}
virtual
std::string
operator()()
{
printf("blank operator\n");
return "";
}
std::string mName;
......@@ -176,6 +215,17 @@ XdmfArray::ApplyVisitor(const XdmfArray * const source, XdmfVisitor<T> & visitor
}
}
template <>
void *
XdmfArray::initialize<void>(const unsigned int size);
template <>
std::string *
XdmfArray::initialize<std::string>(const unsigned int size);
template <>
std::string
XdmfArray::getValue<std::string>(const unsigned int index) const;
class XdmfArray::Erase : public XdmfArray::XdmfVisitor<void> {
public:
......@@ -220,9 +270,15 @@ public:
// And set the last set of characters to 100
for (unsigned int i = mIndex; i < mArray->getSize() - 1; ++i)
{
array[i] = array[i+1];
for (unsigned int j = 0; j < mArray->mStringSize; ++j)
{
((char *)array)[i*mArray->mStringSize + j] = ((char *)array)[i*mArray->mStringSize + j];
}
}
for (unsigned int j = 0; j < mArray->mStringSize; ++j)
{
((char *)array)[mArray->getSize() - 1 + j] = 0;
}
array[mArray->getSize() - 1] = "";
}
private:
......@@ -235,7 +291,9 @@ template <typename T>
class XdmfArray::GetValue : public XdmfArray::XdmfVisitor<T> {
public:
GetValue(const unsigned int index) :
GetValue(const XdmfArray * array,
const unsigned int index) :
mArray(array),
mIndex(index)
{
this->mName = "GetValue";
......@@ -258,9 +316,8 @@ public:
else
{
// TODO STRING
return atof(array[mIndex].c_str());
return atof(std::string(&((char *)array)[mIndex * mArray->mStringSize]).c_str());
}
}
template<typename U>
......@@ -279,6 +336,7 @@ public:
private:
const XdmfArray * mArray;
const unsigned int mIndex;
};
......@@ -286,7 +344,8 @@ template <>
class XdmfArray::GetValue<void> : public XdmfArray::XdmfVisitor<void> {
public:
GetValue(const unsigned int index) :
GetValue(const XdmfArray * array,
const unsigned int index) :
mIndex(index)
{
this->mName = "GetValue";
......@@ -309,7 +368,9 @@ class XdmfArray::GetValue<std::string> :
public XdmfArray::XdmfVisitor<std::string> {
public:
GetValue(const unsigned int index) :
GetValue(const XdmfArray * array,
const unsigned int index) :
mArray(array),
mIndex(index)
{
mName = "GetValue";
......@@ -330,8 +391,7 @@ public:
}
else
{
// TODO STRING
return array[mIndex];
return std::string(&((char *)array)[mIndex * mArray->mStringSize]);
}
}
......@@ -359,6 +419,7 @@ public:
private:
const XdmfArray * mArray;
const unsigned int mIndex;
};
......@@ -410,7 +471,7 @@ public:
// TODO STRING
for(unsigned int i=0; i<mNumValues; ++i) {
mValuesPointer[i*mValuesStride] =
(T)atof(array[mStartIndex + i*mArrayStride].c_str());
(T)atof(&(((char *)array)[(mStartIndex + i*mArrayStride) * mArray->mStringSize]));
}
}
}
......@@ -463,6 +524,22 @@ public:
mName = "GetValues";
}
void
operator()(const std::string * array) const
{
if (array == NULL)
{
return;
}
else
{
for(unsigned int i=0; i<mNumValues; ++i) {
mValuesPointer[i*mValuesStride] =
std::string(&(((char *)array)[(mStartIndex + i*mArrayStride) * mArray->mStringSize]));
}
}
}
template<typename U>
void
operator()(const U * array) const
......@@ -589,15 +666,18 @@ public:
void
operator()(std::string * array) const
{
// TODO STRING
unsigned int size = mStartIndex + (mNumValues - 1) * mArrayStride + 1;
if(array->size() < size) {
array->resize(size);
mDimensions.clear();
if(mArray->getSize() < size) {
mArray->resize(size, std::string(""));
}
for(unsigned int i=0; i<mNumValues; ++i) {
std::stringstream value;
array[mStartIndex + i*mArrayStride] = value.str();
value << mValuesPointer[i*mValuesStride];
const char * valueString = value.str().c_str();
for (unsigned int j = 0; j < value.str().size(); ++j)
{
((char *)array)[((mStartIndex + i*mArrayStride) * mArray->mStringSize) + j] = valueString[j];
}
}
}
......@@ -665,13 +745,16 @@ public:
operator()(std::string * array) const
{
unsigned int size = mStartIndex + (mNumValues - 1) * mArrayStride + 1;
if(array->size() < size) {
array->resize(size);
mDimensions.clear();
if(mArray->getSize() < size) {
mArray->resize(size, std::string(""));
}
// TODO string version
for(unsigned int i=0; i<mNumValues; ++i) {
array[mStartIndex + i*mArrayStride] =
mValuesPointer[i*mValuesStride].c_str();
for (unsigned int j = 0; j < mArray->mStringSize; ++j)
{
((char *)array)[(mStartIndex + i*mArrayStride) * mArray->mStringSize + j] =
mValuesPointer[i*mValuesStride].c_str()[j];
}
}
}
......@@ -756,7 +839,8 @@ template<typename T>
class XdmfArray::GetValuesString : public XdmfArray::XdmfVisitor<T> {
public:
GetValuesString(const int arrayPointerNumValues) :
GetValuesString(XdmfArray * const array,
const int arrayPointerNumValues) :
mArrayPointerNumValues(arrayPointerNumValues)
{
mName = "GetValuesString";
......@@ -766,7 +850,7 @@ public:
T
operator()(U * array)
{
return NULL;
return 0;
}
private:
......@@ -778,7 +862,8 @@ template<>
class XdmfArray::GetValuesString<void> : public XdmfArray::XdmfVisitor<void> {
public:
GetValuesString(const int arrayPointerNumValues) :
GetValuesString(const XdmfArray * const array,
const int arrayPointerNumValues) :
mArrayPointerNumValues(arrayPointerNumValues)
{
mName = "GetValuesString";
......@@ -799,7 +884,9 @@ template<>
class XdmfArray::GetValuesString<std::string> : public XdmfArray::XdmfVisitor<std::string> {
public:
GetValuesString(const int arrayPointerNumValues) :
GetValuesString(const XdmfArray * const array,
const int arrayPointerNumValues) :
mArray(array),
mArrayPointerNumValues(arrayPointerNumValues)
{
mName = "GetValuesString";
......@@ -883,6 +970,22 @@ public:
return std::string(returnstring);
}
std::string
getValuesString(const std::string * const array,
const int numValues) const
{
const int lastIndex = numValues - 1;
if(lastIndex < 0) {
return "";
}
std::string tempReturnString = "";
for(int i=0; i<lastIndex; ++i) {
tempReturnString = tempReturnString + std::string(&(((char *)array)[i * mArray->mStringSize])) + " ";
}
tempReturnString = tempReturnString + std::string(&(((char *)array)[lastIndex * mArray->mStringSize]));
return tempReturnString;
}
template<typename T, typename U>
std::string
getValuesString(const T * const array,
......@@ -937,6 +1040,7 @@ public:
private:
const XdmfArray * const mArray;
const unsigned int mArrayPointerNumValues;
};
......@@ -984,6 +1088,21 @@ public:
array);
}
void
operator()(std::string * array) const
{
unsigned int size = mStartIndex + (mNumValues - 1) * mArrayStride + 1;
if(mArray->getSize() < size) {
mArray->resize(size, std::string(""));
}
// instead, do a series of inserts
for (unsigned int i = 0; i < mNumValues; ++i)
{
mArray->insert<std::string>(mStartIndex + i * mArrayStride,
mArrayToCopy->getValue<std::string>(mValuesStartIndex + i * mValuesStride));
}
}
template<typename T>
void
operator()(T * array) const
......@@ -1129,20 +1248,14 @@ public:
void
operator()(std::string * array) const
{
//resize if outside capacity
unsigned int endpoint = 0;// TODO
array[endpoint+1] = mVal;
mArray->mDimensions.clear();
mArray->insert(mArray->getSize(), mVal);
}
template<typename U>
void
operator()(U * array) const
{
//resize if outside capacity