Skip to content
Snippets Groups Projects
Commit e5142758 authored by Christian Engwer's avatar Christian Engwer
Browse files

add an implementation of a dynamic vector, based on std::vector

[[Imported from SVN: r6130]]
parent f50e7c21
No related branches found
No related tags found
No related merge requests found
......@@ -27,6 +27,7 @@ commoninclude_HEADERS = \
debugstream.hh \
deprecated.hh \
densevector.hh \
dynvector.hh \
enumset.hh \
exceptions.hh \
fassign.hh \
......
// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
// vi: set et ts=4 sw=2 sts=2:
// $Id: fvector.hh 6105 2010-08-25 16:06:36Z christi $
#ifndef DUNE_FVECTOR_HH
#define DUNE_FVECTOR_HH
#include <cmath>
#include <cstddef>
#include <cstdlib>
#include <complex>
#include <cstring>
#include <limits>
#include "exceptions.hh"
#include "genericiterator.hh"
#include <vector>
#include "densevector.hh"
namespace Dune {
/** @addtogroup DenseMatVec
@{
*/
template< class K >
class DynamicVector : public DenseVector< std::vector<K> >
{
public:
typedef typename DenseVector< std::vector<K> >::size_type size_type;
//! Constructor making uninitialized vector
DynamicVector() {}
//! Constructor making vector with identical coordinates
DynamicVector (size_type n, const K& t) :
DenseVector< std::vector<K> > (n,t)
{}
//! Constructor making vector with identical coordinates
DynamicVector (const DenseVector< std::vector<K> > & x) :
DenseVector< std::vector<K> > (x)
{}
using DenseVector< std::vector<K> >::operator=;
};
/** \brief Read a DynamicVector from an input stream
* \relates DynamicVector
*
* \note This operator is STL compilant, i.e., the content of v is only
* changed if the read operation is successful.
*
* \param[in] in std :: istream to read from
* \param[out] v DynamicVector to be read
*
* \returns the input stream (in)
*/
template<class K>
inline std::istream &operator>> ( std::istream &in,
DynamicVector<K> &v )
{
DynamicVector<K> w(v);
for( typename DynamicVector<K>::size_type i = 0; i < w.size(); ++i )
in >> w[ i ];
if(in)
v = w;
return in;
}
/** @} end documentation */
} // end namespace
#endif
......@@ -8,6 +8,7 @@ TESTPROGS = \
configparsertest \
conversiontest \
deprtuplestest \
dynvectortest \
fassigntest \
float_cmp \
fmatrixtest \
......@@ -109,6 +110,8 @@ iteratorfacadetest_SOURCES = iteratorfacadetest.cc iteratorfacadetest.hh \
iteratorfacadetest2_SOURCES = iteratorfacadetest2.cc
dynvectortest_SOURCES = dynvectortest.cc
fvectortest_SOURCES = fvectortest.cc
fmatrixtest_SOURCES = fmatrixtest.cc
......
// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
// vi: set et ts=4 sw=2 sts=2:
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <dune/common/dynvector.hh>
#include <dune/common/exceptions.hh>
#include <iostream>
using Dune::DynamicVector;
template<class ct>
void dynamicVectorTest(int d) {
ct a = 1;
DynamicVector<ct> v(d,1);
DynamicVector<ct> w(d,2);
DynamicVector<ct> z(d,2);
bool b;
// Test whether the norm methods compile
(w+v).two_norm();
(w+v).two_norm2();
(w+v).one_norm();
(w+v).one_norm_real();
(w+v).infinity_norm();
(w+v).infinity_norm_real();
// test op(vec,vec)
z = v + w;
z = v - w;
DynamicVector<ct> z2 = v + w;
w -= v;
w += v;
// test op(vec,scalar)
w +=a;
w -= a;
w *= a;
w /= a;
// test scalar product, axpy
a = v * w;
z = v.axpy(a,w);
// test comparison
b = (w != v);
b = (w == v);
// test istream operator
std::stringstream s;
for (int i=0; i<d; i++)
{
s << i << " ";
v[i] = i;
}
s >> w;
assert(v == w);
}
int main()
{
try {
for (int d=1; d<6; d++)
{
dynamicVectorTest<int>(d);
dynamicVectorTest<float>(d);
dynamicVectorTest<double>(d);
}
} catch (Dune::Exception& e) {
std::cerr << e << std::endl;
return 1;
} catch (...) {
std::cerr << "Generic exception!" << std::endl;
return 2;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment