XdmfArray.cpp 7.73 KB
Newer Older
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
1
2
3
4
5
6
7
8
9
10
11
/*
 * XdmfArray.cpp
 *
 *  Created on: Jan 29, 2010
 *      Author: kleiter
 */

#include "XdmfArray.hpp"
#include "XdmfVisitor.hpp"
#include <sstream>

12
class XdmfArrayClear : public boost::static_visitor <void> {
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
13
public:
14
15

	XdmfArrayClear()
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
16
17
18
	{
	}

19
	template<typename T> void operator()(const boost::shared_ptr<std::vector<T> > & array) const
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
20
	{
21
		return array->clear();
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
22
	}
23
24
25
26
};

class XdmfArrayCopyArrayValues : public boost::static_visitor <void> {
public:
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
27

28
29
30
31
32
33
	XdmfArrayCopyArrayValues(int startIndex, int valuesStartIndex, int numValues, int arrayStride, int valuesStride) :
		mStartIndex(startIndex),
		mValuesStartIndex(valuesStartIndex),
		mNumValues(numValues),
		mArrayStride(arrayStride),
		mValuesStride(valuesStride)
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
34
35
36
	{
	}

37
	template<typename T, typename U> void operator()(const boost::shared_ptr<std::vector<T> > & array, const boost::shared_ptr<std::vector<U> > & arrayToCopy) const
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
38
	{
39
40
41
42
43
44
45
46
47
48
49
50
51
		int size = mStartIndex + mNumValues;
		if(mArrayStride > 1)
		{
			size = mStartIndex + mNumValues * mArrayStride - 1;
		}
		if(array->size() < size)
		{
			array->resize(size);
		}
		for(int i=0; i<mNumValues; ++i)
		{
			array->operator[](mStartIndex + i*mArrayStride) = (T)arrayToCopy->operator[](mValuesStartIndex + i*mValuesStride);
		}
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
52
53
	}

54
55
56
57
58
59
60
61
62
private:

	int mStartIndex;
	int mValuesStartIndex;
	int mNumValues;
	int mArrayStride;
	int mValuesStride;
};

63
class XdmfArrayGetHDF5Type : public boost::static_visitor <hid_t> {
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
64
public:
65
	XdmfArrayGetHDF5Type()
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
66
67
68
	{
	}

69
	hid_t operator()(const boost::shared_ptr<std::vector<char> > & array) const
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
70
	{
71
		return H5T_NATIVE_CHAR;
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
72
73
	}

74
	hid_t operator()(const boost::shared_ptr<std::vector<short> > & array) const
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
75
	{
76
		return H5T_NATIVE_SHORT;
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
77
78
	}

79
	hid_t operator()(const boost::shared_ptr<std::vector<int> > & array) const
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
80
	{
81
		return H5T_NATIVE_INT;
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
82
83
	}

84
	hid_t operator()(const boost::shared_ptr<std::vector<long> > & array) const
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
85
	{
86
		return H5T_NATIVE_LONG;
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
87
88
	}

89
	hid_t operator()(const boost::shared_ptr<std::vector<float> > & array) const
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
90
	{
91
		return H5T_NATIVE_FLOAT;
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
92
93
	}

94
	hid_t operator()(const boost::shared_ptr<std::vector<double> > & array) const
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
95
	{
96
		return H5T_NATIVE_DOUBLE;
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
97
98
	}

99
	hid_t operator()(const boost::shared_ptr<std::vector<unsigned char> > & array) const
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
100
	{
101
		return H5T_NATIVE_UCHAR;
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
102
103
	}

104
	hid_t operator()(const boost::shared_ptr<std::vector<unsigned short> > & array) const
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
105
	{
106
		return H5T_NATIVE_USHORT;
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
107
108
	}

109
	hid_t operator()(const boost::shared_ptr<std::vector<unsigned int> > & array) const
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
110
	{
111
		return H5T_NATIVE_UINT;
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
112
113
114
	}
};

115
class XdmfArrayGetPrecision : public boost::static_visitor <int> {
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
116
public:
117
	XdmfArrayGetPrecision()
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
118
119
120
	{
	}

121
	int operator()(const boost::shared_ptr<std::vector<char> > & array) const
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
122
	{
123
		return 1;
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
124
125
	}

126
	int operator()(const boost::shared_ptr<std::vector<short> > & array) const
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
127
	{
128
		return 2;
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
129
130
	}

131
	int operator()(const boost::shared_ptr<std::vector<int> > & array) const
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
132
	{
133
		return 4;
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
134
135
	}

136
	int operator()(const boost::shared_ptr<std::vector<long> > & array) const
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
137
	{
138
		return 8;
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
139
140
	}

141
	int operator()(const boost::shared_ptr<std::vector<float> > & array) const
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
142
	{
143
		return 4;
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
144
145
	}

146
	int operator()(const boost::shared_ptr<std::vector<double> > & array) const
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
147
	{
148
		return 8;
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
149
150
	}

151
	int operator()(const boost::shared_ptr<std::vector<unsigned char> > & array) const
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
152
	{
153
		return 1;
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
154
155
	}

156
	int operator()(const boost::shared_ptr<std::vector<unsigned short> > & array) const
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
157
	{
158
		return 2;
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
159
160
	}

161
	int operator()(const boost::shared_ptr<std::vector<unsigned int> > & array) const
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
162
	{
163
		return 4;
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
164
165
166
	}
};

167
class XdmfArrayGetType : public boost::static_visitor <std::string> {
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
168
public:
169
	XdmfArrayGetType()
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
170
171
172
	{
	}

173
	std::string operator()(const boost::shared_ptr<std::vector<char> > & array) const
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
174
	{
175
		return "Char";
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
176
177
	}

178
	std::string operator()(const boost::shared_ptr<std::vector<short> > & array) const
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
179
	{
180
		return "Short";
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
181
182
	}

183
	std::string operator()(const boost::shared_ptr<std::vector<int> > & array) const
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
184
	{
185
		return "Int";
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
186
187
	}

188
	std::string operator()(const boost::shared_ptr<std::vector<long> > & array) const
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
189
	{
190
		return "Int";
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
191
192
	}

193
	std::string operator()(const boost::shared_ptr<std::vector<float> > & array) const
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
194
	{
195
		return "Float";
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
196
197
	}

198
	std::string operator()(const boost::shared_ptr<std::vector<double> > & array) const
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
199
	{
200
		return "Float";
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
201
202
	}

203
	std::string operator()(const boost::shared_ptr<std::vector<unsigned char> > & array) const
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
204
	{
205
		return "UChar";
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
206
207
	}

208
	std::string operator()(const boost::shared_ptr<std::vector<unsigned short> > & array) const
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
209
	{
210
		return "UShort";
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
211
212
	}

213
	std::string operator()(const boost::shared_ptr<std::vector<unsigned int> > & array) const
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
214
	{
215
		return "UInt";
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
216
217
218
219
220
221
222
223
224
225
	}
};

class XdmfArrayGetSize : public boost::static_visitor <int> {
public:

	XdmfArrayGetSize()
	{
	}

226
	template<typename T> int operator()(const boost::shared_ptr<std::vector<T> > & array) const
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
227
	{
228
		return array->size();
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
229
230
231
	}
};

232
class XdmfArrayGetValuesPointer : public boost::static_visitor <const void* const> {
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
233
234
public:

235
236
237
238
	XdmfArrayGetValuesPointer()
	{
	}

239
	template<typename T> const void* const operator()(const boost::shared_ptr<std::vector<T> > & array) const
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
240
	{
241
		return &array->operator[](0);
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
242
243
244
	}
};

245
class XdmfArrayGetValuesString : public boost::static_visitor <std::string> {
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
246
247
public:

248
	XdmfArrayGetValuesString()
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
249
250
251
	{
	}

252
	std::string operator()(const boost::shared_ptr<std::vector<char> > & array) const
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
253
254
	{
		std::stringstream toReturn;
255
		for(int i=0; i<array->size(); ++i)
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
256
		{
257
			toReturn << (int)(array->operator[](i)) << " ";
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
258
259
260
261
		}
		return toReturn.str();
	}

262
	template<typename T> std::string operator()(const boost::shared_ptr<std::vector<T> > & array) const
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
263
264
	{
		std::stringstream toReturn;
265
		for(int i=0; i<array->size(); ++i)
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
266
		{
267
			toReturn << array->operator[](i) << " ";
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
268
269
270
271
272
		}
		return toReturn.str();
	}
};

273
class XdmfArrayNewArray : public boost::static_visitor <void> {
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
274
275
public:

276
	XdmfArrayNewArray()
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
277
278
279
	{
	}

280
	template<typename T> void operator()(boost::shared_ptr<std::vector<T> > & array) const
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
281
	{
282
283
		boost::shared_ptr<std::vector<T> > newArray(new std::vector<T>());
		array = newArray;
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
284
285
286
287
288
289
290
291
292
293
294
295
296
297
	}
};

XdmfArray::XdmfArray() :
	mInitialized(false)
{
	std::cout << "Created Array " << this << std::endl;
}

XdmfArray::~XdmfArray()
{
	std::cout << "Deleted Array " << this << std::endl;
}

298
299
300
301
302
void XdmfArray::copyValues(int startIndex, boost::shared_ptr<XdmfArray> values, int valuesStartIndex, int numValues, int arrayStride, int valuesStride)
{
	if(!mInitialized)
	{
		// Copy the values variant in order to get the type (only taking smart pointer so no worries about large copies)
303
		mArray = values->mArray;
304
305
		// Reinitialize variant array to contain new array with same type.
		boost::apply_visitor( XdmfArrayNewArray(), mArray);
306
		mInitialized = true;
307
	}
308
	boost::apply_visitor( XdmfArrayCopyArrayValues(startIndex, valuesStartIndex, numValues, arrayStride, valuesStride), mArray, values->mArray);
309
310
311
312
}

void XdmfArray::clear()
{
313
	mInitialized = false;
314
315
316
	return boost::apply_visitor( XdmfArrayClear(), mArray);
}

Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
hid_t XdmfArray::getHDF5Type() const
{
	return boost::apply_visitor( XdmfArrayGetHDF5Type(), mArray);
}

int XdmfArray::getPrecision() const
{
	return boost::apply_visitor( XdmfArrayGetPrecision(), mArray);
}

int XdmfArray::getSize() const
{
	return boost::apply_visitor( XdmfArrayGetSize(), mArray);
}

std::string XdmfArray::getType() const
{
	return boost::apply_visitor( XdmfArrayGetType(), mArray);
}

337
const void* const XdmfArray::getValuesPointer() const
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
338
339
340
341
{
	return boost::apply_visitor( XdmfArrayGetValuesPointer(), mArray);
}

342
std::string XdmfArray::getValuesString() const
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
343
{
344
	return boost::apply_visitor( XdmfArrayGetValuesString(), mArray);
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
345
346
347
348
349
350
351
}

std::string XdmfArray::printSelf() const
{
	return "";
}

352
void XdmfArray::swap(boost::shared_ptr<XdmfArray> array)
353
354
355
356
357
358
359
{
	array->mArray.swap(mArray);
	bool initialized = array->mInitialized;
	array->mInitialized = mInitialized;
	mInitialized = initialized;
}

Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
360
361
void XdmfArray::write(boost::shared_ptr<XdmfVisitor> visitor) const
{
362
	visitor->visit(this, visitor);
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
363
}