Mercurial > octave
changeset 29280:448bbc1f99a1
oct-sort.h: Use std::function for passing compare function.
* oct-sort.h, oct-sort.cc (octave_sort::compare_fcn_type): Use std::function
instead of bare function pointer.
(octave_sort::octave_sort, octave_sort::set_compare): Pass compare function by
const reference.
(octave_sort::sort, octave_sort::issorted, octave_sort::sort_row,
octave_sort::issorted_rows): Compare to function target.
(octave_sort::lookup, octave_sort::lookup_sorted, octave_sort::nth_element):
Compare to function target. Remove deprecated std::ptr_fun.
author | Markus Mützel <markus.muetzel@gmx.de> |
---|---|
date | Fri, 08 Jan 2021 11:17:59 +0100 |
parents | e4c152e827aa |
children | 6dd456257d81 |
files | liboctave/util/oct-sort.cc liboctave/util/oct-sort.h |
diffstat | 2 files changed, 35 insertions(+), 32 deletions(-) [+] |
line wrap: on
line diff
--- a/liboctave/util/oct-sort.cc Thu Jan 07 18:34:18 2021 +0100 +++ b/liboctave/util/oct-sort.cc Fri Jan 08 11:17:59 2021 +0100 @@ -109,7 +109,6 @@ #include <cassert> #include <algorithm> -#include <functional> #include <cstring> #include <stack> @@ -125,7 +124,7 @@ { } template <typename T> -octave_sort<T>::octave_sort (compare_fcn_type comp) +octave_sort<T>::octave_sort (const compare_fcn_type& comp) : m_compare (comp), m_ms (nullptr) { } @@ -144,7 +143,7 @@ else if (mode == DESCENDING) m_compare = descending_compare; else - m_compare = nullptr; + m_compare = compare_fcn_type (); } template <typename T> @@ -1513,16 +1512,20 @@ } template <typename T> +using compare_fcn_ptr = bool (*) (typename ref_param<T>::type, + typename ref_param<T>::type); + +template <typename T> void octave_sort<T>::sort (T *data, octave_idx_type nel) { #if defined (INLINE_ASCENDING_SORT) - if (m_compare == ascending_compare) + if (*m_compare.template target<compare_fcn_ptr<T>> () == ascending_compare) sort (data, nel, std::less<T> ()); else #endif #if defined (INLINE_DESCENDING_SORT) - if (m_compare == descending_compare) + if (*m_compare.template target<compare_fcn_ptr<T>> () == descending_compare) sort (data, nel, std::greater<T> ()); else #endif @@ -1535,12 +1538,12 @@ octave_sort<T>::sort (T *data, octave_idx_type *idx, octave_idx_type nel) { #if defined (INLINE_ASCENDING_SORT) - if (m_compare == ascending_compare) + if (*m_compare.template target<compare_fcn_ptr<T>> () == ascending_compare) sort (data, idx, nel, std::less<T> ()); else #endif #if defined (INLINE_DESCENDING_SORT) - if (m_compare == descending_compare) + if (*m_compare.template target<compare_fcn_ptr<T>> () == descending_compare) sort (data, idx, nel, std::greater<T> ()); else #endif @@ -1575,12 +1578,12 @@ { bool retval = false; #if defined (INLINE_ASCENDING_SORT) - if (m_compare == ascending_compare) + if (*m_compare.template target<compare_fcn_ptr<T>> () == ascending_compare) retval = issorted (data, nel, std::less<T> ()); else #endif #if defined (INLINE_DESCENDING_SORT) - if (m_compare == descending_compare) + if (*m_compare.template target<compare_fcn_ptr<T>> () == descending_compare) retval = issorted (data, nel, std::greater<T> ()); else #endif @@ -1662,12 +1665,12 @@ octave_idx_type rows, octave_idx_type cols) { #if defined (INLINE_ASCENDING_SORT) - if (m_compare == ascending_compare) + if (*m_compare.template target<compare_fcn_ptr<T>> () == ascending_compare) sort_rows (data, idx, rows, cols, std::less<T> ()); else #endif #if defined (INLINE_DESCENDING_SORT) - if (m_compare == descending_compare) + if (*m_compare.template target<compare_fcn_ptr<T>> () == descending_compare) sort_rows (data, idx, rows, cols, std::greater<T> ()); else #endif @@ -1739,14 +1742,13 @@ octave_idx_type cols) { bool retval = false; - #if defined (INLINE_ASCENDING_SORT) - if (m_compare == ascending_compare) + if (*m_compare.template target<compare_fcn_ptr<T>> () == ascending_compare) retval = is_sorted_rows (data, rows, cols, std::less<T> ()); else #endif #if defined (INLINE_DESCENDING_SORT) - if (m_compare == descending_compare) + if (*m_compare.template target<compare_fcn_ptr<T>> () == descending_compare) retval = is_sorted_rows (data, rows, cols, std::greater<T> ()); else #endif @@ -1785,19 +1787,18 @@ const T& value) { octave_idx_type retval = 0; - #if defined (INLINE_ASCENDING_SORT) - if (m_compare == ascending_compare) + if (*m_compare.template target<compare_fcn_ptr<T>> () == ascending_compare) retval = lookup (data, nel, value, std::less<T> ()); else #endif #if defined (INLINE_DESCENDING_SORT) - if (m_compare == descending_compare) + if (*m_compare.template target<compare_fcn_ptr<T>> () == descending_compare) retval = lookup (data, nel, value, std::greater<T> ()); else #endif if (m_compare) - retval = lookup (data, nel, value, std::ptr_fun (m_compare)); + retval = lookup (data, nel, value, m_compare); return retval; } @@ -1823,17 +1824,17 @@ octave_idx_type *idx) { #if defined (INLINE_ASCENDING_SORT) - if (m_compare == ascending_compare) + if (*m_compare.template target<compare_fcn_ptr<T>> () == ascending_compare) lookup (data, nel, values, nvalues, idx, std::less<T> ()); else #endif #if defined (INLINE_DESCENDING_SORT) - if (m_compare == descending_compare) + if (*m_compare.template target<compare_fcn_ptr<T>> () == descending_compare) lookup (data, nel, values, nvalues, idx, std::greater<T> ()); else #endif if (m_compare) - lookup (data, nel, values, nvalues, idx, std::ptr_fun (m_compare)); + lookup (data, nel, values, nvalues, idx, m_compare); } template <typename T> @@ -1898,18 +1899,17 @@ octave_idx_type *idx, bool rev) { #if defined (INLINE_ASCENDING_SORT) - if (m_compare == ascending_compare) + if (*m_compare.template target<compare_fcn_ptr<T>> () == ascending_compare) lookup_sorted (data, nel, values, nvalues, idx, rev, std::less<T> ()); else #endif #if defined (INLINE_DESCENDING_SORT) - if (m_compare == descending_compare) + if (*m_compare.template target<compare_fcn_ptr<T>> () == descending_compare) lookup_sorted (data, nel, values, nvalues, idx, rev, std::greater<T> ()); else #endif if (m_compare) - lookup_sorted (data, nel, values, nvalues, idx, rev, - std::ptr_fun (m_compare)); + lookup_sorted (data, nel, values, nvalues, idx, rev, m_compare); } template <typename T> @@ -1946,18 +1946,19 @@ { if (up < 0) up = lo + 1; + #if defined (INLINE_ASCENDING_SORT) - if (m_compare == ascending_compare) + if (*m_compare.template target<compare_fcn_ptr<T>> () == ascending_compare) nth_element (data, nel, lo, up, std::less<T> ()); else #endif #if defined (INLINE_DESCENDING_SORT) - if (m_compare == descending_compare) + if (*m_compare.template target<compare_fcn_ptr<T>> () == descending_compare) nth_element (data, nel, lo, up, std::greater<T> ()); else #endif if (m_compare) - nth_element (data, nel, lo, up, std::ptr_fun (m_compare)); + nth_element (data, nel, lo, up, m_compare); } template <typename T>
--- a/liboctave/util/oct-sort.h Thu Jan 07 18:34:18 2021 +0100 +++ b/liboctave/util/oct-sort.h Fri Jan 08 11:17:59 2021 +0100 @@ -87,6 +87,8 @@ #if ! defined (octave_oct_sort_h) #define octave_oct_sort_h 1 +#include <functional> + #include "octave-config.h" #include "lo-traits.h" @@ -100,12 +102,12 @@ { public: - typedef bool (*compare_fcn_type) (typename ref_param<T>::type, - typename ref_param<T>::type); + typedef std::function<bool (typename ref_param<T>::type, + typename ref_param<T>::type)> compare_fcn_type; octave_sort (void); - octave_sort (compare_fcn_type); + octave_sort (const compare_fcn_type&); // No copying! @@ -115,7 +117,7 @@ ~octave_sort (void); - void set_compare (compare_fcn_type comp) { m_compare = comp; } + void set_compare (const compare_fcn_type& comp) { m_compare = comp; } void set_compare (sortmode mode);