Skip to content

Commit d6c26f1

Browse files
committed
Take kth_container by value in xt::partition
1 parent 2982403 commit d6c26f1

File tree

1 file changed

+16
-24
lines changed

1 file changed

+16
-24
lines changed

include/xtensor/xsort.hpp

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -480,14 +480,6 @@ namespace xt
480480
}
481481
);
482482
}
483-
484-
template <class C>
485-
inline auto sorted(C&& container)
486-
{
487-
auto container_copy = std::forward<C>(container);
488-
std::sort(container_copy.begin(), container_copy.end());
489-
return std::move(container_copy);
490-
}
491483
}
492484

493485
/**
@@ -522,16 +514,16 @@ namespace xt
522514
class C,
523515
class R = detail::flatten_sort_result_type_t<E>,
524516
class = std::enable_if_t<!xtl::is_integral<C>::value, int>>
525-
inline R partition(const xexpression<E>& e, C&& kth_container, placeholders::xtuph /*ax*/)
517+
inline R partition(const xexpression<E>& e, C kth_container, placeholders::xtuph /*ax*/)
526518
{
527519
const auto& de = e.derived_cast();
528520

529521
R ev = R::from_shape({de.size()});
530-
auto kth_sorted = detail::sorted(std::forward<C>(kth_container));
522+
std::sort(kth_container.begin(), kth_container.end());
531523

532524
std::copy(de.linear_cbegin(), de.linear_cend(), ev.linear_begin()); // flatten
533525

534-
detail::partition_iter(ev.linear_begin(), ev.linear_end(), kth_sorted.rbegin(), kth_sorted.rend());
526+
detail::partition_iter(ev.linear_begin(), ev.linear_end(), kth_container.rbegin(), kth_container.rend());
535527

536528
return ev;
537529
}
@@ -553,18 +545,18 @@ namespace xt
553545
}
554546

555547
template <class E, class C, class = std::enable_if_t<!xtl::is_integral<C>::value, int>>
556-
inline auto partition(const xexpression<E>& e, C&& kth_container, std::ptrdiff_t axis = -1)
548+
inline auto partition(const xexpression<E>& e, C kth_container, std::ptrdiff_t axis = -1)
557549
{
558550
using eval_type = typename detail::sort_eval_type<E>::type;
559551

560-
auto kth_sorted = detail::sorted(std::forward<C>(kth_container));
552+
std::sort(kth_container.begin(), kth_container.end());
561553

562554
return detail::map_axis<eval_type>(
563555
e.derived_cast(),
564556
axis,
565-
[&kth_sorted](auto begin, auto end)
557+
[&kth_container](auto begin, auto end)
566558
{
567-
detail::partition_iter(begin, end, kth_sorted.rbegin(), kth_sorted.rend());
559+
detail::partition_iter(begin, end, kth_container.rbegin(), kth_container.rend());
568560
}
569561
);
570562
}
@@ -617,7 +609,7 @@ namespace xt
617609
class C,
618610
class R = typename detail::linear_argsort_result_type<typename detail::sort_eval_type<E>::type>::type,
619611
class = std::enable_if_t<!xtl::is_integral<C>::value, int>>
620-
inline R argpartition(const xexpression<E>& e, C&& kth_container, placeholders::xtuph)
612+
inline R argpartition(const xexpression<E>& e, C kth_container, placeholders::xtuph)
621613
{
622614
using eval_type = typename detail::sort_eval_type<E>::type;
623615
using result_type = typename detail::linear_argsort_result_type<eval_type>::type;
@@ -626,15 +618,15 @@ namespace xt
626618

627619
result_type res = result_type::from_shape({de.size()});
628620

629-
auto kth_sorted = detail::sorted(std::forward<C>(kth_container));
621+
std::sort(kth_container.begin(), kth_container.end());
630622

631623
std::iota(res.linear_begin(), res.linear_end(), 0);
632624

633625
detail::partition_iter(
634626
res.linear_begin(),
635627
res.linear_end(),
636-
kth_sorted.rbegin(),
637-
kth_sorted.rend(),
628+
kth_container.rbegin(),
629+
kth_container.rend(),
638630
[&de](std::size_t a, std::size_t b)
639631
{
640632
return de[a] < de[b];
@@ -661,7 +653,7 @@ namespace xt
661653
}
662654

663655
template <class E, class C, class = std::enable_if_t<!xtl::is_integral<C>::value, int>>
664-
inline auto argpartition(const xexpression<E>& e, C&& kth_container, std::ptrdiff_t axis = -1)
656+
inline auto argpartition(const xexpression<E>& e, C kth_container, std::ptrdiff_t axis = -1)
665657
{
666658
using eval_type = typename detail::sort_eval_type<E>::type;
667659
using result_type = typename detail::argsort_result_type<eval_type>::type;
@@ -673,16 +665,16 @@ namespace xt
673665
return argpartition<E, C, result_type>(e, std::forward<C>(kth_container), xnone());
674666
}
675667

676-
auto kth_sorted = detail::sorted(std::forward<C>(kth_container));
668+
std::sort(kth_container.begin(), kth_container.end());
677669
const auto argpartition_w_kth =
678-
[&kth_sorted](auto res_begin, auto res_end, auto ev_begin, auto /*ev_end*/)
670+
[&kth_container](auto res_begin, auto res_end, auto ev_begin, auto /*ev_end*/)
679671
{
680672
std::iota(res_begin, res_end, 0);
681673
detail::partition_iter(
682674
res_begin,
683675
res_end,
684-
kth_sorted.rbegin(),
685-
kth_sorted.rend(),
676+
kth_container.rbegin(),
677+
kth_container.rend(),
686678
[&ev_begin](auto const& i, auto const& j)
687679
{
688680
return *(ev_begin + i) < *(ev_begin + j);

0 commit comments

Comments
 (0)