Merge pull request #622 from ye-luo/init-Vector-val

Initialize Vector value similarly to std::vector.
This commit is contained in:
Mark Dewing 2018-01-04 12:08:12 -06:00 committed by GitHub
commit bb143b5615
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 9 additions and 4 deletions

View File

@ -43,12 +43,12 @@ public:
/** constructor with size n*/
explicit inline
Vector(size_t n=0): nLocal(n), nAllocated(0), X(nullptr)
Vector(size_t n=0, Type_t val = Type_t()): nLocal(n), nAllocated(0), X(nullptr)
{
if(n)
{
resize_impl(n);
std::fill_n(X, n, T());
std::fill_n(X, n, val);
}
}
@ -117,14 +117,19 @@ public:
}
///resize
inline void resize(size_t n)
inline void resize(size_t n, Type_t val = Type_t())
{
if(nLocal>nAllocated)
throw std::runtime_error("Resize not allowed on Vector constructed by initialized memory.");
if(n>nAllocated)
{
resize_impl(n);
std::fill_n(X, n, T());
std::fill_n(X, n, val);
}
else if(n>nLocal)
{
std::fill_n(X+nLocal, n-nLocal, val);
nLocal=n;
}
else
nLocal=n;