Adapted from Kernigan&Ritchie's C book
| 1654 | |
| 1655 | // Adapted from Kernigan&Ritchie's C book |
| 1656 | static void quickSort(PyObject *list, int left, int right, JSContext *cx, JS::HandleFunction callBack) { |
| 1657 | |
| 1658 | if (left >= right) { |
| 1659 | // base case |
| 1660 | return; |
| 1661 | } |
| 1662 | |
| 1663 | swapItems(list, left, (left + right) / 2); |
| 1664 | |
| 1665 | JS::RootedValue leftValue(cx, jsTypeFactory(cx, PyList_GetItem(list, left))); |
| 1666 | |
| 1667 | int last = left; |
| 1668 | for (int index = left + 1; index <= right; index++) { |
| 1669 | int result = invokeCallBack(list, index, leftValue, cx, callBack); |
| 1670 | if (PyErr_Occurred()) { |
| 1671 | return; |
| 1672 | } |
| 1673 | if (result < 0) { |
| 1674 | swapItems(list, ++last, index); |
| 1675 | } |
| 1676 | } |
| 1677 | |
| 1678 | swapItems(list, left, last); |
| 1679 | |
| 1680 | quickSort(list, left, last - 1, cx, callBack); |
| 1681 | |
| 1682 | quickSort(list, last + 1, right, cx, callBack); |
| 1683 | } |
| 1684 | |
| 1685 | // private |
| 1686 | static bool js_sort_compare_default(JSContext *cx, unsigned argc, JS::Value *vp) { |
no test coverage detected