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:
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> {
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() :
mHaveArray(false),
mHaveArrayPointer(false),
mArrayPointerNumValues(0)
mArrayPointerNumValues(0),
mTmpReserveSize(0)
{
std::cout << "Created Array " << this << std::endl;
}
......@@ -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
{
if(mHaveArray)
......@@ -517,6 +560,22 @@ void XdmfArray::releaseArrayPointer()
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)
{
ArrayVariant tmpArray = array->mArray;
......
......@@ -77,6 +77,11 @@ public:
*/
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.
*
......@@ -155,6 +160,13 @@ public:
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
* to val. If numValues is less than the current size, values at indices larger than numValues are removed.
......@@ -245,6 +257,7 @@ private:
template<typename T>
class CopyValues;
class GetCapacity;
class GetHDF5Type;
class GetPrecision;
class GetSize;
......@@ -253,11 +266,11 @@ private:
class GetValuesString;
class InternalizeArrayPointer;
class NewArray;
class Reserve;
template<typename T>
class Resize;
struct NullDeleter;
/**
......@@ -305,6 +318,7 @@ private:
int mArrayPointerNumValues;
bool mHaveArray;
bool mHaveArrayPointer;
int mTmpReserveSize;
};
#include "XdmfArray.tpp"
......
......@@ -125,6 +125,11 @@ boost::shared_ptr<std::vector<T> > XdmfArray::initialize()
}
// Set type of variant to type of pointer
boost::shared_ptr<std::vector<T> > newArray(new std::vector<T>());
if(mTmpReserveSize > 0)
{
newArray->reserve(mTmpReserveSize);
mTmpReserveSize = 0;
}
mArray = newArray;
mHaveArray = true;
return newArray;
......
......@@ -225,6 +225,10 @@ int main(int argc, char* argv[])
//
// Various STL like functions
//
/**
* Resize
*/
boost::shared_ptr<XdmfArray> array8 = XdmfArray::New();
array8->copyValues(0, &values[0], 4, 1, 1);
array8->resize(5, 0);
......@@ -234,5 +238,11 @@ int main(int argc, char* argv[])
array8->resize(8, 1.1);
assert(array8->getValuesString().compare("1 2 3 1 1 1 1 1 ") == 0);
/**
* Reserve / Capacity
*/
array8->reserve(50);
assert(array8->getCapacity() >= 50);
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