Ignore:
Timestamp:
Apr 24, 2012, 3:07:47 AM (10 years ago)
Author:
Peter
Message:

docs: clarification and code example for Dereferencer

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/yat/utility/stl_utility.h

    r2530 r2730  
    11#ifndef _theplu_yat_utility_stl_utility_
    2 #define _theplu_yat_utility_stl_utility_ 
     2#define _theplu_yat_utility_stl_utility_
    33
    44// $Id$
     
    99  Copyright (C) 2006 Jari Häkkinen
    1010  Copyright (C) 2007, 2008 Jari Häkkinen, Peter Johansson
    11   Copyright (C) 2009, 2010, 2011 Peter Johansson
     11  Copyright (C) 2009, 2010, 2011, 2012 Peter Johansson
    1212
    1313  This file is part of the yat library, http://dev.thep.lu.se/yat
     
    6565
    6666  ///
    67   /// Print out a pair 
     67  /// Print out a pair
    6868  ///
    6969  // This is in namespace std because we have not figured out how to have
    7070  // pair and its operator<< in different namespaces
    71   template <class T1, class T2> 
    72   std::ostream& operator<<(std::ostream& out, const std::pair<T1,T2>& p) 
     71  template <class T1, class T2>
     72  std::ostream& operator<<(std::ostream& out, const std::pair<T1,T2>& p)
    7373  { out << p.first << "\t" << p.second; return out; }
    7474
     
    9797     \brief Adaptor between pointer and pointee interface
    9898
    99      Pointer must have an \c operator*, i.e., \c Pointer can be a
    100      traditional pointer or an \input_iterator. Return type is decided
    101      by <a href=http://www.sgi.com/tech/stl/iterator_traits.html>
    102      std::iterator_traits </a>.
     99     Functor takes a pointer and returns a reference to the instance
     100     pointer is pointing to. Return type is decided by <a
     101     href=http://www.sgi.com/tech/stl/iterator_traits.html>
     102     std::iterator_traits<Pointer>::reference </a>. Pointer must have
     103     an \c operator*, i.e., \c Pointer can be a traditional pointer or
     104     an \input_iterator.
     105
     106     The class is designed to be used with boost::transform_iterator
     107
     108     \code
     109     std::vector<MyClass*> vec;
     110     ...
     111     Dereferencer<MyClass*> dereferencer;
     112     std::sort(boost::make_transform_iterator(vec.begin(), dereferencer),
     113               boost::make_transform_iterator(vec.end(), dereferencer))
     114     \endcode
     115
     116     where elements in vec is sorted using MyClass::operator<
    103117
    104118     \since New in yat 0.7
    105119   */
    106120  template<typename Pointer>
    107   struct Dereferencer : 
    108     public std::unary_function<Pointer, 
     121  struct Dereferencer :
     122    public std::unary_function<Pointer,
    109123                               typename std::iterator_traits<Pointer>::reference>
    110124  {
     
    112126       \brief constructor
    113127     */
    114     Dereferencer(void) 
     128    Dereferencer(void)
    115129    { BOOST_CONCEPT_ASSERT((TrivialIterator<Pointer>)); }
    116130
     
    137151     - G must be an <a
    138152     href="http://www.sgi.com/tech/stl/AdaptableUnaryFunction.html">
    139      AdaptableUnaryFunction</a> 
     153     AdaptableUnaryFunction</a>
    140154     - H must be an <a
    141155     href="http://www.sgi.com/tech/stl/AdaptableUnaryFunction.html">
    142      AdaptableUnaryFunction</a> 
     156     AdaptableUnaryFunction</a>
    143157     - \c G::result_type is convertible to \c F::first_argument_type
    144158     - \c H::result_type is convertible to \c F::second_argument_type
     
    147161   */
    148162  template<class F, class G, class H>
    149   class compose_f_gx_hy : 
     163  class compose_f_gx_hy :
    150164    public std::binary_function<typename G::argument_type,
    151165                                typename H::argument_type,
     
    157171     */
    158172    compose_f_gx_hy(F f, G g, H h)
    159       : f_(f), g_(g), h_(h) 
     173      : f_(f), g_(g), h_(h)
    160174    {
    161175      BOOST_CONCEPT_ASSERT((boost::Convertible<typename G::result_type
     
    170184     */
    171185    typename F::result_type
    172     operator()(typename G::argument_type x, 
     186    operator()(typename G::argument_type x,
    173187               typename H::argument_type y) const
    174188    {
     
    193207  {
    194208    return compose_f_gx_hy<F,G,H>(f,g,h);
    195   } 
     209  }
    196210
    197211
     
    210224     - G must be an <a
    211225     href="http://www.sgi.com/tech/stl/AdaptableBinaryFunction.html">
    212      AdaptableBinaryFunction</a> 
     226     AdaptableBinaryFunction</a>
    213227     - \c G::result_type is convertible to \c F::argument_type
    214228
     
    218232   */
    219233  template<class F, class G>
    220   class compose_f_gxy : 
     234  class compose_f_gxy :
    221235    public std::binary_function<typename G::first_argument_type,
    222236                                typename G::second_argument_type,
     
    228242     */
    229243    compose_f_gxy(F f, G g)
    230       : f_(f), g_(g) 
     244      : f_(f), g_(g)
    231245    {
    232246      BOOST_CONCEPT_ASSERT((boost::Convertible<typename G::result_type
     
    238252     */
    239253    typename F::result_type
    240     operator()(typename G::first_argument_type x, 
     254    operator()(typename G::first_argument_type x,
    241255               typename G::second_argument_type y) const
    242256    {
     
    262276  {
    263277    return compose_f_gxy<F,G>(f,g);
    264   } 
     278  }
    265279
    266280
     
    278292     - G must be an <a
    279293     href="http://www.sgi.com/tech/stl/AdaptableUnaryFunction.html">
    280      AdaptableUnaryFunction</a> 
     294     AdaptableUnaryFunction</a>
    281295     - \c G::result_type is convertible to \c F::argument_type
    282296
     
    294308     */
    295309    compose_f_gx(F f, G g)
    296       : f_(f), g_(g) 
     310      : f_(f), g_(g)
    297311    {
    298312      BOOST_CONCEPT_ASSERT((boost::Convertible<typename G::result_type
     
    327341  {
    328342    return compose_f_gx<F,G>(f,g);
    329   } 
     343  }
    330344
    331345
     
    405419   */
    406420  template<typename Key, typename InputIterator, typename Comp>
    407   void inverse(InputIterator first, InputIterator last, 
     421  void inverse(InputIterator first, InputIterator last,
    408422               std::multimap<Key, size_t, Comp>& m)
    409423  {
     
    445459     */
    446460    inline bool operator()(T x, T y) const
    447     { 
     461    {
    448462      if (std::isnan(x))
    449463        return false;
     
    459473   */
    460474  template<>
    461   struct less_nan<DataWeight> 
     475  struct less_nan<DataWeight>
    462476    : std::binary_function<DataWeight, DataWeight, bool>
    463477  {
     
    466480     */
    467481    inline bool operator()(const DataWeight& x, const DataWeight& y) const
    468     { 
     482    {
    469483      less_nan<double> compare;
    470484      return compare(x.data(), y.data());
     
    562576                           const std::pair<T1,T2>& y) {
    563577      return ((x.second<y.second) ||
    564               (!(y.second<x.second) && (x.first<y.first))); 
     578              (!(y.second<x.second) && (x.first<y.first)));
    565579    }
    566580  };
     
    574588   */
    575589  template <class Pair>
    576   struct PairFirst 
     590  struct PairFirst
    577591  {
    578592    /**
     
    582596     */
    583597    typedef typename boost::mpl::if_<
    584                   typename boost::is_const<Pair>::type, 
     598                  typename boost::is_const<Pair>::type,
    585599                  typename boost::add_const<typename Pair::first_type>::type&,
    586600                  typename Pair::first_type&>::type result_type;
    587    
     601
    588602    /**
    589603       The argument type is Pair&.
     
    608622   */
    609623  template <class Pair>
    610   struct PairSecond 
     624  struct PairSecond
    611625  {
    612626    /**
     
    616630     */
    617631    typedef typename boost::mpl::if_<
    618                   typename boost::is_const<Pair>::type, 
     632                  typename boost::is_const<Pair>::type,
    619633                  typename boost::add_const<typename Pair::second_type>::type&,
    620634                  typename Pair::second_type&>::type result_type;
    621    
     635
    622636    /**
    623637       The argument type is Pair&.
     
    662676    typedef typename std::iterator_traits<Iter>::reference ref_type;
    663677    typedef typename boost::remove_reference<ref_type>::type val_type;
    664     typedef PairFirst<val_type> PF; 
     678    typedef PairFirst<val_type> PF;
    665679    return boost::transform_iterator<PF, Iter>(i, PF());
    666680  }
     
    693707    typedef typename std::iterator_traits<Iter>::reference ref_type;
    694708    typedef typename boost::remove_reference<ref_type>::type val_type;
    695     typedef PairSecond<val_type> PS; 
     709    typedef PairSecond<val_type> PS;
    696710    return boost::transform_iterator<PS, Iter>(i, PS());
    697711  }
     
    707721     std::vector<MyClass*> vec(18);
    708722     ...
    709      std::sort(vec.begin(), vec.end(), 
     723     std::sort(vec.begin(), vec.end(),
    710724               make_ptr_compare(vec[0], std::greater<MyClass>());
    711725     \endcode
     
    771785    if (iter==m.end()) {
    772786      std::stringstream ss;
    773       ss << "utility::get(const Map&, const Key&): `" 
     787      ss << "utility::get(const Map&, const Key&): `"
    774788         << key << "' not found in map\n";
    775789      throw runtime_error(ss.str());
Note: See TracChangeset for help on using the changeset viewer.