source: mainline/uspace/lib/cpp/include/impl/algorithm.hpp@ 71f713a

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 71f713a was 1668862, checked in by Dzejrou <dzejrou@…>, 7 years ago

cpp: fixed std::equal, it used the iterators as if they were random access iterator, which they don't have to be

  • Property mode set to 100644
File size: 28.4 KB
Line 
1/*
2 * Copyright (c) 2017 Jaroslav Jindrak
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#ifndef LIBCPP_ALGORITHM
30#define LIBCPP_ALGORITHM
31
32#include <iterator>
33#include <utility>
34
35namespace std
36{
37 template<class T>
38 struct less;
39
40 /**
41 * 25.2, non-modyfing sequence operations:
42 */
43
44 /**
45 * 25.2.1, all_of:
46 */
47
48 template<class InputIterator, class Predicate>
49 bool all_of(InputIterator first, InputIterator last, Predicate pred)
50 {
51 while (first != last)
52 {
53 if (!pred(*first++))
54 return false;
55 }
56
57 return true;
58 }
59
60 /**
61 * 25.2.2, any_of:
62 */
63
64 template<class InputIterator, class Predicate>
65 bool any_of(InputIterator first, InputIterator last, Predicate pred)
66 {
67 while (first != last)
68 {
69 if (pred(*first++))
70 return true;
71 }
72
73 return false;
74 }
75
76 /**
77 * 25.2.3, none_of:
78 */
79
80 template<class InputIterator, class Predicate>
81 bool none_of(InputIterator first, InputIterator last, Predicate pred)
82 {
83 return !any_of(first, last, pred);
84 }
85
86 /**
87 * 25.2.4, for_each:
88 */
89
90 template<class InputIterator, class Function>
91 Function for_each(InputIterator first, InputIterator last, Function f)
92 {
93 while (first != last)
94 f(*first++);
95
96 return move(f);
97 }
98
99 /**
100 * 25.2.5, find:
101 */
102
103 template<class InputIterator, class T>
104 InputIterator find(InputIterator first, InputIterator last, const T& value)
105 {
106 while (first != last)
107 {
108 if (*first == value)
109 return first;
110 ++first;
111 }
112
113 return last;
114 }
115
116 template<class InputIterator, class Predicate>
117 InputIterator find_if(InputIterator first, InputIterator last, Predicate pred)
118 {
119 while (first != last)
120 {
121 if (pred(*first))
122 return first;
123 ++first;
124 }
125
126 return last;
127 }
128
129 template<class InputIterator, class Predicate>
130 InputIterator find_if_not(InputIterator first, InputIterator last, Predicate pred)
131 {
132 while (first != last)
133 {
134 if (!pred(*first))
135 return first;
136 ++first;
137 }
138
139 return last;
140 }
141
142 /**
143 * 25.2.6, find_end:
144 */
145
146 // TODO: implement
147
148 /**
149 * 25.2.7, find_first:
150 */
151
152 // TODO: implement
153
154 /**
155 * 25.2.8, adjacent_find:
156 */
157
158 template<class ForwardIterator>
159 ForwardIterator adjacent_find(ForwardIterator first, ForwardIterator last)
160 {
161 while (first != last)
162 {
163 if (*first == *(first + 1))
164 return first;
165 ++first;
166 }
167
168 return last;
169 }
170
171 template<class ForwardIterator, class Predicate>
172 ForwardIterator adjacent_find(ForwardIterator first, ForwardIterator last, Predicate pred)
173 {
174 while (first != last)
175 {
176 if (pred(*first, *(first + 1)))
177 return first;
178 ++first;
179 }
180
181 return last;
182 }
183
184 /**
185 * 25.2.9, count:
186 */
187
188 template<class InputIterator, class T>
189 typename iterator_traits<InputIterator>::difference_type
190 count(InputIterator first, InputIterator last, const T& value)
191 {
192 typename iterator_traits<InputIterator>::difference_type cnt{};
193
194 while (first != last)
195 {
196 if (*first++ == value)
197 ++cnt;
198 }
199
200 return cnt;
201 }
202
203 template<class InputIterator, class Predicate>
204 typename iterator_traits<InputIterator>::difference_type
205 count_if(InputIterator first, InputIterator last, Predicate pred)
206 {
207 typename iterator_traits<InputIterator>::difference_type cnt{};
208
209 while (first != last)
210 {
211 if (pred(*first++))
212 ++cnt;
213 }
214
215 return cnt;
216 }
217
218 /**
219 * 25.2.10, mismatch:
220 */
221
222 template<class InputIterator1, class InputIterator2>
223 pair<InputIterator1, InputIterator2> mismatch(InputIterator1 first1, InputIterator1 last1,
224 InputIterator2 first2)
225 {
226 while (first1 != last1 && *first1 == *first2)
227 {
228 ++first1;
229 ++first2;
230 }
231
232 return make_pair(first1, first2);
233 }
234
235 template<class InputIterator1, class InputIterator2, class BinaryPredicate>
236 pair<InputIterator1, InputIterator2> mismatch(InputIterator1 first1, InputIterator1 last1,
237 InputIterator2 first2, BinaryPredicate pred)
238 {
239 while (first1 != last1 && pred(*first1, *first2))
240 {
241 ++first1;
242 ++first2;
243 }
244
245 return make_pair(first1, first2);
246 }
247
248 template<class InputIterator1, class InputIterator2>
249 pair<InputIterator1, InputIterator2> mismatch(InputIterator1 first1, InputIterator1 last1,
250 InputIterator2 first2, InputIterator2 last2)
251 {
252 while (first1 != last1 && first2 != last2 && *first1 == *first2)
253 {
254 ++first1;
255 ++first2;
256 }
257
258 return make_pair(first1, first2);
259 }
260
261 template<class InputIterator1, class InputIterator2, class BinaryPredicate>
262 pair<InputIterator1, InputIterator2> mismatch(InputIterator1 first1, InputIterator1 last1,
263 InputIterator2 first2, InputIterator2 last2,
264 BinaryPredicate pred)
265 {
266 while (first1 != last1 && first2 != last2 && pred(*first1, *first2))
267 {
268 ++first1;
269 ++first2;
270 }
271
272 return make_pair(first1, first2);
273 }
274
275 /**
276 * 25.2.11, equal:
277 */
278
279 template<class InputIterator1, class InputIterator2>
280 bool equal(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2)
281 {
282 while (first1 != last1)
283 {
284 if (*first1++ != *first2++)
285 return false;
286 }
287
288 return true;
289 }
290
291 template<class InputIterator1, class InputIterator2>
292 bool equal(InputIterator1 first1, InputIterator1 last1,
293 InputIterator2 first2, InputIterator2 last2)
294 {
295 while (first1 != last1 && first2 != last2)
296 {
297 if (*first1++ != *first2++)
298 return false;
299 }
300
301 return true;
302 }
303
304 template<class InputIterator1, class InputIterator2, class BinaryPredicate>
305 bool equal(InputIterator1 first1, InputIterator1 last1,
306 InputIterator2 first2, BinaryPredicate pred)
307 {
308 while (first1 != last1)
309 {
310 if (!pred(*first1++, *first2++))
311 return false;
312 }
313
314 return true;
315 }
316
317 template<class InputIterator1, class InputIterator2, class BinaryPredicate>
318 bool equal(InputIterator1 first1, InputIterator1 last1,
319 InputIterator2 first2, InputIterator2 last2,
320 BinaryPredicate pred)
321 {
322 while (first1 != last1 && first2 != last2)
323 {
324 if (!pred(*first1++, *first2++))
325 return false;
326 }
327
328 return true;
329 }
330
331 /**
332 * 25.2.12, is_permutation:
333 */
334
335 // TODO: implement
336
337 /**
338 * 25.2.13, search:
339 */
340
341 // TODO: implement
342
343 /**
344 * 25.3, mutating sequence operations:
345 */
346
347 /**
348 * 25.3.1, copy:
349 */
350
351 template<class InputIterator, class OutputIterator>
352 OutputIterator copy(InputIterator first, InputIterator last, OutputIterator result)
353 {
354 while (first != last)
355 *result++ = *first++;
356
357 return result;
358 }
359
360 template<class InputIterator, class Size, class OutputIterator>
361 OutputIterator copy_n(InputIterator first, Size count, OutputIterator result)
362 {
363 for (Size i = 0; i < count; ++i, ++first, ++result)
364 *result = *first;
365
366 return result;
367 }
368
369 template<class InputIterator, class OutputIterator, class Predicate>
370 OutputIterator copy_if(InputIterator first, InputIterator last,
371 OutputIterator result, Predicate pred)
372 {
373 while (first != last)
374 {
375 if (pred(*first))
376 *result++ = *first;
377 ++first;
378 }
379
380 return result;
381 }
382
383 template<class BidirectionalIterator1, class BidirectionalIterator2>
384 BidirectionalIterator2 copy_backward(BidirectionalIterator1 first, BidirectionalIterator1 last,
385 BidirectionalIterator2 result)
386 {
387 // Note: We're copying [first, last) so we need to skip the initial value of last.
388 while (last-- != first)
389 *result-- = *last;
390
391 return result;
392 }
393
394 /**
395 * 25.3.2, move:
396 */
397
398 template<class InputIterator, class OutputIterator>
399 OutputIterator move(InputIterator first, InputIterator last, OutputIterator result)
400 {
401 while (first != last)
402 *result++ = move(first++);
403
404 return result;
405 }
406
407 template<class BidirectionalIterator1, class BidirectionalIterator2>
408 BidirectionalIterator2 move_backward(BidirectionalIterator1 first, BidirectionalIterator1 last,
409 BidirectionalIterator2 result)
410 {
411 // Note: We're copying [first, last) so we need to skip the initial value of last.
412 while (last-- != first)
413 *result-- = move(*last);
414 }
415
416 /**
417 * 25.3.3, swap:
418 */
419
420 template<class ForwardIterator1, class ForwardIterator2>
421 ForwardIterator2 swap_ranges(ForwardIterator1 first1, ForwardIterator1 last1,
422 ForwardIterator2 first2)
423 {
424 while (first1 != last1)
425 swap(*first1++, *first2++);
426
427 return first2;
428 }
429
430 template<class ForwardIterator1, class ForwardIterator2>
431 void iter_swap(ForwardIterator1 iter1, ForwardIterator2 iter2)
432 {
433 swap(*iter1, *iter2);
434 }
435
436 /**
437 * 25.3.4, transform:
438 */
439
440 template<class InputIterator, class OutputIterator, class UnaryOperation>
441 OutputIterator transform(InputIterator first, InputIterator last,
442 OutputIterator result, UnaryOperation op)
443 {
444 while (first != last)
445 *result++ = op(*first++);
446 }
447
448 template<class InputIterator1, class InputIterator2,
449 class OutputIterator, class BinaryOperation>
450 OutputIterator transform(InputIterator1 first1, InputIterator1 last1,
451 InputIterator2 first2, OutputIterator result,
452 BinaryOperation op)
453 {
454 while (first1 != last1)
455 *result++ = op(*first1++, *first2++);
456 }
457
458 /**
459 * 25.3.5, replace:
460 */
461
462 template<class ForwardIterator, class T>
463 void replace(ForwardIterator first, ForwardIterator last,
464 const T& old_value, const T& new_value)
465 {
466 while (first != last)
467 {
468 if (*first == old_value)
469 *first = new_value;
470 ++first;
471 }
472 }
473
474 template<class ForwardIterator, class Predicate, class T>
475 void replace_if(ForwardIterator first, ForwardIterator last,
476 Predicate pred, const T& new_value)
477 {
478 while (first != last)
479 {
480 if (pred(*first))
481 *first = new_value;
482 ++first;
483 }
484 }
485
486 template<class InputIterator, class OutputIterator, class T>
487 OutputIterator replace_copy(InputIterator first, InputIterator last,
488 OutputIterator result, const T& old_value,
489 const T& new_value)
490 {
491 while (first != last)
492 {
493 if (*first == old_value)
494 *result = new_value;
495 else
496 *result = *first;
497
498 ++first;
499 ++result;
500 }
501 }
502
503 template<class InputIterator, class OutputIterator, class Predicate, class T>
504 OutputIterator replace_copy_if(InputIterator first, InputIterator last,
505 OutputIterator result, Predicate pred,
506 const T& new_value)
507 {
508 while (first != last)
509 {
510 if (pred(*first))
511 *result = new_value;
512 else
513 *result = *first;
514
515 ++first;
516 ++result;
517 }
518 }
519
520 /**
521 * 25.3.6, fill:
522 */
523
524 template<class ForwardIterator, class T>
525 void fill(ForwardIterator first, ForwardIterator last, const T& value)
526 {
527 while (first != last)
528 *first++ = value;
529 }
530
531 template<class InputIterator, class Size, class T>
532 void fill_n(InputIterator first, Size count, const T& value)
533 {
534 for (Size i = 0; i < count; ++i)
535 *first++ = value;
536 }
537
538 /**
539 * 25.3.7, generate:
540 */
541
542 template<class ForwardIterator, class Generator>
543 void generate(ForwardIterator first, ForwardIterator last,
544 Generator gen)
545 {
546 while (first != last)
547 *first++ = gen();
548 }
549
550 template<class OutputIterator, class Size, class Generator>
551 void generate(OutputIterator first, Size count, Generator gen)
552 {
553 for (Size i = 0; i < count; ++i)
554 *first++ = gen();
555 }
556
557 /**
558 * 25.3.8, remove:
559 */
560
561 template<class ForwardIterator, class T>
562 ForwardIterator remove(ForwardIterator first, ForwardIterator last,
563 const T& value)
564 {
565 auto it = first;
566 while (it != last)
567 {
568 if (*it != value)
569 *first++ = move(*it);
570 }
571
572 return first;
573 }
574
575 template<class ForwardIterator, class Predicate>
576 ForwardIterator remove_if(ForwardIterator first, ForwardIterator last,
577 Predicate pred)
578 {
579 auto it = first;
580 while (it != last)
581 {
582 if (!pred(*it))
583 *first++ = move(*it);
584 }
585
586 return first;
587 }
588
589 template<class InputIterator, class OutputIterator, class T>
590 OutputIterator remove_copy(InputIterator first, InputIterator last,
591 OutputIterator result, const T& value)
592 {
593 while (first != last)
594 {
595 if (*first != value)
596 *result++ = *first;
597 ++first;
598 }
599
600 return result;
601 }
602
603 template<class InputIterator, class OutputIterator, class Predicate>
604 OutputIterator remove_copy_if(InputIterator first, InputIterator last,
605 OutputIterator result, Predicate pred)
606 {
607 while (first != last)
608 {
609 if (!pred(*first))
610 *result++ = *first;
611 ++first;
612 }
613
614 return result;
615 }
616
617 /**
618 * 25.3.9, unique:
619 */
620
621 // TODO: implement
622
623 /**
624 * 25.3.10, reverse:
625 */
626
627 template<class BidirectionalIterator>
628 void reverse(BidirectionalIterator first, BidirectionalIterator last)
629 {
630 if (first == last)
631 return;
632 auto mid_count = (last - first) / 2;
633
634 --last;
635 for (decltype(mid_count) i = 0; i < mid_count; ++i)
636 iter_swap(first++, last--);
637 }
638
639 template<class BidirectionalIterator, class OutputIterator>
640 OutputIterator reverse_copy(BidirectionalIterator first,
641 BidirectionalIterator last,
642 OutputIterator result)
643 {
644 while (--last != first)
645 *result++ = *last;
646 }
647
648 /**
649 * 25.3.11, rotate:
650 */
651
652 // TODO: implement
653
654 /**
655 * 25.3.12, shuffle:
656 */
657
658 // TODO: implement
659
660 /**
661 * 25.3.13, partitions:
662 */
663
664 // TODO: implement
665
666 /**
667 * 25.4, sorting and related operations:
668 */
669
670 /**
671 * 25.4.1, sorting:
672 */
673
674 /**
675 * 25.4.1.1, sort:
676 */
677
678 template<class RandomAccessIterator, class Compare>
679 void make_heap(RandomAccessIterator, RandomAccessIterator,
680 Compare);
681
682 template<class RandomAccessIterator, class Compare>
683 void sort_heap(RandomAccessIterator, RandomAccessIterator,
684 Compare);
685
686 template<class RandomAccessIterator>
687 void sort(RandomAccessIterator first, RandomAccessIterator last)
688 {
689 using value_type = typename iterator_traits<RandomAccessIterator>::value_type;
690
691 sort(first, last, less<value_type>{});
692 }
693
694 template<class RandomAccessIterator, class Compare>
695 void sort(RandomAccessIterator first, RandomAccessIterator last,
696 Compare comp)
697 {
698 /**
699 * Note: This isn't the most effective approach,
700 * but since we already have these two functions
701 * and they satisfy asymptotic limitations
702 * imposed by the standard, we're using them at
703 * the moment. Might be good to change it to qsort
704 * or merge sort later.
705 */
706
707 make_heap(first, last, comp);
708 sort_heap(first, last, comp);
709 }
710
711 /**
712 * 25.4.1.2, stable_sort:
713 */
714
715 // TODO: implement
716
717 /**
718 * 25.4.1.3, partial_sort:
719 */
720
721 // TODO: implement
722
723 /**
724 * 25.4.1.4, partial_sort_copy:
725 */
726
727 // TODO: implement
728
729 /**
730 * 25.4.1.5, is_sorted:
731 */
732
733 template<class ForwardIterator>
734 bool is_sorted(ForwardIterator first, ForwardIterator last)
735 {
736 return is_sorted_until(first, last) == last;
737 }
738
739 template<class ForwardIterator, class Comp>
740 bool is_sorted(ForwardIterator first, ForwardIterator last,
741 Comp comp)
742 {
743 return is_sorted_until(first, last, comp) == last;
744 }
745
746 template<class ForwardIterator>
747 ForwardIterator is_sorted_until(ForwardIterator first, ForwardIterator last)
748 {
749 if (distance(first, last) < 2)
750 return last;
751
752 while (first != last)
753 {
754 if (*first > *(++first))
755 return first;
756 }
757
758 return last;
759 }
760
761 template<class ForwardIterator, class Comp>
762 ForwardIterator is_sorted_until(ForwardIterator first, ForwardIterator last,
763 Comp comp)
764 {
765 if (distance(first, last) < 2)
766 return last;
767
768 while (first != last)
769 {
770 if (!comp(*first, *(++first)))
771 return first;
772 }
773
774 return last;
775 }
776
777 /**
778 * 25.4.2, nth_element:
779 */
780
781 // TODO: implement
782
783 /**
784 * 25.4.3, binary search:
785 */
786
787 /**
788 * 25.4.3.1, lower_bound
789 */
790
791 // TODO: implement
792
793 /**
794 * 25.4.3.2, upper_bound
795 */
796
797 // TODO: implement
798
799 /**
800 * 25.4.3.3, equal_range:
801 */
802
803 // TODO: implement
804
805 /**
806 * 25.4.3.4, binary_search:
807 */
808
809 // TODO: implement
810
811 /**
812 * 25.4.4, merge:
813 */
814
815 // TODO: implement
816
817 /**
818 * 25.4.5, set operations on sorted structures:
819 */
820
821 /**
822 * 25.4.5.1, includes:
823 */
824
825 // TODO: implement
826
827 /**
828 * 25.4.5.2, set_union:
829 */
830
831 // TODO: implement
832
833 /**
834 * 25.4.5.3, set_intersection:
835 */
836
837 // TODO: implement
838
839 /**
840 * 25.4.5.4, set_difference:
841 */
842
843 // TODO: implement
844
845 /**
846 * 25.4.5.5, set_symmetric_difference:
847 */
848
849 // TODO: implement
850
851 /**
852 * 25.4.6, heap operations:
853 */
854
855 namespace aux
856 {
857 template<class T>
858 T heap_parent(T idx)
859 {
860 return (idx - 1) / 2;
861 }
862
863 template<class T>
864 T heap_left_child(T idx)
865 {
866 return 2 * idx + 1;
867 }
868
869 template<class T>
870 T heap_right_child(T idx)
871 {
872 return 2 * idx + 2;
873 }
874
875 template<class RandomAccessIterator, class Size, class Compare>
876 void correct_children(RandomAccessIterator first,
877 Size idx, Size count, Compare comp)
878 {
879 using aux::heap_left_child;
880 using aux::heap_right_child;
881
882 auto left = heap_left_child(idx);
883 auto right = heap_right_child(idx);
884
885 bool left_incorrect{comp(first[idx], first[left])};
886 bool right_incorrect{comp(first[idx], first[right])};
887 while ((left < count && left_incorrect) ||
888 (right < count && right_incorrect))
889 {
890 if (right >= count || (left_incorrect && comp(first[right], first[left])))
891 {
892 swap(first[idx], first[left]);
893
894 idx = left;
895 }
896 else if (right < count && right_incorrect)
897 {
898 swap(first[idx], first[right]);
899
900 idx = right;
901 } // Else should not happen because of the while condition.
902
903 left = heap_left_child(idx);
904 right = heap_right_child(idx);
905
906 left_incorrect = comp(first[idx], first[left]);
907 right_incorrect = comp(first[idx], first[right]);
908 }
909 }
910 }
911
912 /**
913 * 25.4.6.1, push_heap:
914 */
915
916 template<class RandomAccessIterator>
917 void push_heap(RandomAccessIterator first,
918 RandomAccessIterator last)
919 {
920 using value_type = typename iterator_traits<RandomAccessIterator>::value_type;
921
922 push_heap(first, last, less<value_type>{});
923 }
924
925 template<class RandomAccessIterator, class Compare>
926 void push_heap(RandomAccessIterator first,
927 RandomAccessIterator last,
928 Compare comp)
929 {
930 using aux::heap_parent;
931
932 auto count = distance(first, last);
933 if (count <= 1)
934 return;
935
936 auto idx = count - 1;
937 auto parent = heap_parent(idx);
938 while (idx > 0 && comp(first[parent], first[idx]))
939 {
940 swap(first[idx], first[parent]);
941
942 idx = parent;
943 parent = heap_parent(idx);
944 }
945 }
946
947 /**
948 * 25.4.6.2, pop_heap:
949 */
950
951 template<class RandomAccessIterator>
952 void pop_heap(RandomAccessIterator first,
953 RandomAccessIterator last)
954 {
955 using value_type = typename iterator_traits<RandomAccessIterator>::value_type;
956
957 pop_heap(first, last, less<value_type>{});
958 }
959
960 template<class RandomAccessIterator, class Compare>
961 void pop_heap(RandomAccessIterator first,
962 RandomAccessIterator last,
963 Compare comp)
964 {
965 auto count = distance(first, last);
966 if (count <= 1)
967 return;
968
969 swap(first[0], first[count - 1]);
970 aux::correct_children(first, decltype(count){}, count - 2, comp);
971 }
972
973 /**
974 * 25.4.6.3, make_heap:
975 */
976
977 template<class RandomAccessIterator>
978 void make_heap(RandomAccessIterator first,
979 RandomAccessIterator last)
980 {
981 using value_type = typename iterator_traits<RandomAccessIterator>::value_type;
982
983 make_heap(first, last, less<value_type>{});
984 }
985
986 template<class RandomAccessIterator, class Compare>
987 void make_heap(RandomAccessIterator first,
988 RandomAccessIterator last,
989 Compare comp)
990 {
991 auto count = distance(first, last);
992 if (count <= 1)
993 return;
994
995 for (auto i = count; i > 0; --i)
996 {
997 auto idx = i - 1;
998
999 aux::correct_children(first, idx, count, comp);
1000 }
1001 }
1002
1003 /**
1004 * 25.4.6.4, sort_heap:
1005 */
1006
1007 template<class RandomAccessIterator>
1008 void sort_heap(RandomAccessIterator first,
1009 RandomAccessIterator last)
1010 {
1011 using value_type = typename iterator_traits<RandomAccessIterator>::value_type;
1012
1013 sort_heap(first, last, less<value_type>{});
1014 }
1015
1016 template<class RandomAccessIterator, class Compare>
1017 void sort_heap(RandomAccessIterator first,
1018 RandomAccessIterator last,
1019 Compare comp)
1020 {
1021 while (first != last)
1022 pop_heap(first, last--, comp);
1023 }
1024
1025 /**
1026 * 25.4.6.5, is_heap:
1027 */
1028
1029 template<class RandomAccessIterator>
1030 auto is_heap_until(RandomAccessIterator first, RandomAccessIterator last)
1031 {
1032 using value_type = typename iterator_traits<RandomAccessIterator>::value_type;
1033
1034 return is_heap_until(first, last, less<value_type>{});
1035 }
1036
1037 template<class RandomAccessIterator, class Compare>
1038 auto is_heap_until(RandomAccessIterator first, RandomAccessIterator last,
1039 Compare comp)
1040 {
1041 using aux::heap_left_child;
1042 using aux::heap_right_child;
1043
1044 auto count = distance(first, last);
1045 if (count < 2)
1046 return last;
1047
1048 auto res = first;
1049 for (decltype(count) idx = 0; idx < count; ++idx)
1050 {
1051 auto left = heap_left_child(idx);
1052 auto right = heap_right_child(idx);
1053
1054 if (left < count && comp(first[idx], first[left]))
1055 return res;
1056 if (right < count && comp(first[idx], first[right]))
1057 return res;
1058
1059 ++res;
1060 }
1061
1062 return res;
1063 }
1064
1065 template<class RandomAccessIterator>
1066 bool is_heap(RandomAccessIterator first, RandomAccessIterator last)
1067 {
1068 return is_heap_until(first, last) == last;
1069 }
1070
1071 template<class RandomAccessIterator, class Compare>
1072 bool is_heap(RandomAccessIterator first, RandomAccessIterator last,
1073 Compare comp)
1074 {
1075 return is_heap_until(first, last, comp) == last;
1076 }
1077
1078 /**
1079 * 25.4.7, minimum and maximum:
1080 * // TODO: implement container versions when we have
1081 * numeric limits and min/max element
1082 * // TODO: versions with comparators
1083 * // TODO: minmax
1084 */
1085
1086 template<class T>
1087 constexpr const T& min(const T& lhs, const T& rhs)
1088 {
1089 return (lhs < rhs) ? lhs : rhs;
1090 }
1091
1092 template<class T>
1093 constexpr const T& max(const T& lhs, const T& rhs)
1094 {
1095 return (lhs > rhs) ? lhs : rhs;
1096 }
1097
1098 /**
1099 * 25.4.8, lexicographical comparison:
1100 */
1101
1102 template<class InputIterator1, class InputIterator2>
1103 bool lexicographical_compare(InputIterator1 first1,
1104 InputIterator1 last1,
1105 InputIterator2 first2,
1106 InputIterator2 last2)
1107 {
1108 /**
1109 * *first1 and *first2 can have different types
1110 * so we use a transparent comparator.
1111 */
1112 return lexicographical_compare(
1113 first1, last1, first2, last2,
1114 less<void>{}
1115 );
1116 }
1117
1118 template<class InputIterator1, class InputIterator2, class Compare>
1119 bool lexicographical_compare(InputIterator1 first1,
1120 InputIterator1 last1,
1121 InputIterator2 first2,
1122 InputIterator2 last2,
1123 Compare comp)
1124 {
1125 while ((first1 != last1) && (first2 != last2))
1126 {
1127 if (comp(*first1, *first2))
1128 return true;
1129 if (comp(*first2, *first1))
1130 return false;
1131
1132 ++first1;
1133 ++first2;
1134 }
1135
1136 /**
1137 * Up until now they are same, so we have to check
1138 * if we reached the end on one.
1139 */
1140 return (first1 == last1) && (first2 != last2);
1141 }
1142
1143 /**
1144 * 25.4.9, permutation generators:
1145 */
1146
1147 // TODO: implement
1148}
1149
1150#endif
Note: See TracBrowser for help on using the repository browser.