source: XIOS/dev/dev_rv/src4/xmlio/array_util_impl.hpp @ 2338

Last change on this file since 2338 was 258, checked in by hozdoba, 13 years ago
File size: 4.3 KB
Line 
1/* ************************************************************************** *
2 *      Copyright © IPSL/LSCE, XMLIOServer, Avril 2010 - Octobre 2011         *
3 * ************************************************************************** */
4
5#ifndef __ARRAY_UTIL_IMPL_HPP__
6#define __ARRAY_UTIL_IMPL_HPP__
7
8/**
9 * \file    array_util_impl.hpp
10 * \brief   Définition de quelques méthodes pratiques pour travailler avec les tableaux (implémentation).
11 * \author  Hervé Ozdoba
12 * \version 0.4
13 * \date    12 Juillet 2011
14 */
15
16 #ifndef __XIOS_NO_EXTERN
17 
18// standard Headers
19#include <algorithm>
20
21#endif // __XIOS_NO_EXTERN
22
23// ////////////////////////////// Déclarations ////////////////////////////// //
24
25namespace xmlioserver
26{
27   // ---------------------- Conversion de tableau typé ------------------------
28   
29   // Convertit un tableau de données d'un certain type vers un autre.
30   template <typename ODataType, typename IDataType, std::size_t Size>
31      void CArrayUtil::Convert(const boost::multi_array<IDataType, Size> & _array_in,
32                                     boost::multi_array<ODataType, Size> & _array_out)
33   {
34      _array_out.assign(_array_in.data(), _array_in.data()+_array_in.num_elements());
35   }
36   
37   // --------------------- Opérations sur les tableaux  -----------------------
38   
39   // Applique une opération binaire à des tableaux (tableau et tableau).             
40   template <typename DataType, std::size_t Size>
41      void CArrayUtil::Apply1(const boost::multi_array<DataType, Size> & _input_array1,
42                              const boost::multi_array<DataType, Size> & _input_array2,
43                                    boost::multi_array<DataType, Size> & _output_array,
44                              DataType (*_function) (DataType, DataType))
45   {     
46      const DataType * it1 = _input_array1.data(), * it2 = _input_array2.data(), 
47                     * end = _input_array1.data() + _input_array1.num_elements();
48            DataType * it = _output_array.data();
49     
50      for (; it1 != end; it1++, it2++, it++) *it = (*_function)(*it1, *it2);
51   }
52   
53   // Applique une opération binaire à un tableau (tableau et scalaire).
54   template <typename DataType, typename ScalDataType, std::size_t Size>
55      void CArrayUtil::Apply2(const boost::multi_array<DataType, Size> & _input_array,
56                                    boost::multi_array<DataType, Size> & _output_array,
57                                    ScalDataType _scalar,
58                              DataType (*_function) (DataType, ScalDataType))
59   {
60      const DataType * it1 = _input_array.data(),
61                     * end = _input_array.data() + _input_array.num_elements();
62            DataType * it = _output_array.data();
63      for (; it1 != end; it1++, it++) *it = (*_function)(*it1, _scalar);
64   }
65   
66   // --------------------------- Autres méthodes ------------------------------
67   
68   // Associe au tableau de sortie le même profil que le tableau d'entrée.
69   template <typename ODataType, typename IDataType, std::size_t Size>
70      void CArrayUtil::SameShape(const boost::multi_array<IDataType, Size> & _array_in,
71                                       boost::multi_array<ODataType, Size> & _array_out)
72   {
73      const std::size_t * _shape = _array_in.shape();
74      std::vector<std::size_t> shape(_shape, _shape+Size);
75      _array_out.resize(shape);
76   }
77   
78   // ----------------------- Opération élémentaires ---------------------------
79
80   // Somme de _value_1 et _value_2
81   template <typename DataType, typename ScalDataType>
82      DataType AUSum (DataType _value_1, ScalDataType _value_2)
83   { return (_value_1+_value_2); }
84   
85   // Division de _value_1 et _value_2
86   template <typename DataType, typename ScalDataType>
87      DataType AUDiv (DataType _value_1, ScalDataType _value_2)
88   { return (_value_1/_value_2); }
89   
90   // Maximum de _value_1 et _value_2
91   template <typename DataType, typename ScalDataType>
92      DataType AUMax (DataType _value_1, ScalDataType _value_2)
93   { return (std::max(_value_1, _value_2)); }
94   
95    // Minimum de _value_1 et _value_2
96   template <typename DataType, typename ScalDataType>
97      DataType AUMin (DataType _value_1, ScalDataType _value_2)
98   { return (std::min(_value_1, _value_2)); }
99                                     
100} // namespace xmlioserver
101
102#endif // __ARRAY_UTIL_IMPL_HPP__
Note: See TracBrowser for help on using the repository browser.