summaryrefslogtreecommitdiffstats
path: root/src/include/algorithm
blob: 8d7d6f62fa6a47006a5bb5f1660bd1bf97ce30f4 (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
/* IBM_PROLOG_BEGIN_TAG                                                   */
/* This is an automatically generated prolog.                             */
/*                                                                        */
/* $Source: src/include/algorithm $                                       */
/*                                                                        */
/* OpenPOWER HostBoot Project                                             */
/*                                                                        */
/* Contributors Listed Below - COPYRIGHT 2011,2016                        */
/* [+] International Business Machines Corp.                              */
/*                                                                        */
/*                                                                        */
/* Licensed under the Apache License, Version 2.0 (the "License");        */
/* you may not use this file except in compliance with the License.       */
/* You may obtain a copy of the License at                                */
/*                                                                        */
/*     http://www.apache.org/licenses/LICENSE-2.0                         */
/*                                                                        */
/* Unless required by applicable law or agreed to in writing, software    */
/* distributed under the License is distributed on an "AS IS" BASIS,      */
/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or        */
/* implied. See the License for the specific language governing           */
/* permissions and limitations under the License.                         */
/*                                                                        */
/* IBM_PROLOG_END_TAG                                                     */
#ifndef ALGORITHM
#define ALGORITHM

#include <iterator>
#include <util/impl/qsort.H>
#include <utility>

#ifdef __cplusplus
namespace std
{
    /**
     * Copy a range of elements
     * @param[in] first InputIterator to the initial position in the source sequence.
     * @param[in] last  InputIterator to last position + 1 in the source sequence.
     * @param[in] result OutputIterator to initial position in the destination sequence.
     * @return an iterator to the last element in the destination range
     * @note If both ranges overlap in such a way that result points to an elmenent in the source
     * range then fuction copy_backward should be used.
     */
    template <class InputIterator, class OutputIterator>
    inline OutputIterator
    copy (InputIterator first, InputIterator last, OutputIterator result )
    {
        while(first!=last)
        {
            *result = *first;
            ++result;
            ++first;
        }
        return result;
    }

    /**
     * Copy a range of elements backwards
     * @param[in] first Bidirectional iterator to the initial source position
     * @param[in] last  Bidirectional iterator to the final source position + 1
     * @param[in] result Bidirectional iterator to end of the destination sequence + 1.
     * @return an iterator to the first element in the destination sequence.
     * @note If both ranges overlap in such a way that result points to an element in the  source
     * range, the function copy should be used instead.
     */
    template <class BidirectionalIterator1, class BidirectionalIterator2>
    inline BidirectionalIterator2
    copy_backward (  BidirectionalIterator1 first,
                     BidirectionalIterator1 last,
                     BidirectionalIterator2 result )
    {
        while(last!=first)
        {
            --result;
            --last;
            *result = *last;
        }
        return result;
    }

    /**
     * Swaps the values of the elements the given iterators are pointing to.
     * @param[in] a iterator to the elements to swap
     * @param[in] b iterator to the elements to swap
     */
    template<class ForwardIterator1, class ForwardIterator2>
    inline void iter_swap(ForwardIterator1 a, ForwardIterator2 b)
    {
        swap(*a, *b);
    }

    /**
     * Swaps the values of the elements the given iterators are pointing to.
     * @param[in] first1 iterator of the beginning of first range
     * @param[in] last1 iterator of the end of first range
     * @param[in] first2 iterator of the beginning of the second range
     * @return Iterator to the element past the last element exchanged in the
     *         range beginning with first2.
     */

    template< class ForwardIterator1, class ForwardIterator2 >
    inline ForwardIterator2 swap_ranges(ForwardIterator1 first1,
                                        ForwardIterator1 last1,
                                        ForwardIterator2 first2)
    {
        while (first1 != last1)
        {
            iter_swap(first1++, first2++);
        }
        return first2;
    }

    /**
     * Exchange values of two objects
     * @param[in] a reference to an object to be swaped with b
     * @param[in] b reference to an object to be swaped with a
     * @note this function may not be an efficient way to swap large objects.
     */
    template <class T>
        inline void
        swap(T& a, T&b )
    {
        T tmp(std::move(a));
        a = std::move(b);
        b = std::move(tmp);
    }

    /**
     * Swaps the arrays a and b. In effect calls std::swap_ranges(a, a+N, b)
     * @param[in] a reference to an object to be swaped with b
     * @param[in] b reference to an object to be swaped with a
     */
    template< class T, size_t N>
    inline void swap(T(&a)[N], T(&b)[N])
    {
        swap_ranges(a, a+N, b);
    }

    /**
     * Fill a range with value
     * @param[in] first ForwardIterator to the first position in the source range.
     * @param[in] last  ForwardIterator to the last position +1 in the source range.
     * @param[in] value reference to the object used to fill the sequence.
     */
    template < class ForwardIterator, class T >
    inline void
    fill (ForwardIterator first, ForwardIterator last, const T& value )
    {
        while (first != last)
        {
            *first = value;
            ++first;
        }
    }

    /**
     * Fill a sequence with value
     * @param[in] first OutputIterator to the first position in the sequence.
     * @param[in] n number of elements in the sequence
     * @param[in] value reference to the value used to fill the sequence.
     */
    template < class OutputIterator, class Size, class T >
    inline void
    fill_n( OutputIterator first, Size n, const T& value )
    {
        for(; n>0; --n)
        {
            *first = value;
            ++first;
        }
    }

    /**
     * Fill a sequence with a generated value
     * @param[in] first OutputIterator to the first position in the sequence.
     * @param[in] n number of elements in the sequence
     * @param[in] gen functor to create values used to fill the sequence.
     */
    template <typename OutputIterator, typename Size, typename Generator>
    OutputIterator generate_n(OutputIterator first, Size n, Generator gen)
    {
        for(; n>0; --n)
        {
            *first = gen();
            ++first;
        }

        return first;
    }

    /**
     * Return the lesser of two arguments
     * @param[in] a object reference
     * @param[in] b object reference
     * @return reference to te lesser object
     */
    template <class T>
    inline const T&
    min(const T& a, const T& b)
    {
        if( b < a) return b;
        return a;
    }

    /**
     * Return the greater of two arguments
     * @param[in] a object reference
     * @param[in] b object reference
     * @return reference to te greater object
     */
    template <class T>
    inline const T&
    max(const T& a, const T& b)
    {
        if(a < b) return b;
        return a;
    }

    /**
     * Find the location of an element within a range.
     * @param[in] first InputIterator to the first position in the range.
     * @param[in] last  InputIterator to the last position in the range.
     * @param[in] value Value to use for comparison.
     *
     * Returns the first iterator i in the range [first,last) such that
     *     (*i == value) or else last if no element is found.
     *
     * @return An iterator in the range [first,last].  last implies that no
     *         matching element was found.
     */
    template <typename InputIterator, typename EqualityComparable>
    inline InputIterator
    find(InputIterator first, InputIterator last,
         const EqualityComparable& value)
    {
        while(first != last)
        {
            if ((*first) == value)
                return first;

            ++first;
        }

        return last;
    }

    /**
     * Find the location of an element within a range.
     * @param[in] first InputIterator to the first position in the range.
     * @param[in] last  InputIterator to the last position in the range.
     * @param[in] pred  Predicate used to compare equality.
     *
     * Returns the first iterator i in the range [first,last) such that
     *     pred(*i) is true or else last if no element is found.
     *
     * @return An iterator in the range [first,last].  last implies that no
     *         matching element was found.
     */
    template <typename InputIterator, typename Predicate>
    inline InputIterator
    find_if(InputIterator first, InputIterator last,
            Predicate pred)
    {
        while(first != last)
        {
            if (pred(*first))
                return first;

            ++first;
        }

        return last;
    }

    /**
     * Find the minimum element within a range.
     * @param[in] first - FwdIterator to the first position in the range.
     * @param[in] last - FwdIterator to the last position in the range.
     *
     * Returns the first element (i) such that (*j) < (*i) is false for all
     * other iterators.
     *
     * The iterator last is returned only when the range contains no elements.
     *
     * @return An iterator in [first, last) containing the minimum element.
     *
     */
    template <typename FwdIterator>
    inline FwdIterator min_element(FwdIterator first, FwdIterator last)
    {
        if (first == last) return last;
        FwdIterator e = first++;
        while(first != last)
        {
            if ((*first) < (*e))
            {
                e = first;
            }
            ++first;
        }
        return e;
    }

    /**
     * Find the minimum element within a range.
     * @param[in] first - FwdIterator to the first position in the range.
     * @param[in] last - FwdIterator to the last position in the range.
     * @param[in] comp - BinaryPredicate used to perform comparison.
     *
     * Returns the first element (i) such that comp(*j,*i) is false for all
     * other iterators.
     *
     * The iterator last is returned only when the range contains no elements.
     *
     * @return An iterator in [first, last) containing the minimum element.
     *
     */
    template <typename FwdIterator, typename BinaryPredicate>
    inline FwdIterator min_element(FwdIterator first, FwdIterator last,
                                   BinaryPredicate comp)
    {
        if (first == last) return last;
        FwdIterator e = first++;
        while(first != last)
        {
            if (comp((*first),(*e)))
            {
                e = first;
            }
            ++first;
        }
        return e;
    }

    /**
     * Find the maximum element within a range.
     * @param[in] first - FwdIterator to the first position in the range.
     * @param[in] last - FwdIterator to the last position in the range.
     *
     * Returns the first element (i) such that (*i) < (*j) is false for all
     * other iterators.
     *
     * The iterator last is returned only when the range contains no elements.
     *
     * @return An iterator in [first, last) containing the minimum element.
     *
     */
    template <typename FwdIterator>
    inline FwdIterator max_element(FwdIterator first, FwdIterator last)
    {
        if (first == last) return last;
        FwdIterator e = first++;
        while(first != last)
        {
            if ((*e) < (*first))
            {
                e = first;
            }
            ++first;
        }
        return e;
    }

    /**
     * Find the maximum element within a range.
     * @param[in] first - FwdIterator to the first position in the range.
     * @param[in] last - FwdIterator to the last position in the range.
     * @param[in] comp - BinaryPredicate used to perform comparison.
     *
     * Returns the first element (i) such that comp(*i,*j) is false for all
     * other iterators.
     *
     * The iterator last is returned only when the range contains no elements.
     *
     * @return An iterator in [first, last) containing the minimum element.
     *
     */
    template <typename FwdIterator, typename BinaryPredicate>
    inline FwdIterator max_element(FwdIterator first, FwdIterator last,
                                   BinaryPredicate comp)
    {
        if (first == last) return last;
        FwdIterator e = first++;
        while(first != last)
        {
            if (comp((*e),(*first)))
            {
                e = first;
            }
            ++first;
        }
        return e;
    }


    /**
     * Find the element value in an ordered range [first, last].  Specifically,
     * it returns the first position where value could be inserted without
     * violating the ordering.
     *
     * @param[in] first ForwardIterator to the first position in the range.
     * @param[in] last  ForwardIterator to the last position in the range.
     * @param[in] value Value to use for comparison.
     */

    template <class ForwardIterator, class LessThanComparable>
    inline ForwardIterator
    lower_bound ( ForwardIterator first,
                  ForwardIterator last,
                  const LessThanComparable& value )
    {
        ForwardIterator it;
        auto range = std::distance<ForwardIterator>( first, last );
        auto num = decltype(range)();

        while( range > 0 )
        {
            it = first;
            num = range / 2;
            std::advance( it, num );

            if( (*it) < value )
            {
                first = ++it;
                range = (range - (num+1));
            }
            else
            {
                range = num;
            }
        }

        return first;
    }

    /**
     * Find the element value in an ordered range [first, last].  Specifically,
     * it returns the first position where value could be inserted without
     * violating the ordering.  This is done using the comparison function
     * parameter that is passed in.
     *
     * @param[in] first ForwardIterator to the first position in the range.
     * @param[in] last  ForwardIterator to the last position in the range.
     * @param[in] value Value to use for comparison.
     * @param[in] comp  Function to do the comparison
     */
    template <class ForwardIterator, class T, class StrictWeakOrdering>
    inline ForwardIterator
    lower_bound ( ForwardIterator first,
                  ForwardIterator last,
                  const T& value,
                  StrictWeakOrdering comp )
    {
        ForwardIterator it;
        auto range = std::distance<ForwardIterator>( first, last );
        auto num = decltype(range)();

        while( range > 0 )
        {
            it = first;
            num = range / 2;
            std::advance( it, num );

            if( comp( (*it), value ) )
            {
                first = ++it;
                range = (range - (num+1));
            }
            else
            {
                range = num;
            }
        }

        return first;
    }

    /**
     * Find the element value in an ordered range [first, last].  Specifically,
     * it returns the first position where value could be inserted without
     * violating the ordering.
     *
     * @param[in] first ForwardIterator to the first position in the range.
     * @param[in] last  ForwardIterator to the last position in the range.
     * @param[in] value Value to use for comparison.
     */

    template <class ForwardIterator, class GreaterThanComparable>
    inline ForwardIterator
    upper_bound ( ForwardIterator first,
                  ForwardIterator last,
                  const GreaterThanComparable& value )
    {
        ForwardIterator it;

        auto range = std::distance<ForwardIterator>( first, last );
        auto num = decltype(range)();

        while( range > 0 )
        {
            it = first;
            num = range / 2;
            std::advance( it, num );

            if( !(value < *it))
            {
                first = ++it;
                range = (range - (num+1));
            }
            else
            {
                range = num;
            }
        }

        return first;
    }

    /**
     * Find the element value in an ordered range [first, last].  Specifically,
     * it returns the first position where value could be inserted without
     * violating the ordering.  This is done using the comparison function
     * parameter that is passed in.
     *
     * @param[in] first ForwardIterator to the first position in the range.
     * @param[in] last  ForwardIterator to the last position in the range.
     * @param[in] value Value to use for comparison.
     * @param[in] comp  Function to do the comparison
     */
    template <class ForwardIterator, class T, class StrictWeakOrdering>
    inline ForwardIterator
    upper_bound ( ForwardIterator first,
                  ForwardIterator last,
                  const T& value,
                  StrictWeakOrdering comp )
    {
        ForwardIterator it;
        auto range = std::distance<ForwardIterator>( first, last );
        auto num = decltype(range)();

        while( range > 0 )
        {
            it = first;
            num = range / 2;
            std::advance( it, num );

            if( !comp( (*it), value ) )
            {
                first = ++it;
                range = (range - (num+1));
            }
            else
            {
                range = num;
            }
        }

        return first;
    }

    /**
     * Test if value exists in sorted sequence
     *
     * @param[in] first ForwardIterator to the first position in the range.
     * @param[in] last  ForwardIterator to the last position in the range.
     * @param[in] value Value to use for comparison.
     *
     * @return true if any element in the range [first,last) is
    *               equivalent to value
     *         false otherwise
     */
    template<class ForwardIterator, class T>
    bool binary_search(ForwardIterator first,
                       ForwardIterator last, const T& value)
    {
        first = std::lower_bound(first, last, value);
        return (!(first == last) && !(value < *first));
    }

    /**
     * Test if value exists in sorted sequence using comparison function
     *
     * @param[in] first ForwardIterator to the first position in the range.
     * @param[in] last  ForwardIterator to the last position in the range.
     * @param[in] value Value to use for comparison.
     * @param[in] comp  Function to do the comparison
     *
     * @return true if any element in the range [first,last) is
     *              equivalent to value
     *         false otherwise
     */
    template<class ForwardIterator, class T,  class StrictWeakOrdering>
    bool binary_search(ForwardIterator first,
                       ForwardIterator last,
                       const T& value,
                       StrictWeakOrdering comp)
    {
        first = std::lower_bound(first, last, value);
        return (!(first == last) && !(comp(value,*first)));
    }

    /**
     * Apply a functor to each element in a range.
     *
     * Applies functor 'f' to each element in [first, last).
     *
     * @param[in] first - The beginning of the range.
     * @param[in] last - The end of the range.
     * @param[in] f - The functor.
     *
     * @return The functor after being having been applied.
     */
    template <typename InputIterator, typename UnaryFunction>
    UnaryFunction for_each(InputIterator first, InputIterator last,
                           UnaryFunction f)
    {
        while(first != last)
        {
            f(*first);
            ++first;
        }
        return f;
    }

    /**
     * Remove a value from a range.
     *
     * Removes all instances matching 'value' in the range [first, last)
     * and returns an iterator to the end of the new range [first, new_last)
     * where nothing in the new range has 'value'.
     *
     * Remove does not decrease the size of the container.
     *
     * @param[in] first - The beginning of the range.
     * @param[in] last - The end of the range.
     * @param[in] value - The value to remove.
     *
     * @return An iterator 'new_last' from [first, new_last).
     */
    template <typename ForwardIterator, typename T>
    ForwardIterator remove(ForwardIterator first, ForwardIterator last,
                           const T& value)
    {
        // Find first match.
        first = find(first, last, value);

        if (first == last) // No match found, return un-changed 'last'.
        {
            return last;
        }

        // Match was found.  'new_last' is now the first removed element.
        ForwardIterator new_last = first;
        ++first;

        // Iterate through all the others.
        while(first != last)
        {
            // If 'first' is a desired value, we need to copy it and move
            // 'new_last'.
            if (!(*first == value))
            {
                *new_last = *first;
                ++new_last;
            }

            ++first;
        }

        return new_last;

    }

    /**
     * Remove a value from a range using a predicate.
     *
     * Removes all instances pred(*i) is true in the range [first, last)
     * and returns an iterator to the end of the new range [first, new_last)
     * where nothing in the new range has pred(*i) true.
     *
     * Remove does not decrease the size of the container.
     *
     * @param[in] first - The beginning of the range.
     * @param[in] last - The end of the range.
     * @param[in] pred - The predicate to use for comparison.
     *
     * @return An iterator 'new_last' from [first, new_last).
     */
    template <typename ForwardIterator, typename Predicate>
    ForwardIterator remove_if(ForwardIterator first, ForwardIterator last,
                              Predicate pred)
    {
        // Find first match.
        first = find_if(first, last, pred);

        if (first == last) // No match found, return un-changed 'last'.
        {
            return last;
        }

        // Match was found.  'new_last' is now the first removed element.
        ForwardIterator new_last = first;
        ++first;

        // Iterate through all the others.
        while(first != last)
        {
            // If 'first' is a desired value, we need to copy it and move
            // 'new_last'.
            if (!(pred(*first)))
            {
                *new_last = *first;
                ++new_last;
            }

            ++first;
        }

        return new_last;

    }

    /**
     * Removes consecutive duplicate entries from a range.
     *
     * Removes all instances where (*i == *(i-1)) in the range [first, last)
     * and returns an iterator to the end of the new range [first, new_last)
     * where nothing in the new range is a consecutive duplicate.
     *
     * Unique does not decrease the size of the container.
     *
     * @param[in] first - The beginning of the range.
     * @param[in] last - The end of the range.
     *
     * @return An iterator 'new_last' from [first, new_last).
     *
     */
    template <typename ForwardIterator>
    ForwardIterator unique(ForwardIterator first, ForwardIterator last)
    {
        // Trivial case of 0 items, return.
        if (first == last) return last;

        // The algorithm keeps 3 iterators 'prev', 'first', and 'last'.  The
        // 'prev' iterator is always the last instance to be kept.  'last' is
        // the end of the original range.  'first' is kept to be the item
        // being compared.

        // Point 'prev' at the first element of the range since first item is
        // a keeper.
        ForwardIterator prev = first;
        ++first;

        while (first != last)
        {
            // If the two items are not the same, we found a new item to keep.
            if (!(*prev == *first))
            {
                // Increment the "keep slot".
                ++prev;

                // If the "keep slot" is not the element being compared, we
                // need to move the new item down to that keep slot.
                if (prev != first)
                {
                    *prev = *first;
                }
            }

            // Advance to the next element.
            ++first;
        }

        // 'prev' points to the last item to be kept.  Increment it to make
        // it point to the one past.
        ++prev;
        return prev;
    }

    /**
     * Removes consecutive duplicate entries from a range by predicate.
     *
     * Removes all instances where pred(*i,*(i-1)) is true in the
     * range [first, last) and returns an iterator to the end of the new
     * range [first, new_last) where nothing in the new range is a
     * consecutive duplicate.
     *
     * Unique does not decrease the size of the container.
     *
     * @param[in] first - The beginning of the range.
     * @param[in] last - The end of the range.
     * @param[in] pred - The predicate.
     *
     * @return An iterator 'new_last' from [first, new_last).
     *
     */
    template <typename ForwardIterator, typename BinaryPredicate>
    ForwardIterator unique(ForwardIterator first, ForwardIterator last,
                           BinaryPredicate pred)
    {
        // Trivial case of 0 items, return.
        if (first == last) return last;

        // The algorithm keeps 3 iterators 'prev', 'first', and 'last'.  The
        // 'prev' iterator is always the last instance to be kept.  'last' is
        // the end of the original range.  'first' is kept to be the item
        // being compared.

        // Point 'prev' at the first element of the range since first item is
        // a keeper.
        ForwardIterator prev = first;
        ++first;

        while (first != last)
        {
            // If the two items are not the same, we found a new item to keep.
            if (!(pred(*prev,*first)))
            {
                // Increment the "keep slot".
                ++prev;

                // If the "keep slot" is not the element being compared, we
                // need to move the new item down to that keep slot.
                if (prev != first)
                {
                    *prev = *first;
                }
            }

            // Advance to the next element.
            ++first;
        }

        // 'prev' points to the last item to be kept.  Increment it to make
        // it point to the one past.
        ++prev;
        return prev;
    }

    /** Sort a range.
     *
     *  Sorts all the elements in [first, last) using such that *i < *(i+1)
     *  for all items in the range.
     *
     *  @param[in] first - The beginning of the range.
     *  @param[in] last - The end of the range.
     */
    template <typename RandomAccessIterator>
    void sort(RandomAccessIterator first, RandomAccessIterator last)
    {
        Util::__Util_QSort_Impl::sort(first, last);
    }

    /** Sort a range using a predicate
     *
     *  Sorts all the elements in [first, last) using such that
     *  pred(*i, *(i+1)) is true for all items in the range.
     *
     *  @param[in] first - The beginning of the range.
     *  @param[in] last - The end of the range.
     *  @param[in] pred - The predicate to use for comparison.
     */
    template <typename RandomAccessIterator, typename StrictWeakOrdering>
    void sort(RandomAccessIterator first, RandomAccessIterator last,
              StrictWeakOrdering pred)
    {
        Util::__Util_QSort_Impl::sort(first, last, pred);
    }

    /** Transform one sequence into another.
     *
     *  Executes an operator against all elements in [first, last) and writes
     *  the result to another sequence.
     *
     *  @param first - Beginning of the input range.
     *  @param last - Ending of the input range.
     *  @param result - Beginning of the output range.
     *  @param op - The transformation operator.
     */
    template <typename InputIterator, typename OutputIterator,
              typename UnaryFunction>
    OutputIterator transform(InputIterator first, InputIterator last,
                             OutputIterator result, UnaryFunction op)
    {
        while (first != last)
        {
            *result = op(*first);
            ++result;
            ++first;
        }
        return result;
    }

    /** Transform two sequences into another.
     *
     *  Executes an operator against all elements in [first1, last1) along
     *  with the peer from [first2, ...) and writes the result to
     *  another sequence.
     *
     *  @param first1 - Beginning of the first input range.
     *  @param last1 - Ending of the first input range.
     *  @param first2 - Beginning of the second input range.
     *  @param result - Beginning of the output range.
     *  @param op - The transformation operator.
     */
    template <typename InputIterator1, typename InputIterator2,
              typename OutputIterator, typename BinaryFunction>
    OutputIterator transform(InputIterator1 first1, InputIterator1 last1,
                             InputIterator2 first2, OutputIterator result,
                             BinaryFunction op)
    {
        while (first1 != last1)
        {
            *result = op(*first1, *first2);
            ++result;
            ++first1; ++first2;
        }
        return result;
    }

    /** Returns true if the range [first1, last1) is equal to the range
     *  [first2, first2 + (last1 - first1)), and false otherwise
     *
     * The two ranges are considered equal if, for every iterator i in the range
     * [first1,last1), *i equals *(first2 + (i - first1)). The overloads use
     * operator== to determine if two elements are equal.
     *
     *  @param[in] first1 - The beginning of the first range.
     *  @param[in] last1 - The end of the first range.
     *  @param[in] first2 - The beginning of the second range.
     *  @return - Returns true equal, false otherwise
     */
    template < typename InputIterator1, typename InputIterator2 >
    bool equal(InputIterator1 first1, InputIterator1 last1,
               InputIterator2 first2)
    {
        while(first1 != last1)
        {
            if (!(*first1 == *first2))
            {
                return false;
            }
            ++first1; ++first2;
        }
        return true;
    }

    /** Returns true if the range [first1, last1) is equal, using a predicate,
     *  to the range [first2, first2 + (last1 - first1)), and false otherwise
     *
     * The two ranges are considered equal if, for every iterator i in the range
     * [first1,last1), *i equals *(first2 + (i - first1)). The overloads use
     * operator== to determine if two elements are equal.
     *
     *  @param[in] first1 - The beginning of the first range.
     *  @param[in] last1 - The end of the first range.
     *  @param[in] first2 - The beginning of the second range.
     *  @param[in] comp - Binary predicate which returns true if the elements
     *                    should be treated as equal.
     *  @return - Returns true equal, false otherwise
     */
    template < typename InputIterator1, typename InputIterator2,
               typename BinaryPredicate >
    bool equal( InputIterator1 first1, InputIterator1 last1,
                InputIterator2 first2, BinaryPredicate comp )
    {
        while(first1 != last1)
        {
            if (!comp(*first1, *first2))
            {
                return false;
            }
            ++first1; ++first2;
        }
        return true;
    }

    /** Checks if the first range [first1, last1) is lexicographically less than
     *  the second range [first2, last2).
     *
     *  Elements are compared using operator<.
     *  Lexicographical comparison is a operation with the following properties:
     *  - Two ranges are compared element by element.
     *  - The first mismatching element defines which range is lexicographically
     *    less or greater than the other.
     *  - If one range is a prefix of another, the shorter range is
     *    lexicographically less than the other.
     *  - If two ranges have equivalent elements and are of the same length,
     *    then the ranges are lexicographically equal.
     *  - An empty range is lexicographically less than any non-empty range.
     *  - Two empty ranges are lexicographically equal.
     *
     *  @param[in] first1 - The beginning of the first range.
     *  @param[in] last1 - The end of the first range.
     *  @param[in] first2 - The beginning of the second range.
     *  @param[in] last2 - The end of the second range.
     *  @return - true if the first range is lexicographically less than the
     *            second.
     */
    template < typename InputIterator1, typename InputIterator2 >
    bool lexicographical_compare(InputIterator1 first1, InputIterator1 last1,
                                 InputIterator2 first2, InputIterator2 last2)
    {
        while( (first1 != last1) && (first2 != last2) )
        {
            if (*first1 < *first2)
            {
                return true;
            }
            if (*first2 < *first1)
            {
                return false;
            }
            first1++; first2++;
        }
        return (first1 == last1) && (first2 != last2);
    }

};
#endif

#endif
/* vim: set filetype=cpp : */
OpenPOWER on IntegriCloud