ADTF  3.18.2
multiarray.h
Go to the documentation of this file.
1 
7 #ifndef _MULTIARRAY_CLASS_HEADER_
8 #define _MULTIARRAY_CLASS_HEADER_
9 
10 
11 namespace A_UTILS_NS
12 {
13 
22 class DOEXPORT cMultiArrayIndex
23 {
24 public:
31 
39  explicit cMultiArrayIndex(tUInt nX);
40 
50 
61 
73 
86 
94  tBool IsValid() const;
95 
104  tResult AddDimension(tUInt nDimensionValue);
105 
115  tResult GetDimensionValue(tUInt nDimensionIndex, tUInt& nValue) const;
116 
126  tResult SetDimensionValue(tUInt nDimensionIndex, tUInt nValue);
127 
136 
137 protected:
143  std::vector<tUInt> m_oDimensionvalues;
144 };
145 
156 
167 
177 
197 template<typename T>
198 class DOEXPORT cMultiArray
199 {
200 protected:
207 
213  std::vector<T> m_oData;
214 
215 protected:
225  {
226  if (m_oDimensions.NumDimensions() < 1)
227  {
228  return ERR_INVALID_STATE;
229  }
230 
231  tUInt nTotal = 1;
232  for (tUInt nIndex = 0; nIndex < m_oDimensions.NumDimensions(); ++nIndex)
233  {
234  tUInt nCurrent = 1;
235  if (IS_FAILED(m_oDimensions.GetDimensionValue(nIndex, nCurrent)))
236  {
237  return ERR_INVALID_STATE;
238  }
239 
240  if (nCurrent == 0)
241  {
242  return ERR_INVALID_ARG;
243  }
244 
245  nTotal *= nCurrent;
246  }
247 
248  m_oData = std::vector<T>(nTotal);
250  }
251 
260  tBool ContainsIndex(const cMultiArrayIndex& oIndex) const
261  {
262  if (!m_oDimensions.IsValid() ||
263  !oIndex.IsValid() ||
264  m_oDimensions.NumDimensions() != oIndex.NumDimensions())
265  {
266  return false;
267  }
268 
269  for (tUInt nIndex = 0; nIndex < oIndex.NumDimensions(); ++nIndex)
270  {
271  tUInt nValue = 0;
272  if (IS_FAILED(oIndex.GetDimensionValue(nIndex, nValue)))
273  {
274  return false;
275  }
276 
277  tUInt nThisValue = 0;
278  if (IS_FAILED(m_oDimensions.GetDimensionValue(nIndex, nThisValue)))
279  {
280  return false;
281  }
282 
283  if (nValue >= nThisValue)
284  {
285  return false;
286  }
287  }
288 
289  return true;
290  }
291 
302  tResult CalculateInternIndex(const cMultiArrayIndex& oIndex, tUInt& nInternIndex) const
303  {
304  if (!oIndex.IsValid() ||
305  !m_oDimensions.IsValid() ||
306  oIndex.NumDimensions() != m_oDimensions.NumDimensions())
307  {
308  return ERR_INVALID_INDEX;
309  }
310 
311  // we assume that the index is contained in the array
312 
313  // see http://en.wikipedia.org/wiki/Row-major_order
314  tUInt nIndex = 0;
315  for (tUInt k = 1; k <= oIndex.NumDimensions(); ++k)
316  {
317  tUInt nProduct = 1;
318  for (tUInt nL = k; nL < m_oDimensions.NumDimensions(); ++nL)
319  {
320  tUInt nN = 0;
321  if (IS_FAILED(m_oDimensions.GetDimensionValue(nL, nN)))
322  {
323  return ERR_INVALID_INDEX;
324  }
325 
326  nProduct *= nN;
327  }
328 
329  tUInt nK = 0;
330  if (IS_FAILED(oIndex.GetDimensionValue(k - 1, nK)))
331  {
332  return ERR_INVALID_INDEX;
333  }
334 
335  nIndex += (nProduct * nK);
336  }
337 
338  nInternIndex = nIndex;
339  return ERR_NOERROR;
340  }
341 
342 public:
348  cMultiArray() : m_oDimensions(), m_oData()
349  {
350  }
351 
361  explicit cMultiArray(const cMultiArrayDimensions& oDimensions) :
362  m_oDimensions(oDimensions), m_oData()
363  {
364  if (IS_FAILED(InitializeDataStore()))
365  {
366  // invalidate the dimensions
367  m_oDimensions = cMultiArrayDimensions();
368  }
369  }
370 
376  virtual ~cMultiArray()
377  {
378  }
379 
387  tBool IsValid() const
388  {
389  return m_oDimensions.IsValid() && !m_oData.empty();
390  }
391 
400  {
401  return m_oData.size();
402  }
403 
413  {
414  return m_oDimensions;
415  }
416 
426  tResult Set(const cMultiArrayIndex& oIndex, T value)
427  {
428  if (!oIndex.IsValid() || !ContainsIndex(oIndex))
429  {
430  return ERR_INVALID_INDEX;
431  }
432 
433  tUInt nInternIndex = 0;
434  if (IS_FAILED(CalculateInternIndex(oIndex, nInternIndex)))
435  {
436  return ERR_INVALID_INDEX;
437  }
438 
439  m_oData[nInternIndex] = value;
440 
442  }
443 
453  tResult Get(const cMultiArrayIndex& oIndex, T& value) const
454  {
455  if (!oIndex.IsValid() || !ContainsIndex(oIndex))
456  {
457  return ERR_INVALID_INDEX;
458  }
459 
460  tUInt nInternIndex = 0;
461  if (IS_FAILED(CalculateInternIndex(oIndex, nInternIndex)))
462  {
463  return ERR_INVALID_INDEX;
464  }
465 
466  value = m_oData[nInternIndex];
467 
469  }
470 };
471 
482 template <typename T>
484 
492 template <>
494 
502 template <>
504 
512 template <>
514 
526 template <typename T>
528 
538 template <>
540 
550 template <>
552 
562 template <>
564 
565 } // namespace A_UTILS_NS
566 
567 #endif // _MULTIARRAY_CLASS_HEADER_
unsigned int tUInt
type definition for unsigned integer value (platform and compiler dependent type).
bool tBool
The tBool defines the type for the Values tTrue and tFalse (platform and compiler dependent).
size_t tSize
type definition for a array size values, map size values etc.
#define RETURN_NOERROR
Return status ERR_NOERROR, which requires the calling function's return type to be tResult.
Multi dimensional array class template.
Definition: multiarray.h:199
cMultiArray(const cMultiArrayDimensions &oDimensions)
Constructor that initializes the array to the dimensions specified in the parameter.
Definition: multiarray.h:361
virtual ~cMultiArray()
Virtual destructor.
Definition: multiarray.h:376
tBool IsValid() const
Check if the array is in a valid state and initialized correctly.
Definition: multiarray.h:387
tResult CalculateInternIndex(const cMultiArrayIndex &oIndex, tUInt &nInternIndex) const
Calculate the internal flat index that matches the multi dimensional index specified.
Definition: multiarray.h:302
const cMultiArrayDimensions & Dimensions() const
Get the dimensions of the array.
Definition: multiarray.h:412
cMultiArrayDimensions m_oDimensions
Stores the arrays dimension object.
Definition: multiarray.h:206
tResult InitializeDataStore()
Initializes the data vector to the correct size (specified by the dimensions of the array)
Definition: multiarray.h:224
tSize NumElements() const
Get the total number of stored elements in the array across all dimensions.
Definition: multiarray.h:399
std::vector< T > m_oData
Stores the arrays actual data (in a flat vector)
Definition: multiarray.h:213
tBool ContainsIndex(const cMultiArrayIndex &oIndex) const
Check if an index is in bound of the arrays dimensions.
Definition: multiarray.h:260
tResult Set(const cMultiArrayIndex &oIndex, T value)
Set the element at the specified array index.
Definition: multiarray.h:426
cMultiArray()
Constructor that initializes the array to an invalid state (0 dimensions)
Definition: multiarray.h:348
tResult Get(const cMultiArrayIndex &oIndex, T &value) const
Get the element at the specified array index.
Definition: multiarray.h:453
Multi dimensional index class used to dynamically address elements in a multi array.
Definition: multiarray.h:23
cMultiArrayIndex()
Constructor that initializes the index to an invalid state.
tSize NumDimensions() const
Get the number of dimensions.
cMultiArrayIndex(tUInt nX, tUInt nY, tUInt nZ, tUInt nT)
Constructor that initializes the index to 4 dimension values.
cMultiArrayIndex(tUInt nX, tUInt nY, tUInt nZ)
Constructor that initializes the index to 3 dimension values.
tBool IsValid() const
Check if the index is valid.
tResult GetDimensionValue(tUInt nDimensionIndex, tUInt &nValue) const
Get the value of the specified dimension.
cMultiArrayIndex(tUInt nX, tUInt nY)
Constructor that initializes the index to 2 dimension values.
tResult AddDimension(tUInt nDimensionValue)
Add a dimension value to the index.
std::vector< tUInt > m_oDimensionvalues
Stores the dimensional values.
Definition: multiarray.h:143
cMultiArrayIndex(tUInt nX)
Constructor that initializes the index to a single dimension value.
tResult SetDimensionValue(tUInt nDimensionIndex, tUInt nValue)
Set the value of the specified dimension.
cMultiArrayIndex(tUInt nX, tUInt nY, tUInt nZ, tUInt nT, tUInt nR)
Constructor that initializes the index to 4 dimension values.
ADTF A_UTIL Namespace - Within adtf this is used as adtf::util or adtf_util.
Definition: d_ptr.h:11
tResult ParseMultiArrayFromString< tFloat32 >(const cString &strRepr, cMultiArray< tFloat32 > &arrDest)
tFloat32 specialization for the ParseMultiArrayFromString function
tResult ParseMultiArrayFromString(const cString &strRepr, cMultiArray< T > &arrDest)
Base templated function to parse a cMultiArray from a string representation.
cString MultiArrayToString< tInt >(const cMultiArray< tInt > &oArray)
tInt specialization for the MultiArrayToString function
tBool operator==(const cMultiArrayIndex &o_A, const cMultiArrayIndex &o_B)
Comparison operator.
tResult ParseMultiArrayFromString< tFloat64 >(const cString &strRepr, cMultiArray< tFloat64 > &arrDest)
tFloat64 specialization for the ParseMultiArrayFromString function
tBool operator!=(const cMultiArrayIndex &o_A, const cMultiArrayIndex &o_B)
Comparison operator.
cMultiArrayIndex cMultiArrayDimensions
Type to specify array dimensions.
Definition: multiarray.h:176
cString MultiArrayToString(const cMultiArray< T > &oArray)
Base templated function to serialize a cMultiArray into a string.
tResult ParseMultiArrayFromString< tInt >(const cString &strRepr, cMultiArray< tInt > &arrDest)
tInt specialization for the ParseMultiArrayFromString function
cString MultiArrayToString< tFloat32 >(const cMultiArray< tFloat32 > &oArray)
tFloat32 specialization for the MultiArrayToString function
cString MultiArrayToString< tFloat64 >(const cMultiArray< tFloat64 > &oArray)
tFloat64 specialization for the MultiArrayToString function