ArrayArithmetic.h
Go to the documentation of this file.
1 #ifndef GfxTL__ARRAYARITHMETIC_HEADER__
2 #define GfxTL__ARRAYARITHMETIC_HEADER__
3 #include <algorithm>
4 
5 namespace GfxTL
6 {
7 
8  // c-style (row major) array layout
9  template< unsigned int DimT >
11  {
12  public:
14 
15  template< class ExtentT >
16  ArrayArithmetic(const ExtentT& ext)
17  {
18  Extent(ext);
19  }
20 
21  template< class ExtentT >
22  void Extent(const ExtentT& ext)
23  {
24  m_fac[DimT - 1] = 1;
25  for (unsigned int i = DimT - 1; i != 0; --i)
26  {
27  m_fac[i - 1] = m_fac[i] * ext[i];
28  }
29  m_transFac[0] = 1;
30  for (unsigned int i = 0; i < DimT - 1; ++i)
31  {
32  m_transFac[i + 1] = m_transFac[i] * ext[i];
33  }
34  }
35 
36  template< class SubscriptT >
37  size_t Sub2Idx(const SubscriptT& sub) const
38  {
39  size_t idx = sub[0] * m_fac[0];
40  for (unsigned int i = 1; i < DimT; ++i)
41  {
42  idx += sub[i] * m_fac[i];
43  }
44  return idx;
45  }
46 
47  template< class SubscriptT >
48  size_t Sub2TransposedIdx(const SubscriptT& sub) const
49  {
50  size_t idx = sub[0] * m_transFac[0];
51  for (unsigned int i = 1; i < DimT; ++i)
52  {
53  idx += sub[i] * m_transFac[i];
54  }
55  return idx;
56  }
57 
58  template< class SubscriptT >
59  intptr_t Sub2Off(const SubscriptT& sub) const
60  {
61  intptr_t off = sub[0] * m_fac[0];
62  for (unsigned int i = 1; i < DimT; ++i)
63  {
64  off += sub[i] * m_fac[i];
65  }
66  return off;
67  }
68 
69  template< class SubscriptT >
70  void Idx2Sub(size_t idx, SubscriptT* sub) const
71  {
72  for (unsigned int i = 0; i < DimT; ++i)
73  {
74  (*sub)[i] = idx / m_fac[i];
75  idx %= m_fac[i];
76  }
77  }
78 
79  template< class SubscriptT >
80  void TransposedIdx2Sub(size_t tidx, SubscriptT* sub) const
81  {
82  for (unsigned int i = DimT - 1; i != -1; --i)
83  {
84  (*sub)[i] = tidx / m_transFac[i];
85  tidx %= m_transFac[i];
86  }
87  }
88 
89  // convert to column major index (Fortran and Matlab array layout)
90  size_t Idx2TransposedIdx(size_t idx) const
91  {
92  size_t tidx = 0;
93  for (unsigned int i = 0; i < DimT; ++i)
94  {
95  size_t j = idx / m_fac[i];
96  idx %= m_fac[i];
97  tidx += j * m_transFac[i];
98  }
99  return tidx;
100  }
101 
102  // convert column major index (Fortran and Matlab array layout) to row major (c/c++ array layout)
103  size_t TransposedIdx2Idx(size_t tidx) const
104  {
105  size_t idx = 0;
106  for (unsigned int i = DimT - 1; i != -1; --i)
107  {
108  size_t j = tidx / m_transFac[i];
109  tidx %= m_transFac[i];
110  idx += j * m_fac[i];
111  }
112  return idx;
113  }
114 
115  private:
116  size_t m_fac[DimT];
117  size_t m_transFac[DimT];
118  };
119 
120 };
121 
122 #endif
GfxTL::ArrayArithmetic::ArrayArithmetic
ArrayArithmetic(const ExtentT &ext)
Definition: ArrayArithmetic.h:16
GfxTL::ArrayArithmetic
Definition: ArrayArithmetic.h:10
GfxTL::ArrayArithmetic::Idx2Sub
void Idx2Sub(size_t idx, SubscriptT *sub) const
Definition: ArrayArithmetic.h:70
GfxTL::ArrayArithmetic::Sub2Off
intptr_t Sub2Off(const SubscriptT &sub) const
Definition: ArrayArithmetic.h:59
GfxTL::ArrayArithmetic::Sub2TransposedIdx
size_t Sub2TransposedIdx(const SubscriptT &sub) const
Definition: ArrayArithmetic.h:48
GfxTL::ArrayArithmetic::ArrayArithmetic
ArrayArithmetic()
Definition: ArrayArithmetic.h:13
GfxTL::ArrayArithmetic::TransposedIdx2Sub
void TransposedIdx2Sub(size_t tidx, SubscriptT *sub) const
Definition: ArrayArithmetic.h:80
GfxTL::ArrayArithmetic::TransposedIdx2Idx
size_t TransposedIdx2Idx(size_t tidx) const
Definition: ArrayArithmetic.h:103
GfxTL
Definition: AABox.h:8
GfxTL::ArrayArithmetic::Extent
void Extent(const ExtentT &ext)
Definition: ArrayArithmetic.h:22
GfxTL::ArrayArithmetic::Idx2TransposedIdx
size_t Idx2TransposedIdx(size_t idx) const
Definition: ArrayArithmetic.h:90
sub
Point sub(const Point &x, const Point &y)
Definition: point.hpp:43
GfxTL::ArrayArithmetic::Sub2Idx
size_t Sub2Idx(const SubscriptT &sub) const
Definition: ArrayArithmetic.h:37