source: trunk/yat/utility/Vector.h

Last change on this file was 3938, checked in by Peter, 14 months ago

let configure fail if compiler is not a C++11 compiler. YAT_HAVE_RVALUE and friends are no longer defined in 'config.h' and guards araound declarations and implementations are removed. refs #949

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