XdmfArray.hpp 4.6 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
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
/*
 * XdmfArray.hpp
 *
 *  Created on: Jan 25, 2010
 *      Author: kleiter
 */

#ifndef XDMFARRAY_HPP_
#define XDMFARRAY_HPP_

// Includes
#include "XdmfItem.hpp"
#include <boost/variant.hpp>
#include <hdf5.h>
#include <vector>

template<typename T>
class XdmfArraySetValues : public boost::static_visitor <void> {
public:

	XdmfArraySetValues(int startIndex, T* valuesPointer, int numValues, int arrayStride = 1, int valuesStride = 1) :
		mStartIndex(startIndex),
		mValuesPointer(valuesPointer),
		mNumValues(numValues),
		mArrayStride(arrayStride),
		mValuesStride(valuesStride)
	{
	}

30
	template<typename U> void operator()(boost::shared_ptr<std::vector<U> > array) const
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
31
32
33
34
35
36
	{
		int size = mStartIndex + mNumValues;
		if(mArrayStride > 1)
		{
			size = mStartIndex + mNumValues * mArrayStride - 1;
		}
37
		if(array->size() < size)
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
38
		{
39
			array->resize(size);
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
40
41
42
		}
		for(int i=0; i<mNumValues; ++i)
		{
43
			array->operator[](mStartIndex + i*mArrayStride) = (U)mValuesPointer[i*mValuesStride];
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
		}
	}

private:

	int mStartIndex;
	T* mValuesPointer;
	int mNumValues;
	int mArrayStride;
	int mValuesStride;
};

class XdmfArray : public XdmfItem {
public:

	XdmfNewMacro(XdmfArray);

61
62
63
64
65
66
67
68
69
70
71
	/**
	 * Copy the values from a vector into this array
	 *
	 * @param array the vector to copy into this array.
	 */
    template<typename T> void copyValues(std::vector<T> & array)
    {
    	boost::shared_ptr<std::vector<T> > newArray(new std::vector<T>(array));
    	mArray = newArray;
    }

Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
	/**
     * Get the data type of this array.
     *
     * @return a string containing the Xdmf data type for the array, this is one of
     *      Char, Short, Int, Float, UChar, UShort, UInt.
     */
	virtual std::string getType() const;

	/**
	 * Get the hdf5 data type of this array.
	 *
	 * @return a hid_t value containing the hdf5 data type for the array.
	 */
	virtual hid_t getHDF5Type() const;

	/**
	 * Get the precision, in bytes, of the data type of this array.
	 *
	 * @return a int containing the precision, in bytes, of the data type of this array.
	 */
	virtual int getPrecision() const;

	/**
	 * Get the number of values stored in this array.
	 *
	 * @return a int containing the number of values stored in this array.
	 */
	virtual int getSize() const;

	/**
	 * Get the values stored in this array as a string.
	 *
	 * @return a string containing the contents of the array.
	 */
	virtual std::string getValues() const;

	/**
	 * Get a pointer to the values stored in this array.
	 *
	 * @return a void pointer to the first value stored in this array.
	 */
	virtual void* getValuesPointer();

	/**
	 * Get a pointer to the values stored in this array (const version).
	 *
	 * @return a void pointer to the first value stored in this array.
	 */
	virtual const void* const getValuesPointer() const;

	/**
123
	 * Insert the values from a pointer into this array.
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
124
125
126
127
128
129
130
	 *
	 * @param startIndex the index in this array to begin insertion.
	 * @param valuesPointer a pointer to the values to copy into this array.
	 * @param numValues the number of values to copy into this array.
	 * @param arrayStride number of values to stride in this array between each copy.
	 * @param valuesStride number of values to stride in the pointer between each copy.
	 */
131
	template<typename T> void insertValues(int startIndex, T* valuesPointer, int numValues = 1, int arrayStride = 1, int valuesStride = 1)
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
132
133
134
135
	{
		if(!mInitialized)
		{
			// Set type of variant to type of pointer
136
137
			boost::shared_ptr<std::vector<T> > newArray(new std::vector<T>());
			mArray = newArray;
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
138
			mInitialized = true;
139
140
		}
		boost::apply_visitor( XdmfArraySetValues<T>(startIndex, valuesPointer, numValues, arrayStride, valuesStride), mArray);
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
141
142
	}

143
144
	virtual std::string printSelf() const;

Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
145
	/**
146
147
	 * Sets the values of this array to the values stored in the vector.  No copy is made.  This array shares ownership with
	 * other references to the smart pointer.
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
148
	 *
149
	 * @param a smart pointer to a vector to store in this array.
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
150
	 */
151
	template<typename T> void setValues(boost::shared_ptr<std::vector<T> > array)
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
	{
		mArray = array;
	}

	virtual void write(boost::shared_ptr<XdmfVisitor>) const;

protected:

	XdmfArray();
	virtual ~XdmfArray();

private:

	bool mInitialized;

167
168
169
170
171
172
173
174
175
	boost::variant<boost::shared_ptr<std::vector<char> >,
				   boost::shared_ptr<std::vector<short> >,
				   boost::shared_ptr<std::vector<int> >,
				   boost::shared_ptr<std::vector<long> >,
				   boost::shared_ptr<std::vector<float> >,
				   boost::shared_ptr<std::vector<double> >,
				   boost::shared_ptr<std::vector<unsigned char> >,
				   boost::shared_ptr<std::vector<unsigned short> >,
				   boost::shared_ptr<std::vector<unsigned int> > > mArray;
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
176
177
178
};

#endif /* XDMFARRAY_HPP_ */