110 #include <functional>
121 compare (ascending_compare), ms (0)
127 : compare (comp), ms (0)
142 compare = ascending_compare;
144 compare = descending_compare;
150 template <
class Comp>
158 for (; start < nel; ++start)
162 T pivot = data[start];
171 if (comp (pivot, data[p]))
186 std::swap (pivot, data[p]);
194 template <
class Comp>
202 for (; start < nel; ++start)
206 T pivot = data[start];
215 if (comp (pivot, data[p]))
230 std::swap (pivot, data[p]);
234 std::swap (ipivot, idx[p]);
260 template <
class Comp>
275 if (comp (*lo, *(lo-1)))
278 for (lo = lo+1; lo < hi; ++lo, ++n)
280 if (comp (*lo, *(lo-1)))
288 for (lo = lo+1; lo < hi; ++lo, ++n)
290 if (comp (*lo, *(lo-1)))
320 template <
class Comp>
341 if (comp (a[ofs], key))
344 ofs = (ofs << 1) + 1;
365 if (comp (*(a-ofs), key))
369 ofs = (ofs << 1) + 1;
377 lastofs = hint - ofs;
387 while (lastofs < ofs)
391 if (comp (a[m], key))
415 template <
class Comp>
436 if (comp (key, *(a-ofs)))
439 ofs = (ofs << 1) + 1;
450 lastofs = hint - ofs;
461 if (comp (key, a[ofs]))
465 ofs = (ofs << 1) + 1;
482 while (lastofs < ofs)
486 if (comp (key, a[m]))
498 unsigned int nbits = 3;
529 return ((n >> nbits) + 1) << nbits;
557 if (ia && need <= alloced)
579 template <
class Comp>
592 std::copy (pa, pa + na,
ms->
a);
625 if (bcount >= min_gallop)
636 if (acount >= min_gallop)
649 min_gallop -= min_gallop > 1;
657 dest = std::copy (pa, pa + k, dest);
680 dest = std::copy (pb, pb + k, dest);
702 std::copy (pa, pa + na, dest);
707 std::copy (pb, pb + nb, dest);
714 template <
class Comp>
728 std::copy (pa, pa + na,
ms->
a);
729 std::copy (ipa, ipa + na,
ms->
ia);
730 dest = pa; idest = ipa;
733 *dest++ = *pb++; *idest++ = *ipb++;
753 *dest++ = *pb++; *idest++ = *ipb++;
759 if (bcount >= min_gallop)
764 *dest++ = *pa++; *idest++ = *ipa++;
770 if (acount >= min_gallop)
783 min_gallop -= min_gallop > 1;
791 dest = std::copy (pa, pa + k, dest);
792 idest = std::copy (ipa, ipa + k, idest);
804 *dest++ = *pb++; *idest++ = *ipb++;
815 dest = std::copy (pb, pb + k, dest);
816 idest = std::copy (ipb, ipb + k, idest);
822 *dest++ = *pa++; *idest++ = *ipa++;
839 std::copy (pa, pa + na, dest);
840 std::copy (ipa, ipa + na, idest);
846 std::copy (pb, pb + nb, dest);
847 std::copy (ipb, ipb + nb, idest);
861 template <
class Comp>
876 std::copy (pb, pb + nb,
ms->
a);
907 if (acount >= min_gallop)
918 if (bcount >= min_gallop)
931 min_gallop -= min_gallop > 1;
940 dest = std::copy_backward (pa+1 - k, pa+1, dest+1) - 1;
960 std::copy (pb+1, pb+1 + k, dest+1);
985 std::copy (baseb, baseb + nb, dest-(nb-1));
990 dest = std::copy_backward (pa+1 - na, pa+1, dest+1) - 1;
998 template <
class Comp>
1015 idest = ipb + nb - 1;
1016 std::copy (pb, pb + nb,
ms->
a);
1017 std::copy (ipb, ipb + nb,
ms->
ia);
1019 baseb =
ms->
a; ibaseb =
ms->
ia;
1020 pb =
ms->
a + nb - 1; ipb =
ms->
ia + nb - 1;
1021 pa += na - 1; ipa += na - 1;
1023 *dest-- = *pa--; *idest-- = *ipa--;
1040 if (comp (*pb, *pa))
1042 *dest-- = *pa--; *idest-- = *ipa--;
1048 if (acount >= min_gallop)
1053 *dest-- = *pb--; *idest-- = *ipb--;
1059 if (bcount >= min_gallop)
1072 min_gallop -= min_gallop > 1;
1081 dest = std::copy_backward (pa+1 - k, pa+1, dest+1) - 1;
1082 idest = std::copy_backward (ipa+1 - k, ipa+1, idest+1) - 1;
1088 *dest-- = *pb--; *idest-- = *ipb--;
1100 dest -= k; idest -= k;
1102 std::copy (pb+1, pb+1 + k, dest+1);
1103 std::copy (ipb+1, ipb+1 + k, idest+1);
1114 *dest-- = *pa--; *idest-- = *ipa--;
1129 std::copy (baseb, baseb + nb, dest-(nb-1));
1130 std::copy (ibaseb, ibaseb + nb, idest-(nb-1));
1136 dest = std::copy_backward (pa+1 - na, pa+1, dest+1) - 1;
1137 idest = std::copy_backward (ipa+1 - na, ipa+1, idest+1) - 1;
1138 pa -= na; ipa -= na;
1139 *dest = *pb; *idest = *ipb;
1148 template <
class Comp>
1193 return merge_lo (pa, na, pb, nb, comp);
1195 return merge_hi (pa, na, pb, nb, comp);
1199 template <
class Comp>
1247 return merge_lo (pa, ipa, na, pb, ipb, nb, comp);
1249 return merge_hi (pa, ipa, na, pb, ipb, nb, comp);
1263 template <
class Comp>
1272 if (n > 0 && p[n-1].len <= p[n].len + p[n+1].len)
1274 if (p[n-1].len < p[n+1].len)
1279 else if (p[n].len <= p[n+1].len)
1292 template <
class Comp>
1301 if (n > 0 && p[n-1].len <= p[n].len + p[n+1].len)
1303 if (p[n-1].len < p[n+1].len)
1305 if (
merge_at (n, data, idx, comp) < 0)
1308 else if (p[n].len <= p[n+1].len)
1310 if (
merge_at (n, data, idx, comp) < 0)
1326 template <
class Comp>
1335 if (n > 0 && p[n-1].len < p[n+1].len)
1345 template <
class Comp>
1354 if (n > 0 && p[n-1].len < p[n+1].len)
1356 if (
merge_at (n, data, idx, comp) < 0)
1389 template <
class Comp>
1414 n =
count_run (data + lo, nremaining, descending, comp);
1418 std::reverse (data + lo, data + lo + n);
1448 template <
class Comp>
1474 n =
count_run (data + lo, nremaining, descending, comp);
1479 std::reverse (data + lo, data + lo + n);
1480 std::reverse (idx + lo, idx + lo + n);
1487 binarysort (data + lo, idx + lo, force, n, comp);
1514 #ifdef INLINE_ASCENDING_SORT
1516 sort (data, nel, std::less<T> ());
1519 #ifdef INLINE_DESCENDING_SORT
1521 sort (data, nel, std::greater<T> ());
1532 #ifdef INLINE_ASCENDING_SORT
1534 sort (data, idx, nel, std::less<T> ());
1537 #ifdef INLINE_DESCENDING_SORT
1539 sort (data, idx, nel, std::greater<T> ());
1546 template <
class T>
template <
class Comp>
1550 const T *end = data + nel;
1553 const T *
next = data;
1554 while (++next != end)
1556 if (comp (*next, *data))
1570 bool retval =
false;
1571 #ifdef INLINE_ASCENDING_SORT
1573 retval =
is_sorted (data, nel, std::less<T> ());
1576 #ifdef INLINE_DESCENDING_SORT
1578 retval =
is_sorted (data, nel, std::greater<T> ());
1591 : col (c), ofs (o), nel (n) { }
1596 template <
class T>
template <
class Comp>
1606 if (cols == 0 || rows <= 1)
1612 std::stack<run_t> runs;
1614 runs.push (run_t (0, 0, rows));
1616 while (! runs.empty ())
1624 T *lbuf = buf + ofs;
1625 const T *ldata = data + rows*col;
1630 lbuf[i] = ldata[lidx[i]];
1633 sort (lbuf, lidx, nel, comp);
1641 if (comp (lbuf[lst], lbuf[i]))
1644 runs.push (run_t (col+1, ofs + lst, i - lst));
1649 runs.push (run_t (col+1, ofs + lst, nel - lst));
1659 #ifdef INLINE_ASCENDING_SORT
1661 sort_rows (data, idx, rows, cols, std::less<T> ());
1664 #ifdef INLINE_DESCENDING_SORT
1666 sort_rows (data, idx, rows, cols, std::greater<T> ());
1673 template <
class T>
template <
class Comp>
1678 if (rows <= 1 || cols == 0)
1682 const T *lastrow = data + rows*(cols - 1);
1683 typedef std::pair<const T *, octave_idx_type> run_t;
1684 std::stack<run_t> runs;
1687 runs.push (run_t (data, rows));
1688 while (sorted && ! runs.empty ())
1690 const T *lo = runs.top ().first;
1697 const T *hi = lo + n, *lst = lo;
1698 for (lo++; lo < hi; lo++)
1700 if (comp (*lst, *lo))
1703 runs.push (run_t (lst + rows, lo - lst));
1706 else if (comp (*lo, *lst))
1713 runs.push (run_t (lst + rows, lo - lst));
1734 bool retval =
false;
1736 #ifdef INLINE_ASCENDING_SORT
1741 #ifdef INLINE_DESCENDING_SORT
1754 template <
class T>
template <
class Comp>
1757 const T& value, Comp comp)
1764 if (comp (value, data[mid]))
1780 #ifdef INLINE_ASCENDING_SORT
1782 retval =
lookup (data, nel, value, std::less<T> ());
1785 #ifdef INLINE_DESCENDING_SORT
1787 retval =
lookup (data, nel, value, std::greater<T> ());
1796 template <
class T>
template <
class Comp>
1806 idx[j] =
lookup (data, nel, values[j], comp);
1815 #ifdef INLINE_ASCENDING_SORT
1817 lookup (data, nel, values, nvalues, idx, std::less<T> ());
1820 #ifdef INLINE_DESCENDING_SORT
1822 lookup (data, nel, values, nvalues, idx, std::greater<T> ());
1826 lookup (data, nel, values, nvalues, idx, std::ptr_fun (
compare));
1829 template <
class T>
template <
class Comp>
1839 if (nvalues > 0 && nel > 0)
1843 if (comp (values[j], data[i]))
1849 else if (++i == nel)
1861 if (nvalues > 0 && nel > 0)
1865 if (comp (values[j], data[i]))
1871 else if (++i == nel)
1876 for (; j != nvalues; j++)
1887 #ifdef INLINE_ASCENDING_SORT
1889 lookup_sorted (data, nel, values, nvalues, idx, rev, std::less<T> ());
1892 #ifdef INLINE_DESCENDING_SORT
1894 lookup_sorted (data, nel, values, nvalues, idx, rev, std::greater<T> ());
1902 template <
class T>
template <
class Comp>
1913 std::partial_sort (data, data + up, data + nel, comp);
1920 std::swap (data[lo+1],
1921 *std::min_element (data + lo + 1, data + nel, comp));
1924 std::partial_sort (data + lo + 1, data + up, data + nel, comp);
1935 #ifdef INLINE_ASCENDING_SORT
1940 #ifdef INLINE_DESCENDING_SORT
1942 nth_element (data, nel, lo, up, std::greater<T> ());