source: trunk/yat/utility/Vector.h @ 1506

Last change on this file since 1506 was 1506, checked in by Peter, 13 years ago

fixes #434

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 4.8 KB
Line 
1#ifndef _theplu_yat_utility_vector_
2#define _theplu_yat_utility_vector_
3
4// $Id: Vector.h 1506 2008-09-16 22:53:55Z 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
14  This file is part of the yat library, http://dev.thep.lu.se/yat
15
16  The yat library is free software; you can redistribute it and/or
17  modify it under the terms of the GNU General Public License as
18  published by the Free Software Foundation; either version 3 of the
19  License, or (at your option) any later version.
20
21  The yat library is distributed in the hope that it will be useful,
22  but WITHOUT ANY WARRANTY; without even the implied warranty of
23  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
24  General Public License for more details.
25
26  You should have received a copy of the GNU General Public License
27  along with yat. If not, see <http://www.gnu.org/licenses/>.
28*/
29
30#include "VectorMutable.h"
31#include "Exception.h"
32
33#include <iostream>
34#include <vector>
35#include <utility>
36
37#include <gsl/gsl_vector.h>
38#include <gsl/gsl_sort_vector.h>
39
40namespace theplu {
41namespace yat {
42namespace utility {
43
44  class matrix;
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    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 Copy a VectorBase.
85
86       \throw A GSL_error is indirectly thrown if memory allocation
87       fails.
88    */
89    Vector(const VectorBase& other);
90
91    /**
92       \brief The istream constructor.
93
94       Either elements should be separated with white space characters
95       (default), or elements should be separated by the delimiter \a
96       sep. When delimiter \a sep is used empty elements are stored as
97       NaN's (except that empty lines are ignored). The end of input
98       to the Vector is at end of file marker.
99
100       \throw GSL_error if memory allocation fails, IO_error if
101       unexpected input is found in the input stream.
102    */
103    explicit Vector(std::istream &, char sep='\0')
104      throw(utility::IO_error, std::exception);
105
106    ///
107    /// The destructor.
108    ///
109    ~Vector(void);
110
111    /**
112       \return false
113    */
114    bool isview(void) const; 
115
116    /**
117      \brief Resize Vector
118
119      All elements are set to \a init_value.
120
121      \note Underlying GSL vector is destroyed and a view into this
122      Vector becomes invalid.
123
124      \throw GSL_error if memory allocation fails.
125    */
126    void resize(size_t, double init_value=0);
127
128    /**
129       \brief The assignment operator.
130
131       \note Invalidates views of Vector.
132
133       \return A const reference to the resulting Vector.
134
135       \throw GSL_error if assignment fails.
136    */
137    const Vector& operator=(const Vector&);
138
139    /**
140       \brief The assignment operator.
141
142       \note Invalidates views of Vector.
143
144       \return A const reference to the resulting Vector.
145
146       \throw GSL_error if assignment fails.
147    */
148    const Vector& operator=(const VectorBase&);
149
150  private:
151    const Vector& assign(const VectorBase& other);
152
153    /**
154       \brief Create a new copy of the internal GSL vector.
155
156       Necessary memory for the new GSL vector is allocated and the
157       caller is responsible for freeing the allocated memory.
158
159       \return A pointer to a copy of the internal GSL vector.
160
161       \throw GSL_error if memory cannot be allocated for the new
162       copy, or if dimensions mis-match.
163    */
164    gsl_vector* create_gsl_vector_copy(const VectorBase&) const;
165
166    /**
167       \brief Create a new gsl vector
168
169       Necessary memory for the new GSL vector is allocated and the
170       caller is responsible for freeing the allocated memory.
171
172       \return A pointer to created GSL vector.
173
174       \throw GSL_error if memory cannot be allocated for the new
175       GSL vector.
176    */
177    gsl_vector* create_gsl_vector(size_t n) const;
178
179    void delete_allocated_memory(void);
180  };
181
182  /**
183     \brief Swap Vector elements by copying.
184
185     The two Vectors must have the same length.
186
187     \throw GSL_error if Vector lengths differs.
188  */
189  void swap(Vector&, Vector&);
190
191}}} // of namespace utility, yat, and theplu
192
193#endif
Note: See TracBrowser for help on using the repository browser.