CLAW Library (a C++ Library Absolutely Wonderful) 1.5.5

algorithm.tpp

Go to the documentation of this file.
00001 /*
00002   CLAW - a C++ Library Absolutely Wonderful
00003 
00004   CLAW is a free library without any particular aim but being useful to 
00005   anyone.
00006 
00007   Copyright (C) 2005-2010 Julien Jorge
00008 
00009   This library is free software; you can redistribute it and/or
00010   modify it under the terms of the GNU Lesser General Public
00011   License as published by the Free Software Foundation; either
00012   version 2.1 of the License, or (at your option) any later version.
00013 
00014   This library is distributed in the hope that it will be useful,
00015   but WITHOUT ANY WARRANTY; without even the implied warranty of
00016   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00017   Lesser General Public License for more details.
00018 
00019   You should have received a copy of the GNU Lesser General Public
00020   License along with this library; if not, write to the Free Software
00021   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00022 
00023   contact: julien_jorge@yahoo.fr
00024 */
00031 /*----------------------------------------------------------------------------*/
00044 template<typename InputIterator, typename UnaryFunction>
00045 UnaryFunction claw::inplace_for_each
00046 ( InputIterator first, InputIterator last, UnaryFunction f )
00047 {
00048   for (; first!=last; ++first)
00049     f(*first);
00050 
00051   return f;
00052 } // inplace_for_each()
00053 
00054 /*----------------------------------------------------------------------------*/
00067 template<typename ForwardIterator1, typename ForwardIterator2>
00068 ForwardIterator1 claw::find_first_not_of
00069 ( ForwardIterator1 first1, ForwardIterator1 last1,
00070   ForwardIterator2 first2, ForwardIterator2 last2 )
00071 {
00072   for ( ; first1!=last1; ++first1 )
00073     {
00074       bool found(false);
00075       for ( ForwardIterator2 it(first2); !found && (it!=last2); ++it )
00076         found = *first1 == *it;
00077 
00078       if (!found)
00079         return first1;
00080     }
00081 
00082   return last1;
00083 } // find_first_not_of()
00084 
00085 /*----------------------------------------------------------------------------*/
00111 template<typename ForwardIterator1, typename ForwardIterator2,
00112          typename ForwardIterator3>
00113 std::size_t claw::replace
00114 ( ForwardIterator1 first, ForwardIterator1 last,
00115   ForwardIterator2 e1_first, ForwardIterator2 e1_last,
00116   ForwardIterator3 e2_first, ForwardIterator3 e2_last )
00117 {
00118   if ( (e1_first == e1_last) || (e2_first == e2_last) )
00119     return 0;
00120 
00121   std::size_t count(0);
00122 
00123   for ( ; first != last; ++first )
00124     {
00125       bool stop(false);
00126       ForwardIterator3 r(e2_first);
00127 
00128       for (ForwardIterator2 it=e1_first; !stop && (it!=e1_last); ++it)
00129         {
00130           if ( *first == *it )
00131             {
00132               *first = *r;
00133               ++count;
00134               stop = true;
00135             }
00136           else
00137             {
00138               ForwardIterator3 n=r;
00139               ++n;
00140               if (n!=e2_last)
00141                 r = n;
00142             }
00143         }
00144     }
00145 
00146   return count;
00147 } // replace()