source: trunk/yat/utility/VectorMutable.h @ 1151

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

remove operator [] in Vector.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 6.3 KB
Line 
1#ifndef _theplu_yat_utility_vector_mutable_
2#define _theplu_yat_utility_vector_mutable_
3
4// $Id: VectorMutable.h 1151 2008-02-25 22:32:04Z 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  Copyright (C) 2008 Peter Johansson
13
14  This file is part of the yat library, http://trac.thep.lu.se/trac/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 2 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 this program; if not, write to the Free Software
28  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
29  02111-1307, USA.
30*/
31
32#include "VectorBase.h"
33#include "Exception.h"
34#include "StrideIterator.h"
35
36#include <iostream>
37#include <vector>
38#include <utility>
39
40#include <gsl/gsl_vector.h>
41#include <gsl/gsl_sort_vector.h>
42
43namespace theplu {
44namespace yat {
45namespace utility {
46
47  class matrix;
48  class Vector;
49
50  /**
51     @brief This is the mutable interface to GSL vector.
52
53     This class contains the mutable interface to vector classes.
54
55     The non-mutable interface is inherited from VectorBase. When
56     dealing with const vectors, it is preferable to use the
57     VectorBase signature because this allows usage of VectorConstView
58     too.
59  */
60  class VectorMutable : public VectorBase
61  {
62  public:
63    /**
64       \brief mutable iterator
65    */
66    typedef StrideIterator<double*> iterator;
67
68    /**
69       \brief default constructor
70    */
71    VectorMutable(void);
72
73    /**
74       \brief Constructor.
75    */
76    VectorMutable(gsl_vector*);
77
78    /**
79       \brief Constructor.
80    */
81    VectorMutable(const gsl_vector*);
82
83    /**
84       The destructor.
85    */
86    virtual ~VectorMutable(void);
87
88    /**
89       Set all elements to \a value.
90    */
91    void all(double value);
92
93    /**
94       \return mutable iterator to start of VectorBase
95     */
96    iterator begin(void);
97
98    // to allow overload from base class
99    using VectorBase::begin;
100
101    /**
102       \brief This function performs element-wise division, \f$
103       this_i = this_i / other_i \; \forall i \f$.
104
105       \throw GSL_error if dimensions mis-match.
106    */
107    void div(const VectorBase& other);
108
109    /**
110       \return mutable iterator to end of VectorMutable
111     */
112    iterator end(void);
113
114    // to allow overload from base class
115    using VectorBase::end;
116
117    /**
118       @return A pointer to the internal GSL vector,
119    */
120    gsl_vector* gsl_vector_p(void);
121
122    using VectorBase::gsl_vector_p;
123
124    /**
125       Check if the vector object is a view (sub-vector) to another
126       vector.
127   
128       \return True if the object is a view, false othwerwise.
129   
130     */
131    virtual bool isview(void) const=0; 
132
133    /**
134       \brief This function performs element-wise multiplication, \f$
135       this_i = this_i * other_i \; \forall i \f$.
136
137       \throw GSL_error if dimensions mis-match.
138    */
139    void mul(const VectorBase& other);
140
141    /**
142       \brief Reverse the order of elements in the VectorMutable.
143    */
144    void reverse(void);
145
146    /**
147       \brief Exchange elements \a i and \a j.
148    */
149    void swap(size_t i, size_t j);
150
151    /**
152       \brief Element access operator.
153
154       \return Reference to element \a i.
155
156       \throw If GSL range checks are enabled in the underlying GSL
157       library a GSL_error exception is thrown if either index is out
158       of range.
159    */
160    double& operator()(size_t i);
161
162    // to allow overload from base class
163    using VectorBase::operator();
164
165    /**
166       \brief Addition and assign operator. VectorBase addition, \f$
167       this_i = this_i + other_i \; \forall i \f$.
168
169       \return A const reference to the resulting VectorBase.
170
171       \throw GSL_error if dimensions mis-match.
172    */
173    const VectorMutable& operator+=(const VectorBase&);
174
175    /**
176       \brief Add a constant to a VectorBase, \f$ this_i = this_i + d \;
177       \forall i \f$.
178
179       \return A const reference to the resulting VectorBase.
180    */
181    const VectorMutable& operator+=(double d);
182
183    /**
184       \brief Subtract and assign operator. VectorBase subtraction, \f$
185       this_i = this_i - other_i \; \forall i \f$.
186
187       \return A const reference to the resulting VectorBase.
188
189       \throw GSL_error if dimensions mis-match.
190    */
191    const VectorMutable& operator-=(const VectorBase&);
192
193    /**
194       \brief Subtract a constant to a VectorBase, \f$ this_i = this_i - d
195       \; \forall i \f$.
196
197       \return A const reference to the resulting VectorBase.
198    */
199    const VectorMutable& operator-=(double d);
200
201    /**
202       \brief Multiply with scalar and assign operator, \f$ this_i =
203       this_i * d \; \forall i \f$.
204
205       \return A const reference to the resulting VectorBase.
206    */
207    const VectorMutable& operator*=(double d);
208
209  protected:
210    /**
211       pointer to underlying GSL vector. Should always point to same
212       gsl_vector as const_vec_ in base class does. This pointer should
213       by used for mutable operations. Do not use this in const
214       functions; use const_vec_ inherited from BaseVector.
215     */
216    gsl_vector* vec_;
217
218    /**
219       Proxy class used to allow copy and assignment of VectorView. By
220       design vectors and matrices are passed as non-const references
221       in all constructors of VectorView. Because the standard does
222       not allow temporary objects to be bound to non-const
223       references, it is not possible to directly construct a
224       VectorView from a temporary VectorView returned from a
225       function. Instead this proxy class is created from the
226       temporary object and then a VectorView can be created from this
227       proxy.
228
229       \see VectorView
230     */
231    struct proxy
232    { 
233      /// pointer to GSL vector keeping everything we need to create a
234      /// new VectorMutable from a proxy.
235      gsl_vector* vec_;
236    };
237
238  private:
239    // copy assignment not allowed
240    const VectorMutable& operator=(const VectorMutable&);
241  public:
242    /**
243       conversion operator to protected proxy class.
244     */
245    operator proxy();
246
247  };
248
249  /**
250     Randomly shuffles the elements in VectorMutable \a invec
251  */
252  void shuffle(VectorMutable& invec); 
253
254  /**
255     Sort the elements in the VectorMutable \a vec
256  */
257  void sort(VectorMutable& vec);
258
259}}} // of namespace utility, yat, and theplu
260
261#endif
Note: See TracBrowser for help on using the repository browser.