Chris@16
|
1 // (C) Copyright Gennadiy Rozental 2004-2008.
|
Chris@16
|
2 // Distributed under the Boost Software License, Version 1.0.
|
Chris@16
|
3 // (See accompanying file LICENSE_1_0.txt or copy at
|
Chris@16
|
4 // http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
5
|
Chris@16
|
6 // See http://www.boost.org/libs/test for the library home page.
|
Chris@16
|
7 //
|
Chris@16
|
8 // File : $RCSfile$
|
Chris@16
|
9 //
|
Chris@101
|
10 // Version : $Revision$
|
Chris@16
|
11 //
|
Chris@16
|
12 // Description : addition to STL algorithms
|
Chris@16
|
13 // ***************************************************************************
|
Chris@16
|
14
|
Chris@16
|
15 #ifndef BOOST_ALGORITHM_HPP_062304GER
|
Chris@16
|
16 #define BOOST_ALGORITHM_HPP_062304GER
|
Chris@16
|
17
|
Chris@16
|
18 #include <utility>
|
Chris@16
|
19 #include <algorithm> // std::find
|
Chris@16
|
20 #include <functional> // std::bind1st
|
Chris@16
|
21
|
Chris@16
|
22 #include <boost/test/detail/suppress_warnings.hpp>
|
Chris@16
|
23
|
Chris@16
|
24 //____________________________________________________________________________//
|
Chris@16
|
25
|
Chris@16
|
26 namespace boost {
|
Chris@16
|
27
|
Chris@16
|
28 namespace unit_test {
|
Chris@16
|
29
|
Chris@16
|
30 /// @brief this algorithm search through two collections for first mismatch position that get returned as a pair
|
Chris@16
|
31 /// of iterators, first pointing to the mismatch position in first collection, second iterator in second one
|
Chris@16
|
32
|
Chris@16
|
33 /// @param first1 - first collection begin iterator
|
Chris@16
|
34 /// @param last1 - first collection end iterator
|
Chris@16
|
35 /// @param first2 - second collection begin iterator
|
Chris@16
|
36 /// @param last2 - second collection end iterator
|
Chris@16
|
37 template <class InputIter1, class InputIter2>
|
Chris@16
|
38 inline std::pair<InputIter1, InputIter2>
|
Chris@16
|
39 mismatch( InputIter1 first1, InputIter1 last1,
|
Chris@16
|
40 InputIter2 first2, InputIter2 last2 )
|
Chris@16
|
41 {
|
Chris@16
|
42 while( first1 != last1 && first2 != last2 && *first1 == *first2 ) {
|
Chris@16
|
43 ++first1;
|
Chris@16
|
44 ++first2;
|
Chris@16
|
45 }
|
Chris@16
|
46
|
Chris@16
|
47 return std::pair<InputIter1, InputIter2>(first1, first2);
|
Chris@16
|
48 }
|
Chris@16
|
49
|
Chris@16
|
50 //____________________________________________________________________________//
|
Chris@16
|
51
|
Chris@16
|
52 /// @brief this algorithm search through two collections for first mismatch position that get returned as a pair
|
Chris@16
|
53 /// of iterators, first pointing to the mismatch position in first collection, second iterator in second one. This algorithms
|
Chris@16
|
54 /// uses supplied predicate for collection elements comparison
|
Chris@16
|
55
|
Chris@16
|
56 /// @param first1 - first collection begin iterator
|
Chris@16
|
57 /// @param last1 - first collection end iterator
|
Chris@16
|
58 /// @param first2 - second collection begin iterator
|
Chris@16
|
59 /// @param last2 - second collection end iterator
|
Chris@16
|
60 /// @param pred - predicate to be used for search
|
Chris@16
|
61 template <class InputIter1, class InputIter2, class Predicate>
|
Chris@16
|
62 inline std::pair<InputIter1, InputIter2>
|
Chris@16
|
63 mismatch( InputIter1 first1, InputIter1 last1,
|
Chris@16
|
64 InputIter2 first2, InputIter2 last2,
|
Chris@16
|
65 Predicate pred )
|
Chris@16
|
66 {
|
Chris@16
|
67 while( first1 != last1 && first2 != last2 && pred( *first1, *first2 ) ) {
|
Chris@16
|
68 ++first1;
|
Chris@16
|
69 ++first2;
|
Chris@16
|
70 }
|
Chris@16
|
71
|
Chris@16
|
72 return std::pair<InputIter1, InputIter2>(first1, first2);
|
Chris@16
|
73 }
|
Chris@16
|
74
|
Chris@16
|
75 //____________________________________________________________________________//
|
Chris@16
|
76
|
Chris@16
|
77 /// @brief this algorithm search through first collection for first element that does not belong a second one
|
Chris@16
|
78
|
Chris@16
|
79 /// @param first1 - first collection begin iterator
|
Chris@16
|
80 /// @param last1 - first collection end iterator
|
Chris@16
|
81 /// @param first2 - second collection begin iterator
|
Chris@16
|
82 /// @param last2 - second collection end iterator
|
Chris@16
|
83 template<class ForwardIterator1, class ForwardIterator2>
|
Chris@16
|
84 inline ForwardIterator1
|
Chris@16
|
85 find_first_not_of( ForwardIterator1 first1, ForwardIterator1 last1,
|
Chris@16
|
86 ForwardIterator2 first2, ForwardIterator2 last2 )
|
Chris@16
|
87 {
|
Chris@16
|
88 while( first1 != last1 ) {
|
Chris@16
|
89 if( std::find( first2, last2, *first1 ) == last2 )
|
Chris@16
|
90 break;
|
Chris@16
|
91 ++first1;
|
Chris@16
|
92 }
|
Chris@16
|
93
|
Chris@16
|
94 return first1;
|
Chris@16
|
95 }
|
Chris@16
|
96
|
Chris@16
|
97 //____________________________________________________________________________//
|
Chris@16
|
98
|
Chris@16
|
99 /// @brief this algorithm search through first collection for first element that does not satisfy binary
|
Chris@16
|
100 /// predicate in conjunction will any element in second collection
|
Chris@16
|
101
|
Chris@16
|
102 /// @param first1 - first collection begin iterator
|
Chris@16
|
103 /// @param last1 - first collection end iterator
|
Chris@16
|
104 /// @param first2 - second collection begin iterator
|
Chris@16
|
105 /// @param last2 - second collection end iterator
|
Chris@16
|
106 /// @param pred - predicate to be used for search
|
Chris@16
|
107 template<class ForwardIterator1, class ForwardIterator2, class Predicate>
|
Chris@16
|
108 inline ForwardIterator1
|
Chris@16
|
109 find_first_not_of( ForwardIterator1 first1, ForwardIterator1 last1,
|
Chris@16
|
110 ForwardIterator2 first2, ForwardIterator2 last2,
|
Chris@16
|
111 Predicate pred )
|
Chris@16
|
112 {
|
Chris@16
|
113 while( first1 != last1 ) {
|
Chris@16
|
114 if( std::find_if( first2, last2, std::bind1st( pred, *first1 ) ) == last2 )
|
Chris@16
|
115 break;
|
Chris@16
|
116 ++first1;
|
Chris@16
|
117 }
|
Chris@16
|
118
|
Chris@16
|
119 return first1;
|
Chris@16
|
120 }
|
Chris@16
|
121
|
Chris@16
|
122 //____________________________________________________________________________//
|
Chris@16
|
123
|
Chris@16
|
124 /// @brief this algorithm search through first collection for last element that belongs to a second one
|
Chris@16
|
125
|
Chris@16
|
126 /// @param first1 - first collection begin iterator
|
Chris@16
|
127 /// @param last1 - first collection end iterator
|
Chris@16
|
128 /// @param first2 - second collection begin iterator
|
Chris@16
|
129 /// @param last2 - second collection end iterator
|
Chris@16
|
130 template<class BidirectionalIterator1, class ForwardIterator2>
|
Chris@16
|
131 inline BidirectionalIterator1
|
Chris@16
|
132 find_last_of( BidirectionalIterator1 first1, BidirectionalIterator1 last1,
|
Chris@16
|
133 ForwardIterator2 first2, ForwardIterator2 last2 )
|
Chris@16
|
134 {
|
Chris@16
|
135 if( first1 == last1 || first2 == last2 )
|
Chris@16
|
136 return last1;
|
Chris@16
|
137
|
Chris@16
|
138 BidirectionalIterator1 it1 = last1;
|
Chris@16
|
139 while( --it1 != first1 && std::find( first2, last2, *it1 ) == last2 ) {}
|
Chris@16
|
140
|
Chris@16
|
141 return it1 == first1 && std::find( first2, last2, *it1 ) == last2 ? last1 : it1;
|
Chris@16
|
142 }
|
Chris@16
|
143
|
Chris@16
|
144 //____________________________________________________________________________//
|
Chris@16
|
145
|
Chris@16
|
146 /// @brief this algorithm search through first collection for last element that satisfy binary
|
Chris@16
|
147 /// predicate in conjunction will at least one element in second collection
|
Chris@16
|
148
|
Chris@16
|
149 /// @param first1 - first collection begin iterator
|
Chris@16
|
150 /// @param last1 - first collection end iterator
|
Chris@16
|
151 /// @param first2 - second collection begin iterator
|
Chris@16
|
152 /// @param last2 - second collection end iterator
|
Chris@16
|
153 /// @param pred - predicate to be used for search
|
Chris@16
|
154 template<class BidirectionalIterator1, class ForwardIterator2, class Predicate>
|
Chris@16
|
155 inline BidirectionalIterator1
|
Chris@16
|
156 find_last_of( BidirectionalIterator1 first1, BidirectionalIterator1 last1,
|
Chris@16
|
157 ForwardIterator2 first2, ForwardIterator2 last2,
|
Chris@16
|
158 Predicate pred )
|
Chris@16
|
159 {
|
Chris@16
|
160 if( first1 == last1 || first2 == last2 )
|
Chris@16
|
161 return last1;
|
Chris@16
|
162
|
Chris@16
|
163 BidirectionalIterator1 it1 = last1;
|
Chris@16
|
164 while( --it1 != first1 && std::find_if( first2, last2, std::bind1st( pred, *it1 ) ) == last2 ) {}
|
Chris@16
|
165
|
Chris@16
|
166 return it1 == first1 && std::find_if( first2, last2, std::bind1st( pred, *it1 ) ) == last2 ? last1 : it1;
|
Chris@16
|
167 }
|
Chris@16
|
168
|
Chris@16
|
169 //____________________________________________________________________________//
|
Chris@16
|
170
|
Chris@16
|
171 /// @brief this algorithm search through first collection for last element that does not belong to a second one
|
Chris@16
|
172
|
Chris@16
|
173 /// @param first1 - first collection begin iterator
|
Chris@16
|
174 /// @param last1 - first collection end iterator
|
Chris@16
|
175 /// @param first2 - second collection begin iterator
|
Chris@16
|
176 /// @param last2 - second collection end iterator
|
Chris@16
|
177 template<class BidirectionalIterator1, class ForwardIterator2>
|
Chris@16
|
178 inline BidirectionalIterator1
|
Chris@16
|
179 find_last_not_of( BidirectionalIterator1 first1, BidirectionalIterator1 last1,
|
Chris@16
|
180 ForwardIterator2 first2, ForwardIterator2 last2 )
|
Chris@16
|
181 {
|
Chris@16
|
182 if( first1 == last1 || first2 == last2 )
|
Chris@16
|
183 return last1;
|
Chris@16
|
184
|
Chris@16
|
185 BidirectionalIterator1 it1 = last1;
|
Chris@16
|
186 while( --it1 != first1 && std::find( first2, last2, *it1 ) != last2 ) {}
|
Chris@16
|
187
|
Chris@16
|
188 return it1 == first1 && std::find( first2, last2, *it1 ) != last2 ? last1 : it1;
|
Chris@16
|
189 }
|
Chris@16
|
190
|
Chris@16
|
191 //____________________________________________________________________________//
|
Chris@16
|
192
|
Chris@16
|
193 /// @brief this algorithm search through first collection for last element that does not satisfy binary
|
Chris@16
|
194 /// predicate in conjunction will any element in second collection
|
Chris@16
|
195
|
Chris@16
|
196 /// @param first1 - first collection begin iterator
|
Chris@16
|
197 /// @param last1 - first collection end iterator
|
Chris@16
|
198 /// @param first2 - second collection begin iterator
|
Chris@16
|
199 /// @param last2 - second collection end iterator
|
Chris@16
|
200 /// @param pred - predicate to be used for search
|
Chris@16
|
201 template<class BidirectionalIterator1, class ForwardIterator2, class Predicate>
|
Chris@16
|
202 inline BidirectionalIterator1
|
Chris@16
|
203 find_last_not_of( BidirectionalIterator1 first1, BidirectionalIterator1 last1,
|
Chris@16
|
204 ForwardIterator2 first2, ForwardIterator2 last2,
|
Chris@16
|
205 Predicate pred )
|
Chris@16
|
206 {
|
Chris@16
|
207 if( first1 == last1 || first2 == last2 )
|
Chris@16
|
208 return last1;
|
Chris@16
|
209
|
Chris@16
|
210 BidirectionalIterator1 it1 = last1;
|
Chris@16
|
211 while( --it1 != first1 && std::find_if( first2, last2, std::bind1st( pred, *it1 ) ) != last2 ) {}
|
Chris@16
|
212
|
Chris@16
|
213 return it1 == first1 && std::find_if( first2, last2, std::bind1st( pred, *it1 ) ) == last2 ? last1 : it1;
|
Chris@16
|
214 }
|
Chris@16
|
215
|
Chris@16
|
216 //____________________________________________________________________________//
|
Chris@16
|
217
|
Chris@16
|
218 } // namespace unit_test
|
Chris@16
|
219
|
Chris@16
|
220 } // namespace boost
|
Chris@16
|
221
|
Chris@16
|
222 //____________________________________________________________________________//
|
Chris@16
|
223
|
Chris@16
|
224 #include <boost/test/detail/enable_warnings.hpp>
|
Chris@16
|
225
|
Chris@16
|
226 #endif // BOOST_ALGORITHM_HPP_062304GER
|
Chris@16
|
227
|
Chris@16
|
228
|