Embedded Template Library  1.0
 All Classes Files Functions Variables Typedefs Friends Modules Pages
algorithm.h
Go to the documentation of this file.
1 
3 /******************************************************************************
4 The MIT License(MIT)
5 
6 Embedded Template Library.
7 
8 Copyright(c) 2014 jwellbelove
9 
10 Permission is hereby granted, free of charge, to any person obtaining a copy
11 of this software and associated documentation files(the "Software"), to deal
12 in the Software without restriction, including without limitation the rights
13 to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
14 copies of the Software, and to permit persons to whom the Software is
15 furnished to do so, subject to the following conditions :
16 
17 The above copyright notice and this permission notice shall be included in all
18 copies or substantial portions of the Software.
19 
20 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
23 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 SOFTWARE.
27 ******************************************************************************/
28 
29 #ifndef __ETL_ALGORITHM__
30 #define __ETL_ALGORITHM__
31 
35 
36 #include <algorithm>
37 #include <iterator>
38 #include <utility>
39 #include <functional>
40 
41 #include "type_traits.h"
42 
43 namespace etl
44 {
45  //***************************************************************************
49  //***************************************************************************
50  template <typename TIterator, typename TCompare>
51  std::pair<TIterator, TIterator> minmax_element(TIterator begin, TIterator end, TCompare compare)
52  {
53  TIterator minimum = begin;
54  TIterator maximum = begin;
55 
56  while (begin != end)
57  {
58  if (compare(*begin, *minimum))
59  {
60  minimum = begin;
61  }
62 
63  if (compare(*maximum, *begin))
64  {
65  maximum = begin;
66  }
67 
68  ++begin;
69  }
70 
71  return std::pair<TIterator, TIterator>(minimum, maximum);
72  }
73 
74  //***************************************************************************
78  //***************************************************************************
79  template <typename TIterator>
80  std::pair<TIterator, TIterator> minmax_element(TIterator begin, TIterator end)
81  {
82  typedef typename std::iterator_traits<TIterator>::value_type value_t;
83 
84  return etl::minmax_element(begin, end, std::less<value_t>());
85  }
86 
87  //***************************************************************************
91  //***************************************************************************
92  template <typename T>
93  std::pair<const T&, const T&> minmax(const T& a, const T& b)
94  {
95  return (b < a) ? std::pair<const T&, const T&>(b, a) : std::pair<const T&, const T&>(a, b);
96  }
97 
98  //***************************************************************************
102  //***************************************************************************
103  template <typename T, typename TCompare>
104  std::pair<const T&, const T&> minmax(const T& a, const T& b, TCompare compare)
105  {
106  return compare(b, a) ? std::pair<const T&, const T&>(b, a) : std::pair<const T&, const T&>(a, b);
107  }
108 
109  //***************************************************************************
113  //***************************************************************************
114  template <typename TIterator>
115  TIterator is_sorted_until(TIterator begin, TIterator end)
116  {
117  if (begin != end)
118  {
119  TIterator next = begin;
120 
121  while (++next != end)
122  {
123  if (*next < *begin)
124  {
125  return next;
126  }
127 
128  ++begin;
129  }
130  }
131 
132  return end;
133  }
134 
135  //***************************************************************************
139  //***************************************************************************
140  template <typename TIterator, typename TCompare>
141  TIterator is_sorted_until(TIterator begin, TIterator end, TCompare compare)
142  {
143  if (begin != end)
144  {
145  TIterator next = begin;
146 
147  while (++next != end)
148  {
149  if (compare(*next, *begin))
150  {
151  return next;
152  }
153 
154  ++begin;
155  }
156  }
157 
158  return end;
159  }
160 
161  //***************************************************************************
165  //***************************************************************************
166  template<class TIterator>
167  bool is_sorted(TIterator begin, TIterator end)
168  {
169  return etl::is_sorted_until(begin, end) == end;
170  }
171 
172  //***************************************************************************
176  //***************************************************************************
177  template<class TIterator, class TCompare>
178  bool is_sorted(TIterator begin, TIterator end, TCompare compare)
179  {
180  return etl::is_sorted_until(begin, end, compare) == end;
181  }
182 
183  //***************************************************************************
187  //***************************************************************************
188  template <typename TInputIterator, typename Size, typename TOutputIterator>
189  TOutputIterator copy_n(TInputIterator begin, Size count, TOutputIterator result)
190  {
191  if (count > 0)
192  {
193  for (Size i = 0; i < count; ++i)
194  {
195  *result++ = *begin++;
196  }
197  }
198 
199  return result;
200  }
201 
202  //***************************************************************************
206  //***************************************************************************
207  template <typename TIterator, typename TOutputIterator, typename TUnaryPredicate>
208  TOutputIterator copy_if(TIterator begin, TIterator end, TOutputIterator out, TUnaryPredicate predicate)
209  {
210  while (begin != end)
211  {
212  if (predicate(*begin))
213  {
214  *out++ = *begin;
215  }
216 
217  ++begin;
218  }
219 
220  return out;
221  }
222 
223  //***************************************************************************
227  //***************************************************************************
228  template <typename TIterator, typename TUnaryPredicate>
229  TIterator find_if_not(TIterator begin, TIterator end, TUnaryPredicate predicate)
230  {
231  while (begin != end)
232  {
233  if (!predicate(*begin))
234  {
235  return begin;
236  }
237 
238  ++begin;
239  }
240 
241  return end;
242  }
243 
244  //***************************************************************************
248  //***************************************************************************
249  template <typename TIterator, typename TUnaryPredicate >
250  bool all_of(TIterator begin, TIterator end, TUnaryPredicate predicate)
251  {
252  return etl::find_if_not(begin, end, predicate) == end;
253  }
254 
255  //***************************************************************************
259  //***************************************************************************
260  template <typename TIterator, typename TUnaryPredicate >
261  bool any_of(TIterator begin, TIterator end, TUnaryPredicate predicate)
262  {
263  return std::find_if(begin, end, predicate) != end;
264  }
265 
266  //***************************************************************************
270  //***************************************************************************
271  template <typename TIterator, typename TUnaryPredicate >
272  bool none_of(TIterator begin, TIterator end, TUnaryPredicate predicate)
273  {
274  return std::find_if(begin, end, predicate) == end;
275  }
276 
277  //***************************************************************************
281  //***************************************************************************
282  template <typename TIterator1, typename TIterator2>
283  bool is_permutation(TIterator1 begin1, TIterator1 end1, TIterator2 begin2)
284  {
285  if (begin1 != end1)
286  {
287  TIterator2 end2 = begin2;
288 
289  std::advance(end2, std::distance(begin1, end1));
290 
291  for (TIterator1 i = begin1; i != end1; ++i)
292  {
293  if (i == std::find(begin1, i, *i))
294  {
295  size_t n = std::count(begin2, end2, *i);
296 
297  if (n == 0 || std::count(i, end1, *i) != n)
298  {
299  return false;
300  }
301  }
302  }
303  }
304 
305  return true;
306  }
307 
308  //***************************************************************************
312  //***************************************************************************
313  template <typename TIterator1, typename TIterator2>
314  bool is_permutation(TIterator1 begin1, TIterator1 end1, TIterator2 begin2, TIterator2 end2)
315  {
316  if (begin1 != end1)
317  {
318  for (TIterator1 i = begin1; i != end1; ++i)
319  {
320  if (i == std::find(begin1, i, *i))
321  {
322  size_t n = std::count(begin2, end2, *i);
323 
324  if (n == 0 || std::count(i, end1, *i) != n)
325  {
326  return false;
327  }
328  }
329  }
330  }
331 
332  return true;
333  }
334 
335  //***************************************************************************
339  //***************************************************************************
340  template <typename TIterator1, typename TIterator2, typename TBinaryPredicate>
341  bool is_permutation(TIterator1 begin1, TIterator1 end1, TIterator2 begin2, TBinaryPredicate predicate)
342  {
343  if (begin1 != end1)
344  {
345  TIterator2 end2 = begin2;
346 
347  std::advance(end2, std::distance(begin1, end1));
348 
349  for (TIterator1 i = begin1; i != end1; ++i)
350  {
351  if (i == std::find_if(begin1, i, std::bind1st(predicate, *i)))
352  {
353  size_t n = std::count(begin2, end2, *i);
354 
355  if (n == 0 || std::count(i, end1, *i) != n)
356  {
357  return false;
358  }
359  }
360  }
361  }
362 
363  return true;
364  }
365 
366  //***************************************************************************
370  //***************************************************************************
371  template <typename TIterator1, typename TIterator2, typename TBinaryPredicate>
372  bool is_permutation(TIterator1 begin1, TIterator1 end1, TIterator2 begin2, TIterator2 end2, TBinaryPredicate predicate)
373  {
374  if (begin1 != end1)
375  {
376  for (TIterator1 i = begin1; i != end1; ++i)
377  {
378  if (i == std::find_if(begin1, i, std::bind1st(predicate, *i)))
379  {
380  size_t n = std::count(begin2, end2, *i);
381 
382  if (n == 0 || std::count(i, end1, *i) != n)
383  {
384  return false;
385  }
386  }
387  }
388  }
389 
390  return true;
391  }
392 
393  //***************************************************************************
397  //***************************************************************************
398  template <typename TIterator, typename TUnaryPredicate>
399  bool is_partitioned(TIterator begin, TIterator end, TUnaryPredicate predicate)
400  {
401  while (begin != end)
402  {
403  if (!predicate(*begin++))
404  {
405  break;
406  }
407  }
408 
409  while (begin != end)
410  {
411  if (predicate(*begin++))
412  {
413  return false;
414  }
415  }
416 
417  return true;
418  }
419 
420  //***************************************************************************
424  //***************************************************************************
425  template <class TIterator, class TUnaryPredicate>
426  TIterator partition_point(TIterator begin, TIterator end, TUnaryPredicate predicate)
427  {
428  while (begin != end)
429  {
430  if (!predicate(*begin))
431  {
432  return begin;
433  }
434 
435  ++begin;
436  }
437 
438  return begin;
439  }
440 
441  //***************************************************************************
446  //***************************************************************************
447  template <typename TSource, typename TDestinationTrue, typename TDestinationFalse, typename TUnaryPredicate>
448  std::pair<TDestinationTrue, TDestinationFalse> partition_copy(TSource begin,
449  TSource end,
450  TDestinationTrue destination_true,
451  TDestinationFalse destination_false,
452  TUnaryPredicate predicate)
453  {
454  while (begin != end)
455  {
456  if (predicate(*begin))
457  {
458  *destination_true++ = *begin++;
459  }
460  else
461  {
462  *destination_false++ = *begin++;
463  }
464  }
465 
466  return std::pair<TDestinationTrue, TDestinationFalse>(destination_true, destination_false);
467  }
468 }
469 
470 #endif
471 
std::pair< TDestinationTrue, TDestinationFalse > partition_copy(TSource begin, TSource end, TDestinationTrue destination_true, TDestinationFalse destination_false, TUnaryPredicate predicate)
Definition: algorithm.h:448
bool is_partitioned(TIterator begin, TIterator end, TUnaryPredicate predicate)
Definition: algorithm.h:399
std::pair< const T &, const T & > minmax(const T &a, const T &b, TCompare compare)
Definition: algorithm.h:104
TIterator find_if_not(TIterator begin, TIterator end, TUnaryPredicate predicate)
Definition: algorithm.h:229
TIterator is_sorted_until(TIterator begin, TIterator end, TCompare compare)
Definition: algorithm.h:141
bool none_of(TIterator begin, TIterator end, TUnaryPredicate predicate)
Definition: algorithm.h:272
TOutputIterator copy_if(TIterator begin, TIterator end, TOutputIterator out, TUnaryPredicate predicate)
Definition: algorithm.h:208
TOutputIterator copy_n(TInputIterator begin, Size count, TOutputIterator result)
Definition: algorithm.h:189
Definition: algorithm.h:43
bool any_of(TIterator begin, TIterator end, TUnaryPredicate predicate)
Definition: algorithm.h:261
TContainer::iterator end(TContainer &container)
Definition: container.h:95
TContainer::iterator begin(TContainer &container)
Definition: container.h:45
TIterator next(TIterator iterator, ptrdiff_t n=1)
Definition: container.h:245
std::pair< TIterator, TIterator > minmax_element(TIterator begin, TIterator end)
Definition: algorithm.h:80
bool is_sorted(TIterator begin, TIterator end, TCompare compare)
Definition: algorithm.h:178
bool is_permutation(TIterator1 begin1, TIterator1 end1, TIterator2 begin2, TIterator2 end2, TBinaryPredicate predicate)
Definition: algorithm.h:372
bool all_of(TIterator begin, TIterator end, TUnaryPredicate predicate)
Definition: algorithm.h:250
TIterator partition_point(TIterator begin, TIterator end, TUnaryPredicate predicate)
Definition: algorithm.h:426