Blame view

src/Common/ParameterIndexesTool.hh 5.12 KB
fbe3c2bb   Benjamin Renard   First commit
1
2
3
4
5
6
7
8
9
10
11
12
/*
 * ParameterIndexesTool.hh
 *
 *  Created on: Dec 10, 2014
 *      Author: AKKA
 */

#ifndef PARAMETERINDEXESTOOL_HH_
#define PARAMETERINDEXESTOOL_HH_

#include <boost/algorithm/string.hpp>
#include <boost/lexical_cast.hpp>
46ca92d0   Elena.Budnik   add / move header...
13
#include <vector>
fbe3c2bb   Benjamin Renard   First commit
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239

namespace AMDA {
namespace Common {

struct ParameterIndexComponent
{
public :
	ParameterIndexComponent(int dim1Index = -1, int dim2Index = -1) : _dim1Index(dim1Index), _dim2Index(dim2Index)
	{
	}

	int getDim1Index() const
	{
		return _dim1Index;
	}

	int getDim2Index() const
	{
		return _dim2Index;
	}

	bool isDefined()
	{
		return ((_dim1Index != -1) || (_dim2Index != -1));
	}

	/* operators */
	bool operator==(const ParameterIndexComponent& comp) const
	{
		return ((_dim1Index == comp._dim1Index) && (_dim2Index == comp._dim2Index));
	}

	bool operator!=(const ParameterIndexComponent& comp) const
	{
		return ((_dim1Index != comp._dim1Index) || (_dim2Index != comp._dim2Index));
	}

	bool operator<(const ParameterIndexComponent& comp) const
	{
		if (_dim1Index < comp._dim1Index)
			return true;
		if ((_dim1Index == comp._dim1Index) &&
			(_dim2Index < comp._dim2Index))
			return true;
		return false;
	}

	bool operator<=(const ParameterIndexComponent& comp) const
	{
		if (_dim1Index < comp._dim1Index)
			return true;
		if ((_dim1Index == comp._dim1Index) &&
			(_dim2Index <= comp._dim2Index))
			return true;
		return false;
	}

	bool operator>(const ParameterIndexComponent& comp) const
	{
		if (_dim1Index > comp._dim1Index)
			return true;
		if ((_dim1Index == comp._dim1Index) &&
			(_dim2Index > comp._dim2Index))
			return true;
		return false;
	}

	bool operator>=(const ParameterIndexComponent& comp) const
	{
		if (_dim1Index > comp._dim1Index)
			return true;
		if ((_dim1Index == comp._dim1Index) &&
			(_dim2Index >= comp._dim2Index))
			return true;
		return false;
	}

private :
	int _dim1Index;
	int _dim2Index;
};

typedef std::list<ParameterIndexComponent> ParameterIndexComponentList;

class ParameterIndexesTool {
public:
	ParameterIndexesTool(){}

	static bool parse(std::string indexesDef, int dim1Size, int dim2Size, ParameterIndexComponentList& indexList)
	{
		boost::algorithm::trim(indexesDef);

		if (indexesDef.empty())
		{
			if (dim2Size <= 0)
				indexesDef = "*";
			else
				indexesDef = "[*,*]";
		}

		//try to get single index definition
		int singleIndex;
		if (ParameterIndexesTool::getDimIndex(indexesDef, singleIndex))
		{
			if (singleIndex == -1)
			{
				for (int i = 0; i < dim1Size; ++i)
					indexList.push_back(ParameterIndexComponent(i));
			}
			else if ((singleIndex >= 0) && ((dim1Size == -1) || (singleIndex < dim1Size)))
				indexList.push_back(ParameterIndexComponent(singleIndex));
			else
				return false;

			return (!indexList.empty());
		}

		//try to get two dimensional index definition
		size_t pos = indexesDef.find("[");
		if (pos == std::string::npos)
			return false;

		if (indexesDef.find("]") == std::string::npos)
			return false;

		std::vector<std::string> indexes;
		std::string defPart = indexesDef.substr(1, indexesDef.length() - 2);
		boost::split(indexes, defPart, boost::is_any_of(","));
		if (indexes.size() != 2)
			return false;

		int dim1Index = -1;
		int dim2Index = -1;

		if (!getDimIndex(indexes[0], dim1Index) || !getDimIndex(indexes[1], dim2Index))
			return false;

		if (dim1Index == -1)
		{
			for (int i = 0; i < dim1Size; ++i)
			{
				if (dim2Index == -1)
				{
					for (int j = 0; j < dim2Size; ++j)
						indexList.push_back(ParameterIndexComponent(i,j));
				}
				else if ((dim2Index >= 0) && (dim2Index < dim2Size))
					indexList.push_back(ParameterIndexComponent(i,dim2Index));
				else
					return (!indexList.empty());
			}
		}
		else if ((dim1Index >= 0) || (dim1Index < dim1Size))
		{
			if (dim2Index == -1)
			{
				for (int j = 0; j < dim2Size; ++j)
					indexList.push_back(ParameterIndexComponent(dim1Index,j));
			}
			else if ((dim2Index >= 0) && ((dim2Size == -1) || (dim2Index < dim2Size)))
				indexList.push_back(ParameterIndexComponent(dim1Index,dim2Index));
			else
				return (!indexList.empty());
		}
		else
			return false;

		return true;
	}

	static int getRelatedDimForTab2D(std::string indexesDef)
	{
		boost::algorithm::trim(indexesDef);

		if (indexesDef.empty())
			return 0;

		size_t pos = indexesDef.find("[");
		if (pos == std::string::npos)
			return 0;

		if (indexesDef.find("]") == std::string::npos)
			return 0;

		std::vector<std::string> indexes;
		std::string defPart = indexesDef.substr(1, indexesDef.length() - 2);
		boost::split(indexes, defPart, boost::is_any_of(","));
		if (indexes.size() != 2)
			return 0;

		boost::algorithm::trim(indexes[0]);
		boost::algorithm::trim(indexes[1]);

		if ((indexes[0].compare("*") == 0) && (indexes[1].compare("*") != 0))
			return 0;

		if ((indexes[0].compare("*") != 0) && (indexes[1].compare("*") == 0))
			return 1;

		return 0;
	}
private:

	static bool getDimIndex(std::string dimDef, int& dimIndex)
	{
		dimIndex = -1;
		boost::algorithm::trim(dimDef);
		if (dimDef.compare("*") != 0)
		{
			try
			{
				dimIndex = boost::lexical_cast<int>(dimDef);
			}
			catch(boost::bad_lexical_cast& e)
			{
				return false;
			}
		}

		return true;
	}
};

} /* Common */
} /* AMDA */
#endif