yat/utility/Vector.h

Code
Comments
Other
Rev Date Author Line
680 11 Oct 06 jari 1 #ifndef _theplu_yat_utility_vector_
680 11 Oct 06 jari 2 #define _theplu_yat_utility_vector_
616 31 Aug 06 jari 3
22 05 Aug 03 peter 4 // $Id$
22 05 Aug 03 peter 5
570 05 Apr 06 jari 6 /*
570 05 Apr 06 jari 7   Copyright (C) 2003 Daniel Dalevi, Peter Johansson
2119 12 Dec 09 peter 8   Copyright (C) 2004 Jari Häkkinen, Peter Johansson
2119 12 Dec 09 peter 9   Copyright (C) 2005 Jari Häkkinen, Peter Johansson, Markus Ringnér
4359 23 Aug 23 peter 10   Copyright (C) 2006 Jari Häkkinen
2119 12 Dec 09 peter 11   Copyright (C) 2007 Jari Häkkinen, Peter Johansson, Markus Ringnér
2119 12 Dec 09 peter 12   Copyright (C) 2008 Jari Häkkinen, Peter Johansson
4359 23 Aug 23 peter 13   Copyright (C) 2009, 2012, 2017 Peter Johansson
570 05 Apr 06 jari 14
1437 25 Aug 08 peter 15   This file is part of the yat library, http://dev.thep.lu.se/yat
570 05 Apr 06 jari 16
675 10 Oct 06 jari 17   The yat library is free software; you can redistribute it and/or
675 10 Oct 06 jari 18   modify it under the terms of the GNU General Public License as
1486 09 Sep 08 jari 19   published by the Free Software Foundation; either version 3 of the
675 10 Oct 06 jari 20   License, or (at your option) any later version.
570 05 Apr 06 jari 21
675 10 Oct 06 jari 22   The yat library is distributed in the hope that it will be useful,
675 10 Oct 06 jari 23   but WITHOUT ANY WARRANTY; without even the implied warranty of
675 10 Oct 06 jari 24   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
570 05 Apr 06 jari 25   General Public License for more details.
570 05 Apr 06 jari 26
570 05 Apr 06 jari 27   You should have received a copy of the GNU General Public License
1487 10 Sep 08 jari 28   along with yat. If not, see <http://www.gnu.org/licenses/>.
570 05 Apr 06 jari 29 */
570 05 Apr 06 jari 30
3587 19 Jan 17 peter 31 #include "config_public.h"
3587 19 Jan 17 peter 32
1027 02 Feb 08 peter 33 #include "VectorMutable.h"
680 11 Oct 06 jari 34 #include "Exception.h"
420 02 Dec 05 jari 35
1792 11 Feb 09 peter 36 #include <cstddef> // size_t
1680 29 Dec 08 peter 37 #include <iosfwd>
1680 29 Dec 08 peter 38 #include <stdexcept>
12 19 Jun 03 daniel 39
12 19 Jun 03 daniel 40 #include <gsl/gsl_vector.h>
12 19 Jun 03 daniel 41
42 26 Feb 04 jari 42 namespace theplu {
680 11 Oct 06 jari 43 namespace yat {
616 31 Aug 06 jari 44 namespace utility {
420 02 Dec 05 jari 45
686 16 Oct 06 jari 46   /**
2687 27 Feb 12 peter 47      @brief This is the yat interface to GSL vector.
686 16 Oct 06 jari 48
686 16 Oct 06 jari 49      \par File streams:
686 16 Oct 06 jari 50      Reading and writing vectors to file streams are of course
686 16 Oct 06 jari 51      supported. These are implemented without using GSL functionality,
686 16 Oct 06 jari 52      and thus binary read and write to streams are not supported.
686 16 Oct 06 jari 53
1058 08 Feb 08 peter 54      \par Vector views: GSL vector views are supported in classes
1058 08 Feb 08 peter 55      VectorView and VectorConstView
686 16 Oct 06 jari 56
686 16 Oct 06 jari 57   */
686 16 Oct 06 jari 58
1120 21 Feb 08 peter 59   class Vector : public VectorMutable
42 26 Feb 04 jari 60   {
42 26 Feb 04 jari 61   public:
754 17 Feb 07 jari 62     /**
754 17 Feb 07 jari 63        \brief The default constructor.
754 17 Feb 07 jari 64     */
1120 21 Feb 08 peter 65     Vector(void);
12 19 Jun 03 daniel 66
754 17 Feb 07 jari 67     /**
754 17 Feb 07 jari 68        \brief Allocates memory space for \a n elements, and sets all
754 17 Feb 07 jari 69        elements to \a init_value.
2687 27 Feb 12 peter 70
754 17 Feb 07 jari 71        \throw GSL_error if memory allocation fails.
754 17 Feb 07 jari 72     */
1651 15 Dec 08 peter 73     explicit Vector(size_t n, double init_value=0);
12 19 Jun 03 daniel 74
754 17 Feb 07 jari 75     /**
754 17 Feb 07 jari 76        \brief The copy constructor.
754 17 Feb 07 jari 77
754 17 Feb 07 jari 78        \throw A GSL_error is indirectly thrown if memory allocation
754 17 Feb 07 jari 79        fails.
754 17 Feb 07 jari 80     */
1120 21 Feb 08 peter 81     Vector(const Vector& other);
12 19 Jun 03 daniel 82
3605 27 Jan 17 peter 83     /**
3605 27 Jan 17 peter 84        \brief Constructor from a vector expression
3605 27 Jan 17 peter 85      */
3605 27 Jan 17 peter 86     template<class T>
3605 27 Jan 17 peter 87     Vector(const VectorExpression<T>& other)
3623 09 Feb 17 peter 88       : VectorMutable(detail::create_gsl_vector_copy(other.gsl_vector_p()))
3605 27 Jan 17 peter 89     {
3605 27 Jan 17 peter 90     }
3605 27 Jan 17 peter 91
754 17 Feb 07 jari 92     /**
3587 19 Jan 17 peter 93        \brief The move constructor.
3596 21 Jan 17 peter 94
3596 21 Jan 17 peter 95        \since new in yat 0.15
3587 19 Jan 17 peter 96     */
3691 14 Sep 17 peter 97     Vector(Vector&& other) noexcept;
3605 27 Jan 17 peter 98
3605 27 Jan 17 peter 99     /**
3605 27 Jan 17 peter 100        \brief Move constructor from Vector expression
3605 27 Jan 17 peter 101
3605 27 Jan 17 peter 102        \since new in yat 0.15
3605 27 Jan 17 peter 103      */
3605 27 Jan 17 peter 104     template<class T>
3605 27 Jan 17 peter 105     Vector(VectorExpression<T>&& other)
3605 27 Jan 17 peter 106       : VectorMutable(other.gsl_vector_p())
3605 27 Jan 17 peter 107     {
3605 27 Jan 17 peter 108       other.gsl_vector_p(NULL);
3605 27 Jan 17 peter 109     }
3587 19 Jan 17 peter 110
3587 19 Jan 17 peter 111     /**
1058 08 Feb 08 peter 112        \brief Copy a VectorBase.
754 17 Feb 07 jari 113
1009 01 Feb 08 peter 114        \throw A GSL_error is indirectly thrown if memory allocation
1009 01 Feb 08 peter 115        fails.
754 17 Feb 07 jari 116     */
1651 15 Dec 08 peter 117     explicit Vector(const VectorBase& other);
257 04 Mar 05 jari 118
754 17 Feb 07 jari 119     /**
754 17 Feb 07 jari 120        \brief The istream constructor.
754 17 Feb 07 jari 121
754 17 Feb 07 jari 122        Either elements should be separated with white space characters
754 17 Feb 07 jari 123        (default), or elements should be separated by the delimiter \a
754 17 Feb 07 jari 124        sep. When delimiter \a sep is used empty elements are stored as
754 17 Feb 07 jari 125        NaN's (except that empty lines are ignored). The end of input
1120 21 Feb 08 peter 126        to the Vector is at end of file marker.
754 17 Feb 07 jari 127
759 19 Feb 07 jari 128        \throw GSL_error if memory allocation fails, IO_error if
759 19 Feb 07 jari 129        unexpected input is found in the input stream.
754 17 Feb 07 jari 130     */
3845 13 Sep 19 peter 131     explicit Vector(std::istream &, char sep='\0');
12 19 Jun 03 daniel 132
475 22 Dec 05 peter 133     ///
42 26 Feb 04 jari 134     /// The destructor.
42 26 Feb 04 jari 135     ///
1120 21 Feb 08 peter 136     ~Vector(void);
12 19 Jun 03 daniel 137
754 17 Feb 07 jari 138     /**
1009 01 Feb 08 peter 139        \return false
789 10 Mar 07 jari 140     */
2687 27 Feb 12 peter 141     bool isview(void) const;
789 10 Mar 07 jari 142
789 10 Mar 07 jari 143     /**
1120 21 Feb 08 peter 144       \brief Resize Vector
1099 18 Feb 08 jari 145
1099 18 Feb 08 jari 146       All elements are set to \a init_value.
1099 18 Feb 08 jari 147
2688 27 Feb 12 peter 148       \note Underlying GSL vector is destroyed and a view or an
2688 27 Feb 12 peter 149       iterator into this Vector becomes invalid.
1129 23 Feb 08 peter 150
1129 23 Feb 08 peter 151       \throw GSL_error if memory allocation fails.
1099 18 Feb 08 jari 152     */
1099 18 Feb 08 jari 153     void resize(size_t, double init_value=0);
1099 18 Feb 08 jari 154
1099 18 Feb 08 jari 155     /**
754 17 Feb 07 jari 156        \brief The assignment operator.
754 17 Feb 07 jari 157
2688 27 Feb 12 peter 158        \note Invalidates views and iterators of Vector.
1068 11 Feb 08 peter 159
1120 21 Feb 08 peter 160        \return A const reference to the resulting Vector.
1129 23 Feb 08 peter 161
1129 23 Feb 08 peter 162        \throw GSL_error if assignment fails.
754 17 Feb 07 jari 163     */
1120 21 Feb 08 peter 164      const Vector& operator=(const Vector&);
341 07 Jun 05 jari 165
3605 27 Jan 17 peter 166     /**
3605 27 Jan 17 peter 167        \brief assignment from vector expression
3605 27 Jan 17 peter 168
3605 27 Jan 17 peter 169        \since new in yat 0.15
3605 27 Jan 17 peter 170      */
3605 27 Jan 17 peter 171     template<class T>
3605 27 Jan 17 peter 172     Vector& operator=(const VectorExpression<T>& rhs)
3605 27 Jan 17 peter 173     {
3605 27 Jan 17 peter 174       // access rhs before deleting vec_
3623 09 Feb 17 peter 175       gsl_vector* tmp = detail::create_gsl_vector_copy(rhs.gsl_vector_p());
3605 27 Jan 17 peter 176       delete_allocated_memory();
3605 27 Jan 17 peter 177       const_vec_ = vec_ = tmp;
3605 27 Jan 17 peter 178       return *this;
3605 27 Jan 17 peter 179     }
3605 27 Jan 17 peter 180
1068 11 Feb 08 peter 181     /**
3589 19 Jan 17 peter 182        \brief The move assignment operator.
3589 19 Jan 17 peter 183
3589 19 Jan 17 peter 184        \note Invalidates views and iterators of Vector.
3589 19 Jan 17 peter 185
3589 19 Jan 17 peter 186        \return A reference to the resulting Vector.
3596 21 Jan 17 peter 187
3596 21 Jan 17 peter 188        \since new in yat 0.15
3589 19 Jan 17 peter 189     */
3589 19 Jan 17 peter 190      Vector& operator=(Vector&&);
3605 27 Jan 17 peter 191
3605 27 Jan 17 peter 192     /**
3605 27 Jan 17 peter 193        \brief move assignment from vector expression
3605 27 Jan 17 peter 194
3605 27 Jan 17 peter 195        \since new in yat 0.15
3605 27 Jan 17 peter 196      */
3605 27 Jan 17 peter 197     template<class T>
3605 27 Jan 17 peter 198     Vector& operator=(VectorExpression<T>&& rhs)
3605 27 Jan 17 peter 199     {
3605 27 Jan 17 peter 200       gsl_vector* tmp = rhs.gsl_vector_p();
3605 27 Jan 17 peter 201       delete_allocated_memory();
3605 27 Jan 17 peter 202       const_vec_ = vec_ = tmp;
3605 27 Jan 17 peter 203       rhs.gsl_vector_p(NULL);
3605 27 Jan 17 peter 204       return *this;
3605 27 Jan 17 peter 205     }
3605 27 Jan 17 peter 206
3589 19 Jan 17 peter 207
3589 19 Jan 17 peter 208     /**
1068 11 Feb 08 peter 209        \brief The assignment operator.
1068 11 Feb 08 peter 210
2688 27 Feb 12 peter 211        \note Invalidates views and iterators of Vector.
1068 11 Feb 08 peter 212
1120 21 Feb 08 peter 213        \return A const reference to the resulting Vector.
1129 23 Feb 08 peter 214
1129 23 Feb 08 peter 215        \throw GSL_error if assignment fails.
1068 11 Feb 08 peter 216     */
1120 21 Feb 08 peter 217      const Vector& operator=(const VectorBase&);
1068 11 Feb 08 peter 218
420 02 Dec 05 jari 219   private:
1120 21 Feb 08 peter 220      const Vector& assign(const VectorBase& other);
3605 27 Jan 17 peter 221     const Vector& assign(const gsl_vector* rhs);
12 19 Jun 03 daniel 222
754 17 Feb 07 jari 223     /**
754 17 Feb 07 jari 224        \brief Create a new copy of the internal GSL vector.
754 17 Feb 07 jari 225
754 17 Feb 07 jari 226        Necessary memory for the new GSL vector is allocated and the
754 17 Feb 07 jari 227        caller is responsible for freeing the allocated memory.
754 17 Feb 07 jari 228
754 17 Feb 07 jari 229        \return A pointer to a copy of the internal GSL vector.
754 17 Feb 07 jari 230
754 17 Feb 07 jari 231        \throw GSL_error if memory cannot be allocated for the new
754 17 Feb 07 jari 232        copy, or if dimensions mis-match.
754 17 Feb 07 jari 233     */
1015 01 Feb 08 peter 234     gsl_vector* create_gsl_vector_copy(const VectorBase&) const;
227 01 Feb 05 jari 235
3605 27 Jan 17 peter 236
810 15 Mar 07 jari 237     void delete_allocated_memory(void);
420 02 Dec 05 jari 238   };
12 19 Jun 03 daniel 239
782 05 Mar 07 jari 240   /**
1120 21 Feb 08 peter 241      \brief Swap Vector elements by copying.
782 05 Mar 07 jari 242
1120 21 Feb 08 peter 243      The two Vectors must have the same length.
782 05 Mar 07 jari 244
1120 21 Feb 08 peter 245      \throw GSL_error if Vector lengths differs.
1887 31 Mar 09 peter 246
1887 31 Mar 09 peter 247      \relates Vector
782 05 Mar 07 jari 248   */
1120 21 Feb 08 peter 249   void swap(Vector&, Vector&);
782 05 Mar 07 jari 250
686 16 Oct 06 jari 251 }}} // of namespace utility, yat, and theplu
12 19 Jun 03 daniel 252
420 02 Dec 05 jari 253 #endif