1 | #ifndef _theplu_yat_utility_vector_ |
---|
2 | #define _theplu_yat_utility_vector_ |
---|
3 | |
---|
4 | // $Id: vector.h 1027 2008-02-02 21:29:29Z 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, Markus Ringnér, Peter Johansson |
---|
10 | Copyright (C) 2006 Jari Häkkinen, Markus Ringnér |
---|
11 | Copyright (C) 2007 Jari Häkkinen, Peter Johansson |
---|
12 | |
---|
13 | This file is part of the yat library, http://trac.thep.lu.se/yat |
---|
14 | |
---|
15 | The yat library is free software; you can redistribute it and/or |
---|
16 | modify it under the terms of the GNU General Public License as |
---|
17 | published by the Free Software Foundation; either version 2 of the |
---|
18 | License, or (at your option) any later version. |
---|
19 | |
---|
20 | The yat library is distributed in the hope that it will be useful, |
---|
21 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
22 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
23 | General Public License for more details. |
---|
24 | |
---|
25 | You should have received a copy of the GNU General Public License |
---|
26 | along with this program; if not, write to the Free Software |
---|
27 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
---|
28 | 02111-1307, USA. |
---|
29 | */ |
---|
30 | |
---|
31 | #include "VectorMutable.h" |
---|
32 | #include "Exception.h" |
---|
33 | |
---|
34 | #include <iostream> |
---|
35 | #include <vector> |
---|
36 | #include <utility> |
---|
37 | |
---|
38 | #include <gsl/gsl_vector.h> |
---|
39 | #include <gsl/gsl_sort_vector.h> |
---|
40 | |
---|
41 | namespace theplu { |
---|
42 | namespace yat { |
---|
43 | namespace utility { |
---|
44 | |
---|
45 | class matrix; |
---|
46 | |
---|
47 | /** |
---|
48 | @brief This is the yat interface to GSL vector. |
---|
49 | |
---|
50 | For time being 'double' is the only type supported. |
---|
51 | |
---|
52 | \par File streams: |
---|
53 | Reading and writing vectors to file streams are of course |
---|
54 | supported. These are implemented without using GSL functionality, |
---|
55 | and thus binary read and write to streams are not supported. |
---|
56 | |
---|
57 | \par Vector views: |
---|
58 | GSL vector views are supported and these are disguised as |
---|
59 | ordinary utility::vectors. A support function is added, |
---|
60 | utility::vector::isview(), that can be used to check if a vector |
---|
61 | object is a view. Note that view vectors do not own the |
---|
62 | underlying data, and a view is not valid if the vector/matrix |
---|
63 | owing the data is deallocated. |
---|
64 | |
---|
65 | \par |
---|
66 | Currently there is no restriction on how a vector is used when |
---|
67 | the vector is a const view into another vector or matrix. To |
---|
68 | avoid unexpected runtime errors, the programmer must declare |
---|
69 | const view vectors as 'const' in order to get compile time |
---|
70 | diagnostics about improper use of a const view vector object. If |
---|
71 | 'const' is not used and the const view vector is used erroneously |
---|
72 | (such as on the left hand side in assignments), the compiler will |
---|
73 | not catch the error and runtime error will occur. assert(3) is |
---|
74 | used to catch the runtime error during development. Example on |
---|
75 | bad and proper use of const view vectors: |
---|
76 | @code |
---|
77 | const vector vm(13,1.0); |
---|
78 | vector v1(vm,2,4); // bad code! not const! |
---|
79 | v1(0)=-123; // accepted by compiler, runtime abort will occur |
---|
80 | // or catched by assert depending on compiler flags |
---|
81 | const vector v2(vm,2,4); // proper code |
---|
82 | v2(0)=-123; // not acceptable for the compiler |
---|
83 | @endcode |
---|
84 | */ |
---|
85 | |
---|
86 | class vector : public VectorMutable |
---|
87 | { |
---|
88 | public: |
---|
89 | |
---|
90 | /** |
---|
91 | \brief The default constructor. |
---|
92 | */ |
---|
93 | vector(void); |
---|
94 | |
---|
95 | /** |
---|
96 | \brief Allocates memory space for \a n elements, and sets all |
---|
97 | elements to \a init_value. |
---|
98 | |
---|
99 | \throw GSL_error if memory allocation fails. |
---|
100 | */ |
---|
101 | vector(size_t n, double init_value=0); |
---|
102 | |
---|
103 | /** |
---|
104 | \brief The copy constructor. |
---|
105 | |
---|
106 | \note If the object to be copied is a vector view, the values |
---|
107 | of the view will be copied, i.e. the view is not copied. |
---|
108 | |
---|
109 | \throw A GSL_error is indirectly thrown if memory allocation |
---|
110 | fails. |
---|
111 | */ |
---|
112 | vector(const vector& other); |
---|
113 | |
---|
114 | /** |
---|
115 | \brief The copy constructor. |
---|
116 | |
---|
117 | \note If the object to be copied is a vector view, the values |
---|
118 | of the view will be copied, i.e. the view is not copied. |
---|
119 | |
---|
120 | \throw A GSL_error is indirectly thrown if memory allocation |
---|
121 | fails. |
---|
122 | */ |
---|
123 | vector(const VectorBase& other); |
---|
124 | |
---|
125 | /** |
---|
126 | \brief The istream constructor. |
---|
127 | |
---|
128 | Either elements should be separated with white space characters |
---|
129 | (default), or elements should be separated by the delimiter \a |
---|
130 | sep. When delimiter \a sep is used empty elements are stored as |
---|
131 | NaN's (except that empty lines are ignored). The end of input |
---|
132 | to the vector is at end of file marker. |
---|
133 | |
---|
134 | \throw GSL_error if memory allocation fails, IO_error if |
---|
135 | unexpected input is found in the input stream. |
---|
136 | */ |
---|
137 | explicit vector(std::istream &, char sep='\0') |
---|
138 | throw(utility::IO_error, std::exception); |
---|
139 | |
---|
140 | /// |
---|
141 | /// The destructor. |
---|
142 | /// |
---|
143 | ~vector(void); |
---|
144 | |
---|
145 | /** |
---|
146 | \return false |
---|
147 | */ |
---|
148 | bool isview(void) const; |
---|
149 | |
---|
150 | /** |
---|
151 | @brief Resize vector |
---|
152 | |
---|
153 | All elements are set to @a init_value. |
---|
154 | |
---|
155 | \note Underlying GSL vector is destroyed and a view into this |
---|
156 | vector becomes invalid. |
---|
157 | */ |
---|
158 | void resize(size_t n, double init_value); |
---|
159 | |
---|
160 | // using VectorBase::operator=; |
---|
161 | |
---|
162 | /** |
---|
163 | \brief The assignment operator. |
---|
164 | |
---|
165 | \return A const reference to the resulting vector. |
---|
166 | */ |
---|
167 | //const VectorBase& operator=(const VectorBase&); |
---|
168 | const vector& operator=(const vector&); |
---|
169 | |
---|
170 | private: |
---|
171 | const vector& assign(const VectorBase& other); |
---|
172 | //const VectorBase& assign(VectorBase& other); |
---|
173 | |
---|
174 | /** |
---|
175 | \brief Create a new copy of the internal GSL vector. |
---|
176 | |
---|
177 | Necessary memory for the new GSL vector is allocated and the |
---|
178 | caller is responsible for freeing the allocated memory. |
---|
179 | |
---|
180 | \return A pointer to a copy of the internal GSL vector. |
---|
181 | |
---|
182 | \throw GSL_error if memory cannot be allocated for the new |
---|
183 | copy, or if dimensions mis-match. |
---|
184 | */ |
---|
185 | gsl_vector* create_gsl_vector_copy(const VectorBase&) const; |
---|
186 | |
---|
187 | void delete_allocated_memory(void); |
---|
188 | }; |
---|
189 | |
---|
190 | /** |
---|
191 | \brief Swap vector elements by copying. |
---|
192 | |
---|
193 | The two vectors must have the same length. |
---|
194 | |
---|
195 | \throw GSL_error if vector lengths differs. |
---|
196 | */ |
---|
197 | void swap(vector&, vector&); |
---|
198 | |
---|
199 | }}} // of namespace utility, yat, and theplu |
---|
200 | |
---|
201 | #endif |
---|