ProteoWizard
copyif.hpp
Go to the documentation of this file.
1 //
2 // $Id: copyif.hpp 5313 2013-12-17 18:06:54Z chambm $
3 //
4 //
5 // Original author: Witold Wolski <wewolski@gmail.com>
6 //
7 // Copyright : ETH Zurich
8 //
9 // Licensed under the Apache License, Version 2.0 (the "License");
10 // you may not use this file except in compliance with the License.
11 // You may obtain a copy of the License at
12 //
13 // http://www.apache.org/licenses/LICENSE-2.0
14 //
15 // Unless required by applicable law or agreed to in writing, software
16 // distributed under the License is distributed on an "AS IS" BASIS,
17 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 // See the License for the specific language governing permissions and
19 // limitations under the License.
20 //
21 
22 #ifndef COPY_IF_H
23 #define COPY_IF_H
24 
25 #include <iterator>
26 
27 namespace ralab
28 {
29  namespace base
30  {
31  namespace utils
32  {
33 
34  template <class InputIterator,class InputIterator2, class OutputIterator, class Predicate>
35  OutputIterator copy_if(
36  InputIterator first,
37  InputIterator last,
38  InputIterator2 source,
39  OutputIterator result,
40  Predicate pred)
41  {
42  while(first!=last)
43  {
44  if(pred(*first))
45  {
46  *result = *source;
47  ++result;
48  }
49  ++first;
50  ++source;
51  }
52  return result;
53  }
54 
55  template <class InputIterator,class InputIterator2, class OutputIterator, class Predicate>
56  OutputIterator copy_if_not(
57  InputIterator first,
58  InputIterator last,
59  InputIterator2 source,
60  OutputIterator result,
61  Predicate pred)
62  {
63  while(first!=last)
64  {
65  if(!pred(*first))
66  {
67  *result = *source;
68  ++result;
69  }
70  ++first;
71  ++source;
72  }
73  return result;
74  }
75 
76  /*! \brief copy_if
77 
78  Implementation of copy_if as suggested
79  in Efficient STL (Scott Meyers) item 37.
80  */
81  template < typename InputIterator,
82  typename OutputIterator,
83  typename Predicate >
84  OutputIterator copy_if(
85  InputIterator begin,
86  InputIterator end,
87  OutputIterator destBegin,
88  Predicate p)
89  {
90 
91  while(begin != end)
92  {
93  typename std::iterator_traits<InputIterator>::reference r= *begin;
94  if(p(r))
95  {
96  *destBegin = r;
97  ++destBegin;
98  }
99  ++begin;
100  }
101  return destBegin;
102  }
103 
104  /*! \brief copy_if_not for containers
105 
106  Implementation of copy_if as suggested
107  in Efficient STL (Scott Meyers) item 37.
108  */
109  template < typename InputIterator,
110  typename OutputIterator,
111  typename Predicate >
112  OutputIterator copy_if_not(
113  InputIterator begin,
114  InputIterator end,
115  OutputIterator destBegin,
116  Predicate p
117  )
118  {
119 
120  while(begin != end)
121  {
122  typename std::iterator_traits<InputIterator>::reference r= *begin;
123  if(!p(r))
124  {
125  *destBegin = r;
126  ++destBegin;
127  }
128  ++begin;
129  }
130  return destBegin;
131  }
132 
133 
134  }//end utils
135  }//end base
136 }//end ralab
137 
138 
139 
140 #endif // COPY_IF_H
EQUISPACEINTERPOL Interpolation on a equidistantly spaced grid.
Definition: base.hpp:39
OutputIterator copy_if(InputIterator first, InputIterator last, InputIterator2 source, OutputIterator result, Predicate pred)
Definition: copyif.hpp:35
OutputIterator copy_if_not(InputIterator first, InputIterator last, InputIterator2 source, OutputIterator result, Predicate pred)
Definition: copyif.hpp:56