// -*-C++-*- // Copyright (C) 2005 // Christian Stimming // This library is free software; you can redistribute it and/or // modify it under the terms of the GNU Lesser General Public License as // published by the Free Software Foundation; either version 2, or (at // your option) any later version. // This library is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU Lesser General Public License for more details. // You should have received a copy of the GNU Lesser General Public License along // with this library; see the file COPYING. If not, write to the Free // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, // USA. #ifndef VTMPL_H #define VTMPL_H #include "lafnames.h" #include LA_EXCEPTION_H // for assert() /** This file and this namespace includes the template functions * that are common to all simple vector classes. * * This way we do not start to switch from normal classes to * template classes to the outside, but in the inside * implementation all classes already use the identical function * code. Of course this has the advantage that one bugfix will be * available in all classes at once.*/ namespace vtmpl { /** Resize to a \e new vector of size n. The element values of the * new vector are \e uninitialized, even if resizing to a smaller * vector. */ template inline int resize(V& vec, int new_size) { assert(new_size >= 0); // this actually frees memory first, then resizes it. it reduces // internal fragmentation of memory pool, and the resizing of // matrices > 1/2 available memory. vec.ref(V(0)); // possibly free up destination if (new_size > 0) vec.ref(V(new_size)); return new_size; } /** Copy elements of s into the memory space referenced by the * left-hand side, without first releasing it. The effect is * that if other vectors share memory with left-hand side, * they too will be affected. Note that the size of s must be * the same as that of the left-hand side vector. * * @note If you rather wanted to create a new copy of \c s, * you should use \c copy() instead. */ template inline V& inject(V& dest, const V& src) { assert(src.size() == dest.size()); typedef typename V::value_type value_type; const value_type *srcptr = src.addr(); value_type *destptr = dest.addr(); int N = dest.size(); //for (int i=0; i inline V& copy(V& dest, const V& src) { dest.resize(src.size()); return inject(dest, src); } /** Prints this vector to the ostream. */ template inline std::ostream& print(std::ostream& s, const V& m) { int n = m.size(); for (int i=0; i inline V& assign(V& vec, typename V::value_type scalar) { typename V::value_type *iter = vec.addr(); //for (int i=0; i