Array.h
Go to the documentation of this file.
1 #ifndef GfxTL__ARRAY_HEADER__
2 #define GfxTL__ARRAY_HEADER__
3 #include <iterator>
4 #include <iostream>
5 
6 namespace GfxTL
7 {
8 
9  template< unsigned int DimT, class IteratorT >
11  {
12  public:
13  ArrayAccessor(IteratorT arr, const size_t* fac)
14  : m_arr(arr)
15  , m_fac(fac)
16  {}
17 
18  ArrayAccessor < DimT - 1, IteratorT > operator[](size_t i)
19  {
20  return ArrayAccessor < DimT - 1, IteratorT > (m_arr + i * (*m_fac), m_fac + 1);
21  }
22 
23  const ArrayAccessor < DimT - 1, IteratorT > operator[](size_t i) const
24  {
25  return ArrayAccessor < DimT - 1, IteratorT > (m_arr + i * (*m_fac), m_fac + 1);
26  }
27 
28  IteratorT begin() const
29  {
30  return m_arr;
31  }
32  IteratorT end() const
33  {
34  return m_arr + *(m_fac - 1);
35  }
36 
37  private:
38  IteratorT m_arr;
39  const size_t* m_fac;
40  };
41 
42  template< class IteratorT >
43  class ArrayAccessor< 1, IteratorT >
44  {
45  public:
46  typedef typename std::iterator_traits< IteratorT >::value_type value_type;
47  typedef typename std::iterator_traits< IteratorT >::reference reference;
48 
49  ArrayAccessor(IteratorT arr, const size_t* fac)
50  : m_arr(arr)
51  , m_fac(fac)
52  {}
53 
54  reference operator[](size_t i) const
55  {
56  return m_arr[i];
57  }
58 
59  IteratorT begin() const
60  {
61  return m_arr;
62  }
63  IteratorT end() const
64  {
65  return m_arr + *(m_fac - 1);
66  }
67 
68  private:
69  IteratorT m_arr;
70  const size_t* m_fac;
71  };
72 
73  template< unsigned int DimT, class IteratorT >
74  class Array
75  {
76  public:
77  typedef typename std::iterator_traits< IteratorT >::value_type value_type;
78  typedef IteratorT iterator;
80  template< unsigned int D >
81  friend std::ostream& operator<<(std::ostream& o, const Array< D, IteratorT >& a);
82 
84  {
85  for (unsigned int i = 0; i < DimT; ++i)
86  {
87  m_ext[i] = 0;
88  }
89  }
90 
91  Array(IteratorT arr, const size_t* ext)
92  : m_arr(arr)
93  {
94  SetExtent(ext);
95  }
96 
97  void SetExtent(const size_t* ext)
98  {
99  for (unsigned int i = 0; i < DimT; ++i)
100  {
101  m_ext[i] = ext[i];
102  }
103  m_fac[DimT - 1] = 1;
104  for (unsigned int i = DimT - 1; i != 0; --i)
105  {
106  m_fac[i - 1] = m_fac[i] * m_ext[i];
107  }
108  }
109 
111  {
112  m_arr = i;
113  }
114 
115  ArrayAccessor < DimT - 1, IteratorT > operator[](size_t i)
116  {
117  return ArrayAccessor < DimT - 1, IteratorT > (m_arr + i * m_fac[0], m_fac + 1);
118  }
119 
120  const ArrayAccessor < DimT - 1, IteratorT > operator[](size_t i) const
121  {
122  return ArrayAccessor < DimT - 1, IteratorT > (m_arr + i * m_fac[0], m_fac + 1);
123  }
124 
125  const size_t Extent(unsigned int d) const
126  {
127  return m_ext[d];
128  }
129 
130  const size_t* Extent() const
131  {
132  return m_ext;
133  }
134 
136  {
137  return m_arr;
138  }
140  {
141  return m_arr + m_fac[0] * m_ext[0];
142  }
143  iterator begin() const
144  {
145  return m_arr;
146  }
147  iterator end() const
148  {
149  return m_arr + m_fac[0] * m_ext[0];
150  }
151 
152  private:
153  Array(const ThisType& a)
154  {}
155 
156  void operator=(const ThisType& a)
157  {}
158 
159  private:
160  IteratorT m_arr;
161  size_t m_fac[DimT];
162  size_t m_ext[DimT];
163  };
164 
165  template< unsigned int DimT, class IteratorT >
166  std::ostream& operator<<(std::ostream& o, const Array< DimT, IteratorT >& a)
167  {
168  o << "[";
169  for (size_t i = 0; i < a.Extent(0); ++i)
170  {
171  o << Array < DimT - 1, IteratorT > (a.m_arr + a.m_fac[0] * i, a.m_ext + 1);
172  if (i < a.Extent(0) - 1)
173  {
174  o << ", ";
175  }
176  }
177  o << "]";
178  return o;
179  }
180 
181  template< class IteratorT >
182  std::ostream& operator<<(std::ostream& o, const Array< 1, IteratorT >& a)
183  {
184  o << "[";
185  for (size_t i = 0; i < a.Extent(0); ++i)
186  {
187  o << a.m_arr[i];
188  if (i < a.Extent(0) - 1)
189  {
190  o << ", ";
191  }
192  }
193  o << "]";
194  return o;
195  }
196 
197 };
198 
199 #endif
GfxTL::ArrayAccessor< 1, IteratorT >::operator[]
reference operator[](size_t i) const
Definition: Array.h:54
GfxTL::ArrayAccessor::operator[]
const ArrayAccessor< DimT - 1, IteratorT > operator[](size_t i) const
Definition: Array.h:23
GfxTL::Array::operator<<
friend std::ostream & operator<<(std::ostream &o, const Array< D, IteratorT > &a)
GfxTL::Array::ThisType
Array< DimT, IteratorT > ThisType
Definition: Array.h:79
GfxTL::ArrayAccessor< 1, IteratorT >::value_type
std::iterator_traits< IteratorT >::value_type value_type
Definition: Array.h:46
GfxTL::Array::begin
iterator begin()
Definition: Array.h:135
GfxTL::ArrayAccessor::ArrayAccessor
ArrayAccessor(IteratorT arr, const size_t *fac)
Definition: Array.h:13
GfxTL::Array
Definition: Array.h:74
GfxTL::Array::operator[]
ArrayAccessor< DimT - 1, IteratorT > operator[](size_t i)
Definition: Array.h:115
GfxTL::Array::end
iterator end() const
Definition: Array.h:147
GfxTL::ArrayAccessor::begin
IteratorT begin() const
Definition: Array.h:28
GfxTL::Array::iterator
IteratorT iterator
Definition: Array.h:78
armarx::ctrlutil::a
double a(double t, double a0, double j)
Definition: CtrlUtil.h:45
GfxTL::operator<<
std::ostream & operator<<(std::ostream &o, const Array< DimT, IteratorT > &a)
Definition: Array.h:166
GfxTL::ArrayAccessor::end
IteratorT end() const
Definition: Array.h:32
GfxTL::ArrayAccessor::operator[]
ArrayAccessor< DimT - 1, IteratorT > operator[](size_t i)
Definition: Array.h:18
GfxTL::Array::Extent
const size_t * Extent() const
Definition: Array.h:130
GfxTL
Definition: AABox.h:8
GfxTL::ArrayAccessor
Definition: Array.h:10
GfxTL::Array::begin
iterator begin() const
Definition: Array.h:143
GfxTL::ArrayAccessor< 1, IteratorT >::reference
std::iterator_traits< IteratorT >::reference reference
Definition: Array.h:47
GfxTL::Array::end
iterator end()
Definition: Array.h:139
GfxTL::Array::Array
Array(IteratorT arr, const size_t *ext)
Definition: Array.h:91
GfxTL::ArrayAccessor< 1, IteratorT >::end
IteratorT end() const
Definition: Array.h:63
GfxTL::Array::SetExtent
void SetExtent(const size_t *ext)
Definition: Array.h:97
GfxTL::Array::value_type
std::iterator_traits< IteratorT >::value_type value_type
Definition: Array.h:77
GfxTL::ArrayAccessor< 1, IteratorT >::ArrayAccessor
ArrayAccessor(IteratorT arr, const size_t *fac)
Definition: Array.h:49
GfxTL::Array::Array
Array()
Definition: Array.h:83
GfxTL::Array::Iterator
void Iterator(iterator i)
Definition: Array.h:110
GfxTL::Array::Extent
const size_t Extent(unsigned int d) const
Definition: Array.h:125
GfxTL::ArrayAccessor< 1, IteratorT >::begin
IteratorT begin() const
Definition: Array.h:59
GfxTL::Array::operator[]
const ArrayAccessor< DimT - 1, IteratorT > operator[](size_t i) const
Definition: Array.h:120