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

Last change on this file since 1099 was 1099, checked in by Jari Häkkinen, 14 years ago

Readded resize() to vector.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 4.3 KB
Line 
1#ifndef _theplu_yat_utility_vector_
2#define _theplu_yat_utility_vector_
3
4// $Id: vector.h 1099 2008-02-18 03:42:46Z jari $
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
41namespace theplu {
42namespace yat {
43namespace utility {
44
45  class matrix;
46
47  /**
48     @brief This is the yat interface to GSL vector.
49
50     \par File streams:
51     Reading and writing vectors to file streams are of course
52     supported. These are implemented without using GSL functionality,
53     and thus binary read and write to streams are not supported.
54
55     \par Vector views: GSL vector views are supported in classes
56     VectorView and VectorConstView
57
58  */
59
60  class vector : public VectorMutable
61  {
62  public:
63    /**
64       \brief The default constructor.
65    */
66    vector(void);
67
68    /**
69       \brief Allocates memory space for \a n elements, and sets all
70       elements to \a init_value.
71       
72       \throw GSL_error if memory allocation fails.
73    */
74    vector(size_t n, double init_value=0);
75
76    /**
77       \brief The copy constructor.
78
79       \throw A GSL_error is indirectly thrown if memory allocation
80       fails.
81    */
82    vector(const vector& other);
83
84    /**
85       \brief Copy a VectorBase.
86
87       \throw A GSL_error is indirectly thrown if memory allocation
88       fails.
89    */
90    vector(const VectorBase& other);
91
92    /**
93       \brief The istream constructor.
94
95       Either elements should be separated with white space characters
96       (default), or elements should be separated by the delimiter \a
97       sep. When delimiter \a sep is used empty elements are stored as
98       NaN's (except that empty lines are ignored). The end of input
99       to the vector is at end of file marker.
100
101       \throw GSL_error if memory allocation fails, IO_error if
102       unexpected input is found in the input stream.
103    */
104    explicit vector(std::istream &, char sep='\0')
105      throw(utility::IO_error, std::exception);
106
107    ///
108    /// The destructor.
109    ///
110    ~vector(void);
111
112    /**
113       \return false
114    */
115    bool isview(void) const; 
116
117    /**
118      \brief Resize vector
119
120      All elements are set to \a init_value.
121
122      \note Underlying GSL vector is destroyed and a view into this
123      vector becomes invalid.
124    */
125    void resize(size_t, double init_value=0);
126
127    /**
128       \brief The assignment operator.
129
130       \note Invalidates views of vector.
131
132       \return A const reference to the resulting vector.
133    */
134    const vector& operator=(const vector&);
135
136    /**
137       \brief The assignment operator.
138
139       \note Invalidates views of vector.
140
141       \return A const reference to the resulting vector.
142    */
143    const vector& operator=(const VectorBase&);
144
145  private:
146    const vector& assign(const VectorBase& other);
147
148    /**
149       \brief Create a new copy of the internal GSL vector.
150
151       Necessary memory for the new GSL vector is allocated and the
152       caller is responsible for freeing the allocated memory.
153
154       \return A pointer to a copy of the internal GSL vector.
155
156       \throw GSL_error if memory cannot be allocated for the new
157       copy, or if dimensions mis-match.
158    */
159    gsl_vector* create_gsl_vector_copy(const VectorBase&) const;
160
161    void delete_allocated_memory(void);
162  };
163
164  /**
165     \brief Swap vector elements by copying.
166
167     The two vectors must have the same length.
168
169     \throw GSL_error if vector lengths differs.
170  */
171  void swap(vector&, vector&);
172
173}}} // of namespace utility, yat, and theplu
174
175#endif
Note: See TracBrowser for help on using the repository browser.