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

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

cpp: added heap related algorithms

  • Property mode set to 100644
File size: 26.0 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 // TODO: Function has to be MoveConstructible
91 template<class InputIterator, class Function>
92 Function for_each(InputIterator first, InputIterator last, Function f)
93 {
94 while (first != last)
95 f(*first++);
96
97 return move(f);
98 }
99
100 /**
101 * 25.2.5, find:
102 */
103
104 template<class InputIterator, class T>
105 InputIterator find(InputIterator first, InputIterator last, const T& value)
106 {
107 while (first != last)
108 {
109 if (*first == value)
110 return first;
111 ++first;
112 }
113
114 return last;
115 }
116
117 template<class InputIterator, class Predicate>
118 InputIterator find_if(InputIterator first, InputIterator last, Predicate pred)
119 {
120 while (first != last)
121 {
122 if (pred(*first))
123 return first;
124 ++first;
125 }
126
127 return last;
128 }
129
130 template<class InputIterator, class Predicate>
131 InputIterator find_if_not(InputIterator first, InputIterator last, Predicate pred)
132 {
133 while (first != last)
134 {
135 if (!pred(*first))
136 return first;
137 ++first;
138 }
139
140 return last;
141 }
142
143 /**
144 * 25.2.6, find_end:
145 */
146
147 // TODO: implement
148
149 /**
150 * 25.2.7, find_first:
151 */
152
153 // TODO: implement
154
155 /**
156 * 25.2.8, adjacent_find:
157 */
158
159 template<class ForwardIterator>
160 ForwardIterator adjacent_find(ForwardIterator first, ForwardIterator last)
161 {
162 while (first != last)
163 {
164 if (*first == *(first + 1))
165 return first;
166 ++first;
167 }
168
169 return last;
170 }
171
172 template<class ForwardIterator, class Predicate>
173 ForwardIterator adjacent_find(ForwardIterator first, ForwardIterator last, Predicate pred)
174 {
175 while (first != last)
176 {
177 if (pred(*first, *(first + 1)))
178 return first;
179 ++first;
180 }
181
182 return last;
183 }
184
185 /**
186 * 25.2.9, count:
187 */
188
189 template<class InputIterator, class T>
190 typename iterator_traits<InputIterator>::difference_type
191 count(InputIterator first, InputIterator last, const T& value)
192 {
193 typename iterator_traits<InputIterator>::difference_type cnt{};
194
195 while (first != last)
196 {
197 if (*first++ == value)
198 ++cnt;
199 }
200
201 return cnt;
202 }
203
204 template<class InputIterator, class Predicate>
205 typename iterator_traits<InputIterator>::difference_type
206 count(InputIterator first, InputIterator last, Predicate pred)
207 {
208 typename iterator_traits<InputIterator>::difference_type cnt{};
209
210 while (first != last)
211 {
212 if (pred(*first++))
213 ++cnt;
214 }
215
216 return cnt;
217 }
218
219 /**
220 * 25.2.10, mismatch:
221 */
222
223 template<class InputIterator1, class InputIterator2>
224 pair<InputIterator1, InputIterator2> mismatch(InputIterator1 first1, InputIterator1 last1,
225 InputIterator2 first2)
226 {
227 while (first1 != last1 && *first1++ == *first2++)
228 { /* DUMMY BODY */ }
229
230 return make_pair(first1, first2);
231 }
232
233 template<class InputIterator1, class InputIterator2, class BinaryPredicate>
234 pair<InputIterator1, InputIterator2> mismatch(InputIterator1 first1, InputIterator1 last1,
235 InputIterator2 first2, BinaryPredicate pred)
236 {
237 while (first1 != last1 && pred(*first1++, *first2++))
238 { /* DUMMY BODY */ }
239
240 return make_pair(first1, first2);
241 }
242
243 template<class InputIterator1, class InputIterator2>
244 pair<InputIterator1, InputIterator2> mismatch(InputIterator1 first1, InputIterator1 last1,
245 InputIterator2 first2, InputIterator2 last2)
246 {
247 while (first1 != last1 && first2 != last2 && *first1++ == *first2++)
248 { /* DUMMY BODY */ }
249
250 return make_pair(first1, first2);
251 }
252
253 template<class InputIterator1, class InputIterator2, class BinaryPredicate>
254 pair<InputIterator1, InputIterator2> mismatch(InputIterator1 first1, InputIterator1 last1,
255 InputIterator2 first2, InputIterator2 last2,
256 BinaryPredicate pred)
257 {
258 while (first1 != last1 && first2 != last2 && pred(*first1++, *first2++))
259 { /* DUMMY BODY */ }
260
261 return make_pair(first1, first2);
262 }
263
264 /**
265 * 25.2.11, equal:
266 */
267
268 template<class InputIterator1, class InputIterator2>
269 bool equal(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2)
270 {
271 auto last2 = first2 + (last1 - first1);
272
273 return equal(first1, last1, first2, last2);
274 }
275
276 template<class InputIterator1, class InputIterator2>
277 bool equal(InputIterator1 first1, InputIterator1 last1,
278 InputIterator2 first2, InputIterator2 last2)
279 {
280 if ((last1 - first1) != (last2 - first2))
281 return false;
282
283 while (first1 != last1)
284 {
285 if (*first1++ != *first2++)
286 return false;
287 }
288
289 return true;
290 }
291
292 template<class InputIterator1, class InputIterator2, class BinaryPredicate>
293 bool equal(InputIterator1 first1, InputIterator1 last1,
294 InputIterator2 first2, BinaryPredicate pred)
295 {
296 auto last2 = first2 + (last1 - first1);
297
298 return equal(first1, last1, first2, last2, pred);
299 }
300
301 template<class InputIterator1, class InputIterator2, class BinaryPredicate>
302 bool equal(InputIterator1 first1, InputIterator1 last1,
303 InputIterator2 first2, InputIterator2 last2,
304 BinaryPredicate pred)
305 {
306 if ((last1 - first1) != (last2 - first2))
307 return false;
308
309 while (first1 != last1)
310 {
311 if (!pred(*first1++, *first2++))
312 return false;
313 }
314
315 return true;
316 }
317
318 /**
319 * 25.2.12, is_permutation:
320 */
321
322 // TODO: implement
323
324 /**
325 * 25.2.13, search:
326 */
327
328 // TODO: implement
329
330 /**
331 * 25.3, mutating sequence operations:
332 */
333
334 /**
335 * 25.3.1, copy:
336 */
337
338 template<class InputIterator, class OutputIterator>
339 OutputIterator copy(InputIterator first, InputIterator last, OutputIterator result)
340 {
341 while (first != last)
342 *result++ = *first++;
343
344 return result;
345 }
346
347 template<class InputIterator, class Size, class OutputIterator>
348 OutputIterator copy_n(InputIterator first, Size count, OutputIterator result)
349 {
350 for (Size i = 0; i < count; ++i, ++first, ++result)
351 *result = *first;
352
353 return result;
354 }
355
356 template<class InputIterator, class OutputIterator, class Predicate>
357 OutputIterator copy_if(InputIterator first, InputIterator last,
358 OutputIterator result, Predicate pred)
359 {
360 while (first != last)
361 {
362 if (pred(*first))
363 *result++ = *first;
364 ++first;
365 }
366
367 return result;
368 }
369
370 template<class BidirectionalIterator1, class BidirectionalIterator2>
371 BidirectionalIterator2 copy_backward(BidirectionalIterator1 first, BidirectionalIterator1 last,
372 BidirectionalIterator2 result)
373 {
374 // Note: We're copying [first, last) so we need to skip the initial value of last.
375 while (last-- != first)
376 *result-- = *last;
377
378 return result;
379 }
380
381 /**
382 * 25.3.2, move:
383 */
384
385 template<class InputIterator, class OutputIterator>
386 OutputIterator move(InputIterator first, InputIterator last, OutputIterator result)
387 {
388 while (first != last)
389 *result++ = move(first++);
390
391 return result;
392 }
393
394 template<class BidirectionalIterator1, class BidirectionalIterator2>
395 BidirectionalIterator2 move_backward(BidirectionalIterator1 first, BidirectionalIterator1 last,
396 BidirectionalIterator2 result)
397 {
398 // Note: We're copying [first, last) so we need to skip the initial value of last.
399 while (last-- != first)
400 *result-- = move(*last);
401 }
402
403 /**
404 * 25.3.3, swap:
405 */
406
407 template<class ForwardIterator1, class ForwardIterator2>
408 ForwardIterator2 swap_ranges(ForwardIterator1 first1, ForwardIterator1 last1,
409 ForwardIterator2 first2)
410 {
411 while (first1 != last1)
412 swap(*first1++, *first2++);
413
414 return first2;
415 }
416
417 template<class ForwardIterator1, class ForwardIterator2>
418 void iter_swap(ForwardIterator1 iter1, ForwardIterator2 iter2)
419 {
420 swap(*iter1, *iter2);
421 }
422
423 /**
424 * 25.3.4, transform:
425 */
426
427 template<class InputIterator, class OutputIterator, class UnaryOperation>
428 OutputIterator transform(InputIterator first, InputIterator last,
429 OutputIterator result, UnaryOperation op)
430 {
431 while (first != last)
432 *result++ = op(*first++);
433 }
434
435 template<class InputIterator1, class InputIterator2,
436 class OutputIterator, class BinaryOperation>
437 OutputIterator transform(InputIterator1 first1, InputIterator1 last1,
438 InputIterator2 first2, OutputIterator result,
439 BinaryOperation op)
440 {
441 while (first1 != last1)
442 *result++ = op(*first1++, *first2++);
443 }
444
445 /**
446 * 25.3.5, replace:
447 */
448
449 template<class ForwardIterator, class T>
450 void replace(ForwardIterator first, ForwardIterator last,
451 const T& old_value, const T& new_value)
452 {
453 while (first != last)
454 {
455 if (*first == old_value)
456 *first = new_value;
457 ++first;
458 }
459 }
460
461 template<class ForwardIterator, class Predicate, class T>
462 void replace_if(ForwardIterator first, ForwardIterator last,
463 Predicate pred, const T& new_value)
464 {
465 while (first != last)
466 {
467 if (pred(*first))
468 *first = new_value;
469 ++first;
470 }
471 }
472
473 template<class InputIterator, class OutputIterator, class T>
474 OutputIterator replace_copy(InputIterator first, InputIterator last,
475 OutputIterator result, const T& old_value,
476 const T& new_value)
477 {
478 while (first != last)
479 {
480 if (*first == old_value)
481 *result = new_value;
482 else
483 *result = *first;
484
485 ++first;
486 ++result;
487 }
488 }
489
490 template<class InputIterator, class OutputIterator, class Predicate, class T>
491 OutputIterator replace_copy_if(InputIterator first, InputIterator last,
492 OutputIterator result, Predicate pred,
493 const T& new_value)
494 {
495 while (first != last)
496 {
497 if (pred(*first))
498 *result = new_value;
499 else
500 *result = *first;
501
502 ++first;
503 ++result;
504 }
505 }
506
507 /**
508 * 25.3.6, fill:
509 */
510
511 template<class ForwardIterator, class T>
512 void fill(ForwardIterator first, ForwardIterator last, const T& value)
513 {
514 while (first != last)
515 *first++ = value;
516 }
517
518 template<class InputIterator, class Size, class T>
519 void fill_n(InputIterator first, Size count, const T& value)
520 {
521 for (Size i = 0; i < count; ++i)
522 *first++ = value;
523 }
524
525 /**
526 * 25.3.7, generate:
527 */
528
529 template<class ForwardIterator, class Generator>
530 void generate(ForwardIterator first, ForwardIterator last,
531 Generator gen)
532 {
533 while (first != last)
534 *first++ = gen();
535 }
536
537 template<class OutputIterator, class Size, class Generator>
538 void generate(OutputIterator first, Size count, Generator gen)
539 {
540 for (Size i = 0; i < count; ++i)
541 *first++ = gen();
542 }
543
544 /**
545 * 25.3.8, remove:
546 */
547
548 template<class ForwardIterator, class T>
549 ForwardIterator remove(ForwardIterator first, ForwardIterator last,
550 const T& value)
551 {
552 auto it = first;
553 while (it != last)
554 {
555 if (*it != value)
556 *first++ = move(*it);
557 }
558
559 return first;
560 }
561
562 template<class ForwardIterator, class Predicate>
563 ForwardIterator remove_if(ForwardIterator first, ForwardIterator last,
564 Predicate pred)
565 {
566 auto it = first;
567 while (it != last)
568 {
569 if (!pred(*it))
570 *first++ = move(*it);
571 }
572
573 return first;
574 }
575
576 template<class InputIterator, class OutputIterator, class T>
577 OutputIterator remove_copy(InputIterator first, InputIterator last,
578 OutputIterator result, const T& value)
579 {
580 while (first != last)
581 {
582 if (*first != value)
583 *result++ = *first;
584 ++first;
585 }
586
587 return result;
588 }
589
590 template<class InputIterator, class OutputIterator, class Predicate>
591 OutputIterator remove_copy_if(InputIterator first, InputIterator last,
592 OutputIterator result, Predicate pred)
593 {
594 while (first != last)
595 {
596 if (!pred(*first))
597 *result++ = *first;
598 ++first;
599 }
600
601 return result;
602 }
603
604 /**
605 * 25.3.9, unique:
606 */
607
608 // TODO: implement
609
610 /**
611 * 25.3.10, reverse:
612 */
613
614 template<class BidirectionalIterator>
615 void reverse(BidirectionalIterator first, BidirectionalIterator last)
616 {
617 if (first == last)
618 return;
619 auto mid_count = (last - first) / 2;
620
621 --last;
622 for (decltype(mid_count) i = 0; i < mid_count; ++i)
623 iter_swap(first++, last--);
624 }
625
626 template<class BidirectionalIterator, class OutputIterator>
627 OutputIterator reverse_copy(BidirectionalIterator first,
628 BidirectionalIterator last,
629 OutputIterator result)
630 {
631 while (--last != first)
632 *result++ = *last;
633 }
634
635 /**
636 * 25.3.11, rotate:
637 */
638
639 // TODO: implement
640
641 /**
642 * 25.3.12, shuffle:
643 */
644
645 // TODO: implement
646
647 /**
648 * 25.3.13, partitions:
649 */
650
651 // TODO: implement
652
653 /**
654 * 25.4, sorting and related operations:
655 */
656
657 /**
658 * 25.4.1, sorting:
659 */
660
661 /**
662 * 25.4.1.1, sort:
663 */
664
665 // TODO: implement
666
667 /**
668 * 25.4.1.2, stable_sort:
669 */
670
671 // TODO: implement
672
673 /**
674 * 25.4.1.3, partial_sort:
675 */
676
677 // TODO: implement
678
679 /**
680 * 25.4.1.4, partial_sort_copy:
681 */
682
683 // TODO: implement
684
685 /**
686 * 25.4.1.5, is_sorted:
687 */
688
689 template<class ForwardIterator>
690 bool is_sorted(ForwardIterator first, ForwardIterator last)
691 {
692 return is_sorted_until(first, last) == last;
693 }
694
695 template<class ForwardIterator, class Comp>
696 bool is_sorted(ForwardIterator first, ForwardIterator last,
697 Comp comp)
698 {
699 return is_sorted_until(first, last, comp) == last;
700 }
701
702 template<class ForwardIterator>
703 ForwardIterator is_sorted_until(ForwardIterator first, ForwardIterator last)
704 {
705 if (distance(first, last) < 2)
706 return last;
707
708 while (first != last)
709 {
710 if (*first > *(++first))
711 return first;
712 }
713
714 return last;
715 }
716
717 template<class ForwardIterator, class Comp>
718 ForwardIterator is_sorted_until(ForwardIterator first, ForwardIterator last,
719 Comp comp)
720 {
721 if (distance(first, last) < 2)
722 return last;
723
724 while (first != last)
725 {
726 if (!comp(*first, *(++first)))
727 return first;
728 }
729
730 return last;
731 }
732
733 /**
734 * 25.4.2, nth_element:
735 */
736
737 // TODO: implement
738
739 /**
740 * 25.4.3, binary search:
741 */
742
743 /**
744 * 25.4.3.1, lower_bound
745 */
746
747 // TODO: implement
748
749 /**
750 * 25.4.3.2, upper_bound
751 */
752
753 // TODO: implement
754
755 /**
756 * 25.4.3.3, equal_range:
757 */
758
759 // TODO: implement
760
761 /**
762 * 25.4.3.4, binary_search:
763 */
764
765 // TODO: implement
766
767 /**
768 * 25.4.4, merge:
769 */
770
771 // TODO: implement
772
773 /**
774 * 25.4.5, set operations on sorted structures:
775 */
776
777 /**
778 * 25.4.5.1, includes:
779 */
780
781 // TODO: implement
782
783 /**
784 * 25.4.5.2, set_union:
785 */
786
787 // TODO: implement
788
789 /**
790 * 25.4.5.3, set_intersection:
791 */
792
793 // TODO: implement
794
795 /**
796 * 25.4.5.4, set_difference:
797 */
798
799 // TODO: implement
800
801 /**
802 * 25.4.5.5, set_symmetric_difference:
803 */
804
805 // TODO: implement
806
807 /**
808 * 25.4.6, heap operations:
809 */
810
811 namespace aux
812 {
813 template<class T>
814 T heap_parent(T idx)
815 {
816 return (idx - 1) / 2;
817 }
818
819 template<class T>
820 T heap_left_child(T idx)
821 {
822 return 2 * idx + 1;
823 }
824
825 template<class T>
826 T heap_right_child(T idx)
827 {
828 return 2 * idx + 2;
829 }
830
831 template<class RandomAccessIterator, class Size, class Compare>
832 void correct_children(RandomAccessIterator first,
833 Size idx, Size count, Compare comp)
834 {
835 using aux::heap_left_child;
836 using aux::heap_right_child;
837
838 auto left = heap_left_child(idx);
839 auto right = heap_right_child(idx);
840
841 bool left_incorrect{comp(first[idx], first[left])};
842 bool right_incorrect{comp(first[idx], first[right])};
843 while ((left < count && left_incorrect) ||
844 (right < count && right_incorrect))
845 {
846 if (right >= count || (left_incorrect && comp(first[right], first[left])))
847 {
848 swap(first[idx], first[left]);
849
850 idx = left;
851 }
852 else if (right < count && right_incorrect)
853 {
854 swap(first[idx], first[right]);
855
856 idx = right;
857 } // Else should not happen because of the while condition.
858
859 left = heap_left_child(idx);
860 right = heap_right_child(idx);
861
862 left_incorrect = comp(first[idx], first[left]);
863 right_incorrect = comp(first[idx], first[right]);
864 }
865 }
866 }
867
868 /**
869 * 25.4.6.1, push_heap:
870 */
871
872 template<class RandomAccessIterator>
873 void push_heap(RandomAccessIterator first,
874 RandomAccessIterator last)
875 {
876 using value_type = typename iterator_traits<RandomAccessIterator>::value_type;
877
878 push_heap(first, last, less<value_type>{});
879 }
880
881 template<class RandomAccessIterator, class Compare>
882 void push_heap(RandomAccessIterator first,
883 RandomAccessIterator last,
884 Compare comp)
885 {
886 using aux::heap_parent;
887
888 auto count = distance(first, last);
889 if (count <= 1)
890 return;
891
892 auto idx = count - 1;
893 auto parent = heap_parent(idx);
894 while (idx > 0 && comp(first[parent], first[idx]))
895 {
896 swap(first[idx], first[parent]);
897
898 idx = parent;
899 parent = heap_parent(idx);
900 }
901 }
902
903 /**
904 * 25.4.6.2, pop_heap:
905 */
906
907 template<class RandomAccessIterator>
908 void pop_heap(RandomAccessIterator first,
909 RandomAccessIterator last)
910 {
911 using value_type = typename iterator_traits<RandomAccessIterator>::value_type;
912
913 pop_heap(first, last, less<value_type>{});
914 }
915
916 template<class RandomAccessIterator, class Compare>
917 void pop_heap(RandomAccessIterator first,
918 RandomAccessIterator last,
919 Compare comp)
920 {
921 auto count = distance(first, last);
922 if (count <= 1)
923 return;
924
925 swap(first[0], first[count - 1]);
926 aux::correct_children(first, decltype(count){}, count - 2, comp);
927 }
928
929 /**
930 * 25.4.6.3, make_heap:
931 */
932
933 template<class RandomAccessIterator>
934 void make_heap(RandomAccessIterator first,
935 RandomAccessIterator last)
936 {
937 using value_type = typename iterator_traits<RandomAccessIterator>::value_type;
938
939 make_heap(first, last, less<value_type>{});
940 }
941
942 template<class RandomAccessIterator, class Compare>
943 void make_heap(RandomAccessIterator first,
944 RandomAccessIterator last,
945 Compare comp)
946 {
947 auto count = distance(first, last);
948 if (count <= 1)
949 return;
950
951 for (auto i = count; i > 0; --i)
952 {
953 auto idx = i - 1;
954
955 aux::correct_children(first, idx, count, comp);
956 }
957 }
958
959 /**
960 * 25.4.6.4, sort_heap:
961 */
962
963 template<class RandomAccessIterator>
964 void sort_heap(RandomAccessIterator first,
965 RandomAccessIterator last)
966 {
967 using value_type = typename iterator_traits<RandomAccessIterator>::value_type;
968
969 sort_heap(first, last, less<value_type>{});
970 }
971
972 template<class RandomAccessIterator, class Compare>
973 void sort_heap(RandomAccessIterator first,
974 RandomAccessIterator last,
975 Compare comp)
976 {
977 while (first != last)
978 pop_heap(first, last--, comp);
979 }
980
981 /**
982 * 25.4.6.5, is_heap:
983 */
984
985 template<class RandomAccessIterator>
986 auto is_heap_until(RandomAccessIterator first, RandomAccessIterator last)
987 {
988 using value_type = typename iterator_traits<RandomAccessIterator>::value_type;
989
990 return is_heap_until(first, last, less<value_type>{});
991 }
992
993 template<class RandomAccessIterator, class Compare>
994 auto is_heap_until(RandomAccessIterator first, RandomAccessIterator last,
995 Compare comp)
996 {
997 using aux::heap_left_child;
998 using aux::heap_right_child;
999
1000 auto count = distance(first, last);
1001 if (count < 2)
1002 return last;
1003
1004 auto res = first;
1005 for (decltype(count) idx = 0; idx < count; ++idx)
1006 {
1007 auto left = heap_left_child(idx);
1008 auto right = heap_right_child(idx);
1009
1010 if (left < count && comp(first[idx], first[left]))
1011 return res;
1012 if (right < count && comp(first[idx], first[right]))
1013 return res;
1014
1015 ++res;
1016 }
1017
1018 return res;
1019 }
1020
1021 template<class RandomAccessIterator>
1022 bool is_heap(RandomAccessIterator first, RandomAccessIterator last)
1023 {
1024 return is_heap_until(first, last) == last;
1025 }
1026
1027 template<class RandomAccessIterator, class Compare>
1028 bool is_heap(RandomAccessIterator first, RandomAccessIterator last,
1029 Compare comp)
1030 {
1031 return is_heap_until(first, last, comp) == last;
1032 }
1033
1034 /**
1035 * 25.4.7, minimum and maximum:
1036 * // TODO: implement container versions when we have
1037 * numeric limits and min/max element
1038 * // TODO: versions with comparators
1039 * // TODO: minmax
1040 */
1041
1042 template<class T>
1043 constexpr const T& min(const T& lhs, const T& rhs)
1044 {
1045 return (lhs < rhs) ? lhs : rhs;
1046 }
1047
1048 template<class T>
1049 constexpr const T& max(const T& lhs, const T& rhs)
1050 {
1051 return (lhs > rhs) ? lhs : rhs;
1052 }
1053
1054 /**
1055 * 25.4.8, lexicographical comparison:
1056 */
1057
1058 // TODO: implement
1059
1060 /**
1061 * 25.4.9, permutation generators:
1062 */
1063
1064 // TODO: implement
1065}
1066
1067#endif
Note: See TracBrowser for help on using the repository browser.