Changeset 914


Ignore:
Timestamp:
Sep 29, 2007, 4:18:52 AM (14 years ago)
Author:
Peter
Message:

refactoring add function in Averagers, refs #246

Location:
trunk/yat
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/yat/statistics/Averager.h

    r911 r914  
    200200  void add(Averager& a, Iter first, Iter last)
    201201  {
    202     add(a, first, last,
    203         typename utility::weighted_iterator_traits<Iter>::type());
    204   }
    205 
    206   // unweighted impl. (weighted version is not implemented and should
    207   // not compile)
    208   template <typename Iter>
    209   void add(Averager& a, Iter first, Iter last,
    210            utility::unweighted_type type)
    211   {
     202    utility::check_iterator_is_unweighted(first);
    212203    for ( ; first != last; ++first)
    213204      a.add(*first);
    214205  }
    215 
    216206
    217207  // Template implementations
  • trunk/yat/statistics/AveragerPair.h

    r913 r914  
    177177  void add(AveragerPair& ap, Iter1 first1, Iter1 last1, Iter2 first2)
    178178  {
    179     add(ap, first1, last1, first2,
    180         typename utility::weighted_iterator_traits<Iter1>::type(),
    181         typename utility::weighted_iterator_traits<Iter2>::type());
    182   }
    183 
    184   // unweighted impl. (weighted version is not implemented and should
    185   // not compile)
    186   template <typename Iter1, typename Iter2>
    187   void add(AveragerPair& ap, Iter1 first1, Iter1 last1, Iter2 first2,
    188            utility::unweighted_type type1, utility::unweighted_type type2)
    189   {
     179    utility::check_iterator_is_unweighted(first1);
     180    utility::check_iterator_is_unweighted(first2);
    190181    for ( ; first1 != last1; ++first1, ++first2)
    191182      ap.add(*first1, *first2);
  • trunk/yat/statistics/AveragerPairWeighted.h

    r899 r914  
    2828
    2929#include "AveragerWeighted.h"
     30
     31#include "yat/utility/IteratorTraits.h"
    3032
    3133#include <cmath>
     
    176178
    177179  /**
    178      \brief adding a ranges of values to AveragerPairWeighted \a ap
    179   */
     180     \brief adding a ranges of values to AveragerPair \a ap
     181   */
    180182  template <class Iter1, class Iter2>
    181183  void add(AveragerPairWeighted& ap, Iter1 first1, Iter1 last1, Iter2 first2)
    182184  {
    183185    for ( ; first1 != last1; ++first1, ++first2)
    184       ap.add(first1.data(), first2.data(),first1.weight(),first2.weight());
     186      ap.add(utility::iterator_traits_data(first1),
     187             utility::iterator_traits_data(first2),
     188             utility::iterator_traits_weight(first1),
     189             utility::iterator_traits_weight(first2));
    185190  }
    186 
    187 
    188191
    189192
  • trunk/yat/statistics/AveragerWeighted.h

    r910 r914  
    226226  void add(AveragerWeighted& a, Iter first, Iter last)
    227227  {
    228     add(a, first, last,
    229         typename utility::weighted_iterator_traits<Iter>::type());
     228    for ( ; first != last; ++first)
     229      a.add(utility::iterator_traits_data(first));
    230230  }
    231 
    232 
    233   // unweighted impl.
    234   template <typename Iter>
    235   void add(AveragerWeighted& a, Iter first, Iter last,
    236            utility::unweighted_type type)
    237   {
    238     for ( ; first != last; ++first)
    239       a.add(*first);
    240   }
    241 
    242   // weighted impl.
    243   template <typename Iter>
    244   void add(AveragerWeighted& a, Iter first, Iter last,
    245            utility::weighted_type type)
    246   {
    247     for ( ; first != last; ++first)
    248       a.add(first.data(), first.weight());
    249   }
    250 
    251231
    252232  // Template implementations
  • trunk/yat/utility/IteratorTraits.h

    r910 r914  
    4848    typedef weighted_type type;
    4949  };
    50    
    5150
    5251  // All iterators should default to unweighted type ...
     
    6261  };
    6362
     63  // check (at compile time) that iterator is unweighted.
     64  inline void check_iterator_is_unweighted(unweighted_type x){}
     65  template <class Iter>
     66  void check_iterator_is_unweighted(Iter iter)
     67  {check_iterator_unweighted(typename weighted_iterator_traits<Iter>::type());}
     68
     69
     70  /*
     71    Function to be used to make compile-time decision how to return
     72    data from an iterator separating two cases: weighted and
     73    unweighted.
     74
     75    In this way unweighted iterators can be used in a weighted context.
     76   */
     77  // Peter, perhaps these should be templatized, but for now return
     78  // type double is hard coded.
     79  template <class Iter>
     80  double iterator_traits_data(Iter iter)
     81  { return iterator_traits_data(iter, weighted_iterator_traits<Iter>::type()); }
     82
     83  template <class Iter>
     84  double iterator_traits_data(Iter i, weighted_type)
     85  { return i.data(); }
     86
     87  template <class Iter>
     88  double iterator_traits_data(Iter i, unweighted_type)
     89  { return *i; }
     90
     91  /*
     92    Function to be used to make compile-time decision how to return
     93    data from an iterator separating two cases: weighted and
     94    unweighted.
     95
     96    In this way unweighted iterators can be used in a weighted context
     97    and weight will be set to unity.
     98   */
     99  // Peter, perhaps these should be templatized, but for now return
     100  // type double is hard coded.
     101  template <class Iter>
     102  double iterator_traits_weight(Iter iter)
     103  { return iterator_traits_data(iter, weighted_iterator_traits<Iter>::type()); }
     104
     105  template <class Iter>
     106  double iterator_traits_weight(Iter i, weighted_type)
     107  { return i.weight(); }
     108
     109  template <class Iter>
     110  double iterator_traits_weight(Iter i, unweighted_type)
     111  { return 1.0; }
     112
     113
    64114}}} // of namespace utility, yat, and theplu
    65115
Note: See TracChangeset for help on using the changeset viewer.