Changeset 1399


Ignore:
Timestamp:
Aug 7, 2008, 12:52:39 AM (15 years ago)
Author:
Peter
Message:

refs #369 added a function to create a boost::transform_iterator that was mentioned in ticket:369 - should probably add the corresponding for pair::second too.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/test/iterator_test.cc

    r1386 r1399  
    3232#include "yat/utility/Matrix.h"
    3333#include "yat/utility/MatrixWeighted.h"
     34#include "yat/utility/stl_utility.h"
    3435#include "yat/utility/Vector.h"
    3536#include "yat/utility/WeightIterator.h"
    3637
     38#include <boost/iterator/transform_iterator.hpp>
     39
    3740#include <algorithm>
    3841#include <fstream>
    39 
     42#include <string>
    4043#include <vector>
    4144
    4245using namespace theplu::yat;
     46
     47void old_main(test::Suite&);
     48void test_boost_util(test::Suite&);
    4349
    4450int main(int argc, char* argv[])
     
    4753  suite.err() << "testing iterator" << std::endl;
    4854
     55  old_main(suite);
     56  test_boost_util(suite);
     57  return suite.return_value();
     58}
     59 
     60
     61void old_main(test::Suite& suite)
     62{
    4963  suite.err() << "testing utility::vector::iterator" << std::endl;
    5064  utility::Vector vec(12);
     
    142156    suite.add(false);
    143157
    144   return suite.return_value();
    145158}
     159
     160void test_boost_util(test::Suite& suite)
     161{
     162  bool ok_cached=suite.ok();
     163  utility::PairFirst<std::pair<std::string, int> > pf;
     164  std::pair<std::string, int> p("July", 31);
     165  std::string str=pf(p);
     166  suite.add(str=="July");
     167  pf(p)="juli";
     168  suite.add(pf(p)=="juli");
     169
     170  typedef utility::PairFirst<std::pair<const std::string, int> > PF2;
     171  PF2 pf2;
     172  std::pair<const std::string, int> p2("July", 31);
     173  suite.add(pf2(p2)=="July");
     174
     175  utility::PairFirst<const std::pair<std::string, int> > pf3;
     176  std::pair<std::string, int> p3("July", 31);
     177  suite.add(pf3(p3)=="July");
     178
     179  utility::PairFirst<std::pair<std::string, const int> > pf4;
     180  std::pair<std::string, const int> p4("July", 31);
     181  suite.add(pf4(p4)=="July");
     182  pf4(p4)="juli";
     183  suite.add(pf4(p4)=="juli");
     184
     185  utility::PairFirst<std::pair<const std::string, const int> > pf5;
     186  std::pair<const std::string, const int> p5("July", 31);
     187  suite.add(pf5(p5)=="July");
     188
     189  typedef std::map<std::string, int> Map;
     190  Map m;
     191  m["July"]=31;
     192  m["September"]=30;
     193  m["June"]=30;
     194  m["April"]=30;
     195
     196  boost::transform_iterator<PF2, Map::iterator> first_iterator(m.begin(), pf2);
     197  boost::transform_iterator<PF2, Map::iterator> first_iterator_end(m.end(), pf2);
     198  std::vector<std::string> vec(m.size());
     199  std::copy(first_iterator, first_iterator_end, vec.begin());
     200  if (!suite.add(vec[0]=="April"))
     201    suite.err() << "Error: vec[0] = " << vec[0] << " expected April\n";
     202  if (!suite.add(vec[1]=="July"))
     203    suite.err() << "Error: vec[1] = " << vec[1] << " expected July\n";
     204  if (!suite.add(vec[2]=="June"))
     205    suite.err() << "Error: vec[2] = " << vec[2] << " expected June\n";
     206  if (!suite.add(vec[3]=="September"))
     207    suite.err() << "Error: vec[3] = " << vec[3] << " expected September\n";
     208
     209  std::vector<std::string> vec2(m.size());
     210  std::copy(utility::pair_first_iterator(m.begin()),
     211            utility::pair_first_iterator(m.end()),
     212            vec2.begin());
     213  suite.add(vec2==vec);
     214 
     215
     216  if (ok_cached && !suite.ok())
     217    suite.err() << "test_bool_util failed" << std::endl;
     218}
  • trunk/yat/utility/stl_utility.h

    r1387 r1399  
    3535/// extensions to STL functionality.
    3636///
     37
     38#include <boost/iterator/transform_iterator.hpp>
     39#include <boost/mpl/if.hpp>
     40#include <boost/type_traits/is_const.hpp>
     41#include <boost/type_traits/add_const.hpp>
    3742
    3843#include <algorithm>
     
    266271  };
    267272
     273  /**
     274     \brief Functor that return std::pair.first
     275
     276     \see pair_first_iterator
     277
     278     \since New in yat 0.5
     279   */
     280  template <class Pair>
     281  struct PairFirst
     282  {
     283    /**
     284       The type returned is Pair::first_type& with the exception when
     285       Pair is const and Pair::first_type is non-const, in which case
     286       const Pair::first_type& is return type.
     287     */
     288    typedef typename boost::mpl::if_<
     289                  typename boost::is_const<Pair>::type,
     290                  typename boost::add_const<typename Pair::first_type>::type&,
     291                  typename Pair::first_type&>::type result_type;
     292   
     293    /**
     294       The argument type is Pair&.
     295     */
     296    typedef Pair& argument_type;
     297
     298    /**
     299       \return p.first
     300     */
     301    inline result_type operator()(argument_type p) const
     302    { return p.first; }
     303
     304  };
     305
     306
     307  /**
     308     Creates a transform_iterator that transforms an iterator with
     309     value type std::pair to an iterator with value type
     310     std::pair::first_type. This can be used, for example, to
     311     communicate between a std::map and std::vector
     312
     313     \code
     314     std::map<std::string, int> map;
     315     ...
     316     std::vector<std::string> vec;
     317     vec.resize(map.size());
     318     std::copy(pair_first_iterator(map.begin()), pair_first_iterator(map.end()),
     319               vec.begin());
     320     \endcode
     321
     322     \since New in yat 0.5
     323   */
     324  template<class Iter>
     325  boost::transform_iterator<
     326    PairFirst<typename std::iterator_traits<Iter>::value_type>,
     327    Iter> pair_first_iterator(Iter i)
     328  {
     329    typedef PairFirst<typename std::iterator_traits<Iter>::value_type> PF;
     330    return boost::transform_iterator<PF, Iter>(i, PF());
     331  }
     332
     333
     334
     335
    268336  ///
    269337  /// @brief Function converting a string to lower case
Note: See TracChangeset for help on using the changeset viewer.