From 931d54184d5b2cd03eab3d16f6b3b509679421f8 Mon Sep 17 00:00:00 2001 From: MikeGitb Date: Tue, 6 Dec 2016 00:10:07 +0100 Subject: [PATCH] [algorithm] Add nth_element --- gsl/gsl_algorithm | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/gsl/gsl_algorithm b/gsl/gsl_algorithm index fc5b101..b7cf753 100644 --- a/gsl/gsl_algorithm +++ b/gsl/gsl_algorithm @@ -45,7 +45,8 @@ void copy(span src, span { static_assert(std::is_assignable::value, "Elements of source span can not be assigned to elements of destination span"); - static_assert(SrcExtent == dynamic_extent || DestExtent == dynamic_extent || (SrcExtent <= DestExtent), + static_assert(SrcExtent == dynamic_extent || DestExtent == dynamic_extent || + (SrcExtent <= DestExtent), "Source range is longer than target range"); Expects(dest.size() >= src.size()); @@ -152,6 +153,21 @@ void stable_sort(span rng, Compare comp) std::stable_sort(rng.data(), rng.data() + rng.size(), comp); } +// nth_element +template +void nth_element(span rng, + typename span::index_type nth) +{ + std::nth_element(rng.data(), &rng[nth], rng.data() + rng.size()); +} + +template +void nth_element(span rng, + typename span::index_type nth, Compare comp) +{ + std::nth_element(rng.data(), &rng[nth], rng.data() + rng.size(), comp); +} + } // namespace gsl #ifdef _MSC_VER