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