source: XIOS/trunk/src/utils.hpp @ 775

Last change on this file since 775 was 775, checked in by mhnguyen, 9 years ago

Implementing the reading of attributes of an axis from a file

+) 3d grid can be read directly from a file
+) Clean some redundant codes
+) Add new attribute declaration that allows to output only desired attributes

Test
+) On Curie
+) test_remap passes and result is correct

File size: 9.2 KB
Line 
1/*!
2   \file utils.hpp
3   \author Ha NGUYEN
4   \since 06 Oct 2014
5   \date 10 Feb 2015
6
7
8   \brief Some utils for Xios
9 */
10
11#ifndef __XIOS_UTILS_HPP__
12#define __XIOS_UTILS_HPP__
13
14#include <vector>
15#include <limits>
16#include "array_new.hpp"
17#include "exception.hpp"
18
19namespace xios
20{
21  template<typename K>
22  struct CArrayTraits {
23    typedef K ArrayType;
24  };
25
26  template<typename K>
27  struct CArrayBoolTraits : public CArrayTraits<K> {
28    typedef bool Type;
29  };
30
31  template<>
32  struct CArrayBoolTraits<CArray<bool,1> >
33  {
34    static inline void resizeArray(CArray<bool,1>& boolArray, const std::vector<int>& dimensionSize)
35    {
36      if (1 != dimensionSize.size())
37        ERROR("utils::CArrayBoolTraits",
38                <<"Dimension of resized array mismatch"<<endl
39                <<"Dimension of resized is 1 "<<endl
40                <<"Dimension of vetor resizing is "<< dimensionSize.size());
41      boolArray.resize(dimensionSize[0]);
42    }
43  };
44
45  template<>
46  struct CArrayBoolTraits<CArray<bool,2> >
47  {
48    static inline void resizeArray(CArray<bool,2>& boolArray, const std::vector<int>& dimensionSize)
49    {
50      if (2 != dimensionSize.size())
51        ERROR("utils::CArrayBoolTraits",
52                <<"Dimension of resized array mismatch"<<endl
53                <<"Dimension of resized is 2 "<<endl
54                <<"Dimension of vetor resizing is "<< dimensionSize.size());
55      boolArray.resize(dimensionSize[0], dimensionSize[1]);
56    }
57  };
58
59  template<>
60  struct CArrayBoolTraits<CArray<bool,3> >
61  {
62    static inline void resizeArray(CArray<bool,3>& boolArray, const std::vector<int>& dimensionSize)
63    {
64      if (3 != dimensionSize.size())
65        ERROR("utils::CArrayBoolTraits",
66                <<"Dimension of resized array mismatch"<<endl
67                <<"Dimension of resized is 3 "<<endl
68                <<"Dimension of vetor resizing is "<< dimensionSize.size());
69      boolArray.resize(dimensionSize[0], dimensionSize[1], dimensionSize[2]);
70    }
71  };
72
73  template<>
74  struct CArrayBoolTraits<CArray<bool,4> >
75  {
76    static inline void resizeArray(CArray<bool,4>& boolArray, const std::vector<int>& dimensionSize)
77    {
78      if (4 != dimensionSize.size())
79        ERROR("utils::CArrayBoolTraits",
80                <<"Dimension of resized array mismatch"<<endl
81                <<"Dimension of resized is 4 "<<endl
82                <<"Dimension of vetor resizing is "<< dimensionSize.size());
83      boolArray.resize(dimensionSize[0], dimensionSize[1], dimensionSize[2], dimensionSize[3]);
84    }
85  };
86
87  template<>
88  struct CArrayBoolTraits<CArray<bool,5> >
89  {
90    static inline void resizeArray(CArray<bool,5>& boolArray, const std::vector<int>& dimensionSize)
91    {
92      if (5 != dimensionSize.size())
93        ERROR("utils::CArrayBoolTraits",
94                <<"Dimension of resized array mismatch"<<endl
95                <<"Dimension of resized is 5 "<<endl
96                <<"Dimension of vetor resizing is "<< dimensionSize.size());
97      boolArray.resize(dimensionSize[0], dimensionSize[1],
98                       dimensionSize[2], dimensionSize[3], dimensionSize[4]);
99    }
100  };
101
102  template<>
103  struct CArrayBoolTraits<CArray<bool,6> >
104  {
105    static inline void resizeArray(CArray<bool,6>& boolArray, const std::vector<int>& dimensionSize)
106    {
107      if (6 != dimensionSize.size())
108        ERROR("utils::CArrayBoolTraits",
109                <<"Dimension of resized array mismatch"<<endl
110                <<"Dimension of resized is 6 "<<endl
111                <<"Dimension of vetor resizing is "<< dimensionSize.size());
112      boolArray.resize(dimensionSize[0], dimensionSize[1],
113                       dimensionSize[2], dimensionSize[3],
114                       dimensionSize[4], dimensionSize[5]);
115    }
116  };
117
118  template<>
119  struct CArrayBoolTraits<CArray<bool,7> >
120  {
121    static inline void resizeArray(CArray<bool,7>& boolArray, const std::vector<int>& dimensionSize)
122    {
123      if (7 != dimensionSize.size())
124        ERROR("utils::CArrayBoolTraits",
125                <<"Dimension of resized array mismatch"<<endl
126                <<"Dimension of resized is 7 "<<endl
127                <<"Dimension of vetor resizing is "<< dimensionSize.size());
128      boolArray.resize(dimensionSize[0], dimensionSize[1],
129                       dimensionSize[2], dimensionSize[3],
130                       dimensionSize[4], dimensionSize[5], dimensionSize[6]);
131    }
132  };
133
134  template <int v>
135  struct Int2Type
136  {
137  enum { value = v };
138  };
139
140  template<typename T>
141  union TypeToBytes {
142    T value;
143    unsigned char bytes[sizeof(value)];
144  };
145
146  template<typename T>
147  struct HashAlgorithm
148  {
149    /*!
150      Adapted version of one-at-a-time hash by Bob Jenkins,
151      which is an expanded version of his Dr. Dobbs article.
152    */
153    static inline size_t jenkins_hash(const T& value)
154    {
155      TypeToBytes<T> u;
156      (u.value) = value;
157
158      size_t hash = 0;
159      size_t length = sizeof(value);
160
161      for (size_t i = 0; i < length; ++i)
162      {
163          hash += u.bytes[i];
164          hash += (hash << 10);
165          hash ^= (hash >> 6);
166      }
167      hash += (hash << 3);
168      hash ^= (hash >> 11);
169      hash += (hash << 15);
170
171      return hash;
172    }
173
174    /*!
175      Adatped version of (stupid) boost hash (but working)
176    */
177    static inline size_t boost_hash(const std::vector<T>& vec)
178    {
179      size_t hash = 0;
180      int sizeVec = vec.size();
181      for(int i = 0; i < sizeVec; ++i)
182      {
183        hash ^= i + 0x9e3779b9 + (hash << 6) + (hash >> 2);
184      }
185      return hash;
186    }
187  };
188
189  template<typename T, typename Algo = Int2Type<0> >
190  struct HashXIOS
191  {
192    std::size_t operator()(const T& val)
193    {
194      Algo al;
195      return hash_value(val, al);
196    }
197
198    std::size_t hashVec(const std::vector<T>& vec)
199    {
200      return HashAlgorithm<T>::boost_hash(vec);
201    }
202
203  private:
204    size_t hash_value(const T& val, Int2Type<0>)
205    {
206      return HashAlgorithm<T>::jenkins_hash(val);
207    }
208  };
209
210template<typename K>
211struct NumTraits {
212    typedef K Type;
213};
214
215template<>
216struct NumTraits<unsigned long>
217{
218  typedef unsigned long Scalar;
219  typedef Scalar magnitudeType;
220
221  static inline Scalar sfmin() {
222    return std::numeric_limits<Scalar>::min();
223  }
224  static inline Scalar sfmax() {
225    return std::numeric_limits<Scalar>::max();
226  }
227  static inline Scalar sflowest() {
228    return -(std::numeric_limits<Scalar>::max());
229  }
230  static inline Scalar epsilon() {
231    return std::numeric_limits<Scalar>::epsilon();
232  }
233};
234
235template<>
236struct NumTraits<double>
237{
238  typedef double Scalar;
239  typedef Scalar magnitudeType;
240
241  static inline Scalar sfmin() {
242    return std::numeric_limits<Scalar>::min();
243  }
244  static inline Scalar sfmax() {
245    return std::numeric_limits<Scalar>::max();
246  }
247  static inline Scalar sflowest() {
248    return -(std::numeric_limits<Scalar>::max());
249  }
250  static inline Scalar epsilon() {
251    return std::numeric_limits<Scalar>::epsilon();
252  }
253};
254
255template<typename T>
256class CArrayStorage
257{
258public:
259  typedef CArray<T,1> StorageType;
260
261public:
262  CArrayStorage(const CArray<T,1>& arr) : values(arr) {}
263
264protected:
265  const T& atIndex(int idx) const { return values(idx); }
266  const StorageType& values;
267};
268
269template<typename T>
270class CVectorStorage
271{
272public:
273  typedef std::vector<T> StorageType;
274
275public:
276  CVectorStorage(const std::vector<T>& vec) : values(vec) {}
277
278protected:
279  const T& atIndex(int idx) const { return values[idx]; }
280  const StorageType& values;
281};
282
283
284template<
285  typename T,
286  template <class> class StoragePolicy = CVectorStorage
287  >
288class XIOSComparatorWithIndex :
289  public StoragePolicy<T>
290{
291public:
292  typedef typename  StoragePolicy<T>::StorageType StorageType;
293
294public:
295  XIOSComparatorWithIndex(const StorageType& v) : StoragePolicy<T>(v) {}
296  bool operator()(int a, int b) { return this->atIndex(a) < this->atIndex(b); }
297};
298
299template<
300  typename T,
301  template <class> class StoragePolicy = CVectorStorage
302  >
303class XIOSLowerBoundWithIndex :
304  public StoragePolicy<T>
305{
306public:
307  typedef typename  StoragePolicy<T>::StorageType StorageType;
308
309public:
310  XIOSLowerBoundWithIndex(const StorageType &v) : StoragePolicy<T>(v) {}
311  bool operator()(const int a, const T& b) { return this->atIndex(a) < b; }
312};
313
314template<
315  typename T,
316  template <class> class StoragePolicy = CVectorStorage
317  >
318class XIOSBinarySearchWithIndex :
319  public StoragePolicy<T>
320{
321public:
322  typedef typename  StoragePolicy<T>::StorageType StorageType;
323
324public:
325  XIOSBinarySearchWithIndex(const StorageType& v) : StoragePolicy<T>(v) {}
326
327  template<typename ForwardIterator>
328  bool search(ForwardIterator first, ForwardIterator last, const T& val, ForwardIterator& position)
329  {
330    first = std::lower_bound(first, last, val, XIOSLowerBoundWithIndex<T, StoragePolicy>(this->values));
331    position = first;
332    return (first!=last && !(val<this->atIndex(*first)));
333  }
334};
335
336
337struct XIOSAlgorithms
338{
339public:
340  template<typename T, template <class> class StoragePolicy>
341  static void sortWithIndex(const typename StoragePolicy<T>::StorageType& values, std::vector<int>& rv)
342  {
343    std::sort(rv.begin(), rv.end(), XIOSComparatorWithIndex<T, StoragePolicy>(values));
344  }
345
346  //! Fill in an vector with index begin at 0
347  static void fillInIndex(int nbIndex, std::vector<int>& rvIndex)
348  {
349    if ((0 < nbIndex) && (nbIndex != rvIndex.size())) rvIndex.resize(nbIndex);
350    for (int idx = 0; idx < nbIndex; ++idx) rvIndex[idx] = idx;
351  }
352};
353
354}
355
356#endif // __UTILS_HPP__
Note: See TracBrowser for help on using the repository browser.