Commit 9eb1f0c2 authored by Kenneth Leiter's avatar Kenneth Leiter

ENH: Add resize() method to XdmfArray.

parent 364fcbb3
......@@ -147,7 +147,7 @@ public:
virtual std::string printSelf() const;
/**
* Releases all data held by this XdmfArray.
* Release all data held by this XdmfArray.
*/
void releaseData()
{
......@@ -155,6 +155,16 @@ public:
releaseArrayPointer();
}
/**
* 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.
*
* @param numValues the number of values to resize this array to.
* @param val the number to initialized newly created values to, if needed.
*/
template<typename T>
void resize(const unsigned int numValues, const T & val);
/**
* Sets the values of this array to the values stored in the arrayPointer array. No copy is made. Modifications to the array are
* not permitted through the XdmfArray API. Any calls through the XdmfArray API to modify the array (i.e. any non-const function)
......@@ -244,6 +254,10 @@ private:
class InternalizeArrayPointer;
class NewArray;
template<typename T>
class Resize;
struct NullDeleter;
/**
......
......@@ -41,6 +41,28 @@ private:
const int mValuesStride;
};
template<typename T>
class XdmfArray::Resize : public boost::static_visitor <void> {
public:
Resize(const unsigned int numValues, const T & val) :
mNumValues(numValues),
mVal(val)
{
}
template<typename U>
void operator()(boost::shared_ptr<std::vector<U> > & array) const
{
array->resize(mNumValues, (U)mVal);
}
private:
const unsigned int mNumValues;
const T & mVal;
};
struct XdmfArray::NullDeleter
{
void operator()(void const *) const
......@@ -108,6 +130,20 @@ boost::shared_ptr<std::vector<T> > XdmfArray::initialize()
return newArray;
}
template<typename T>
void XdmfArray::resize(const unsigned int numValues, const T & val)
{
if(mHaveArrayPointer)
{
internalizeArrayPointer();
}
if(!mHaveArray)
{
initialize<T>();
}
return boost::apply_visitor(Resize<T>(numValues, val), mArray);
}
template<typename T>
void XdmfArray::setValues(const T * const arrayPointer, const int numValues, const bool transferOwnership)
{
......@@ -176,10 +212,6 @@ bool XdmfArray::swap(std::vector<T> & array)
{
boost::shared_ptr<std::vector<T> > currArray = boost::get<boost::shared_ptr<std::vector<T> > >(mArray);
currArray->swap(array);
//if(currArray->size() == 0)
//{
// mHaveArray = false;
//}
return true;
}
catch(const boost::bad_get& exception)
......
......@@ -222,5 +222,17 @@ int main(int argc, char* argv[])
assert(array5->getSize() == 0);
assert(array7->getSize() == 3);
//
// Various STL like functions
//
boost::shared_ptr<XdmfArray> array8 = XdmfArray::New();
array8->copyValues(0, &values[0], 4, 1, 1);
array8->resize(5, 0);
assert(array8->getValuesString().compare("1 2 3 4 0 ") == 0);
array8->resize(3, 0);
assert(array8->getValuesString().compare("1 2 3 ") == 0);
array8->resize(8, 1.1);
assert(array8->getValuesString().compare("1 2 3 1 1 1 1 1 ") == 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