1990-10-14 09:07:46 -03:00
|
|
|
/* List object implementation */
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
#include "Python.h"
|
2020-06-22 12:27:35 -03:00
|
|
|
#include "pycore_abstract.h" // _PyIndex_Check()
|
2023-08-24 15:25:22 -03:00
|
|
|
#include "pycore_ceval.h" // _PyEval_GetBuiltin()
|
2020-06-22 12:27:35 -03:00
|
|
|
#include "pycore_interp.h" // PyInterpreterState.list
|
2022-06-21 07:19:26 -03:00
|
|
|
#include "pycore_list.h" // struct _Py_list_state, _PyListIterObject
|
2023-03-22 11:49:51 -03:00
|
|
|
#include "pycore_long.h" // _PyLong_DigitCount
|
2023-07-03 06:39:11 -03:00
|
|
|
#include "pycore_modsupport.h" // _PyArg_NoKwnames()
|
2023-07-23 17:09:08 -03:00
|
|
|
#include "pycore_object.h" // _PyObject_GC_TRACK(), _PyDebugAllocatorStats()
|
2020-06-22 12:27:35 -03:00
|
|
|
#include "pycore_tuple.h" // _PyTuple_FromArray()
|
1994-08-29 09:45:32 -03:00
|
|
|
#include <stddef.h>
|
1990-10-14 09:07:46 -03:00
|
|
|
|
2017-03-11 03:19:20 -04:00
|
|
|
/*[clinic input]
|
|
|
|
class list "PyListObject *" "&PyList_Type"
|
|
|
|
[clinic start generated code]*/
|
|
|
|
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=f9b222678f9f71e0]*/
|
|
|
|
|
|
|
|
#include "clinic/listobject.c.h"
|
|
|
|
|
2022-02-22 20:23:51 -04:00
|
|
|
_Py_DECLARE_STR(list_err, "list index out of range");
|
2022-01-22 21:20:44 -04:00
|
|
|
|
2024-01-11 20:31:28 -04:00
|
|
|
#ifdef WITH_FREELISTS
|
2020-06-23 11:40:40 -03:00
|
|
|
static struct _Py_list_state *
|
|
|
|
get_list_state(void)
|
|
|
|
{
|
2024-01-09 19:04:41 -04:00
|
|
|
_PyFreeListState *state = _PyFreeListState_GET();
|
|
|
|
assert(state != NULL);
|
2024-01-26 11:25:16 -04:00
|
|
|
return &state->lists;
|
2020-06-23 11:40:40 -03:00
|
|
|
}
|
2021-10-21 10:12:20 -03:00
|
|
|
#endif
|
2020-06-23 11:40:40 -03:00
|
|
|
|
2004-07-30 23:24:20 -03:00
|
|
|
/* Ensure ob_item has room for at least newsize elements, and set
|
|
|
|
* ob_size to newsize. If newsize > ob_size on entry, the content
|
|
|
|
* of the new slots at exit is undefined heap trash; it's the caller's
|
2011-03-16 06:05:33 -03:00
|
|
|
* responsibility to overwrite them with sane values.
|
2004-07-30 23:24:20 -03:00
|
|
|
* The number of allocated elements may grow, shrink, or stay the same.
|
|
|
|
* Failure is impossible if newsize <= self.allocated on entry, although
|
|
|
|
* that partly relies on an assumption that the system realloc() never
|
|
|
|
* fails when passed a number of bytes <= the number of bytes last
|
|
|
|
* allocated (the C standard doesn't guarantee this, but it's hard to
|
|
|
|
* imagine a realloc implementation where it wouldn't be true).
|
|
|
|
* Note that self->ob_item may change, and even if newsize is less
|
|
|
|
* than ob_size on entry.
|
|
|
|
*/
|
1995-01-26 18:59:43 -04:00
|
|
|
static int
|
2006-02-15 13:27:45 -04:00
|
|
|
list_resize(PyListObject *self, Py_ssize_t newsize)
|
* Optimized list appends and pops by making fewer calls the underlying system
realloc(). This is achieved by tracking the overallocation size in a new
field and using that information to skip calls to realloc() whenever
possible.
* Simplified and tightened the amount of overallocation. For larger lists,
this overallocates by 1/8th (compared to the previous scheme which ranged
between 1/4th to 1/32nd over-allocation). For smaller lists (n<6), the
maximum overallocation is one byte (formerly it could be upto eight bytes).
This saves memory in applications with large numbers of small lists.
* Eliminated the NRESIZE macro in favor of a new, static list_resize function
that encapsulates the resizing logic. Coverting this back to macro would
give a small (under 1%) speed-up. This was too small to warrant the loss
of readability, maintainability, and de-coupling.
* Some functions using NRESIZE had grown unnecessarily complex in their
efforts to bend to the macro's calling pattern. With the new list_resize
function in place, those other functions could be simplified. That is
being saved for a separate patch.
* The ob_item==NULL check could be eliminated from the new list_resize
function. This would entail finding each piece of code that sets ob_item
to NULL and adding a new line to invalidate the overallocation tracking
field. Rather than impose a new requirement on other pieces of list code,
it was preferred to leave the NULL check in place and retain the benefits
of decoupling, maintainability and information hiding (only PyList_New()
and list_sort() need to know about the new field). This approach also
reduces the odds of breaking an extension module.
(Collaborative effort by Raymond Hettinger, Hye-Shik Chang, Tim Peters,
and Armin Rigo.)
2004-02-13 07:36:39 -04:00
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
PyObject **items;
|
2017-02-22 00:32:30 -04:00
|
|
|
size_t new_allocated, num_allocated_bytes;
|
2010-05-09 12:52:27 -03:00
|
|
|
Py_ssize_t allocated = self->allocated;
|
|
|
|
|
|
|
|
/* Bypass realloc() when a previous overallocation is large enough
|
|
|
|
to accommodate the newsize. If the newsize falls lower than half
|
|
|
|
the allocated size, then proceed with the realloc() to shrink the list.
|
|
|
|
*/
|
|
|
|
if (allocated >= newsize && newsize >= (allocated >> 1)) {
|
|
|
|
assert(self->ob_item != NULL || newsize == 0);
|
2020-02-07 18:18:08 -04:00
|
|
|
Py_SET_SIZE(self, newsize);
|
2010-05-09 12:52:27 -03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* This over-allocates proportional to the list size, making room
|
|
|
|
* for additional growth. The over-allocation is mild, but is
|
|
|
|
* enough to give linear-time amortized behavior over a long
|
|
|
|
* sequence of appends() in the presence of a poorly-performing
|
|
|
|
* system realloc().
|
2020-03-17 18:46:00 -03:00
|
|
|
* Add padding to make the allocated size multiple of 4.
|
|
|
|
* The growth pattern is: 0, 4, 8, 16, 24, 32, 40, 52, 64, 76, ...
|
2017-02-22 00:32:30 -04:00
|
|
|
* Note: new_allocated won't overflow because the largest possible value
|
|
|
|
* is PY_SSIZE_T_MAX * (9 / 8) + 6 which always fits in a size_t.
|
2010-05-09 12:52:27 -03:00
|
|
|
*/
|
2020-03-17 18:46:00 -03:00
|
|
|
new_allocated = ((size_t)newsize + (newsize >> 3) + 6) & ~(size_t)3;
|
2020-06-29 15:56:56 -03:00
|
|
|
/* Do not overallocate if the new size is closer to overallocated size
|
2020-03-17 18:46:00 -03:00
|
|
|
* than to the old size.
|
|
|
|
*/
|
|
|
|
if (newsize - Py_SIZE(self) > (Py_ssize_t)(new_allocated - newsize))
|
|
|
|
new_allocated = ((size_t)newsize + 3) & ~(size_t)3;
|
2010-05-09 12:52:27 -03:00
|
|
|
|
|
|
|
if (newsize == 0)
|
|
|
|
new_allocated = 0;
|
2022-09-28 19:07:07 -03:00
|
|
|
if (new_allocated <= (size_t)PY_SSIZE_T_MAX / sizeof(PyObject *)) {
|
|
|
|
num_allocated_bytes = new_allocated * sizeof(PyObject *);
|
|
|
|
items = (PyObject **)PyMem_Realloc(self->ob_item, num_allocated_bytes);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// integer overflow
|
|
|
|
items = NULL;
|
|
|
|
}
|
2010-05-09 12:52:27 -03:00
|
|
|
if (items == NULL) {
|
|
|
|
PyErr_NoMemory();
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
self->ob_item = items;
|
2020-02-07 18:18:08 -04:00
|
|
|
Py_SET_SIZE(self, newsize);
|
2010-05-09 12:52:27 -03:00
|
|
|
self->allocated = new_allocated;
|
|
|
|
return 0;
|
* Optimized list appends and pops by making fewer calls the underlying system
realloc(). This is achieved by tracking the overallocation size in a new
field and using that information to skip calls to realloc() whenever
possible.
* Simplified and tightened the amount of overallocation. For larger lists,
this overallocates by 1/8th (compared to the previous scheme which ranged
between 1/4th to 1/32nd over-allocation). For smaller lists (n<6), the
maximum overallocation is one byte (formerly it could be upto eight bytes).
This saves memory in applications with large numbers of small lists.
* Eliminated the NRESIZE macro in favor of a new, static list_resize function
that encapsulates the resizing logic. Coverting this back to macro would
give a small (under 1%) speed-up. This was too small to warrant the loss
of readability, maintainability, and de-coupling.
* Some functions using NRESIZE had grown unnecessarily complex in their
efforts to bend to the macro's calling pattern. With the new list_resize
function in place, those other functions could be simplified. That is
being saved for a separate patch.
* The ob_item==NULL check could be eliminated from the new list_resize
function. This would entail finding each piece of code that sets ob_item
to NULL and adding a new line to invalidate the overallocation tracking
field. Rather than impose a new requirement on other pieces of list code,
it was preferred to leave the NULL check in place and retain the benefits
of decoupling, maintainability and information hiding (only PyList_New()
and list_sort() need to know about the new field). This approach also
reduces the odds of breaking an extension module.
(Collaborative effort by Raymond Hettinger, Hye-Shik Chang, Tim Peters,
and Armin Rigo.)
2004-02-13 07:36:39 -04:00
|
|
|
}
|
1995-01-26 18:59:43 -04:00
|
|
|
|
2018-10-28 17:16:26 -03:00
|
|
|
static int
|
|
|
|
list_preallocate_exact(PyListObject *self, Py_ssize_t size)
|
|
|
|
{
|
|
|
|
assert(self->ob_item == NULL);
|
2018-12-29 18:31:36 -04:00
|
|
|
assert(size > 0);
|
2018-10-28 17:16:26 -03:00
|
|
|
|
2022-05-19 02:43:50 -03:00
|
|
|
/* Since the Python memory allocator has granularity of 16 bytes on 64-bit
|
|
|
|
* platforms (8 on 32-bit), there is no benefit of allocating space for
|
|
|
|
* the odd number of items, and there is no drawback of rounding the
|
|
|
|
* allocated size up to the nearest even number.
|
|
|
|
*/
|
|
|
|
size = (size + 1) & ~(size_t)1;
|
2018-12-29 18:31:36 -04:00
|
|
|
PyObject **items = PyMem_New(PyObject*, size);
|
2018-10-28 17:16:26 -03:00
|
|
|
if (items == NULL) {
|
|
|
|
PyErr_NoMemory();
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
self->ob_item = items;
|
2018-12-29 18:31:36 -04:00
|
|
|
self->allocated = size;
|
2018-10-28 17:16:26 -03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-04-28 21:29:20 -03:00
|
|
|
void
|
2024-01-09 19:04:41 -04:00
|
|
|
_PyList_ClearFreeList(_PyFreeListState *freelist_state, int is_finalization)
|
2004-10-07 00:58:07 -03:00
|
|
|
{
|
2024-01-11 20:31:28 -04:00
|
|
|
#ifdef WITH_FREELISTS
|
2024-01-26 11:25:16 -04:00
|
|
|
struct _Py_list_state *state = &freelist_state->lists;
|
2024-01-09 19:04:41 -04:00
|
|
|
while (state->numfree > 0) {
|
2020-06-04 21:05:41 -03:00
|
|
|
PyListObject *op = state->free_list[--state->numfree];
|
2010-05-09 12:52:27 -03:00
|
|
|
assert(PyList_CheckExact(op));
|
|
|
|
PyObject_GC_Del(op);
|
|
|
|
}
|
2024-01-09 19:04:41 -04:00
|
|
|
if (is_finalization) {
|
|
|
|
state->numfree = -1;
|
|
|
|
}
|
2021-10-21 10:12:20 -03:00
|
|
|
#endif
|
2011-11-14 19:00:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2024-01-09 19:04:41 -04:00
|
|
|
_PyList_Fini(_PyFreeListState *state)
|
2011-11-14 19:00:12 -04:00
|
|
|
{
|
2024-02-01 16:53:53 -04:00
|
|
|
// With Py_GIL_DISABLED:
|
|
|
|
// the freelists for the current thread state have already been cleared.
|
|
|
|
#ifndef Py_GIL_DISABLED
|
2024-01-09 19:04:41 -04:00
|
|
|
_PyList_ClearFreeList(state, 1);
|
2024-02-01 16:53:53 -04:00
|
|
|
#endif
|
2004-10-07 00:58:07 -03:00
|
|
|
}
|
|
|
|
|
2012-06-22 15:55:41 -03:00
|
|
|
/* Print summary info about the state of the optimized allocator */
|
|
|
|
void
|
|
|
|
_PyList_DebugMallocStats(FILE *out)
|
|
|
|
{
|
2024-01-11 20:31:28 -04:00
|
|
|
#ifdef WITH_FREELISTS
|
2020-06-23 11:40:40 -03:00
|
|
|
struct _Py_list_state *state = get_list_state();
|
2012-06-22 15:55:41 -03:00
|
|
|
_PyDebugAllocatorStats(out,
|
|
|
|
"free PyListObject",
|
2020-06-04 21:05:41 -03:00
|
|
|
state->numfree, sizeof(PyListObject));
|
2021-10-21 10:12:20 -03:00
|
|
|
#endif
|
2012-06-22 15:55:41 -03:00
|
|
|
}
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
PyObject *
|
2006-02-15 13:27:45 -04:00
|
|
|
PyList_New(Py_ssize_t size)
|
1990-10-14 09:07:46 -03:00
|
|
|
{
|
2021-10-21 10:12:20 -03:00
|
|
|
PyListObject *op;
|
|
|
|
|
2010-05-09 12:52:27 -03:00
|
|
|
if (size < 0) {
|
|
|
|
PyErr_BadInternalCall();
|
|
|
|
return NULL;
|
|
|
|
}
|
2020-06-04 21:05:41 -03:00
|
|
|
|
2024-01-11 20:31:28 -04:00
|
|
|
#ifdef WITH_FREELISTS
|
2020-06-23 11:40:40 -03:00
|
|
|
struct _Py_list_state *state = get_list_state();
|
2024-01-11 20:31:28 -04:00
|
|
|
if (PyList_MAXFREELIST && state->numfree > 0) {
|
2020-06-04 21:05:41 -03:00
|
|
|
state->numfree--;
|
|
|
|
op = state->free_list[state->numfree];
|
2022-05-03 19:40:24 -03:00
|
|
|
OBJECT_STAT_INC(from_freelist);
|
2010-05-09 12:52:27 -03:00
|
|
|
_Py_NewReference((PyObject *)op);
|
2020-06-04 21:05:41 -03:00
|
|
|
}
|
2021-10-21 10:12:20 -03:00
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
op = PyObject_GC_New(PyListObject, &PyList_Type);
|
2020-06-04 21:05:41 -03:00
|
|
|
if (op == NULL) {
|
2010-05-09 12:52:27 -03:00
|
|
|
return NULL;
|
2020-06-04 21:05:41 -03:00
|
|
|
}
|
2010-05-09 12:52:27 -03:00
|
|
|
}
|
2020-06-04 21:05:41 -03:00
|
|
|
if (size <= 0) {
|
2010-05-09 12:52:27 -03:00
|
|
|
op->ob_item = NULL;
|
2020-06-04 21:05:41 -03:00
|
|
|
}
|
2010-05-09 12:52:27 -03:00
|
|
|
else {
|
2016-08-21 04:55:15 -03:00
|
|
|
op->ob_item = (PyObject **) PyMem_Calloc(size, sizeof(PyObject *));
|
2010-05-09 12:52:27 -03:00
|
|
|
if (op->ob_item == NULL) {
|
|
|
|
Py_DECREF(op);
|
|
|
|
return PyErr_NoMemory();
|
|
|
|
}
|
|
|
|
}
|
2020-02-07 18:18:08 -04:00
|
|
|
Py_SET_SIZE(op, size);
|
2010-05-09 12:52:27 -03:00
|
|
|
op->allocated = size;
|
|
|
|
_PyObject_GC_TRACK(op);
|
|
|
|
return (PyObject *) op;
|
1990-10-14 09:07:46 -03:00
|
|
|
}
|
|
|
|
|
2018-08-11 10:12:07 -03:00
|
|
|
static PyObject *
|
|
|
|
list_new_prealloc(Py_ssize_t size)
|
|
|
|
{
|
2021-07-29 08:36:24 -03:00
|
|
|
assert(size > 0);
|
2018-08-11 10:12:07 -03:00
|
|
|
PyListObject *op = (PyListObject *) PyList_New(0);
|
2021-07-29 08:36:24 -03:00
|
|
|
if (op == NULL) {
|
|
|
|
return NULL;
|
2018-08-11 10:12:07 -03:00
|
|
|
}
|
|
|
|
assert(op->ob_item == NULL);
|
|
|
|
op->ob_item = PyMem_New(PyObject *, size);
|
|
|
|
if (op->ob_item == NULL) {
|
|
|
|
Py_DECREF(op);
|
|
|
|
return PyErr_NoMemory();
|
|
|
|
}
|
|
|
|
op->allocated = size;
|
|
|
|
return (PyObject *) op;
|
|
|
|
}
|
|
|
|
|
2006-02-15 13:27:45 -04:00
|
|
|
Py_ssize_t
|
2000-07-09 12:16:51 -03:00
|
|
|
PyList_Size(PyObject *op)
|
1990-10-14 09:07:46 -03:00
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
if (!PyList_Check(op)) {
|
|
|
|
PyErr_BadInternalCall();
|
|
|
|
return -1;
|
|
|
|
}
|
2024-01-31 19:58:08 -04:00
|
|
|
else {
|
|
|
|
return PyList_GET_SIZE(op);
|
|
|
|
}
|
1990-10-14 09:07:46 -03:00
|
|
|
}
|
|
|
|
|
2018-10-11 00:37:28 -03:00
|
|
|
static inline int
|
|
|
|
valid_index(Py_ssize_t i, Py_ssize_t limit)
|
|
|
|
{
|
|
|
|
/* The cast to size_t lets us use just a single comparison
|
|
|
|
to check whether i is in the range: 0 <= i < limit.
|
|
|
|
|
|
|
|
See: Section 14.2 "Bounds Checking" in the Agner Fog
|
|
|
|
optimization manual found at:
|
|
|
|
https://www.agner.org/optimize/optimizing_cpp.pdf
|
|
|
|
*/
|
|
|
|
return (size_t) i < (size_t) limit;
|
|
|
|
}
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
PyObject *
|
2006-02-15 13:27:45 -04:00
|
|
|
PyList_GetItem(PyObject *op, Py_ssize_t i)
|
1990-10-14 09:07:46 -03:00
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
if (!PyList_Check(op)) {
|
|
|
|
PyErr_BadInternalCall();
|
|
|
|
return NULL;
|
|
|
|
}
|
2018-10-11 00:37:28 -03:00
|
|
|
if (!valid_index(i, Py_SIZE(op))) {
|
2022-02-22 20:23:51 -04:00
|
|
|
_Py_DECLARE_STR(list_err, "list index out of range");
|
|
|
|
PyErr_SetObject(PyExc_IndexError, &_Py_STR(list_err));
|
2010-05-09 12:52:27 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return ((PyListObject *)op) -> ob_item[i];
|
1990-10-14 09:07:46 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2013-08-13 15:18:52 -03:00
|
|
|
PyList_SetItem(PyObject *op, Py_ssize_t i,
|
|
|
|
PyObject *newitem)
|
1990-10-14 09:07:46 -03:00
|
|
|
{
|
2013-08-13 15:18:52 -03:00
|
|
|
PyObject **p;
|
2010-05-09 12:52:27 -03:00
|
|
|
if (!PyList_Check(op)) {
|
|
|
|
Py_XDECREF(newitem);
|
|
|
|
PyErr_BadInternalCall();
|
|
|
|
return -1;
|
|
|
|
}
|
2024-01-15 20:11:14 -04:00
|
|
|
int ret;
|
|
|
|
PyListObject *self = ((PyListObject *)op);
|
|
|
|
Py_BEGIN_CRITICAL_SECTION(self);
|
|
|
|
if (!valid_index(i, Py_SIZE(self))) {
|
2010-05-09 12:52:27 -03:00
|
|
|
Py_XDECREF(newitem);
|
|
|
|
PyErr_SetString(PyExc_IndexError,
|
|
|
|
"list assignment index out of range");
|
2024-01-15 20:11:14 -04:00
|
|
|
ret = -1;
|
|
|
|
goto end;
|
2010-05-09 12:52:27 -03:00
|
|
|
}
|
2024-01-15 20:11:14 -04:00
|
|
|
p = self->ob_item + i;
|
2016-04-06 03:50:03 -03:00
|
|
|
Py_XSETREF(*p, newitem);
|
2024-01-15 20:11:14 -04:00
|
|
|
ret = 0;
|
|
|
|
end:
|
|
|
|
Py_END_CRITICAL_SECTION();
|
|
|
|
return ret;
|
1990-10-14 09:07:46 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2006-02-15 13:27:45 -04:00
|
|
|
ins1(PyListObject *self, Py_ssize_t where, PyObject *v)
|
1990-10-14 09:07:46 -03:00
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
Py_ssize_t i, n = Py_SIZE(self);
|
|
|
|
PyObject **items;
|
|
|
|
if (v == NULL) {
|
|
|
|
PyErr_BadInternalCall();
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2020-05-25 11:54:40 -03:00
|
|
|
assert((size_t)n + 1 < PY_SSIZE_T_MAX);
|
2016-01-25 14:33:30 -04:00
|
|
|
if (list_resize(self, n+1) < 0)
|
2010-05-09 12:52:27 -03:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (where < 0) {
|
|
|
|
where += n;
|
|
|
|
if (where < 0)
|
|
|
|
where = 0;
|
|
|
|
}
|
|
|
|
if (where > n)
|
|
|
|
where = n;
|
|
|
|
items = self->ob_item;
|
|
|
|
for (i = n; --i >= where; )
|
|
|
|
items[i+1] = items[i];
|
2022-11-10 17:09:18 -04:00
|
|
|
items[where] = Py_NewRef(v);
|
2010-05-09 12:52:27 -03:00
|
|
|
return 0;
|
1990-10-14 09:07:46 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2006-02-15 13:27:45 -04:00
|
|
|
PyList_Insert(PyObject *op, Py_ssize_t where, PyObject *newitem)
|
1990-10-14 09:07:46 -03:00
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
if (!PyList_Check(op)) {
|
|
|
|
PyErr_BadInternalCall();
|
|
|
|
return -1;
|
|
|
|
}
|
2024-01-15 20:11:14 -04:00
|
|
|
PyListObject *self = (PyListObject *)op;
|
|
|
|
int err;
|
|
|
|
Py_BEGIN_CRITICAL_SECTION(self);
|
|
|
|
err = ins1(self, where, newitem);
|
|
|
|
Py_END_CRITICAL_SECTION();
|
|
|
|
return err;
|
1990-10-14 09:07:46 -03:00
|
|
|
}
|
|
|
|
|
2022-04-01 07:23:42 -03:00
|
|
|
/* internal, used by _PyList_AppendTakeRef */
|
|
|
|
int
|
|
|
|
_PyList_AppendTakeRefListResize(PyListObject *self, PyObject *newitem)
|
2004-04-12 10:05:09 -03:00
|
|
|
{
|
2024-01-31 19:58:08 -04:00
|
|
|
Py_ssize_t len = Py_SIZE(self);
|
2022-04-01 07:23:42 -03:00
|
|
|
assert(self->allocated == -1 || self->allocated == len);
|
|
|
|
if (list_resize(self, len + 1) < 0) {
|
|
|
|
Py_DECREF(newitem);
|
2010-05-09 12:52:27 -03:00
|
|
|
return -1;
|
2022-04-01 07:23:42 -03:00
|
|
|
}
|
|
|
|
PyList_SET_ITEM(self, len, newitem);
|
2010-05-09 12:52:27 -03:00
|
|
|
return 0;
|
2004-04-12 10:05:09 -03:00
|
|
|
}
|
|
|
|
|
1990-10-14 09:07:46 -03:00
|
|
|
int
|
2000-07-09 12:16:51 -03:00
|
|
|
PyList_Append(PyObject *op, PyObject *newitem)
|
1990-10-14 09:07:46 -03:00
|
|
|
{
|
2022-04-01 07:23:42 -03:00
|
|
|
if (PyList_Check(op) && (newitem != NULL)) {
|
2024-01-31 19:58:08 -04:00
|
|
|
int ret;
|
|
|
|
Py_BEGIN_CRITICAL_SECTION(op);
|
|
|
|
ret = _PyList_AppendTakeRef((PyListObject *)op, Py_NewRef(newitem));
|
|
|
|
Py_END_CRITICAL_SECTION();
|
|
|
|
return ret;
|
2022-04-01 07:23:42 -03:00
|
|
|
}
|
2010-05-09 12:52:27 -03:00
|
|
|
PyErr_BadInternalCall();
|
|
|
|
return -1;
|
1990-10-14 09:07:46 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Methods */
|
|
|
|
|
|
|
|
static void
|
2024-01-02 09:41:32 -04:00
|
|
|
list_dealloc(PyObject *self)
|
1990-10-14 09:07:46 -03:00
|
|
|
{
|
2024-01-02 09:41:32 -04:00
|
|
|
PyListObject *op = (PyListObject *)self;
|
2010-05-09 12:52:27 -03:00
|
|
|
Py_ssize_t i;
|
|
|
|
PyObject_GC_UnTrack(op);
|
2019-05-10 14:21:11 -03:00
|
|
|
Py_TRASHCAN_BEGIN(op, list_dealloc)
|
2010-05-09 12:52:27 -03:00
|
|
|
if (op->ob_item != NULL) {
|
|
|
|
/* Do it backwards, for Christian Tismer.
|
|
|
|
There's a simple test case where somehow this reduces
|
|
|
|
thrashing when a *very* large list is created and
|
|
|
|
immediately deleted. */
|
|
|
|
i = Py_SIZE(op);
|
|
|
|
while (--i >= 0) {
|
|
|
|
Py_XDECREF(op->ob_item[i]);
|
|
|
|
}
|
2020-12-01 04:56:42 -04:00
|
|
|
PyMem_Free(op->ob_item);
|
2010-05-09 12:52:27 -03:00
|
|
|
}
|
2024-01-11 20:31:28 -04:00
|
|
|
#ifdef WITH_FREELISTS
|
2020-06-23 11:40:40 -03:00
|
|
|
struct _Py_list_state *state = get_list_state();
|
2024-01-11 20:31:28 -04:00
|
|
|
if (state->numfree < PyList_MAXFREELIST && state->numfree >= 0 && PyList_CheckExact(op)) {
|
2020-06-04 21:05:41 -03:00
|
|
|
state->free_list[state->numfree++] = op;
|
2022-05-03 19:40:24 -03:00
|
|
|
OBJECT_STAT_INC(to_freelist);
|
2020-06-04 21:05:41 -03:00
|
|
|
}
|
2021-10-21 10:12:20 -03:00
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
Py_TYPE(op)->tp_free((PyObject *)op);
|
2020-06-04 21:05:41 -03:00
|
|
|
}
|
2019-05-10 14:21:11 -03:00
|
|
|
Py_TRASHCAN_END
|
1990-10-14 09:07:46 -03:00
|
|
|
}
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
static PyObject *
|
2024-01-26 12:20:21 -04:00
|
|
|
list_repr_impl(PyListObject *v)
|
1990-10-14 09:07:46 -03:00
|
|
|
{
|
2013-11-18 16:11:57 -04:00
|
|
|
PyObject *s;
|
|
|
|
_PyUnicodeWriter writer;
|
2024-01-26 12:20:21 -04:00
|
|
|
Py_ssize_t i = Py_ReprEnter((PyObject*)v);
|
2010-05-09 12:52:27 -03:00
|
|
|
if (i != 0) {
|
|
|
|
return i > 0 ? PyUnicode_FromString("[...]") : NULL;
|
|
|
|
}
|
|
|
|
|
2013-11-18 16:11:57 -04:00
|
|
|
_PyUnicodeWriter_Init(&writer);
|
|
|
|
writer.overallocate = 1;
|
2013-11-18 17:15:44 -04:00
|
|
|
/* "[" + "1" + ", 2" * (len - 1) + "]" */
|
|
|
|
writer.min_length = 1 + 1 + (2 + 1) * (Py_SIZE(v) - 1) + 1;
|
2010-05-09 12:52:27 -03:00
|
|
|
|
2013-11-18 16:11:57 -04:00
|
|
|
if (_PyUnicodeWriter_WriteChar(&writer, '[') < 0)
|
2011-10-06 13:57:27 -03:00
|
|
|
goto error;
|
2010-05-09 12:52:27 -03:00
|
|
|
|
|
|
|
/* Do repr() on each element. Note that this may mutate the list,
|
|
|
|
so must refetch the list size on each iteration. */
|
|
|
|
for (i = 0; i < Py_SIZE(v); ++i) {
|
2013-11-18 16:11:57 -04:00
|
|
|
if (i > 0) {
|
2013-11-19 07:54:53 -04:00
|
|
|
if (_PyUnicodeWriter_WriteASCIIString(&writer, ", ", 2) < 0)
|
2013-11-18 16:11:57 -04:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
2010-05-09 12:52:27 -03:00
|
|
|
s = PyObject_Repr(v->ob_item[i]);
|
2013-11-18 16:11:57 -04:00
|
|
|
if (s == NULL)
|
2011-10-06 13:57:27 -03:00
|
|
|
goto error;
|
2013-11-18 16:11:57 -04:00
|
|
|
|
|
|
|
if (_PyUnicodeWriter_WriteStr(&writer, s) < 0) {
|
|
|
|
Py_DECREF(s);
|
2011-10-06 13:57:27 -03:00
|
|
|
goto error;
|
2013-11-18 16:11:57 -04:00
|
|
|
}
|
|
|
|
Py_DECREF(s);
|
2010-05-09 12:52:27 -03:00
|
|
|
}
|
2013-11-18 16:11:57 -04:00
|
|
|
|
2013-11-19 07:09:00 -04:00
|
|
|
writer.overallocate = 0;
|
2013-11-18 16:11:57 -04:00
|
|
|
if (_PyUnicodeWriter_WriteChar(&writer, ']') < 0)
|
2011-10-06 13:57:27 -03:00
|
|
|
goto error;
|
2010-05-09 12:52:27 -03:00
|
|
|
|
2011-10-06 13:57:27 -03:00
|
|
|
Py_ReprLeave((PyObject *)v);
|
2013-11-18 16:11:57 -04:00
|
|
|
return _PyUnicodeWriter_Finish(&writer);
|
2010-05-09 12:52:27 -03:00
|
|
|
|
2011-10-06 13:57:27 -03:00
|
|
|
error:
|
2013-11-18 16:11:57 -04:00
|
|
|
_PyUnicodeWriter_Dealloc(&writer);
|
2010-05-09 12:52:27 -03:00
|
|
|
Py_ReprLeave((PyObject *)v);
|
2011-10-06 13:57:27 -03:00
|
|
|
return NULL;
|
1990-10-14 09:07:46 -03:00
|
|
|
}
|
|
|
|
|
2024-01-26 12:20:21 -04:00
|
|
|
static PyObject *
|
|
|
|
list_repr(PyObject *self)
|
|
|
|
{
|
|
|
|
if (PyList_GET_SIZE(self) == 0) {
|
|
|
|
return PyUnicode_FromString("[]");
|
|
|
|
}
|
|
|
|
PyListObject *v = (PyListObject *)self;
|
|
|
|
PyObject *ret = NULL;
|
|
|
|
Py_BEGIN_CRITICAL_SECTION(v);
|
|
|
|
ret = list_repr_impl(v);
|
|
|
|
Py_END_CRITICAL_SECTION();
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2006-02-15 13:27:45 -04:00
|
|
|
static Py_ssize_t
|
2024-01-02 09:41:32 -04:00
|
|
|
list_length(PyObject *a)
|
1990-10-14 09:07:46 -03:00
|
|
|
{
|
2024-01-26 12:20:21 -04:00
|
|
|
return PyList_GET_SIZE(a);
|
1990-10-14 09:07:46 -03:00
|
|
|
}
|
|
|
|
|
2000-04-27 18:41:03 -03:00
|
|
|
static int
|
2024-01-02 09:41:32 -04:00
|
|
|
list_contains(PyObject *aa, PyObject *el)
|
2000-04-27 18:41:03 -03:00
|
|
|
{
|
2024-01-02 09:41:32 -04:00
|
|
|
PyListObject *a = (PyListObject *)aa;
|
2020-01-27 11:02:23 -04:00
|
|
|
PyObject *item;
|
2010-05-09 12:52:27 -03:00
|
|
|
Py_ssize_t i;
|
|
|
|
int cmp;
|
2000-04-27 18:41:03 -03:00
|
|
|
|
2020-01-27 11:02:23 -04:00
|
|
|
for (i = 0, cmp = 0 ; cmp == 0 && i < Py_SIZE(a); ++i) {
|
|
|
|
item = PyList_GET_ITEM(a, i);
|
|
|
|
Py_INCREF(item);
|
2020-01-27 13:04:25 -04:00
|
|
|
cmp = PyObject_RichCompareBool(item, el, Py_EQ);
|
2020-01-27 11:02:23 -04:00
|
|
|
Py_DECREF(item);
|
|
|
|
}
|
2010-05-09 12:52:27 -03:00
|
|
|
return cmp;
|
2000-04-27 18:41:03 -03:00
|
|
|
}
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
static PyObject *
|
2024-01-02 09:41:32 -04:00
|
|
|
list_item(PyObject *aa, Py_ssize_t i)
|
1990-10-14 09:07:46 -03:00
|
|
|
{
|
2024-01-02 09:41:32 -04:00
|
|
|
PyListObject *a = (PyListObject *)aa;
|
2024-01-31 19:58:08 -04:00
|
|
|
if (!valid_index(i, PyList_GET_SIZE(a))) {
|
2022-02-22 20:23:51 -04:00
|
|
|
PyErr_SetObject(PyExc_IndexError, &_Py_STR(list_err));
|
2010-05-09 12:52:27 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
2022-11-10 17:09:18 -04:00
|
|
|
return Py_NewRef(a->ob_item[i]);
|
1990-10-14 09:07:46 -03:00
|
|
|
}
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
static PyObject *
|
2006-02-15 13:27:45 -04:00
|
|
|
list_slice(PyListObject *a, Py_ssize_t ilow, Py_ssize_t ihigh)
|
1990-10-14 09:07:46 -03:00
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
PyListObject *np;
|
|
|
|
PyObject **src, **dest;
|
|
|
|
Py_ssize_t i, len;
|
|
|
|
len = ihigh - ilow;
|
2021-07-29 08:36:24 -03:00
|
|
|
if (len <= 0) {
|
|
|
|
return PyList_New(0);
|
|
|
|
}
|
2018-08-11 10:12:07 -03:00
|
|
|
np = (PyListObject *) list_new_prealloc(len);
|
2010-05-09 12:52:27 -03:00
|
|
|
if (np == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
src = a->ob_item + ilow;
|
|
|
|
dest = np->ob_item;
|
|
|
|
for (i = 0; i < len; i++) {
|
|
|
|
PyObject *v = src[i];
|
2022-11-10 17:09:18 -04:00
|
|
|
dest[i] = Py_NewRef(v);
|
2010-05-09 12:52:27 -03:00
|
|
|
}
|
2020-02-07 18:18:08 -04:00
|
|
|
Py_SET_SIZE(np, len);
|
2010-05-09 12:52:27 -03:00
|
|
|
return (PyObject *)np;
|
1990-10-14 09:07:46 -03:00
|
|
|
}
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
PyObject *
|
2006-02-15 13:27:45 -04:00
|
|
|
PyList_GetSlice(PyObject *a, Py_ssize_t ilow, Py_ssize_t ihigh)
|
1993-06-17 09:35:49 -03:00
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
if (!PyList_Check(a)) {
|
|
|
|
PyErr_BadInternalCall();
|
|
|
|
return NULL;
|
|
|
|
}
|
2024-01-31 19:58:08 -04:00
|
|
|
PyObject *ret;
|
|
|
|
Py_BEGIN_CRITICAL_SECTION(a);
|
2019-02-21 02:51:52 -04:00
|
|
|
if (ilow < 0) {
|
|
|
|
ilow = 0;
|
|
|
|
}
|
|
|
|
else if (ilow > Py_SIZE(a)) {
|
|
|
|
ilow = Py_SIZE(a);
|
|
|
|
}
|
|
|
|
if (ihigh < ilow) {
|
|
|
|
ihigh = ilow;
|
|
|
|
}
|
|
|
|
else if (ihigh > Py_SIZE(a)) {
|
|
|
|
ihigh = Py_SIZE(a);
|
|
|
|
}
|
2024-01-31 19:58:08 -04:00
|
|
|
ret = list_slice((PyListObject *)a, ilow, ihigh);
|
|
|
|
Py_END_CRITICAL_SECTION();
|
|
|
|
return ret;
|
1993-06-17 09:35:49 -03:00
|
|
|
}
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
static PyObject *
|
2024-01-02 09:41:32 -04:00
|
|
|
list_concat(PyObject *aa, PyObject *bb)
|
1990-10-14 09:07:46 -03:00
|
|
|
{
|
2024-01-02 09:41:32 -04:00
|
|
|
PyListObject *a = (PyListObject *)aa;
|
2010-05-09 12:52:27 -03:00
|
|
|
Py_ssize_t size;
|
|
|
|
Py_ssize_t i;
|
|
|
|
PyObject **src, **dest;
|
|
|
|
PyListObject *np;
|
|
|
|
if (!PyList_Check(bb)) {
|
|
|
|
PyErr_Format(PyExc_TypeError,
|
|
|
|
"can only concatenate list (not \"%.200s\") to list",
|
2020-02-06 22:04:21 -04:00
|
|
|
Py_TYPE(bb)->tp_name);
|
2010-05-09 12:52:27 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
1997-05-02 00:12:38 -03:00
|
|
|
#define b ((PyListObject *)bb)
|
2020-05-25 11:54:40 -03:00
|
|
|
assert((size_t)Py_SIZE(a) + (size_t)Py_SIZE(b) < PY_SSIZE_T_MAX);
|
2016-07-24 23:39:20 -03:00
|
|
|
size = Py_SIZE(a) + Py_SIZE(b);
|
2021-07-29 08:36:24 -03:00
|
|
|
if (size == 0) {
|
|
|
|
return PyList_New(0);
|
|
|
|
}
|
2018-08-11 10:12:07 -03:00
|
|
|
np = (PyListObject *) list_new_prealloc(size);
|
2010-05-09 12:52:27 -03:00
|
|
|
if (np == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
src = a->ob_item;
|
|
|
|
dest = np->ob_item;
|
|
|
|
for (i = 0; i < Py_SIZE(a); i++) {
|
|
|
|
PyObject *v = src[i];
|
2022-11-10 17:09:18 -04:00
|
|
|
dest[i] = Py_NewRef(v);
|
2010-05-09 12:52:27 -03:00
|
|
|
}
|
|
|
|
src = b->ob_item;
|
|
|
|
dest = np->ob_item + Py_SIZE(a);
|
|
|
|
for (i = 0; i < Py_SIZE(b); i++) {
|
|
|
|
PyObject *v = src[i];
|
2022-11-10 17:09:18 -04:00
|
|
|
dest[i] = Py_NewRef(v);
|
2010-05-09 12:52:27 -03:00
|
|
|
}
|
2020-02-07 18:18:08 -04:00
|
|
|
Py_SET_SIZE(np, size);
|
2010-05-09 12:52:27 -03:00
|
|
|
return (PyObject *)np;
|
1990-10-14 09:07:46 -03:00
|
|
|
#undef b
|
|
|
|
}
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
static PyObject *
|
2024-01-02 09:41:32 -04:00
|
|
|
list_repeat(PyObject *aa, Py_ssize_t n)
|
1991-03-06 09:07:53 -04:00
|
|
|
{
|
2024-01-02 09:41:32 -04:00
|
|
|
PyListObject *a = (PyListObject *)aa;
|
2022-07-25 23:10:23 -03:00
|
|
|
const Py_ssize_t input_size = Py_SIZE(a);
|
|
|
|
if (input_size == 0 || n <= 0)
|
2010-05-09 12:52:27 -03:00
|
|
|
return PyList_New(0);
|
2022-07-25 23:10:23 -03:00
|
|
|
assert(n > 0);
|
|
|
|
|
|
|
|
if (input_size > PY_SSIZE_T_MAX / n)
|
|
|
|
return PyErr_NoMemory();
|
|
|
|
Py_ssize_t output_size = input_size * n;
|
|
|
|
|
|
|
|
PyListObject *np = (PyListObject *) list_new_prealloc(output_size);
|
2010-05-09 12:52:27 -03:00
|
|
|
if (np == NULL)
|
|
|
|
return NULL;
|
2022-07-25 23:10:23 -03:00
|
|
|
|
2022-01-07 23:47:58 -04:00
|
|
|
PyObject **dest = np->ob_item;
|
2022-07-25 23:10:23 -03:00
|
|
|
if (input_size == 1) {
|
2022-01-07 23:47:58 -04:00
|
|
|
PyObject *elem = a->ob_item[0];
|
2022-07-25 23:10:23 -03:00
|
|
|
_Py_RefcntAdd(elem, n);
|
|
|
|
PyObject **dest_end = dest + output_size;
|
2022-01-07 23:47:58 -04:00
|
|
|
while (dest < dest_end) {
|
|
|
|
*dest++ = elem;
|
2010-05-09 12:52:27 -03:00
|
|
|
}
|
2018-08-11 10:12:07 -03:00
|
|
|
}
|
|
|
|
else {
|
2022-01-07 23:47:58 -04:00
|
|
|
PyObject **src = a->ob_item;
|
2022-07-25 23:10:23 -03:00
|
|
|
PyObject **src_end = src + input_size;
|
2022-01-07 23:47:58 -04:00
|
|
|
while (src < src_end) {
|
2022-07-25 23:10:23 -03:00
|
|
|
_Py_RefcntAdd(*src, n);
|
2022-01-07 23:47:58 -04:00
|
|
|
*dest++ = *src++;
|
2010-05-09 12:52:27 -03:00
|
|
|
}
|
2022-07-25 23:10:23 -03:00
|
|
|
|
|
|
|
_Py_memory_repeat((char *)np->ob_item, sizeof(PyObject *)*output_size,
|
|
|
|
sizeof(PyObject *)*input_size);
|
2010-05-09 12:52:27 -03:00
|
|
|
}
|
2022-07-25 23:10:23 -03:00
|
|
|
|
|
|
|
Py_SET_SIZE(np, output_size);
|
2010-05-09 12:52:27 -03:00
|
|
|
return (PyObject *) np;
|
1991-03-06 09:07:53 -04:00
|
|
|
}
|
|
|
|
|
2023-11-13 12:14:56 -04:00
|
|
|
static void
|
|
|
|
list_clear(PyListObject *a)
|
2004-07-29 09:40:23 -03:00
|
|
|
{
|
2023-11-13 12:14:56 -04:00
|
|
|
PyObject **items = a->ob_item;
|
|
|
|
if (items == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Because XDECREF can recursively invoke operations on
|
|
|
|
this list, we make it empty first. */
|
|
|
|
Py_ssize_t i = Py_SIZE(a);
|
|
|
|
Py_SET_SIZE(a, 0);
|
|
|
|
a->ob_item = NULL;
|
|
|
|
a->allocated = 0;
|
|
|
|
while (--i >= 0) {
|
|
|
|
Py_XDECREF(items[i]);
|
2010-05-09 12:52:27 -03:00
|
|
|
}
|
2023-11-13 12:14:56 -04:00
|
|
|
PyMem_Free(items);
|
|
|
|
|
|
|
|
// Note that there is no guarantee that the list is actually empty
|
|
|
|
// at this point, because XDECREF may have populated it indirectly again!
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2024-01-02 09:41:32 -04:00
|
|
|
list_clear_slot(PyObject *self)
|
2023-11-13 12:14:56 -04:00
|
|
|
{
|
2024-01-02 09:41:32 -04:00
|
|
|
list_clear((PyListObject *)self);
|
2010-05-09 12:52:27 -03:00
|
|
|
return 0;
|
2004-07-29 09:40:23 -03:00
|
|
|
}
|
|
|
|
|
2004-07-31 18:53:19 -03:00
|
|
|
/* a[ilow:ihigh] = v if v != NULL.
|
|
|
|
* del a[ilow:ihigh] if v == NULL.
|
|
|
|
*
|
|
|
|
* Special speed gimmick: when v is NULL and ihigh - ilow <= 8, it's
|
|
|
|
* guaranteed the call cannot fail.
|
|
|
|
*/
|
1990-10-14 09:07:46 -03:00
|
|
|
static int
|
2006-02-15 13:27:45 -04:00
|
|
|
list_ass_slice(PyListObject *a, Py_ssize_t ilow, Py_ssize_t ihigh, PyObject *v)
|
1990-10-14 09:07:46 -03:00
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
/* Because [X]DECREF can recursively invoke list operations on
|
|
|
|
this list, we must postpone all [X]DECREF activity until
|
|
|
|
after the list is back in its canonical shape. Therefore
|
|
|
|
we must allocate an additional array, 'recycle', into which
|
|
|
|
we temporarily copy the items that are deleted from the
|
|
|
|
list. :-( */
|
|
|
|
PyObject *recycle_on_stack[8];
|
|
|
|
PyObject **recycle = recycle_on_stack; /* will allocate more if needed */
|
|
|
|
PyObject **item;
|
|
|
|
PyObject **vitem = NULL;
|
|
|
|
PyObject *v_as_SF = NULL; /* PySequence_Fast(v) */
|
|
|
|
Py_ssize_t n; /* # of elements in replacement list */
|
|
|
|
Py_ssize_t norig; /* # of elements in list getting replaced */
|
|
|
|
Py_ssize_t d; /* Change in size */
|
|
|
|
Py_ssize_t k;
|
|
|
|
size_t s;
|
|
|
|
int result = -1; /* guilty until proved innocent */
|
1997-05-02 00:12:38 -03:00
|
|
|
#define b ((PyListObject *)v)
|
2010-05-09 12:52:27 -03:00
|
|
|
if (v == NULL)
|
|
|
|
n = 0;
|
|
|
|
else {
|
|
|
|
if (a == b) {
|
|
|
|
/* Special case "a[i:j] = a" -- copy b first */
|
|
|
|
v = list_slice(b, 0, Py_SIZE(b));
|
|
|
|
if (v == NULL)
|
|
|
|
return result;
|
|
|
|
result = list_ass_slice(a, ilow, ihigh, v);
|
|
|
|
Py_DECREF(v);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
v_as_SF = PySequence_Fast(v, "can only assign an iterable");
|
|
|
|
if(v_as_SF == NULL)
|
|
|
|
goto Error;
|
|
|
|
n = PySequence_Fast_GET_SIZE(v_as_SF);
|
|
|
|
vitem = PySequence_Fast_ITEMS(v_as_SF);
|
|
|
|
}
|
|
|
|
if (ilow < 0)
|
|
|
|
ilow = 0;
|
|
|
|
else if (ilow > Py_SIZE(a))
|
|
|
|
ilow = Py_SIZE(a);
|
|
|
|
|
|
|
|
if (ihigh < ilow)
|
|
|
|
ihigh = ilow;
|
|
|
|
else if (ihigh > Py_SIZE(a))
|
|
|
|
ihigh = Py_SIZE(a);
|
|
|
|
|
|
|
|
norig = ihigh - ilow;
|
|
|
|
assert(norig >= 0);
|
|
|
|
d = n - norig;
|
|
|
|
if (Py_SIZE(a) + d == 0) {
|
|
|
|
Py_XDECREF(v_as_SF);
|
2023-11-13 12:14:56 -04:00
|
|
|
list_clear(a);
|
|
|
|
return 0;
|
2010-05-09 12:52:27 -03:00
|
|
|
}
|
|
|
|
item = a->ob_item;
|
|
|
|
/* recycle the items that we are about to remove */
|
|
|
|
s = norig * sizeof(PyObject *);
|
2016-09-06 21:58:25 -03:00
|
|
|
/* If norig == 0, item might be NULL, in which case we may not memcpy from it. */
|
|
|
|
if (s) {
|
|
|
|
if (s > sizeof(recycle_on_stack)) {
|
2020-12-01 04:56:42 -04:00
|
|
|
recycle = (PyObject **)PyMem_Malloc(s);
|
2016-09-06 21:58:25 -03:00
|
|
|
if (recycle == NULL) {
|
|
|
|
PyErr_NoMemory();
|
|
|
|
goto Error;
|
|
|
|
}
|
2010-05-09 12:52:27 -03:00
|
|
|
}
|
2016-09-06 21:58:25 -03:00
|
|
|
memcpy(recycle, &item[ilow], s);
|
2010-05-09 12:52:27 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (d < 0) { /* Delete -d items */
|
2013-07-19 18:06:21 -03:00
|
|
|
Py_ssize_t tail;
|
|
|
|
tail = (Py_SIZE(a) - ihigh) * sizeof(PyObject *);
|
|
|
|
memmove(&item[ihigh+d], &item[ihigh], tail);
|
|
|
|
if (list_resize(a, Py_SIZE(a) + d) < 0) {
|
|
|
|
memmove(&item[ihigh], &item[ihigh+d], tail);
|
|
|
|
memcpy(&item[ilow], recycle, s);
|
|
|
|
goto Error;
|
|
|
|
}
|
2010-05-09 12:52:27 -03:00
|
|
|
item = a->ob_item;
|
|
|
|
}
|
|
|
|
else if (d > 0) { /* Insert d items */
|
|
|
|
k = Py_SIZE(a);
|
|
|
|
if (list_resize(a, k+d) < 0)
|
|
|
|
goto Error;
|
|
|
|
item = a->ob_item;
|
|
|
|
memmove(&item[ihigh+d], &item[ihigh],
|
|
|
|
(k - ihigh)*sizeof(PyObject *));
|
|
|
|
}
|
|
|
|
for (k = 0; k < n; k++, ilow++) {
|
|
|
|
PyObject *w = vitem[k];
|
2022-11-10 17:09:18 -04:00
|
|
|
item[ilow] = Py_XNewRef(w);
|
2010-05-09 12:52:27 -03:00
|
|
|
}
|
|
|
|
for (k = norig - 1; k >= 0; --k)
|
|
|
|
Py_XDECREF(recycle[k]);
|
|
|
|
result = 0;
|
2004-07-30 23:24:20 -03:00
|
|
|
Error:
|
2010-05-09 12:52:27 -03:00
|
|
|
if (recycle != recycle_on_stack)
|
2020-12-01 04:56:42 -04:00
|
|
|
PyMem_Free(recycle);
|
2010-05-09 12:52:27 -03:00
|
|
|
Py_XDECREF(v_as_SF);
|
|
|
|
return result;
|
1990-10-14 09:07:46 -03:00
|
|
|
#undef b
|
|
|
|
}
|
|
|
|
|
1993-06-17 09:35:49 -03:00
|
|
|
int
|
2006-02-15 13:27:45 -04:00
|
|
|
PyList_SetSlice(PyObject *a, Py_ssize_t ilow, Py_ssize_t ihigh, PyObject *v)
|
1993-06-17 09:35:49 -03:00
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
if (!PyList_Check(a)) {
|
|
|
|
PyErr_BadInternalCall();
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return list_ass_slice((PyListObject *)a, ilow, ihigh, v);
|
1993-06-17 09:35:49 -03:00
|
|
|
}
|
|
|
|
|
2000-08-24 17:08:19 -03:00
|
|
|
static PyObject *
|
2024-01-02 09:41:32 -04:00
|
|
|
list_inplace_repeat(PyObject *_self, Py_ssize_t n)
|
2000-08-24 17:08:19 -03:00
|
|
|
{
|
2024-01-02 09:41:32 -04:00
|
|
|
PyListObject *self = (PyListObject *)_self;
|
2022-07-25 23:10:23 -03:00
|
|
|
Py_ssize_t input_size = PyList_GET_SIZE(self);
|
|
|
|
if (input_size == 0 || n == 1) {
|
2022-11-10 17:09:18 -04:00
|
|
|
return Py_NewRef(self);
|
2010-05-09 12:52:27 -03:00
|
|
|
}
|
2000-08-24 17:08:19 -03:00
|
|
|
|
2010-05-09 12:52:27 -03:00
|
|
|
if (n < 1) {
|
2023-11-13 12:14:56 -04:00
|
|
|
list_clear(self);
|
2022-11-10 17:09:18 -04:00
|
|
|
return Py_NewRef(self);
|
2010-05-09 12:52:27 -03:00
|
|
|
}
|
2000-08-24 17:08:19 -03:00
|
|
|
|
2022-07-25 23:10:23 -03:00
|
|
|
if (input_size > PY_SSIZE_T_MAX / n) {
|
2010-05-09 12:52:27 -03:00
|
|
|
return PyErr_NoMemory();
|
|
|
|
}
|
2022-07-25 23:10:23 -03:00
|
|
|
Py_ssize_t output_size = input_size * n;
|
Merged revisions 60284-60349 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r60286 | christian.heimes | 2008-01-25 15:54:23 +0100 (Fri, 25 Jan 2008) | 1 line
setup.py doesn't pick up changes to a header file
........
r60287 | christian.heimes | 2008-01-25 16:52:11 +0100 (Fri, 25 Jan 2008) | 2 lines
Added the Python core headers Include/*.h and pyconfig.h as dependencies for the extensions in Modules/
It forces a rebuild of all extensions when a header files has been modified
........
r60291 | raymond.hettinger | 2008-01-25 20:24:46 +0100 (Fri, 25 Jan 2008) | 4 lines
Changes 54857 and 54840 broke code and were reverted in Py2.5 just before
it was released, but that reversion never made it to the Py2.6 head.
........
r60296 | guido.van.rossum | 2008-01-25 20:50:26 +0100 (Fri, 25 Jan 2008) | 2 lines
Rewrite the list_inline_repeat overflow check slightly differently.
........
r60301 | thomas.wouters | 2008-01-25 22:09:34 +0100 (Fri, 25 Jan 2008) | 4 lines
Use the right (portable) definition of the max of a Py_ssize_t.
........
r60303 | thomas.wouters | 2008-01-26 02:47:05 +0100 (Sat, 26 Jan 2008) | 5 lines
Make 'testall' work again when building in a separate directory.
test_distutils still fails when doing that.
........
r60305 | neal.norwitz | 2008-01-26 06:54:48 +0100 (Sat, 26 Jan 2008) | 3 lines
Prevent this test from failing if there are transient network problems
by retrying the host for up to 3 times.
........
r60306 | neal.norwitz | 2008-01-26 08:26:12 +0100 (Sat, 26 Jan 2008) | 12 lines
Use a condition variable (threading.Event) rather than sleeps and checking a
global to determine when the server is ready to be used. This slows the test
down, but should make it correct. There was a race condition before where the
server could have assigned a port, yet it wasn't ready to serve requests. If
the client sent a request before the server was completely ready, it would get
an exception. There was machinery to try to handle this condition. All of
that should be unnecessary and removed if this change works. A NOTE was
added as a comment about what needs to be fixed.
The buildbots will tell us if there are more errors or
if this test is now stable.
........
r60307 | neal.norwitz | 2008-01-26 08:38:03 +0100 (Sat, 26 Jan 2008) | 3 lines
Fix exception in tearDown on ppc buildbot. If there's no directory,
that shouldn't cause the test to fail. Just like it setUp.
........
r60308 | raymond.hettinger | 2008-01-26 09:19:06 +0100 (Sat, 26 Jan 2008) | 3 lines
Make PySet_Add() work with frozensets. Works like PyTuple_SetItem() to build-up values in a brand new frozenset.
........
r60309 | neal.norwitz | 2008-01-26 09:26:00 +0100 (Sat, 26 Jan 2008) | 1 line
The OS X buildbot had errors with the unavailable exceptions disabled. Restore it.
........
r60310 | raymond.hettinger | 2008-01-26 09:37:28 +0100 (Sat, 26 Jan 2008) | 4 lines
Let marshal build-up sets and frozensets one element at a time.
Saves the unnecessary creation of a tuple as intermediate container.
........
r60311 | raymond.hettinger | 2008-01-26 09:41:13 +0100 (Sat, 26 Jan 2008) | 1 line
Update test code for change to PySet_Add().
........
r60312 | raymond.hettinger | 2008-01-26 10:31:11 +0100 (Sat, 26 Jan 2008) | 1 line
Revert PySet_Add() changes.
........
r60314 | georg.brandl | 2008-01-26 10:43:35 +0100 (Sat, 26 Jan 2008) | 2 lines
#1934: fix os.path.isabs docs.
........
r60316 | georg.brandl | 2008-01-26 12:00:18 +0100 (Sat, 26 Jan 2008) | 2 lines
Add missing things in re docstring.
........
r60317 | georg.brandl | 2008-01-26 12:02:22 +0100 (Sat, 26 Jan 2008) | 2 lines
Slashes allowed on Windows.
........
r60319 | georg.brandl | 2008-01-26 14:41:21 +0100 (Sat, 26 Jan 2008) | 2 lines
Fix markup again.
........
r60320 | andrew.kuchling | 2008-01-26 14:50:51 +0100 (Sat, 26 Jan 2008) | 1 line
Add some items
........
r60321 | georg.brandl | 2008-01-26 15:02:38 +0100 (Sat, 26 Jan 2008) | 2 lines
Clarify "b" mode under Unix.
........
r60322 | georg.brandl | 2008-01-26 15:03:47 +0100 (Sat, 26 Jan 2008) | 3 lines
#1940: make it possible to use curses.filter() before curses.initscr()
as the documentation says.
........
r60324 | georg.brandl | 2008-01-26 15:14:20 +0100 (Sat, 26 Jan 2008) | 3 lines
#1473257: add generator.gi_code attribute that refers to
the original code object backing the generator. Patch by Collin Winter.
........
r60325 | georg.brandl | 2008-01-26 15:19:22 +0100 (Sat, 26 Jan 2008) | 2 lines
Move C API entries to the corresponding section.
........
r60326 | christian.heimes | 2008-01-26 17:43:35 +0100 (Sat, 26 Jan 2008) | 1 line
Unit test fix from Giampaolo Rodola, #1938
........
r60327 | gregory.p.smith | 2008-01-26 19:51:05 +0100 (Sat, 26 Jan 2008) | 2 lines
Update docs for new callpack params added in r60188
........
r60329 | neal.norwitz | 2008-01-26 21:24:36 +0100 (Sat, 26 Jan 2008) | 3 lines
Cleanup the code a bit. test_rfind is failing on PPC and PPC64 buildbots,
this might fix the problem.
........
r60330 | neal.norwitz | 2008-01-26 22:02:45 +0100 (Sat, 26 Jan 2008) | 1 line
Always try to remove the test file even if close raises an exception
........
r60331 | neal.norwitz | 2008-01-26 22:21:59 +0100 (Sat, 26 Jan 2008) | 3 lines
Reduce the race condition by signalling when the server is ready
and not trying to connect before.
........
r60334 | neal.norwitz | 2008-01-27 00:13:46 +0100 (Sun, 27 Jan 2008) | 5 lines
On some systems (e.g., Ubuntu on hppa) the flush()
doesn't cause the exception, but the close() does.
Will backport.
........
r60335 | neal.norwitz | 2008-01-27 00:14:17 +0100 (Sun, 27 Jan 2008) | 2 lines
Consistently use tempfile.tempdir for the db_home directory.
........
r60338 | neal.norwitz | 2008-01-27 02:44:05 +0100 (Sun, 27 Jan 2008) | 4 lines
Eliminate the sleeps that assume the server will start in .5 seconds.
This should make the test less flaky. It also speeds up the test
by about 75% on my box (20+ seconds -> ~4 seconds).
........
r60342 | neal.norwitz | 2008-01-27 06:02:34 +0100 (Sun, 27 Jan 2008) | 6 lines
Try to prevent this test from being flaky. We might need a sleep in here
which isn't as bad as it sounds. The close() *should* raise an exception,
so if it didn't we should give more time to sync and really raise it.
Will backport.
........
r60344 | jeffrey.yasskin | 2008-01-27 06:40:35 +0100 (Sun, 27 Jan 2008) | 3 lines
Make rational.gcd() public and allow Rational to take decimal strings, per
Raymond's advice.
........
r60345 | neal.norwitz | 2008-01-27 08:36:03 +0100 (Sun, 27 Jan 2008) | 3 lines
Mostly reformat. Also set an error and return NULL if neither MS_WINDOWS
nor UNIX is defined. This may have caused problems on cygwin.
........
r60346 | neal.norwitz | 2008-01-27 08:37:38 +0100 (Sun, 27 Jan 2008) | 3 lines
Use int for the sign rather than a char. char can be signed or unsigned.
It's system dependent. This might fix the problem with test_rfind failing.
........
r60347 | neal.norwitz | 2008-01-27 08:41:33 +0100 (Sun, 27 Jan 2008) | 1 line
Add stdarg include for va_list to get this to compile on cygwin
........
r60348 | raymond.hettinger | 2008-01-27 11:13:57 +0100 (Sun, 27 Jan 2008) | 1 line
Docstring nit
........
r60349 | raymond.hettinger | 2008-01-27 11:47:55 +0100 (Sun, 27 Jan 2008) | 1 line
Removed an unnecessary and confusing paragraph from the namedtuple docs.
........
2008-01-27 11:18:18 -04:00
|
|
|
|
2022-07-25 23:10:23 -03:00
|
|
|
if (list_resize(self, output_size) < 0)
|
2010-05-09 12:52:27 -03:00
|
|
|
return NULL;
|
* Optimized list appends and pops by making fewer calls the underlying system
realloc(). This is achieved by tracking the overallocation size in a new
field and using that information to skip calls to realloc() whenever
possible.
* Simplified and tightened the amount of overallocation. For larger lists,
this overallocates by 1/8th (compared to the previous scheme which ranged
between 1/4th to 1/32nd over-allocation). For smaller lists (n<6), the
maximum overallocation is one byte (formerly it could be upto eight bytes).
This saves memory in applications with large numbers of small lists.
* Eliminated the NRESIZE macro in favor of a new, static list_resize function
that encapsulates the resizing logic. Coverting this back to macro would
give a small (under 1%) speed-up. This was too small to warrant the loss
of readability, maintainability, and de-coupling.
* Some functions using NRESIZE had grown unnecessarily complex in their
efforts to bend to the macro's calling pattern. With the new list_resize
function in place, those other functions could be simplified. That is
being saved for a separate patch.
* The ob_item==NULL check could be eliminated from the new list_resize
function. This would entail finding each piece of code that sets ob_item
to NULL and adding a new line to invalidate the overallocation tracking
field. Rather than impose a new requirement on other pieces of list code,
it was preferred to leave the NULL check in place and retain the benefits
of decoupling, maintainability and information hiding (only PyList_New()
and list_sort() need to know about the new field). This approach also
reduces the odds of breaking an extension module.
(Collaborative effort by Raymond Hettinger, Hye-Shik Chang, Tim Peters,
and Armin Rigo.)
2004-02-13 07:36:39 -04:00
|
|
|
|
2022-07-25 23:10:23 -03:00
|
|
|
PyObject **items = self->ob_item;
|
|
|
|
for (Py_ssize_t j = 0; j < input_size; j++) {
|
|
|
|
_Py_RefcntAdd(items[j], n-1);
|
2010-05-09 12:52:27 -03:00
|
|
|
}
|
2022-07-25 23:10:23 -03:00
|
|
|
_Py_memory_repeat((char *)items, sizeof(PyObject *)*output_size,
|
|
|
|
sizeof(PyObject *)*input_size);
|
|
|
|
|
2022-11-10 17:09:18 -04:00
|
|
|
return Py_NewRef(self);
|
2000-08-24 17:08:19 -03:00
|
|
|
}
|
|
|
|
|
1991-04-03 15:05:18 -04:00
|
|
|
static int
|
2024-01-02 09:41:32 -04:00
|
|
|
list_ass_item(PyObject *aa, Py_ssize_t i, PyObject *v)
|
1991-04-03 15:05:18 -04:00
|
|
|
{
|
2024-01-02 09:41:32 -04:00
|
|
|
PyListObject *a = (PyListObject *)aa;
|
2018-10-11 00:37:28 -03:00
|
|
|
if (!valid_index(i, Py_SIZE(a))) {
|
2010-05-09 12:52:27 -03:00
|
|
|
PyErr_SetString(PyExc_IndexError,
|
|
|
|
"list assignment index out of range");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (v == NULL)
|
|
|
|
return list_ass_slice(a, i, i+1, v);
|
2022-11-10 17:09:18 -04:00
|
|
|
Py_SETREF(a->ob_item[i], Py_NewRef(v));
|
2010-05-09 12:52:27 -03:00
|
|
|
return 0;
|
1991-04-03 15:05:18 -04:00
|
|
|
}
|
|
|
|
|
2017-03-11 03:19:20 -04:00
|
|
|
/*[clinic input]
|
2024-01-08 20:00:55 -04:00
|
|
|
@critical_section
|
2017-03-11 03:19:20 -04:00
|
|
|
list.insert
|
|
|
|
|
|
|
|
index: Py_ssize_t
|
|
|
|
object: object
|
|
|
|
/
|
|
|
|
|
|
|
|
Insert object before index.
|
|
|
|
[clinic start generated code]*/
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
static PyObject *
|
2017-03-11 03:19:20 -04:00
|
|
|
list_insert_impl(PyListObject *self, Py_ssize_t index, PyObject *object)
|
2024-01-08 20:00:55 -04:00
|
|
|
/*[clinic end generated code: output=7f35e32f60c8cb78 input=b1987ca998a4ae2d]*/
|
1990-10-14 09:07:46 -03:00
|
|
|
{
|
2024-01-18 19:03:28 -04:00
|
|
|
if (ins1(self, index, object) == 0) {
|
|
|
|
Py_RETURN_NONE;
|
2024-01-15 20:11:14 -04:00
|
|
|
}
|
2024-01-18 19:03:28 -04:00
|
|
|
return NULL;
|
1990-10-14 09:07:46 -03:00
|
|
|
}
|
|
|
|
|
2017-03-11 03:19:20 -04:00
|
|
|
/*[clinic input]
|
2024-01-08 20:00:55 -04:00
|
|
|
@critical_section
|
2023-11-13 12:14:56 -04:00
|
|
|
list.clear as py_list_clear
|
2017-03-11 03:19:20 -04:00
|
|
|
|
|
|
|
Remove all items from list.
|
|
|
|
[clinic start generated code]*/
|
|
|
|
|
2011-02-25 01:47:53 -04:00
|
|
|
static PyObject *
|
2023-11-13 12:14:56 -04:00
|
|
|
py_list_clear_impl(PyListObject *self)
|
2024-01-08 20:00:55 -04:00
|
|
|
/*[clinic end generated code: output=83726743807e3518 input=e285b7f09051a9ba]*/
|
2011-02-25 01:47:53 -04:00
|
|
|
{
|
2023-11-13 12:14:56 -04:00
|
|
|
list_clear(self);
|
2011-02-25 01:47:53 -04:00
|
|
|
Py_RETURN_NONE;
|
|
|
|
}
|
|
|
|
|
2017-03-11 03:19:20 -04:00
|
|
|
/*[clinic input]
|
2024-01-15 20:11:14 -04:00
|
|
|
@critical_section
|
2017-03-11 03:19:20 -04:00
|
|
|
list.copy
|
|
|
|
|
|
|
|
Return a shallow copy of the list.
|
|
|
|
[clinic start generated code]*/
|
|
|
|
|
2011-02-25 01:47:53 -04:00
|
|
|
static PyObject *
|
2017-03-11 03:19:20 -04:00
|
|
|
list_copy_impl(PyListObject *self)
|
2024-01-15 20:11:14 -04:00
|
|
|
/*[clinic end generated code: output=ec6b72d6209d418e input=81c54b0c7bb4f73d]*/
|
2011-02-25 01:47:53 -04:00
|
|
|
{
|
|
|
|
return list_slice(self, 0, Py_SIZE(self));
|
|
|
|
}
|
|
|
|
|
2017-03-11 03:19:20 -04:00
|
|
|
/*[clinic input]
|
2024-01-15 20:11:14 -04:00
|
|
|
@critical_section
|
2017-03-11 03:19:20 -04:00
|
|
|
list.append
|
|
|
|
|
|
|
|
object: object
|
|
|
|
/
|
|
|
|
|
|
|
|
Append object to the end of the list.
|
|
|
|
[clinic start generated code]*/
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
static PyObject *
|
2024-01-15 20:11:14 -04:00
|
|
|
list_append_impl(PyListObject *self, PyObject *object)
|
|
|
|
/*[clinic end generated code: output=78423561d92ed405 input=122b0853de54004f]*/
|
1990-10-14 09:07:46 -03:00
|
|
|
{
|
2022-04-01 07:23:42 -03:00
|
|
|
if (_PyList_AppendTakeRef(self, Py_NewRef(object)) < 0) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
Py_RETURN_NONE;
|
1990-10-14 09:07:46 -03:00
|
|
|
}
|
|
|
|
|
2023-11-13 12:14:56 -04:00
|
|
|
static int
|
|
|
|
list_extend_fast(PyListObject *self, PyObject *iterable)
|
2000-08-24 17:08:19 -03:00
|
|
|
{
|
2023-11-13 12:14:56 -04:00
|
|
|
Py_ssize_t n = PySequence_Fast_GET_SIZE(iterable);
|
|
|
|
if (n == 0) {
|
|
|
|
/* short circuit when iterable is empty */
|
|
|
|
return 0;
|
|
|
|
}
|
2010-05-09 12:52:27 -03:00
|
|
|
|
2023-11-13 12:14:56 -04:00
|
|
|
Py_ssize_t m = Py_SIZE(self);
|
|
|
|
// It should not be possible to allocate a list large enough to cause
|
|
|
|
// an overflow on any relevant platform.
|
|
|
|
assert(m < PY_SSIZE_T_MAX - n);
|
|
|
|
if (self->ob_item == NULL) {
|
|
|
|
if (list_preallocate_exact(self, n) < 0) {
|
|
|
|
return -1;
|
2010-05-09 12:52:27 -03:00
|
|
|
}
|
2023-11-13 12:14:56 -04:00
|
|
|
Py_SET_SIZE(self, n);
|
|
|
|
}
|
|
|
|
else if (list_resize(self, m + n) < 0) {
|
|
|
|
return -1;
|
2010-05-09 12:52:27 -03:00
|
|
|
}
|
|
|
|
|
2023-11-13 12:14:56 -04:00
|
|
|
// note that we may still have self == iterable here for the
|
|
|
|
// situation a.extend(a), but the following code works
|
|
|
|
// in that case too. Just make sure to resize self
|
|
|
|
// before calling PySequence_Fast_ITEMS.
|
|
|
|
//
|
|
|
|
// populate the end of self with iterable's items.
|
|
|
|
PyObject **src = PySequence_Fast_ITEMS(iterable);
|
|
|
|
PyObject **dest = self->ob_item + m;
|
|
|
|
for (Py_ssize_t i = 0; i < n; i++) {
|
|
|
|
PyObject *o = src[i];
|
|
|
|
dest[i] = Py_NewRef(o);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
list_extend_iter(PyListObject *self, PyObject *iterable)
|
|
|
|
{
|
|
|
|
PyObject *it = PyObject_GetIter(iterable);
|
|
|
|
if (it == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
PyObject *(*iternext)(PyObject *) = *Py_TYPE(it)->tp_iternext;
|
2010-05-09 12:52:27 -03:00
|
|
|
|
|
|
|
/* Guess a result list size. */
|
2023-11-13 12:14:56 -04:00
|
|
|
Py_ssize_t n = PyObject_LengthHint(iterable, 8);
|
2016-01-25 14:33:30 -04:00
|
|
|
if (n < 0) {
|
2010-05-09 12:52:27 -03:00
|
|
|
Py_DECREF(it);
|
2023-11-13 12:14:56 -04:00
|
|
|
return -1;
|
2010-05-09 12:52:27 -03:00
|
|
|
}
|
2023-11-13 12:14:56 -04:00
|
|
|
|
|
|
|
Py_ssize_t m = Py_SIZE(self);
|
2016-07-24 23:39:20 -03:00
|
|
|
if (m > PY_SSIZE_T_MAX - n) {
|
|
|
|
/* m + n overflowed; on the chance that n lied, and there really
|
|
|
|
* is enough room, ignore it. If n was telling the truth, we'll
|
|
|
|
* eventually run out of memory during the loop.
|
|
|
|
*/
|
|
|
|
}
|
2022-03-13 22:23:59 -03:00
|
|
|
else if (self->ob_item == NULL) {
|
|
|
|
if (n && list_preallocate_exact(self, n) < 0)
|
|
|
|
goto error;
|
|
|
|
}
|
2016-07-24 23:39:20 -03:00
|
|
|
else {
|
2010-05-09 12:52:27 -03:00
|
|
|
/* Make room. */
|
2023-11-13 12:14:56 -04:00
|
|
|
if (list_resize(self, m + n) < 0) {
|
2010-05-09 12:52:27 -03:00
|
|
|
goto error;
|
2023-11-13 12:14:56 -04:00
|
|
|
}
|
|
|
|
|
2010-05-09 12:52:27 -03:00
|
|
|
/* Make the list sane again. */
|
2020-02-07 18:18:08 -04:00
|
|
|
Py_SET_SIZE(self, m);
|
2010-05-09 12:52:27 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Run iterator to exhaustion. */
|
|
|
|
for (;;) {
|
|
|
|
PyObject *item = iternext(it);
|
|
|
|
if (item == NULL) {
|
|
|
|
if (PyErr_Occurred()) {
|
|
|
|
if (PyErr_ExceptionMatches(PyExc_StopIteration))
|
|
|
|
PyErr_Clear();
|
|
|
|
else
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2023-11-13 12:14:56 -04:00
|
|
|
|
2010-05-09 12:52:27 -03:00
|
|
|
if (Py_SIZE(self) < self->allocated) {
|
2023-06-27 22:45:57 -03:00
|
|
|
Py_ssize_t len = Py_SIZE(self);
|
2023-11-13 12:14:56 -04:00
|
|
|
PyList_SET_ITEM(self, len, item); // steals item ref
|
2023-11-03 08:02:39 -03:00
|
|
|
Py_SET_SIZE(self, len + 1);
|
2010-05-09 12:52:27 -03:00
|
|
|
}
|
|
|
|
else {
|
2022-04-01 07:23:42 -03:00
|
|
|
if (_PyList_AppendTakeRef(self, item) < 0)
|
2010-05-09 12:52:27 -03:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Cut back result list if initial guess was too large. */
|
2013-07-16 16:45:58 -03:00
|
|
|
if (Py_SIZE(self) < self->allocated) {
|
|
|
|
if (list_resize(self, Py_SIZE(self)) < 0)
|
|
|
|
goto error;
|
|
|
|
}
|
2010-05-09 12:52:27 -03:00
|
|
|
|
|
|
|
Py_DECREF(it);
|
2023-11-13 12:14:56 -04:00
|
|
|
return 0;
|
2004-02-14 23:57:00 -04:00
|
|
|
|
|
|
|
error:
|
2010-05-09 12:52:27 -03:00
|
|
|
Py_DECREF(it);
|
2023-11-13 12:14:56 -04:00
|
|
|
return -1;
|
2000-08-24 17:08:19 -03:00
|
|
|
}
|
1998-10-09 13:37:25 -03:00
|
|
|
|
2023-11-13 12:14:56 -04:00
|
|
|
|
|
|
|
static int
|
|
|
|
list_extend(PyListObject *self, PyObject *iterable)
|
|
|
|
{
|
|
|
|
// Special cases:
|
|
|
|
// 1) lists and tuples which can use PySequence_Fast ops
|
|
|
|
// 2) extending self to self requires making a copy first
|
|
|
|
if (PyList_CheckExact(iterable)
|
|
|
|
|| PyTuple_CheckExact(iterable)
|
|
|
|
|| (PyObject *)self == iterable)
|
|
|
|
{
|
|
|
|
iterable = PySequence_Fast(iterable, "argument must be iterable");
|
|
|
|
if (!iterable) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int res = list_extend_fast(self, iterable);
|
|
|
|
Py_DECREF(iterable);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return list_extend_iter(self, iterable);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-03-11 05:13:12 -04:00
|
|
|
PyObject *
|
2017-03-11 03:19:20 -04:00
|
|
|
_PyList_Extend(PyListObject *self, PyObject *iterable)
|
2004-03-11 05:13:12 -04:00
|
|
|
{
|
2023-11-13 12:14:56 -04:00
|
|
|
if (list_extend(self, iterable) < 0) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
Py_RETURN_NONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*[clinic input]
|
2024-01-15 20:11:14 -04:00
|
|
|
@critical_section self iterable
|
2023-11-13 12:14:56 -04:00
|
|
|
list.extend as py_list_extend
|
|
|
|
|
|
|
|
iterable: object
|
|
|
|
/
|
|
|
|
|
|
|
|
Extend list by appending elements from the iterable.
|
|
|
|
[clinic start generated code]*/
|
|
|
|
|
|
|
|
static PyObject *
|
2024-01-15 20:11:14 -04:00
|
|
|
py_list_extend_impl(PyListObject *self, PyObject *iterable)
|
|
|
|
/*[clinic end generated code: output=a2f115ceace2c845 input=1d42175414e1a5f3]*/
|
2023-11-13 12:14:56 -04:00
|
|
|
{
|
|
|
|
return _PyList_Extend(self, iterable);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
PyList_Extend(PyObject *self, PyObject *iterable)
|
|
|
|
{
|
|
|
|
if (!PyList_Check(self)) {
|
|
|
|
PyErr_BadInternalCall();
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return list_extend((PyListObject*)self, iterable);
|
2004-03-11 05:13:12 -04:00
|
|
|
}
|
|
|
|
|
2023-11-13 12:14:56 -04:00
|
|
|
|
|
|
|
int
|
|
|
|
PyList_Clear(PyObject *self)
|
|
|
|
{
|
|
|
|
if (!PyList_Check(self)) {
|
|
|
|
PyErr_BadInternalCall();
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
list_clear((PyListObject*)self);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-03-11 03:34:19 -04:00
|
|
|
static PyObject *
|
2024-01-02 09:41:32 -04:00
|
|
|
list_inplace_concat(PyObject *_self, PyObject *other)
|
2004-03-11 03:34:19 -04:00
|
|
|
{
|
2024-01-02 09:41:32 -04:00
|
|
|
PyListObject *self = (PyListObject *)_self;
|
2023-11-13 12:14:56 -04:00
|
|
|
if (list_extend(self, other) < 0) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2022-11-10 17:09:18 -04:00
|
|
|
return Py_NewRef(self);
|
2004-03-11 03:34:19 -04:00
|
|
|
}
|
|
|
|
|
2017-03-11 03:19:20 -04:00
|
|
|
/*[clinic input]
|
2024-01-08 20:00:55 -04:00
|
|
|
@critical_section
|
2017-03-11 03:19:20 -04:00
|
|
|
list.pop
|
|
|
|
|
|
|
|
index: Py_ssize_t = -1
|
|
|
|
/
|
|
|
|
|
|
|
|
Remove and return item at index (default last).
|
|
|
|
|
|
|
|
Raises IndexError if list is empty or index is out of range.
|
|
|
|
[clinic start generated code]*/
|
|
|
|
|
1998-06-30 12:36:32 -03:00
|
|
|
static PyObject *
|
2017-03-11 03:19:20 -04:00
|
|
|
list_pop_impl(PyListObject *self, Py_ssize_t index)
|
2024-01-08 20:00:55 -04:00
|
|
|
/*[clinic end generated code: output=6bd69dcb3f17eca8 input=c269141068ae4b8f]*/
|
1998-06-30 12:36:32 -03:00
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
PyObject *v;
|
|
|
|
int status;
|
|
|
|
|
|
|
|
if (Py_SIZE(self) == 0) {
|
|
|
|
/* Special-case most common failure cause */
|
|
|
|
PyErr_SetString(PyExc_IndexError, "pop from empty list");
|
|
|
|
return NULL;
|
|
|
|
}
|
2017-03-11 03:19:20 -04:00
|
|
|
if (index < 0)
|
|
|
|
index += Py_SIZE(self);
|
2018-10-11 00:37:28 -03:00
|
|
|
if (!valid_index(index, Py_SIZE(self))) {
|
2010-05-09 12:52:27 -03:00
|
|
|
PyErr_SetString(PyExc_IndexError, "pop index out of range");
|
|
|
|
return NULL;
|
|
|
|
}
|
2022-12-27 06:55:54 -04:00
|
|
|
|
|
|
|
PyObject **items = self->ob_item;
|
|
|
|
v = items[index];
|
|
|
|
const Py_ssize_t size_after_pop = Py_SIZE(self) - 1;
|
|
|
|
if (size_after_pop == 0) {
|
|
|
|
Py_INCREF(v);
|
2023-11-13 12:14:56 -04:00
|
|
|
list_clear(self);
|
|
|
|
status = 0;
|
2022-12-27 06:55:54 -04:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
if ((size_after_pop - index) > 0) {
|
|
|
|
memmove(&items[index], &items[index+1], (size_after_pop - index) * sizeof(PyObject *));
|
|
|
|
}
|
|
|
|
status = list_resize(self, size_after_pop);
|
2010-05-09 12:52:27 -03:00
|
|
|
}
|
2022-12-27 06:55:54 -04:00
|
|
|
if (status >= 0) {
|
|
|
|
return v; // and v now owns the reference the list had
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// list resize failed, need to restore
|
|
|
|
memmove(&items[index+1], &items[index], (size_after_pop - index)* sizeof(PyObject *));
|
|
|
|
items[index] = v;
|
2013-07-17 16:58:01 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
1998-06-30 12:36:32 -03:00
|
|
|
}
|
|
|
|
|
2002-07-18 23:33:08 -03:00
|
|
|
/* Reverse a slice of a list in place, from lo up to (exclusive) hi. */
|
|
|
|
static void
|
|
|
|
reverse_slice(PyObject **lo, PyObject **hi)
|
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
assert(lo && hi);
|
2002-07-18 23:33:08 -03:00
|
|
|
|
2010-05-09 12:52:27 -03:00
|
|
|
--hi;
|
|
|
|
while (lo < hi) {
|
|
|
|
PyObject *t = *lo;
|
|
|
|
*lo = *hi;
|
|
|
|
*hi = t;
|
|
|
|
++lo;
|
|
|
|
--hi;
|
|
|
|
}
|
2002-07-18 23:33:08 -03:00
|
|
|
}
|
|
|
|
|
2002-07-31 23:13:36 -03:00
|
|
|
/* Lots of code for an adaptive, stable, natural mergesort. There are many
|
|
|
|
* pieces to this algorithm; read listsort.txt for overviews and details.
|
|
|
|
*/
|
1996-12-10 19:55:39 -04:00
|
|
|
|
2010-12-02 17:55:33 -04:00
|
|
|
/* A sortslice contains a pointer to an array of keys and a pointer to
|
|
|
|
* an array of corresponding values. In other words, keys[i]
|
|
|
|
* corresponds with values[i]. If values == NULL, then the keys are
|
|
|
|
* also the values.
|
|
|
|
*
|
|
|
|
* Several convenience routines are provided here, so that keys and
|
|
|
|
* values are always moved in sync.
|
|
|
|
*/
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
PyObject **keys;
|
|
|
|
PyObject **values;
|
|
|
|
} sortslice;
|
|
|
|
|
|
|
|
Py_LOCAL_INLINE(void)
|
|
|
|
sortslice_copy(sortslice *s1, Py_ssize_t i, sortslice *s2, Py_ssize_t j)
|
|
|
|
{
|
|
|
|
s1->keys[i] = s2->keys[j];
|
|
|
|
if (s1->values != NULL)
|
|
|
|
s1->values[i] = s2->values[j];
|
|
|
|
}
|
|
|
|
|
|
|
|
Py_LOCAL_INLINE(void)
|
2010-12-02 21:44:10 -04:00
|
|
|
sortslice_copy_incr(sortslice *dst, sortslice *src)
|
|
|
|
{
|
2010-12-02 17:55:33 -04:00
|
|
|
*dst->keys++ = *src->keys++;
|
|
|
|
if (dst->values != NULL)
|
|
|
|
*dst->values++ = *src->values++;
|
|
|
|
}
|
|
|
|
|
|
|
|
Py_LOCAL_INLINE(void)
|
2010-12-02 21:44:10 -04:00
|
|
|
sortslice_copy_decr(sortslice *dst, sortslice *src)
|
|
|
|
{
|
2010-12-02 17:55:33 -04:00
|
|
|
*dst->keys-- = *src->keys--;
|
|
|
|
if (dst->values != NULL)
|
|
|
|
*dst->values-- = *src->values--;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Py_LOCAL_INLINE(void)
|
|
|
|
sortslice_memcpy(sortslice *s1, Py_ssize_t i, sortslice *s2, Py_ssize_t j,
|
2010-12-02 21:44:10 -04:00
|
|
|
Py_ssize_t n)
|
|
|
|
{
|
2010-12-02 17:55:33 -04:00
|
|
|
memcpy(&s1->keys[i], &s2->keys[j], sizeof(PyObject *) * n);
|
|
|
|
if (s1->values != NULL)
|
|
|
|
memcpy(&s1->values[i], &s2->values[j], sizeof(PyObject *) * n);
|
|
|
|
}
|
|
|
|
|
|
|
|
Py_LOCAL_INLINE(void)
|
|
|
|
sortslice_memmove(sortslice *s1, Py_ssize_t i, sortslice *s2, Py_ssize_t j,
|
2010-12-02 21:44:10 -04:00
|
|
|
Py_ssize_t n)
|
|
|
|
{
|
2010-12-02 17:55:33 -04:00
|
|
|
memmove(&s1->keys[i], &s2->keys[j], sizeof(PyObject *) * n);
|
|
|
|
if (s1->values != NULL)
|
|
|
|
memmove(&s1->values[i], &s2->values[j], sizeof(PyObject *) * n);
|
|
|
|
}
|
|
|
|
|
|
|
|
Py_LOCAL_INLINE(void)
|
2010-12-02 21:44:10 -04:00
|
|
|
sortslice_advance(sortslice *slice, Py_ssize_t n)
|
|
|
|
{
|
2010-12-02 17:55:33 -04:00
|
|
|
slice->keys += n;
|
|
|
|
if (slice->values != NULL)
|
|
|
|
slice->values += n;
|
|
|
|
}
|
|
|
|
|
2018-01-28 23:03:23 -04:00
|
|
|
/* Comparison function: ms->key_compare, which is set at run-time in
|
|
|
|
* listsort_impl to optimize for various special cases.
|
2002-07-31 23:13:36 -03:00
|
|
|
* Returns -1 on error, 1 if x < y, 0 if x >= y.
|
|
|
|
*/
|
1996-12-10 19:55:39 -04:00
|
|
|
|
2018-01-28 23:03:23 -04:00
|
|
|
#define ISLT(X, Y) (*(ms->key_compare))(X, Y, ms)
|
2002-08-04 14:47:26 -03:00
|
|
|
|
|
|
|
/* Compare X to Y via "<". Goto "fail" if the comparison raises an
|
2002-07-19 00:30:57 -03:00
|
|
|
error. Else "k" is set to true iff X<Y, and an "if (k)" block is
|
|
|
|
started. It makes more sense in context <wink>. X and Y are PyObject*s.
|
|
|
|
*/
|
2008-01-30 16:15:17 -04:00
|
|
|
#define IFLT(X, Y) if ((k = ISLT(X, Y)) < 0) goto fail; \
|
2010-05-09 12:52:27 -03:00
|
|
|
if (k)
|
1998-06-16 12:18:28 -03:00
|
|
|
|
2018-01-28 23:03:23 -04:00
|
|
|
/* The maximum number of entries in a MergeState's pending-runs stack.
|
2021-09-06 14:54:41 -03:00
|
|
|
* For a list with n elements, this needs at most floor(log2(n)) + 1 entries
|
|
|
|
* even if we didn't force runs to a minimal length. So the number of bits
|
|
|
|
* in a Py_ssize_t is plenty large enough for all cases.
|
2018-01-28 23:03:23 -04:00
|
|
|
*/
|
2021-09-06 14:54:41 -03:00
|
|
|
#define MAX_MERGE_PENDING (SIZEOF_SIZE_T * 8)
|
2018-01-28 23:03:23 -04:00
|
|
|
|
|
|
|
/* When we get into galloping mode, we stay there until both runs win less
|
|
|
|
* often than MIN_GALLOP consecutive times. See listsort.txt for more info.
|
|
|
|
*/
|
|
|
|
#define MIN_GALLOP 7
|
|
|
|
|
|
|
|
/* Avoid malloc for small temp arrays. */
|
|
|
|
#define MERGESTATE_TEMP_SIZE 256
|
|
|
|
|
|
|
|
/* One MergeState exists on the stack per invocation of mergesort. It's just
|
|
|
|
* a convenient way to pass state around among the helper functions.
|
|
|
|
*/
|
|
|
|
struct s_slice {
|
|
|
|
sortslice base;
|
2021-09-06 14:54:41 -03:00
|
|
|
Py_ssize_t len; /* length of run */
|
|
|
|
int power; /* node "level" for powersort merge strategy */
|
2018-01-28 23:03:23 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
typedef struct s_MergeState MergeState;
|
|
|
|
struct s_MergeState {
|
|
|
|
/* This controls when we get *into* galloping mode. It's initialized
|
|
|
|
* to MIN_GALLOP. merge_lo and merge_hi tend to nudge it higher for
|
|
|
|
* random data, and lower for highly structured data.
|
|
|
|
*/
|
|
|
|
Py_ssize_t min_gallop;
|
|
|
|
|
2021-09-06 14:54:41 -03:00
|
|
|
Py_ssize_t listlen; /* len(input_list) - read only */
|
|
|
|
PyObject **basekeys; /* base address of keys array - read only */
|
|
|
|
|
2018-01-28 23:03:23 -04:00
|
|
|
/* 'a' is temp storage to help with merges. It contains room for
|
|
|
|
* alloced entries.
|
|
|
|
*/
|
|
|
|
sortslice a; /* may point to temparray below */
|
|
|
|
Py_ssize_t alloced;
|
|
|
|
|
|
|
|
/* A stack of n pending runs yet to be merged. Run #i starts at
|
|
|
|
* address base[i] and extends for len[i] elements. It's always
|
|
|
|
* true (so long as the indices are in bounds) that
|
|
|
|
*
|
|
|
|
* pending[i].base + pending[i].len == pending[i+1].base
|
|
|
|
*
|
|
|
|
* so we could cut the storage for this, but it's a minor amount,
|
|
|
|
* and keeping all the info explicit simplifies the code.
|
|
|
|
*/
|
|
|
|
int n;
|
|
|
|
struct s_slice pending[MAX_MERGE_PENDING];
|
|
|
|
|
|
|
|
/* 'a' points to this when possible, rather than muck with malloc. */
|
|
|
|
PyObject *temparray[MERGESTATE_TEMP_SIZE];
|
|
|
|
|
|
|
|
/* This is the function we will use to compare two keys,
|
|
|
|
* even when none of our special cases apply and we have to use
|
|
|
|
* safe_object_compare. */
|
|
|
|
int (*key_compare)(PyObject *, PyObject *, MergeState *);
|
|
|
|
|
|
|
|
/* This function is used by unsafe_object_compare to optimize comparisons
|
|
|
|
* when we know our list is type-homogeneous but we can't assume anything else.
|
2020-02-06 22:04:21 -04:00
|
|
|
* In the pre-sort check it is set equal to Py_TYPE(key)->tp_richcompare */
|
2018-01-28 23:03:23 -04:00
|
|
|
PyObject *(*key_richcompare)(PyObject *, PyObject *, int);
|
|
|
|
|
|
|
|
/* This function is used by unsafe_tuple_compare to compare the first elements
|
|
|
|
* of tuples. It may be set to safe_object_compare, but the idea is that hopefully
|
|
|
|
* we can assume more, and use one of the special-case compares. */
|
|
|
|
int (*tuple_elem_compare)(PyObject *, PyObject *, MergeState *);
|
|
|
|
};
|
|
|
|
|
1998-06-16 12:18:28 -03:00
|
|
|
/* binarysort is the best method for sorting small arrays: it does
|
|
|
|
few compares, but can do data movement quadratic in the number of
|
|
|
|
elements.
|
1998-06-17 11:15:44 -03:00
|
|
|
[lo, hi) is a contiguous slice of a list, and is sorted via
|
2002-07-19 00:30:57 -03:00
|
|
|
binary insertion. This sort is stable.
|
1998-06-16 12:18:28 -03:00
|
|
|
On entry, must have lo <= start <= hi, and that [lo, start) is already
|
|
|
|
sorted (pass start == lo if you don't know!).
|
2002-07-19 00:30:57 -03:00
|
|
|
If islt() complains return -1, else 0.
|
1998-06-16 12:18:28 -03:00
|
|
|
Even in case of error, the output slice will be some permutation of
|
|
|
|
the input (nothing is lost or duplicated).
|
|
|
|
*/
|
|
|
|
static int
|
2018-01-28 23:03:23 -04:00
|
|
|
binarysort(MergeState *ms, sortslice lo, PyObject **hi, PyObject **start)
|
1998-06-16 12:18:28 -03:00
|
|
|
{
|
2013-08-13 15:18:52 -03:00
|
|
|
Py_ssize_t k;
|
|
|
|
PyObject **l, **p, **r;
|
|
|
|
PyObject *pivot;
|
2010-05-09 12:52:27 -03:00
|
|
|
|
2010-12-02 17:55:33 -04:00
|
|
|
assert(lo.keys <= start && start <= hi);
|
2010-05-09 12:52:27 -03:00
|
|
|
/* assert [lo, start) is sorted */
|
2010-12-02 17:55:33 -04:00
|
|
|
if (lo.keys == start)
|
2010-05-09 12:52:27 -03:00
|
|
|
++start;
|
|
|
|
for (; start < hi; ++start) {
|
|
|
|
/* set l to where *start belongs */
|
2010-12-02 17:55:33 -04:00
|
|
|
l = lo.keys;
|
2010-05-09 12:52:27 -03:00
|
|
|
r = start;
|
|
|
|
pivot = *r;
|
|
|
|
/* Invariants:
|
|
|
|
* pivot >= all in [lo, l).
|
|
|
|
* pivot < all in [r, start).
|
|
|
|
* The second is vacuously true at the start.
|
|
|
|
*/
|
|
|
|
assert(l < r);
|
|
|
|
do {
|
|
|
|
p = l + ((r - l) >> 1);
|
|
|
|
IFLT(pivot, *p)
|
|
|
|
r = p;
|
|
|
|
else
|
|
|
|
l = p+1;
|
|
|
|
} while (l < r);
|
|
|
|
assert(l == r);
|
|
|
|
/* The invariants still hold, so pivot >= all in [lo, l) and
|
|
|
|
pivot < all in [l, start), so pivot belongs at l. Note
|
|
|
|
that if there are elements equal to pivot, l points to the
|
|
|
|
first slot after them -- that's why this sort is stable.
|
|
|
|
Slide over to make room.
|
|
|
|
Caution: using memmove is much slower under MSVC 5;
|
|
|
|
we're not usually moving many slots. */
|
|
|
|
for (p = start; p > l; --p)
|
|
|
|
*p = *(p-1);
|
|
|
|
*l = pivot;
|
2010-12-02 17:55:33 -04:00
|
|
|
if (lo.values != NULL) {
|
|
|
|
Py_ssize_t offset = lo.values - lo.keys;
|
|
|
|
p = start + offset;
|
|
|
|
pivot = *p;
|
|
|
|
l += offset;
|
|
|
|
for (p = start + offset; p > l; --p)
|
|
|
|
*p = *(p-1);
|
|
|
|
*l = pivot;
|
|
|
|
}
|
2010-05-09 12:52:27 -03:00
|
|
|
}
|
|
|
|
return 0;
|
1996-12-10 19:55:39 -04:00
|
|
|
|
1998-06-16 12:18:28 -03:00
|
|
|
fail:
|
2010-05-09 12:52:27 -03:00
|
|
|
return -1;
|
1998-06-16 12:18:28 -03:00
|
|
|
}
|
|
|
|
|
2002-07-31 23:13:36 -03:00
|
|
|
/*
|
|
|
|
Return the length of the run beginning at lo, in the slice [lo, hi). lo < hi
|
|
|
|
is required on entry. "A run" is the longest ascending sequence, with
|
1998-06-16 12:18:28 -03:00
|
|
|
|
2002-07-31 23:13:36 -03:00
|
|
|
lo[0] <= lo[1] <= lo[2] <= ...
|
1998-06-16 12:18:28 -03:00
|
|
|
|
2002-07-31 23:13:36 -03:00
|
|
|
or the longest descending sequence, with
|
1998-06-16 12:18:28 -03:00
|
|
|
|
2002-07-31 23:13:36 -03:00
|
|
|
lo[0] > lo[1] > lo[2] > ...
|
|
|
|
|
|
|
|
Boolean *descending is set to 0 in the former case, or to 1 in the latter.
|
|
|
|
For its intended use in a stable mergesort, the strictness of the defn of
|
|
|
|
"descending" is needed so that the caller can safely reverse a descending
|
|
|
|
sequence without violating stability (strict > ensures there are no equal
|
|
|
|
elements to get out of order).
|
1996-12-10 19:55:39 -04:00
|
|
|
|
2002-07-31 23:13:36 -03:00
|
|
|
Returns -1 in case of error.
|
|
|
|
*/
|
2006-02-15 13:27:45 -04:00
|
|
|
static Py_ssize_t
|
2018-01-28 23:03:23 -04:00
|
|
|
count_run(MergeState *ms, PyObject **lo, PyObject **hi, int *descending)
|
1996-12-10 19:55:39 -04:00
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
Py_ssize_t k;
|
|
|
|
Py_ssize_t n;
|
|
|
|
|
|
|
|
assert(lo < hi);
|
|
|
|
*descending = 0;
|
|
|
|
++lo;
|
|
|
|
if (lo == hi)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
n = 2;
|
|
|
|
IFLT(*lo, *(lo-1)) {
|
|
|
|
*descending = 1;
|
|
|
|
for (lo = lo+1; lo < hi; ++lo, ++n) {
|
|
|
|
IFLT(*lo, *(lo-1))
|
|
|
|
;
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
for (lo = lo+1; lo < hi; ++lo, ++n) {
|
|
|
|
IFLT(*lo, *(lo-1))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return n;
|
2002-07-31 23:13:36 -03:00
|
|
|
fail:
|
2010-05-09 12:52:27 -03:00
|
|
|
return -1;
|
2002-07-31 23:13:36 -03:00
|
|
|
}
|
1998-06-16 12:18:28 -03:00
|
|
|
|
2002-07-31 23:13:36 -03:00
|
|
|
/*
|
|
|
|
Locate the proper position of key in a sorted vector; if the vector contains
|
|
|
|
an element equal to key, return the position immediately to the left of
|
|
|
|
the leftmost equal element. [gallop_right() does the same except returns
|
|
|
|
the position to the right of the rightmost equal element (if any).]
|
1998-06-16 12:18:28 -03:00
|
|
|
|
2002-07-31 23:13:36 -03:00
|
|
|
"a" is a sorted vector with n elements, starting at a[0]. n must be > 0.
|
1998-05-26 12:06:32 -03:00
|
|
|
|
2002-07-31 23:13:36 -03:00
|
|
|
"hint" is an index at which to begin the search, 0 <= hint < n. The closer
|
|
|
|
hint is to the final result, the faster this runs.
|
|
|
|
|
|
|
|
The return value is the int k in 0..n such that
|
|
|
|
|
|
|
|
a[k-1] < key <= a[k]
|
|
|
|
|
|
|
|
pretending that *(a-1) is minus infinity and a[n] is plus infinity. IOW,
|
|
|
|
key belongs at index k; or, IOW, the first k elements of a should precede
|
|
|
|
key, and the last n-k should follow key.
|
|
|
|
|
|
|
|
Returns -1 on error. See listsort.txt for info on the method.
|
|
|
|
*/
|
2006-02-15 13:27:45 -04:00
|
|
|
static Py_ssize_t
|
2018-01-28 23:03:23 -04:00
|
|
|
gallop_left(MergeState *ms, PyObject *key, PyObject **a, Py_ssize_t n, Py_ssize_t hint)
|
2002-07-31 23:13:36 -03:00
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
Py_ssize_t ofs;
|
|
|
|
Py_ssize_t lastofs;
|
|
|
|
Py_ssize_t k;
|
|
|
|
|
|
|
|
assert(key && a && n > 0 && hint >= 0 && hint < n);
|
|
|
|
|
|
|
|
a += hint;
|
|
|
|
lastofs = 0;
|
|
|
|
ofs = 1;
|
|
|
|
IFLT(*a, key) {
|
|
|
|
/* a[hint] < key -- gallop right, until
|
|
|
|
* a[hint + lastofs] < key <= a[hint + ofs]
|
|
|
|
*/
|
|
|
|
const Py_ssize_t maxofs = n - hint; /* &a[n-1] is highest */
|
|
|
|
while (ofs < maxofs) {
|
|
|
|
IFLT(a[ofs], key) {
|
|
|
|
lastofs = ofs;
|
2019-05-22 21:01:08 -03:00
|
|
|
assert(ofs <= (PY_SSIZE_T_MAX - 1) / 2);
|
2010-05-09 12:52:27 -03:00
|
|
|
ofs = (ofs << 1) + 1;
|
|
|
|
}
|
|
|
|
else /* key <= a[hint + ofs] */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (ofs > maxofs)
|
|
|
|
ofs = maxofs;
|
|
|
|
/* Translate back to offsets relative to &a[0]. */
|
|
|
|
lastofs += hint;
|
|
|
|
ofs += hint;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* key <= a[hint] -- gallop left, until
|
|
|
|
* a[hint - ofs] < key <= a[hint - lastofs]
|
|
|
|
*/
|
|
|
|
const Py_ssize_t maxofs = hint + 1; /* &a[0] is lowest */
|
|
|
|
while (ofs < maxofs) {
|
|
|
|
IFLT(*(a-ofs), key)
|
|
|
|
break;
|
|
|
|
/* key <= a[hint - ofs] */
|
|
|
|
lastofs = ofs;
|
2019-05-22 21:01:08 -03:00
|
|
|
assert(ofs <= (PY_SSIZE_T_MAX - 1) / 2);
|
2010-05-09 12:52:27 -03:00
|
|
|
ofs = (ofs << 1) + 1;
|
|
|
|
}
|
|
|
|
if (ofs > maxofs)
|
|
|
|
ofs = maxofs;
|
|
|
|
/* Translate back to positive offsets relative to &a[0]. */
|
|
|
|
k = lastofs;
|
|
|
|
lastofs = hint - ofs;
|
|
|
|
ofs = hint - k;
|
|
|
|
}
|
|
|
|
a -= hint;
|
|
|
|
|
|
|
|
assert(-1 <= lastofs && lastofs < ofs && ofs <= n);
|
|
|
|
/* Now a[lastofs] < key <= a[ofs], so key belongs somewhere to the
|
|
|
|
* right of lastofs but no farther right than ofs. Do a binary
|
|
|
|
* search, with invariant a[lastofs-1] < key <= a[ofs].
|
|
|
|
*/
|
|
|
|
++lastofs;
|
|
|
|
while (lastofs < ofs) {
|
|
|
|
Py_ssize_t m = lastofs + ((ofs - lastofs) >> 1);
|
|
|
|
|
|
|
|
IFLT(a[m], key)
|
|
|
|
lastofs = m+1; /* a[m] < key */
|
|
|
|
else
|
|
|
|
ofs = m; /* key <= a[m] */
|
|
|
|
}
|
|
|
|
assert(lastofs == ofs); /* so a[ofs-1] < key <= a[ofs] */
|
|
|
|
return ofs;
|
1996-12-10 19:55:39 -04:00
|
|
|
|
2002-07-31 23:13:36 -03:00
|
|
|
fail:
|
2010-05-09 12:52:27 -03:00
|
|
|
return -1;
|
2002-07-31 23:13:36 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Exactly like gallop_left(), except that if key already exists in a[0:n],
|
|
|
|
finds the position immediately to the right of the rightmost equal value.
|
|
|
|
|
|
|
|
The return value is the int k in 0..n such that
|
|
|
|
|
|
|
|
a[k-1] <= key < a[k]
|
|
|
|
|
|
|
|
or -1 if error.
|
|
|
|
|
|
|
|
The code duplication is massive, but this is enough different given that
|
|
|
|
we're sticking to "<" comparisons that it's much harder to follow if
|
|
|
|
written as one routine with yet another "left or right?" flag.
|
|
|
|
*/
|
2006-02-15 13:27:45 -04:00
|
|
|
static Py_ssize_t
|
2018-01-28 23:03:23 -04:00
|
|
|
gallop_right(MergeState *ms, PyObject *key, PyObject **a, Py_ssize_t n, Py_ssize_t hint)
|
2002-07-31 23:13:36 -03:00
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
Py_ssize_t ofs;
|
|
|
|
Py_ssize_t lastofs;
|
|
|
|
Py_ssize_t k;
|
|
|
|
|
|
|
|
assert(key && a && n > 0 && hint >= 0 && hint < n);
|
|
|
|
|
|
|
|
a += hint;
|
|
|
|
lastofs = 0;
|
|
|
|
ofs = 1;
|
|
|
|
IFLT(key, *a) {
|
|
|
|
/* key < a[hint] -- gallop left, until
|
|
|
|
* a[hint - ofs] <= key < a[hint - lastofs]
|
|
|
|
*/
|
|
|
|
const Py_ssize_t maxofs = hint + 1; /* &a[0] is lowest */
|
|
|
|
while (ofs < maxofs) {
|
|
|
|
IFLT(key, *(a-ofs)) {
|
|
|
|
lastofs = ofs;
|
2019-05-22 21:01:08 -03:00
|
|
|
assert(ofs <= (PY_SSIZE_T_MAX - 1) / 2);
|
2010-05-09 12:52:27 -03:00
|
|
|
ofs = (ofs << 1) + 1;
|
|
|
|
}
|
|
|
|
else /* a[hint - ofs] <= key */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (ofs > maxofs)
|
|
|
|
ofs = maxofs;
|
|
|
|
/* Translate back to positive offsets relative to &a[0]. */
|
|
|
|
k = lastofs;
|
|
|
|
lastofs = hint - ofs;
|
|
|
|
ofs = hint - k;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* a[hint] <= key -- gallop right, until
|
|
|
|
* a[hint + lastofs] <= key < a[hint + ofs]
|
|
|
|
*/
|
|
|
|
const Py_ssize_t maxofs = n - hint; /* &a[n-1] is highest */
|
|
|
|
while (ofs < maxofs) {
|
|
|
|
IFLT(key, a[ofs])
|
|
|
|
break;
|
|
|
|
/* a[hint + ofs] <= key */
|
|
|
|
lastofs = ofs;
|
2019-05-22 21:01:08 -03:00
|
|
|
assert(ofs <= (PY_SSIZE_T_MAX - 1) / 2);
|
2010-05-09 12:52:27 -03:00
|
|
|
ofs = (ofs << 1) + 1;
|
|
|
|
}
|
|
|
|
if (ofs > maxofs)
|
|
|
|
ofs = maxofs;
|
|
|
|
/* Translate back to offsets relative to &a[0]. */
|
|
|
|
lastofs += hint;
|
|
|
|
ofs += hint;
|
|
|
|
}
|
|
|
|
a -= hint;
|
|
|
|
|
|
|
|
assert(-1 <= lastofs && lastofs < ofs && ofs <= n);
|
|
|
|
/* Now a[lastofs] <= key < a[ofs], so key belongs somewhere to the
|
|
|
|
* right of lastofs but no farther right than ofs. Do a binary
|
|
|
|
* search, with invariant a[lastofs-1] <= key < a[ofs].
|
|
|
|
*/
|
|
|
|
++lastofs;
|
|
|
|
while (lastofs < ofs) {
|
|
|
|
Py_ssize_t m = lastofs + ((ofs - lastofs) >> 1);
|
|
|
|
|
|
|
|
IFLT(key, a[m])
|
|
|
|
ofs = m; /* key < a[m] */
|
|
|
|
else
|
|
|
|
lastofs = m+1; /* a[m] <= key */
|
|
|
|
}
|
|
|
|
assert(lastofs == ofs); /* so a[ofs-1] <= key < a[ofs] */
|
|
|
|
return ofs;
|
2002-07-31 23:13:36 -03:00
|
|
|
|
|
|
|
fail:
|
2010-05-09 12:52:27 -03:00
|
|
|
return -1;
|
2002-07-31 23:13:36 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Conceptually a MergeState's constructor. */
|
|
|
|
static void
|
2021-09-06 14:54:41 -03:00
|
|
|
merge_init(MergeState *ms, Py_ssize_t list_size, int has_keyfunc,
|
|
|
|
sortslice *lo)
|
2002-07-31 23:13:36 -03:00
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
assert(ms != NULL);
|
2010-12-02 17:55:33 -04:00
|
|
|
if (has_keyfunc) {
|
|
|
|
/* The temporary space for merging will need at most half the list
|
|
|
|
* size rounded up. Use the minimum possible space so we can use the
|
|
|
|
* rest of temparray for other things. In particular, if there is
|
|
|
|
* enough extra space, listsort() will use it to store the keys.
|
|
|
|
*/
|
|
|
|
ms->alloced = (list_size + 1) / 2;
|
|
|
|
|
|
|
|
/* ms->alloced describes how many keys will be stored at
|
|
|
|
ms->temparray, but we also need to store the values. Hence,
|
|
|
|
ms->alloced is capped at half of MERGESTATE_TEMP_SIZE. */
|
|
|
|
if (MERGESTATE_TEMP_SIZE / 2 < ms->alloced)
|
|
|
|
ms->alloced = MERGESTATE_TEMP_SIZE / 2;
|
|
|
|
ms->a.values = &ms->temparray[ms->alloced];
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
ms->alloced = MERGESTATE_TEMP_SIZE;
|
|
|
|
ms->a.values = NULL;
|
|
|
|
}
|
|
|
|
ms->a.keys = ms->temparray;
|
2010-05-09 12:52:27 -03:00
|
|
|
ms->n = 0;
|
|
|
|
ms->min_gallop = MIN_GALLOP;
|
2021-09-06 14:54:41 -03:00
|
|
|
ms->listlen = list_size;
|
|
|
|
ms->basekeys = lo->keys;
|
2002-07-31 23:13:36 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Free all the temp memory owned by the MergeState. This must be called
|
|
|
|
* when you're done with a MergeState, and may be called before then if
|
|
|
|
* you want to free the temp memory early.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
merge_freemem(MergeState *ms)
|
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
assert(ms != NULL);
|
2022-05-06 01:14:09 -03:00
|
|
|
if (ms->a.keys != ms->temparray) {
|
2010-12-02 17:55:33 -04:00
|
|
|
PyMem_Free(ms->a.keys);
|
2022-05-06 01:14:09 -03:00
|
|
|
ms->a.keys = NULL;
|
|
|
|
}
|
2002-07-31 23:13:36 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Ensure enough temp memory for 'need' array slots is available.
|
|
|
|
* Returns 0 on success and -1 if the memory can't be gotten.
|
|
|
|
*/
|
|
|
|
static int
|
2006-02-15 13:27:45 -04:00
|
|
|
merge_getmem(MergeState *ms, Py_ssize_t need)
|
2002-07-31 23:13:36 -03:00
|
|
|
{
|
2010-12-02 17:55:33 -04:00
|
|
|
int multiplier;
|
|
|
|
|
2010-05-09 12:52:27 -03:00
|
|
|
assert(ms != NULL);
|
|
|
|
if (need <= ms->alloced)
|
|
|
|
return 0;
|
2010-12-02 17:55:33 -04:00
|
|
|
|
|
|
|
multiplier = ms->a.values != NULL ? 2 : 1;
|
|
|
|
|
2010-05-09 12:52:27 -03:00
|
|
|
/* Don't realloc! That can cost cycles to copy the old data, but
|
|
|
|
* we don't care what's in the block.
|
|
|
|
*/
|
|
|
|
merge_freemem(ms);
|
2018-01-28 23:03:23 -04:00
|
|
|
if ((size_t)need > PY_SSIZE_T_MAX / sizeof(PyObject *) / multiplier) {
|
2010-05-09 12:52:27 -03:00
|
|
|
PyErr_NoMemory();
|
|
|
|
return -1;
|
|
|
|
}
|
2018-01-28 23:03:23 -04:00
|
|
|
ms->a.keys = (PyObject **)PyMem_Malloc(multiplier * need
|
2010-12-02 17:55:33 -04:00
|
|
|
* sizeof(PyObject *));
|
|
|
|
if (ms->a.keys != NULL) {
|
2010-05-09 12:52:27 -03:00
|
|
|
ms->alloced = need;
|
2010-12-02 17:55:33 -04:00
|
|
|
if (ms->a.values != NULL)
|
|
|
|
ms->a.values = &ms->a.keys[need];
|
2010-05-09 12:52:27 -03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
PyErr_NoMemory();
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
#define MERGE_GETMEM(MS, NEED) ((NEED) <= (MS)->alloced ? 0 : \
|
|
|
|
merge_getmem(MS, NEED))
|
2002-07-31 23:13:36 -03:00
|
|
|
|
2010-12-02 17:55:33 -04:00
|
|
|
/* Merge the na elements starting at ssa with the nb elements starting at
|
|
|
|
* ssb.keys = ssa.keys + na in a stable way, in-place. na and nb must be > 0.
|
|
|
|
* Must also have that ssa.keys[na-1] belongs at the end of the merge, and
|
|
|
|
* should have na <= nb. See listsort.txt for more info. Return 0 if
|
|
|
|
* successful, -1 if error.
|
2002-07-31 23:13:36 -03:00
|
|
|
*/
|
2006-02-15 13:27:45 -04:00
|
|
|
static Py_ssize_t
|
2010-12-02 17:55:33 -04:00
|
|
|
merge_lo(MergeState *ms, sortslice ssa, Py_ssize_t na,
|
|
|
|
sortslice ssb, Py_ssize_t nb)
|
2002-07-31 23:13:36 -03:00
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
Py_ssize_t k;
|
2010-12-02 17:55:33 -04:00
|
|
|
sortslice dest;
|
2010-05-09 12:52:27 -03:00
|
|
|
int result = -1; /* guilty until proved innocent */
|
|
|
|
Py_ssize_t min_gallop;
|
|
|
|
|
2010-12-02 17:55:33 -04:00
|
|
|
assert(ms && ssa.keys && ssb.keys && na > 0 && nb > 0);
|
|
|
|
assert(ssa.keys + na == ssb.keys);
|
2010-05-09 12:52:27 -03:00
|
|
|
if (MERGE_GETMEM(ms, na) < 0)
|
|
|
|
return -1;
|
2010-12-02 17:55:33 -04:00
|
|
|
sortslice_memcpy(&ms->a, 0, &ssa, 0, na);
|
|
|
|
dest = ssa;
|
|
|
|
ssa = ms->a;
|
2010-05-09 12:52:27 -03:00
|
|
|
|
2010-12-02 17:55:33 -04:00
|
|
|
sortslice_copy_incr(&dest, &ssb);
|
2010-05-09 12:52:27 -03:00
|
|
|
--nb;
|
|
|
|
if (nb == 0)
|
|
|
|
goto Succeed;
|
|
|
|
if (na == 1)
|
|
|
|
goto CopyB;
|
|
|
|
|
|
|
|
min_gallop = ms->min_gallop;
|
|
|
|
for (;;) {
|
|
|
|
Py_ssize_t acount = 0; /* # of times A won in a row */
|
|
|
|
Py_ssize_t bcount = 0; /* # of times B won in a row */
|
|
|
|
|
|
|
|
/* Do the straightforward thing until (if ever) one run
|
|
|
|
* appears to win consistently.
|
|
|
|
*/
|
|
|
|
for (;;) {
|
|
|
|
assert(na > 1 && nb > 0);
|
2010-12-02 17:55:33 -04:00
|
|
|
k = ISLT(ssb.keys[0], ssa.keys[0]);
|
2010-05-09 12:52:27 -03:00
|
|
|
if (k) {
|
|
|
|
if (k < 0)
|
|
|
|
goto Fail;
|
2010-12-02 17:55:33 -04:00
|
|
|
sortslice_copy_incr(&dest, &ssb);
|
2010-05-09 12:52:27 -03:00
|
|
|
++bcount;
|
|
|
|
acount = 0;
|
|
|
|
--nb;
|
|
|
|
if (nb == 0)
|
|
|
|
goto Succeed;
|
|
|
|
if (bcount >= min_gallop)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else {
|
2010-12-02 17:55:33 -04:00
|
|
|
sortslice_copy_incr(&dest, &ssa);
|
2010-05-09 12:52:27 -03:00
|
|
|
++acount;
|
|
|
|
bcount = 0;
|
|
|
|
--na;
|
|
|
|
if (na == 1)
|
|
|
|
goto CopyB;
|
|
|
|
if (acount >= min_gallop)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* One run is winning so consistently that galloping may
|
|
|
|
* be a huge win. So try that, and continue galloping until
|
|
|
|
* (if ever) neither run appears to be winning consistently
|
|
|
|
* anymore.
|
|
|
|
*/
|
|
|
|
++min_gallop;
|
|
|
|
do {
|
|
|
|
assert(na > 1 && nb > 0);
|
|
|
|
min_gallop -= min_gallop > 1;
|
|
|
|
ms->min_gallop = min_gallop;
|
2018-01-28 23:03:23 -04:00
|
|
|
k = gallop_right(ms, ssb.keys[0], ssa.keys, na, 0);
|
2010-05-09 12:52:27 -03:00
|
|
|
acount = k;
|
|
|
|
if (k) {
|
|
|
|
if (k < 0)
|
|
|
|
goto Fail;
|
2010-12-02 17:55:33 -04:00
|
|
|
sortslice_memcpy(&dest, 0, &ssa, 0, k);
|
|
|
|
sortslice_advance(&dest, k);
|
|
|
|
sortslice_advance(&ssa, k);
|
2010-05-09 12:52:27 -03:00
|
|
|
na -= k;
|
|
|
|
if (na == 1)
|
|
|
|
goto CopyB;
|
|
|
|
/* na==0 is impossible now if the comparison
|
|
|
|
* function is consistent, but we can't assume
|
|
|
|
* that it is.
|
|
|
|
*/
|
|
|
|
if (na == 0)
|
|
|
|
goto Succeed;
|
|
|
|
}
|
2010-12-02 17:55:33 -04:00
|
|
|
sortslice_copy_incr(&dest, &ssb);
|
2010-05-09 12:52:27 -03:00
|
|
|
--nb;
|
|
|
|
if (nb == 0)
|
|
|
|
goto Succeed;
|
|
|
|
|
2018-01-28 23:03:23 -04:00
|
|
|
k = gallop_left(ms, ssa.keys[0], ssb.keys, nb, 0);
|
2010-05-09 12:52:27 -03:00
|
|
|
bcount = k;
|
|
|
|
if (k) {
|
|
|
|
if (k < 0)
|
|
|
|
goto Fail;
|
2010-12-02 17:55:33 -04:00
|
|
|
sortslice_memmove(&dest, 0, &ssb, 0, k);
|
|
|
|
sortslice_advance(&dest, k);
|
|
|
|
sortslice_advance(&ssb, k);
|
2010-05-09 12:52:27 -03:00
|
|
|
nb -= k;
|
|
|
|
if (nb == 0)
|
|
|
|
goto Succeed;
|
|
|
|
}
|
2010-12-02 17:55:33 -04:00
|
|
|
sortslice_copy_incr(&dest, &ssa);
|
2010-05-09 12:52:27 -03:00
|
|
|
--na;
|
|
|
|
if (na == 1)
|
|
|
|
goto CopyB;
|
|
|
|
} while (acount >= MIN_GALLOP || bcount >= MIN_GALLOP);
|
|
|
|
++min_gallop; /* penalize it for leaving galloping mode */
|
|
|
|
ms->min_gallop = min_gallop;
|
|
|
|
}
|
2002-07-31 23:13:36 -03:00
|
|
|
Succeed:
|
2010-05-09 12:52:27 -03:00
|
|
|
result = 0;
|
2002-07-31 23:13:36 -03:00
|
|
|
Fail:
|
2010-05-09 12:52:27 -03:00
|
|
|
if (na)
|
2010-12-02 17:55:33 -04:00
|
|
|
sortslice_memcpy(&dest, 0, &ssa, 0, na);
|
2010-05-09 12:52:27 -03:00
|
|
|
return result;
|
2002-07-31 23:13:36 -03:00
|
|
|
CopyB:
|
2010-05-09 12:52:27 -03:00
|
|
|
assert(na == 1 && nb > 0);
|
2010-12-02 17:55:33 -04:00
|
|
|
/* The last element of ssa belongs at the end of the merge. */
|
|
|
|
sortslice_memmove(&dest, 0, &ssb, 0, nb);
|
|
|
|
sortslice_copy(&dest, nb, &ssa, 0);
|
2010-05-09 12:52:27 -03:00
|
|
|
return 0;
|
2002-07-31 23:13:36 -03:00
|
|
|
}
|
|
|
|
|
2010-12-02 17:55:33 -04:00
|
|
|
/* Merge the na elements starting at pa with the nb elements starting at
|
|
|
|
* ssb.keys = ssa.keys + na in a stable way, in-place. na and nb must be > 0.
|
|
|
|
* Must also have that ssa.keys[na-1] belongs at the end of the merge, and
|
|
|
|
* should have na >= nb. See listsort.txt for more info. Return 0 if
|
|
|
|
* successful, -1 if error.
|
2002-07-31 23:13:36 -03:00
|
|
|
*/
|
2006-02-15 13:27:45 -04:00
|
|
|
static Py_ssize_t
|
2010-12-02 17:55:33 -04:00
|
|
|
merge_hi(MergeState *ms, sortslice ssa, Py_ssize_t na,
|
|
|
|
sortslice ssb, Py_ssize_t nb)
|
2002-07-31 23:13:36 -03:00
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
Py_ssize_t k;
|
2010-12-02 17:55:33 -04:00
|
|
|
sortslice dest, basea, baseb;
|
2010-05-09 12:52:27 -03:00
|
|
|
int result = -1; /* guilty until proved innocent */
|
|
|
|
Py_ssize_t min_gallop;
|
|
|
|
|
2010-12-02 17:55:33 -04:00
|
|
|
assert(ms && ssa.keys && ssb.keys && na > 0 && nb > 0);
|
|
|
|
assert(ssa.keys + na == ssb.keys);
|
2010-05-09 12:52:27 -03:00
|
|
|
if (MERGE_GETMEM(ms, nb) < 0)
|
|
|
|
return -1;
|
2010-12-02 17:55:33 -04:00
|
|
|
dest = ssb;
|
|
|
|
sortslice_advance(&dest, nb-1);
|
|
|
|
sortslice_memcpy(&ms->a, 0, &ssb, 0, nb);
|
|
|
|
basea = ssa;
|
2010-05-09 12:52:27 -03:00
|
|
|
baseb = ms->a;
|
2010-12-02 17:55:33 -04:00
|
|
|
ssb.keys = ms->a.keys + nb - 1;
|
|
|
|
if (ssb.values != NULL)
|
|
|
|
ssb.values = ms->a.values + nb - 1;
|
|
|
|
sortslice_advance(&ssa, na - 1);
|
2010-05-09 12:52:27 -03:00
|
|
|
|
2010-12-02 17:55:33 -04:00
|
|
|
sortslice_copy_decr(&dest, &ssa);
|
2010-05-09 12:52:27 -03:00
|
|
|
--na;
|
|
|
|
if (na == 0)
|
|
|
|
goto Succeed;
|
|
|
|
if (nb == 1)
|
|
|
|
goto CopyA;
|
|
|
|
|
|
|
|
min_gallop = ms->min_gallop;
|
|
|
|
for (;;) {
|
|
|
|
Py_ssize_t acount = 0; /* # of times A won in a row */
|
|
|
|
Py_ssize_t bcount = 0; /* # of times B won in a row */
|
|
|
|
|
|
|
|
/* Do the straightforward thing until (if ever) one run
|
|
|
|
* appears to win consistently.
|
|
|
|
*/
|
|
|
|
for (;;) {
|
|
|
|
assert(na > 0 && nb > 1);
|
2010-12-02 17:55:33 -04:00
|
|
|
k = ISLT(ssb.keys[0], ssa.keys[0]);
|
2010-05-09 12:52:27 -03:00
|
|
|
if (k) {
|
|
|
|
if (k < 0)
|
|
|
|
goto Fail;
|
2010-12-02 17:55:33 -04:00
|
|
|
sortslice_copy_decr(&dest, &ssa);
|
2010-05-09 12:52:27 -03:00
|
|
|
++acount;
|
|
|
|
bcount = 0;
|
|
|
|
--na;
|
|
|
|
if (na == 0)
|
|
|
|
goto Succeed;
|
|
|
|
if (acount >= min_gallop)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else {
|
2010-12-02 17:55:33 -04:00
|
|
|
sortslice_copy_decr(&dest, &ssb);
|
2010-05-09 12:52:27 -03:00
|
|
|
++bcount;
|
|
|
|
acount = 0;
|
|
|
|
--nb;
|
|
|
|
if (nb == 1)
|
|
|
|
goto CopyA;
|
|
|
|
if (bcount >= min_gallop)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* One run is winning so consistently that galloping may
|
|
|
|
* be a huge win. So try that, and continue galloping until
|
|
|
|
* (if ever) neither run appears to be winning consistently
|
|
|
|
* anymore.
|
|
|
|
*/
|
|
|
|
++min_gallop;
|
|
|
|
do {
|
|
|
|
assert(na > 0 && nb > 1);
|
|
|
|
min_gallop -= min_gallop > 1;
|
|
|
|
ms->min_gallop = min_gallop;
|
2018-01-28 23:03:23 -04:00
|
|
|
k = gallop_right(ms, ssb.keys[0], basea.keys, na, na-1);
|
2010-05-09 12:52:27 -03:00
|
|
|
if (k < 0)
|
|
|
|
goto Fail;
|
|
|
|
k = na - k;
|
|
|
|
acount = k;
|
|
|
|
if (k) {
|
2010-12-02 17:55:33 -04:00
|
|
|
sortslice_advance(&dest, -k);
|
|
|
|
sortslice_advance(&ssa, -k);
|
|
|
|
sortslice_memmove(&dest, 1, &ssa, 1, k);
|
2010-05-09 12:52:27 -03:00
|
|
|
na -= k;
|
|
|
|
if (na == 0)
|
|
|
|
goto Succeed;
|
|
|
|
}
|
2010-12-02 17:55:33 -04:00
|
|
|
sortslice_copy_decr(&dest, &ssb);
|
2010-05-09 12:52:27 -03:00
|
|
|
--nb;
|
|
|
|
if (nb == 1)
|
|
|
|
goto CopyA;
|
|
|
|
|
2018-01-28 23:03:23 -04:00
|
|
|
k = gallop_left(ms, ssa.keys[0], baseb.keys, nb, nb-1);
|
2010-05-09 12:52:27 -03:00
|
|
|
if (k < 0)
|
|
|
|
goto Fail;
|
|
|
|
k = nb - k;
|
|
|
|
bcount = k;
|
|
|
|
if (k) {
|
2010-12-02 17:55:33 -04:00
|
|
|
sortslice_advance(&dest, -k);
|
|
|
|
sortslice_advance(&ssb, -k);
|
|
|
|
sortslice_memcpy(&dest, 1, &ssb, 1, k);
|
2010-05-09 12:52:27 -03:00
|
|
|
nb -= k;
|
|
|
|
if (nb == 1)
|
|
|
|
goto CopyA;
|
|
|
|
/* nb==0 is impossible now if the comparison
|
|
|
|
* function is consistent, but we can't assume
|
|
|
|
* that it is.
|
|
|
|
*/
|
|
|
|
if (nb == 0)
|
|
|
|
goto Succeed;
|
|
|
|
}
|
2010-12-02 17:55:33 -04:00
|
|
|
sortslice_copy_decr(&dest, &ssa);
|
2010-05-09 12:52:27 -03:00
|
|
|
--na;
|
|
|
|
if (na == 0)
|
|
|
|
goto Succeed;
|
|
|
|
} while (acount >= MIN_GALLOP || bcount >= MIN_GALLOP);
|
|
|
|
++min_gallop; /* penalize it for leaving galloping mode */
|
|
|
|
ms->min_gallop = min_gallop;
|
|
|
|
}
|
2002-07-31 23:13:36 -03:00
|
|
|
Succeed:
|
2010-05-09 12:52:27 -03:00
|
|
|
result = 0;
|
2002-07-31 23:13:36 -03:00
|
|
|
Fail:
|
2010-05-09 12:52:27 -03:00
|
|
|
if (nb)
|
2010-12-02 17:55:33 -04:00
|
|
|
sortslice_memcpy(&dest, -(nb-1), &baseb, 0, nb);
|
2010-05-09 12:52:27 -03:00
|
|
|
return result;
|
2002-07-31 23:13:36 -03:00
|
|
|
CopyA:
|
2010-05-09 12:52:27 -03:00
|
|
|
assert(nb == 1 && na > 0);
|
2010-12-02 17:55:33 -04:00
|
|
|
/* The first element of ssb belongs at the front of the merge. */
|
|
|
|
sortslice_memmove(&dest, 1-na, &ssa, 1-na, na);
|
|
|
|
sortslice_advance(&dest, -na);
|
|
|
|
sortslice_advance(&ssa, -na);
|
|
|
|
sortslice_copy(&dest, 0, &ssb, 0);
|
2010-05-09 12:52:27 -03:00
|
|
|
return 0;
|
2002-07-31 23:13:36 -03:00
|
|
|
}
|
1998-05-13 18:20:49 -03:00
|
|
|
|
2002-07-31 23:13:36 -03:00
|
|
|
/* Merge the two runs at stack indices i and i+1.
|
|
|
|
* Returns 0 on success, -1 on error.
|
|
|
|
*/
|
2006-02-15 13:27:45 -04:00
|
|
|
static Py_ssize_t
|
|
|
|
merge_at(MergeState *ms, Py_ssize_t i)
|
2002-07-31 23:13:36 -03:00
|
|
|
{
|
2010-12-02 17:55:33 -04:00
|
|
|
sortslice ssa, ssb;
|
2010-05-09 12:52:27 -03:00
|
|
|
Py_ssize_t na, nb;
|
|
|
|
Py_ssize_t k;
|
|
|
|
|
|
|
|
assert(ms != NULL);
|
|
|
|
assert(ms->n >= 2);
|
|
|
|
assert(i >= 0);
|
|
|
|
assert(i == ms->n - 2 || i == ms->n - 3);
|
|
|
|
|
2010-12-02 17:55:33 -04:00
|
|
|
ssa = ms->pending[i].base;
|
2010-05-09 12:52:27 -03:00
|
|
|
na = ms->pending[i].len;
|
2010-12-02 17:55:33 -04:00
|
|
|
ssb = ms->pending[i+1].base;
|
2010-05-09 12:52:27 -03:00
|
|
|
nb = ms->pending[i+1].len;
|
|
|
|
assert(na > 0 && nb > 0);
|
2010-12-02 17:55:33 -04:00
|
|
|
assert(ssa.keys + na == ssb.keys);
|
2010-05-09 12:52:27 -03:00
|
|
|
|
|
|
|
/* Record the length of the combined runs; if i is the 3rd-last
|
|
|
|
* run now, also slide over the last run (which isn't involved
|
|
|
|
* in this merge). The current run i+1 goes away in any case.
|
|
|
|
*/
|
|
|
|
ms->pending[i].len = na + nb;
|
|
|
|
if (i == ms->n - 3)
|
|
|
|
ms->pending[i+1] = ms->pending[i+2];
|
|
|
|
--ms->n;
|
|
|
|
|
|
|
|
/* Where does b start in a? Elements in a before that can be
|
|
|
|
* ignored (already in place).
|
|
|
|
*/
|
2018-01-28 23:03:23 -04:00
|
|
|
k = gallop_right(ms, *ssb.keys, ssa.keys, na, 0);
|
2010-05-09 12:52:27 -03:00
|
|
|
if (k < 0)
|
|
|
|
return -1;
|
2010-12-02 17:55:33 -04:00
|
|
|
sortslice_advance(&ssa, k);
|
2010-05-09 12:52:27 -03:00
|
|
|
na -= k;
|
|
|
|
if (na == 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* Where does a end in b? Elements in b after that can be
|
|
|
|
* ignored (already in place).
|
|
|
|
*/
|
2018-01-28 23:03:23 -04:00
|
|
|
nb = gallop_left(ms, ssa.keys[na-1], ssb.keys, nb, nb-1);
|
2010-05-09 12:52:27 -03:00
|
|
|
if (nb <= 0)
|
|
|
|
return nb;
|
|
|
|
|
|
|
|
/* Merge what remains of the runs, using a temp array with
|
|
|
|
* min(na, nb) elements.
|
|
|
|
*/
|
|
|
|
if (na <= nb)
|
2010-12-02 17:55:33 -04:00
|
|
|
return merge_lo(ms, ssa, na, ssb, nb);
|
2010-05-09 12:52:27 -03:00
|
|
|
else
|
2010-12-02 17:55:33 -04:00
|
|
|
return merge_hi(ms, ssa, na, ssb, nb);
|
2002-07-31 23:13:36 -03:00
|
|
|
}
|
|
|
|
|
2021-09-06 14:54:41 -03:00
|
|
|
/* Two adjacent runs begin at index s1. The first run has length n1, and
|
|
|
|
* the second run (starting at index s1+n1) has length n2. The list has total
|
|
|
|
* length n.
|
|
|
|
* Compute the "power" of the first run. See listsort.txt for details.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
powerloop(Py_ssize_t s1, Py_ssize_t n1, Py_ssize_t n2, Py_ssize_t n)
|
|
|
|
{
|
|
|
|
int result = 0;
|
|
|
|
assert(s1 >= 0);
|
|
|
|
assert(n1 > 0 && n2 > 0);
|
|
|
|
assert(s1 + n1 + n2 <= n);
|
|
|
|
/* midpoints a and b:
|
|
|
|
* a = s1 + n1/2
|
|
|
|
* b = s1 + n1 + n2/2 = a + (n1 + n2)/2
|
|
|
|
*
|
|
|
|
* Those may not be integers, though, because of the "/2". So we work with
|
|
|
|
* 2*a and 2*b instead, which are necessarily integers. It makes no
|
|
|
|
* difference to the outcome, since the bits in the expansion of (2*i)/n
|
|
|
|
* are merely shifted one position from those of i/n.
|
|
|
|
*/
|
|
|
|
Py_ssize_t a = 2 * s1 + n1; /* 2*a */
|
|
|
|
Py_ssize_t b = a + n1 + n2; /* 2*b */
|
|
|
|
/* Emulate a/n and b/n one bit a time, until bits differ. */
|
|
|
|
for (;;) {
|
|
|
|
++result;
|
|
|
|
if (a >= n) { /* both quotient bits are 1 */
|
|
|
|
assert(b >= a);
|
|
|
|
a -= n;
|
|
|
|
b -= n;
|
|
|
|
}
|
|
|
|
else if (b >= n) { /* a/n bit is 0, b/n bit is 1 */
|
|
|
|
break;
|
|
|
|
} /* else both quotient bits are 0 */
|
|
|
|
assert(a < b && b < n);
|
|
|
|
a <<= 1;
|
|
|
|
b <<= 1;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The next run has been identified, of length n2.
|
|
|
|
* If there's already a run on the stack, apply the "powersort" merge strategy:
|
|
|
|
* compute the topmost run's "power" (depth in a conceptual binary merge tree)
|
|
|
|
* and merge adjacent runs on the stack with greater power. See listsort.txt
|
|
|
|
* for more info.
|
2002-07-31 23:13:36 -03:00
|
|
|
*
|
2021-10-06 20:57:10 -03:00
|
|
|
* It's the caller's responsibility to push the new run on the stack when this
|
2021-09-06 14:54:41 -03:00
|
|
|
* returns.
|
2002-07-31 23:13:36 -03:00
|
|
|
*
|
|
|
|
* Returns 0 on success, -1 on error.
|
|
|
|
*/
|
|
|
|
static int
|
2021-09-06 14:54:41 -03:00
|
|
|
found_new_run(MergeState *ms, Py_ssize_t n2)
|
2002-07-31 23:13:36 -03:00
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
assert(ms);
|
2021-09-06 14:54:41 -03:00
|
|
|
if (ms->n) {
|
|
|
|
assert(ms->n > 0);
|
|
|
|
struct s_slice *p = ms->pending;
|
|
|
|
Py_ssize_t s1 = p[ms->n - 1].base.keys - ms->basekeys; /* start index */
|
|
|
|
Py_ssize_t n1 = p[ms->n - 1].len;
|
|
|
|
int power = powerloop(s1, n1, n2, ms->listlen);
|
|
|
|
while (ms->n > 1 && p[ms->n - 2].power > power) {
|
|
|
|
if (merge_at(ms, ms->n - 2) < 0)
|
2010-05-09 12:52:27 -03:00
|
|
|
return -1;
|
|
|
|
}
|
2021-09-06 14:54:41 -03:00
|
|
|
assert(ms->n < 2 || p[ms->n - 2].power < power);
|
|
|
|
p[ms->n - 1].power = power;
|
2010-05-09 12:52:27 -03:00
|
|
|
}
|
|
|
|
return 0;
|
2002-07-31 23:13:36 -03:00
|
|
|
}
|
1997-12-10 11:14:24 -04:00
|
|
|
|
2002-07-31 23:13:36 -03:00
|
|
|
/* Regardless of invariants, merge all runs on the stack until only one
|
|
|
|
* remains. This is used at the end of the mergesort.
|
|
|
|
*
|
|
|
|
* Returns 0 on success, -1 on error.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
merge_force_collapse(MergeState *ms)
|
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
struct s_slice *p = ms->pending;
|
2002-07-31 23:13:36 -03:00
|
|
|
|
2010-05-09 12:52:27 -03:00
|
|
|
assert(ms);
|
|
|
|
while (ms->n > 1) {
|
|
|
|
Py_ssize_t n = ms->n - 2;
|
|
|
|
if (n > 0 && p[n-1].len < p[n+1].len)
|
|
|
|
--n;
|
|
|
|
if (merge_at(ms, n) < 0)
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
2002-07-31 23:13:36 -03:00
|
|
|
}
|
1998-05-29 14:56:32 -03:00
|
|
|
|
2002-07-31 23:13:36 -03:00
|
|
|
/* Compute a good value for the minimum run length; natural runs shorter
|
|
|
|
* than this are boosted artificially via binary insertion.
|
|
|
|
*
|
|
|
|
* If n < 64, return n (it's too small to bother with fancy stuff).
|
|
|
|
* Else if n is an exact power of 2, return 32.
|
|
|
|
* Else return an int k, 32 <= k <= 64, such that n/k is close to, but
|
|
|
|
* strictly less than, an exact power of 2.
|
|
|
|
*
|
|
|
|
* See listsort.txt for more info.
|
|
|
|
*/
|
2006-02-15 13:27:45 -04:00
|
|
|
static Py_ssize_t
|
|
|
|
merge_compute_minrun(Py_ssize_t n)
|
2002-07-31 23:13:36 -03:00
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
Py_ssize_t r = 0; /* becomes 1 if any 1 bits are shifted off */
|
2002-07-31 23:13:36 -03:00
|
|
|
|
2010-05-09 12:52:27 -03:00
|
|
|
assert(n >= 0);
|
|
|
|
while (n >= 64) {
|
|
|
|
r |= n & 1;
|
|
|
|
n >>= 1;
|
|
|
|
}
|
|
|
|
return n + r;
|
1998-06-16 12:18:28 -03:00
|
|
|
}
|
1998-05-29 14:56:32 -03:00
|
|
|
|
2006-04-21 07:40:58 -03:00
|
|
|
static void
|
2010-12-02 17:55:33 -04:00
|
|
|
reverse_sortslice(sortslice *s, Py_ssize_t n)
|
2003-10-16 00:41:09 -03:00
|
|
|
{
|
2010-12-02 17:55:33 -04:00
|
|
|
reverse_slice(s->keys, &s->keys[n]);
|
|
|
|
if (s->values != NULL)
|
|
|
|
reverse_slice(s->values, &s->values[n]);
|
2003-10-16 00:41:09 -03:00
|
|
|
}
|
|
|
|
|
2018-01-28 23:03:23 -04:00
|
|
|
/* Here we define custom comparison functions to optimize for the cases one commonly
|
|
|
|
* encounters in practice: homogeneous lists, often of one of the basic types. */
|
|
|
|
|
|
|
|
/* This struct holds the comparison function and helper functions
|
|
|
|
* selected in the pre-sort check. */
|
|
|
|
|
|
|
|
/* These are the special case compare functions.
|
|
|
|
* ms->key_compare will always point to one of these: */
|
|
|
|
|
|
|
|
/* Heterogeneous compare: default, always safe to fall back on. */
|
|
|
|
static int
|
|
|
|
safe_object_compare(PyObject *v, PyObject *w, MergeState *ms)
|
|
|
|
{
|
|
|
|
/* No assumptions necessary! */
|
|
|
|
return PyObject_RichCompareBool(v, w, Py_LT);
|
|
|
|
}
|
|
|
|
|
2021-10-06 20:57:10 -03:00
|
|
|
/* Homogeneous compare: safe for any two comparable objects of the same type.
|
2018-01-28 23:03:23 -04:00
|
|
|
* (ms->key_richcompare is set to ob_type->tp_richcompare in the
|
|
|
|
* pre-sort check.)
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
unsafe_object_compare(PyObject *v, PyObject *w, MergeState *ms)
|
|
|
|
{
|
|
|
|
PyObject *res_obj; int res;
|
|
|
|
|
|
|
|
/* No assumptions, because we check first: */
|
2020-02-06 22:04:21 -04:00
|
|
|
if (Py_TYPE(v)->tp_richcompare != ms->key_richcompare)
|
2018-01-28 23:03:23 -04:00
|
|
|
return PyObject_RichCompareBool(v, w, Py_LT);
|
|
|
|
|
|
|
|
assert(ms->key_richcompare != NULL);
|
|
|
|
res_obj = (*(ms->key_richcompare))(v, w, Py_LT);
|
|
|
|
|
|
|
|
if (res_obj == Py_NotImplemented) {
|
|
|
|
Py_DECREF(res_obj);
|
|
|
|
return PyObject_RichCompareBool(v, w, Py_LT);
|
|
|
|
}
|
|
|
|
if (res_obj == NULL)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (PyBool_Check(res_obj)) {
|
|
|
|
res = (res_obj == Py_True);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
res = PyObject_IsTrue(res_obj);
|
|
|
|
}
|
|
|
|
Py_DECREF(res_obj);
|
|
|
|
|
|
|
|
/* Note that we can't assert
|
|
|
|
* res == PyObject_RichCompareBool(v, w, Py_LT);
|
|
|
|
* because of evil compare functions like this:
|
|
|
|
* lambda a, b: int(random.random() * 3) - 1)
|
|
|
|
* (which is actually in test_sort.py) */
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Latin string compare: safe for any two latin (one byte per char) strings. */
|
|
|
|
static int
|
|
|
|
unsafe_latin_compare(PyObject *v, PyObject *w, MergeState *ms)
|
|
|
|
{
|
2018-01-29 08:47:06 -04:00
|
|
|
Py_ssize_t len;
|
|
|
|
int res;
|
2018-01-28 23:03:23 -04:00
|
|
|
|
|
|
|
/* Modified from Objects/unicodeobject.c:unicode_compare, assuming: */
|
2020-03-04 09:15:20 -04:00
|
|
|
assert(Py_IS_TYPE(v, &PyUnicode_Type));
|
|
|
|
assert(Py_IS_TYPE(w, &PyUnicode_Type));
|
2018-01-28 23:03:23 -04:00
|
|
|
assert(PyUnicode_KIND(v) == PyUnicode_KIND(w));
|
|
|
|
assert(PyUnicode_KIND(v) == PyUnicode_1BYTE_KIND);
|
|
|
|
|
|
|
|
len = Py_MIN(PyUnicode_GET_LENGTH(v), PyUnicode_GET_LENGTH(w));
|
|
|
|
res = memcmp(PyUnicode_DATA(v), PyUnicode_DATA(w), len);
|
|
|
|
|
|
|
|
res = (res != 0 ?
|
|
|
|
res < 0 :
|
|
|
|
PyUnicode_GET_LENGTH(v) < PyUnicode_GET_LENGTH(w));
|
|
|
|
|
|
|
|
assert(res == PyObject_RichCompareBool(v, w, Py_LT));;
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Bounded int compare: compare any two longs that fit in a single machine word. */
|
|
|
|
static int
|
|
|
|
unsafe_long_compare(PyObject *v, PyObject *w, MergeState *ms)
|
|
|
|
{
|
2023-03-22 11:49:51 -03:00
|
|
|
PyLongObject *vl, *wl;
|
|
|
|
intptr_t v0, w0;
|
|
|
|
int res;
|
2018-01-28 23:03:23 -04:00
|
|
|
|
|
|
|
/* Modified from Objects/longobject.c:long_compare, assuming: */
|
2020-03-04 09:15:20 -04:00
|
|
|
assert(Py_IS_TYPE(v, &PyLong_Type));
|
|
|
|
assert(Py_IS_TYPE(w, &PyLong_Type));
|
2023-03-22 11:49:51 -03:00
|
|
|
assert(_PyLong_IsCompact((PyLongObject *)v));
|
|
|
|
assert(_PyLong_IsCompact((PyLongObject *)w));
|
2018-01-28 23:03:23 -04:00
|
|
|
|
|
|
|
vl = (PyLongObject*)v;
|
|
|
|
wl = (PyLongObject*)w;
|
|
|
|
|
2023-03-22 11:49:51 -03:00
|
|
|
v0 = _PyLong_CompactValue(vl);
|
|
|
|
w0 = _PyLong_CompactValue(wl);
|
2018-01-28 23:03:23 -04:00
|
|
|
|
|
|
|
res = v0 < w0;
|
|
|
|
assert(res == PyObject_RichCompareBool(v, w, Py_LT));
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Float compare: compare any two floats. */
|
|
|
|
static int
|
|
|
|
unsafe_float_compare(PyObject *v, PyObject *w, MergeState *ms)
|
|
|
|
{
|
|
|
|
int res;
|
|
|
|
|
|
|
|
/* Modified from Objects/floatobject.c:float_richcompare, assuming: */
|
2020-03-04 09:15:20 -04:00
|
|
|
assert(Py_IS_TYPE(v, &PyFloat_Type));
|
|
|
|
assert(Py_IS_TYPE(w, &PyFloat_Type));
|
2018-01-28 23:03:23 -04:00
|
|
|
|
|
|
|
res = PyFloat_AS_DOUBLE(v) < PyFloat_AS_DOUBLE(w);
|
|
|
|
assert(res == PyObject_RichCompareBool(v, w, Py_LT));
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Tuple compare: compare *any* two tuples, using
|
|
|
|
* ms->tuple_elem_compare to compare the first elements, which is set
|
|
|
|
* using the same pre-sort check as we use for ms->key_compare,
|
|
|
|
* but run on the list [x[0] for x in L]. This allows us to optimize compares
|
|
|
|
* on two levels (as long as [x[0] for x in L] is type-homogeneous.) The idea is
|
2022-07-24 18:33:06 -03:00
|
|
|
* that most tuple compares don't involve x[1:]. */
|
2018-01-28 23:03:23 -04:00
|
|
|
static int
|
|
|
|
unsafe_tuple_compare(PyObject *v, PyObject *w, MergeState *ms)
|
|
|
|
{
|
|
|
|
PyTupleObject *vt, *wt;
|
|
|
|
Py_ssize_t i, vlen, wlen;
|
|
|
|
int k;
|
|
|
|
|
|
|
|
/* Modified from Objects/tupleobject.c:tuplerichcompare, assuming: */
|
2020-03-04 09:15:20 -04:00
|
|
|
assert(Py_IS_TYPE(v, &PyTuple_Type));
|
|
|
|
assert(Py_IS_TYPE(w, &PyTuple_Type));
|
2018-01-28 23:03:23 -04:00
|
|
|
assert(Py_SIZE(v) > 0);
|
|
|
|
assert(Py_SIZE(w) > 0);
|
|
|
|
|
|
|
|
vt = (PyTupleObject *)v;
|
|
|
|
wt = (PyTupleObject *)w;
|
|
|
|
|
|
|
|
vlen = Py_SIZE(vt);
|
|
|
|
wlen = Py_SIZE(wt);
|
2022-07-24 18:33:06 -03:00
|
|
|
|
|
|
|
for (i = 0; i < vlen && i < wlen; i++) {
|
2018-01-28 23:03:23 -04:00
|
|
|
k = PyObject_RichCompareBool(vt->ob_item[i], wt->ob_item[i], Py_EQ);
|
|
|
|
if (k < 0)
|
|
|
|
return -1;
|
2022-07-24 18:33:06 -03:00
|
|
|
if (!k)
|
|
|
|
break;
|
2018-01-28 23:03:23 -04:00
|
|
|
}
|
|
|
|
|
2022-07-24 18:33:06 -03:00
|
|
|
if (i >= vlen || i >= wlen)
|
|
|
|
return vlen < wlen;
|
|
|
|
|
|
|
|
if (i == 0)
|
|
|
|
return ms->tuple_elem_compare(vt->ob_item[i], wt->ob_item[i], ms);
|
|
|
|
else
|
|
|
|
return PyObject_RichCompareBool(vt->ob_item[i], wt->ob_item[i], Py_LT);
|
|
|
|
}
|
2018-01-28 23:03:23 -04:00
|
|
|
|
2002-07-31 23:13:36 -03:00
|
|
|
/* An adaptive, stable, natural mergesort. See listsort.txt.
|
|
|
|
* Returns Py_None on success, NULL on error. Even in case of error, the
|
|
|
|
* list will be some permutation of its input state (nothing is lost or
|
|
|
|
* duplicated).
|
|
|
|
*/
|
2017-03-11 03:19:20 -04:00
|
|
|
/*[clinic input]
|
|
|
|
list.sort
|
|
|
|
|
|
|
|
*
|
|
|
|
key as keyfunc: object = None
|
2022-12-03 15:52:21 -04:00
|
|
|
reverse: bool = False
|
2017-03-11 03:19:20 -04:00
|
|
|
|
2019-06-01 01:10:02 -03:00
|
|
|
Sort the list in ascending order and return None.
|
|
|
|
|
|
|
|
The sort is in-place (i.e. the list itself is modified) and stable (i.e. the
|
|
|
|
order of two equal elements is maintained).
|
|
|
|
|
|
|
|
If a key function is given, apply it once to each list item and sort them,
|
|
|
|
ascending or descending, according to their function values.
|
|
|
|
|
|
|
|
The reverse flag can be set to sort in descending order.
|
2017-03-11 03:19:20 -04:00
|
|
|
[clinic start generated code]*/
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
static PyObject *
|
2017-03-11 03:19:20 -04:00
|
|
|
list_sort_impl(PyListObject *self, PyObject *keyfunc, int reverse)
|
2022-12-03 15:52:21 -04:00
|
|
|
/*[clinic end generated code: output=57b9f9c5e23fbe42 input=a74c4cd3ec6b5c08]*/
|
1996-12-10 19:55:39 -04:00
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
MergeState ms;
|
|
|
|
Py_ssize_t nremaining;
|
|
|
|
Py_ssize_t minrun;
|
2010-12-02 17:55:33 -04:00
|
|
|
sortslice lo;
|
2010-05-09 12:52:27 -03:00
|
|
|
Py_ssize_t saved_ob_size, saved_allocated;
|
|
|
|
PyObject **saved_ob_item;
|
|
|
|
PyObject **final_ob_item;
|
|
|
|
PyObject *result = NULL; /* guilty until proved innocent */
|
|
|
|
Py_ssize_t i;
|
2010-12-02 17:55:33 -04:00
|
|
|
PyObject **keys;
|
2010-05-09 12:52:27 -03:00
|
|
|
|
|
|
|
assert(self != NULL);
|
2017-03-11 03:19:20 -04:00
|
|
|
assert(PyList_Check(self));
|
2010-05-09 12:52:27 -03:00
|
|
|
if (keyfunc == Py_None)
|
|
|
|
keyfunc = NULL;
|
|
|
|
|
|
|
|
/* The list is temporarily made empty, so that mutations performed
|
|
|
|
* by comparison functions can't affect the slice of memory we're
|
|
|
|
* sorting (allowing mutations during sorting is a core-dump
|
|
|
|
* factory, since ob_item may change).
|
|
|
|
*/
|
|
|
|
saved_ob_size = Py_SIZE(self);
|
|
|
|
saved_ob_item = self->ob_item;
|
|
|
|
saved_allocated = self->allocated;
|
2020-02-07 18:18:08 -04:00
|
|
|
Py_SET_SIZE(self, 0);
|
2010-05-09 12:52:27 -03:00
|
|
|
self->ob_item = NULL;
|
|
|
|
self->allocated = -1; /* any operation will reset it to >= 0 */
|
|
|
|
|
2010-12-02 17:55:33 -04:00
|
|
|
if (keyfunc == NULL) {
|
|
|
|
keys = NULL;
|
|
|
|
lo.keys = saved_ob_item;
|
|
|
|
lo.values = NULL;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (saved_ob_size < MERGESTATE_TEMP_SIZE/2)
|
|
|
|
/* Leverage stack space we allocated but won't otherwise use */
|
|
|
|
keys = &ms.temparray[saved_ob_size+1];
|
|
|
|
else {
|
2020-12-01 04:56:42 -04:00
|
|
|
keys = PyMem_Malloc(sizeof(PyObject *) * saved_ob_size);
|
2015-04-23 18:04:36 -03:00
|
|
|
if (keys == NULL) {
|
|
|
|
PyErr_NoMemory();
|
|
|
|
goto keyfunc_fail;
|
|
|
|
}
|
2010-12-02 17:55:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < saved_ob_size ; i++) {
|
2020-02-11 12:46:57 -04:00
|
|
|
keys[i] = PyObject_CallOneArg(keyfunc, saved_ob_item[i]);
|
2010-12-02 17:55:33 -04:00
|
|
|
if (keys[i] == NULL) {
|
|
|
|
for (i=i-1 ; i>=0 ; i--)
|
|
|
|
Py_DECREF(keys[i]);
|
2014-03-15 14:21:28 -03:00
|
|
|
if (saved_ob_size >= MERGESTATE_TEMP_SIZE/2)
|
2020-12-01 04:56:42 -04:00
|
|
|
PyMem_Free(keys);
|
2010-12-02 17:55:33 -04:00
|
|
|
goto keyfunc_fail;
|
2010-05-09 12:52:27 -03:00
|
|
|
}
|
|
|
|
}
|
2010-12-02 17:55:33 -04:00
|
|
|
|
|
|
|
lo.keys = keys;
|
|
|
|
lo.values = saved_ob_item;
|
2010-05-09 12:52:27 -03:00
|
|
|
}
|
|
|
|
|
2018-01-28 23:03:23 -04:00
|
|
|
|
|
|
|
/* The pre-sort check: here's where we decide which compare function to use.
|
|
|
|
* How much optimization is safe? We test for homogeneity with respect to
|
|
|
|
* several properties that are expensive to check at compare-time, and
|
|
|
|
* set ms appropriately. */
|
|
|
|
if (saved_ob_size > 1) {
|
|
|
|
/* Assume the first element is representative of the whole list. */
|
2020-03-04 09:15:20 -04:00
|
|
|
int keys_are_in_tuples = (Py_IS_TYPE(lo.keys[0], &PyTuple_Type) &&
|
2018-01-28 23:03:23 -04:00
|
|
|
Py_SIZE(lo.keys[0]) > 0);
|
|
|
|
|
|
|
|
PyTypeObject* key_type = (keys_are_in_tuples ?
|
2020-02-06 22:04:21 -04:00
|
|
|
Py_TYPE(PyTuple_GET_ITEM(lo.keys[0], 0)) :
|
|
|
|
Py_TYPE(lo.keys[0]));
|
2018-01-28 23:03:23 -04:00
|
|
|
|
|
|
|
int keys_are_all_same_type = 1;
|
|
|
|
int strings_are_latin = 1;
|
|
|
|
int ints_are_bounded = 1;
|
|
|
|
|
|
|
|
/* Prove that assumption by checking every key. */
|
|
|
|
for (i=0; i < saved_ob_size; i++) {
|
|
|
|
|
|
|
|
if (keys_are_in_tuples &&
|
2020-03-04 09:15:20 -04:00
|
|
|
!(Py_IS_TYPE(lo.keys[i], &PyTuple_Type) && Py_SIZE(lo.keys[i]) != 0)) {
|
2018-01-28 23:03:23 -04:00
|
|
|
keys_are_in_tuples = 0;
|
|
|
|
keys_are_all_same_type = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Note: for lists of tuples, key is the first element of the tuple
|
|
|
|
* lo.keys[i], not lo.keys[i] itself! We verify type-homogeneity
|
|
|
|
* for lists of tuples in the if-statement directly above. */
|
|
|
|
PyObject *key = (keys_are_in_tuples ?
|
|
|
|
PyTuple_GET_ITEM(lo.keys[i], 0) :
|
|
|
|
lo.keys[i]);
|
|
|
|
|
2020-03-04 09:15:20 -04:00
|
|
|
if (!Py_IS_TYPE(key, key_type)) {
|
2018-01-28 23:03:23 -04:00
|
|
|
keys_are_all_same_type = 0;
|
2019-03-25 04:25:37 -03:00
|
|
|
/* If keys are in tuple we must loop over the whole list to make
|
|
|
|
sure all items are tuples */
|
|
|
|
if (!keys_are_in_tuples) {
|
|
|
|
break;
|
|
|
|
}
|
2018-01-28 23:03:23 -04:00
|
|
|
}
|
|
|
|
|
2019-03-25 04:25:37 -03:00
|
|
|
if (keys_are_all_same_type) {
|
|
|
|
if (key_type == &PyLong_Type &&
|
|
|
|
ints_are_bounded &&
|
2023-03-22 11:49:51 -03:00
|
|
|
!_PyLong_IsCompact((PyLongObject *)key)) {
|
2019-03-25 04:25:37 -03:00
|
|
|
|
2018-01-28 23:03:23 -04:00
|
|
|
ints_are_bounded = 0;
|
2019-03-25 04:25:37 -03:00
|
|
|
}
|
|
|
|
else if (key_type == &PyUnicode_Type &&
|
|
|
|
strings_are_latin &&
|
|
|
|
PyUnicode_KIND(key) != PyUnicode_1BYTE_KIND) {
|
|
|
|
|
|
|
|
strings_are_latin = 0;
|
|
|
|
}
|
|
|
|
}
|
2018-01-28 23:03:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Choose the best compare, given what we now know about the keys. */
|
|
|
|
if (keys_are_all_same_type) {
|
|
|
|
|
|
|
|
if (key_type == &PyUnicode_Type && strings_are_latin) {
|
|
|
|
ms.key_compare = unsafe_latin_compare;
|
|
|
|
}
|
|
|
|
else if (key_type == &PyLong_Type && ints_are_bounded) {
|
|
|
|
ms.key_compare = unsafe_long_compare;
|
|
|
|
}
|
|
|
|
else if (key_type == &PyFloat_Type) {
|
|
|
|
ms.key_compare = unsafe_float_compare;
|
|
|
|
}
|
|
|
|
else if ((ms.key_richcompare = key_type->tp_richcompare) != NULL) {
|
|
|
|
ms.key_compare = unsafe_object_compare;
|
|
|
|
}
|
2019-02-21 03:47:14 -04:00
|
|
|
else {
|
|
|
|
ms.key_compare = safe_object_compare;
|
|
|
|
}
|
2018-01-28 23:03:23 -04:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
ms.key_compare = safe_object_compare;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (keys_are_in_tuples) {
|
|
|
|
/* Make sure we're not dealing with tuples of tuples
|
|
|
|
* (remember: here, key_type refers list [key[0] for key in keys]) */
|
2019-03-25 04:25:37 -03:00
|
|
|
if (key_type == &PyTuple_Type) {
|
2018-01-28 23:03:23 -04:00
|
|
|
ms.tuple_elem_compare = safe_object_compare;
|
2019-03-25 04:25:37 -03:00
|
|
|
}
|
|
|
|
else {
|
2018-01-28 23:03:23 -04:00
|
|
|
ms.tuple_elem_compare = ms.key_compare;
|
2019-03-25 04:25:37 -03:00
|
|
|
}
|
2018-01-28 23:03:23 -04:00
|
|
|
|
|
|
|
ms.key_compare = unsafe_tuple_compare;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* End of pre-sort check: ms is now set properly! */
|
|
|
|
|
2021-09-06 14:54:41 -03:00
|
|
|
merge_init(&ms, saved_ob_size, keys != NULL, &lo);
|
2010-05-09 12:52:27 -03:00
|
|
|
|
|
|
|
nremaining = saved_ob_size;
|
|
|
|
if (nremaining < 2)
|
|
|
|
goto succeed;
|
|
|
|
|
2010-08-23 16:35:39 -03:00
|
|
|
/* Reverse sort stability achieved by initially reversing the list,
|
|
|
|
applying a stable forward sort, then reversing the final result. */
|
2010-12-02 17:55:33 -04:00
|
|
|
if (reverse) {
|
|
|
|
if (keys != NULL)
|
|
|
|
reverse_slice(&keys[0], &keys[saved_ob_size]);
|
|
|
|
reverse_slice(&saved_ob_item[0], &saved_ob_item[saved_ob_size]);
|
|
|
|
}
|
2010-08-23 16:35:39 -03:00
|
|
|
|
2010-05-09 12:52:27 -03:00
|
|
|
/* March over the array once, left to right, finding natural runs,
|
|
|
|
* and extending short natural runs to minrun elements.
|
|
|
|
*/
|
|
|
|
minrun = merge_compute_minrun(nremaining);
|
|
|
|
do {
|
|
|
|
int descending;
|
|
|
|
Py_ssize_t n;
|
|
|
|
|
|
|
|
/* Identify next run. */
|
2018-01-28 23:03:23 -04:00
|
|
|
n = count_run(&ms, lo.keys, lo.keys + nremaining, &descending);
|
2010-05-09 12:52:27 -03:00
|
|
|
if (n < 0)
|
|
|
|
goto fail;
|
|
|
|
if (descending)
|
2010-12-02 17:55:33 -04:00
|
|
|
reverse_sortslice(&lo, n);
|
2010-05-09 12:52:27 -03:00
|
|
|
/* If short, extend to min(minrun, nremaining). */
|
|
|
|
if (n < minrun) {
|
|
|
|
const Py_ssize_t force = nremaining <= minrun ?
|
|
|
|
nremaining : minrun;
|
2018-01-28 23:03:23 -04:00
|
|
|
if (binarysort(&ms, lo, lo.keys + force, lo.keys + n) < 0)
|
2010-05-09 12:52:27 -03:00
|
|
|
goto fail;
|
|
|
|
n = force;
|
|
|
|
}
|
2021-09-06 14:54:41 -03:00
|
|
|
/* Maybe merge pending runs. */
|
|
|
|
assert(ms.n == 0 || ms.pending[ms.n -1].base.keys +
|
|
|
|
ms.pending[ms.n-1].len == lo.keys);
|
|
|
|
if (found_new_run(&ms, n) < 0)
|
|
|
|
goto fail;
|
|
|
|
/* Push new run on stack. */
|
2010-05-09 12:52:27 -03:00
|
|
|
assert(ms.n < MAX_MERGE_PENDING);
|
|
|
|
ms.pending[ms.n].base = lo;
|
|
|
|
ms.pending[ms.n].len = n;
|
|
|
|
++ms.n;
|
|
|
|
/* Advance to find next run. */
|
2010-12-02 17:55:33 -04:00
|
|
|
sortslice_advance(&lo, n);
|
2010-05-09 12:52:27 -03:00
|
|
|
nremaining -= n;
|
|
|
|
} while (nremaining);
|
|
|
|
|
|
|
|
if (merge_force_collapse(&ms) < 0)
|
|
|
|
goto fail;
|
|
|
|
assert(ms.n == 1);
|
2010-12-02 17:55:33 -04:00
|
|
|
assert(keys == NULL
|
|
|
|
? ms.pending[0].base.keys == saved_ob_item
|
|
|
|
: ms.pending[0].base.keys == &keys[0]);
|
2010-05-09 12:52:27 -03:00
|
|
|
assert(ms.pending[0].len == saved_ob_size);
|
2010-12-02 17:55:33 -04:00
|
|
|
lo = ms.pending[0].base;
|
2002-07-19 04:05:44 -03:00
|
|
|
|
2002-07-31 23:13:36 -03:00
|
|
|
succeed:
|
2010-05-09 12:52:27 -03:00
|
|
|
result = Py_None;
|
2002-07-19 04:05:44 -03:00
|
|
|
fail:
|
2010-12-02 17:55:33 -04:00
|
|
|
if (keys != NULL) {
|
|
|
|
for (i = 0; i < saved_ob_size; i++)
|
|
|
|
Py_DECREF(keys[i]);
|
2014-03-14 23:54:31 -03:00
|
|
|
if (saved_ob_size >= MERGESTATE_TEMP_SIZE/2)
|
2020-12-01 04:56:42 -04:00
|
|
|
PyMem_Free(keys);
|
2010-05-09 12:52:27 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (self->allocated != -1 && result != NULL) {
|
|
|
|
/* The user mucked with the list during the sort,
|
|
|
|
* and we don't already have another error to report.
|
|
|
|
*/
|
|
|
|
PyErr_SetString(PyExc_ValueError, "list modified during sort");
|
|
|
|
result = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (reverse && saved_ob_size > 1)
|
|
|
|
reverse_slice(saved_ob_item, saved_ob_item + saved_ob_size);
|
|
|
|
|
|
|
|
merge_freemem(&ms);
|
2003-10-16 00:41:09 -03:00
|
|
|
|
2010-12-02 17:55:33 -04:00
|
|
|
keyfunc_fail:
|
2010-05-09 12:52:27 -03:00
|
|
|
final_ob_item = self->ob_item;
|
|
|
|
i = Py_SIZE(self);
|
2020-02-07 18:18:08 -04:00
|
|
|
Py_SET_SIZE(self, saved_ob_size);
|
2010-05-09 12:52:27 -03:00
|
|
|
self->ob_item = saved_ob_item;
|
|
|
|
self->allocated = saved_allocated;
|
|
|
|
if (final_ob_item != NULL) {
|
2023-11-13 12:14:56 -04:00
|
|
|
/* we cannot use list_clear() for this because it does not
|
2010-05-09 12:52:27 -03:00
|
|
|
guarantee that the list is really empty when it returns */
|
|
|
|
while (--i >= 0) {
|
|
|
|
Py_XDECREF(final_ob_item[i]);
|
|
|
|
}
|
2020-12-01 04:56:42 -04:00
|
|
|
PyMem_Free(final_ob_item);
|
2010-05-09 12:52:27 -03:00
|
|
|
}
|
2022-11-10 17:09:18 -04:00
|
|
|
return Py_XNewRef(result);
|
1996-12-10 19:55:39 -04:00
|
|
|
}
|
2002-07-19 04:05:44 -03:00
|
|
|
#undef IFLT
|
2002-08-04 14:47:26 -03:00
|
|
|
#undef ISLT
|
2002-07-19 04:05:44 -03:00
|
|
|
|
1998-06-16 12:18:28 -03:00
|
|
|
int
|
2000-07-09 12:16:51 -03:00
|
|
|
PyList_Sort(PyObject *v)
|
1998-06-16 12:18:28 -03:00
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
if (v == NULL || !PyList_Check(v)) {
|
|
|
|
PyErr_BadInternalCall();
|
|
|
|
return -1;
|
|
|
|
}
|
2017-03-11 03:19:20 -04:00
|
|
|
v = list_sort_impl((PyListObject *)v, NULL, 0);
|
2010-05-09 12:52:27 -03:00
|
|
|
if (v == NULL)
|
|
|
|
return -1;
|
|
|
|
Py_DECREF(v);
|
|
|
|
return 0;
|
1998-06-16 12:18:28 -03:00
|
|
|
}
|
|
|
|
|
2017-03-11 03:19:20 -04:00
|
|
|
/*[clinic input]
|
2024-01-08 20:00:55 -04:00
|
|
|
@critical_section
|
2017-03-11 03:19:20 -04:00
|
|
|
list.reverse
|
|
|
|
|
|
|
|
Reverse *IN PLACE*.
|
|
|
|
[clinic start generated code]*/
|
|
|
|
|
2001-02-12 18:06:02 -04:00
|
|
|
static PyObject *
|
2017-03-11 03:19:20 -04:00
|
|
|
list_reverse_impl(PyListObject *self)
|
2024-01-08 20:00:55 -04:00
|
|
|
/*[clinic end generated code: output=482544fc451abea9 input=04ac8e0c6a66e4d9]*/
|
2001-02-12 18:06:02 -04:00
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
if (Py_SIZE(self) > 1)
|
|
|
|
reverse_slice(self->ob_item, self->ob_item + Py_SIZE(self));
|
|
|
|
Py_RETURN_NONE;
|
1991-03-06 09:07:53 -04:00
|
|
|
}
|
|
|
|
|
1995-01-17 12:34:45 -04:00
|
|
|
int
|
2000-07-09 12:16:51 -03:00
|
|
|
PyList_Reverse(PyObject *v)
|
1995-01-17 12:34:45 -04:00
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
PyListObject *self = (PyListObject *)v;
|
2002-08-07 22:06:39 -03:00
|
|
|
|
2010-05-09 12:52:27 -03:00
|
|
|
if (v == NULL || !PyList_Check(v)) {
|
|
|
|
PyErr_BadInternalCall();
|
|
|
|
return -1;
|
|
|
|
}
|
2024-01-15 20:11:14 -04:00
|
|
|
Py_BEGIN_CRITICAL_SECTION(self);
|
|
|
|
if (Py_SIZE(self) > 1) {
|
2010-05-09 12:52:27 -03:00
|
|
|
reverse_slice(self->ob_item, self->ob_item + Py_SIZE(self));
|
2024-01-15 20:11:14 -04:00
|
|
|
}
|
|
|
|
Py_END_CRITICAL_SECTION()
|
2010-05-09 12:52:27 -03:00
|
|
|
return 0;
|
1995-01-17 12:34:45 -04:00
|
|
|
}
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
PyObject *
|
2000-07-09 12:16:51 -03:00
|
|
|
PyList_AsTuple(PyObject *v)
|
1994-08-29 09:45:32 -03:00
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
if (v == NULL || !PyList_Check(v)) {
|
|
|
|
PyErr_BadInternalCall();
|
|
|
|
return NULL;
|
|
|
|
}
|
2024-01-15 20:11:14 -04:00
|
|
|
PyObject *ret;
|
|
|
|
PyListObject *self = (PyListObject *)v;
|
|
|
|
Py_BEGIN_CRITICAL_SECTION(self);
|
|
|
|
ret = _PyTuple_FromArray(self->ob_item, Py_SIZE(v));
|
|
|
|
Py_END_CRITICAL_SECTION();
|
|
|
|
return ret;
|
1994-08-29 09:45:32 -03:00
|
|
|
}
|
|
|
|
|
2023-01-03 14:49:49 -04:00
|
|
|
PyObject *
|
|
|
|
_PyList_FromArraySteal(PyObject *const *src, Py_ssize_t n)
|
|
|
|
{
|
|
|
|
if (n == 0) {
|
|
|
|
return PyList_New(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
PyListObject *list = (PyListObject *)PyList_New(n);
|
|
|
|
if (list == NULL) {
|
|
|
|
for (Py_ssize_t i = 0; i < n; i++) {
|
|
|
|
Py_DECREF(src[i]);
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
PyObject **dst = list->ob_item;
|
|
|
|
memcpy(dst, src, n * sizeof(PyObject *));
|
|
|
|
|
|
|
|
return (PyObject *)list;
|
|
|
|
}
|
|
|
|
|
2017-03-11 03:19:20 -04:00
|
|
|
/*[clinic input]
|
|
|
|
list.index
|
|
|
|
|
|
|
|
value: object
|
2017-03-19 14:37:40 -03:00
|
|
|
start: slice_index(accept={int}) = 0
|
|
|
|
stop: slice_index(accept={int}, c_default="PY_SSIZE_T_MAX") = sys.maxsize
|
2017-03-11 03:19:20 -04:00
|
|
|
/
|
|
|
|
|
|
|
|
Return first index of value.
|
|
|
|
|
|
|
|
Raises ValueError if the value is not present.
|
|
|
|
[clinic start generated code]*/
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
static PyObject *
|
2017-03-11 03:19:20 -04:00
|
|
|
list_index_impl(PyListObject *self, PyObject *value, Py_ssize_t start,
|
|
|
|
Py_ssize_t stop)
|
2017-03-19 14:37:40 -03:00
|
|
|
/*[clinic end generated code: output=ec51b88787e4e481 input=40ec5826303a0eb1]*/
|
1991-03-06 09:07:53 -04:00
|
|
|
{
|
2017-03-11 03:19:20 -04:00
|
|
|
Py_ssize_t i;
|
2010-05-09 12:52:27 -03:00
|
|
|
|
|
|
|
if (start < 0) {
|
|
|
|
start += Py_SIZE(self);
|
|
|
|
if (start < 0)
|
|
|
|
start = 0;
|
|
|
|
}
|
|
|
|
if (stop < 0) {
|
|
|
|
stop += Py_SIZE(self);
|
|
|
|
if (stop < 0)
|
|
|
|
stop = 0;
|
|
|
|
}
|
|
|
|
for (i = start; i < stop && i < Py_SIZE(self); i++) {
|
2019-12-30 15:32:58 -04:00
|
|
|
PyObject *obj = self->ob_item[i];
|
|
|
|
Py_INCREF(obj);
|
|
|
|
int cmp = PyObject_RichCompareBool(obj, value, Py_EQ);
|
|
|
|
Py_DECREF(obj);
|
2010-05-09 12:52:27 -03:00
|
|
|
if (cmp > 0)
|
|
|
|
return PyLong_FromSsize_t(i);
|
|
|
|
else if (cmp < 0)
|
|
|
|
return NULL;
|
|
|
|
}
|
2017-03-11 03:19:20 -04:00
|
|
|
PyErr_Format(PyExc_ValueError, "%R is not in list", value);
|
2010-05-09 12:52:27 -03:00
|
|
|
return NULL;
|
1991-03-06 09:07:53 -04:00
|
|
|
}
|
|
|
|
|
2017-03-11 03:19:20 -04:00
|
|
|
/*[clinic input]
|
|
|
|
list.count
|
|
|
|
|
|
|
|
value: object
|
|
|
|
/
|
|
|
|
|
|
|
|
Return number of occurrences of value.
|
|
|
|
[clinic start generated code]*/
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
static PyObject *
|
2017-03-11 03:19:20 -04:00
|
|
|
list_count(PyListObject *self, PyObject *value)
|
|
|
|
/*[clinic end generated code: output=b1f5d284205ae714 input=3bdc3a5e6f749565]*/
|
1991-10-20 17:20:40 -03:00
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
Py_ssize_t count = 0;
|
|
|
|
Py_ssize_t i;
|
2000-02-24 11:23:03 -04:00
|
|
|
|
2010-05-09 12:52:27 -03:00
|
|
|
for (i = 0; i < Py_SIZE(self); i++) {
|
2019-12-30 15:32:58 -04:00
|
|
|
PyObject *obj = self->ob_item[i];
|
2020-01-22 13:36:54 -04:00
|
|
|
if (obj == value) {
|
|
|
|
count++;
|
|
|
|
continue;
|
|
|
|
}
|
2019-12-30 15:32:58 -04:00
|
|
|
Py_INCREF(obj);
|
|
|
|
int cmp = PyObject_RichCompareBool(obj, value, Py_EQ);
|
|
|
|
Py_DECREF(obj);
|
2010-05-09 12:52:27 -03:00
|
|
|
if (cmp > 0)
|
|
|
|
count++;
|
|
|
|
else if (cmp < 0)
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return PyLong_FromSsize_t(count);
|
1991-10-20 17:20:40 -03:00
|
|
|
}
|
|
|
|
|
2017-03-11 03:19:20 -04:00
|
|
|
/*[clinic input]
|
2024-01-08 20:00:55 -04:00
|
|
|
@critical_section
|
2017-03-11 03:19:20 -04:00
|
|
|
list.remove
|
|
|
|
|
|
|
|
value: object
|
|
|
|
/
|
|
|
|
|
|
|
|
Remove first occurrence of value.
|
|
|
|
|
|
|
|
Raises ValueError if the value is not present.
|
|
|
|
[clinic start generated code]*/
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
static PyObject *
|
2024-01-08 20:00:55 -04:00
|
|
|
list_remove_impl(PyListObject *self, PyObject *value)
|
|
|
|
/*[clinic end generated code: output=b9b76a6633b18778 input=26c813dbb95aa93b]*/
|
1991-03-06 09:07:53 -04:00
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
Py_ssize_t i;
|
2000-02-24 11:23:03 -04:00
|
|
|
|
2010-05-09 12:52:27 -03:00
|
|
|
for (i = 0; i < Py_SIZE(self); i++) {
|
2019-12-30 15:32:58 -04:00
|
|
|
PyObject *obj = self->ob_item[i];
|
|
|
|
Py_INCREF(obj);
|
|
|
|
int cmp = PyObject_RichCompareBool(obj, value, Py_EQ);
|
|
|
|
Py_DECREF(obj);
|
2010-05-09 12:52:27 -03:00
|
|
|
if (cmp > 0) {
|
|
|
|
if (list_ass_slice(self, i, i+1,
|
|
|
|
(PyObject *)NULL) == 0)
|
|
|
|
Py_RETURN_NONE;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
else if (cmp < 0)
|
|
|
|
return NULL;
|
|
|
|
}
|
2023-07-05 19:19:49 -03:00
|
|
|
PyErr_Format(PyExc_ValueError, "%R is not in list", value);
|
2010-05-09 12:52:27 -03:00
|
|
|
return NULL;
|
1991-03-06 09:07:53 -04:00
|
|
|
}
|
|
|
|
|
2000-06-23 11:18:11 -03:00
|
|
|
static int
|
2024-01-02 09:41:32 -04:00
|
|
|
list_traverse(PyObject *self, visitproc visit, void *arg)
|
2000-06-23 11:18:11 -03:00
|
|
|
{
|
2024-01-02 09:41:32 -04:00
|
|
|
PyListObject *o = (PyListObject *)self;
|
2010-05-09 12:52:27 -03:00
|
|
|
Py_ssize_t i;
|
2006-04-21 07:40:58 -03:00
|
|
|
|
2010-05-09 12:52:27 -03:00
|
|
|
for (i = Py_SIZE(o); --i >= 0; )
|
|
|
|
Py_VISIT(o->ob_item[i]);
|
|
|
|
return 0;
|
2000-06-23 11:18:11 -03:00
|
|
|
}
|
|
|
|
|
2001-01-17 18:11:59 -04:00
|
|
|
static PyObject *
|
2024-01-15 20:11:14 -04:00
|
|
|
list_richcompare_impl(PyObject *v, PyObject *w, int op)
|
2001-01-17 18:11:59 -04:00
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
PyListObject *vl, *wl;
|
|
|
|
Py_ssize_t i;
|
|
|
|
|
2011-08-10 22:28:54 -03:00
|
|
|
if (!PyList_Check(v) || !PyList_Check(w))
|
|
|
|
Py_RETURN_NOTIMPLEMENTED;
|
2010-05-09 12:52:27 -03:00
|
|
|
|
|
|
|
vl = (PyListObject *)v;
|
|
|
|
wl = (PyListObject *)w;
|
|
|
|
|
|
|
|
if (Py_SIZE(vl) != Py_SIZE(wl) && (op == Py_EQ || op == Py_NE)) {
|
|
|
|
/* Shortcut: if the lengths differ, the lists differ */
|
|
|
|
if (op == Py_EQ)
|
2017-11-02 07:32:54 -03:00
|
|
|
Py_RETURN_FALSE;
|
2010-05-09 12:52:27 -03:00
|
|
|
else
|
2017-11-02 07:32:54 -03:00
|
|
|
Py_RETURN_TRUE;
|
2010-05-09 12:52:27 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Search for the first index where items are different */
|
|
|
|
for (i = 0; i < Py_SIZE(vl) && i < Py_SIZE(wl); i++) {
|
2019-12-30 21:04:22 -04:00
|
|
|
PyObject *vitem = vl->ob_item[i];
|
|
|
|
PyObject *witem = wl->ob_item[i];
|
2019-12-30 21:58:37 -04:00
|
|
|
if (vitem == witem) {
|
|
|
|
continue;
|
|
|
|
}
|
2019-12-30 21:04:22 -04:00
|
|
|
|
|
|
|
Py_INCREF(vitem);
|
|
|
|
Py_INCREF(witem);
|
2020-02-26 03:00:35 -04:00
|
|
|
int k = PyObject_RichCompareBool(vitem, witem, Py_EQ);
|
2019-12-30 21:04:22 -04:00
|
|
|
Py_DECREF(vitem);
|
|
|
|
Py_DECREF(witem);
|
2010-05-09 12:52:27 -03:00
|
|
|
if (k < 0)
|
|
|
|
return NULL;
|
|
|
|
if (!k)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i >= Py_SIZE(vl) || i >= Py_SIZE(wl)) {
|
|
|
|
/* No more items to compare -- compare sizes */
|
2017-11-02 07:32:54 -03:00
|
|
|
Py_RETURN_RICHCOMPARE(Py_SIZE(vl), Py_SIZE(wl), op);
|
2010-05-09 12:52:27 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* We have an item that differs -- shortcuts for EQ/NE */
|
|
|
|
if (op == Py_EQ) {
|
2017-01-23 03:47:21 -04:00
|
|
|
Py_RETURN_FALSE;
|
2010-05-09 12:52:27 -03:00
|
|
|
}
|
|
|
|
if (op == Py_NE) {
|
2017-01-23 03:47:21 -04:00
|
|
|
Py_RETURN_TRUE;
|
2010-05-09 12:52:27 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Compare the final item again using the proper operator */
|
|
|
|
return PyObject_RichCompare(vl->ob_item[i], wl->ob_item[i], op);
|
2001-01-17 18:11:59 -04:00
|
|
|
}
|
|
|
|
|
2024-01-15 20:11:14 -04:00
|
|
|
static PyObject *
|
|
|
|
list_richcompare(PyObject *v, PyObject *w, int op)
|
|
|
|
{
|
|
|
|
PyObject *ret;
|
|
|
|
Py_BEGIN_CRITICAL_SECTION2(v, w);
|
|
|
|
ret = list_richcompare_impl(v, w, op);
|
|
|
|
Py_END_CRITICAL_SECTION2()
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-03-11 03:19:20 -04:00
|
|
|
/*[clinic input]
|
|
|
|
list.__init__
|
2001-08-02 01:15:00 -03:00
|
|
|
|
2017-03-11 03:19:20 -04:00
|
|
|
iterable: object(c_default="NULL") = ()
|
|
|
|
/
|
|
|
|
|
|
|
|
Built-in mutable sequence.
|
|
|
|
|
|
|
|
If no argument is given, the constructor creates a new empty list.
|
|
|
|
The argument must be an iterable if specified.
|
|
|
|
[clinic start generated code]*/
|
2004-07-29 20:31:29 -03:00
|
|
|
|
2017-03-11 03:19:20 -04:00
|
|
|
static int
|
|
|
|
list___init___impl(PyListObject *self, PyObject *iterable)
|
|
|
|
/*[clinic end generated code: output=0f3c21379d01de48 input=b3f3fe7206af8f6b]*/
|
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
/* Verify list invariants established by PyType_GenericAlloc() */
|
|
|
|
assert(0 <= Py_SIZE(self));
|
|
|
|
assert(Py_SIZE(self) <= self->allocated || self->allocated == -1);
|
|
|
|
assert(self->ob_item != NULL ||
|
|
|
|
self->allocated == 0 || self->allocated == -1);
|
2004-07-29 20:31:29 -03:00
|
|
|
|
2010-05-09 12:52:27 -03:00
|
|
|
/* Empty previous contents */
|
|
|
|
if (self->ob_item != NULL) {
|
2023-11-13 12:14:56 -04:00
|
|
|
list_clear(self);
|
2010-05-09 12:52:27 -03:00
|
|
|
}
|
2017-03-11 03:19:20 -04:00
|
|
|
if (iterable != NULL) {
|
2023-11-13 12:14:56 -04:00
|
|
|
if (list_extend(self, iterable) < 0) {
|
2010-05-09 12:52:27 -03:00
|
|
|
return -1;
|
2023-11-13 12:14:56 -04:00
|
|
|
}
|
2010-05-09 12:52:27 -03:00
|
|
|
}
|
|
|
|
return 0;
|
2001-08-02 01:15:00 -03:00
|
|
|
}
|
|
|
|
|
2020-03-30 09:16:16 -03:00
|
|
|
static PyObject *
|
|
|
|
list_vectorcall(PyObject *type, PyObject * const*args,
|
|
|
|
size_t nargsf, PyObject *kwnames)
|
|
|
|
{
|
|
|
|
if (!_PyArg_NoKwnames("list", kwnames)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
|
|
|
|
if (!_PyArg_CheckPositional("list", nargs, 0, 1)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2022-01-21 18:33:43 -04:00
|
|
|
PyObject *list = PyType_GenericAlloc(_PyType_CAST(type), 0);
|
2020-03-30 09:16:16 -03:00
|
|
|
if (list == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (nargs) {
|
|
|
|
if (list___init___impl((PyListObject *)list, args[0])) {
|
|
|
|
Py_DECREF(list);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-03-11 03:19:20 -04:00
|
|
|
/*[clinic input]
|
|
|
|
list.__sizeof__
|
|
|
|
|
|
|
|
Return the size of the list in memory, in bytes.
|
|
|
|
[clinic start generated code]*/
|
|
|
|
|
2008-06-04 11:18:43 -03:00
|
|
|
static PyObject *
|
2017-03-11 03:19:20 -04:00
|
|
|
list___sizeof___impl(PyListObject *self)
|
|
|
|
/*[clinic end generated code: output=3417541f95f9a53e input=b8030a5d5ce8a187]*/
|
2008-06-04 11:18:43 -03:00
|
|
|
{
|
2022-11-30 12:22:52 -04:00
|
|
|
size_t res = _PyObject_SIZE(Py_TYPE(self));
|
|
|
|
res += (size_t)self->allocated * sizeof(void*);
|
|
|
|
return PyLong_FromSize_t(res);
|
2008-06-04 11:18:43 -03:00
|
|
|
}
|
|
|
|
|
2003-11-07 11:38:09 -04:00
|
|
|
static PyObject *list_iter(PyObject *seq);
|
2024-01-02 09:41:32 -04:00
|
|
|
static PyObject *list_subscript(PyObject*, PyObject*);
|
2003-12-13 07:26:12 -04:00
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
static PyMethodDef list_methods[] = {
|
2024-01-02 09:41:32 -04:00
|
|
|
{"__getitem__", list_subscript, METH_O|METH_COEXIST,
|
2022-09-09 05:37:02 -03:00
|
|
|
PyDoc_STR("__getitem__($self, index, /)\n--\n\nReturn self[index].")},
|
2017-03-11 03:19:20 -04:00
|
|
|
LIST___REVERSED___METHODDEF
|
|
|
|
LIST___SIZEOF___METHODDEF
|
2023-11-13 12:14:56 -04:00
|
|
|
PY_LIST_CLEAR_METHODDEF
|
2017-03-11 03:19:20 -04:00
|
|
|
LIST_COPY_METHODDEF
|
|
|
|
LIST_APPEND_METHODDEF
|
|
|
|
LIST_INSERT_METHODDEF
|
2023-11-13 12:14:56 -04:00
|
|
|
PY_LIST_EXTEND_METHODDEF
|
2017-03-11 03:19:20 -04:00
|
|
|
LIST_POP_METHODDEF
|
|
|
|
LIST_REMOVE_METHODDEF
|
|
|
|
LIST_INDEX_METHODDEF
|
|
|
|
LIST_COUNT_METHODDEF
|
|
|
|
LIST_REVERSE_METHODDEF
|
|
|
|
LIST_SORT_METHODDEF
|
2021-09-19 12:05:30 -03:00
|
|
|
{"__class_getitem__", Py_GenericAlias, METH_O|METH_CLASS, PyDoc_STR("See PEP 585")},
|
2010-05-09 12:52:27 -03:00
|
|
|
{NULL, NULL} /* sentinel */
|
1990-10-14 09:07:46 -03:00
|
|
|
};
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
static PySequenceMethods list_as_sequence = {
|
2024-01-02 09:41:32 -04:00
|
|
|
list_length, /* sq_length */
|
|
|
|
list_concat, /* sq_concat */
|
|
|
|
list_repeat, /* sq_repeat */
|
|
|
|
list_item, /* sq_item */
|
2010-05-09 12:52:27 -03:00
|
|
|
0, /* sq_slice */
|
2024-01-02 09:41:32 -04:00
|
|
|
list_ass_item, /* sq_ass_item */
|
2010-05-09 12:52:27 -03:00
|
|
|
0, /* sq_ass_slice */
|
2024-01-02 09:41:32 -04:00
|
|
|
list_contains, /* sq_contains */
|
|
|
|
list_inplace_concat, /* sq_inplace_concat */
|
|
|
|
list_inplace_repeat, /* sq_inplace_repeat */
|
1990-10-14 09:07:46 -03:00
|
|
|
};
|
|
|
|
|
2002-07-13 00:51:17 -03:00
|
|
|
static PyObject *
|
2024-01-02 09:41:32 -04:00
|
|
|
list_subscript(PyObject* _self, PyObject* item)
|
2002-06-11 07:55:12 -03:00
|
|
|
{
|
2024-01-02 09:41:32 -04:00
|
|
|
PyListObject* self = (PyListObject*)_self;
|
2020-04-07 21:01:56 -03:00
|
|
|
if (_PyIndex_Check(item)) {
|
2010-05-09 12:52:27 -03:00
|
|
|
Py_ssize_t i;
|
|
|
|
i = PyNumber_AsSsize_t(item, PyExc_IndexError);
|
|
|
|
if (i == -1 && PyErr_Occurred())
|
|
|
|
return NULL;
|
|
|
|
if (i < 0)
|
|
|
|
i += PyList_GET_SIZE(self);
|
2024-01-02 09:41:32 -04:00
|
|
|
return list_item((PyObject *)self, i);
|
2010-05-09 12:52:27 -03:00
|
|
|
}
|
|
|
|
else if (PySlice_Check(item)) {
|
2019-09-08 07:15:56 -03:00
|
|
|
Py_ssize_t start, stop, step, slicelength, i;
|
|
|
|
size_t cur;
|
2010-05-09 12:52:27 -03:00
|
|
|
PyObject* result;
|
|
|
|
PyObject* it;
|
|
|
|
PyObject **src, **dest;
|
|
|
|
|
2017-04-08 03:53:51 -03:00
|
|
|
if (PySlice_Unpack(item, &start, &stop, &step) < 0) {
|
2010-05-09 12:52:27 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
2017-04-08 03:53:51 -03:00
|
|
|
slicelength = PySlice_AdjustIndices(Py_SIZE(self), &start, &stop,
|
|
|
|
step);
|
2010-05-09 12:52:27 -03:00
|
|
|
|
|
|
|
if (slicelength <= 0) {
|
|
|
|
return PyList_New(0);
|
|
|
|
}
|
|
|
|
else if (step == 1) {
|
|
|
|
return list_slice(self, start, stop);
|
|
|
|
}
|
|
|
|
else {
|
2018-08-11 10:12:07 -03:00
|
|
|
result = list_new_prealloc(slicelength);
|
2010-05-09 12:52:27 -03:00
|
|
|
if (!result) return NULL;
|
|
|
|
|
|
|
|
src = self->ob_item;
|
|
|
|
dest = ((PyListObject *)result)->ob_item;
|
|
|
|
for (cur = start, i = 0; i < slicelength;
|
2011-09-25 11:34:32 -03:00
|
|
|
cur += (size_t)step, i++) {
|
2022-11-10 17:09:18 -04:00
|
|
|
it = Py_NewRef(src[cur]);
|
2010-05-09 12:52:27 -03:00
|
|
|
dest[i] = it;
|
|
|
|
}
|
2020-02-07 18:18:08 -04:00
|
|
|
Py_SET_SIZE(result, slicelength);
|
2010-05-09 12:52:27 -03:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
PyErr_Format(PyExc_TypeError,
|
2014-08-02 02:30:37 -03:00
|
|
|
"list indices must be integers or slices, not %.200s",
|
2020-02-06 22:04:21 -04:00
|
|
|
Py_TYPE(item)->tp_name);
|
2010-05-09 12:52:27 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
2002-06-11 07:55:12 -03:00
|
|
|
}
|
|
|
|
|
2002-07-18 23:35:45 -03:00
|
|
|
static int
|
2024-01-02 09:41:32 -04:00
|
|
|
list_ass_subscript(PyObject* _self, PyObject* item, PyObject* value)
|
2002-06-11 07:55:12 -03:00
|
|
|
{
|
2024-01-02 09:41:32 -04:00
|
|
|
PyListObject *self = (PyListObject *)_self;
|
2020-04-07 21:01:56 -03:00
|
|
|
if (_PyIndex_Check(item)) {
|
2010-05-09 12:52:27 -03:00
|
|
|
Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
|
|
|
|
if (i == -1 && PyErr_Occurred())
|
|
|
|
return -1;
|
|
|
|
if (i < 0)
|
|
|
|
i += PyList_GET_SIZE(self);
|
2024-01-02 09:41:32 -04:00
|
|
|
return list_ass_item((PyObject *)self, i, value);
|
2010-05-09 12:52:27 -03:00
|
|
|
}
|
|
|
|
else if (PySlice_Check(item)) {
|
|
|
|
Py_ssize_t start, stop, step, slicelength;
|
|
|
|
|
2017-04-08 03:53:51 -03:00
|
|
|
if (PySlice_Unpack(item, &start, &stop, &step) < 0) {
|
2010-05-09 12:52:27 -03:00
|
|
|
return -1;
|
|
|
|
}
|
2017-04-08 03:53:51 -03:00
|
|
|
slicelength = PySlice_AdjustIndices(Py_SIZE(self), &start, &stop,
|
|
|
|
step);
|
2010-05-09 12:52:27 -03:00
|
|
|
|
|
|
|
if (step == 1)
|
|
|
|
return list_ass_slice(self, start, stop, value);
|
|
|
|
|
|
|
|
/* Make sure s[5:2] = [..] inserts at the right place:
|
|
|
|
before 5, not before 2. */
|
|
|
|
if ((step < 0 && start < stop) ||
|
|
|
|
(step > 0 && start > stop))
|
|
|
|
stop = start;
|
|
|
|
|
|
|
|
if (value == NULL) {
|
|
|
|
/* delete slice */
|
|
|
|
PyObject **garbage;
|
|
|
|
size_t cur;
|
|
|
|
Py_ssize_t i;
|
2013-11-21 07:16:35 -04:00
|
|
|
int res;
|
2010-05-09 12:52:27 -03:00
|
|
|
|
|
|
|
if (slicelength <= 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (step < 0) {
|
|
|
|
stop = start + 1;
|
|
|
|
start = stop + step*(slicelength - 1) - 1;
|
|
|
|
step = -step;
|
|
|
|
}
|
|
|
|
|
|
|
|
garbage = (PyObject**)
|
2020-12-01 04:56:42 -04:00
|
|
|
PyMem_Malloc(slicelength*sizeof(PyObject*));
|
2010-05-09 12:52:27 -03:00
|
|
|
if (!garbage) {
|
|
|
|
PyErr_NoMemory();
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* drawing pictures might help understand these for
|
|
|
|
loops. Basically, we memmove the parts of the
|
|
|
|
list that are *not* part of the slice: step-1
|
|
|
|
items for each item that is part of the slice,
|
|
|
|
and then tail end of the list that was not
|
|
|
|
covered by the slice */
|
|
|
|
for (cur = start, i = 0;
|
|
|
|
cur < (size_t)stop;
|
|
|
|
cur += step, i++) {
|
|
|
|
Py_ssize_t lim = step - 1;
|
|
|
|
|
|
|
|
garbage[i] = PyList_GET_ITEM(self, cur);
|
|
|
|
|
|
|
|
if (cur + step >= (size_t)Py_SIZE(self)) {
|
|
|
|
lim = Py_SIZE(self) - cur - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
memmove(self->ob_item + cur - i,
|
|
|
|
self->ob_item + cur + 1,
|
|
|
|
lim * sizeof(PyObject *));
|
|
|
|
}
|
2011-09-25 11:34:32 -03:00
|
|
|
cur = start + (size_t)slicelength * step;
|
2010-05-09 12:52:27 -03:00
|
|
|
if (cur < (size_t)Py_SIZE(self)) {
|
|
|
|
memmove(self->ob_item + cur - slicelength,
|
|
|
|
self->ob_item + cur,
|
|
|
|
(Py_SIZE(self) - cur) *
|
|
|
|
sizeof(PyObject *));
|
|
|
|
}
|
|
|
|
|
2020-02-07 18:18:08 -04:00
|
|
|
Py_SET_SIZE(self, Py_SIZE(self) - slicelength);
|
2013-11-21 07:16:35 -04:00
|
|
|
res = list_resize(self, Py_SIZE(self));
|
2010-05-09 12:52:27 -03:00
|
|
|
|
|
|
|
for (i = 0; i < slicelength; i++) {
|
|
|
|
Py_DECREF(garbage[i]);
|
|
|
|
}
|
2020-12-01 04:56:42 -04:00
|
|
|
PyMem_Free(garbage);
|
2010-05-09 12:52:27 -03:00
|
|
|
|
2013-11-21 07:16:35 -04:00
|
|
|
return res;
|
2010-05-09 12:52:27 -03:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* assign slice */
|
|
|
|
PyObject *ins, *seq;
|
|
|
|
PyObject **garbage, **seqitems, **selfitems;
|
2019-09-08 07:15:56 -03:00
|
|
|
Py_ssize_t i;
|
|
|
|
size_t cur;
|
2010-05-09 12:52:27 -03:00
|
|
|
|
|
|
|
/* protect against a[::-1] = a */
|
|
|
|
if (self == (PyListObject*)value) {
|
|
|
|
seq = list_slice((PyListObject*)value, 0,
|
|
|
|
PyList_GET_SIZE(value));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
seq = PySequence_Fast(value,
|
|
|
|
"must assign iterable "
|
|
|
|
"to extended slice");
|
|
|
|
}
|
|
|
|
if (!seq)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (PySequence_Fast_GET_SIZE(seq) != slicelength) {
|
|
|
|
PyErr_Format(PyExc_ValueError,
|
|
|
|
"attempt to assign sequence of "
|
|
|
|
"size %zd to extended slice of "
|
|
|
|
"size %zd",
|
|
|
|
PySequence_Fast_GET_SIZE(seq),
|
|
|
|
slicelength);
|
|
|
|
Py_DECREF(seq);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!slicelength) {
|
|
|
|
Py_DECREF(seq);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
garbage = (PyObject**)
|
2020-12-01 04:56:42 -04:00
|
|
|
PyMem_Malloc(slicelength*sizeof(PyObject*));
|
2010-05-09 12:52:27 -03:00
|
|
|
if (!garbage) {
|
|
|
|
Py_DECREF(seq);
|
|
|
|
PyErr_NoMemory();
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
selfitems = self->ob_item;
|
|
|
|
seqitems = PySequence_Fast_ITEMS(seq);
|
|
|
|
for (cur = start, i = 0; i < slicelength;
|
2011-09-25 11:34:32 -03:00
|
|
|
cur += (size_t)step, i++) {
|
2010-05-09 12:52:27 -03:00
|
|
|
garbage[i] = selfitems[cur];
|
2022-11-10 17:09:18 -04:00
|
|
|
ins = Py_NewRef(seqitems[i]);
|
2010-05-09 12:52:27 -03:00
|
|
|
selfitems[cur] = ins;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < slicelength; i++) {
|
|
|
|
Py_DECREF(garbage[i]);
|
|
|
|
}
|
|
|
|
|
2020-12-01 04:56:42 -04:00
|
|
|
PyMem_Free(garbage);
|
2010-05-09 12:52:27 -03:00
|
|
|
Py_DECREF(seq);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
PyErr_Format(PyExc_TypeError,
|
2014-08-02 02:30:37 -03:00
|
|
|
"list indices must be integers or slices, not %.200s",
|
2020-02-06 22:04:21 -04:00
|
|
|
Py_TYPE(item)->tp_name);
|
2010-05-09 12:52:27 -03:00
|
|
|
return -1;
|
|
|
|
}
|
2002-06-11 07:55:12 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
static PyMappingMethods list_as_mapping = {
|
2024-01-02 09:41:32 -04:00
|
|
|
list_length,
|
|
|
|
list_subscript,
|
|
|
|
list_ass_subscript
|
2002-06-11 07:55:12 -03:00
|
|
|
};
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
PyTypeObject PyList_Type = {
|
2010-05-09 12:52:27 -03:00
|
|
|
PyVarObject_HEAD_INIT(&PyType_Type, 0)
|
|
|
|
"list",
|
|
|
|
sizeof(PyListObject),
|
|
|
|
0,
|
2024-01-02 09:41:32 -04:00
|
|
|
list_dealloc, /* tp_dealloc */
|
2019-05-30 23:13:39 -03:00
|
|
|
0, /* tp_vectorcall_offset */
|
2010-05-09 12:52:27 -03:00
|
|
|
0, /* tp_getattr */
|
|
|
|
0, /* tp_setattr */
|
2019-05-30 23:13:39 -03:00
|
|
|
0, /* tp_as_async */
|
2024-01-02 09:41:32 -04:00
|
|
|
list_repr, /* tp_repr */
|
2010-05-09 12:52:27 -03:00
|
|
|
0, /* tp_as_number */
|
|
|
|
&list_as_sequence, /* tp_as_sequence */
|
|
|
|
&list_as_mapping, /* tp_as_mapping */
|
2010-10-18 04:32:48 -03:00
|
|
|
PyObject_HashNotImplemented, /* tp_hash */
|
2010-05-09 12:52:27 -03:00
|
|
|
0, /* tp_call */
|
|
|
|
0, /* tp_str */
|
|
|
|
PyObject_GenericGetAttr, /* tp_getattro */
|
|
|
|
0, /* tp_setattro */
|
|
|
|
0, /* tp_as_buffer */
|
|
|
|
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
|
2021-02-26 18:51:55 -04:00
|
|
|
Py_TPFLAGS_BASETYPE | Py_TPFLAGS_LIST_SUBCLASS |
|
2021-04-30 05:50:28 -03:00
|
|
|
_Py_TPFLAGS_MATCH_SELF | Py_TPFLAGS_SEQUENCE, /* tp_flags */
|
2017-03-11 03:19:20 -04:00
|
|
|
list___init____doc__, /* tp_doc */
|
2024-01-02 09:41:32 -04:00
|
|
|
list_traverse, /* tp_traverse */
|
|
|
|
list_clear_slot, /* tp_clear */
|
2010-05-09 12:52:27 -03:00
|
|
|
list_richcompare, /* tp_richcompare */
|
|
|
|
0, /* tp_weaklistoffset */
|
|
|
|
list_iter, /* tp_iter */
|
|
|
|
0, /* tp_iternext */
|
|
|
|
list_methods, /* tp_methods */
|
|
|
|
0, /* tp_members */
|
|
|
|
0, /* tp_getset */
|
|
|
|
0, /* tp_base */
|
|
|
|
0, /* tp_dict */
|
|
|
|
0, /* tp_descr_get */
|
|
|
|
0, /* tp_descr_set */
|
|
|
|
0, /* tp_dictoffset */
|
2017-03-11 03:19:20 -04:00
|
|
|
(initproc)list___init__, /* tp_init */
|
2010-05-09 12:52:27 -03:00
|
|
|
PyType_GenericAlloc, /* tp_alloc */
|
|
|
|
PyType_GenericNew, /* tp_new */
|
|
|
|
PyObject_GC_Del, /* tp_free */
|
2020-03-30 09:16:16 -03:00
|
|
|
.tp_vectorcall = list_vectorcall,
|
1990-10-14 09:07:46 -03:00
|
|
|
};
|
1998-06-16 12:18:28 -03:00
|
|
|
|
2002-05-31 18:40:38 -03:00
|
|
|
/*********************** List Iterator **************************/
|
|
|
|
|
2024-01-02 09:41:32 -04:00
|
|
|
static void listiter_dealloc(PyObject *);
|
|
|
|
static int listiter_traverse(PyObject *, visitproc, void *);
|
|
|
|
static PyObject *listiter_next(PyObject *);
|
|
|
|
static PyObject *listiter_len(PyObject *, PyObject *);
|
2012-04-03 07:49:41 -03:00
|
|
|
static PyObject *listiter_reduce_general(void *_it, int forward);
|
2024-01-02 09:41:32 -04:00
|
|
|
static PyObject *listiter_reduce(PyObject *, PyObject *);
|
|
|
|
static PyObject *listiter_setstate(PyObject *, PyObject *state);
|
2006-04-21 07:40:58 -03:00
|
|
|
|
|
|
|
PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
|
2012-04-03 07:49:41 -03:00
|
|
|
PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
|
|
|
|
PyDoc_STRVAR(setstate_doc, "Set state information for unpickling.");
|
2006-04-21 07:40:58 -03:00
|
|
|
|
|
|
|
static PyMethodDef listiter_methods[] = {
|
2024-01-02 09:41:32 -04:00
|
|
|
{"__length_hint__", listiter_len, METH_NOARGS, length_hint_doc},
|
|
|
|
{"__reduce__", listiter_reduce, METH_NOARGS, reduce_doc},
|
|
|
|
{"__setstate__", listiter_setstate, METH_O, setstate_doc},
|
2010-05-09 12:52:27 -03:00
|
|
|
{NULL, NULL} /* sentinel */
|
2006-04-21 07:40:58 -03:00
|
|
|
};
|
|
|
|
|
|
|
|
PyTypeObject PyListIter_Type = {
|
2010-05-09 12:52:27 -03:00
|
|
|
PyVarObject_HEAD_INIT(&PyType_Type, 0)
|
|
|
|
"list_iterator", /* tp_name */
|
2022-06-21 07:19:26 -03:00
|
|
|
sizeof(_PyListIterObject), /* tp_basicsize */
|
2010-05-09 12:52:27 -03:00
|
|
|
0, /* tp_itemsize */
|
|
|
|
/* methods */
|
2024-01-02 09:41:32 -04:00
|
|
|
listiter_dealloc, /* tp_dealloc */
|
2019-05-30 23:13:39 -03:00
|
|
|
0, /* tp_vectorcall_offset */
|
2010-05-09 12:52:27 -03:00
|
|
|
0, /* tp_getattr */
|
|
|
|
0, /* tp_setattr */
|
2019-05-30 23:13:39 -03:00
|
|
|
0, /* tp_as_async */
|
2010-05-09 12:52:27 -03:00
|
|
|
0, /* tp_repr */
|
|
|
|
0, /* tp_as_number */
|
|
|
|
0, /* tp_as_sequence */
|
|
|
|
0, /* tp_as_mapping */
|
|
|
|
0, /* tp_hash */
|
|
|
|
0, /* tp_call */
|
|
|
|
0, /* tp_str */
|
|
|
|
PyObject_GenericGetAttr, /* tp_getattro */
|
|
|
|
0, /* tp_setattro */
|
|
|
|
0, /* tp_as_buffer */
|
|
|
|
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
|
|
|
|
0, /* tp_doc */
|
2024-01-02 09:41:32 -04:00
|
|
|
listiter_traverse, /* tp_traverse */
|
2010-05-09 12:52:27 -03:00
|
|
|
0, /* tp_clear */
|
|
|
|
0, /* tp_richcompare */
|
|
|
|
0, /* tp_weaklistoffset */
|
|
|
|
PyObject_SelfIter, /* tp_iter */
|
2024-01-02 09:41:32 -04:00
|
|
|
listiter_next, /* tp_iternext */
|
2010-05-09 12:52:27 -03:00
|
|
|
listiter_methods, /* tp_methods */
|
|
|
|
0, /* tp_members */
|
2006-04-21 07:40:58 -03:00
|
|
|
};
|
|
|
|
|
2002-05-31 18:40:38 -03:00
|
|
|
|
2002-07-16 12:56:52 -03:00
|
|
|
static PyObject *
|
2002-05-31 18:40:38 -03:00
|
|
|
list_iter(PyObject *seq)
|
|
|
|
{
|
2022-06-21 07:19:26 -03:00
|
|
|
_PyListIterObject *it;
|
2002-07-16 17:10:23 -03:00
|
|
|
|
2010-05-09 12:52:27 -03:00
|
|
|
if (!PyList_Check(seq)) {
|
|
|
|
PyErr_BadInternalCall();
|
|
|
|
return NULL;
|
|
|
|
}
|
2022-06-21 07:19:26 -03:00
|
|
|
it = PyObject_GC_New(_PyListIterObject, &PyListIter_Type);
|
2010-05-09 12:52:27 -03:00
|
|
|
if (it == NULL)
|
|
|
|
return NULL;
|
|
|
|
it->it_index = 0;
|
2022-11-10 17:09:18 -04:00
|
|
|
it->it_seq = (PyListObject *)Py_NewRef(seq);
|
2010-05-09 12:52:27 -03:00
|
|
|
_PyObject_GC_TRACK(it);
|
|
|
|
return (PyObject *)it;
|
2002-05-31 18:40:38 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2024-01-02 09:41:32 -04:00
|
|
|
listiter_dealloc(PyObject *self)
|
2002-05-31 18:40:38 -03:00
|
|
|
{
|
2024-01-02 09:41:32 -04:00
|
|
|
_PyListIterObject *it = (_PyListIterObject *)self;
|
2010-05-09 12:52:27 -03:00
|
|
|
_PyObject_GC_UNTRACK(it);
|
|
|
|
Py_XDECREF(it->it_seq);
|
|
|
|
PyObject_GC_Del(it);
|
2002-05-31 18:40:38 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2024-01-02 09:41:32 -04:00
|
|
|
listiter_traverse(PyObject *it, visitproc visit, void *arg)
|
2002-05-31 18:40:38 -03:00
|
|
|
{
|
2024-01-02 09:41:32 -04:00
|
|
|
Py_VISIT(((_PyListIterObject *)it)->it_seq);
|
2010-05-09 12:52:27 -03:00
|
|
|
return 0;
|
2002-05-31 18:40:38 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *
|
2024-01-02 09:41:32 -04:00
|
|
|
listiter_next(PyObject *self)
|
2002-05-31 18:40:38 -03:00
|
|
|
{
|
2024-01-02 09:41:32 -04:00
|
|
|
_PyListIterObject *it = (_PyListIterObject *)self;
|
2010-05-09 12:52:27 -03:00
|
|
|
PyListObject *seq;
|
|
|
|
PyObject *item;
|
2002-05-31 18:40:38 -03:00
|
|
|
|
2010-05-09 12:52:27 -03:00
|
|
|
assert(it != NULL);
|
|
|
|
seq = it->it_seq;
|
|
|
|
if (seq == NULL)
|
|
|
|
return NULL;
|
|
|
|
assert(PyList_Check(seq));
|
2002-05-31 18:40:38 -03:00
|
|
|
|
2010-05-09 12:52:27 -03:00
|
|
|
if (it->it_index < PyList_GET_SIZE(seq)) {
|
|
|
|
item = PyList_GET_ITEM(seq, it->it_index);
|
|
|
|
++it->it_index;
|
2022-11-10 17:09:18 -04:00
|
|
|
return Py_NewRef(item);
|
2010-05-09 12:52:27 -03:00
|
|
|
}
|
2002-07-16 17:07:32 -03:00
|
|
|
|
2010-05-09 12:52:27 -03:00
|
|
|
it->it_seq = NULL;
|
Issue #26494: Fixed crash on iterating exhausting iterators.
Affected classes are generic sequence iterators, iterators of str, bytes,
bytearray, list, tuple, set, frozenset, dict, OrderedDict, corresponding
views and os.scandir() iterator.
2016-03-30 14:40:02 -03:00
|
|
|
Py_DECREF(seq);
|
2010-05-09 12:52:27 -03:00
|
|
|
return NULL;
|
2002-05-31 18:40:38 -03:00
|
|
|
}
|
|
|
|
|
2005-09-24 18:23:05 -03:00
|
|
|
static PyObject *
|
2024-01-02 09:41:32 -04:00
|
|
|
listiter_len(PyObject *self, PyObject *Py_UNUSED(ignored))
|
2004-03-18 18:43:10 -04:00
|
|
|
{
|
2024-01-02 09:41:32 -04:00
|
|
|
_PyListIterObject *it = (_PyListIterObject *)self;
|
2010-05-09 12:52:27 -03:00
|
|
|
Py_ssize_t len;
|
|
|
|
if (it->it_seq) {
|
|
|
|
len = PyList_GET_SIZE(it->it_seq) - it->it_index;
|
|
|
|
if (len >= 0)
|
|
|
|
return PyLong_FromSsize_t(len);
|
|
|
|
}
|
|
|
|
return PyLong_FromLong(0);
|
2004-03-18 18:43:10 -04:00
|
|
|
}
|
2012-04-03 07:49:41 -03:00
|
|
|
|
|
|
|
static PyObject *
|
2024-01-02 09:41:32 -04:00
|
|
|
listiter_reduce(PyObject *it, PyObject *Py_UNUSED(ignored))
|
2012-04-03 07:49:41 -03:00
|
|
|
{
|
|
|
|
return listiter_reduce_general(it, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *
|
2024-01-02 09:41:32 -04:00
|
|
|
listiter_setstate(PyObject *self, PyObject *state)
|
2012-04-03 07:49:41 -03:00
|
|
|
{
|
2024-01-02 09:41:32 -04:00
|
|
|
_PyListIterObject *it = (_PyListIterObject *)self;
|
2013-06-24 18:59:24 -03:00
|
|
|
Py_ssize_t index = PyLong_AsSsize_t(state);
|
2012-04-03 07:49:41 -03:00
|
|
|
if (index == -1 && PyErr_Occurred())
|
|
|
|
return NULL;
|
|
|
|
if (it->it_seq != NULL) {
|
|
|
|
if (index < 0)
|
|
|
|
index = 0;
|
2014-03-05 09:47:57 -04:00
|
|
|
else if (index > PyList_GET_SIZE(it->it_seq))
|
|
|
|
index = PyList_GET_SIZE(it->it_seq); /* iterator exhausted */
|
2012-04-03 07:49:41 -03:00
|
|
|
it->it_index = index;
|
|
|
|
}
|
|
|
|
Py_RETURN_NONE;
|
|
|
|
}
|
|
|
|
|
2006-04-21 07:40:58 -03:00
|
|
|
/*********************** List Reverse Iterator **************************/
|
2004-03-18 18:43:10 -04:00
|
|
|
|
2006-04-21 07:40:58 -03:00
|
|
|
typedef struct {
|
2010-05-09 12:52:27 -03:00
|
|
|
PyObject_HEAD
|
|
|
|
Py_ssize_t it_index;
|
|
|
|
PyListObject *it_seq; /* Set to NULL when iterator is exhausted */
|
2006-04-21 07:40:58 -03:00
|
|
|
} listreviterobject;
|
2005-09-24 18:23:05 -03:00
|
|
|
|
2024-01-02 09:41:32 -04:00
|
|
|
static void listreviter_dealloc(PyObject *);
|
|
|
|
static int listreviter_traverse(PyObject *, visitproc, void *);
|
|
|
|
static PyObject *listreviter_next(PyObject *);
|
|
|
|
static PyObject *listreviter_len(PyObject *, PyObject *);
|
|
|
|
static PyObject *listreviter_reduce(PyObject *, PyObject *);
|
|
|
|
static PyObject *listreviter_setstate(PyObject *, PyObject *);
|
2006-04-21 07:40:58 -03:00
|
|
|
|
2008-12-02 17:33:45 -04:00
|
|
|
static PyMethodDef listreviter_methods[] = {
|
2024-01-02 09:41:32 -04:00
|
|
|
{"__length_hint__", listreviter_len, METH_NOARGS, length_hint_doc},
|
|
|
|
{"__reduce__", listreviter_reduce, METH_NOARGS, reduce_doc},
|
|
|
|
{"__setstate__", listreviter_setstate, METH_O, setstate_doc},
|
2010-05-09 12:52:27 -03:00
|
|
|
{NULL, NULL} /* sentinel */
|
2004-03-18 18:43:10 -04:00
|
|
|
};
|
|
|
|
|
2006-04-21 07:40:58 -03:00
|
|
|
PyTypeObject PyListRevIter_Type = {
|
2010-05-09 12:52:27 -03:00
|
|
|
PyVarObject_HEAD_INIT(&PyType_Type, 0)
|
|
|
|
"list_reverseiterator", /* tp_name */
|
|
|
|
sizeof(listreviterobject), /* tp_basicsize */
|
|
|
|
0, /* tp_itemsize */
|
|
|
|
/* methods */
|
2024-01-02 09:41:32 -04:00
|
|
|
listreviter_dealloc, /* tp_dealloc */
|
2019-05-30 23:13:39 -03:00
|
|
|
0, /* tp_vectorcall_offset */
|
2010-05-09 12:52:27 -03:00
|
|
|
0, /* tp_getattr */
|
|
|
|
0, /* tp_setattr */
|
2019-05-30 23:13:39 -03:00
|
|
|
0, /* tp_as_async */
|
2010-05-09 12:52:27 -03:00
|
|
|
0, /* tp_repr */
|
|
|
|
0, /* tp_as_number */
|
|
|
|
0, /* tp_as_sequence */
|
|
|
|
0, /* tp_as_mapping */
|
|
|
|
0, /* tp_hash */
|
|
|
|
0, /* tp_call */
|
|
|
|
0, /* tp_str */
|
|
|
|
PyObject_GenericGetAttr, /* tp_getattro */
|
|
|
|
0, /* tp_setattro */
|
|
|
|
0, /* tp_as_buffer */
|
|
|
|
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
|
|
|
|
0, /* tp_doc */
|
2024-01-02 09:41:32 -04:00
|
|
|
listreviter_traverse, /* tp_traverse */
|
2010-05-09 12:52:27 -03:00
|
|
|
0, /* tp_clear */
|
|
|
|
0, /* tp_richcompare */
|
|
|
|
0, /* tp_weaklistoffset */
|
|
|
|
PyObject_SelfIter, /* tp_iter */
|
2024-01-02 09:41:32 -04:00
|
|
|
listreviter_next, /* tp_iternext */
|
2010-05-09 12:52:27 -03:00
|
|
|
listreviter_methods, /* tp_methods */
|
|
|
|
0,
|
2002-05-31 18:40:38 -03:00
|
|
|
};
|
2003-11-07 11:38:09 -04:00
|
|
|
|
2017-03-11 03:19:20 -04:00
|
|
|
/*[clinic input]
|
|
|
|
list.__reversed__
|
|
|
|
|
|
|
|
Return a reverse iterator over the list.
|
|
|
|
[clinic start generated code]*/
|
|
|
|
|
2003-11-07 11:38:09 -04:00
|
|
|
static PyObject *
|
2017-03-11 03:19:20 -04:00
|
|
|
list___reversed___impl(PyListObject *self)
|
|
|
|
/*[clinic end generated code: output=b166f073208c888c input=eadb6e17f8a6a280]*/
|
2003-11-07 11:38:09 -04:00
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
listreviterobject *it;
|
2003-11-07 11:38:09 -04:00
|
|
|
|
2010-05-09 12:52:27 -03:00
|
|
|
it = PyObject_GC_New(listreviterobject, &PyListRevIter_Type);
|
|
|
|
if (it == NULL)
|
|
|
|
return NULL;
|
2017-03-11 03:19:20 -04:00
|
|
|
assert(PyList_Check(self));
|
|
|
|
it->it_index = PyList_GET_SIZE(self) - 1;
|
2022-11-10 17:09:18 -04:00
|
|
|
it->it_seq = (PyListObject*)Py_NewRef(self);
|
2010-05-09 12:52:27 -03:00
|
|
|
PyObject_GC_Track(it);
|
|
|
|
return (PyObject *)it;
|
2003-11-07 11:38:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2024-01-02 09:41:32 -04:00
|
|
|
listreviter_dealloc(PyObject *self)
|
2003-11-07 11:38:09 -04:00
|
|
|
{
|
2024-01-02 09:41:32 -04:00
|
|
|
listreviterobject *it = (listreviterobject *)self;
|
2010-05-09 12:52:27 -03:00
|
|
|
PyObject_GC_UnTrack(it);
|
|
|
|
Py_XDECREF(it->it_seq);
|
|
|
|
PyObject_GC_Del(it);
|
2003-11-07 11:38:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2024-01-02 09:41:32 -04:00
|
|
|
listreviter_traverse(PyObject *it, visitproc visit, void *arg)
|
2003-11-07 11:38:09 -04:00
|
|
|
{
|
2024-01-02 09:41:32 -04:00
|
|
|
Py_VISIT(((listreviterobject *)it)->it_seq);
|
2010-05-09 12:52:27 -03:00
|
|
|
return 0;
|
2003-11-07 11:38:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *
|
2024-01-02 09:41:32 -04:00
|
|
|
listreviter_next(PyObject *self)
|
2003-11-07 11:38:09 -04:00
|
|
|
{
|
2024-01-02 09:41:32 -04:00
|
|
|
listreviterobject *it = (listreviterobject *)self;
|
2010-05-09 12:52:27 -03:00
|
|
|
PyObject *item;
|
Issue #26494: Fixed crash on iterating exhausting iterators.
Affected classes are generic sequence iterators, iterators of str, bytes,
bytearray, list, tuple, set, frozenset, dict, OrderedDict, corresponding
views and os.scandir() iterator.
2016-03-30 14:40:02 -03:00
|
|
|
Py_ssize_t index;
|
|
|
|
PyListObject *seq;
|
|
|
|
|
|
|
|
assert(it != NULL);
|
|
|
|
seq = it->it_seq;
|
|
|
|
if (seq == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
assert(PyList_Check(seq));
|
2003-11-07 11:38:09 -04:00
|
|
|
|
Issue #26494: Fixed crash on iterating exhausting iterators.
Affected classes are generic sequence iterators, iterators of str, bytes,
bytearray, list, tuple, set, frozenset, dict, OrderedDict, corresponding
views and os.scandir() iterator.
2016-03-30 14:40:02 -03:00
|
|
|
index = it->it_index;
|
2010-05-09 12:52:27 -03:00
|
|
|
if (index>=0 && index < PyList_GET_SIZE(seq)) {
|
|
|
|
item = PyList_GET_ITEM(seq, index);
|
|
|
|
it->it_index--;
|
2022-11-10 17:09:18 -04:00
|
|
|
return Py_NewRef(item);
|
2010-05-09 12:52:27 -03:00
|
|
|
}
|
|
|
|
it->it_index = -1;
|
Issue #26494: Fixed crash on iterating exhausting iterators.
Affected classes are generic sequence iterators, iterators of str, bytes,
bytearray, list, tuple, set, frozenset, dict, OrderedDict, corresponding
views and os.scandir() iterator.
2016-03-30 14:40:02 -03:00
|
|
|
it->it_seq = NULL;
|
|
|
|
Py_DECREF(seq);
|
2010-05-09 12:52:27 -03:00
|
|
|
return NULL;
|
2003-11-07 11:38:09 -04:00
|
|
|
}
|
|
|
|
|
2008-12-02 17:33:45 -04:00
|
|
|
static PyObject *
|
2024-01-02 09:41:32 -04:00
|
|
|
listreviter_len(PyObject *self, PyObject *Py_UNUSED(ignored))
|
2004-03-10 06:10:42 -04:00
|
|
|
{
|
2024-01-02 09:41:32 -04:00
|
|
|
listreviterobject *it = (listreviterobject *)self;
|
2010-05-09 12:52:27 -03:00
|
|
|
Py_ssize_t len = it->it_index + 1;
|
|
|
|
if (it->it_seq == NULL || PyList_GET_SIZE(it->it_seq) < len)
|
|
|
|
len = 0;
|
|
|
|
return PyLong_FromSsize_t(len);
|
2004-03-10 06:10:42 -04:00
|
|
|
}
|
2012-04-03 07:49:41 -03:00
|
|
|
|
|
|
|
static PyObject *
|
2024-01-02 09:41:32 -04:00
|
|
|
listreviter_reduce(PyObject *it, PyObject *Py_UNUSED(ignored))
|
2012-04-03 07:49:41 -03:00
|
|
|
{
|
|
|
|
return listiter_reduce_general(it, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *
|
2024-01-02 09:41:32 -04:00
|
|
|
listreviter_setstate(PyObject *self, PyObject *state)
|
2012-04-03 07:49:41 -03:00
|
|
|
{
|
2024-01-02 09:41:32 -04:00
|
|
|
listreviterobject *it = (listreviterobject *)self;
|
2012-04-03 07:49:41 -03:00
|
|
|
Py_ssize_t index = PyLong_AsSsize_t(state);
|
|
|
|
if (index == -1 && PyErr_Occurred())
|
|
|
|
return NULL;
|
|
|
|
if (it->it_seq != NULL) {
|
|
|
|
if (index < -1)
|
|
|
|
index = -1;
|
|
|
|
else if (index > PyList_GET_SIZE(it->it_seq) - 1)
|
|
|
|
index = PyList_GET_SIZE(it->it_seq) - 1;
|
|
|
|
it->it_index = index;
|
|
|
|
}
|
|
|
|
Py_RETURN_NONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* common pickling support */
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
listiter_reduce_general(void *_it, int forward)
|
|
|
|
{
|
|
|
|
PyObject *list;
|
|
|
|
|
2023-02-24 19:02:04 -04:00
|
|
|
/* _PyEval_GetBuiltin can invoke arbitrary code,
|
|
|
|
* call must be before access of iterator pointers.
|
|
|
|
* see issue #101765 */
|
|
|
|
|
2012-04-03 07:49:41 -03:00
|
|
|
/* the objects are not the same, index is of different types! */
|
|
|
|
if (forward) {
|
2023-02-24 19:02:04 -04:00
|
|
|
PyObject *iter = _PyEval_GetBuiltin(&_Py_ID(iter));
|
2023-02-25 20:01:58 -04:00
|
|
|
if (!iter) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2022-06-21 07:19:26 -03:00
|
|
|
_PyListIterObject *it = (_PyListIterObject *)_it;
|
2022-02-08 16:39:07 -04:00
|
|
|
if (it->it_seq) {
|
2023-02-24 19:02:04 -04:00
|
|
|
return Py_BuildValue("N(O)n", iter, it->it_seq, it->it_index);
|
2022-02-08 16:39:07 -04:00
|
|
|
}
|
2023-02-25 20:01:58 -04:00
|
|
|
Py_DECREF(iter);
|
2012-04-03 07:49:41 -03:00
|
|
|
} else {
|
2023-02-24 19:02:04 -04:00
|
|
|
PyObject *reversed = _PyEval_GetBuiltin(&_Py_ID(reversed));
|
2023-02-25 20:01:58 -04:00
|
|
|
if (!reversed) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2012-04-03 07:49:41 -03:00
|
|
|
listreviterobject *it = (listreviterobject *)_it;
|
2022-02-08 16:39:07 -04:00
|
|
|
if (it->it_seq) {
|
2023-02-24 19:02:04 -04:00
|
|
|
return Py_BuildValue("N(O)n", reversed, it->it_seq, it->it_index);
|
2022-02-08 16:39:07 -04:00
|
|
|
}
|
2023-02-25 20:01:58 -04:00
|
|
|
Py_DECREF(reversed);
|
2012-04-03 07:49:41 -03:00
|
|
|
}
|
|
|
|
/* empty iterator, create an empty list */
|
|
|
|
list = PyList_New(0);
|
|
|
|
if (list == NULL)
|
|
|
|
return NULL;
|
2022-02-08 16:39:07 -04:00
|
|
|
return Py_BuildValue("N(N)", _PyEval_GetBuiltin(&_Py_ID(iter)), list);
|
2012-04-03 07:49:41 -03:00
|
|
|
}
|