Commit 4a0e2da2 authored by Kenneth Leiter's avatar Kenneth Leiter

ENH: Add reserve() and getCapacity() methods to XdmfArray.

parent 9eb1f0c2
...@@ -62,6 +62,20 @@ private: ...@@ -62,6 +62,20 @@ private:
const int mValuesStride; const int mValuesStride;
}; };
class XdmfArray::GetCapacity : public boost::static_visitor <unsigned int> {
public:
GetCapacity()
{
}
template<typename T>
unsigned int operator()(const boost::shared_ptr<std::vector<T> > & array) const
{
return array->capacity();
}
};
class XdmfArray::GetHDF5Type : public boost::static_visitor <hid_t> { class XdmfArray::GetHDF5Type : public boost::static_visitor <hid_t> {
public: public:
...@@ -374,10 +388,30 @@ public: ...@@ -374,10 +388,30 @@ public:
} }
}; };
class XdmfArray::Reserve : public boost::static_visitor <void> {
public:
Reserve(const unsigned int size):
mSize(size)
{
}
template<typename T>
void operator()(boost::shared_ptr<std::vector<T> > & array) const
{
array->reserve(mSize);
}
private:
const unsigned int mSize;
};
XdmfArray::XdmfArray() : XdmfArray::XdmfArray() :
mHaveArray(false), mHaveArray(false),
mHaveArrayPointer(false), mHaveArrayPointer(false),
mArrayPointerNumValues(0) mArrayPointerNumValues(0),
mTmpReserveSize(0)
{ {
std::cout << "Created Array " << this << std::endl; std::cout << "Created Array " << this << std::endl;
} }
...@@ -412,6 +446,15 @@ void XdmfArray::clear() ...@@ -412,6 +446,15 @@ void XdmfArray::clear()
} }
} }
unsigned int XdmfArray::getCapacity() const
{
if(mHaveArray)
{
return boost::apply_visitor(GetCapacity(), mArray);
}
return 0;
}
hid_t XdmfArray::getHDF5Type() const hid_t XdmfArray::getHDF5Type() const
{ {
if(mHaveArray) if(mHaveArray)
...@@ -517,6 +560,22 @@ void XdmfArray::releaseArrayPointer() ...@@ -517,6 +560,22 @@ void XdmfArray::releaseArrayPointer()
mHaveArrayPointer = false; mHaveArrayPointer = false;
} }
void XdmfArray::reserve(const unsigned int size)
{
if(mHaveArrayPointer)
{
internalizeArrayPointer();
}
if(!mHaveArray)
{
mTmpReserveSize = size;
}
else
{
boost::apply_visitor(Reserve(size), mArray);
}
}
void XdmfArray::swap(boost::shared_ptr<XdmfArray> array) void XdmfArray::swap(boost::shared_ptr<XdmfArray> array)
{ {
ArrayVariant tmpArray = array->mArray; ArrayVariant tmpArray = array->mArray;
......
...@@ -77,6 +77,11 @@ public: ...@@ -77,6 +77,11 @@ public:
*/ */
virtual void clear(); virtual void clear();
/**
* Get the capacity of this array (the number of values this array can store without reallocation).
*/
unsigned int getCapacity() const;
/** /**
* Get the hdf5 data type of this array. * Get the hdf5 data type of this array.
* *
...@@ -155,6 +160,13 @@ public: ...@@ -155,6 +160,13 @@ public:
releaseArrayPointer(); releaseArrayPointer();
} }
/**
* Set the capacity of the XdmfArray to at least size.
*
* @param size the capacity to set this XdmfArray to.
*/
void reserve(const unsigned int size);
/** /**
* Resizes the XdmfArray to contain numValues. If numValues is larger than the current size, append values to end equal * Resizes the XdmfArray to contain numValues. If numValues is larger than the current size, append values to end equal
* to val. If numValues is less than the current size, values at indices larger than numValues are removed. * to val. If numValues is less than the current size, values at indices larger than numValues are removed.
...@@ -245,6 +257,7 @@ private: ...@@ -245,6 +257,7 @@ private:
template<typename T> template<typename T>
class CopyValues; class CopyValues;
class GetCapacity;
class GetHDF5Type; class GetHDF5Type;
class GetPrecision; class GetPrecision;
class GetSize; class GetSize;
...@@ -253,11 +266,11 @@ private: ...@@ -253,11 +266,11 @@ private:
class GetValuesString; class GetValuesString;
class InternalizeArrayPointer; class InternalizeArrayPointer;
class NewArray; class NewArray;
class Reserve;
template<typename T> template<typename T>
class Resize; class Resize;
struct NullDeleter; struct NullDeleter;
/** /**
...@@ -305,6 +318,7 @@ private: ...@@ -305,6 +318,7 @@ private:
int mArrayPointerNumValues; int mArrayPointerNumValues;
bool mHaveArray; bool mHaveArray;
bool mHaveArrayPointer; bool mHaveArrayPointer;
int mTmpReserveSize;
}; };
#include "XdmfArray.tpp" #include "XdmfArray.tpp"
......
...@@ -125,6 +125,11 @@ boost::shared_ptr<std::vector<T> > XdmfArray::initialize() ...@@ -125,6 +125,11 @@ boost::shared_ptr<std::vector<T> > XdmfArray::initialize()
} }
// Set type of variant to type of pointer // Set type of variant to type of pointer
boost::shared_ptr<std::vector<T> > newArray(new std::vector<T>()); boost::shared_ptr<std::vector<T> > newArray(new std::vector<T>());
if(mTmpReserveSize > 0)
{
newArray->reserve(mTmpReserveSize);
mTmpReserveSize = 0;
}
mArray = newArray; mArray = newArray;
mHaveArray = true; mHaveArray = true;
return newArray; return newArray;
......
...@@ -225,6 +225,10 @@ int main(int argc, char* argv[]) ...@@ -225,6 +225,10 @@ int main(int argc, char* argv[])
// //
// Various STL like functions // Various STL like functions
// //
/**
* Resize
*/
boost::shared_ptr<XdmfArray> array8 = XdmfArray::New(); boost::shared_ptr<XdmfArray> array8 = XdmfArray::New();
array8->copyValues(0, &values[0], 4, 1, 1); array8->copyValues(0, &values[0], 4, 1, 1);
array8->resize(5, 0); array8->resize(5, 0);
...@@ -234,5 +238,11 @@ int main(int argc, char* argv[]) ...@@ -234,5 +238,11 @@ int main(int argc, char* argv[])
array8->resize(8, 1.1); array8->resize(8, 1.1);
assert(array8->getValuesString().compare("1 2 3 1 1 1 1 1 ") == 0); assert(array8->getValuesString().compare("1 2 3 1 1 1 1 1 ") == 0);
/**
* Reserve / Capacity
*/
array8->reserve(50);
assert(array8->getCapacity() >= 50);
return 0; return 0;
} }
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