-
Notifications
You must be signed in to change notification settings - Fork 180
/
Copy pathalgorithm
1695 lines (1516 loc) · 40.9 KB
/
algorithm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Copyright (C) 2004 Garrett A. Kajmowicz
This file is part of the uClibc++ Library.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <cstdlib>
#include <iterator>
#include <utility.h>
#include <functional>
#ifndef __STD_HEADER_ALGORITHM
#define __STD_HEADER_ALGORITHM 1
//Elliminate any previously defined macro
#undef min
#undef max
#pragma GCC visibility push(default)
namespace std{
// subclause _lib.alg.nonmodifying_, non-modifying sequence operations:
template<class InputIterator, class Function> _UCXXEXPORT
Function for_each(InputIterator first, InputIterator last, Function f)
{
while(first !=last){
f(*first);
++first;
}
return f;
}
template<class InputIterator, class T> _UCXXEXPORT
InputIterator find(InputIterator first, InputIterator last, const T& value)
{
while(first !=last && ! ( *first == value ) ){
++first;
}
return first;
}
template<class InputIterator, class Predicate> _UCXXEXPORT
InputIterator find_if(InputIterator first, InputIterator last, Predicate pred)
{
while(first !=last && !pred(*first)){
++first;
}
return first;
}
template<class ForwardIterator1, class ForwardIterator2> _UCXXEXPORT ForwardIterator1
find_end(ForwardIterator1 first1, ForwardIterator1 last1,
ForwardIterator2 first2, ForwardIterator2 last2)
{
ForwardIterator1 retval = last1;
while(first1 !=last1){
ForwardIterator1 temp1(first1);
ForwardIterator2 temp2(first2);
while(temp2!=last2 && temp1!= last1){
if(*temp1 != *temp2){
break; //Exit while loop
}
++temp1;
++temp2;
if(temp2 == last2){ //Have successfully checked the whole sequence
retval = first1;
}
}
}
return retval;
}
template<class ForwardIterator1, class ForwardIterator2, class BinaryPredicate> _UCXXEXPORT
ForwardIterator1
find_end(ForwardIterator1 first1, ForwardIterator1 last1,
ForwardIterator2 first2, ForwardIterator2 last2,
BinaryPredicate pred)
{
ForwardIterator1 retval = last1;
while(first1 !=last1){
ForwardIterator1 temp1(first1);
ForwardIterator2 temp2(first2);
while(temp2!=last2 && temp1!= last1){
if( ! pred(*temp1, *temp2)){
break; //Exit while loop
}
++temp1;
++temp2;
if(temp2 == last2){ //Have successfully checked the whole sequence
retval = first1;
}
}
}
return retval;
}
template<class ForwardIterator1, class ForwardIterator2> _UCXXEXPORT
ForwardIterator1
find_first_of(ForwardIterator1 first1, ForwardIterator1 last1,
ForwardIterator2 first2, ForwardIterator2 last2)
{
while(first1 != last1){
ForwardIterator2 temp2(first2);
while(temp2 != last2 ){
if(*temp2 == first1){
return first1;
}
++temp2;
}
}
return last1;
}
template<class ForwardIterator1, class ForwardIterator2, class BinaryPredicate> _UCXXEXPORT
ForwardIterator1
find_first_of(ForwardIterator1 first1, ForwardIterator1 last1,
ForwardIterator2 first2, ForwardIterator2 last2,
BinaryPredicate pred)
{
while(first1 != last1){
ForwardIterator2 temp2(first2);
while(temp2 != last2 ){
if(*temp2 == first1){
return first1;
}
++temp2;
}
}
return last1;
}
template<class ForwardIterator> _UCXXEXPORT ForwardIterator
adjacent_find(ForwardIterator first, ForwardIterator last)
{
ForwardIterator temp;
while(first != last){
temp = first;
++temp;
if(*temp == *first){
return first;
}
++first;
}
return first;
}
template<class ForwardIterator, class BinaryPredicate> _UCXXEXPORT
ForwardIterator
adjacent_find(ForwardIterator first, ForwardIterator last, BinaryPredicate pred)
{
ForwardIterator temp;
while(first != last){
temp = first;
++temp;
if( pred(*temp, *first)){
return first;
}
++first;
}
return first;
}
template<class InputIterator, class T> _UCXXEXPORT
typename iterator_traits<InputIterator>::difference_type
count(InputIterator first, InputIterator last, const T& value)
{
typename iterator_traits<InputIterator>::difference_type i = 0;
while(first!=last){
if(*first == value){
++i;
}
++first;
}
return i;
}
template<class InputIterator, class Predicate> _UCXXEXPORT
typename iterator_traits<InputIterator>::difference_type
count_if(InputIterator first, InputIterator last, Predicate pred)
{
typename iterator_traits<InputIterator>::difference_type i = 0;
while(first!=last){
if( pred(*first) ){
++i;
}
++first;
}
return i;
}
template<class InputIterator1, class InputIterator2> _UCXXEXPORT
pair<InputIterator1, InputIterator2>
mismatch(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2)
{
while(first1 != last1){
if(*first1 != *first2){
break;
}
++first1;
++first2;
}
pair<InputIterator1, InputIterator2> retval;
retval.first = first1;
retval.second = first2;
return retval;
}
template<class InputIterator1, class InputIterator2, class BinaryPredicate> _UCXXEXPORT
pair<InputIterator1, InputIterator2>
mismatch(InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, BinaryPredicate pred)
{
while(first1 != last1){
if( !pred(*first1, *first2) ){
break;
}
++first1;
++first2;
}
pair<InputIterator1, InputIterator2> retval;
retval.first = first1;
retval.second = first2;
return retval;
}
template<class InputIterator1, class InputIterator2> _UCXXEXPORT
bool
equal(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2)
{
while(first1 !=last1){
if(*first1 != *first2){
return false;
}
++first1;
++first2;
}
return true;
}
template<class InputIterator1, class InputIterator2, class BinaryPredicate> _UCXXEXPORT
bool
equal(InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, BinaryPredicate pred)
{
while(first1 !=last1){
if( !pred(*first1, *first2) ){
return false;
}
++first1;
++first2;
}
return true;
}
template<class ForwardIterator1, class ForwardIterator2> _UCXXEXPORT
ForwardIterator1 search(ForwardIterator1 first1, ForwardIterator1 last1,
ForwardIterator2 first2, ForwardIterator2 last2)
{
equal_to<typename iterator_traits<ForwardIterator1>::value_type> c;
return search(first1, last1, first2, last2, c);
}
template<class ForwardIterator1, class ForwardIterator2, class BinaryPredicate> _UCXXEXPORT
ForwardIterator1
search(ForwardIterator1 first1, ForwardIterator1 last1,
ForwardIterator2 first2, ForwardIterator2 last2,
BinaryPredicate pred)
{
while(first1 != last1){
ForwardIterator1 temp1(first1);
ForwardIterator2 temp2(first2);
while(temp2 != last2 && temp1 != last1){
if( !pred(*temp2, *temp1) ){
break;
}
++temp1;
++temp2;
if(temp2 == last2){
return first1;
}
}
++first1;
}
return last1;
}
template<class ForwardIterator, class Size, class T> _UCXXEXPORT
ForwardIterator
search_n(ForwardIterator first, ForwardIterator last, Size count, const T& value)
{
while( first != last ){
if(*first == value){
ForwardIterator temp(first);
Size i = 1;
++first; //Avoid doing the same comparison over again
while(i < count && *temp == value){
++first;
++i;
}
if(i == count){
return first;
}
}
++first;
}
return first;
}
template<class ForwardIterator, class Size, class T, class BinaryPredicate> _UCXXEXPORT
ForwardIterator
search_n(ForwardIterator first, ForwardIterator last,
Size count, const T& value, BinaryPredicate pred)
{
while( first != last ){
if( pred(*first, value) ){
ForwardIterator temp(first);
Size i = 1;
++first; //Avoid doing the same comparison over again
while(i < count && pred(*temp, value) ){
++first;
++i;
}
if(i == count){
return first;
}
}
++first;
}
return first;
}
// subclause _lib.alg.modifying.operations_, modifying sequence operations:
template<class InputIterator, class OutputIterator> _UCXXEXPORT
OutputIterator
copy(InputIterator first, InputIterator last, OutputIterator result)
{
while(first != last){
*result = *first;
++first;
++result;
}
return result;
}
template<class BidirectionalIterator1, class BidirectionalIterator2> _UCXXEXPORT
BidirectionalIterator2
copy_backward(BidirectionalIterator1 first, BidirectionalIterator1 last,
BidirectionalIterator2 result)
{
while(first !=last ){
--result;
--last;
*result = *last;
}
return result;
}
template<class T> _UCXXEXPORT void swap(T& a, T& b){
T temp(a);
a = b;
b = temp;
}
template<class ForwardIterator1, class ForwardIterator2> _UCXXEXPORT
ForwardIterator2
swap_ranges(ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2)
{
while(first1 != last1){
iter_swap(first1, first2);
++first1;
++first2;
}
return first2;
}
template<class ForwardIterator1, class ForwardIterator2> _UCXXEXPORT
void
iter_swap(ForwardIterator1 a, ForwardIterator2 b)
{
typename iterator_traits<ForwardIterator1>::value_type temp(*a);
*a = *b;
*b = temp;
}
template<class InputIterator, class OutputIterator, class UnaryOperation> _UCXXEXPORT
OutputIterator
transform(InputIterator first, InputIterator last,
OutputIterator result, UnaryOperation op)
{
while(first != last){
*result = op(*first);
++first;
++result;
}
return result;
}
template<class InputIterator1, class InputIterator2, class OutputIterator, class BinaryOperation> _UCXXEXPORT
OutputIterator transform(InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, OutputIterator result,
BinaryOperation binary_op)
{
while(first1 != last1){
*result = binary_op(*first1, *first2);
++first1;
++first2;
++result;
}
return result;
}
template<class ForwardIterator, class T> _UCXXEXPORT
void
replace(ForwardIterator first, ForwardIterator last,
const T& old_value, const T& new_value)
{
while(first != last){
if(*first == old_value){
*first = new_value;
}
++first;
}
}
template<class ForwardIterator, class Predicate, class T> _UCXXEXPORT
void
replace_if(ForwardIterator first, ForwardIterator last,
Predicate pred, const T& new_value)
{
while(first != last){
if( pred(*first) ){
*first = new_value;
}
++first;
}
}
template<class InputIterator, class OutputIterator, class T> _UCXXEXPORT
OutputIterator
replace_copy(InputIterator first, InputIterator last,
OutputIterator result, const T& old_value, const T& new_value)
{
while(first != last){
if(*first == old_value){
*result = new_value;
}else{
*result = *first;
}
++first;
++result;
}
return result;
}
template<class Iterator, class OutputIterator, class Predicate, class T> _UCXXEXPORT
OutputIterator
replace_copy_if(Iterator first, Iterator last,
OutputIterator result, Predicate pred, const T& new_value)
{
while(first != last){
if( pred(*first) ){
*result = new_value;
}else{
*result = *first;
}
++first;
++result;
}
return result;
}
template<class ForwardIterator, class T> _UCXXEXPORT
void
fill(ForwardIterator first, ForwardIterator last, const T& value)
{
while(first != last){
*first = value;
++first;
}
}
template<class OutputIterator, class Size, class T> _UCXXEXPORT
void
fill_n(OutputIterator first, Size n, const T& value)
{
while(n != 0){
*first = value;
--n;
++first;
}
}
template<class ForwardIterator, class Generator> _UCXXEXPORT
void
generate(ForwardIterator first, ForwardIterator last, Generator gen)
{
while(first != last){
*first = gen();
++first;
}
}
template<class OutputIterator, class Size, class Generator> _UCXXEXPORT
void
generate_n(OutputIterator first, Size n, Generator gen)
{
while(n !=0){
*first = gen();
--n;
++first;
}
}
template<class ForwardIterator, class T> _UCXXEXPORT
ForwardIterator
remove(ForwardIterator first, ForwardIterator last, const T& value)
{
ForwardIterator temp(first);
while(temp !=last){
if(*temp == value){
}else{
*first = *temp;
++first;
}
++temp;
}
return first;
}
template<class ForwardIterator, class Predicate> _UCXXEXPORT
ForwardIterator
remove_if(ForwardIterator first, ForwardIterator last, Predicate pred)
{
ForwardIterator temp(first);
while(temp !=last){
if( pred(*temp) ){
}else{
*first = *temp;
++first;
}
++temp;
}
return first;
}
template<class InputIterator, class OutputIterator, class T> _UCXXEXPORT
OutputIterator
remove_copy(InputIterator first, InputIterator last,
OutputIterator result, const T& value)
{
while(first !=last){
if(*first != value){
*result = *first;
++result;
}
++first;
}
return result;
}
template<class InputIterator, class OutputIterator, class Predicate> _UCXXEXPORT
OutputIterator
remove_copy_if(InputIterator first, InputIterator last,
OutputIterator result, Predicate pred)
{
while(first !=last){
if( !pred(*first) ){
*result = *first;
++result;
}
++first;
}
return result;
}
template<class ForwardIterator> _UCXXEXPORT
ForwardIterator
unique(ForwardIterator first, ForwardIterator last)
{
ForwardIterator new_val(first);
ForwardIterator old_val(first);
while(new_val !=last){
if(*new_val == *old_val && new_val != old_val){
}else{
*first = *new_val;
old_val = new_val;
++first;
}
++new_val;
}
return first;
}
template<class ForwardIterator, class BinaryPredicate> _UCXXEXPORT
ForwardIterator
unique(ForwardIterator first, ForwardIterator last, BinaryPredicate pred)
{
ForwardIterator new_val(first);
ForwardIterator old_val(first);
while(new_val !=last){
if( pred(*new_val, *old_val) && new_val != old_val){
}else{
*first = *new_val;
old_val = new_val;
++first;
}
++new_val;
}
return first;
}
template<class InputIterator, class OutputIterator> _UCXXEXPORT
OutputIterator
unique_copy(InputIterator first, InputIterator last, OutputIterator result)
{
InputIterator temp(first);
while(first !=last ){
if(*first == *temp && first != temp){
}else{
*result = *first;
temp = first;
++result;
}
++first;
}
return result;
}
template<class InputIterator, class OutputIterator, class BinaryPredicate> _UCXXEXPORT
OutputIterator
unique_copy(InputIterator first, InputIterator last,
OutputIterator result, BinaryPredicate pred)
{
InputIterator temp(first);
while(first !=last ){
if( pred(*first, *temp) && first != temp){
}else{
*result = *first;
temp = first;
++result;
}
++first;
}
return result;
}
template<class BidirectionalIterator> _UCXXEXPORT
void
reverse(BidirectionalIterator first, BidirectionalIterator last)
{
--last; //Don't work with one past the end element
while(first < last){
iter_swap(first, last);
++first;
--last;
}
}
template<class BidirectionalIterator, class OutputIterator> _UCXXEXPORT
OutputIterator
reverse_copy(BidirectionalIterator first, BidirectionalIterator last,
OutputIterator result)
{
while(last > first){
--last;
*result = *last;
++result;
}
return result;
}
template<class ForwardIterator> _UCXXEXPORT
void
rotate(ForwardIterator first, ForwardIterator middle, ForwardIterator last)
{
if ((first == middle) || (last == middle)){
return;
}
ForwardIterator first2 = middle;
do {
swap(*first, *first2);
first++;
first2++;
if(first == middle){
middle = first2;
}
} while(first2 != last);
first2 = middle;
while (first2 != last) {
swap(*first, *first2);
first++;
first2++;
if (first == middle){
middle = first2;
}else if (first2 == last){
first2 = middle;
}
}
}
template<class ForwardIterator, class OutputIterator> _UCXXEXPORT
OutputIterator
rotate_copy(ForwardIterator first, ForwardIterator middle,
ForwardIterator last, OutputIterator result)
{
ForwardIterator temp(middle);
while(temp !=last){
*result = *temp;
++temp;
++result;
}
while(first != middle){
*result = *first;
++first;
++result;
}
return result;
}
template<class RandomAccessIterator> _UCXXEXPORT
void
random_shuffle(RandomAccessIterator first, RandomAccessIterator last)
{
--last;
while(first != last){
iter_swap(first, (first + (rand() % (last - first) ) ) );
++first;
}
}
template<class RandomAccessIterator, class RandomNumberGenerator> _UCXXEXPORT
void
random_shuffle(RandomAccessIterator first, RandomAccessIterator last,
RandomNumberGenerator& rand)
{
--last;
while(first != last){
iter_swap(first, (first + (rand(last - first) ) ) );
++first;
}
}
// _lib.alg.partitions_, partitions:
template<class BidirectionalIterator, class Predicate> _UCXXEXPORT BidirectionalIterator
partition(BidirectionalIterator first, BidirectionalIterator last, Predicate pred)
{
return stable_partition(first, last, pred);
}
template<class BidirectionalIterator, class Predicate> _UCXXEXPORT BidirectionalIterator
stable_partition(BidirectionalIterator first, BidirectionalIterator last, Predicate pred)
{
//first now points to the first non-desired element
while( first != last && pred(*first) ){
++first;
}
BidirectionalIterator tempb;
BidirectionalIterator tempe = first;
while( tempe != last ){
//Find the next desired element
while( !pred(*tempe) ){
++tempe;
//See if we should exit
if(tempe == last){
return first;
}
}
//Rotate the element back to the begining
tempb = tempe;
while(tempb != first){
iter_swap(tempb, tempb-1 );
--tempb;
}
//First now has a desired element
++first;
}
return first;
}
template<class RandomAccessIterator> _UCXXEXPORT
void sort(RandomAccessIterator first, RandomAccessIterator last)
{
less<typename iterator_traits<RandomAccessIterator>::value_type> c;
sort(first, last, c );
}
template<class RandomAccessIterator, class Compare> _UCXXEXPORT
void sort(RandomAccessIterator first, RandomAccessIterator last, Compare comp)
{
stable_sort(first, last, comp);
}
template<class RandomAccessIterator> _UCXXEXPORT
void stable_sort(RandomAccessIterator first, RandomAccessIterator last)
{
less<typename iterator_traits<RandomAccessIterator>::value_type> c;
stable_sort(first, last, c);
}
template<class RandomAccessIterator, class Compare> _UCXXEXPORT
void stable_sort(RandomAccessIterator first, RandomAccessIterator last, Compare comp)
{
//FIXME - bubble sort
RandomAccessIterator temp;
--last;
while(last - first > 0){
temp = last;
while(temp != first){
if( comp( *temp, *(temp-1) ) ){
iter_swap( temp-1, temp);
}
--temp;
}
++first;
}
}
template<class RandomAccessIterator> _UCXXEXPORT
void partial_sort(RandomAccessIterator first, RandomAccessIterator middle, RandomAccessIterator last)
{
less<typename iterator_traits<RandomAccessIterator>::value_type> c;
partial_sort(first, middle, last, c);
}
template<class RandomAccessIterator, class Compare> _UCXXEXPORT
void partial_sort(RandomAccessIterator first, RandomAccessIterator middle,
RandomAccessIterator last, Compare comp)
{
//Fixme - partial bubble sort
RandomAccessIterator temp;
--last;
while(first < middle){
temp = last;
while(temp != first){
if( comp(*temp, *(temp -1 ) ) ) {
iter_swap( temp-1, temp);
}
--temp;
}
++first;
}
}
template<class InputIterator, class RandomAccessIterator> _UCXXEXPORT
RandomAccessIterator
partial_sort_copy(InputIterator first, InputIterator last,
RandomAccessIterator result_first, RandomAccessIterator result_last)
{
less<typename iterator_traits<RandomAccessIterator>::value_type> c;
return partial_sort_copy(first, last, result_first, result_last, c);
}
template<class InputIterator, class RandomAccessIterator, class Compare> _UCXXEXPORT
RandomAccessIterator
partial_sort_copy(InputIterator first, InputIterator last,
RandomAccessIterator result_first, RandomAccessIterator result_last, Compare comp)
{
size_t output_size = last-first;
size_t temp_size = result_last - result_first;
if(temp_size < output_size){
output_size = temp_size;
}
RandomAccessIterator middle = result_first + output_size;
RandomAccessIterator temp = result_first;
while(temp != middle){
*temp = *first;
++temp;
++first;
}
sort(result_first, middle, comp);
while( first != last){
if( comp( *first, *(middle-1) ) ){
*(middle-1) = *first;
sort(result_first, middle, comp);
}
++first;
}
return middle;
}
template<class RandomAccessIterator> _UCXXEXPORT
void nth_element(RandomAccessIterator first, RandomAccessIterator nth, RandomAccessIterator last)
{
less<typename iterator_traits<RandomAccessIterator>::value_type> c;
nth_element(first, nth, last, c);
}
template<class RandomAccessIterator, class Compare> _UCXXEXPORT
void nth_element(RandomAccessIterator first, RandomAccessIterator nth,
RandomAccessIterator last, Compare comp)
{
partial_sort(first, nth, last, comp);
}
template<class ForwardIterator, class T> _UCXXEXPORT
ForwardIterator lower_bound(ForwardIterator first, ForwardIterator last,
const T& value)
{
less<typename iterator_traits<ForwardIterator>::value_type> c;
return lower_bound(first, last, value, c);
}
template<class ForwardIterator, class T, class Compare> _UCXXEXPORT
ForwardIterator lower_bound(ForwardIterator first, ForwardIterator last,
const T& value, Compare comp)
{
if(first == last){
return last;
}
//If below or equal to the first element
if( comp(*first, value) == false){
return first;
}
ForwardIterator middle;
//If greater than the top element
middle = first;
advance(middle, distance(first, last) - 1);
if( comp(*middle, value) ){
return last;
}
//Now begin the actual search for the begining
while( distance(first, last) > 1 ){
//Find middle
middle = first;
advance(middle, (distance(first, last)/2) );
if( !comp(*middle, value) ){
last = middle;
}else{
first = middle;
}
}
if( !comp(*first, value) ){
return first;
}
return last;
}
template<class ForwardIterator, class T> _UCXXEXPORT
ForwardIterator upper_bound(ForwardIterator first, ForwardIterator last,
const T& value)
{
less<typename iterator_traits<ForwardIterator>::value_type> c;
return upper_bound(first, last, value, c);
}
template<class ForwardIterator, class T, class Compare> _UCXXEXPORT
ForwardIterator upper_bound(ForwardIterator first, ForwardIterator last,
const T& value, Compare comp)
{
if(first == last){
return last;