Particle Identification and Tracking
ndarray.h
Go to the documentation of this file.
1 //Copyright 2010 Thomas A Caswell
2 //tcaswell@uchicago.edu
3 //http://jfi.uchicago.edu/~tcaswell
4 //
5 //This program is free software; you can redistribute it and/or modify
6 //it under the terms of the GNU General Public License as published by
7 //the Free Software Foundation; either version 3 of the License, or (at
8 //your option) any later version.
9 //
10 //This program is distributed in the hope that it will be useful, but
11 //WITHOUT ANY WARRANTY; without even the implied warranty of
12 //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 //General Public License for more details.
14 //
15 //You should have received a copy of the GNU General Public License
16 //along with this program; if not, see <http://www.gnu.org/licenses>.
17 //
18 //Additional permission under GNU GPL version 3 section 7
19 //
20 //If you modify this Program, or any covered work, by linking or
21 //combining it with MATLAB (or a modified version of that library),
22 //containing parts covered by the terms of MATLAB User License, the
23 //licensors of this Program grant you additional permission to convey
24 //the resulting work.
25 #ifndef ARRAY_ND
26 #define ARRAY_ND
27 
28 
29 #include "tuple.h"
30 #include <iostream>
31 namespace utilities{
32 
33 
39 template <class T,int N>
40 class ND_Array{
41 public:
46  const T * data_ptr() const{
47  return data_ptr_;
48  }
49 
53  T& operator()(const Tuple<int,N> & pos);
57  const T& operator()(const Tuple<int,N> & pos)const;
58 
59 
63  T& operator()(int pos);
67  const T& operator()(int pos)const;
68 
69 
70  ND_Array(const Tuple<int,N> & dim);
71  ~ND_Array();
72 
76  void print() const;
77 
82  void fill_test();
86  const static int rank_ = N;
87 
88 
89 
90 private:
91 
93  T * data_ptr_;
99  const int * dims_ptr_;
100 
106  const int * cum_dims_ptr_;
107 
112 
113 
114  int cord_to_indx(const Tuple<int,N> & pos) const;
115  Tuple<int,N> indx_to_cord(int indx) const;
116 
120  bool increment_cord( Tuple<int,N> & cord)const;
121 
122 
123 
124 };
125 template <class T,int N>
127 {
128  return data_ptr_[cord_to_indx(pos)];
129 }
130 template <class T,int N>
131 const T& ND_Array<T,N>::operator()(const Tuple<int,N> & pos)const
132 {
133  return data_ptr_[cord_to_indx(pos)];
134 }
135 
136 
137 template <class T,int N>
139 {
140  if(!(pos < elm_count_))
141  throw std::out_of_range("posistion out of range");
142  return data_ptr_[pos];
143 }
144 
145 template <class T,int N>
146 const T& ND_Array<T,N>::operator()(int pos) const
147 {
148  if(!(pos < elm_count_))
149  throw std::out_of_range("posistion out of range");
150  return data_ptr_[pos];
151 }
152 
153 
154 template <class T,int N>
156  dims_(dims),cum_dims_(0)
157 {
158  int dim_prod = 1;
159 
160 
161 
162 
163  for(int j = 0; j<rank_;++j)
164  {
165  dim_prod*=dims_[j];
166 
167 
168  unsigned int tmp_prod = 1;
169  for(int k = 0;k<(j);++k)
170  {
171  tmp_prod*=dims_[k];
172  }
173  cum_dims_[j] = tmp_prod;
174  }
175  elm_count_ = dims_.prod();
176 
177  data_ptr_= new T[dim_prod] ;
178  dims_ptr_ = dims_.get_ptr();
180 
181 
182 }
183 
184 template <class T,int N>
186 {
187  delete [] data_ptr_;
188 }
189 
190 template <class T,int N>
192 {
193  int indx = 0;
194 
195  for(int j = 0; j<rank_;++j)
196  {
197  int tmp = in[j];
198 
199  if (tmp>dims_ptr_[j])
200  throw std::out_of_range("cord out of dimensions");
201  indx +=tmp*cum_dims_ptr_[j];
202  }
203  return indx;
204 
205 }
206 template <class T,int N>
208 {
209  if(!(indx<elm_count_))
210  throw std::out_of_range("Index out of range");
211 
212  const Tuple<int,N> & cord;
213  for(int j = 0;j<N;++j)
214  cord[j] = (indx/cum_dims_ptr_[j])%dims_ptr_[j];
215 
216  return Tuple<int,N> (cord);
217 
218 }
219 
220 template <class T,int N>
222 {
223  int max = dims_.prod();
224  for(int j= 0; j<max;++j)
225  data_ptr_[j] = j;
226 
227 }
228 
229 
230 template <class T,int N>
232 {
233  using std::cout;
234  using std::endl;
235 
236  Tuple<int,N> plane;
237  do
238  {
239  cout<<"plane "<<plane<<endl;
240  int offset = cord_to_indx(plane);
241  for(int j = 0;j<dims_[0];++j)
242  {
243  for(int k = 0;k<dims_[1];++k)
244  {
245  cout<<data_ptr_[offset + j + cum_dims_[1]*k]<<'\t';
246  }
247  cout<<endl;
248 
249  }
250 
251  }
252  while(increment_cord(plane));
253 
254 
255 }
256 
257 template <class T,int N>
259 {
260 
261  for(int j = rank_-1;j>1;--j)
262  {
263  if(cord[j]+1 < dims_ptr_[j])
264  {
265  ++cord[j];
266  return true;
267  }
268  else
269  cord[j] = 0;
270 
271  }
272  return false;
273 }
274 
275 
276 }
277 
278 
279 
280 
281 
282 #endif