Changeset 3287


Ignore:
Timestamp:
Jul 12, 2014, 4:07:13 PM (9 years ago)
Author:
Peter
Message:

new function sum_weight

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/test/utility.cc

    r3285 r3287  
    7171
    7272void test_sort_index(test::Suite& suite);
     73void test_sum_weight(test::Suite& suite);
    7374void test_less_nan(test::Suite& suite);
    7475void test_ptr_compare(test::Suite& suite);
     
    201202  test_inverse(suite);
    202203  test_sort_index(suite);
     204  test_sum_weight(suite);
    203205  test_less_nan(suite);
    204206  test_errno_error(suite);
     
    579581}
    580582
     583
     584void test_sum_weight(test::Suite& suite)
     585{
     586  // do not run compile test
     587  if (false) {
     588    boost::input_iterator_archetype<double> iter;
     589    utility::sum_weight(iter, iter);
     590    boost::input_iterator_archetype_no_proxy<utility::DataWeight> iter2;
     591    utility::sum_weight(iter2, iter2);
     592  }
     593}
     594
     595
    581596void test_ptr_compare(test::Suite& suite)
    582597{
  • trunk/yat/utility/utility.h

    r3188 r3287  
    3232///
    3333
     34#include "concept_check.h"
    3435#include "deprecate.h"
    3536#include "Exception.h"
     37#include "iterator_traits.h"
     38#include "WeightIterator.h"
    3639#include "yat_assert.h"
    3740
    3841#include <gsl/gsl_math.h>
     42
     43#include <boost/iterator/iterator_concepts.hpp>
     44#include <boost/concept_check.hpp>
    3945
    4046#include <algorithm>
     
    4551#include <locale>
    4652#include <istream>
     53#include <numeric>
    4754#include <string>
    4855#include <stdexcept>
     
    274281  T log2(T x) { return std::log(x)/M_LN2; }
    275282
     283  /**
     284     \brief create a directory \a dir
     285
     286     \see http://linux.die.net/man/3/mkdir
     287
     288     \throw runtime_error if creation failed
     289
     290     \since New in yat 0.10
     291   */
     292  void mkdir(const std::string& dir, mode_t mode=0777);
     293
     294  /**
     295     Similar to mkdir(const std::string&, mode_t).
     296
     297     No error if \a dir already exist. Make parent directories as needed.
     298
     299     \since New in yat 0.10
     300   */
     301  void mkdir_p(const std::string& dir, mode_t mode=0777);
     302
     303  /**
     304     same as C function remove but throws errno_error at failure
     305
     306     \see http://linux.die.net/man/3/remove
     307
     308     \since New in yat 0.10
     309   */
     310  void remove(const std::string& fn);
     311
     312  /**
     313     same as C function with same name but throws errno_error if error
     314     is encountered
     315
     316     \see http://linux.die.net/man/3/rename
     317
     318     \since New in yat 0.10
     319   */
     320  void rename(const std::string& from, const std::string to);
     321
     322  /**
     323     In \a full_str replace every sub-string \a old_str with \a
     324     new_str;
     325
     326     \since New in yat 0.10
     327   */
     328  void replace(std::string& full_str, std::string old_str, std::string new_str);
     329
     330
     331  /**
     332     Calculate sum of weights in range [first, last). The complexity
     333     is linear except in the important case when \c Iterator is
     334     unweighted and \random_access_iterator when complexity is constant.
     335
     336     Type Requirments:
     337     - \c Iterator is \ref concept_data_iterator
     338     - \c Iterator is \readable_iterator
     339     - \c Iterator is \single_pass_iterator
     340   */
     341  template<typename Iterator>
     342  double sum_weight(Iterator first, Iterator last);
     343
    276344/// \cond IGNORE_DOXYGEN
    277345
     
    305373     */
    306374    void operator()(const std::string& element, std::vector<T>& vec)
    307     { 
     375    {
    308376      if (!element.size())
    309377        vec.push_back(std::numeric_limits<T>::quiet_NaN());
     
    331399  };
    332400
     401
     402  template<typename Iterator>
     403  double sum_weight(Iterator first, Iterator last, unweighted_iterator_tag tag)
     404  {
     405    return std::distance(first, last);
     406  }
     407
     408
     409  template<typename Iterator>
     410  double sum_weight(Iterator first, Iterator last, weighted_iterator_tag tag)
     411  {
     412    return std::accumulate(weight_iterator(first), weight_iterator(last), 0);
     413  }
     414
     415
    333416} // end of namespace detail
    334417
    335418/// \endcond
    336419
    337 
    338   /**
    339      \brief create a directory \a dir
    340 
    341      \see http://linux.die.net/man/3/mkdir
    342 
    343      \throw runtime_error if creation failed
    344 
    345      \since New in yat 0.10
    346    */
    347   void mkdir(const std::string& dir, mode_t mode=0777);
    348 
    349   /**
    350      Similar to mkdir(const std::string&, mode_t).
    351 
    352      No error if \a dir already exist. Make parent directories as needed.
    353 
    354      \since New in yat 0.10
    355    */
    356   void mkdir_p(const std::string& dir, mode_t mode=0777);
    357 
    358   /**
    359      same as C function remove but throws errno_error at failure
    360 
    361      \see http://linux.die.net/man/3/remove
    362 
    363      \since New in yat 0.10
    364    */
    365   void remove(const std::string& fn);
    366 
    367   /**
    368      same as C function with same name but throws errno_error if error
    369      is encountered
    370 
    371      \see http://linux.die.net/man/3/rename
    372 
    373      \since New in yat 0.10
    374    */
    375   void rename(const std::string& from, const std::string to);
    376 
    377   /**
    378      In \a full_str replace every sub-string \a old_str with \a
    379      new_str;
    380 
    381      \since New in yat 0.10
    382    */
    383   void replace(std::string& full_str, std::string old_str, std::string new_str);
    384420
    385421  // template implementations
     
    404440
    405441
    406   // template implementations
    407442  template<typename T>
    408443  std::string convert(T input)
     
    434469
    435470  template<typename T>
    436   void load(std::istream& is, std::vector<std::vector<T> >& matrix, 
    437             char sep, char line_sep, bool ignore_empty, 
     471  void load(std::istream& is, std::vector<std::vector<T> >& matrix,
     472            char sep, char line_sep, bool ignore_empty,
    438473            bool rectangle)
    439474  {
     
    450485      // add NaN for final separator (or empty string if T=std::string)
    451486      detail::VectorPusher<T> pusher;
    452       if(sep!='\0' && !line.empty() && line[line.size()-1]==sep) 
     487      if(sep!='\0' && !line.empty() && line[line.size()-1]==sep)
    453488        pusher("", v);
    454      
     489
    455490      if (rectangle && nof_columns && v.size()!=nof_columns) {
    456491        std::ostringstream s;
     
    459494          << " columns; expected " << nof_columns << " columns.";
    460495        throw utility::IO_error(s.str());
    461       }       
     496      }
    462497      nof_columns = std::max(nof_columns, v.size());
    463498    }
     
    483518    }
    484519  }
     520
     521
     522  template<typename Iterator>
     523  double sum_weight(Iterator first, Iterator last)
     524  {
     525    BOOST_CONCEPT_ASSERT((DataIteratorConcept<Iterator>));
     526    BOOST_CONCEPT_ASSERT((boost_concepts::ReadableIterator<Iterator>));
     527    BOOST_CONCEPT_ASSERT((boost_concepts::SinglePassIterator<Iterator>));
     528    typename weighted_iterator_traits<Iterator>::type tag;
     529    return detail::sum_weight(first, last, tag);
     530  }
     531
    485532
    486533/// \cond IGNORE_DOXYGEN
Note: See TracChangeset for help on using the changeset viewer.