22 * Copyright (c) 2021-2025 Morwenn
33 * SPDX-License-Identifier: MIT
44 */
5- #ifndef CPPSORT_DETAIL_LONGEST_NON_DESCENDING_SUBSEQUENCE_H_
6- #define CPPSORT_DETAIL_LONGEST_NON_DESCENDING_SUBSEQUENCE_H_
5+ #ifndef CPPSORT_DETAIL_LONGEST_INCREASING_SUBSEQUENCE_H_
6+ #define CPPSORT_DETAIL_LONGEST_INCREASING_SUBSEQUENCE_H_
77
88// //////////////////////////////////////////////////////////
99// Headers
1515#include < cpp-sort/utility/as_function.h>
1616#include < cpp-sort/utility/functional.h>
1717#include " iterator_traits.h"
18- #include " upper_bound .h"
18+ #include " lower_bound .h"
1919
2020namespace cppsort ::detail
2121{
22- // Longest non-decreasing subsequence, computed with an altered
22+ // Longest increasing subsequence, computed with an altered
2323 // patience sorting algorithm - returns a pair containing the
24- // size of the LNDS and the size of the collection
24+ // size of the LIS and the size of the collection
2525
2626 template <
2727 bool RecomputeSize,
2828 typename ForwardIterator,
2929 typename Compare,
3030 typename Projection
3131 >
32- auto longest_non_descending_subsequence (ForwardIterator first, ForwardIterator last,
33- difference_type_t <ForwardIterator> size,
34- Compare compare, Projection projection)
32+ auto longest_increasing_subsequence (ForwardIterator first, ForwardIterator last,
33+ difference_type_t <ForwardIterator> size,
34+ Compare compare, Projection projection)
3535 -> std::pair<difference_type_t<ForwardIterator>, difference_type_t<ForwardIterator>>
3636 {
3737 constexpr bool is_random_access = std::is_base_of_v<
@@ -59,18 +59,18 @@ namespace cppsort::detail
5959 // Top (smaller) elements in patience sorting stacks
6060 std::vector<ForwardIterator> stack_tops;
6161
62- while (first != last) {
63- auto it = detail::upper_bound (
62+ do {
63+ auto it = detail::lower_bound (
6464 stack_tops.begin (), stack_tops.end (),
6565 proj (*first), compare, utility::indirect{} | projection);
6666
6767 if (it == stack_tops.end ()) {
68- // The element is bigger than everything else,
68+ // The element is strictly bigger than everything else,
6969 // create a new "stack" to put it
7070 stack_tops.emplace_back (first);
7171 } else {
72- // The element is strictly smaller than the top
73- // of a given stack, replace the stack top
72+ // The element is strictly smaller than or equal to
73+ // the top of a given stack, replace the stack top
7474 *it = first;
7575 }
7676 ++first;
@@ -79,10 +79,10 @@ namespace cppsort::detail
7979 // Compute the size as-we-go if iterators are not random-access
8080 ++size;
8181 }
82- }
82+ } while (first != last);
8383
8484 return { stack_tops.size (), size };
8585 }
8686}
8787
88- #endif // CPPSORT_DETAIL_LONGEST_NON_DESCENDING_SUBSEQUENCE_H_
88+ #endif // CPPSORT_DETAIL_LONGEST_INCREASING_SUBSEQUENCE_H_
0 commit comments