/[escript]/trunk/esys2/escript/src/Data/DataVector.h
ViewVC logotype

Contents of /trunk/esys2/escript/src/Data/DataVector.h

Parent Directory Parent Directory | Revision Log Revision Log


Revision 117 - (show annotations)
Fri Apr 1 05:48:57 2005 UTC (14 years, 9 months ago) by jgs
File MIME type: text/plain
File size: 6323 byte(s)
*** empty log message ***

1 /*
2 ******************************************************************************
3 * *
4 * COPYRIGHT ACcESS 2004 - All Rights Reserved *
5 * *
6 * This software is the property of ACcESS. No part of this code *
7 * may be copied in any form or by any means without the expressed written *
8 * consent of ACcESS. Copying, use or modification of this software *
9 * by any unauthorised person is illegal unless that person has a software *
10 * license agreement with ACcESS. *
11 * *
12 ******************************************************************************
13 */
14
15 #if !defined escript_DataVector_20050324_H
16 #define escript_DataVector_20050324_H
17
18 #include <vector>
19
20 #include "esysUtils/EsysAssert.h"
21
22 namespace escript {
23
24 /**
25 \brief
26 DataVector implements an arbitrarily long vector of data values.
27
28 Description:
29 DataVector provides an implementation of a vector of data values for use
30 by DataBlocks2D and DataArrayView. Hiding the vector in this container
31 allows different implementations to be swapped in without disrupting the
32 client classes. This is the underlying data container for Data objects.
33 */
34
35 class DataVector {
36
37 public:
38
39 //
40 // The type of the elements stored in the vector.
41 typedef double ElementType;
42
43 //
44 // The underlying type used to implement the vector.
45 typedef std::vector<ElementType> ValueType;
46
47 //
48 // Various types needed by clients of this class.
49 typedef ValueType::value_type value_type;
50 typedef ValueType::size_type size_type;
51 typedef ValueType::reference reference;
52 typedef ValueType::const_reference const_reference;
53 typedef ValueType::iterator iterator;
54
55 /**
56 \brief
57 Default constructor for DataVector.
58
59 Description:
60 Constructs an empty DataVector object.
61 */
62 DataVector();
63
64 /**
65 \brief
66 Copy constructor for DataVector.
67
68 Description:
69 Constructs a DataVector object which is a copy of the
70 given DataVector object.
71 */
72 DataVector(const DataVector& other);
73
74 /**
75 \brief
76 Constructor for DataVector.
77
78 Description:
79 Constructs a DataVector object of length "size" with all elements
80 initilised to "val". Default for "val" is zero.
81
82 \param size - Input - Number of elements in the vector.
83 */
84 DataVector(ValueType::size_type size, ValueType::value_type val=0.0);
85
86 /**
87 \brief
88 Default destructor for DataVector.
89
90 Description:
91 Destroys the current DataVector object.
92 */
93 ~DataVector();
94
95 /**
96 \brief
97 Resize the DataVector to the given length "newSize".
98 All current data is lost. All elements in the new DataVector are
99 initialised to "newVal", which defaults to zero.
100
101 \param newSize - Input - New size for the vector.
102 */
103 inline
104 void
105 resize(ValueType::size_type newSize, ValueType::value_type newVal=0.0);
106
107 /**
108 \brief
109 Return the number of elements in this DataVector.
110 */
111 inline
112 ValueType::size_type
113 size() const;
114
115 /**
116 \brief
117 DataVector assignment operator "=".
118 Assign the given DataVector object to this.
119 */
120 inline
121 DataVector&
122 operator=(const DataVector& other);
123
124 /**
125 \brief
126 DataVector equality comparison operator "==".
127 Return true if the given DataVector is equal to this.
128 */
129 inline
130 bool
131 operator==(const DataVector& other);
132
133 /**
134 \brief
135 DataVector inequality comparison operator "!=".
136 Return true if the given DataVector is not equal to this.
137 */
138 inline
139 bool
140 operator!=(const DataVector& other);
141
142 /**
143 \brief
144 Return a reference to the element at position i in this DataVector.
145 Will throw an exception if an invalid index "i" is given.
146
147 NB: access to the element one past the end of the vector is permitted
148 in order to provide a facility equivalent to an end() pointer.
149 */
150 inline
151 ValueType::reference
152 operator[](ValueType::size_type i);
153
154 inline
155 ValueType::const_reference
156 operator[](ValueType::size_type i) const;
157
158 /**
159 \brief
160 Add the element x at the end of the vector.
161 */
162 inline
163 void
164 push_back(const ValueType::value_type& x);
165
166 /**
167 \brief
168 Insert.
169 */
170 inline
171 void
172 insert(iterator pos, const ValueType::value_type* first, const ValueType::value_type* last);
173
174 /**
175 \brief
176 End.
177 */
178 inline
179 iterator
180 end();
181
182 protected:
183
184 private:
185
186 //
187 // The container for the elements contained in this DataVector.
188 ValueType m_data;
189
190 };
191
192 inline
193 DataVector::ValueType::size_type
194 DataVector::size() const
195 {
196 return m_data.size();
197 }
198
199 inline
200 DataVector::ValueType::reference
201 DataVector::operator[](DataVector::ValueType::size_type i)
202 {
203 // Allow access to element one beyond end of vector to simulate end().
204 EsysAssert(i<=size(),"DataVector: invalid index specified.");
205 return m_data[i];
206 }
207
208 inline
209 DataVector::ValueType::const_reference
210 DataVector::operator[](DataVector::ValueType::size_type i) const
211 {
212 // Allow access to element one beyond end of vector to simulate end().
213 EsysAssert(i<=size(),"DataVector: invalid index specified.");
214 return m_data[i];
215 }
216
217 inline
218 DataVector&
219 DataVector::operator=(const DataVector& other)
220 {
221 DataVector temp(other);
222 swap(m_data,temp.m_data);
223 return *this;
224 }
225
226 inline
227 bool
228 DataVector::operator==(const DataVector& other)
229 {
230 return m_data==other.m_data;
231 }
232
233 inline
234 bool
235 DataVector::operator!=(const DataVector& other)
236 {
237 return m_data!=other.m_data;
238 }
239
240 inline
241 void
242 DataVector::resize(DataVector::ValueType::size_type newSize, DataVector::ValueType::value_type newValue)
243 {
244 m_data.resize(newSize,newValue);
245 }
246
247 inline
248 void
249 DataVector::push_back(const ValueType::value_type& x)
250 {
251 m_data.push_back(x);
252 }
253
254 inline
255 void
256 DataVector::insert(DataVector::iterator pos, const ValueType::value_type* first, const ValueType::value_type* last)
257 {
258 m_data.insert(pos, first, last);
259 }
260
261 inline
262 DataVector::iterator
263 DataVector::end()
264 {
265 return m_data.end();
266 }
267
268 } // end of namespace
269
270 #endif

Properties

Name Value
svn:eol-style native
svn:keywords Author Date Id Revision

  ViewVC Help
Powered by ViewVC 1.1.26