1 |
/* |
2 |
************************************************************ |
3 |
* Copyright 2006 by ACcESS MNRF * |
4 |
* * |
5 |
* http://www.access.edu.au * |
6 |
* Primary Business: Queensland, Australia * |
7 |
* Licensed under the Open Software License version 3.0 * |
8 |
* http://www.opensource.org/licenses/osl-3.0.php * |
9 |
* * |
10 |
************************************************************ |
11 |
*/ |
12 |
|
13 |
#if !defined escript_DataVector_20050324_H |
14 |
#define escript_DataVector_20050324_H |
15 |
#include "system_dep.h" |
16 |
|
17 |
#include "esysUtils/EsysAssert.h" |
18 |
|
19 |
#ifdef PASO_MPI |
20 |
#include <mpi.h> |
21 |
#endif |
22 |
#include <vector> |
23 |
#include <iostream> |
24 |
#include <fstream> |
25 |
|
26 |
namespace escript { |
27 |
|
28 |
/** |
29 |
\brief |
30 |
DataVector implements an arbitrarily long vector of data values. |
31 |
DataVector is the underlying data container for Data objects. |
32 |
|
33 |
Description: |
34 |
DataVector provides an implementation of a vector of data values for use |
35 |
by DataBlocks2D and DataArrayView. Hiding the vector in this container |
36 |
allows different implementations to be swapped in without disrupting the |
37 |
client classes. |
38 |
*/ |
39 |
|
40 |
class ESCRIPT_DLL_API DataVector { |
41 |
|
42 |
public: |
43 |
|
44 |
// |
45 |
// The type of the elements stored in the vector. |
46 |
typedef double ElementType; |
47 |
|
48 |
// |
49 |
// The underlying type used to implement the vector. |
50 |
typedef ElementType * ValueType; |
51 |
|
52 |
// |
53 |
// Various types exported to clients of this class. |
54 |
typedef ElementType value_type; |
55 |
typedef long size_type; |
56 |
typedef ElementType & reference; |
57 |
typedef const ElementType & const_reference; |
58 |
|
59 |
/** |
60 |
\brief |
61 |
Default constructor for DataVector. |
62 |
|
63 |
Description: |
64 |
Constructs an empty DataVector object. |
65 |
*/ |
66 |
DataVector(); |
67 |
|
68 |
/** |
69 |
\brief |
70 |
Copy constructor for DataVector. |
71 |
|
72 |
Description: |
73 |
Constructs a DataVector object which is a copy of the |
74 |
given DataVector object. |
75 |
*/ |
76 |
DataVector(const DataVector& other); |
77 |
|
78 |
/** |
79 |
\brief |
80 |
Constructor for DataVector. |
81 |
|
82 |
Description: |
83 |
Constructs a DataVector object of length "size" with all elements |
84 |
initilised to "val". |
85 |
|
86 |
\param size - Input - Number of elements in the vector. |
87 |
\param val - Input - Initial value for all elements in the vector. Default is 0.0. |
88 |
\param blockSize - Input - size of blocks within the vector, overall vector |
89 |
size must be a precise multiple of the block size. Default is 1. |
90 |
|
91 |
In escript::Data, blocksize corresponds to the number of elements required to hold all |
92 |
the data-points for a sample, ie: the product of the dimensions of a data-point and the |
93 |
number of data-points per sample. Size is the total number of elements required to hold |
94 |
all elements for all data-points in the given object, ie: number of samples * blocksize. |
95 |
*/ |
96 |
DataVector(const size_type size, |
97 |
const value_type val=0.0, |
98 |
const size_type blockSize=1); |
99 |
|
100 |
/** |
101 |
\brief |
102 |
Default destructor for DataVector. |
103 |
|
104 |
Description: |
105 |
Destroys the current DataVector object. |
106 |
*/ |
107 |
~DataVector(); |
108 |
|
109 |
/** |
110 |
\brief |
111 |
Resize the DataVector to the given length "newSize". |
112 |
All current data is lost. All elements in the new DataVector are |
113 |
initialised to "newVal". |
114 |
|
115 |
\param newSize - Input - New size for the vector. |
116 |
\param newVal - Input - New initial value for all elements in the vector. |
117 |
\param newBlockSize - Input - New block size for the vector. |
118 |
*/ |
119 |
void |
120 |
resize(const size_type newSize, |
121 |
const value_type newVal=0.0, |
122 |
const size_type newBlockSize=1); |
123 |
|
124 |
/** |
125 |
\brief |
126 |
Return the number of elements in this DataVector. |
127 |
*/ |
128 |
inline |
129 |
size_type |
130 |
size() const; |
131 |
|
132 |
/** |
133 |
\brief |
134 |
DataVector assignment operator "=". |
135 |
Assign the given DataVector object to this. |
136 |
*/ |
137 |
DataVector& |
138 |
operator=(const DataVector& other); |
139 |
|
140 |
/** |
141 |
\brief |
142 |
DataVector equality comparison operator "==". |
143 |
Return true if the given DataVector is equal to this. |
144 |
*/ |
145 |
bool |
146 |
operator==(const DataVector& other) const; |
147 |
|
148 |
/** |
149 |
\brief |
150 |
DataVector inequality comparison operator "!=". |
151 |
Return true if the given DataVector is not equal to this. |
152 |
*/ |
153 |
bool |
154 |
operator!=(const DataVector& other) const; |
155 |
|
156 |
/** |
157 |
\brief |
158 |
Return a reference to the element at position i in this DataVector. |
159 |
Will throw an exception if an invalid index "i" is given. |
160 |
|
161 |
NB: access to the element one past the end of the vector is permitted |
162 |
in order to provide a facility equivalent to an end() pointer. |
163 |
*/ |
164 |
inline |
165 |
reference |
166 |
operator[](const size_type i); |
167 |
|
168 |
inline |
169 |
const_reference |
170 |
operator[](const size_type i) const; |
171 |
|
172 |
/** |
173 |
\brief |
174 |
Archive the data managed by this DataVector to the file referenced |
175 |
by ofstream. A count of the number of values expected to be written |
176 |
is provided as a cross-check. |
177 |
|
178 |
|
179 |
The return value indicates success (0) or otherwise (1). |
180 |
*/ |
181 |
int |
182 |
archiveData(std::ofstream& archiveFile, |
183 |
const size_type noValues) const; |
184 |
|
185 |
/** |
186 |
\brief |
187 |
Extract the number of values specified by noValues from the file |
188 |
referenced by ifstream to this DataVector. |
189 |
|
190 |
The return value indicates success (0) or otherwise (1). |
191 |
*/ |
192 |
int |
193 |
extractData(std::ifstream& archiveFile, |
194 |
const size_type noValues); |
195 |
|
196 |
protected: |
197 |
|
198 |
private: |
199 |
|
200 |
size_type m_size; |
201 |
size_type m_dim; |
202 |
size_type m_N; |
203 |
|
204 |
// |
205 |
// The container for the elements contained in this DataVector. |
206 |
ValueType m_array_data; |
207 |
}; |
208 |
|
209 |
inline |
210 |
DataVector::size_type |
211 |
DataVector::size() const |
212 |
{ |
213 |
return m_size; |
214 |
} |
215 |
|
216 |
inline |
217 |
DataVector::reference |
218 |
DataVector::operator[](const DataVector::size_type i) |
219 |
{ |
220 |
EsysAssert(i<size(),"DataVector: invalid index specified."); |
221 |
return m_array_data[i]; |
222 |
} |
223 |
|
224 |
inline |
225 |
DataVector::const_reference |
226 |
DataVector::operator[](const DataVector::size_type i) const |
227 |
{ |
228 |
EsysAssert(i<size(),"DataVector: invalid index specified."); |
229 |
return m_array_data[i]; |
230 |
} |
231 |
|
232 |
} // end of namespace |
233 |
|
234 |
#endif |