PageRenderTime 75ms CodeModel.GetById 31ms RepoModel.GetById 1ms app.codeStats 0ms

/necco/python/Objects/listobject.c

https://github.com/brosner/cleese
C | 2466 lines | 1912 code | 219 blank | 335 comment | 521 complexity | ddd8f04d813916d5d3eb947e433794be MD5 | raw file

Large files files are truncated, but you can click here to view the full file

  1. /* List object implementation */
  2. #include "Python.h"
  3. static int
  4. roundupsize(int n)
  5. {
  6. unsigned int nbits = 0;
  7. unsigned int n2 = (unsigned int)n >> 5;
  8. /* Round up:
  9. * If n < 256, to a multiple of 8.
  10. * If n < 2048, to a multiple of 64.
  11. * If n < 16384, to a multiple of 512.
  12. * If n < 131072, to a multiple of 4096.
  13. * If n < 1048576, to a multiple of 32768.
  14. * If n < 8388608, to a multiple of 262144.
  15. * If n < 67108864, to a multiple of 2097152.
  16. * If n < 536870912, to a multiple of 16777216.
  17. * ...
  18. * If n < 2**(5+3*i), to a multiple of 2**(3*i).
  19. *
  20. * This over-allocates proportional to the list size, making room
  21. * for additional growth. The over-allocation is mild, but is
  22. * enough to give linear-time amortized behavior over a long
  23. * sequence of appends() in the presence of a poorly-performing
  24. * system realloc() (which is a reality, e.g., across all flavors
  25. * of Windows, with Win9x behavior being particularly bad -- and
  26. * we've still got address space fragmentation problems on Win9x
  27. * even with this scheme, although it requires much longer lists to
  28. * provoke them than it used to).
  29. */
  30. do {
  31. n2 >>= 3;
  32. nbits += 3;
  33. } while (n2);
  34. return ((n >> nbits) + 1) << nbits;
  35. }
  36. #define NRESIZE(var, type, nitems) \
  37. do { \
  38. size_t _new_size = roundupsize(nitems); \
  39. if (_new_size <= ((~(size_t)0) / sizeof(type))) \
  40. PyMem_RESIZE(var, type, _new_size); \
  41. else \
  42. var = NULL; \
  43. } while (0)
  44. PyObject *
  45. PyList_New(int size)
  46. {
  47. PyListObject *op;
  48. size_t nbytes;
  49. if (size < 0) {
  50. PyErr_BadInternalCall();
  51. return NULL;
  52. }
  53. nbytes = size * sizeof(PyObject *);
  54. /* Check for overflow */
  55. if (nbytes / sizeof(PyObject *) != (size_t)size) {
  56. return PyErr_NoMemory();
  57. }
  58. op = PyObject_GC_New(PyListObject, &PyList_Type);
  59. if (op == NULL) {
  60. return NULL;
  61. }
  62. if (size <= 0) {
  63. op->ob_item = NULL;
  64. }
  65. else {
  66. op->ob_item = (PyObject **) PyMem_MALLOC(nbytes);
  67. if (op->ob_item == NULL) {
  68. return PyErr_NoMemory();
  69. }
  70. memset(op->ob_item, 0, sizeof(*op->ob_item) * size);
  71. }
  72. op->ob_size = size;
  73. _PyObject_GC_TRACK(op);
  74. return (PyObject *) op;
  75. }
  76. int
  77. PyList_Size(PyObject *op)
  78. {
  79. if (!PyList_Check(op)) {
  80. PyErr_BadInternalCall();
  81. return -1;
  82. }
  83. else
  84. return ((PyListObject *)op) -> ob_size;
  85. }
  86. static PyObject *indexerr;
  87. PyObject *
  88. PyList_GetItem(PyObject *op, int i)
  89. {
  90. if (!PyList_Check(op)) {
  91. PyErr_BadInternalCall();
  92. return NULL;
  93. }
  94. if (i < 0 || i >= ((PyListObject *)op) -> ob_size) {
  95. if (indexerr == NULL)
  96. indexerr = PyString_FromString(
  97. "list index out of range");
  98. PyErr_SetObject(PyExc_IndexError, indexerr);
  99. return NULL;
  100. }
  101. return ((PyListObject *)op) -> ob_item[i];
  102. }
  103. int
  104. PyList_SetItem(register PyObject *op, register int i,
  105. register PyObject *newitem)
  106. {
  107. register PyObject *olditem;
  108. register PyObject **p;
  109. if (!PyList_Check(op)) {
  110. Py_XDECREF(newitem);
  111. PyErr_BadInternalCall();
  112. return -1;
  113. }
  114. if (i < 0 || i >= ((PyListObject *)op) -> ob_size) {
  115. Py_XDECREF(newitem);
  116. PyErr_SetString(PyExc_IndexError,
  117. "list assignment index out of range");
  118. return -1;
  119. }
  120. p = ((PyListObject *)op) -> ob_item + i;
  121. olditem = *p;
  122. *p = newitem;
  123. Py_XDECREF(olditem);
  124. return 0;
  125. }
  126. static int
  127. ins1(PyListObject *self, int where, PyObject *v)
  128. {
  129. int i;
  130. PyObject **items;
  131. if (v == NULL) {
  132. PyErr_BadInternalCall();
  133. return -1;
  134. }
  135. if (self->ob_size == INT_MAX) {
  136. PyErr_SetString(PyExc_OverflowError,
  137. "cannot add more objects to list");
  138. return -1;
  139. }
  140. items = self->ob_item;
  141. NRESIZE(items, PyObject *, self->ob_size+1);
  142. if (items == NULL) {
  143. PyErr_NoMemory();
  144. return -1;
  145. }
  146. if (where < 0) {
  147. where += self->ob_size;
  148. if (where < 0)
  149. where = 0;
  150. }
  151. if (where > self->ob_size)
  152. where = self->ob_size;
  153. for (i = self->ob_size; --i >= where; )
  154. items[i+1] = items[i];
  155. Py_INCREF(v);
  156. items[where] = v;
  157. self->ob_item = items;
  158. self->ob_size++;
  159. return 0;
  160. }
  161. int
  162. PyList_Insert(PyObject *op, int where, PyObject *newitem)
  163. {
  164. if (!PyList_Check(op)) {
  165. PyErr_BadInternalCall();
  166. return -1;
  167. }
  168. return ins1((PyListObject *)op, where, newitem);
  169. }
  170. int
  171. PyList_Append(PyObject *op, PyObject *newitem)
  172. {
  173. if (!PyList_Check(op)) {
  174. PyErr_BadInternalCall();
  175. return -1;
  176. }
  177. return ins1((PyListObject *)op,
  178. (int) ((PyListObject *)op)->ob_size, newitem);
  179. }
  180. /* Methods */
  181. static int
  182. list_print(PyListObject *op)
  183. {
  184. int i;
  185. i = Py_ReprEnter((PyObject*)op);
  186. if (i != 0) {
  187. if (i < 0)
  188. return i;
  189. printf("[...]");
  190. return 0;
  191. }
  192. printf("[");
  193. for (i = 0; i < op->ob_size; i++) {
  194. if (i > 0)
  195. printf(", ");
  196. if (PyObject_Print(op->ob_item[i]) != 0) {
  197. Py_ReprLeave((PyObject *)op);
  198. return -1;
  199. }
  200. }
  201. printf("]");
  202. Py_ReprLeave((PyObject *)op);
  203. return 0;
  204. }
  205. static PyObject *
  206. list_repr(PyListObject *v)
  207. {
  208. int i;
  209. PyObject *s, *temp;
  210. PyObject *pieces = NULL, *result = NULL;
  211. i = Py_ReprEnter((PyObject*)v);
  212. if (i != 0) {
  213. return i > 0 ? PyString_FromString("[...]") : NULL;
  214. }
  215. if (v->ob_size == 0) {
  216. result = PyString_FromString("[]");
  217. goto Done;
  218. }
  219. pieces = PyList_New(0);
  220. if (pieces == NULL)
  221. goto Done;
  222. /* Do repr() on each element. Note that this may mutate the list,
  223. so must refetch the list size on each iteration. */
  224. for (i = 0; i < v->ob_size; ++i) {
  225. int status;
  226. s = PyObject_Repr(v->ob_item[i]);
  227. if (s == NULL)
  228. goto Done;
  229. status = PyList_Append(pieces, s);
  230. Py_DECREF(s); /* append created a new ref */
  231. if (status < 0)
  232. goto Done;
  233. }
  234. /* Add "[]" decorations to the first and last items. */
  235. assert(PyList_GET_SIZE(pieces) > 0);
  236. s = PyString_FromString("[");
  237. if (s == NULL)
  238. goto Done;
  239. temp = PyList_GET_ITEM(pieces, 0);
  240. PyString_ConcatAndDel(&s, temp);
  241. PyList_SET_ITEM(pieces, 0, s);
  242. if (s == NULL)
  243. goto Done;
  244. s = PyString_FromString("]");
  245. if (s == NULL)
  246. goto Done;
  247. temp = PyList_GET_ITEM(pieces, PyList_GET_SIZE(pieces) - 1);
  248. PyString_ConcatAndDel(&temp, s);
  249. PyList_SET_ITEM(pieces, PyList_GET_SIZE(pieces) - 1, temp);
  250. if (temp == NULL)
  251. goto Done;
  252. /* Paste them all together with ", " between. */
  253. s = PyString_FromString(", ");
  254. if (s == NULL)
  255. goto Done;
  256. result = _PyString_Join(s, pieces);
  257. Py_DECREF(s);
  258. Done:
  259. Py_XDECREF(pieces);
  260. Py_ReprLeave((PyObject *)v);
  261. return result;
  262. }
  263. static int
  264. list_length(PyListObject *a)
  265. {
  266. return a->ob_size;
  267. }
  268. static int
  269. list_contains(PyListObject *a, PyObject *el)
  270. {
  271. int i, cmp;
  272. for (i = 0, cmp = 0 ; cmp == 0 && i < a->ob_size; ++i)
  273. cmp = PyObject_RichCompareBool(el, PyList_GET_ITEM(a, i),
  274. Py_EQ);
  275. return cmp;
  276. }
  277. static PyObject *
  278. list_item(PyListObject *a, int i)
  279. {
  280. if (i < 0 || i >= a->ob_size) {
  281. if (indexerr == NULL)
  282. indexerr = PyString_FromString(
  283. "list index out of range");
  284. PyErr_SetObject(PyExc_IndexError, indexerr);
  285. return NULL;
  286. }
  287. Py_INCREF(a->ob_item[i]);
  288. return a->ob_item[i];
  289. }
  290. static PyObject *
  291. list_slice(PyListObject *a, int ilow, int ihigh)
  292. {
  293. PyListObject *np;
  294. int i;
  295. if (ilow < 0)
  296. ilow = 0;
  297. else if (ilow > a->ob_size)
  298. ilow = a->ob_size;
  299. if (ihigh < ilow)
  300. ihigh = ilow;
  301. else if (ihigh > a->ob_size)
  302. ihigh = a->ob_size;
  303. np = (PyListObject *) PyList_New(ihigh - ilow);
  304. if (np == NULL)
  305. return NULL;
  306. for (i = ilow; i < ihigh; i++) {
  307. PyObject *v = a->ob_item[i];
  308. Py_INCREF(v);
  309. np->ob_item[i - ilow] = v;
  310. }
  311. return (PyObject *)np;
  312. }
  313. PyObject *
  314. PyList_GetSlice(PyObject *a, int ilow, int ihigh)
  315. {
  316. if (!PyList_Check(a)) {
  317. PyErr_BadInternalCall();
  318. return NULL;
  319. }
  320. return list_slice((PyListObject *)a, ilow, ihigh);
  321. }
  322. static PyObject *
  323. list_concat(PyListObject *a, PyObject *bb)
  324. {
  325. int size;
  326. int i;
  327. PyListObject *np;
  328. if (!PyList_Check(bb)) {
  329. PyErr_Format(PyExc_TypeError,
  330. "can only concatenate list (not \"%.200s\") to list",
  331. bb->ob_type->tp_name);
  332. return NULL;
  333. }
  334. #define b ((PyListObject *)bb)
  335. size = a->ob_size + b->ob_size;
  336. if (size < 0)
  337. return PyErr_NoMemory();
  338. np = (PyListObject *) PyList_New(size);
  339. if (np == NULL) {
  340. return NULL;
  341. }
  342. for (i = 0; i < a->ob_size; i++) {
  343. PyObject *v = a->ob_item[i];
  344. Py_INCREF(v);
  345. np->ob_item[i] = v;
  346. }
  347. for (i = 0; i < b->ob_size; i++) {
  348. PyObject *v = b->ob_item[i];
  349. Py_INCREF(v);
  350. np->ob_item[i + a->ob_size] = v;
  351. }
  352. return (PyObject *)np;
  353. #undef b
  354. }
  355. static PyObject *
  356. list_repeat(PyListObject *a, int n)
  357. {
  358. int i, j;
  359. int size;
  360. PyListObject *np;
  361. PyObject **p;
  362. PyObject *elem;
  363. if (n < 0)
  364. n = 0;
  365. size = a->ob_size * n;
  366. if (size == 0)
  367. return PyList_New(0);
  368. if (n && size/n != a->ob_size)
  369. return PyErr_NoMemory();
  370. np = (PyListObject *) PyList_New(size);
  371. if (np == NULL)
  372. return NULL;
  373. if (a->ob_size == 1) {
  374. elem = a->ob_item[0];
  375. for (i = 0; i < n; i++) {
  376. np->ob_item[i] = elem;
  377. Py_INCREF(elem);
  378. }
  379. return (PyObject *) np;
  380. }
  381. p = np->ob_item;
  382. for (i = 0; i < n; i++) {
  383. for (j = 0; j < a->ob_size; j++) {
  384. *p = a->ob_item[j];
  385. Py_INCREF(*p);
  386. p++;
  387. }
  388. }
  389. return (PyObject *) np;
  390. }
  391. static int
  392. list_ass_slice(PyListObject *a, int ilow, int ihigh, PyObject *v)
  393. {
  394. /* Because [X]DECREF can recursively invoke list operations on
  395. this list, we must postpone all [X]DECREF activity until
  396. after the list is back in its canonical shape. Therefore
  397. we must allocate an additional array, 'recycle', into which
  398. we temporarily copy the items that are deleted from the
  399. list. :-( */
  400. PyObject **recycle, **p;
  401. PyObject **item;
  402. PyObject *v_as_SF = NULL; /* PySequence_Fast(v) */
  403. int n; /* Size of replacement list */
  404. int d; /* Change in size */
  405. int k; /* Loop index */
  406. #define b ((PyListObject *)v)
  407. if (v == NULL)
  408. n = 0;
  409. else {
  410. char msg[256];
  411. PyOS_snprintf(msg, sizeof(msg),
  412. "must assign sequence"
  413. " (not \"%.200s\") to slice",
  414. v->ob_type->tp_name);
  415. v_as_SF = PySequence_Fast(v, msg);
  416. if(v_as_SF == NULL)
  417. return -1;
  418. n = PySequence_Fast_GET_SIZE(v_as_SF);
  419. if (a == b) {
  420. /* Special case "a[i:j] = a" -- copy b first */
  421. int ret;
  422. v = list_slice(b, 0, n);
  423. if (v == NULL)
  424. return -1;
  425. ret = list_ass_slice(a, ilow, ihigh, v);
  426. Py_DECREF(v);
  427. return ret;
  428. }
  429. }
  430. if (ilow < 0)
  431. ilow = 0;
  432. else if (ilow > a->ob_size)
  433. ilow = a->ob_size;
  434. if (ihigh < ilow)
  435. ihigh = ilow;
  436. else if (ihigh > a->ob_size)
  437. ihigh = a->ob_size;
  438. item = a->ob_item;
  439. d = n - (ihigh-ilow);
  440. if (ihigh > ilow) {
  441. p = recycle = PyMem_NEW(PyObject *, (ihigh-ilow));
  442. if (recycle == NULL) {
  443. PyErr_NoMemory();
  444. return -1;
  445. }
  446. }
  447. else
  448. p = recycle = NULL;
  449. if (d <= 0) { /* Delete -d items; recycle ihigh-ilow items */
  450. for (k = ilow; k < ihigh; k++)
  451. *p++ = item[k];
  452. if (d < 0) {
  453. for (/*k = ihigh*/; k < a->ob_size; k++)
  454. item[k+d] = item[k];
  455. a->ob_size += d;
  456. NRESIZE(item, PyObject *, a->ob_size); /* Can't fail */
  457. a->ob_item = item;
  458. }
  459. }
  460. else { /* Insert d items; recycle ihigh-ilow items */
  461. NRESIZE(item, PyObject *, a->ob_size + d);
  462. if (item == NULL) {
  463. if (recycle != NULL)
  464. PyMem_DEL(recycle);
  465. PyErr_NoMemory();
  466. return -1;
  467. }
  468. for (k = a->ob_size; --k >= ihigh; )
  469. item[k+d] = item[k];
  470. for (/*k = ihigh-1*/; k >= ilow; --k)
  471. *p++ = item[k];
  472. a->ob_item = item;
  473. a->ob_size += d;
  474. }
  475. for (k = 0; k < n; k++, ilow++) {
  476. PyObject *w = PySequence_Fast_GET_ITEM(v_as_SF, k);
  477. Py_XINCREF(w);
  478. item[ilow] = w;
  479. }
  480. if (recycle) {
  481. while (--p >= recycle)
  482. Py_XDECREF(*p);
  483. PyMem_DEL(recycle);
  484. }
  485. if (a->ob_size == 0 && a->ob_item != NULL) {
  486. PyMem_FREE(a->ob_item);
  487. a->ob_item = NULL;
  488. }
  489. Py_XDECREF(v_as_SF);
  490. return 0;
  491. #undef b
  492. }
  493. int
  494. PyList_SetSlice(PyObject *a, int ilow, int ihigh, PyObject *v)
  495. {
  496. if (!PyList_Check(a)) {
  497. PyErr_BadInternalCall();
  498. return -1;
  499. }
  500. return list_ass_slice((PyListObject *)a, ilow, ihigh, v);
  501. }
  502. static PyObject *
  503. list_inplace_repeat(PyListObject *self, int n)
  504. {
  505. PyObject **items;
  506. int size, i, j;
  507. size = PyList_GET_SIZE(self);
  508. if (size == 0) {
  509. Py_INCREF(self);
  510. return (PyObject *)self;
  511. }
  512. items = self->ob_item;
  513. if (n < 1) {
  514. self->ob_item = NULL;
  515. self->ob_size = 0;
  516. for (i = 0; i < size; i++)
  517. Py_XDECREF(items[i]);
  518. PyMem_DEL(items);
  519. Py_INCREF(self);
  520. return (PyObject *)self;
  521. }
  522. NRESIZE(items, PyObject*, size*n);
  523. if (items == NULL) {
  524. PyErr_NoMemory();
  525. goto finally;
  526. }
  527. self->ob_item = items;
  528. for (i = 1; i < n; i++) { /* Start counting at 1, not 0 */
  529. for (j = 0; j < size; j++) {
  530. PyObject *o = PyList_GET_ITEM(self, j);
  531. Py_INCREF(o);
  532. PyList_SET_ITEM(self, self->ob_size++, o);
  533. }
  534. }
  535. Py_INCREF(self);
  536. return (PyObject *)self;
  537. finally:
  538. return NULL;
  539. }
  540. static int
  541. list_ass_item(PyListObject *a, int i, PyObject *v)
  542. {
  543. PyObject *old_value;
  544. if (i < 0 || i >= a->ob_size) {
  545. PyErr_SetString(PyExc_IndexError,
  546. "list assignment index out of range");
  547. return -1;
  548. }
  549. if (v == NULL)
  550. return list_ass_slice(a, i, i+1, v);
  551. Py_INCREF(v);
  552. old_value = a->ob_item[i];
  553. a->ob_item[i] = v;
  554. Py_DECREF(old_value);
  555. return 0;
  556. }
  557. static PyObject *
  558. ins(PyListObject *self, int where, PyObject *v)
  559. {
  560. if (ins1(self, where, v) != 0)
  561. return NULL;
  562. Py_INCREF(Py_None);
  563. return Py_None;
  564. }
  565. static PyObject *
  566. listinsert(PyListObject *self, PyObject *args)
  567. {
  568. int i;
  569. PyObject *v;
  570. if (!PyArg_ParseTuple(args, "iO:insert", &i, &v))
  571. return NULL;
  572. return ins(self, i, v);
  573. }
  574. static PyObject *
  575. listappend(PyListObject *self, PyObject *v)
  576. {
  577. return ins(self, (int) self->ob_size, v);
  578. }
  579. static int
  580. listextend_internal(PyListObject *self, PyObject *b)
  581. {
  582. PyObject **items;
  583. int selflen = PyList_GET_SIZE(self);
  584. int blen;
  585. register int i;
  586. if (PyObject_Size(b) == 0) {
  587. /* short circuit when b is empty */
  588. Py_DECREF(b);
  589. return 0;
  590. }
  591. if (self == (PyListObject*)b) {
  592. /* as in list_ass_slice() we must special case the
  593. * situation: a.extend(a)
  594. *
  595. * XXX: I think this way ought to be faster than using
  596. * list_slice() the way list_ass_slice() does.
  597. */
  598. Py_DECREF(b);
  599. b = PyList_New(selflen);
  600. if (!b)
  601. return -1;
  602. for (i = 0; i < selflen; i++) {
  603. PyObject *o = PyList_GET_ITEM(self, i);
  604. Py_INCREF(o);
  605. PyList_SET_ITEM(b, i, o);
  606. }
  607. }
  608. blen = PyObject_Size(b);
  609. /* resize a using idiom */
  610. items = self->ob_item;
  611. NRESIZE(items, PyObject*, selflen + blen);
  612. if (items == NULL) {
  613. PyErr_NoMemory();
  614. Py_DECREF(b);
  615. return -1;
  616. }
  617. self->ob_item = items;
  618. /* populate the end of self with b's items */
  619. for (i = 0; i < blen; i++) {
  620. PyObject *o = PySequence_Fast_GET_ITEM(b, i);
  621. Py_INCREF(o);
  622. PyList_SET_ITEM(self, self->ob_size++, o);
  623. }
  624. Py_DECREF(b);
  625. return 0;
  626. }
  627. static PyObject *
  628. list_inplace_concat(PyListObject *self, PyObject *other)
  629. {
  630. other = PySequence_Fast(other, "argument to += must be iterable");
  631. if (!other)
  632. return NULL;
  633. if (listextend_internal(self, other) < 0)
  634. return NULL;
  635. Py_INCREF(self);
  636. return (PyObject *)self;
  637. }
  638. static PyObject *
  639. listextend(PyListObject *self, PyObject *b)
  640. {
  641. b = PySequence_Fast(b, "list.extend() argument must be iterable");
  642. if (!b)
  643. return NULL;
  644. if (listextend_internal(self, b) < 0)
  645. return NULL;
  646. Py_INCREF(Py_None);
  647. return Py_None;
  648. }
  649. static PyObject *
  650. listpop(PyListObject *self, PyObject *args)
  651. {
  652. int i = -1;
  653. PyObject *v;
  654. if (!PyArg_ParseTuple(args, "|i:pop", &i))
  655. return NULL;
  656. if (self->ob_size == 0) {
  657. /* Special-case most common failure cause */
  658. PyErr_SetString(PyExc_IndexError, "pop from empty list");
  659. return NULL;
  660. }
  661. if (i < 0)
  662. i += self->ob_size;
  663. if (i < 0 || i >= self->ob_size) {
  664. PyErr_SetString(PyExc_IndexError, "pop index out of range");
  665. return NULL;
  666. }
  667. v = self->ob_item[i];
  668. Py_INCREF(v);
  669. if (list_ass_slice(self, i, i+1, (PyObject *)NULL) != 0) {
  670. Py_DECREF(v);
  671. return NULL;
  672. }
  673. return v;
  674. }
  675. /* Reverse a slice of a list in place, from lo up to (exclusive) hi. */
  676. static void
  677. reverse_slice(PyObject **lo, PyObject **hi)
  678. {
  679. assert(lo && hi);
  680. --hi;
  681. while (lo < hi) {
  682. PyObject *t = *lo;
  683. *lo = *hi;
  684. *hi = t;
  685. ++lo;
  686. --hi;
  687. }
  688. }
  689. /* Lots of code for an adaptive, stable, natural mergesort. There are many
  690. * pieces to this algorithm; read listsort.txt for overviews and details.
  691. */
  692. /* Comparison function. Takes care of calling a user-supplied
  693. * comparison function (any callable Python object), which must not be
  694. * NULL (use the ISLT macro if you don't know, or call PyObject_RichCompareBool
  695. * with Py_LT if you know it's NULL).
  696. * Returns -1 on error, 1 if x < y, 0 if x >= y.
  697. */
  698. static int
  699. islt(PyObject *x, PyObject *y, PyObject *compare)
  700. {
  701. PyObject *res;
  702. PyObject *args;
  703. int i;
  704. assert(compare != NULL);
  705. /* Call the user's comparison function and translate the 3-way
  706. * result into true or false (or error).
  707. */
  708. args = PyTuple_New(2);
  709. if (args == NULL)
  710. return -1;
  711. Py_INCREF(x);
  712. Py_INCREF(y);
  713. PyTuple_SET_ITEM(args, 0, x);
  714. PyTuple_SET_ITEM(args, 1, y);
  715. res = PyObject_Call(compare, args, NULL);
  716. Py_DECREF(args);
  717. if (res == NULL)
  718. return -1;
  719. if (!PyInt_Check(res)) {
  720. Py_DECREF(res);
  721. PyErr_SetString(PyExc_TypeError,
  722. "comparison function must return int");
  723. return -1;
  724. }
  725. i = PyInt_AsLong(res);
  726. Py_DECREF(res);
  727. return i < 0;
  728. }
  729. /* If COMPARE is NULL, calls PyObject_RichCompareBool with Py_LT, else calls
  730. * islt. This avoids a layer of function call in the usual case, and
  731. * sorting does many comparisons.
  732. * Returns -1 on error, 1 if x < y, 0 if x >= y.
  733. */
  734. #define ISLT(X, Y, COMPARE) ((COMPARE) == NULL ? \
  735. PyObject_RichCompareBool(X, Y, Py_LT) : \
  736. islt(X, Y, COMPARE))
  737. /* Compare X to Y via "<". Goto "fail" if the comparison raises an
  738. error. Else "k" is set to true iff X<Y, and an "if (k)" block is
  739. started. It makes more sense in context <wink>. X and Y are PyObject*s.
  740. */
  741. #define IFLT(X, Y) if ((k = ISLT(X, Y, compare)) < 0) goto fail; \
  742. if (k)
  743. /* binarysort is the best method for sorting small arrays: it does
  744. few compares, but can do data movement quadratic in the number of
  745. elements.
  746. [lo, hi) is a contiguous slice of a list, and is sorted via
  747. binary insertion. This sort is stable.
  748. On entry, must have lo <= start <= hi, and that [lo, start) is already
  749. sorted (pass start == lo if you don't know!).
  750. If islt() complains return -1, else 0.
  751. Even in case of error, the output slice will be some permutation of
  752. the input (nothing is lost or duplicated).
  753. */
  754. static int
  755. binarysort(PyObject **lo, PyObject **hi, PyObject **start, PyObject *compare)
  756. /* compare -- comparison function object, or NULL for default */
  757. {
  758. register int k;
  759. register PyObject **l, **p, **r;
  760. register PyObject *pivot;
  761. assert(lo <= start && start <= hi);
  762. /* assert [lo, start) is sorted */
  763. if (lo == start)
  764. ++start;
  765. for (; start < hi; ++start) {
  766. /* set l to where *start belongs */
  767. l = lo;
  768. r = start;
  769. pivot = *r;
  770. /* Invariants:
  771. * pivot >= all in [lo, l).
  772. * pivot < all in [r, start).
  773. * The second is vacuously true at the start.
  774. */
  775. assert(l < r);
  776. do {
  777. p = l + ((r - l) >> 1);
  778. IFLT(pivot, *p)
  779. r = p;
  780. else
  781. l = p+1;
  782. } while (l < r);
  783. assert(l == r);
  784. /* The invariants still hold, so pivot >= all in [lo, l) and
  785. pivot < all in [l, start), so pivot belongs at l. Note
  786. that if there are elements equal to pivot, l points to the
  787. first slot after them -- that's why this sort is stable.
  788. Slide over to make room.
  789. Caution: using memmove is much slower under MSVC 5;
  790. we're not usually moving many slots. */
  791. for (p = start; p > l; --p)
  792. *p = *(p-1);
  793. *l = pivot;
  794. }
  795. return 0;
  796. fail:
  797. return -1;
  798. }
  799. /*
  800. Return the length of the run beginning at lo, in the slice [lo, hi). lo < hi
  801. is required on entry. "A run" is the longest ascending sequence, with
  802. lo[0] <= lo[1] <= lo[2] <= ...
  803. or the longest descending sequence, with
  804. lo[0] > lo[1] > lo[2] > ...
  805. Boolean *descending is set to 0 in the former case, or to 1 in the latter.
  806. For its intended use in a stable mergesort, the strictness of the defn of
  807. "descending" is needed so that the caller can safely reverse a descending
  808. sequence without violating stability (strict > ensures there are no equal
  809. elements to get out of order).
  810. Returns -1 in case of error.
  811. */
  812. static int
  813. count_run(PyObject **lo, PyObject **hi, PyObject *compare, int *descending)
  814. {
  815. int k;
  816. int n;
  817. assert(lo < hi);
  818. *descending = 0;
  819. ++lo;
  820. if (lo == hi)
  821. return 1;
  822. n = 2;
  823. IFLT(*lo, *(lo-1)) {
  824. *descending = 1;
  825. for (lo = lo+1; lo < hi; ++lo, ++n) {
  826. IFLT(*lo, *(lo-1))
  827. ;
  828. else
  829. break;
  830. }
  831. }
  832. else {
  833. for (lo = lo+1; lo < hi; ++lo, ++n) {
  834. IFLT(*lo, *(lo-1))
  835. break;
  836. }
  837. }
  838. return n;
  839. fail:
  840. return -1;
  841. }
  842. /*
  843. Locate the proper position of key in a sorted vector; if the vector contains
  844. an element equal to key, return the position immediately to the left of
  845. the leftmost equal element. [gallop_right() does the same except returns
  846. the position to the right of the rightmost equal element (if any).]
  847. "a" is a sorted vector with n elements, starting at a[0]. n must be > 0.
  848. "hint" is an index at which to begin the search, 0 <= hint < n. The closer
  849. hint is to the final result, the faster this runs.
  850. The return value is the int k in 0..n such that
  851. a[k-1] < key <= a[k]
  852. pretending that *(a-1) is minus infinity and a[n] is plus infinity. IOW,
  853. key belongs at index k; or, IOW, the first k elements of a should precede
  854. key, and the last n-k should follow key.
  855. Returns -1 on error. See listsort.txt for info on the method.
  856. */
  857. static int
  858. gallop_left(PyObject *key, PyObject **a, int n, int hint, PyObject *compare)
  859. {
  860. int ofs;
  861. int lastofs;
  862. int k;
  863. assert(key && a && n > 0 && hint >= 0 && hint < n);
  864. a += hint;
  865. lastofs = 0;
  866. ofs = 1;
  867. IFLT(*a, key) {
  868. /* a[hint] < key -- gallop right, until
  869. * a[hint + lastofs] < key <= a[hint + ofs]
  870. */
  871. const int maxofs = n - hint; /* &a[n-1] is highest */
  872. while (ofs < maxofs) {
  873. IFLT(a[ofs], key) {
  874. lastofs = ofs;
  875. ofs = (ofs << 1) + 1;
  876. if (ofs <= 0) /* int overflow */
  877. ofs = maxofs;
  878. }
  879. else /* key <= a[hint + ofs] */
  880. break;
  881. }
  882. if (ofs > maxofs)
  883. ofs = maxofs;
  884. /* Translate back to offsets relative to &a[0]. */
  885. lastofs += hint;
  886. ofs += hint;
  887. }
  888. else {
  889. /* key <= a[hint] -- gallop left, until
  890. * a[hint - ofs] < key <= a[hint - lastofs]
  891. */
  892. const int maxofs = hint + 1; /* &a[0] is lowest */
  893. while (ofs < maxofs) {
  894. IFLT(*(a-ofs), key)
  895. break;
  896. /* key <= a[hint - ofs] */
  897. lastofs = ofs;
  898. ofs = (ofs << 1) + 1;
  899. if (ofs <= 0) /* int overflow */
  900. ofs = maxofs;
  901. }
  902. if (ofs > maxofs)
  903. ofs = maxofs;
  904. /* Translate back to positive offsets relative to &a[0]. */
  905. k = lastofs;
  906. lastofs = hint - ofs;
  907. ofs = hint - k;
  908. }
  909. a -= hint;
  910. assert(-1 <= lastofs && lastofs < ofs && ofs <= n);
  911. /* Now a[lastofs] < key <= a[ofs], so key belongs somewhere to the
  912. * right of lastofs but no farther right than ofs. Do a binary
  913. * search, with invariant a[lastofs-1] < key <= a[ofs].
  914. */
  915. ++lastofs;
  916. while (lastofs < ofs) {
  917. int m = lastofs + ((ofs - lastofs) >> 1);
  918. IFLT(a[m], key)
  919. lastofs = m+1; /* a[m] < key */
  920. else
  921. ofs = m; /* key <= a[m] */
  922. }
  923. assert(lastofs == ofs); /* so a[ofs-1] < key <= a[ofs] */
  924. return ofs;
  925. fail:
  926. return -1;
  927. }
  928. /*
  929. Exactly like gallop_left(), except that if key already exists in a[0:n],
  930. finds the position immediately to the right of the rightmost equal value.
  931. The return value is the int k in 0..n such that
  932. a[k-1] <= key < a[k]
  933. or -1 if error.
  934. The code duplication is massive, but this is enough different given that
  935. we're sticking to "<" comparisons that it's much harder to follow if
  936. written as one routine with yet another "left or right?" flag.
  937. */
  938. static int
  939. gallop_right(PyObject *key, PyObject **a, int n, int hint, PyObject *compare)
  940. {
  941. int ofs;
  942. int lastofs;
  943. int k;
  944. assert(key && a && n > 0 && hint >= 0 && hint < n);
  945. a += hint;
  946. lastofs = 0;
  947. ofs = 1;
  948. IFLT(key, *a) {
  949. /* key < a[hint] -- gallop left, until
  950. * a[hint - ofs] <= key < a[hint - lastofs]
  951. */
  952. const int maxofs = hint + 1; /* &a[0] is lowest */
  953. while (ofs < maxofs) {
  954. IFLT(key, *(a-ofs)) {
  955. lastofs = ofs;
  956. ofs = (ofs << 1) + 1;
  957. if (ofs <= 0) /* int overflow */
  958. ofs = maxofs;
  959. }
  960. else /* a[hint - ofs] <= key */
  961. break;
  962. }
  963. if (ofs > maxofs)
  964. ofs = maxofs;
  965. /* Translate back to positive offsets relative to &a[0]. */
  966. k = lastofs;
  967. lastofs = hint - ofs;
  968. ofs = hint - k;
  969. }
  970. else {
  971. /* a[hint] <= key -- gallop right, until
  972. * a[hint + lastofs] <= key < a[hint + ofs]
  973. */
  974. const int maxofs = n - hint; /* &a[n-1] is highest */
  975. while (ofs < maxofs) {
  976. IFLT(key, a[ofs])
  977. break;
  978. /* a[hint + ofs] <= key */
  979. lastofs = ofs;
  980. ofs = (ofs << 1) + 1;
  981. if (ofs <= 0) /* int overflow */
  982. ofs = maxofs;
  983. }
  984. if (ofs > maxofs)
  985. ofs = maxofs;
  986. /* Translate back to offsets relative to &a[0]. */
  987. lastofs += hint;
  988. ofs += hint;
  989. }
  990. a -= hint;
  991. assert(-1 <= lastofs && lastofs < ofs && ofs <= n);
  992. /* Now a[lastofs] <= key < a[ofs], so key belongs somewhere to the
  993. * right of lastofs but no farther right than ofs. Do a binary
  994. * search, with invariant a[lastofs-1] <= key < a[ofs].
  995. */
  996. ++lastofs;
  997. while (lastofs < ofs) {
  998. int m = lastofs + ((ofs - lastofs) >> 1);
  999. IFLT(key, a[m])
  1000. ofs = m; /* key < a[m] */
  1001. else
  1002. lastofs = m+1; /* a[m] <= key */
  1003. }
  1004. assert(lastofs == ofs); /* so a[ofs-1] <= key < a[ofs] */
  1005. return ofs;
  1006. fail:
  1007. return -1;
  1008. }
  1009. /* The maximum number of entries in a MergeState's pending-runs stack.
  1010. * This is enough to sort arrays of size up to about
  1011. * 32 * phi ** MAX_MERGE_PENDING
  1012. * where phi ~= 1.618. 85 is ridiculouslylarge enough, good for an array
  1013. * with 2**64 elements.
  1014. */
  1015. #define MAX_MERGE_PENDING 85
  1016. /* When we get into galloping mode, we stay there until both runs win less
  1017. * often than MIN_GALLOP consecutive times. See listsort.txt for more info.
  1018. */
  1019. #define MIN_GALLOP 7
  1020. /* Avoid malloc for small temp arrays. */
  1021. #define MERGESTATE_TEMP_SIZE 256
  1022. /* One MergeState exists on the stack per invocation of mergesort. It's just
  1023. * a convenient way to pass state around among the helper functions.
  1024. */
  1025. struct s_slice {
  1026. PyObject **base;
  1027. int len;
  1028. };
  1029. typedef struct s_MergeState {
  1030. /* The user-supplied comparison function. or NULL if none given. */
  1031. PyObject *compare;
  1032. /* This controls when we get *into* galloping mode. It's initialized
  1033. * to MIN_GALLOP. merge_lo and merge_hi tend to nudge it higher for
  1034. * random data, and lower for highly structured data.
  1035. */
  1036. int min_gallop;
  1037. /* 'a' is temp storage to help with merges. It contains room for
  1038. * alloced entries.
  1039. */
  1040. PyObject **a; /* may point to temparray below */
  1041. int alloced;
  1042. /* A stack of n pending runs yet to be merged. Run #i starts at
  1043. * address base[i] and extends for len[i] elements. It's always
  1044. * true (so long as the indices are in bounds) that
  1045. *
  1046. * pending[i].base + pending[i].len == pending[i+1].base
  1047. *
  1048. * so we could cut the storage for this, but it's a minor amount,
  1049. * and keeping all the info explicit simplifies the code.
  1050. */
  1051. int n;
  1052. struct s_slice pending[MAX_MERGE_PENDING];
  1053. /* 'a' points to this when possible, rather than muck with malloc. */
  1054. PyObject *temparray[MERGESTATE_TEMP_SIZE];
  1055. } MergeState;
  1056. /* Conceptually a MergeState's constructor. */
  1057. static void
  1058. merge_init(MergeState *ms, PyObject *compare)
  1059. {
  1060. assert(ms != NULL);
  1061. ms->compare = compare;
  1062. ms->a = ms->temparray;
  1063. ms->alloced = MERGESTATE_TEMP_SIZE;
  1064. ms->n = 0;
  1065. ms->min_gallop = MIN_GALLOP;
  1066. }
  1067. /* Free all the temp memory owned by the MergeState. This must be called
  1068. * when you're done with a MergeState, and may be called before then if
  1069. * you want to free the temp memory early.
  1070. */
  1071. static void
  1072. merge_freemem(MergeState *ms)
  1073. {
  1074. assert(ms != NULL);
  1075. if (ms->a != ms->temparray)
  1076. PyMem_Free(ms->a);
  1077. ms->a = ms->temparray;
  1078. ms->alloced = MERGESTATE_TEMP_SIZE;
  1079. }
  1080. /* Ensure enough temp memory for 'need' array slots is available.
  1081. * Returns 0 on success and -1 if the memory can't be gotten.
  1082. */
  1083. static int
  1084. merge_getmem(MergeState *ms, int need)
  1085. {
  1086. assert(ms != NULL);
  1087. if (need <= ms->alloced)
  1088. return 0;
  1089. /* Don't realloc! That can cost cycles to copy the old data, but
  1090. * we don't care what's in the block.
  1091. */
  1092. merge_freemem(ms);
  1093. ms->a = (PyObject **)PyMem_Malloc(need * sizeof(PyObject*));
  1094. if (ms->a) {
  1095. ms->alloced = need;
  1096. return 0;
  1097. }
  1098. PyErr_NoMemory();
  1099. merge_freemem(ms); /* reset to sane state */
  1100. return -1;
  1101. }
  1102. #define MERGE_GETMEM(MS, NEED) ((NEED) <= (MS)->alloced ? 0 : \
  1103. merge_getmem(MS, NEED))
  1104. /* Merge the na elements starting at pa with the nb elements starting at pb
  1105. * in a stable way, in-place. na and nb must be > 0, and pa + na == pb.
  1106. * Must also have that *pb < *pa, that pa[na-1] belongs at the end of the
  1107. * merge, and should have na <= nb. See listsort.txt for more info.
  1108. * Return 0 if successful, -1 if error.
  1109. */
  1110. static int
  1111. merge_lo(MergeState *ms, PyObject **pa, int na, PyObject **pb, int nb)
  1112. {
  1113. int k;
  1114. PyObject *compare;
  1115. PyObject **dest;
  1116. int result = -1; /* guilty until proved innocent */
  1117. int min_gallop = ms->min_gallop;
  1118. assert(ms && pa && pb && na > 0 && nb > 0 && pa + na == pb);
  1119. if (MERGE_GETMEM(ms, na) < 0)
  1120. return -1;
  1121. memcpy(ms->a, pa, na * sizeof(PyObject*));
  1122. dest = pa;
  1123. pa = ms->a;
  1124. *dest++ = *pb++;
  1125. --nb;
  1126. if (nb == 0)
  1127. goto Succeed;
  1128. if (na == 1)
  1129. goto CopyB;
  1130. compare = ms->compare;
  1131. for (;;) {
  1132. int acount = 0; /* # of times A won in a row */
  1133. int bcount = 0; /* # of times B won in a row */
  1134. /* Do the straightforward thing until (if ever) one run
  1135. * appears to win consistently.
  1136. */
  1137. for (;;) {
  1138. assert(na > 1 && nb > 0);
  1139. k = ISLT(*pb, *pa, compare);
  1140. if (k) {
  1141. if (k < 0)
  1142. goto Fail;
  1143. *dest++ = *pb++;
  1144. ++bcount;
  1145. acount = 0;
  1146. --nb;
  1147. if (nb == 0)
  1148. goto Succeed;
  1149. if (bcount >= min_gallop)
  1150. break;
  1151. }
  1152. else {
  1153. *dest++ = *pa++;
  1154. ++acount;
  1155. bcount = 0;
  1156. --na;
  1157. if (na == 1)
  1158. goto CopyB;
  1159. if (acount >= min_gallop)
  1160. break;
  1161. }
  1162. }
  1163. /* One run is winning so consistently that galloping may
  1164. * be a huge win. So try that, and continue galloping until
  1165. * (if ever) neither run appears to be winning consistently
  1166. * anymore.
  1167. */
  1168. ++min_gallop;
  1169. do {
  1170. assert(na > 1 && nb > 0);
  1171. min_gallop -= min_gallop > 1;
  1172. ms->min_gallop = min_gallop;
  1173. k = gallop_right(*pb, pa, na, 0, compare);
  1174. acount = k;
  1175. if (k) {
  1176. if (k < 0)
  1177. goto Fail;
  1178. memcpy(dest, pa, k * sizeof(PyObject *));
  1179. dest += k;
  1180. pa += k;
  1181. na -= k;
  1182. if (na == 1)
  1183. goto CopyB;
  1184. /* na==0 is impossible now if the comparison
  1185. * function is consistent, but we can't assume
  1186. * that it is.
  1187. */
  1188. if (na == 0)
  1189. goto Succeed;
  1190. }
  1191. *dest++ = *pb++;
  1192. --nb;
  1193. if (nb == 0)
  1194. goto Succeed;
  1195. k = gallop_left(*pa, pb, nb, 0, compare);
  1196. bcount = k;
  1197. if (k) {
  1198. if (k < 0)
  1199. goto Fail;
  1200. memmove(dest, pb, k * sizeof(PyObject *));
  1201. dest += k;
  1202. pb += k;
  1203. nb -= k;
  1204. if (nb == 0)
  1205. goto Succeed;
  1206. }
  1207. *dest++ = *pa++;
  1208. --na;
  1209. if (na == 1)
  1210. goto CopyB;
  1211. } while (acount >= MIN_GALLOP || bcount >= MIN_GALLOP);
  1212. ++min_gallop; /* penalize it for leaving galloping mode */
  1213. ms->min_gallop = min_gallop;
  1214. }
  1215. Succeed:
  1216. result = 0;
  1217. Fail:
  1218. if (na)
  1219. memcpy(dest, pa, na * sizeof(PyObject*));
  1220. return result;
  1221. CopyB:
  1222. assert(na == 1 && nb > 0);
  1223. /* The last element of pa belongs at the end of the merge. */
  1224. memmove(dest, pb, nb * sizeof(PyObject *));
  1225. dest[nb] = *pa;
  1226. return 0;
  1227. }
  1228. /* Merge the na elements starting at pa with the nb elements starting at pb
  1229. * in a stable way, in-place. na and nb must be > 0, and pa + na == pb.
  1230. * Must also have that *pb < *pa, that pa[na-1] belongs at the end of the
  1231. * merge, and should have na >= nb. See listsort.txt for more info.
  1232. * Return 0 if successful, -1 if error.
  1233. */
  1234. static int
  1235. merge_hi(MergeState *ms, PyObject **pa, int na, PyObject **pb, int nb)
  1236. {
  1237. int k;
  1238. PyObject *compare;
  1239. PyObject **dest;
  1240. int result = -1; /* guilty until proved innocent */
  1241. PyObject **basea;
  1242. PyObject **baseb;
  1243. int min_gallop = ms->min_gallop;
  1244. assert(ms && pa && pb && na > 0 && nb > 0 && pa + na == pb);
  1245. if (MERGE_GETMEM(ms, nb) < 0)
  1246. return -1;
  1247. dest = pb + nb - 1;
  1248. memcpy(ms->a, pb, nb * sizeof(PyObject*));
  1249. basea = pa;
  1250. baseb = ms->a;
  1251. pb = ms->a + nb - 1;
  1252. pa += na - 1;
  1253. *dest-- = *pa--;
  1254. --na;
  1255. if (na == 0)
  1256. goto Succeed;
  1257. if (nb == 1)
  1258. goto CopyA;
  1259. compare = ms->compare;
  1260. for (;;) {
  1261. int acount = 0; /* # of times A won in a row */
  1262. int bcount = 0; /* # of times B won in a row */
  1263. /* Do the straightforward thing until (if ever) one run
  1264. * appears to win consistently.
  1265. */
  1266. for (;;) {
  1267. assert(na > 0 && nb > 1);
  1268. k = ISLT(*pb, *pa, compare);
  1269. if (k) {
  1270. if (k < 0)
  1271. goto Fail;
  1272. *dest-- = *pa--;
  1273. ++acount;
  1274. bcount = 0;
  1275. --na;
  1276. if (na == 0)
  1277. goto Succeed;
  1278. if (acount >= min_gallop)
  1279. break;
  1280. }
  1281. else {
  1282. *dest-- = *pb--;
  1283. ++bcount;
  1284. acount = 0;
  1285. --nb;
  1286. if (nb == 1)
  1287. goto CopyA;
  1288. if (bcount >= min_gallop)
  1289. break;
  1290. }
  1291. }
  1292. /* One run is winning so consistently that galloping may
  1293. * be a huge win. So try that, and continue galloping until
  1294. * (if ever) neither run appears to be winning consistently
  1295. * anymore.
  1296. */
  1297. ++min_gallop;
  1298. do {
  1299. assert(na > 0 && nb > 1);
  1300. min_gallop -= min_gallop > 1;
  1301. ms->min_gallop = min_gallop;
  1302. k = gallop_right(*pb, basea, na, na-1, compare);
  1303. if (k < 0)
  1304. goto Fail;
  1305. k = na - k;
  1306. acount = k;
  1307. if (k) {
  1308. dest -= k;
  1309. pa -= k;
  1310. memmove(dest+1, pa+1, k * sizeof(PyObject *));
  1311. na -= k;
  1312. if (na == 0)
  1313. goto Succeed;
  1314. }
  1315. *dest-- = *pb--;
  1316. --nb;
  1317. if (nb == 1)
  1318. goto CopyA;
  1319. k = gallop_left(*pa, baseb, nb, nb-1, compare);
  1320. if (k < 0)
  1321. goto Fail;
  1322. k = nb - k;
  1323. bcount = k;
  1324. if (k) {
  1325. dest -= k;
  1326. pb -= k;
  1327. memcpy(dest+1, pb+1, k * sizeof(PyObject *));
  1328. nb -= k;
  1329. if (nb == 1)
  1330. goto CopyA;
  1331. /* nb==0 is impossible now if the comparison
  1332. * function is consistent, but we can't assume
  1333. * that it is.
  1334. */
  1335. if (nb == 0)
  1336. goto Succeed;
  1337. }
  1338. *dest-- = *pa--;
  1339. --na;
  1340. if (na == 0)
  1341. goto Succeed;
  1342. } while (acount >= MIN_GALLOP || bcount >= MIN_GALLOP);
  1343. ++min_gallop; /* penalize it for leaving galloping mode */
  1344. ms->min_gallop = min_gallop;
  1345. }
  1346. Succeed:
  1347. result = 0;
  1348. Fail:
  1349. if (nb)
  1350. memcpy(dest-(nb-1), baseb, nb * sizeof(PyObject*));
  1351. return result;
  1352. CopyA:
  1353. assert(nb == 1 && na > 0);
  1354. /* The first element of pb belongs at the front of the merge. */
  1355. dest -= na;
  1356. pa -= na;
  1357. memmove(dest+1, pa+1, na * sizeof(PyObject *));
  1358. *dest = *pb;
  1359. return 0;
  1360. }
  1361. /* Merge the two runs at stack indices i and i+1.
  1362. * Returns 0 on success, -1 on error.
  1363. */
  1364. static int
  1365. merge_at(MergeState *ms, int i)
  1366. {
  1367. PyObject **pa, **pb;
  1368. int na, nb;
  1369. int k;
  1370. PyObject *compare;
  1371. assert(ms != NULL);
  1372. assert(ms->n >= 2);
  1373. assert(i >= 0);
  1374. assert(i == ms->n - 2 || i == ms->n - 3);
  1375. pa = ms->pending[i].base;
  1376. na = ms->pending[i].len;
  1377. pb = ms->pending[i+1].base;
  1378. nb = ms->pending[i+1].len;
  1379. assert(na > 0 && nb > 0);
  1380. assert(pa + na == pb);
  1381. /* Record the length of the combined runs; if i is the 3rd-last
  1382. * run now, also slide over the last run (which isn't involved
  1383. * in this merge). The current run i+1 goes away in any case.
  1384. */
  1385. ms->pending[i].len = na + nb;
  1386. if (i == ms->n - 3)
  1387. ms->pending[i+1] = ms->pending[i+2];
  1388. --ms->n;
  1389. /* Where does b start in a? Elements in a before that can be
  1390. * ignored (already in place).
  1391. */
  1392. compare = ms->compare;
  1393. k = gallop_right(*pb, pa, na, 0, compare);
  1394. if (k < 0)
  1395. return -1;
  1396. pa += k;
  1397. na -= k;
  1398. if (na == 0)
  1399. return 0;
  1400. /* Where does a end in b? Elements in b after that can be
  1401. * ignored (already in place).
  1402. */
  1403. nb = gallop_left(pa[na-1], pb, nb, nb-1, compare);
  1404. if (nb <= 0)
  1405. return nb;
  1406. /* Merge what remains of the runs, using a temp array with
  1407. * min(na, nb) elements.
  1408. */
  1409. if (na <= nb)
  1410. return merge_lo(ms, pa, na, pb, nb);
  1411. else
  1412. return merge_hi(ms, pa, na, pb, nb);
  1413. }
  1414. /* Examine the stack of runs waiting to be merged, merging adjacent runs
  1415. * until the stack invariants are re-established:
  1416. *
  1417. * 1. len[-3] > len[-2] + len[-1]
  1418. * 2. len[-2] > len[-1]
  1419. *
  1420. * See listsort.txt for more info.
  1421. *
  1422. * Returns 0 on success, -1 on error.
  1423. */
  1424. static int
  1425. merge_collapse(MergeState *ms)
  1426. {
  1427. struct s_slice *p = ms->pending;
  1428. assert(ms);
  1429. while (ms->n > 1) {
  1430. int n = ms->n - 2;
  1431. if (n > 0 && p[n-1].len <= p[n].len + p[n+1].len) {
  1432. if (p[n-1].len < p[n+1].len)
  1433. --n;
  1434. if (merge_at(ms, n) < 0)
  1435. return -1;
  1436. }
  1437. else if (p[n].len <= p[n+1].len) {
  1438. if (merge_at(ms, n) < 0)
  1439. return -1;
  1440. }
  1441. else
  1442. break;
  1443. }
  1444. return 0;
  1445. }
  1446. /* Regardless of invariants, merge all runs on the stack until only one
  1447. * remains. This is used at the end of the mergesort.
  1448. *
  1449. * Returns 0 on success, -1 on error.
  1450. */
  1451. static int
  1452. merge_force_collapse(MergeState *ms)
  1453. {
  1454. struct s_slice *p = ms->pending;
  1455. assert(ms);
  1456. while (ms->n > 1) {
  1457. int n = ms->n - 2;
  1458. if (n > 0 && p[n-1].len < p[n+1].len)
  1459. --n;
  1460. if (merge_at(ms, n) < 0)
  1461. return -1;
  1462. }
  1463. return 0;
  1464. }
  1465. /* Compute a good value for the minimum run length; natural runs shorter
  1466. * than this are boosted artificially via binary insertion.
  1467. *
  1468. * If n < 64, return n (it's too small to bother with fancy stuff).
  1469. * Else if n is an exact power of 2, return 32.
  1470. * Else return an int k, 32 <= k <= 64, such that n/k is close to, but
  1471. * strictly less than, an exact power of 2.
  1472. *
  1473. * See listsort.txt for more info.
  1474. */
  1475. static int
  1476. merge_compute_minrun(int n)
  1477. {
  1478. int r = 0; /* becomes 1 if any 1 bits are shifted off */
  1479. assert(n >= 0);
  1480. while (n >= 64) {
  1481. r |= n & 1;
  1482. n >>= 1;
  1483. }
  1484. return n + r;
  1485. }
  1486. /* An adaptive, stable, natural mergesort. See listsort.txt.
  1487. * Returns Py_None on success, NULL on error. Even in case of error, the
  1488. * list will be some permutation of its input state (nothing is lost or
  1489. * duplicated).
  1490. */
  1491. static PyObject *
  1492. listsort(PyListObject *self, PyObject *args)
  1493. {
  1494. MergeState ms;
  1495. PyObject **lo, **hi;
  1496. int nremaining;
  1497. int minrun;
  1498. int saved_ob_size;
  1499. PyObject **saved_ob_item;
  1500. PyObject **empty_ob_item;
  1501. PyObject *compare = NULL;
  1502. PyObject *result = NULL; /* guilty until proved innocent */
  1503. assert(self != NULL);
  1504. if (args != NULL) {
  1505. if (!PyArg_UnpackTuple(args, "sort", 0, 1, &compare))
  1506. return NULL;
  1507. }
  1508. if (compare == Py_None)
  1509. compare = NULL;
  1510. merge_init(&ms, compare);
  1511. /* The list is temporarily made empty, so that mutations performed
  1512. * by comparison functions can't affect the slice of memory we're
  1513. * sorting (allowing mutations during sorting is a core-dump
  1514. * factory, since ob_item may change).
  1515. */
  1516. saved_ob_size = self->ob_size;
  1517. saved_ob_item = self->ob_item;
  1518. self->ob_size = 0;
  1519. self->ob_item = empty_ob_item = PyMem_NEW(PyObject *, 0);
  1520. nremaining = saved_ob_size;
  1521. if (nremaining < 2)
  1522. goto succeed;
  1523. /* March over the array once, left to right, finding natural runs,
  1524. * and extending short natural runs to minrun elements.
  1525. */
  1526. lo = saved_ob_item;
  1527. hi = lo + nremaining;
  1528. minrun = merge_compute_minrun(nremaining);
  1529. do {
  1530. int descending;
  1531. int n;
  1532. /* Identify next run. */
  1533. n = count_run(lo, hi, compare, &descending);
  1534. if (n < 0)
  1535. goto fail;
  1536. if (descending)
  1537. reverse_slice(lo, lo + n);
  1538. /* If short, extend to min(minrun, nremaining). */
  1539. if (n < minrun) {
  1540. const int force = nremaining <= minrun ?
  1541. nremaining : minrun;
  1542. if (binarysort(lo, lo + force, lo + n, compare) < 0)
  1543. goto fail;
  1544. n = force;
  1545. }
  1546. /* Push run onto pending-runs stack, and maybe merge. */
  1547. assert(ms.n < MAX_MERGE_PENDING);
  1548. ms.pending[ms.n].base = lo;
  1549. ms.pending[ms.n].len = n;
  1550. ++ms.n;
  1551. if (merge_collapse(&ms) < 0)
  1552. goto fail;
  1553. /* Advance to find next run. */
  1554. lo += n;
  1555. nremaining -= n;
  1556. } while (nremaining);
  1557. assert(lo == hi);
  1558. if (merge_force_collapse(&ms) < 0)
  1559. goto fail;
  1560. assert(ms.n == 1);
  1561. assert(ms.pending[0].base == saved_ob_item);
  1562. assert(ms.pending[0].len == saved_ob_size);
  1563. succeed:
  1564. result = Py_None;
  1565. fail:
  1566. if (self->ob_item != empty_ob_item || self->ob_size) {
  1567. /* The user mucked with the list during the sort. */
  1568. (void)list_ass_slice(self, 0, self->ob_size, (PyObject *)NULL);
  1569. if (result != NULL) {
  1570. PyErr_SetString(PyExc_ValueError,
  1571. "list modified during sort");
  1572. result = NULL;
  1573. }
  1574. }
  1575. if (self->ob_item == empty_ob_item)
  1576. PyMem_FREE(empty_ob_item);
  1577. self->ob_size = saved_ob_size;
  1578. self->ob_item = saved_ob_item;
  1579. merge_freemem(&ms);
  1580. Py_XINCREF(result);
  1581. return result;
  1582. }
  1583. #undef IFLT
  1584. #undef ISLT
  1585. int
  1586. PyList_Sort(PyObject *v)
  1587. {
  1588. if (v == NULL || !PyList_Check(v)) {
  1589. PyErr_BadInternalCall();
  1590. return -1;
  1591. }
  1592. v = listsort((PyListObject *)v, (PyObject *)NULL);
  1593. if (v == NULL)
  1594. return -1;
  1595. Py_DECREF(v);
  1596. return 0;
  1597. }
  1598. static PyObject *
  1599. listreverse(PyListObject *self)
  1600. {
  1601. if (self->ob_size > 1)
  1602. reverse_slice(self->ob_item, self->ob_item + self->ob_size);
  1603. Py_INCREF(Py_None);
  1604. return Py_None;
  1605. }
  1606. int
  1607. PyList_Reverse(PyObject *v)
  1608. {
  1609. PyListObject *self = (PyListObject *)v;
  1610. if (v == NULL || !PyList_Check(v)) {
  1611. PyErr_BadInternalCall();
  1612. return -1;
  1613. }
  1614. if (self->ob_size > 1)
  1615. reverse_slice(self->ob_item, self->ob_item + self->ob_size);
  1616. return 0;
  1617. }
  1618. PyObject *
  1619. PyList_AsTuple(PyObject *v)
  1620. {
  1621. PyObject *w;
  1622. PyObject **p;
  1623. int n;
  1624. if (v == NULL || !PyList_Check(v)) {
  1625. PyErr_BadInternalCall();
  1626. return NULL;
  1627. }
  1628. n = ((PyListObject *)v)->ob_size;
  1629. w = PyTuple_New(n);
  1630. if (w == NULL)
  1631. return NULL;
  1632. p = ((PyTupleObject *)w)->ob_item;
  1633. memcpy((void *)p,
  1634. (void *)((PyListObject *)v)->ob_item,
  1635. n*sizeof(PyObject *));
  1636. while (--n >= 0) {
  1637. Py_INCREF(*p);
  1638. p++;
  1639. }
  1640. return w;
  1641. }
  1642. static PyObject *
  1643. listindex(PyListObject *self, PyObject *args)
  1644. {
  1645. int i, start=0, stop=self->ob_size;
  1646. PyObject *v;
  1647. if (!PyArg_ParseTuple(args, "O|O&O&:index", &v,
  1648. _PyEval_SliceIndex, &start,
  1649. _PyEval_SliceIndex, &stop))
  1650. return NULL;
  1651. if (start < 0) {
  1652. start += self->ob_size;
  1653. if (start < 0)
  1654. start = 0;
  1655. }
  1656. if (stop < 0) {
  1657. stop += self->ob_size;
  1658. if (stop < 0)
  1659. stop = 0;
  1660. }
  1661. else if (stop > self->ob_size)
  1662. stop = self->ob_size;
  1663. for (i = start; i < stop; i++) {
  1664. int cmp = PyObject_RichCompareBool(self->ob_item[i], v, Py_EQ);
  1665. if (cmp > 0)
  1666. return PyInt_FromLong((long)i);
  1667. else if (cmp < 0)
  1668. return NULL;
  1669. }
  1670. PyErr_SetString(PyExc_ValueError, "list.index(x): x not in list");
  1671. return NULL;
  1672. }
  1673. static PyObject *
  1674. listcount(PyListObject *self, PyObject *v)
  1675. {
  1676. int count = 0;
  1677. int i;
  1678. for (i = 0; i < self->ob_size; i++) {
  1679. int cmp = PyObject_RichCompareBool(self->ob_item[i], v, Py_EQ);
  1680. if (cmp > 0)
  1681. count++;
  1682. else if (cmp < 0)
  1683. return NULL;
  1684. }
  1685. return PyInt_FromLong((long)count);
  1686. }
  1687. static PyObject *
  1688. listremove(PyListObject *self, PyObject *v)
  1689. {
  1690. int i;
  1691. for (i = 0; i < self->ob_size; i++) {
  1692. int cmp = PyObject_RichCompareBool(self->ob_item[i], v, Py_EQ);
  1693. if (cmp > 0) {
  1694. if (list_ass_slice(self, i, i+1,
  1695. (PyObject *)NULL) != 0)
  1696. return NULL;
  1697. Py_INCREF(Py_None);
  1698. return Py_None;
  1699. }
  1700. else if (cmp < 0)
  1701. return NULL;
  1702. }
  1703. PyErr_SetString(PyExc_ValueError, "list.remove(x): x not in list");
  1704. return NULL;
  1705. }
  1706. static int
  1707. list_traverse(PyListObject *o, visitproc visit, void *arg)
  1708. {
  1709. int i, err;
  1710. PyObject *x;
  1711. for (i = o->ob_size; --i >= 0; ) {
  1712. x = o->ob_item[i];
  1713. if (x != NULL) {
  1714. err = visit(x, arg);
  1715. if (err)
  1716. return err;
  1717. }
  1718. }
  1719. return 0;
  1720. }
  1721. static int
  1722. list_clear(PyListObject *lp)
  1723. {
  1724. (void) PyList_SetSlice((PyObject *)lp, 0, lp->ob_size, 0);
  1725. return 0;
  1726. }
  1727. static PyObject *
  1728. list_richcompare(PyObject *v, PyObject *w, int op)
  1729. {
  1730. PyListObject *vl, *wl;
  1731. int i;
  1732. if (!PyList_Check(v) || !PyList_Check(w)) {
  1733. Py_INCREF(Py_NotImplemented);
  1734. return Py_NotImplemented;
  1735. }
  1736. vl = (PyListObject *)v;
  1737. wl = (PyListObject *)w;
  1738. if (vl->ob_size != wl->ob_size && (op == Py_EQ || op == Py_NE)) {
  1739. /* Shortcut: if the lengths differ, the lists differ */
  1740. PyObject *res;
  1741. if (op == Py_EQ)
  1742. res = Py_False;
  1743. else
  1744. res = Py_True;
  1745. Py_INCREF(res);
  1746. return res;
  1747. }
  1748. /* Search for the first index where items are different */
  1749. for (i = 0; i < vl->ob_size && i < wl->ob_size; i++) {
  1750. int k = PyObject_RichCompareBool(vl->ob_item[i],
  1751. wl->ob_item[i], Py_EQ);
  1752. if (k < 0)
  1753. return NULL;
  1754. if (!k)
  1755. break;
  1756. }
  1757. if (i >= vl->ob_size || i >= wl->ob_size) {
  1758. /* No more items to compare -- compare sizes */
  1759. int vs = vl->ob_size;
  1760. int ws = wl->ob_size;
  1761. int cmp;
  1762. PyObject *res;
  1763. switch (op) {
  1764. case Py_LT: cmp = vs < ws; break;
  1765. case Py_LE: cmp = vs <= ws; break;
  1766. case Py_EQ: cmp = vs == ws; break;
  1767. case Py_NE: cmp = vs != ws; break;
  1768. case Py_GT: cmp = vs > ws; break;
  1769. case Py_GE: cmp = vs >= ws; break;
  1770. default: return NULL; /* cannot happen */
  1771. }
  1772. if (cmp)
  1773. res = Py_True;
  1774. else
  1775. res = Py_False;
  1776. Py_INCREF(res);
  1777. return res;
  1778. }
  1779. /* We have an item that differs -- shortcuts for EQ/NE */
  1780. if (op == Py_EQ) {
  1781. Py_INCREF(Py_False);
  1782. return Py_False;
  1783. }
  1784. if (op == Py_NE) {
  1785. Py_INCREF(Py_True);
  1786. return Py_True;
  1787. }
  1788. /* Compare the final item again using the proper operator */
  1789. return PyObject_RichCompare(vl->ob_item[i], wl->ob_item[i], op);
  1790. }
  1791. /* Adapted from newer code by Tim */
  1792. static int
  1793. list_fill(PyListObject *result, PyObject *v)
  1794. {
  1795. PyObject *it; /* iter(v) */
  1796. int n; /* guess for result list size */
  1797. int i;
  1798. n = result->ob_size;
  1799. /* Special-case list(a_list), for speed. */
  1800. if (PyList_Check(v)) {
  1801. if (v == (PyObject *)result)
  1802. return 0; /* source is destination, we're done */
  1803. return list_ass_slice(result, 0, n, v);
  1804. }
  1805. /* Empty previous contents */
  1806. if (n != 0) {
  1807. if (list_ass_slice(result, 0, n, (PyObject *)NULL) != 0)
  1808. return -1;
  1809. }
  1810. /* Get iterator. There may be some low-level efficiency to be gained
  1811. * by caching the tp_iternext slot instead of using PyIter_Next()
  1812. * later, but premature optimization is the root etc.
  1813. */
  1814. it = PyObject_GetIter(v);
  1815. if (it == NULL)
  1816. return -1;
  1817. /* Guess a result list size. */
  1818. n = -1; /* unknown */
  1819. if (PySequence_Check(v) &&
  1820. v->ob_type->tp_as_sequence->sq_length) {
  1821. n = PySequence_Size(v);
  1822. if (n < 0)
  1823. PyErr_Clear();
  1824. }
  1825. if (n < 0)
  1826. n = 8; /* arbitrary */
  1827. NRESIZE(result->ob_item, PyObject*, n);
  1828. if (result->ob_item == NULL) {
  1829. PyErr_NoMemory();
  1830. goto error;
  1831. }
  1832. memset(result->ob_item, 0, sizeof(*result->ob_item) * n);
  1833. result->ob_size = n;
  1834. /* Run iterator to exhaustion. */
  1835. for (i = 0; ; i++) {
  1836. PyObject *item = PyIter_Next(it);
  1837. if (item == NULL) {
  1838. if (PyErr_Occurred())
  1839. goto error;
  1840. break;
  1841. }
  1842. if (i < n)
  1843. PyList_SET_ITEM(result, i, item); /* steals ref */
  1844. else {
  1845. int status = ins1(result, result->ob_size, item);
  1846. Py_DECREF(item); /* append creates a new ref */
  1847. if (status < 0)
  1848. goto error;
  1849. }
  1850. }
  1851. /* Cut back result list if initial guess was too large. */
  1852. if (i < n && result != NULL) {
  1853. if (list_ass_slice(result, i, n, (PyObject *)NULL) != 0)
  1854. goto error;
  1855. }
  1856. Py_DECREF(it);
  1857. return 0;
  1858. error:
  1859. Py_DECREF(it);
  1860. return -1;
  1861. }
  1862. static int
  1863. list_init(PyListObject *self, PyObject *args, PyObject *kw)
  1864. {
  1865. PyObject *arg = NULL;
  1866. static char *kwlist[] = {"sequence", 0};
  1867. if (!PyArg_ParseTupleAndKeywords(args, kw, "|O:list", kwlist, &arg))
  1868. return -1;
  1869. if (arg != NULL)
  1870. return list_fill(self, arg);
  1871. if (self->ob_size > 0)
  1872. return list_ass_slice(self, 0, self->ob_size, (PyObject*)NULL);
  1873. return 0;
  1874. }
  1875. static long
  1876. list_nohash(PyObject *self)
  1877. {
  1878. PyErr_SetString(PyExc_TypeError, "list objects are unhashable");
  1879. return -1;
  1880. }
  1881. PyDoc_STRVAR(append_doc,
  1882. "L.append(object) -- append object to end");
  1883. PyDoc_STRVAR(extend_doc,
  1884. "L.extend(iterable) -- extend list by appending elements from the iterable");
  1885. PyDoc_STRVAR(insert_doc,
  1886. "L.insert(index, object) -- insert object before index");
  1887. PyDoc_STRVAR(pop_doc,
  1888. "L.pop([index]) -> item -- remove and return item at index (default last)");
  1889. PyDoc_STRVAR(remove_doc,
  1890. "L.remove(value) -- remove first occurrence of value");
  1891. PyDoc_STRVAR(index_doc,
  1892. "L.index(value, [start, [stop]]) -> integer -- return first index of value");
  1893. PyDoc_STRVAR(count_doc,
  1894. "L.count(value) -> integer -- return number of occurrences of value");
  1895. PyDoc_STRVAR(reverse_doc,
  1896. "L.reverse() -- reverse *IN PLACE*");
  1897. PyDoc_STRVAR(sort_doc,
  1898. "L.sort(cmpfunc=None) -- stable sort *IN PLACE*; cmpfunc(x, y) -> -1, 0, 1");
  1899. static PyMethodDef list_methods[] = {
  1900. {"append", (PyCFunction)listappend, METH_O, append_doc},
  1901. {"insert", (PyCFunction)listinsert, METH_VARARGS, insert_doc},
  1902. {"extend", (PyCFunction)listextend, METH_O, extend_doc},
  1903. {"pop", (PyCFunction)listpop, METH_VARARGS, pop_doc},
  1904. {"remove", (PyCFunction)listremove, METH_O, remove_doc},
  1905. {"index", (PyCFunction)listindex, METH_VARARGS, index_doc},
  1906. {"count", (PyCFunction)listcount, METH_O, count_doc},
  1907. {"reverse", (PyCFunction)listreverse, METH_NOARGS, reverse_doc},
  1908. {"sort", (PyCFunction)listsort, METH_VARARGS, sort_doc},
  1909. {NULL, NULL} /* sentinel */
  1910. };
  1911. static PySequenceMethods list_as_sequence = {
  1912. (inquiry)list_length, /* sq_length */
  1913. (binaryfunc)list_concat, /* sq_concat */
  1914. (intargfunc)list_repeat, /* sq_repeat */
  1915. (intargfunc)list_item, /* sq_item */
  1916. (intintargfunc)list_slice, /* sq_slice */
  1917. (intobjargproc)list_ass_item, /* sq_ass_item */
  1918. (intintobjargproc)list_ass_slice, /* sq_ass_slice */
  1919. (objobjproc)list_contains, /* sq_contains */
  1920. (binaryfunc)list_inplace_concat, /* sq_inplace_concat */
  1921. (intargfunc)list_inplace_repeat, /* sq_inplace_repeat */
  1922. };
  1923. PyDoc_STRVAR(list_doc,
  1924. "list() -> new list\n"
  1925. "list(sequence) -> new list initialized from sequence's items");
  1926. static PyObject *list_iter(PyObject *seq);
  1927. static PyObject *
  1928. list_subscript(PyListObject* self, PyObject* item)
  1929. {
  1930. if (PyInt_Check(item)) {
  1931. long i = PyInt_AS_LONG(item);
  1932. if (i < 0)
  1933. i += PyList_GET_SIZE(self);
  1934. return list_item(self, i);
  1935. }
  1936. else if (PyLong_Check(item)) {
  1937. long i = PyLong_AsLong(item);
  1938. if (i == -1 && PyErr_Occurred())
  1939. return NULL;
  1940. if (i < 0)
  1941. i += PyList_GET_SIZE(self);
  1942. return list_item(self, i);
  1943. }
  1944. else if (PySlice_Check(item)) {
  1945. int start, stop, step, slicelength, cur, i;
  1946. PyObject* result;
  1947. PyObject* it;
  1948. if (PySlice_GetIndicesEx((PySliceObject*)item, self->ob_size,
  1949. &start, &stop, &step, &slicelength) < 0) {
  1950. return NULL;

Large files files are truncated, but you can click here to view the full file