Claw  1.7.3
algorithm.tpp
Go to the documentation of this file.
1 /*
2  CLAW - a C++ Library Absolutely Wonderful
3 
4  CLAW is a free library without any particular aim but being useful to
5  anyone.
6 
7  Copyright (C) 2005-2011 Julien Jorge
8 
9  This library is free software; you can redistribute it and/or
10  modify it under the terms of the GNU Lesser General Public
11  License as published by the Free Software Foundation; either
12  version 2.1 of the License, or (at your option) any later version.
13 
14  This library is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  Lesser General Public License for more details.
18 
19  You should have received a copy of the GNU Lesser General Public
20  License along with this library; if not, write to the Free Software
21  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 
23  contact: julien.jorge@gamned.org
24 */
31 /*----------------------------------------------------------------------------*/
44 template<typename InputIterator, typename UnaryFunction>
45 UnaryFunction claw::inplace_for_each
46 ( InputIterator first, InputIterator last, UnaryFunction f )
47 {
48  for (; first!=last; ++first)
49  f(*first);
50 
51  return f;
52 } // inplace_for_each()
53 
54 /*----------------------------------------------------------------------------*/
67 template<typename ForwardIterator1, typename ForwardIterator2>
68 ForwardIterator1 claw::find_first_not_of
69 ( ForwardIterator1 first1, ForwardIterator1 last1,
70  ForwardIterator2 first2, ForwardIterator2 last2 )
71 {
72  for ( ; first1!=last1; ++first1 )
73  {
74  bool found(false);
75  for ( ForwardIterator2 it(first2); !found && (it!=last2); ++it )
76  found = *first1 == *it;
77 
78  if (!found)
79  return first1;
80  }
81 
82  return last1;
83 } // find_first_not_of()
84 
85 /*----------------------------------------------------------------------------*/
111 template<typename ForwardIterator1, typename ForwardIterator2,
112  typename ForwardIterator3>
113 std::size_t claw::replace
114 ( ForwardIterator1 first, ForwardIterator1 last,
115  ForwardIterator2 e1_first, ForwardIterator2 e1_last,
116  ForwardIterator3 e2_first, ForwardIterator3 e2_last )
117 {
118  if ( (e1_first == e1_last) || (e2_first == e2_last) )
119  return 0;
120 
121  std::size_t count(0);
122 
123  for ( ; first != last; ++first )
124  {
125  bool stop(false);
126  ForwardIterator3 r(e2_first);
127 
128  for (ForwardIterator2 it=e1_first; !stop && (it!=e1_last); ++it)
129  {
130  if ( *first == *it )
131  {
132  *first = *r;
133  ++count;
134  stop = true;
135  }
136  else
137  {
138  ForwardIterator3 n=r;
139  ++n;
140  if (n!=e2_last)
141  r = n;
142  }
143  }
144  }
145 
146  return count;
147 } // replace()