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