1
/* Copyright (C) 2004 Garrett A. Kajmowicz
2
This file is part of the uClibc++ Library.
3
This library is free software; you can redistribute it and/or
4
modify it under the terms of the GNU Lesser General Public
5
License as published by the Free Software Foundation; either
6
version 2.1 of the License, or (at your option) any later version.
8
This library is distributed in the hope that it will be useful,
9
but WITHOUT ANY WARRANTY; without even the implied warranty of
10
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11
Lesser General Public License for more details.
13
You should have received a copy of the GNU Lesser General Public
14
License along with this library; if not, write to the Free Software
15
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23
#ifndef __STD_HEADER_ALGORITHM
24
#define __STD_HEADER_ALGORITHM 1
26
//Elliminate any previously defined macro
30
#pragma GCC visibility push(default)
34
// subclause _lib.alg.nonmodifying_, non-modifying sequence operations:
35
template<class InputIterator, class Function> _UCXXEXPORT
36
Function for_each(InputIterator first, InputIterator last, Function f)
46
template<class InputIterator, class T> _UCXXEXPORT
47
InputIterator find(InputIterator first, InputIterator last, const T& value)
49
while(first !=last && ! ( *first == value ) ){
55
template<class InputIterator, class Predicate> _UCXXEXPORT
56
InputIterator find_if(InputIterator first, InputIterator last, Predicate pred)
58
while(first !=last && !pred(*first)){
64
template<class ForwardIterator1, class ForwardIterator2> _UCXXEXPORT ForwardIterator1
65
find_end(ForwardIterator1 first1, ForwardIterator1 last1,
66
ForwardIterator2 first2, ForwardIterator2 last2)
68
ForwardIterator1 retval = last1;
69
while(first1 !=last1){
70
ForwardIterator1 temp1(first1);
71
ForwardIterator2 temp2(first2);
72
while(temp2!=last2 && temp1!= last1){
74
break; //Exit while loop
78
if(temp2 == last2){ //Have successfully checked the whole sequence
86
template<class ForwardIterator1, class ForwardIterator2, class BinaryPredicate> _UCXXEXPORT
88
find_end(ForwardIterator1 first1, ForwardIterator1 last1,
89
ForwardIterator2 first2, ForwardIterator2 last2,
92
ForwardIterator1 retval = last1;
93
while(first1 !=last1){
94
ForwardIterator1 temp1(first1);
95
ForwardIterator2 temp2(first2);
96
while(temp2!=last2 && temp1!= last1){
97
if( ! pred(*temp1, *temp2)){
98
break; //Exit while loop
102
if(temp2 == last2){ //Have successfully checked the whole sequence
110
template<class ForwardIterator1, class ForwardIterator2> _UCXXEXPORT
112
find_first_of(ForwardIterator1 first1, ForwardIterator1 last1,
113
ForwardIterator2 first2, ForwardIterator2 last2)
115
while(first1 != last1){
116
ForwardIterator2 temp2(first2);
117
while(temp2 != last2 ){
118
if(*temp2 == first1){
128
template<class ForwardIterator1, class ForwardIterator2, class BinaryPredicate> _UCXXEXPORT
130
find_first_of(ForwardIterator1 first1, ForwardIterator1 last1,
131
ForwardIterator2 first2, ForwardIterator2 last2,
132
BinaryPredicate pred)
134
while(first1 != last1){
135
ForwardIterator2 temp2(first2);
136
while(temp2 != last2 ){
137
if(*temp2 == first1){
147
template<class ForwardIterator> _UCXXEXPORT ForwardIterator
148
adjacent_find(ForwardIterator first, ForwardIterator last)
150
ForwardIterator temp;
151
while(first != last){
162
template<class ForwardIterator, class BinaryPredicate> _UCXXEXPORT
164
adjacent_find(ForwardIterator first, ForwardIterator last, BinaryPredicate pred)
166
ForwardIterator temp;
167
while(first != last){
170
if( pred(*temp, *first)){
178
template<class InputIterator, class T> _UCXXEXPORT
179
typename iterator_traits<InputIterator>::difference_type
180
count(InputIterator first, InputIterator last, const T& value)
182
typename iterator_traits<InputIterator>::difference_type i = 0;
192
template<class InputIterator, class Predicate> _UCXXEXPORT
193
typename iterator_traits<InputIterator>::difference_type
194
count_if(InputIterator first, InputIterator last, Predicate pred)
196
typename iterator_traits<InputIterator>::difference_type i = 0;
206
template<class InputIterator1, class InputIterator2> _UCXXEXPORT
207
pair<InputIterator1, InputIterator2>
208
mismatch(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2)
210
while(first1 != last1){
211
if(*first1 != *first2){
218
pair<InputIterator1, InputIterator2> retval;
219
retval.first = first1;
220
retval.second = first2;
224
template<class InputIterator1, class InputIterator2, class BinaryPredicate> _UCXXEXPORT
225
pair<InputIterator1, InputIterator2>
226
mismatch(InputIterator1 first1, InputIterator1 last1,
227
InputIterator2 first2, BinaryPredicate pred)
229
while(first1 != last1){
230
if( !pred(*first1, *first2) ){
237
pair<InputIterator1, InputIterator2> retval;
238
retval.first = first1;
239
retval.second = first2;
243
template<class InputIterator1, class InputIterator2> _UCXXEXPORT
245
equal(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2)
247
while(first1 !=last1){
248
if(*first1 != *first2){
257
template<class InputIterator1, class InputIterator2, class BinaryPredicate> _UCXXEXPORT
259
equal(InputIterator1 first1, InputIterator1 last1,
260
InputIterator2 first2, BinaryPredicate pred)
262
while(first1 !=last1){
263
if( !pred(*first1, *first2) ){
272
template<class ForwardIterator1, class ForwardIterator2> _UCXXEXPORT
273
ForwardIterator1 search(ForwardIterator1 first1, ForwardIterator1 last1,
274
ForwardIterator2 first2, ForwardIterator2 last2)
276
equal_to<typename iterator_traits<ForwardIterator1>::value_type> c;
277
return search(first1, last1, first2, last2, c);
281
template<class ForwardIterator1, class ForwardIterator2, class BinaryPredicate> _UCXXEXPORT
283
search(ForwardIterator1 first1, ForwardIterator1 last1,
284
ForwardIterator2 first2, ForwardIterator2 last2,
285
BinaryPredicate pred)
287
while(first1 != last1){
288
ForwardIterator1 temp1(first1);
289
ForwardIterator2 temp2(first2);
290
while(temp2 != last2 && temp1 != last1){
291
if( !pred(*temp2, *temp1) ){
306
template<class ForwardIterator, class Size, class T> _UCXXEXPORT
308
search_n(ForwardIterator first, ForwardIterator last, Size count, const T& value)
310
while( first != last ){
312
ForwardIterator temp(first);
314
++first; //Avoid doing the same comparison over again
315
while(i < count && *temp == value){
329
template<class ForwardIterator, class Size, class T, class BinaryPredicate> _UCXXEXPORT
331
search_n(ForwardIterator first, ForwardIterator last,
332
Size count, const T& value, BinaryPredicate pred)
334
while( first != last ){
335
if( pred(*first, value) ){
336
ForwardIterator temp(first);
338
++first; //Avoid doing the same comparison over again
339
while(i < count && pred(*temp, value) ){
353
// subclause _lib.alg.modifying.operations_, modifying sequence operations:
355
template<class InputIterator, class OutputIterator> _UCXXEXPORT
357
copy(InputIterator first, InputIterator last, OutputIterator result)
359
while(first != last){
367
template<class BidirectionalIterator1, class BidirectionalIterator2> _UCXXEXPORT
368
BidirectionalIterator2
369
copy_backward(BidirectionalIterator1 first, BidirectionalIterator1 last,
370
BidirectionalIterator2 result)
372
while(first !=last ){
380
template<class T> _UCXXEXPORT void swap(T& a, T& b){
386
template<class ForwardIterator1, class ForwardIterator2> _UCXXEXPORT
388
swap_ranges(ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2)
390
while(first1 != last1){
391
iter_swap(first1, first2);
399
template<class ForwardIterator1, class ForwardIterator2> _UCXXEXPORT
401
iter_swap(ForwardIterator1 a, ForwardIterator2 b)
403
typename iterator_traits<ForwardIterator1>::value_type temp(*a);
409
template<class InputIterator, class OutputIterator, class UnaryOperation> _UCXXEXPORT
411
transform(InputIterator first, InputIterator last,
412
OutputIterator result, UnaryOperation op)
414
while(first != last){
415
*result = op(*first);
423
template<class InputIterator1, class InputIterator2, class OutputIterator, class BinaryOperation> _UCXXEXPORT
424
OutputIterator transform(InputIterator1 first1, InputIterator1 last1,
425
InputIterator2 first2, OutputIterator result,
426
BinaryOperation binary_op)
428
while(first1 != last1){
429
*result = binary_op(*first1, *first2);
438
template<class ForwardIterator, class T> _UCXXEXPORT
440
replace(ForwardIterator first, ForwardIterator last,
441
const T& old_value, const T& new_value)
443
while(first != last){
444
if(*first == old_value){
451
template<class ForwardIterator, class Predicate, class T> _UCXXEXPORT
453
replace_if(ForwardIterator first, ForwardIterator last,
454
Predicate pred, const T& new_value)
456
while(first != last){
465
template<class InputIterator, class OutputIterator, class T> _UCXXEXPORT
467
replace_copy(InputIterator first, InputIterator last,
468
OutputIterator result, const T& old_value, const T& new_value)
470
while(first != last){
471
if(*first == old_value){
482
template<class Iterator, class OutputIterator, class Predicate, class T> _UCXXEXPORT
484
replace_copy_if(Iterator first, Iterator last,
485
OutputIterator result, Predicate pred, const T& new_value)
487
while(first != last){
499
template<class ForwardIterator, class T> _UCXXEXPORT
501
fill(ForwardIterator first, ForwardIterator last, const T& value)
503
while(first != last){
509
template<class OutputIterator, class Size, class T> _UCXXEXPORT
511
fill_n(OutputIterator first, Size n, const T& value)
520
template<class ForwardIterator, class Generator> _UCXXEXPORT
522
generate(ForwardIterator first, ForwardIterator last, Generator gen)
524
while(first != last){
530
template<class OutputIterator, class Size, class Generator> _UCXXEXPORT
532
generate_n(OutputIterator first, Size n, Generator gen)
541
template<class ForwardIterator, class T> _UCXXEXPORT
543
remove(ForwardIterator first, ForwardIterator last, const T& value)
545
ForwardIterator temp(first);
558
template<class ForwardIterator, class Predicate> _UCXXEXPORT
560
remove_if(ForwardIterator first, ForwardIterator last, Predicate pred)
562
ForwardIterator temp(first);
576
template<class InputIterator, class OutputIterator, class T> _UCXXEXPORT
578
remove_copy(InputIterator first, InputIterator last,
579
OutputIterator result, const T& value)
591
template<class InputIterator, class OutputIterator, class Predicate> _UCXXEXPORT
593
remove_copy_if(InputIterator first, InputIterator last,
594
OutputIterator result, Predicate pred)
606
template<class ForwardIterator> _UCXXEXPORT
608
unique(ForwardIterator first, ForwardIterator last)
610
ForwardIterator new_val(first);
611
ForwardIterator old_val(first);
613
while(new_val !=last){
614
if(*new_val == *old_val && new_val != old_val){
626
template<class ForwardIterator, class BinaryPredicate> _UCXXEXPORT
628
unique(ForwardIterator first, ForwardIterator last, BinaryPredicate pred)
630
ForwardIterator new_val(first);
631
ForwardIterator old_val(first);
633
while(new_val !=last){
634
if( pred(*new_val, *old_val) && new_val != old_val){
646
template<class InputIterator, class OutputIterator> _UCXXEXPORT
648
unique_copy(InputIterator first, InputIterator last, OutputIterator result)
650
InputIterator temp(first);
651
while(first !=last ){
652
if(*first == *temp && first != temp){
664
template<class InputIterator, class OutputIterator, class BinaryPredicate> _UCXXEXPORT
666
unique_copy(InputIterator first, InputIterator last,
667
OutputIterator result, BinaryPredicate pred)
669
InputIterator temp(first);
670
while(first !=last ){
671
if( pred(*first, *temp) && first != temp){
683
template<class BidirectionalIterator> _UCXXEXPORT
685
reverse(BidirectionalIterator first, BidirectionalIterator last)
687
--last; //Don't work with one past the end element
689
iter_swap(first, last);
695
template<class BidirectionalIterator, class OutputIterator> _UCXXEXPORT
697
reverse_copy(BidirectionalIterator first, BidirectionalIterator last,
698
OutputIterator result)
708
template<class ForwardIterator> _UCXXEXPORT
710
rotate(ForwardIterator first, ForwardIterator middle, ForwardIterator last)
712
if ((first == middle) || (last == middle)){
716
ForwardIterator first2 = middle;
719
swap(*first, *first2);
725
} while(first2 != last);
729
while (first2 != last) {
730
swap(*first, *first2);
733
if (first == middle){
735
}else if (first2 == last){
741
template<class ForwardIterator, class OutputIterator> _UCXXEXPORT
743
rotate_copy(ForwardIterator first, ForwardIterator middle,
744
ForwardIterator last, OutputIterator result)
746
ForwardIterator temp(middle);
752
while(first != middle){
761
template<class RandomAccessIterator> _UCXXEXPORT
763
random_shuffle(RandomAccessIterator first, RandomAccessIterator last)
766
while(first != last){
767
iter_swap(first, (first + (rand() % (last - first) ) ) );
772
template<class RandomAccessIterator, class RandomNumberGenerator> _UCXXEXPORT
774
random_shuffle(RandomAccessIterator first, RandomAccessIterator last,
775
RandomNumberGenerator& rand)
778
while(first != last){
779
iter_swap(first, (first + (rand(last - first) ) ) );
784
// _lib.alg.partitions_, partitions:
785
template<class BidirectionalIterator, class Predicate> _UCXXEXPORT BidirectionalIterator
786
partition(BidirectionalIterator first, BidirectionalIterator last, Predicate pred)
788
return stable_partition(first, last, pred);
791
template<class BidirectionalIterator, class Predicate> _UCXXEXPORT BidirectionalIterator
792
stable_partition(BidirectionalIterator first, BidirectionalIterator last, Predicate pred)
794
//first now points to the first non-desired element
795
while( first != last && pred(*first) ){
799
BidirectionalIterator tempb;
800
BidirectionalIterator tempe = first;
802
while( tempe != last ){
803
//Find the next desired element
804
while( !pred(*tempe) ){
807
//See if we should exit
813
//Rotate the element back to the begining
815
while(tempb != first){
816
iter_swap(tempb, tempb-1 );
819
//First now has a desired element
826
template<class RandomAccessIterator> _UCXXEXPORT
827
void sort(RandomAccessIterator first, RandomAccessIterator last)
829
less<typename iterator_traits<RandomAccessIterator>::value_type> c;
830
sort(first, last, c );
833
template<class RandomAccessIterator, class Compare> _UCXXEXPORT
834
void sort(RandomAccessIterator first, RandomAccessIterator last, Compare comp)
836
stable_sort(first, last, comp);
839
template<class RandomAccessIterator> _UCXXEXPORT
840
void stable_sort(RandomAccessIterator first, RandomAccessIterator last)
842
less<typename iterator_traits<RandomAccessIterator>::value_type> c;
843
stable_sort(first, last, c);
846
template<class RandomAccessIterator, class Compare> _UCXXEXPORT
847
void stable_sort(RandomAccessIterator first, RandomAccessIterator last, Compare comp)
849
//FIXME - bubble sort
850
RandomAccessIterator temp;
852
while(last - first > 0){
854
while(temp != first){
855
if( comp( *temp, *(temp-1) ) ){
856
iter_swap( temp-1, temp);
864
template<class RandomAccessIterator> _UCXXEXPORT
865
void partial_sort(RandomAccessIterator first, RandomAccessIterator middle, RandomAccessIterator last)
867
less<typename iterator_traits<RandomAccessIterator>::value_type> c;
868
partial_sort(first, middle, last, c);
870
template<class RandomAccessIterator, class Compare> _UCXXEXPORT
871
void partial_sort(RandomAccessIterator first, RandomAccessIterator middle,
872
RandomAccessIterator last, Compare comp)
874
//Fixme - partial bubble sort
875
RandomAccessIterator temp;
877
while(first < middle){
879
while(temp != first){
880
if( comp(*temp, *(temp -1 ) ) ) {
881
iter_swap( temp-1, temp);
888
template<class InputIterator, class RandomAccessIterator> _UCXXEXPORT
890
partial_sort_copy(InputIterator first, InputIterator last,
891
RandomAccessIterator result_first, RandomAccessIterator result_last)
893
less<typename iterator_traits<RandomAccessIterator>::value_type> c;
894
return partial_sort_copy(first, last, result_first, result_last, c);
896
template<class InputIterator, class RandomAccessIterator, class Compare> _UCXXEXPORT
898
partial_sort_copy(InputIterator first, InputIterator last,
899
RandomAccessIterator result_first, RandomAccessIterator result_last, Compare comp)
901
size_t output_size = last-first;
902
size_t temp_size = result_last - result_first;
903
if(temp_size < output_size){
904
output_size = temp_size;
907
RandomAccessIterator middle = result_first + output_size;
908
RandomAccessIterator temp = result_first;
910
while(temp != middle){
915
sort(result_first, middle, comp);
917
while( first != last){
918
if( comp( *first, *(middle-1) ) ){
919
*(middle-1) = *first;
920
sort(result_first, middle, comp);
927
template<class RandomAccessIterator> _UCXXEXPORT
928
void nth_element(RandomAccessIterator first, RandomAccessIterator nth, RandomAccessIterator last)
930
less<typename iterator_traits<RandomAccessIterator>::value_type> c;
931
nth_element(first, nth, last, c);
933
template<class RandomAccessIterator, class Compare> _UCXXEXPORT
934
void nth_element(RandomAccessIterator first, RandomAccessIterator nth,
935
RandomAccessIterator last, Compare comp)
937
partial_sort(first, nth, last, comp);
940
template<class ForwardIterator, class T> _UCXXEXPORT
941
ForwardIterator lower_bound(ForwardIterator first, ForwardIterator last,
944
less<typename iterator_traits<ForwardIterator>::value_type> c;
945
return lower_bound(first, last, value, c);
948
template<class ForwardIterator, class T, class Compare> _UCXXEXPORT
949
ForwardIterator lower_bound(ForwardIterator first, ForwardIterator last,
950
const T& value, Compare comp)
955
//If below or equal to the first element
956
if( comp(*first, value) == false){
959
ForwardIterator middle;
961
//If greater than the top element
963
advance(middle, distance(first, last) - 1);
964
if( comp(*middle, value) ){
968
//Now begin the actual search for the begining
969
while( distance(first, last) > 1 ){
972
advance(middle, (distance(first, last)/2) );
973
if( !comp(*middle, value) ){
980
if( !comp(*first, value) ){
986
template<class ForwardIterator, class T> _UCXXEXPORT
987
ForwardIterator upper_bound(ForwardIterator first, ForwardIterator last,
990
less<typename iterator_traits<ForwardIterator>::value_type> c;
991
return upper_bound(first, last, value, c);
995
template<class ForwardIterator, class T, class Compare> _UCXXEXPORT
996
ForwardIterator upper_bound(ForwardIterator first, ForwardIterator last,
997
const T& value, Compare comp)
1002
//If below the first element
1003
if( comp(value, *first) == true){
1007
ForwardIterator middle;
1009
//If greater than the top element
1011
advance(middle, distance(first, last) - 1);
1012
if( comp(*middle, value) ){
1016
//Now begin the actual search for the end
1017
while( distance(first, last) > 1 ){
1020
advance(middle, (distance(first, last)/2) );
1021
if( comp(value, *middle) ){
1028
if( comp(value, *first) ){
1035
template<class ForwardIterator, class T> _UCXXEXPORT
1036
pair<ForwardIterator, ForwardIterator>
1037
equal_range(ForwardIterator first, ForwardIterator last, const T& value)
1039
less<typename iterator_traits<ForwardIterator>::value_type> c;
1040
return equal_range(first, last, value, c);
1043
template<class ForwardIterator, class T, class Compare> _UCXXEXPORT
1044
pair<ForwardIterator, ForwardIterator>
1045
equal_range(ForwardIterator first, ForwardIterator last, const T& value, Compare comp)
1047
pair<ForwardIterator, ForwardIterator> retval;
1048
retval.first = lower_bound(first, last, value, comp);
1049
//Reuse retval.first in order to (possibly) save a few comparisons
1050
retval.second = upper_bound(retval.first, last, value, comp);
1055
template<class ForwardIterator, class T> _UCXXEXPORT
1056
bool binary_search(ForwardIterator first, ForwardIterator last,
1059
less<typename iterator_traits<ForwardIterator>::value_type> c;
1060
return binary_search(first, last, value, c);
1063
template<class ForwardIterator, class T, class Compare> _UCXXEXPORT
1064
bool binary_search(ForwardIterator first, ForwardIterator last,
1065
const T& value, Compare comp)
1067
if( distance(first, last) == 0){
1071
ForwardIterator middle;
1073
while( distance(first, last) > 1 ){
1074
//Set middle between first and last
1076
advance(middle, distance(first, last)/2 );
1078
if( comp(*middle, value ) == true){
1085
if( !comp(*first, value) && !comp(value, *first) ){
1088
if( !comp(*last, value) && !comp(value, *last) ){
1095
// _lib.alg.merge_, merge:
1096
template<class InputIterator1, class InputIterator2, class OutputIterator> _UCXXEXPORT
1098
merge(InputIterator1 first1, InputIterator1 last1,
1099
InputIterator2 first2, InputIterator2 last2, OutputIterator result)
1101
less<typename iterator_traits<InputIterator1>::value_type> c;
1102
return merge(first1, last1, first2, last2, result, c);
1104
template<class InputIterator1, class InputIterator2, class OutputIterator, class Compare> _UCXXEXPORT
1106
merge(InputIterator1 first1, InputIterator1 last1,
1107
InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp)
1109
while( first1 != last1 && first2 != last2){
1110
//If in this order so first1 elements which are equal come first
1111
if( comp(*first2, *first1) ){
1120
//Clean up remaining elements
1121
while(first1 != last1){
1126
while(first2 != last2){
1133
template<class BidirectionalIterator> _UCXXEXPORT
1134
void inplace_merge(BidirectionalIterator first,
1135
BidirectionalIterator middle, BidirectionalIterator last)
1137
less<typename iterator_traits<BidirectionalIterator>::value_type> c;
1138
inplace_merge(first, middle, last, c);
1140
template<class BidirectionalIterator, class Compare> _UCXXEXPORT
1141
void inplace_merge(BidirectionalIterator first,
1142
BidirectionalIterator middle, BidirectionalIterator last, Compare comp)
1144
//FIXME - using a bubble exchange method
1145
while(first != middle && middle !=last){
1146
if( comp(*middle, *first) ){
1147
BidirectionalIterator temp(middle);
1148
while( temp != first){
1149
iter_swap(temp, temp-1);
1158
// _lib.alg.set.operations_, set operations:
1159
template<class InputIterator1, class InputIterator2> _UCXXEXPORT
1160
bool includes(InputIterator1 first1, InputIterator1 last1,
1161
InputIterator2 first2, InputIterator2 last2)
1163
less<typename iterator_traits<InputIterator1>::value_type> c;
1164
return includes(first1, last1, first2, last2, c );
1167
template<class InputIterator1, class InputIterator2, class Compare> _UCXXEXPORT
1168
bool includes(InputIterator1 first1, InputIterator1 last1,
1169
InputIterator2 first2, InputIterator2 last2, Compare comp)
1171
while(first2 != last2){
1172
//Advance the large set until no longer smaller than the element we are looking for
1173
while( comp(*first1, *first2) ){
1175
//If we are at the end of the list, we don't have the desired element
1176
if(first1 == last1){
1180
//If we are past the element we want, it isn't here
1181
if( comp(*first2, *first1) ){
1184
++first2; //Move to next element
1189
template<class InputIterator1, class InputIterator2, class OutputIterator> _UCXXEXPORT
1190
OutputIterator set_union(InputIterator1 first1, InputIterator1 last1,
1191
InputIterator2 first2, InputIterator2 last2, OutputIterator result)
1193
less<typename iterator_traits<InputIterator1>::value_type> c;
1194
return set_union(first1, last1, first2, last2, result, c);
1197
template<class InputIterator1, class InputIterator2, class OutputIterator, class Compare> _UCXXEXPORT
1198
OutputIterator set_union(InputIterator1 first1, InputIterator1 last1,
1199
InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp)
1201
while( first1 != last1 && first2 != last2){
1202
if( comp(*first2, *first1) ){
1205
//Elliminate duplicates
1206
InputIterator2 temp = first2;
1207
while( first1 != last1 && !comp( *temp, *first1) ){
1210
while( first2 != last2 && !comp( *temp, *first2) ){
1215
//Elliminate duplicates
1216
InputIterator1 temp = first1;
1217
while( first1 != last1 && !comp( *temp, *first1) ){
1220
while( first2 != last2 && !comp( *temp, *first2) ){
1227
//Clean up remaining elements
1228
while(first1 != last1){
1231
InputIterator1 temp = first1;
1232
while( first1 != last1 && !comp( *temp, *first1) ){
1237
while(first2 != last2){
1240
InputIterator2 temp = first2;
1241
while( first2 != last2 && !comp( *temp, *first2) ){
1248
template<class InputIterator1, class InputIterator2, class OutputIterator> _UCXXEXPORT
1249
OutputIterator set_intersection(InputIterator1 first1, InputIterator1 last1,
1250
InputIterator2 first2, InputIterator2 last2, OutputIterator result)
1252
less<typename iterator_traits<InputIterator1>::value_type> c;
1253
return set_intersection(first1, last1, first2, last2, result, c);
1255
template<class InputIterator1, class InputIterator2, class OutputIterator, class Compare> _UCXXEXPORT
1256
OutputIterator set_intersection(InputIterator1 first1, InputIterator1 last1,
1257
InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp)
1259
while( first1 != last1 && first2 != last2){
1260
if( comp(*first2, *first1) ){
1262
}else if( comp(*first1, *first2) ) {
1267
InputIterator1 temp = first1;
1268
while( first1 != last1 && !comp( *temp, *first1) ){
1276
template<class InputIterator1, class InputIterator2, class OutputIterator> _UCXXEXPORT
1277
OutputIterator set_difference(InputIterator1 first1, InputIterator1 last1,
1278
InputIterator2 first2, InputIterator2 last2, OutputIterator result)
1280
less<typename iterator_traits<InputIterator1>::value_type> c;
1281
return set_difference(first1, last1, first2, last2, result, c);
1283
template<class InputIterator1, class InputIterator2, class OutputIterator, class Compare> _UCXXEXPORT
1284
OutputIterator set_difference(InputIterator1 first1, InputIterator1 last1,
1285
InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp)
1287
while( first1 != last1 && first2 != last2){
1288
if( comp(*first1, *first2) ){
1292
//Elliminate duplicates
1293
InputIterator1 temp = first1;
1294
while( first1 != last1 && !comp( *temp, *first1) ){
1297
}else if( comp(*first2, *first1) ){
1299
//Elliminate duplicates
1300
InputIterator2 temp = first2;
1301
while( first2 != last2 && !comp( *temp, *first2) ){
1305
}else{ //They are identical - skip
1306
//Elliminate duplicates
1307
InputIterator1 temp = first1;
1308
while( first1 != last1 && !comp( *temp, *first1) ){
1311
while( first2 != last2 && !comp( *temp, *first2) ){
1317
//Clean up remaining elements
1318
while(first1 != last1){
1321
InputIterator1 temp = first1;
1322
while( first1 != last1 && !comp( *temp, *first1) ){
1329
template<class InputIterator1, class InputIterator2, class OutputIterator> _UCXXEXPORT
1330
OutputIterator set_symmetric_difference(InputIterator1 first1, InputIterator1 last1,
1331
InputIterator2 first2, InputIterator2 last2, OutputIterator result)
1333
less<typename iterator_traits<InputIterator1>::value_type> c;
1334
return set_symmetric_difference(first1, last1, first2, last2, result, c);
1336
template<class InputIterator1, class InputIterator2, class OutputIterator, class Compare> _UCXXEXPORT
1337
OutputIterator set_symmetric_difference(InputIterator1 first1, InputIterator1 last1,
1338
InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp)
1340
while( first1 != last1 && first2 != last2){
1341
if( comp(*first1, *first2) ){
1345
//Elliminate duplicates
1346
InputIterator1 temp = first1;
1347
while( first1 != last1 && !comp( *temp, *first1) ){
1350
}else if( comp(*first2, *first1) ){
1354
//Elliminate duplicates
1355
InputIterator2 temp = first2;
1356
while( first2 != last2 && !comp( *temp, *first2) ){
1360
}else{ //They are identical - skip
1361
//Elliminate duplicates
1362
InputIterator1 temp = first1;
1363
while( first1 != last1 && !comp( *temp, *first1) ){
1366
while( first2 != last2 && !comp( *temp, *first2) ){
1372
//Clean up remaining elements
1373
while(first1 != last1){
1376
InputIterator1 temp = first1;
1377
while( first1 != last1 && !comp( *temp, *first1) ){
1382
while(first2 != last2){
1385
InputIterator2 temp = first2;
1386
while( first2 != last2 && !comp( *temp, *first2) ){
1394
// _lib.alg.heap.operations_, heap operations:
1396
template<class RandomAccessIterator> _UCXXEXPORT
1397
void push_heap(RandomAccessIterator first, RandomAccessIterator last)
1399
less<typename iterator_traits<RandomAccessIterator>::value_type> c;
1400
push_heap(first, last, c);
1403
template<class RandomAccessIterator, class Compare> _UCXXEXPORT
1404
void push_heap(RandomAccessIterator first, RandomAccessIterator last, Compare comp)
1406
// *(last - 1) is the last element
1407
RandomAccessIterator begin, middle, end;
1410
if(last - first < 2){ //Empty heap
1413
if ( comp(*(last-1), *end) ){ //Belongs past the end - already in place
1416
while(end - begin > 1){
1417
middle = begin + ((end - begin)/2);
1418
if( comp(*middle, *(last-1) ) ){
1425
if( comp(*begin, *(last-1)) ){
1431
//Now all we do is swap the elements up to the begining
1434
while(last > middle){
1435
iter_swap(last, last-1);
1440
template<class RandomAccessIterator> _UCXXEXPORT
1441
void pop_heap(RandomAccessIterator first, RandomAccessIterator last)
1443
less<typename iterator_traits<RandomAccessIterator>::value_type> c;
1444
pop_heap(first, last, c);
1446
template<class RandomAccessIterator, class Compare> _UCXXEXPORT
1447
void pop_heap(RandomAccessIterator first, RandomAccessIterator last, Compare)
1450
while(first < last){
1451
iter_swap( first, first+1);
1456
template<class RandomAccessIterator> _UCXXEXPORT
1457
void make_heap(RandomAccessIterator first, RandomAccessIterator last)
1459
less<typename iterator_traits<RandomAccessIterator>::value_type> c;
1460
make_heap(first, last, c);
1462
template<class RandomAccessIterator, class Compare> _UCXXEXPORT
1463
void make_heap(RandomAccessIterator first, RandomAccessIterator last, Compare comp)
1465
sort( reverse_iterator<RandomAccessIterator>(last), reverse_iterator<RandomAccessIterator>(first), comp);
1467
template<class RandomAccessIterator> _UCXXEXPORT
1468
void sort_heap(RandomAccessIterator first, RandomAccessIterator last)
1470
less<typename iterator_traits<RandomAccessIterator>::value_type> c;
1471
sort_heap(first, last, c);
1473
template<class RandomAccessIterator, class Compare> _UCXXEXPORT
1474
void sort_heap(RandomAccessIterator first, RandomAccessIterator last, Compare comp)
1476
sort( first, last, comp);
1480
// _lib.alg.min.max_, minimum and maximum:
1481
template<class T> _UCXXEXPORT
1482
const T& min(const T& a, const T& b)
1490
template<class T, class Compare> _UCXXEXPORT
1491
const T& min(const T& a, const T& b, Compare comp)
1493
if( comp(b, a) == true){
1499
template<class T> _UCXXEXPORT
1500
const T& max(const T& a, const T& b)
1508
template<class T, class Compare> _UCXXEXPORT
1509
const T& max(const T& a, const T& b, Compare comp)
1517
template<class ForwardIterator> _UCXXEXPORT
1518
ForwardIterator min_element(ForwardIterator first, ForwardIterator last)
1520
less<typename iterator_traits<ForwardIterator>::value_type> c;
1521
return min_element(first, last, c);
1524
template<class ForwardIterator, class Compare> _UCXXEXPORT
1525
ForwardIterator min_element(ForwardIterator first, ForwardIterator last, Compare comp)
1527
ForwardIterator retval = first;
1529
while(first != last){
1530
if( comp( *first, *retval) ){
1538
template<class ForwardIterator> _UCXXEXPORT
1539
ForwardIterator max_element(ForwardIterator first, ForwardIterator last)
1541
less<typename iterator_traits<ForwardIterator>::value_type> c;
1542
return max_element(first, last, c);
1545
template<class ForwardIterator, class Compare> _UCXXEXPORT
1546
ForwardIterator max_element(ForwardIterator first, ForwardIterator last, Compare comp)
1548
ForwardIterator retval = first;
1550
while(first != last){
1551
if( comp( *retval, *first ) ){
1559
template<class InputIterator1, class InputIterator2> _UCXXEXPORT
1560
bool lexicographical_compare(InputIterator1 first1, InputIterator1 last1,
1561
InputIterator2 first2, InputIterator2 last2)
1563
less<typename iterator_traits<InputIterator1>::value_type> c;
1564
return lexicographical_compare(first1, last1, first2, last2, c);
1567
template<class InputIterator1, class InputIterator2, class Compare> _UCXXEXPORT
1568
bool lexicographical_compare(InputIterator1 first1, InputIterator1 last1,
1569
InputIterator2 first2, InputIterator2 last2, Compare comp)
1571
while(first1 != last1 && first2 != last2){
1572
if( comp(*first1, *first2) ){
1575
if( comp(*first2, *first1) ){
1581
return first1==last1 && first2 != last2;
1584
// _lib.alg.permutation.generators_, permutations
1585
template<class BidirectionalIterator> _UCXXEXPORT
1586
bool next_permutation(BidirectionalIterator first, BidirectionalIterator last)
1588
less<typename iterator_traits<BidirectionalIterator>::value_type> c;
1589
return next_permutation(first, last, c);
1592
template<class BidirectionalIterator, class Compare> _UCXXEXPORT
1593
bool next_permutation(BidirectionalIterator first, BidirectionalIterator last, Compare comp)
1596
return false; // No data
1598
BidirectionalIterator i = last;
1601
return false; //Only one element
1603
BidirectionalIterator ii = i;
1605
//If the last two items are in order, swap them and call it done
1606
if( comp(*ii, *i) ){
1612
//This part of the algorithm copied from G++ header files ver. 3.4.2
1618
if ( comp(*i, *ii) ){
1619
BidirectionalIterator j = last;
1621
while ( !comp(*i, *j)){
1629
reverse(first, last);
1637
template<class BidirectionalIterator> _UCXXEXPORT
1638
bool prev_permutation(BidirectionalIterator first, BidirectionalIterator last)
1640
less<typename iterator_traits<BidirectionalIterator>::value_type> c;
1641
return prev_permutation(first, last, c);
1644
template<class BidirectionalIterator, class Compare> _UCXXEXPORT
1645
bool prev_permutation(BidirectionalIterator first, BidirectionalIterator last, Compare comp)
1648
return false; // No data
1650
BidirectionalIterator i = last;
1653
return false; //Only one element
1655
BidirectionalIterator ii = i;
1657
//If the last two items are in reverse order, swap them and call it done
1658
if( comp(*i, *ii) ){
1663
//Copied from GNU GCC header files version 3.4.2
1670
if ( comp(*ii, *i)){
1671
BidirectionalIterator j = last;
1673
while ( !comp(*j, *i)){
1681
reverse(first, last);
1690
#pragma GCC visibility pop