PageRenderTime 49ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 1ms

/MOULOpenSourceClientPlugin/Plasma20/SDKs/XPlatform/Cypython-2.3.3/Objects/listobject.c

https://bitbucket.org/boq/cwe-ou
C | 2501 lines | 1940 code | 222 blank | 339 comment | 524 complexity | c198fbe1dac2249b102f8ee257067324 MD5 | raw file
Possible License(s): GPL-3.0, AGPL-1.0, BSD-3-Clause, MPL-2.0-no-copyleft-exception, LGPL-3.0

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

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

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