Changeset 465 for trunk/lib/utility.h


Ignore:
Timestamp:
Aug 24, 2007, 3:28:24 PM (15 years ago)
Author:
Peter Johansson
Message:

fixes #217. Moving functors to a Functor.h and Functor.cc

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/lib/utility.h

    r462 r465  
    176176                    std::string);
    177177
    178   struct AlNum
    179   {
    180     inline bool operator()(std::string::const_iterator i) const
    181     { return isalnum(*i); }
    182   };
    183 
    184 
    185   struct Digit
    186   {
    187     inline bool operator()(std::string::const_iterator i) const
    188     { return isdigit(*i); }
    189   };
    190 
    191 
    192   class notChar
    193   {
    194   public:
    195     notChar(char);
    196     inline bool operator()(std::string::const_iterator i) const
    197     { return *i!=char_; }
    198   private:
    199     char char_;
    200   };
    201 
    202 
    203   class not2Char
    204   {
    205   public:
    206     not2Char(char, char);
    207     inline bool operator()(std::string::const_iterator i) const
    208     { return *i!=char1_ && *i!=char2_; }
    209   private:
    210     const char char1_;
    211     const char char2_;
    212   };
    213 
    214   class not2Str
    215   {
    216   public:
    217     not2Str(std::string, std::string);
    218     inline bool operator()(std::string::const_iterator i) const
    219     {
    220       return !(std::equal(str1_.begin(), str1_.end(), i) ||
    221               std::equal(str2_.begin(), str2_.end(), i));
    222     }
    223 
    224   private:
    225     const std::string str1_;
    226     const std::string str2_;
    227   };
    228 
    229   ///
    230   /// Functor to be used on contaioners and works as standard less,
    231   /// but on the reversed container.
    232   ///
    233   /// Requirements on T is that has rend and rbegin. T::value_type
    234   /// must be comparable (i.e. have operator<)
    235   ///
    236   template <typename T>
    237   struct LessReversed
    238   {
    239     ///
    240     /// using std::lexicographical_compare on the reversed container
    241     ///
    242     inline bool operator()(const T& x, const T& y) const
    243     { return std::lexicographical_compare(x.rbegin(),x.rend(),
    244                                           y.rbegin(),y.rend()); }
    245   };
    246 
    247 
    248   ///
    249   /// @brief Functor comparing pairs using second.
    250   ///
    251   /// STL provides operator< for the pair.first element, but none for
    252   /// pair.second. This template provides this and can be used as the
    253   /// comparison object in generic functions such as the STL sort.
    254   ///
    255   template <typename T1, typename T2>
    256   struct pair_value_compare
    257   {
    258     ///
    259     /// @return true if x.second<y.second or (x.second==y.second and
    260     /// x.first<y.first)
    261     ///
    262     inline bool operator()(const std::pair<T1,T2>& x,
    263                            const std::pair<T1,T2>& y) {
    264       return ((x.second<y.second) ||
    265               (!(y.second<x.second) && (x.first<y.first)));
    266     }
    267   };
    268 
    269  
    270   ///
    271   /// Functor working on pair.second, using a user passed functor.
    272   ///
    273   template <typename T1, typename T2, typename T3>
    274   struct PairSecondCompare
    275   {
    276 
    277     ///
    278     /// @brief Constructor
    279     ///
    280     explicit PairSecondCompare(const T3& comp)
    281       : compare_(comp) {}
    282 
    283     ///
    284     /// @return compare(x.second, y.second) where compare is a
    285     /// internal comparison functor.
    286     ///
    287     inline bool operator()(const std::pair<T1,T2>& x,
    288                            const std::pair<T1,T2>& y) const
    289     { return compare_(x.second,y.second); }
    290 
    291   private:
    292     T3 compare_;
    293 
    294   };
    295  
    296   ///
    297   /// Calculating sum of two vectors.
    298   ///
    299   /// @return resulting vector
    300   ///
    301   template <typename T >
    302   struct VectorPlus :
    303     public std::binary_function<std::vector<T>,std::vector<T>,std::vector<T> >
    304   {
    305     std::vector<T> operator()(const std::vector<T>& u,
    306                               const std::vector<T>& v) const
    307     {
    308       if ( u.size() > v.size() ){
    309         std::vector<T> res(u.size());
    310         transform(u.begin(), u.end(), v.begin(), res.begin(), std::plus<T>());
    311         copy(u.begin()+v.size(), u.end(), res.begin()+v.size());
    312         return res;
    313       }
    314  
    315       std::vector<T> res(v.size());
    316       transform(v.begin(), v.end(), u.begin(), res.begin(), std::plus<T>());
    317       if ( v.size() > u.size() )
    318         copy(v.begin()+u.size(), v.end(), res.begin()+u.size());
    319       return res;
    320     }
    321 
    322   };
    323 
    324   ///
    325   /// @return resulting vector
    326   ///
    327   template <typename Key, typename T>
    328   struct PairValuePlus :
    329     public std::binary_function<std::vector<T>,
    330                                 std::pair<const Key, std::vector<T> >,
    331                                 std::vector<T> >
    332   {
    333     std::vector<T> operator()(const std::vector<T>& sum,
    334                               const std::pair<const Key,std::vector<T> >& p)
    335     {
    336       return VectorPlus<T>()(sum, p.second);
    337     }
    338   };
    339 
    340178}} // end of namespace svndigest end of namespace theplu
    341179
Note: See TracChangeset for help on using the changeset viewer.