source: trunk/yat/utility/vector.h @ 1009

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

merging branch peter-dev into trunk delta 1008:994

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 5.8 KB
Line 
1#ifndef _theplu_yat_utility_vector_
2#define _theplu_yat_utility_vector_
3
4// $Id: vector.h 1009 2008-02-01 04:15:44Z 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 "vectorBase.h"
32#include "Exception.h"
33#include "Iterator.h"
34
35#include <iostream>
36#include <vector>
37#include <utility>
38
39#include <gsl/gsl_vector.h>
40#include <gsl/gsl_sort_vector.h>
41
42namespace theplu {
43namespace yat {
44namespace utility {
45
46  class matrix;
47
48  /**
49     @brief This is the yat interface to GSL vector.
50
51     For time being 'double' is the only type supported.
52
53     \par File streams:
54     Reading and writing vectors to file streams are of course
55     supported. These are implemented without using GSL functionality,
56     and thus binary read and write to streams are not supported.
57
58     \par Vector views:
59     GSL vector views are supported and these are disguised as
60     ordinary utility::vectors. A support function is added,
61     utility::vector::isview(), that can be used to check if a vector
62     object is a view. Note that view vectors do not own the
63     underlying data, and a view is not valid if the vector/matrix
64     owing the data is deallocated.
65
66     \par
67     Currently there is no restriction on how a vector is used when
68     the vector is a const view into another vector or matrix. To
69     avoid unexpected runtime errors, the programmer must declare
70     const view vectors as 'const' in order to get compile time
71     diagnostics about improper use of a const view vector object. If
72     'const' is not used and the const view vector is used erroneously
73     (such as on the left hand side in assignments), the compiler will
74     not catch the error and runtime error will occur. assert(3) is
75     used to catch the runtime error during development. Example on
76     bad and proper use of const view vectors:
77     @code
78  const vector vm(13,1.0);
79  vector v1(vm,2,4);       // bad code! not const!
80  v1(0)=-123;              // accepted by compiler, runtime abort will occur
81                           // or catched by assert depending on compiler flags
82  const vector v2(vm,2,4); // proper code
83  v2(0)=-123;              // not acceptable for the compiler
84     @endcode
85  */
86
87  class vector : public vectorBase
88  {
89  public:
90
91    /**
92       \brief The default constructor.
93    */
94    vector(void);
95
96    /**
97       \brief Allocates memory space for \a n elements, and sets all
98       elements to \a init_value.
99       
100       \throw GSL_error if memory allocation fails.
101    */
102    vector(size_t n, double init_value=0);
103
104    /**
105       \brief The copy constructor.
106
107       \note If the object to be copied is a vector view, the values
108       of the view will be copied, i.e. the view is not copied.
109
110       \throw A GSL_error is indirectly thrown if memory allocation
111       fails.
112    */
113    vector(const vector& other);
114
115    /**
116       \brief The copy constructor.
117
118       \note If the object to be copied is a vector view, the values
119       of the view will be copied, i.e. the view is not copied.
120
121       \throw A GSL_error is indirectly thrown if memory allocation
122       fails.
123    */
124    vector(const vectorBase& other);
125
126    /**
127       \brief The istream constructor.
128
129       Either elements should be separated with white space characters
130       (default), or elements should be separated by the delimiter \a
131       sep. When delimiter \a sep is used empty elements are stored as
132       NaN's (except that empty lines are ignored). The end of input
133       to the vector is at end of file marker.
134
135       \throw GSL_error if memory allocation fails, IO_error if
136       unexpected input is found in the input stream.
137    */
138    explicit vector(std::istream &, char sep='\0')
139      throw(utility::IO_error, std::exception);
140
141    ///
142    /// The destructor.
143    ///
144    ~vector(void);
145
146    /**
147       \return false
148    */
149    bool isview(void) const; 
150
151    /**
152       @brief Resize vector
153       
154       All elements are set to @a init_value.
155
156       \note Underlying GSL vector is destroyed and a view into this
157       vector becomes invalid.
158    */
159    void resize(size_t n, double init_value);
160
161    //    using vectorBase::operator=;
162
163    /**
164       \brief The assignment operator.
165
166       \return A const reference to the resulting vector.
167    */
168    //const vectorBase& operator=(const vectorBase&);
169    const vectorBase& operator=(const vector&);
170
171  private:
172    const vectorBase& assign(const vectorBase& other);
173    //const vectorBase& assign(vectorBase& other);
174
175    /**
176       \brief Create a new copy of the internal GSL vector.
177
178       Necessary memory for the new GSL vector is allocated and the
179       caller is responsible for freeing the allocated memory.
180
181       \return A pointer to a copy of the internal GSL vector.
182
183       \throw GSL_error if memory cannot be allocated for the new
184       copy, or if dimensions mis-match.
185    */
186    gsl_vector* create_gsl_vector_copy(const vectorBase&) const;
187
188    void delete_allocated_memory(void);
189  };
190
191  /**
192     \brief Swap vector elements by copying.
193
194     The two vectors must have the same length.
195
196     \throw GSL_error if vector lengths differs.
197  */
198  void swap(vector&, vector&);
199
200}}} // of namespace utility, yat, and theplu
201
202#endif
Note: See TracBrowser for help on using the repository browser.