121 template <
typename T>
123 m_compare (ascending_compare), m_ms (nullptr)
126 template <
typename T>
128 : m_compare (comp), m_ms (nullptr)
131 template <
typename T>
137 template <
typename T>
142 m_compare = ascending_compare;
144 m_compare = descending_compare;
149 template <
typename T>
150 template <
typename Comp>
158 for (; start < nel; ++start)
163 T pivot = data[start];
172 if (comp (pivot, data[p]))
187 std::swap (pivot, data[p]);
194 template <
typename T>
195 template <
typename Comp>
203 for (; start < nel; ++start)
208 T pivot = data[start];
217 if (comp (pivot, data[p]))
232 std::swap (pivot, data[p]);
236 std::swap (ipivot, idx[p]);
261 template <
typename T>
262 template <
typename Comp>
277 if (comp (*lo, *(lo-1)))
280 for (lo = lo+1; lo < hi; ++lo, ++
n)
282 if (comp (*lo, *(lo-1)))
290 for (lo = lo+1; lo < hi; ++lo, ++
n)
292 if (comp (*lo, *(lo-1)))
321 template <
typename T>
322 template <
typename Comp>
343 if (comp (a[ofs], key))
346 ofs = (ofs << 1) + 1;
367 if (comp (*(a-ofs), key))
371 ofs = (ofs << 1) + 1;
379 lastofs = hint - ofs;
389 while (lastofs < ofs)
393 if (comp (a[
m], key))
416 template <
typename T>
417 template <
typename Comp>
438 if (comp (key, *(a-ofs)))
441 ofs = (ofs << 1) + 1;
452 lastofs = hint - ofs;
463 if (comp (key, a[ofs]))
467 ofs = (ofs << 1) + 1;
484 while (lastofs < ofs)
488 if (comp (key, a[
m]))
500 unsigned int nbits = 3;
501 std::size_t n2 =
n >> 8;
531 std::size_t new_size = ((
n >> nbits) + 1) << nbits;
536 (*current_liboctave_error_handler)
537 (
"unable to allocate sufficient memory for sort");
545 template <
typename T>
563 template <
typename T>
567 if (m_ia && need <= m_alloced)
588 template <
typename T>
589 template <
typename Comp>
602 std::copy (pa, pa + na,
m_ms->
m_a);
635 if (bcount >= min_gallop)
646 if (acount >= min_gallop)
659 min_gallop -= (min_gallop > 1);
667 dest = std::copy (pa, pa + k, dest);
690 dest = std::copy (pb, pb + k, dest);
712 std::copy (pa, pa + na, dest);
717 std::copy (pb, pb + nb, dest);
723 template <
typename T>
724 template <
typename Comp>
738 std::copy (pa, pa + na,
m_ms->
m_a);
739 std::copy (ipa, ipa + na,
m_ms->
m_ia);
740 dest = pa; idest = ipa;
743 *dest++ = *pb++; *idest++ = *ipb++;
763 *dest++ = *pb++; *idest++ = *ipb++;
769 if (bcount >= min_gallop)
774 *dest++ = *pa++; *idest++ = *ipa++;
780 if (acount >= min_gallop)
793 min_gallop -= (min_gallop > 1);
801 dest = std::copy (pa, pa + k, dest);
802 idest = std::copy (ipa, ipa + k, idest);
814 *dest++ = *pb++; *idest++ = *ipb++;
825 dest = std::copy (pb, pb + k, dest);
826 idest = std::copy (ipb, ipb + k, idest);
832 *dest++ = *pa++; *idest++ = *ipa++;
849 std::copy (pa, pa + na, dest);
850 std::copy (ipa, ipa + na, idest);
856 std::copy (pb, pb + nb, dest);
857 std::copy (ipb, ipb + nb, idest);
870 template <
typename T>
871 template <
typename Comp>
886 std::copy (pb, pb + nb,
m_ms->
m_a);
917 if (acount >= min_gallop)
928 if (bcount >= min_gallop)
941 min_gallop -= (min_gallop > 1);
950 dest = std::copy_backward (pa+1 - k, pa+1, dest+1) - 1;
970 std::copy (pb+1, pb+1 + k, dest+1);
995 std::copy (baseb, baseb + nb, dest-(nb-1));
1000 dest = std::copy_backward (pa+1 - na, pa+1, dest+1) - 1;
1007 template <
typename T>
1008 template <
typename Comp>
1025 idest = ipb + nb - 1;
1026 std::copy (pb, pb + nb,
m_ms->
m_a);
1027 std::copy (ipb, ipb + nb,
m_ms->
m_ia);
1031 pa += na - 1; ipa += na - 1;
1033 *dest-- = *pa--; *idest-- = *ipa--;
1050 if (comp (*pb, *pa))
1052 *dest-- = *pa--; *idest-- = *ipa--;
1058 if (acount >= min_gallop)
1063 *dest-- = *pb--; *idest-- = *ipb--;
1069 if (bcount >= min_gallop)
1082 min_gallop -= (min_gallop > 1);
1091 dest = std::copy_backward (pa+1 - k, pa+1, dest+1) - 1;
1092 idest = std::copy_backward (ipa+1 - k, ipa+1, idest+1) - 1;
1098 *dest-- = *pb--; *idest-- = *ipb--;
1110 dest -= k; idest -= k;
1112 std::copy (pb+1, pb+1 + k, dest+1);
1113 std::copy (ipb+1, ipb+1 + k, idest+1);
1124 *dest-- = *pa--; *idest-- = *ipa--;
1139 std::copy (baseb, baseb + nb, dest-(nb-1));
1140 std::copy (ibaseb, ibaseb + nb, idest-(nb-1));
1146 dest = std::copy_backward (pa+1 - na, pa+1, dest+1) - 1;
1147 idest = std::copy_backward (ipa+1 - na, ipa+1, idest+1) - 1;
1148 pa -= na; ipa -= na;
1149 *dest = *pb; *idest = *ipb;
1157 template <
typename T>
1158 template <
typename Comp>
1203 return merge_lo (pa, na, pb, nb, comp);
1205 return merge_hi (pa, na, pb, nb, comp);
1208 template <
typename T>
1209 template <
typename Comp>
1257 return merge_lo (pa, ipa, na, pb, ipb, nb, comp);
1259 return merge_hi (pa, ipa, na, pb, ipb, nb, comp);
1272 template <
typename T>
1273 template <
typename Comp>
1282 if (
n > 0 && p[
n-1].m_len <= p[
n].m_len + p[
n+1].m_len)
1284 if (p[
n-1].m_len < p[
n+1].m_len)
1289 else if (p[
n].m_len <= p[
n+1].m_len)
1301 template <
typename T>
1302 template <
typename Comp>
1311 if (
n > 0 && p[
n-1].m_len <= p[
n].m_len + p[
n+1].m_len)
1313 if (p[
n-1].m_len < p[
n+1].m_len)
1318 else if (p[
n].m_len <= p[
n+1].m_len)
1335 template <
typename T>
1336 template <
typename Comp>
1345 if (
n > 0 && p[
n-1].m_len < p[
n+1].m_len)
1354 template <
typename T>
1355 template <
typename Comp>
1364 if (
n > 0 && p[
n-1].m_len < p[
n+1].m_len)
1383 template <
typename T>
1398 template <
typename T>
1399 template <
typename Comp>
1424 n =
count_run (data + lo, nremaining, descending, comp);
1428 std::reverse (data + lo, data + lo +
n);
1454 template <
typename T>
1455 template <
typename Comp>
1481 n =
count_run (data + lo, nremaining, descending, comp);
1486 std::reverse (data + lo, data + lo +
n);
1487 std::reverse (idx + lo, idx + lo +
n);
1514 template <
typename T>
1518 template <
typename T>
1522 #if defined (INLINE_ASCENDING_SORT)
1524 sort (data, nel, std::less<T> ());
1527 #if defined (INLINE_DESCENDING_SORT)
1529 sort (data, nel, std::greater<T> ());
1536 template <
typename T>
1540 #if defined (INLINE_ASCENDING_SORT)
1542 sort (data, idx, nel, std::less<T> ());
1545 #if defined (INLINE_DESCENDING_SORT)
1547 sort (data, idx, nel, std::greater<T> ());
1554 template <
typename T>
1555 template <
typename Comp>
1559 const T *end = data + nel;
1562 const T *
next = data;
1563 while (++
next != end)
1565 if (comp (*
next, *data))
1575 template <
typename T>
1579 bool retval =
false;
1580 #if defined (INLINE_ASCENDING_SORT)
1582 retval =
issorted (data, nel, std::less<T> ());
1585 #if defined (INLINE_DESCENDING_SORT)
1587 retval =
issorted (data, nel, std::greater<T> ());
1600 : col (c), ofs (o), nel (
n) { }
1605 template <
typename T>
1606 template <
typename Comp>
1616 if (cols == 0 || rows <= 1)
1621 std::stack<run_t> runs;
1623 runs.push (run_t (0, 0, rows));
1625 while (! runs.empty ())
1633 T *lbuf = buf + ofs;
1634 const T *ldata = data + rows*col;
1639 lbuf[i] = ldata[lidx[i]];
1642 sort (lbuf, lidx, nel, comp);
1650 if (comp (lbuf[lst], lbuf[i]))
1653 runs.push (run_t (col+1, ofs + lst, i - lst));
1658 runs.push (run_t (col+1, ofs + lst, nel - lst));
1663 template <
typename T>
1668 #if defined (INLINE_ASCENDING_SORT)
1670 sort_rows (data, idx, rows, cols, std::less<T> ());
1673 #if defined (INLINE_DESCENDING_SORT)
1675 sort_rows (data, idx, rows, cols, std::greater<T> ());
1682 template <
typename T>
1683 template <
typename Comp>
1688 if (rows <= 1 || cols == 0)
1692 const T *lastrow = data + rows*(cols - 1);
1693 typedef std::pair<const T *, octave_idx_type> run_t;
1694 std::stack<run_t> runs;
1697 runs.push (run_t (data, rows));
1698 while (sorted && ! runs.empty ())
1700 const T *lo = runs.top ().first;
1707 const T *hi = lo +
n;
1709 for (lo++; lo < hi; lo++)
1711 if (comp (*lst, *lo))
1714 runs.push (run_t (lst + rows, lo - lst));
1717 else if (comp (*lo, *lst))
1724 runs.push (run_t (lst + rows, lo - lst));
1740 template <
typename T>
1745 bool retval =
false;
1746 #if defined (INLINE_ASCENDING_SORT)
1751 #if defined (INLINE_DESCENDING_SORT)
1764 template <
typename T>
1765 template <
typename Comp>
1768 const T& value, Comp comp)
1776 if (comp (value, data[mid]))
1785 template <
typename T>
1791 #if defined (INLINE_ASCENDING_SORT)
1793 retval =
lookup (data, nel, value, std::less<T> ());
1796 #if defined (INLINE_DESCENDING_SORT)
1798 retval =
lookup (data, nel, value, std::greater<T> ());
1807 template <
typename T>
1808 template <
typename Comp>
1818 idx[j] =
lookup (data, nel, values[j], comp);
1821 template <
typename T>
1827 #if defined (INLINE_ASCENDING_SORT)
1829 lookup (data, nel, values, nvalues, idx, std::less<T> ());
1832 #if defined (INLINE_DESCENDING_SORT)
1834 lookup (data, nel, values, nvalues, idx, std::greater<T> ());
1841 template <
typename T>
1842 template <
typename Comp>
1853 if (nvalues > 0 && nel > 0)
1857 if (comp (values[j], data[i]))
1863 else if (++i == nel)
1876 if (nvalues > 0 && nel > 0)
1880 if (comp (values[j], data[i]))
1886 else if (++i == nel)
1891 for (; j != nvalues; j++)
1896 template <
typename T>
1902 #if defined (INLINE_ASCENDING_SORT)
1904 lookup_sorted (data, nel, values, nvalues, idx, rev, std::less<T> ());
1907 #if defined (INLINE_DESCENDING_SORT)
1909 lookup_sorted (data, nel, values, nvalues, idx, rev, std::greater<T> ());
1916 template <
typename T>
1917 template <
typename Comp>
1926 std::nth_element (data, data + lo, data + nel, comp);
1928 std::partial_sort (data, data + up, data + nel, comp);
1931 std::nth_element (data, data + lo, data + nel, comp);
1935 std::swap (data[lo+1],
1936 *std::min_element (data + lo + 1, data + nel, comp));
1939 std::partial_sort (data + lo + 1, data + up, data + nel, comp);
1943 template <
typename T>
1951 #if defined (INLINE_ASCENDING_SORT)
1956 #if defined (INLINE_DESCENDING_SORT)
1958 nth_element (data, nel, lo, up, std::greater<T> ());
1965 template <
typename T>
1973 template <
typename T>
charNDArray max(char d, const charNDArray &m)
void set_compare(const compare_fcn_type &comp)
static const int MERGESTATE_TEMP_SIZE
int merge_collapse(T *data, Comp comp)
void sort_rows(const T *data, octave_idx_type *idx, octave_idx_type rows, octave_idx_type cols)
octave_idx_type gallop_right(T key, T *a, octave_idx_type n, octave_idx_type hint, Comp comp)
int merge_hi(T *pa, octave_idx_type na, T *pb, octave_idx_type nb, Comp comp)
octave_idx_type count_run(T *lo, octave_idx_type n, bool &descending, Comp comp)
compare_fcn_type m_compare
octave_idx_type gallop_left(T key, T *a, octave_idx_type n, octave_idx_type hint, Comp comp)
static bool descending_compare(typename ref_param< T >::type, typename ref_param< T >::type)
bool issorted(const T *data, octave_idx_type nel)
std::function< bool(typename ref_param< T >::type, typename ref_param< T >::type)> compare_fcn_type
static bool ascending_compare(typename ref_param< T >::type, typename ref_param< T >::type)
octave_idx_type lookup(const T *data, octave_idx_type nel, const T &value)
int merge_at(octave_idx_type i, T *data, Comp comp)
octave_idx_type merge_compute_minrun(octave_idx_type n)
void binarysort(T *data, octave_idx_type nel, octave_idx_type start, Comp comp)
void sort(T *data, octave_idx_type nel)
static const int MIN_GALLOP
void nth_element(T *data, octave_idx_type nel, octave_idx_type lo, octave_idx_type up=-1)
bool is_sorted_rows(const T *data, octave_idx_type rows, octave_idx_type cols)
static const int MAX_MERGE_PENDING
int merge_force_collapse(T *data, Comp comp)
int merge_lo(T *pa, octave_idx_type na, T *pb, octave_idx_type nb, Comp comp)
void lookup_sorted(const T *data, octave_idx_type nel, const T *values, octave_idx_type nvalues, octave_idx_type *idx, bool rev=false)
if_then_else< is_class_type< T >::no, T, T const & >::result type
F77_RET_T const F77_DBLE * x
#define OCTAVE_LOCAL_BUFFER(T, buf, size)
bool(*)(typename ref_param< T >::type, typename ref_param< T >::type) compare_fcn_ptr
static octave_idx_type roundupsize(std::size_t n)
void getmem(octave_idx_type need)
struct s_slice m_pending[MAX_MERGE_PENDING]
octave_idx_type m_alloced
void getmemi(octave_idx_type need)
octave_idx_type m_min_gallop
sortrows_run_t(octave_idx_type c, octave_idx_type o, octave_idx_type n)