libpnicore
array_operations.hpp
1 
25 #pragma once
26 
27 #include "../types/types.hpp"
28 
29 
30 namespace pni{
31 namespace core{
32 
33  //--------------------------------------------------------------------------
45  template<typename ARRAYT> typename ARRAYT::value_type min(const ARRAYT &a)
46  {
47  typedef typename ARRAYT::value_type RType;
48  RType result(a[0]);
49 
50 #ifdef NOFOREACH
51  for(auto iter=a.begin();iter!=a.end();iter++)
52  {
53  auto v = *iter;
54 #else
55  for(auto v: a)
56  {
57 #endif
58  if(v<result) result = v;
59  }
60 
61  return result;
62  }
63 
64  //--------------------------------------------------------------------------
75  template<typename ARRAYT> typename ARRAYT::value_type max(const ARRAYT &a)
76  {
77  typedef typename ARRAYT::value_type RType;
78 
79  RType result(a[0]);
80 
81 #ifdef NOFOREACH
82  for(auto iter=a.begin();iter!=a.end();iter++)
83  {
84  auto v = *iter;
85 #else
86  for(auto v: a)
87  {
88 #endif
89  if(v>result) result = v;
90  }
91 
92  return result;
93  }
94 
95  //--------------------------------------------------------------------------
107  template<typename ARRAYT> void min_max(const ARRAYT &a,
108  typename ARRAYT::value_type &min,
109  typename ARRAYT::value_type &max)
110  {
111  min=typename ARRAYT::value_type(a[0]);
112  max=typename ARRAYT::value_type(a[0]);
113 #ifdef NOFOREACH
114  for(auto iter=a.begin();iter!=a.end();iter++)
115  {
116  auto v=*iter;
117 #else
118  for(auto v: a)
119  {
120 #endif
121  if(v<min) min = v;
122  if(v>max) max = v;
123  }
124  }
125 
126  //--------------------------------------------------------------------------
137  template<typename ARRAYT> void clip(ARRAYT &a,
138  typename ARRAYT::value_type minth,
139  typename ARRAYT::value_type maxth)
140  {
141 #ifdef NOFOREACH
142  for(auto iter=a.begin();iter!=a.end();iter++)
143  {
144  typename ARRAYT::value_type &v = *iter;
145 #else
146  for(typename ARRAYT::value_type &v: a)
147  {
148 #endif
149  if(v <= minth)
150  {
151  v = minth;
152  continue;
153  }
154 
155  if(v >= maxth)
156  {
157  v = maxth;
158  continue;
159  }
160  }
161  }
162 
163  //--------------------------------------------------------------------------
176  template<typename ARRAYT> void clip(ARRAYT &a,
177  typename ARRAYT::value_type minth,
178  typename ARRAYT::value_type maxth,
179  typename ARRAYT::value_type minval,
180  typename ARRAYT::value_type maxval)
181  {
182 #ifdef NOFOREACH
183  for(auto iter=a.begin();iter!=a.end();iter++)
184  {
185  typename ARRAYT::value_type &v = *iter;
186 #else
187  for(typename ARRAYT::value_type &v: a)
188  {
189 #endif
190  if(v <= minth)
191  {
192  v = minval;
193  continue;
194  }
195 
196  if(v >= maxth)
197  {
198  v = maxval;
199  continue;
200  }
201  }
202  }
203 
204  //--------------------------------------------------------------------------
213  template<typename ARRAYT>
214  void min_clip(ARRAYT &a,typename ARRAYT::value_type threshold)
215  {
216 #ifdef NOFOREACH
217  for(auto iter=a.begin();iter!=a.end();iter++)
218  {
219  typename ARRAYT::value_type &v = *iter;
220 #else
221  for(typename ARRAYT::value_type &v: a)
222  {
223 #endif
224  if(v<=threshold) v = threshold;
225  }
226  }
227 
228  //--------------------------------------------------------------------------
238  template<typename ARRAYT>
239  void min_clip(ARRAYT &a,typename ARRAYT::value_type threshold,
240  typename ARRAYT::value_type value)
241  {
242 #ifdef NOFOREACH
243  for(auto iter=a.begin();iter!=a.end();iter++)
244  {
245  typename ARRAYT::value_type &v = *iter;
246 #else
247  for(typename ARRAYT::value_type &v: a)
248  {
249 #endif
250  if(v<=threshold) v = value;
251  }
252  }
253  //--------------------------------------------------------------------------
262  template<typename ARRAYT>
263  void max_clip(ARRAYT &a,typename ARRAYT::value_type threshold)
264  {
265 #ifdef NOFOREACH
266  for(auto iter=a.begin();iter!=a.end();iter++)
267  {
268  typename ARRAYT::value_type &v = *iter;
269 #else
270  for(typename ARRAYT::value_type &v: a)
271  {
272 #endif
273  if(v>=threshold) v = threshold;
274  }
275  }
276 
277  //--------------------------------------------------------------------------
287  template<typename ARRAYT>
288  void max_clip(ARRAYT &a,typename ARRAYT::value_type threshold,
289  typename ARRAYT::value_type value)
290  {
291 #ifdef NOFOREACH
292  for(auto iter=a.begin();iter!=a.end();iter++)
293  {
294  typename ARRAYT::value_type &v = *iter;
295 #else
296  for(typename ARRAYT::value_type &v: a)
297  {
298 #endif
299  if(v>=threshold) v = value;
300  }
301  }
302  //--------------------------------------------------------------------------
311  template<typename ARRAYT> size_t max_offset(const ARRAYT &a)
312  {
313  typedef typename ARRAYT::value_type value_type;
314  size_t offset = 0;
315  size_t index = 0;
316  value_type max_value = value_type(a[0]);
317 
318  index = 0;
319 #ifdef NOFOREACH
320  for(auto iter=a.begin();iter!=a.end();iter++)
321  {
322  auto v = *iter;
323 #else
324  for(auto v: a)
325  {
326 #endif
327  if(v > max_value)
328  {
329  max_value = v;
330  offset = index;
331  }
332  index++;
333  }
334  return offset;
335  }
336 
337  //--------------------------------------------------------------------------
346  template<typename CONT,typename ARRAYT> CONT max_index(const ARRAYT &a)
347  {
348  size_t offset = max_offset(a);
349  return a.map().template index<CONT>(offset);
350  }
351 
352  //--------------------------------------------------------------------------
361  template<typename ARRAYT> size_t min_offset(const ARRAYT &a)
362  {
363 
364  typedef typename ARRAYT::value_type value_type;
365  size_t offset = 0;
366  size_t index = 0;
367  value_type min_value = value_type(a[0]);
368 
369  index = 0;
370 #ifdef NOFOREACH
371  for(auto iter=a.begin();iter!=a.end();iter++)
372  {
373  auto v = *iter;
374 #else
375  for(auto v: a)
376  {
377 #endif
378  if(v < min_value)
379  {
380  min_value = v;
381  offset = index;
382  }
383  index++;
384  }
385  return offset;
386  }
387 
388  //--------------------------------------------------------------------------
397  template<typename CONT,typename ARRAYT> CONT min_index(const ARRAYT &a)
398  {
399  size_t offset = min_offset(a);
400  return a.map().template index<CONT>(offset);
401  }
402 
403 
404 //end of namespace
405 }
406 }
407 
Definition: add_op.hpp:29
size_t offset(const MAPT &map, const array_selection &s, const CTYPE &index)
compute offset
Definition: array_selection.hpp:510