| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1 | /* List object implementation */ | 
 | 2 |  | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3 | #include "Python.h" | 
 | 4 |  | 
| Guido van Rossum | 6cd2fe0 | 1994-08-29 12:45:32 +0000 | [diff] [blame] | 5 | #ifdef STDC_HEADERS | 
 | 6 | #include <stddef.h> | 
 | 7 | #else | 
 | 8 | #include <sys/types.h>		/* For size_t */ | 
 | 9 | #endif | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 10 |  | 
| Guido van Rossum | a46d51d | 1995-01-26 22:59:43 +0000 | [diff] [blame] | 11 | static int | 
| Raymond Hettinger | d4ff741 | 2004-03-15 09:01:31 +0000 | [diff] [blame] | 12 | list_resize(PyListObject *self, int newsize) | 
| Guido van Rossum | a46d51d | 1995-01-26 22:59:43 +0000 | [diff] [blame] | 13 | { | 
| Raymond Hettinger | 4bb9540 | 2004-02-13 11:36:39 +0000 | [diff] [blame] | 14 | 	PyObject **items; | 
 | 15 | 	size_t _new_size; | 
| Tim Peters | 65b8b84 | 2001-05-26 05:28:40 +0000 | [diff] [blame] | 16 |  | 
| Raymond Hettinger | 4bb9540 | 2004-02-13 11:36:39 +0000 | [diff] [blame] | 17 | 	/* Bypass realloc() when a previous overallocation is large enough | 
 | 18 | 	   to accommodate the newsize.  If the newsize is 16 smaller than the | 
 | 19 | 	   current size, then proceed with the realloc() to shrink the list. | 
 | 20 | 	*/ | 
 | 21 |  | 
 | 22 | 	if (self->allocated >= newsize && | 
 | 23 | 	    self->ob_size < newsize + 16 && | 
 | 24 | 	    self->ob_item != NULL) { | 
 | 25 | 		self->ob_size = newsize; | 
 | 26 | 		return 0; | 
 | 27 | 	} | 
 | 28 |  | 
 | 29 | 	/* This over-allocates proportional to the list size, making room | 
| Tim Peters | 65b8b84 | 2001-05-26 05:28:40 +0000 | [diff] [blame] | 30 | 	 * for additional growth.  The over-allocation is mild, but is | 
 | 31 | 	 * enough to give linear-time amortized behavior over a long | 
 | 32 | 	 * sequence of appends() in the presence of a poorly-performing | 
| Raymond Hettinger | ab517d2 | 2004-02-14 18:34:46 +0000 | [diff] [blame] | 33 | 	 * system realloc(). | 
 | 34 | 	 * The growth pattern is:  0, 4, 8, 16, 25, 35, 46, 58, 72, 88, ... | 
| Tim Peters | 65b8b84 | 2001-05-26 05:28:40 +0000 | [diff] [blame] | 35 | 	 */ | 
| Raymond Hettinger | d4ff741 | 2004-03-15 09:01:31 +0000 | [diff] [blame] | 36 | 	_new_size = (newsize >> 3) + (self->ob_size < 8 ? 3 : 6) + newsize; | 
| Raymond Hettinger | 4bb9540 | 2004-02-13 11:36:39 +0000 | [diff] [blame] | 37 | 	items = self->ob_item; | 
 | 38 | 	if (_new_size <= ((~(size_t)0) / sizeof(PyObject *))) | 
 | 39 | 		PyMem_RESIZE(items, PyObject *, _new_size); | 
 | 40 | 	else | 
 | 41 | 		items = NULL; | 
 | 42 | 	if (items == NULL) { | 
 | 43 | 		PyErr_NoMemory(); | 
 | 44 | 		return -1; | 
 | 45 | 	} | 
 | 46 | 	self->ob_item = items; | 
 | 47 | 	self->ob_size = newsize; | 
 | 48 | 	self->allocated = _new_size; | 
 | 49 | 	return 0; | 
 | 50 | } | 
| Guido van Rossum | a46d51d | 1995-01-26 22:59:43 +0000 | [diff] [blame] | 51 |  | 
| Raymond Hettinger | 0468e41 | 2004-05-05 05:37:53 +0000 | [diff] [blame] | 52 | /* Empty list reuse scheme to save calls to malloc and free */ | 
 | 53 | #define MAXFREELISTS 80 | 
 | 54 | static PyListObject *free_lists[MAXFREELISTS]; | 
 | 55 | static int num_free_lists = 0; | 
 | 56 |  | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 57 | PyObject * | 
| Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 58 | PyList_New(int size) | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 59 | { | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 60 | 	PyListObject *op; | 
| Guido van Rossum | 6cd2fe0 | 1994-08-29 12:45:32 +0000 | [diff] [blame] | 61 | 	size_t nbytes; | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 62 | 	if (size < 0) { | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 63 | 		PyErr_BadInternalCall(); | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 64 | 		return NULL; | 
 | 65 | 	} | 
| Tim Peters | 7049d81 | 2004-01-18 20:31:02 +0000 | [diff] [blame] | 66 | 	nbytes = size * sizeof(PyObject *); | 
| Guido van Rossum | 1e28e5e | 1992-08-19 16:46:30 +0000 | [diff] [blame] | 67 | 	/* Check for overflow */ | 
| Raymond Hettinger | fdfe618 | 2004-05-05 06:28:16 +0000 | [diff] [blame] | 68 | 	if (nbytes / sizeof(PyObject *) != (size_t)size) | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 69 | 		return PyErr_NoMemory(); | 
| Raymond Hettinger | 0468e41 | 2004-05-05 05:37:53 +0000 | [diff] [blame] | 70 | 	if (num_free_lists) { | 
 | 71 | 		num_free_lists--; | 
 | 72 | 		op = free_lists[num_free_lists]; | 
 | 73 | 		_Py_NewReference((PyObject *)op); | 
 | 74 | 	} else { | 
 | 75 | 		op = PyObject_GC_New(PyListObject, &PyList_Type); | 
 | 76 | 		if (op == NULL) | 
 | 77 | 			return NULL; | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 78 | 	} | 
| Raymond Hettinger | fdfe618 | 2004-05-05 06:28:16 +0000 | [diff] [blame] | 79 | 	if (size <= 0) | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 80 | 		op->ob_item = NULL; | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 81 | 	else { | 
| Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 82 | 		op->ob_item = (PyObject **) PyMem_MALLOC(nbytes); | 
| Raymond Hettinger | fdfe618 | 2004-05-05 06:28:16 +0000 | [diff] [blame] | 83 | 		if (op->ob_item == NULL) | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 84 | 			return PyErr_NoMemory(); | 
| Tim Peters | 7049d81 | 2004-01-18 20:31:02 +0000 | [diff] [blame] | 85 | 		memset(op->ob_item, 0, sizeof(*op->ob_item) * size); | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 86 | 	} | 
| Neil Schemenauer | e83c00e | 2001-08-29 23:54:21 +0000 | [diff] [blame] | 87 | 	op->ob_size = size; | 
| Raymond Hettinger | 4bb9540 | 2004-02-13 11:36:39 +0000 | [diff] [blame] | 88 | 	op->allocated = size; | 
| Neil Schemenauer | e83c00e | 2001-08-29 23:54:21 +0000 | [diff] [blame] | 89 | 	_PyObject_GC_TRACK(op); | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 90 | 	return (PyObject *) op; | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 91 | } | 
 | 92 |  | 
 | 93 | int | 
| Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 94 | PyList_Size(PyObject *op) | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 95 | { | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 96 | 	if (!PyList_Check(op)) { | 
 | 97 | 		PyErr_BadInternalCall(); | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 98 | 		return -1; | 
 | 99 | 	} | 
 | 100 | 	else | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 101 | 		return ((PyListObject *)op) -> ob_size; | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 102 | } | 
 | 103 |  | 
| Raymond Hettinger | fdfe618 | 2004-05-05 06:28:16 +0000 | [diff] [blame] | 104 | static PyObject *indexerr = NULL; | 
| Guido van Rossum | 929f1b8 | 1996-08-09 20:51:27 +0000 | [diff] [blame] | 105 |  | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 106 | PyObject * | 
| Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 107 | PyList_GetItem(PyObject *op, int i) | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 108 | { | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 109 | 	if (!PyList_Check(op)) { | 
 | 110 | 		PyErr_BadInternalCall(); | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 111 | 		return NULL; | 
 | 112 | 	} | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 113 | 	if (i < 0 || i >= ((PyListObject *)op) -> ob_size) { | 
| Guido van Rossum | 929f1b8 | 1996-08-09 20:51:27 +0000 | [diff] [blame] | 114 | 		if (indexerr == NULL) | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 115 | 			indexerr = PyString_FromString( | 
 | 116 | 				"list index out of range"); | 
 | 117 | 		PyErr_SetObject(PyExc_IndexError, indexerr); | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 118 | 		return NULL; | 
 | 119 | 	} | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 120 | 	return ((PyListObject *)op) -> ob_item[i]; | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 121 | } | 
 | 122 |  | 
 | 123 | int | 
| Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 124 | PyList_SetItem(register PyObject *op, register int i, | 
 | 125 |                register PyObject *newitem) | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 126 | { | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 127 | 	register PyObject *olditem; | 
 | 128 | 	register PyObject **p; | 
 | 129 | 	if (!PyList_Check(op)) { | 
 | 130 | 		Py_XDECREF(newitem); | 
 | 131 | 		PyErr_BadInternalCall(); | 
| Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 132 | 		return -1; | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 133 | 	} | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 134 | 	if (i < 0 || i >= ((PyListObject *)op) -> ob_size) { | 
 | 135 | 		Py_XDECREF(newitem); | 
 | 136 | 		PyErr_SetString(PyExc_IndexError, | 
 | 137 | 				"list assignment index out of range"); | 
| Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 138 | 		return -1; | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 139 | 	} | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 140 | 	p = ((PyListObject *)op) -> ob_item + i; | 
| Guido van Rossum | 5fe6058 | 1995-03-09 12:12:50 +0000 | [diff] [blame] | 141 | 	olditem = *p; | 
 | 142 | 	*p = newitem; | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 143 | 	Py_XDECREF(olditem); | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 144 | 	return 0; | 
 | 145 | } | 
 | 146 |  | 
 | 147 | static int | 
| Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 148 | ins1(PyListObject *self, int where, PyObject *v) | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 149 | { | 
| Raymond Hettinger | 4bb9540 | 2004-02-13 11:36:39 +0000 | [diff] [blame] | 150 | 	int i, n = self->ob_size; | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 151 | 	PyObject **items; | 
| Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 152 | 	if (v == NULL) { | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 153 | 		PyErr_BadInternalCall(); | 
| Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 154 | 		return -1; | 
 | 155 | 	} | 
| Raymond Hettinger | 4bb9540 | 2004-02-13 11:36:39 +0000 | [diff] [blame] | 156 | 	if (n == INT_MAX) { | 
| Trent Mick | a584664 | 2000-08-13 22:47:45 +0000 | [diff] [blame] | 157 | 		PyErr_SetString(PyExc_OverflowError, | 
 | 158 | 			"cannot add more objects to list"); | 
 | 159 | 		return -1; | 
 | 160 | 	} | 
| Raymond Hettinger | 4bb9540 | 2004-02-13 11:36:39 +0000 | [diff] [blame] | 161 | 	 | 
| Raymond Hettinger | d4ff741 | 2004-03-15 09:01:31 +0000 | [diff] [blame] | 162 | 	if (list_resize(self, n+1) == -1) | 
| Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 163 | 		return -1; | 
| Raymond Hettinger | 4bb9540 | 2004-02-13 11:36:39 +0000 | [diff] [blame] | 164 |  | 
| Guido van Rossum | 3a3cca5 | 2003-04-14 20:58:14 +0000 | [diff] [blame] | 165 | 	if (where < 0) { | 
| Raymond Hettinger | 4bb9540 | 2004-02-13 11:36:39 +0000 | [diff] [blame] | 166 | 		where += n; | 
| Guido van Rossum | 3a3cca5 | 2003-04-14 20:58:14 +0000 | [diff] [blame] | 167 | 		if (where < 0) | 
 | 168 | 			where = 0; | 
 | 169 | 	} | 
| Raymond Hettinger | 4bb9540 | 2004-02-13 11:36:39 +0000 | [diff] [blame] | 170 | 	if (where > n) | 
 | 171 | 		where = n; | 
 | 172 | 	items = self->ob_item; | 
 | 173 | 	for (i = n; --i >= where; ) | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 174 | 		items[i+1] = items[i]; | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 175 | 	Py_INCREF(v); | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 176 | 	items[where] = v; | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 177 | 	return 0; | 
 | 178 | } | 
 | 179 |  | 
 | 180 | int | 
| Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 181 | PyList_Insert(PyObject *op, int where, PyObject *newitem) | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 182 | { | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 183 | 	if (!PyList_Check(op)) { | 
 | 184 | 		PyErr_BadInternalCall(); | 
| Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 185 | 		return -1; | 
 | 186 | 	} | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 187 | 	return ins1((PyListObject *)op, where, newitem); | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 188 | } | 
 | 189 |  | 
| Raymond Hettinger | 40a0382 | 2004-04-12 13:05:09 +0000 | [diff] [blame] | 190 | static int | 
 | 191 | app1(PyListObject *self, PyObject *v) | 
 | 192 | { | 
 | 193 | 	int n = PyList_GET_SIZE(self); | 
 | 194 |  | 
 | 195 | 	assert (v != NULL); | 
 | 196 | 	if (n == INT_MAX) { | 
 | 197 | 		PyErr_SetString(PyExc_OverflowError, | 
 | 198 | 			"cannot add more objects to list"); | 
 | 199 | 		return -1; | 
 | 200 | 	} | 
 | 201 |  | 
 | 202 | 	if (list_resize(self, n+1) == -1) | 
 | 203 | 		return -1; | 
 | 204 |  | 
 | 205 | 	Py_INCREF(v); | 
 | 206 | 	PyList_SET_ITEM(self, n, v); | 
 | 207 | 	return 0; | 
 | 208 | } | 
 | 209 |  | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 210 | int | 
| Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 211 | PyList_Append(PyObject *op, PyObject *newitem) | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 212 | { | 
| Raymond Hettinger | fdfe618 | 2004-05-05 06:28:16 +0000 | [diff] [blame] | 213 | 	if (PyList_Check(op) && (newitem != NULL)) | 
 | 214 | 		return app1((PyListObject *)op, newitem); | 
 | 215 | 	PyErr_BadInternalCall(); | 
 | 216 | 	return -1; | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 217 | } | 
 | 218 |  | 
 | 219 | /* Methods */ | 
 | 220 |  | 
 | 221 | static void | 
| Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 222 | list_dealloc(PyListObject *op) | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 223 | { | 
 | 224 | 	int i; | 
| Guido van Rossum | ff413af | 2002-03-28 20:34:59 +0000 | [diff] [blame] | 225 | 	PyObject_GC_UnTrack(op); | 
| Guido van Rossum | d724b23 | 2000-03-13 16:01:29 +0000 | [diff] [blame] | 226 | 	Py_TRASHCAN_SAFE_BEGIN(op) | 
| Jack Jansen | 7874d1f | 1995-01-19 12:09:27 +0000 | [diff] [blame] | 227 | 	if (op->ob_item != NULL) { | 
| Guido van Rossum | fa71701 | 1999-06-09 15:19:34 +0000 | [diff] [blame] | 228 | 		/* Do it backwards, for Christian Tismer. | 
 | 229 | 		   There's a simple test case where somehow this reduces | 
 | 230 | 		   thrashing when a *very* large list is created and | 
 | 231 | 		   immediately deleted. */ | 
 | 232 | 		i = op->ob_size; | 
 | 233 | 		while (--i >= 0) { | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 234 | 			Py_XDECREF(op->ob_item[i]); | 
| Jack Jansen | 7874d1f | 1995-01-19 12:09:27 +0000 | [diff] [blame] | 235 | 		} | 
| Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 236 | 		PyMem_FREE(op->ob_item); | 
| Jack Jansen | 7874d1f | 1995-01-19 12:09:27 +0000 | [diff] [blame] | 237 | 	} | 
| Raymond Hettinger | 0468e41 | 2004-05-05 05:37:53 +0000 | [diff] [blame] | 238 | 	if (num_free_lists < MAXFREELISTS && PyList_CheckExact(op)) | 
 | 239 | 		free_lists[num_free_lists++] = op; | 
 | 240 | 	else  | 
 | 241 | 		op->ob_type->tp_free((PyObject *)op);		 | 
| Guido van Rossum | d724b23 | 2000-03-13 16:01:29 +0000 | [diff] [blame] | 242 | 	Py_TRASHCAN_SAFE_END(op) | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 243 | } | 
 | 244 |  | 
| Guido van Rossum | 9093361 | 1991-06-07 16:10:43 +0000 | [diff] [blame] | 245 | static int | 
| Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 246 | list_print(PyListObject *op, FILE *fp, int flags) | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 247 | { | 
 | 248 | 	int i; | 
| Guido van Rossum | fb376de | 1998-04-10 22:47:27 +0000 | [diff] [blame] | 249 |  | 
 | 250 | 	i = Py_ReprEnter((PyObject*)op); | 
 | 251 | 	if (i != 0) { | 
 | 252 | 		if (i < 0) | 
 | 253 | 			return i; | 
 | 254 | 		fprintf(fp, "[...]"); | 
 | 255 | 		return 0; | 
 | 256 | 	} | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 257 | 	fprintf(fp, "["); | 
| Guido van Rossum | 9093361 | 1991-06-07 16:10:43 +0000 | [diff] [blame] | 258 | 	for (i = 0; i < op->ob_size; i++) { | 
 | 259 | 		if (i > 0) | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 260 | 			fprintf(fp, ", "); | 
| Guido van Rossum | fb376de | 1998-04-10 22:47:27 +0000 | [diff] [blame] | 261 | 		if (PyObject_Print(op->ob_item[i], fp, 0) != 0) { | 
 | 262 | 			Py_ReprLeave((PyObject *)op); | 
| Guido van Rossum | 9093361 | 1991-06-07 16:10:43 +0000 | [diff] [blame] | 263 | 			return -1; | 
| Guido van Rossum | fb376de | 1998-04-10 22:47:27 +0000 | [diff] [blame] | 264 | 		} | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 265 | 	} | 
 | 266 | 	fprintf(fp, "]"); | 
| Guido van Rossum | fb376de | 1998-04-10 22:47:27 +0000 | [diff] [blame] | 267 | 	Py_ReprLeave((PyObject *)op); | 
| Guido van Rossum | 9093361 | 1991-06-07 16:10:43 +0000 | [diff] [blame] | 268 | 	return 0; | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 269 | } | 
 | 270 |  | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 271 | static PyObject * | 
| Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 272 | list_repr(PyListObject *v) | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 273 | { | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 274 | 	int i; | 
| Tim Peters | a725959 | 2001-06-16 05:11:17 +0000 | [diff] [blame] | 275 | 	PyObject *s, *temp; | 
 | 276 | 	PyObject *pieces = NULL, *result = NULL; | 
| Guido van Rossum | fb376de | 1998-04-10 22:47:27 +0000 | [diff] [blame] | 277 |  | 
 | 278 | 	i = Py_ReprEnter((PyObject*)v); | 
 | 279 | 	if (i != 0) { | 
| Tim Peters | a725959 | 2001-06-16 05:11:17 +0000 | [diff] [blame] | 280 | 		return i > 0 ? PyString_FromString("[...]") : NULL; | 
| Guido van Rossum | fb376de | 1998-04-10 22:47:27 +0000 | [diff] [blame] | 281 | 	} | 
| Tim Peters | a725959 | 2001-06-16 05:11:17 +0000 | [diff] [blame] | 282 |  | 
 | 283 | 	if (v->ob_size == 0) { | 
 | 284 | 		result = PyString_FromString("[]"); | 
 | 285 | 		goto Done; | 
 | 286 | 	} | 
 | 287 |  | 
 | 288 | 	pieces = PyList_New(0); | 
 | 289 | 	if (pieces == NULL) | 
 | 290 | 		goto Done; | 
 | 291 |  | 
 | 292 | 	/* Do repr() on each element.  Note that this may mutate the list, | 
 | 293 | 	   so must refetch the list size on each iteration. */ | 
 | 294 | 	for (i = 0; i < v->ob_size; ++i) { | 
 | 295 | 		int status; | 
 | 296 | 		s = PyObject_Repr(v->ob_item[i]); | 
 | 297 | 		if (s == NULL) | 
 | 298 | 			goto Done; | 
 | 299 | 		status = PyList_Append(pieces, s); | 
 | 300 | 		Py_DECREF(s);  /* append created a new ref */ | 
 | 301 | 		if (status < 0) | 
 | 302 | 			goto Done; | 
 | 303 | 	} | 
 | 304 |  | 
 | 305 | 	/* Add "[]" decorations to the first and last items. */ | 
 | 306 | 	assert(PyList_GET_SIZE(pieces) > 0); | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 307 | 	s = PyString_FromString("["); | 
| Tim Peters | a725959 | 2001-06-16 05:11:17 +0000 | [diff] [blame] | 308 | 	if (s == NULL) | 
 | 309 | 		goto Done; | 
 | 310 | 	temp = PyList_GET_ITEM(pieces, 0); | 
 | 311 | 	PyString_ConcatAndDel(&s, temp); | 
 | 312 | 	PyList_SET_ITEM(pieces, 0, s); | 
 | 313 | 	if (s == NULL) | 
 | 314 | 		goto Done; | 
 | 315 |  | 
 | 316 | 	s = PyString_FromString("]"); | 
 | 317 | 	if (s == NULL) | 
 | 318 | 		goto Done; | 
 | 319 | 	temp = PyList_GET_ITEM(pieces, PyList_GET_SIZE(pieces) - 1); | 
 | 320 | 	PyString_ConcatAndDel(&temp, s); | 
 | 321 | 	PyList_SET_ITEM(pieces, PyList_GET_SIZE(pieces) - 1, temp); | 
 | 322 | 	if (temp == NULL) | 
 | 323 | 		goto Done; | 
 | 324 |  | 
 | 325 | 	/* Paste them all together with ", " between. */ | 
 | 326 | 	s = PyString_FromString(", "); | 
 | 327 | 	if (s == NULL) | 
 | 328 | 		goto Done; | 
 | 329 | 	result = _PyString_Join(s, pieces); | 
| Tim Peters | 3b01a12 | 2002-07-19 02:35:45 +0000 | [diff] [blame] | 330 | 	Py_DECREF(s); | 
| Tim Peters | a725959 | 2001-06-16 05:11:17 +0000 | [diff] [blame] | 331 |  | 
 | 332 | Done: | 
 | 333 | 	Py_XDECREF(pieces); | 
| Guido van Rossum | fb376de | 1998-04-10 22:47:27 +0000 | [diff] [blame] | 334 | 	Py_ReprLeave((PyObject *)v); | 
| Tim Peters | a725959 | 2001-06-16 05:11:17 +0000 | [diff] [blame] | 335 | 	return result; | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 336 | } | 
 | 337 |  | 
 | 338 | static int | 
| Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 339 | list_length(PyListObject *a) | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 340 | { | 
 | 341 | 	return a->ob_size; | 
 | 342 | } | 
 | 343 |  | 
| Jeremy Hylton | 37b1a26 | 2000-04-27 21:41:03 +0000 | [diff] [blame] | 344 | static int | 
| Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 345 | list_contains(PyListObject *a, PyObject *el) | 
| Jeremy Hylton | 37b1a26 | 2000-04-27 21:41:03 +0000 | [diff] [blame] | 346 | { | 
| Raymond Hettinger | aae5999 | 2002-09-05 14:23:49 +0000 | [diff] [blame] | 347 | 	int i, cmp; | 
| Jeremy Hylton | 37b1a26 | 2000-04-27 21:41:03 +0000 | [diff] [blame] | 348 |  | 
| Raymond Hettinger | aae5999 | 2002-09-05 14:23:49 +0000 | [diff] [blame] | 349 | 	for (i = 0, cmp = 0 ; cmp == 0 && i < a->ob_size; ++i) | 
 | 350 | 		cmp = PyObject_RichCompareBool(el, PyList_GET_ITEM(a, i), | 
| Guido van Rossum | 65e1cea | 2001-01-17 22:11:59 +0000 | [diff] [blame] | 351 | 						   Py_EQ); | 
| Neal Norwitz | bb9c5f5 | 2002-09-05 21:32:55 +0000 | [diff] [blame] | 352 | 	return cmp; | 
| Jeremy Hylton | 37b1a26 | 2000-04-27 21:41:03 +0000 | [diff] [blame] | 353 | } | 
 | 354 |  | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 355 | static PyObject * | 
| Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 356 | list_item(PyListObject *a, int i) | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 357 | { | 
 | 358 | 	if (i < 0 || i >= a->ob_size) { | 
| Guido van Rossum | 929f1b8 | 1996-08-09 20:51:27 +0000 | [diff] [blame] | 359 | 		if (indexerr == NULL) | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 360 | 			indexerr = PyString_FromString( | 
 | 361 | 				"list index out of range"); | 
 | 362 | 		PyErr_SetObject(PyExc_IndexError, indexerr); | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 363 | 		return NULL; | 
 | 364 | 	} | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 365 | 	Py_INCREF(a->ob_item[i]); | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 366 | 	return a->ob_item[i]; | 
 | 367 | } | 
 | 368 |  | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 369 | static PyObject * | 
| Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 370 | list_slice(PyListObject *a, int ilow, int ihigh) | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 371 | { | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 372 | 	PyListObject *np; | 
| Raymond Hettinger | b7d05db | 2004-03-08 07:25:05 +0000 | [diff] [blame] | 373 | 	PyObject **src, **dest; | 
| Raymond Hettinger | 99842b6 | 2004-03-08 05:56:15 +0000 | [diff] [blame] | 374 | 	int i, len; | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 375 | 	if (ilow < 0) | 
 | 376 | 		ilow = 0; | 
 | 377 | 	else if (ilow > a->ob_size) | 
 | 378 | 		ilow = a->ob_size; | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 379 | 	if (ihigh < ilow) | 
 | 380 | 		ihigh = ilow; | 
 | 381 | 	else if (ihigh > a->ob_size) | 
 | 382 | 		ihigh = a->ob_size; | 
| Raymond Hettinger | 99842b6 | 2004-03-08 05:56:15 +0000 | [diff] [blame] | 383 | 	len = ihigh - ilow; | 
 | 384 | 	np = (PyListObject *) PyList_New(len); | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 385 | 	if (np == NULL) | 
 | 386 | 		return NULL; | 
| Raymond Hettinger | 99842b6 | 2004-03-08 05:56:15 +0000 | [diff] [blame] | 387 |  | 
| Raymond Hettinger | b7d05db | 2004-03-08 07:25:05 +0000 | [diff] [blame] | 388 | 	src = a->ob_item + ilow; | 
 | 389 | 	dest = np->ob_item; | 
| Raymond Hettinger | 99842b6 | 2004-03-08 05:56:15 +0000 | [diff] [blame] | 390 | 	for (i = 0; i < len; i++) { | 
| Raymond Hettinger | b7d05db | 2004-03-08 07:25:05 +0000 | [diff] [blame] | 391 | 		PyObject *v = src[i]; | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 392 | 		Py_INCREF(v); | 
| Raymond Hettinger | b7d05db | 2004-03-08 07:25:05 +0000 | [diff] [blame] | 393 | 		dest[i] = v; | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 394 | 	} | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 395 | 	return (PyObject *)np; | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 396 | } | 
 | 397 |  | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 398 | PyObject * | 
| Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 399 | PyList_GetSlice(PyObject *a, int ilow, int ihigh) | 
| Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 400 | { | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 401 | 	if (!PyList_Check(a)) { | 
 | 402 | 		PyErr_BadInternalCall(); | 
| Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 403 | 		return NULL; | 
 | 404 | 	} | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 405 | 	return list_slice((PyListObject *)a, ilow, ihigh); | 
| Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 406 | } | 
 | 407 |  | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 408 | static PyObject * | 
| Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 409 | list_concat(PyListObject *a, PyObject *bb) | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 410 | { | 
 | 411 | 	int size; | 
 | 412 | 	int i; | 
| Raymond Hettinger | a6366fe | 2004-03-09 13:05:22 +0000 | [diff] [blame] | 413 | 	PyObject **src, **dest; | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 414 | 	PyListObject *np; | 
 | 415 | 	if (!PyList_Check(bb)) { | 
| Fred Drake | b6a9ada | 2000-06-01 03:12:13 +0000 | [diff] [blame] | 416 | 		PyErr_Format(PyExc_TypeError, | 
| Fred Drake | 914a2ed | 2000-06-01 14:31:03 +0000 | [diff] [blame] | 417 | 			  "can only concatenate list (not \"%.200s\") to list", | 
 | 418 | 			  bb->ob_type->tp_name); | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 419 | 		return NULL; | 
 | 420 | 	} | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 421 | #define b ((PyListObject *)bb) | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 422 | 	size = a->ob_size + b->ob_size; | 
| Guido van Rossum | a5c0e6d | 2002-10-11 21:05:56 +0000 | [diff] [blame] | 423 | 	if (size < 0) | 
 | 424 | 		return PyErr_NoMemory(); | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 425 | 	np = (PyListObject *) PyList_New(size); | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 426 | 	if (np == NULL) { | 
| Guido van Rossum | 9093361 | 1991-06-07 16:10:43 +0000 | [diff] [blame] | 427 | 		return NULL; | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 428 | 	} | 
| Raymond Hettinger | a6366fe | 2004-03-09 13:05:22 +0000 | [diff] [blame] | 429 | 	src = a->ob_item; | 
 | 430 | 	dest = np->ob_item; | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 431 | 	for (i = 0; i < a->ob_size; i++) { | 
| Raymond Hettinger | a6366fe | 2004-03-09 13:05:22 +0000 | [diff] [blame] | 432 | 		PyObject *v = src[i]; | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 433 | 		Py_INCREF(v); | 
| Raymond Hettinger | a6366fe | 2004-03-09 13:05:22 +0000 | [diff] [blame] | 434 | 		dest[i] = v; | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 435 | 	} | 
| Raymond Hettinger | a6366fe | 2004-03-09 13:05:22 +0000 | [diff] [blame] | 436 | 	src = b->ob_item; | 
 | 437 | 	dest = np->ob_item + a->ob_size; | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 438 | 	for (i = 0; i < b->ob_size; i++) { | 
| Raymond Hettinger | a6366fe | 2004-03-09 13:05:22 +0000 | [diff] [blame] | 439 | 		PyObject *v = src[i]; | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 440 | 		Py_INCREF(v); | 
| Raymond Hettinger | a6366fe | 2004-03-09 13:05:22 +0000 | [diff] [blame] | 441 | 		dest[i] = v; | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 442 | 	} | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 443 | 	return (PyObject *)np; | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 444 | #undef b | 
 | 445 | } | 
 | 446 |  | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 447 | static PyObject * | 
| Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 448 | list_repeat(PyListObject *a, int n) | 
| Guido van Rossum | ed98d48 | 1991-03-06 13:07:53 +0000 | [diff] [blame] | 449 | { | 
 | 450 | 	int i, j; | 
 | 451 | 	int size; | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 452 | 	PyListObject *np; | 
| Raymond Hettinger | a6366fe | 2004-03-09 13:05:22 +0000 | [diff] [blame] | 453 | 	PyObject **p, **items; | 
| Raymond Hettinger | 6624e68 | 2003-05-21 05:58:46 +0000 | [diff] [blame] | 454 | 	PyObject *elem; | 
| Guido van Rossum | ed98d48 | 1991-03-06 13:07:53 +0000 | [diff] [blame] | 455 | 	if (n < 0) | 
 | 456 | 		n = 0; | 
 | 457 | 	size = a->ob_size * n; | 
| Raymond Hettinger | 6624e68 | 2003-05-21 05:58:46 +0000 | [diff] [blame] | 458 | 	if (size == 0) | 
 | 459 |               return PyList_New(0); | 
| Guido van Rossum | bfa5a14 | 2002-10-11 23:39:35 +0000 | [diff] [blame] | 460 | 	if (n && size/n != a->ob_size) | 
| Guido van Rossum | a5c0e6d | 2002-10-11 21:05:56 +0000 | [diff] [blame] | 461 | 		return PyErr_NoMemory(); | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 462 | 	np = (PyListObject *) PyList_New(size); | 
| Guido van Rossum | ed98d48 | 1991-03-06 13:07:53 +0000 | [diff] [blame] | 463 | 	if (np == NULL) | 
 | 464 | 		return NULL; | 
| Raymond Hettinger | 6624e68 | 2003-05-21 05:58:46 +0000 | [diff] [blame] | 465 |  | 
| Raymond Hettinger | a6366fe | 2004-03-09 13:05:22 +0000 | [diff] [blame] | 466 | 	items = np->ob_item; | 
| Raymond Hettinger | 6624e68 | 2003-05-21 05:58:46 +0000 | [diff] [blame] | 467 | 	if (a->ob_size == 1) { | 
 | 468 | 		elem = a->ob_item[0]; | 
 | 469 | 		for (i = 0; i < n; i++) { | 
| Raymond Hettinger | a6366fe | 2004-03-09 13:05:22 +0000 | [diff] [blame] | 470 | 			items[i] = elem; | 
| Raymond Hettinger | 6624e68 | 2003-05-21 05:58:46 +0000 | [diff] [blame] | 471 | 			Py_INCREF(elem); | 
 | 472 | 		} | 
 | 473 | 		return (PyObject *) np; | 
 | 474 | 	} | 
| Guido van Rossum | ed98d48 | 1991-03-06 13:07:53 +0000 | [diff] [blame] | 475 | 	p = np->ob_item; | 
| Raymond Hettinger | a6366fe | 2004-03-09 13:05:22 +0000 | [diff] [blame] | 476 | 	items = a->ob_item; | 
| Guido van Rossum | ed98d48 | 1991-03-06 13:07:53 +0000 | [diff] [blame] | 477 | 	for (i = 0; i < n; i++) { | 
 | 478 | 		for (j = 0; j < a->ob_size; j++) { | 
| Raymond Hettinger | a6366fe | 2004-03-09 13:05:22 +0000 | [diff] [blame] | 479 | 			*p = items[j]; | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 480 | 			Py_INCREF(*p); | 
| Guido van Rossum | ed98d48 | 1991-03-06 13:07:53 +0000 | [diff] [blame] | 481 | 			p++; | 
 | 482 | 		} | 
 | 483 | 	} | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 484 | 	return (PyObject *) np; | 
| Guido van Rossum | ed98d48 | 1991-03-06 13:07:53 +0000 | [diff] [blame] | 485 | } | 
 | 486 |  | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 487 | static int | 
| Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 488 | list_ass_slice(PyListObject *a, int ilow, int ihigh, PyObject *v) | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 489 | { | 
| Guido van Rossum | ae7bf1a | 1995-01-17 10:21:11 +0000 | [diff] [blame] | 490 | 	/* Because [X]DECREF can recursively invoke list operations on | 
 | 491 | 	   this list, we must postpone all [X]DECREF activity until | 
 | 492 | 	   after the list is back in its canonical shape.  Therefore | 
 | 493 | 	   we must allocate an additional array, 'recycle', into which | 
 | 494 | 	   we temporarily copy the items that are deleted from the | 
 | 495 | 	   list. :-( */ | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 496 | 	PyObject **recycle, **p; | 
 | 497 | 	PyObject **item; | 
| Raymond Hettinger | f889e10 | 2004-03-09 08:04:33 +0000 | [diff] [blame] | 498 | 	PyObject **vitem = NULL; | 
| Michael W. Hudson | 5da854f | 2002-11-05 17:38:05 +0000 | [diff] [blame] | 499 | 	PyObject *v_as_SF = NULL; /* PySequence_Fast(v) */ | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 500 | 	int n; /* Size of replacement list */ | 
 | 501 | 	int d; /* Change in size */ | 
 | 502 | 	int k; /* Loop index */ | 
| Raymond Hettinger | 4bb9540 | 2004-02-13 11:36:39 +0000 | [diff] [blame] | 503 | 	int s; | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 504 | #define b ((PyListObject *)v) | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 505 | 	if (v == NULL) | 
 | 506 | 		n = 0; | 
| Michael W. Hudson | 5da854f | 2002-11-05 17:38:05 +0000 | [diff] [blame] | 507 | 	else { | 
| Guido van Rossum | 32dffaa | 1991-12-24 13:27:34 +0000 | [diff] [blame] | 508 | 		if (a == b) { | 
 | 509 | 			/* Special case "a[i:j] = a" -- copy b first */ | 
 | 510 | 			int ret; | 
| Michael W. Hudson | da0a067 | 2003-08-15 12:06:41 +0000 | [diff] [blame] | 511 | 			v = list_slice(b, 0, b->ob_size); | 
| Martin v. Löwis | cd12bfc | 2003-05-03 10:53:08 +0000 | [diff] [blame] | 512 | 			if (v == NULL) | 
 | 513 | 				return -1; | 
| Guido van Rossum | 32dffaa | 1991-12-24 13:27:34 +0000 | [diff] [blame] | 514 | 			ret = list_ass_slice(a, ilow, ihigh, v); | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 515 | 			Py_DECREF(v); | 
| Guido van Rossum | 32dffaa | 1991-12-24 13:27:34 +0000 | [diff] [blame] | 516 | 			return ret; | 
 | 517 | 		} | 
| Raymond Hettinger | f889e10 | 2004-03-09 08:04:33 +0000 | [diff] [blame] | 518 | 		v_as_SF = PySequence_Fast(v, "can only assign an iterable"); | 
| Michael W. Hudson | b4f4938 | 2003-08-14 17:04:28 +0000 | [diff] [blame] | 519 | 		if(v_as_SF == NULL) | 
 | 520 | 			return -1; | 
 | 521 | 		n = PySequence_Fast_GET_SIZE(v_as_SF); | 
| Raymond Hettinger | 42bec93 | 2004-03-12 16:38:17 +0000 | [diff] [blame] | 522 | 		vitem = PySequence_Fast_ITEMS(v_as_SF); | 
| Guido van Rossum | 32dffaa | 1991-12-24 13:27:34 +0000 | [diff] [blame] | 523 | 	} | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 524 | 	if (ilow < 0) | 
 | 525 | 		ilow = 0; | 
 | 526 | 	else if (ilow > a->ob_size) | 
 | 527 | 		ilow = a->ob_size; | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 528 | 	if (ihigh < ilow) | 
 | 529 | 		ihigh = ilow; | 
 | 530 | 	else if (ihigh > a->ob_size) | 
 | 531 | 		ihigh = a->ob_size; | 
 | 532 | 	item = a->ob_item; | 
 | 533 | 	d = n - (ihigh-ilow); | 
| Martin v. Löwis | cd12bfc | 2003-05-03 10:53:08 +0000 | [diff] [blame] | 534 | 	if (ihigh > ilow) { | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 535 | 		p = recycle = PyMem_NEW(PyObject *, (ihigh-ilow)); | 
| Martin v. Löwis | cd12bfc | 2003-05-03 10:53:08 +0000 | [diff] [blame] | 536 | 		if (recycle == NULL) { | 
 | 537 | 			PyErr_NoMemory(); | 
 | 538 | 			return -1; | 
 | 539 | 		} | 
 | 540 | 	} | 
| Guido van Rossum | ae7bf1a | 1995-01-17 10:21:11 +0000 | [diff] [blame] | 541 | 	else | 
 | 542 | 		p = recycle = NULL; | 
 | 543 | 	if (d <= 0) { /* Delete -d items; recycle ihigh-ilow items */ | 
| Raymond Hettinger | 66d31f8 | 2004-03-10 11:44:04 +0000 | [diff] [blame] | 544 | 		memcpy(p, &item[ilow], (ihigh - ilow)*sizeof(PyObject *)); | 
| Raymond Hettinger | f889e10 | 2004-03-09 08:04:33 +0000 | [diff] [blame] | 545 | 		p += ihigh - ilow; | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 546 | 		if (d < 0) { | 
| Raymond Hettinger | f889e10 | 2004-03-09 08:04:33 +0000 | [diff] [blame] | 547 | 			memmove(&item[ihigh+d], &item[ihigh],  | 
 | 548 | 				(a->ob_size - ihigh)*sizeof(PyObject *)); | 
| Raymond Hettinger | d4ff741 | 2004-03-15 09:01:31 +0000 | [diff] [blame] | 549 | 			list_resize(a, a->ob_size + d); | 
| Raymond Hettinger | 4bb9540 | 2004-02-13 11:36:39 +0000 | [diff] [blame] | 550 | 			item = a->ob_item; | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 551 | 		} | 
 | 552 | 	} | 
| Guido van Rossum | ae7bf1a | 1995-01-17 10:21:11 +0000 | [diff] [blame] | 553 | 	else { /* Insert d items; recycle ihigh-ilow items */ | 
| Raymond Hettinger | 4bb9540 | 2004-02-13 11:36:39 +0000 | [diff] [blame] | 554 | 		s = a->ob_size; | 
| Raymond Hettinger | d4ff741 | 2004-03-15 09:01:31 +0000 | [diff] [blame] | 555 | 		if (list_resize(a, s+d) == -1) { | 
| Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 556 | 			if (recycle != NULL) | 
 | 557 | 				PyMem_DEL(recycle); | 
| Raymond Hettinger | 2731ae4 | 2004-02-14 03:07:21 +0000 | [diff] [blame] | 558 | 			return -1; | 
| Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 559 | 		} | 
| Raymond Hettinger | 4bb9540 | 2004-02-13 11:36:39 +0000 | [diff] [blame] | 560 | 		item = a->ob_item; | 
| Raymond Hettinger | f889e10 | 2004-03-09 08:04:33 +0000 | [diff] [blame] | 561 | 		memmove(&item[ihigh+d], &item[ihigh], | 
 | 562 | 			(s - ihigh)*sizeof(PyObject *)); | 
| Raymond Hettinger | 66d31f8 | 2004-03-10 11:44:04 +0000 | [diff] [blame] | 563 | 		memcpy(p, &item[ilow], (ihigh - ilow)*sizeof(PyObject *)); | 
| Raymond Hettinger | f889e10 | 2004-03-09 08:04:33 +0000 | [diff] [blame] | 564 | 		p += ihigh - ilow; | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 565 | 	} | 
 | 566 | 	for (k = 0; k < n; k++, ilow++) { | 
| Raymond Hettinger | f889e10 | 2004-03-09 08:04:33 +0000 | [diff] [blame] | 567 | 		PyObject *w = vitem[k]; | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 568 | 		Py_XINCREF(w); | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 569 | 		item[ilow] = w; | 
 | 570 | 	} | 
| Guido van Rossum | ae7bf1a | 1995-01-17 10:21:11 +0000 | [diff] [blame] | 571 | 	if (recycle) { | 
 | 572 | 		while (--p >= recycle) | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 573 | 			Py_XDECREF(*p); | 
 | 574 | 		PyMem_DEL(recycle); | 
| Guido van Rossum | ae7bf1a | 1995-01-17 10:21:11 +0000 | [diff] [blame] | 575 | 	} | 
| Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 576 | 	if (a->ob_size == 0 && a->ob_item != NULL) { | 
 | 577 | 		PyMem_FREE(a->ob_item); | 
 | 578 | 		a->ob_item = NULL; | 
 | 579 | 	} | 
| Michael W. Hudson | 5da854f | 2002-11-05 17:38:05 +0000 | [diff] [blame] | 580 | 	Py_XDECREF(v_as_SF); | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 581 | 	return 0; | 
 | 582 | #undef b | 
 | 583 | } | 
 | 584 |  | 
| Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 585 | int | 
| Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 586 | PyList_SetSlice(PyObject *a, int ilow, int ihigh, PyObject *v) | 
| Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 587 | { | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 588 | 	if (!PyList_Check(a)) { | 
 | 589 | 		PyErr_BadInternalCall(); | 
| Guido van Rossum | 1fc238a | 1993-07-29 08:25:09 +0000 | [diff] [blame] | 590 | 		return -1; | 
| Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 591 | 	} | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 592 | 	return list_ass_slice((PyListObject *)a, ilow, ihigh, v); | 
| Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 593 | } | 
 | 594 |  | 
| Thomas Wouters | e289e0b | 2000-08-24 20:08:19 +0000 | [diff] [blame] | 595 | static PyObject * | 
 | 596 | list_inplace_repeat(PyListObject *self, int n) | 
 | 597 | { | 
 | 598 | 	PyObject **items; | 
| Raymond Hettinger | 4bb9540 | 2004-02-13 11:36:39 +0000 | [diff] [blame] | 599 | 	int size, i, j, p; | 
| Thomas Wouters | e289e0b | 2000-08-24 20:08:19 +0000 | [diff] [blame] | 600 |  | 
 | 601 |  | 
 | 602 | 	size = PyList_GET_SIZE(self); | 
 | 603 | 	if (size == 0) { | 
 | 604 | 		Py_INCREF(self); | 
 | 605 | 		return (PyObject *)self; | 
 | 606 | 	} | 
 | 607 |  | 
| Thomas Wouters | e289e0b | 2000-08-24 20:08:19 +0000 | [diff] [blame] | 608 | 	if (n < 1) { | 
| Raymond Hettinger | 4bb9540 | 2004-02-13 11:36:39 +0000 | [diff] [blame] | 609 | 		items = self->ob_item; | 
| Thomas Wouters | e289e0b | 2000-08-24 20:08:19 +0000 | [diff] [blame] | 610 | 		self->ob_item = NULL; | 
 | 611 | 		self->ob_size = 0; | 
 | 612 | 		for (i = 0; i < size; i++) | 
 | 613 | 			Py_XDECREF(items[i]); | 
 | 614 | 		PyMem_DEL(items); | 
 | 615 | 		Py_INCREF(self); | 
 | 616 | 		return (PyObject *)self; | 
 | 617 | 	} | 
 | 618 |  | 
| Raymond Hettinger | d4ff741 | 2004-03-15 09:01:31 +0000 | [diff] [blame] | 619 | 	if (list_resize(self, size*n) == -1)  | 
| Raymond Hettinger | 4bb9540 | 2004-02-13 11:36:39 +0000 | [diff] [blame] | 620 | 		return NULL; | 
 | 621 |  | 
 | 622 | 	p = size; | 
| Raymond Hettinger | a6366fe | 2004-03-09 13:05:22 +0000 | [diff] [blame] | 623 | 	items = self->ob_item; | 
| Thomas Wouters | e289e0b | 2000-08-24 20:08:19 +0000 | [diff] [blame] | 624 | 	for (i = 1; i < n; i++) { /* Start counting at 1, not 0 */ | 
 | 625 | 		for (j = 0; j < size; j++) { | 
| Raymond Hettinger | a6366fe | 2004-03-09 13:05:22 +0000 | [diff] [blame] | 626 | 			PyObject *o = items[j]; | 
| Thomas Wouters | e289e0b | 2000-08-24 20:08:19 +0000 | [diff] [blame] | 627 | 			Py_INCREF(o); | 
| Raymond Hettinger | a6366fe | 2004-03-09 13:05:22 +0000 | [diff] [blame] | 628 | 			items[p++] = o; | 
| Thomas Wouters | e289e0b | 2000-08-24 20:08:19 +0000 | [diff] [blame] | 629 | 		} | 
 | 630 | 	} | 
 | 631 | 	Py_INCREF(self); | 
 | 632 | 	return (PyObject *)self; | 
| Thomas Wouters | e289e0b | 2000-08-24 20:08:19 +0000 | [diff] [blame] | 633 | } | 
 | 634 |  | 
| Guido van Rossum | 4a450d0 | 1991-04-03 19:05:18 +0000 | [diff] [blame] | 635 | static int | 
| Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 636 | list_ass_item(PyListObject *a, int i, PyObject *v) | 
| Guido van Rossum | 4a450d0 | 1991-04-03 19:05:18 +0000 | [diff] [blame] | 637 | { | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 638 | 	PyObject *old_value; | 
| Guido van Rossum | 4a450d0 | 1991-04-03 19:05:18 +0000 | [diff] [blame] | 639 | 	if (i < 0 || i >= a->ob_size) { | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 640 | 		PyErr_SetString(PyExc_IndexError, | 
 | 641 | 				"list assignment index out of range"); | 
| Guido van Rossum | 4a450d0 | 1991-04-03 19:05:18 +0000 | [diff] [blame] | 642 | 		return -1; | 
 | 643 | 	} | 
 | 644 | 	if (v == NULL) | 
 | 645 | 		return list_ass_slice(a, i, i+1, v); | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 646 | 	Py_INCREF(v); | 
| Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 647 | 	old_value = a->ob_item[i]; | 
| Guido van Rossum | 4a450d0 | 1991-04-03 19:05:18 +0000 | [diff] [blame] | 648 | 	a->ob_item[i] = v; | 
| Tim Peters | 3b01a12 | 2002-07-19 02:35:45 +0000 | [diff] [blame] | 649 | 	Py_DECREF(old_value); | 
| Guido van Rossum | 4a450d0 | 1991-04-03 19:05:18 +0000 | [diff] [blame] | 650 | 	return 0; | 
 | 651 | } | 
 | 652 |  | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 653 | static PyObject * | 
| Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 654 | listinsert(PyListObject *self, PyObject *args) | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 655 | { | 
 | 656 | 	int i; | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 657 | 	PyObject *v; | 
| Guido van Rossum | c00a938 | 2000-02-24 21:48:29 +0000 | [diff] [blame] | 658 | 	if (!PyArg_ParseTuple(args, "iO:insert", &i, &v)) | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 659 | 		return NULL; | 
| Raymond Hettinger | 45d0b5c | 2004-04-12 17:21:03 +0000 | [diff] [blame] | 660 | 	if (ins1(self, i, v) == 0) | 
 | 661 | 		Py_RETURN_NONE; | 
| Raymond Hettinger | 501f02c | 2004-04-12 14:01:16 +0000 | [diff] [blame] | 662 | 	return NULL; | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 663 | } | 
 | 664 |  | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 665 | static PyObject * | 
| Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 666 | listappend(PyListObject *self, PyObject *v) | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 667 | { | 
| Raymond Hettinger | 45d0b5c | 2004-04-12 17:21:03 +0000 | [diff] [blame] | 668 | 	if (app1(self, v) == 0) | 
 | 669 | 		Py_RETURN_NONE; | 
| Raymond Hettinger | 501f02c | 2004-04-12 14:01:16 +0000 | [diff] [blame] | 670 | 	return NULL; | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 671 | } | 
 | 672 |  | 
| Barry Warsaw | dedf6d6 | 1998-10-09 16:37:25 +0000 | [diff] [blame] | 673 | static PyObject * | 
| Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 674 | listextend(PyListObject *self, PyObject *b) | 
| Thomas Wouters | e289e0b | 2000-08-24 20:08:19 +0000 | [diff] [blame] | 675 | { | 
| Raymond Hettinger | 90a39bf | 2004-02-15 03:57:00 +0000 | [diff] [blame] | 676 | 	PyObject *it;      /* iter(v) */ | 
 | 677 | 	int m;		   /* size of self */ | 
 | 678 | 	int n;		   /* guess for size of b */ | 
 | 679 | 	int mn;		   /* m + n */ | 
 | 680 | 	int i; | 
| Raymond Hettinger | 57c4542 | 2004-03-11 09:48:18 +0000 | [diff] [blame] | 681 | 	PyObject *(*iternext)(PyObject *); | 
| Thomas Wouters | e289e0b | 2000-08-24 20:08:19 +0000 | [diff] [blame] | 682 |  | 
| Raymond Hettinger | 90a39bf | 2004-02-15 03:57:00 +0000 | [diff] [blame] | 683 | 	/* Special cases: | 
 | 684 | 	   1) lists and tuples which can use PySequence_Fast ops  | 
 | 685 | 	   2) extending self to self requires making a copy first  | 
 | 686 | 	*/ | 
 | 687 | 	if (PyList_CheckExact(b) || PyTuple_CheckExact(b) || (PyObject *)self == b) { | 
| Armin Rigo | 70d172d | 2004-03-20 22:19:23 +0000 | [diff] [blame] | 688 | 		PyObject **src, **dest; | 
| Raymond Hettinger | 90a39bf | 2004-02-15 03:57:00 +0000 | [diff] [blame] | 689 | 		b = PySequence_Fast(b, "argument must be iterable"); | 
 | 690 | 		if (!b) | 
 | 691 | 			return NULL; | 
| Armin Rigo | 70d172d | 2004-03-20 22:19:23 +0000 | [diff] [blame] | 692 | 		n = PySequence_Fast_GET_SIZE(b); | 
 | 693 | 		if (n == 0) { | 
 | 694 | 			/* short circuit when b is empty */ | 
 | 695 | 			Py_DECREF(b); | 
 | 696 | 			Py_RETURN_NONE; | 
 | 697 | 		} | 
 | 698 | 		m = self->ob_size; | 
 | 699 | 		if (list_resize(self, m + n) == -1) { | 
 | 700 | 			Py_DECREF(b); | 
| Raymond Hettinger | 90a39bf | 2004-02-15 03:57:00 +0000 | [diff] [blame] | 701 | 			return NULL; | 
| Armin Rigo | 70d172d | 2004-03-20 22:19:23 +0000 | [diff] [blame] | 702 | 		} | 
 | 703 | 		/* note that we may still have self == b here for the | 
 | 704 | 		 * situation a.extend(a), but the following code works | 
 | 705 | 		 * in that case too.  Just make sure to resize self | 
 | 706 | 		 * before calling PySequence_Fast_ITEMS. | 
 | 707 | 		 */ | 
 | 708 | 		/* populate the end of self with b's items */ | 
 | 709 | 		src = PySequence_Fast_ITEMS(b); | 
 | 710 | 		dest = self->ob_item + m; | 
 | 711 | 		for (i = 0; i < n; i++) { | 
 | 712 | 			PyObject *o = src[i]; | 
 | 713 | 			Py_INCREF(o); | 
 | 714 | 			dest[i] = o; | 
 | 715 | 		} | 
 | 716 | 		Py_DECREF(b); | 
| Raymond Hettinger | 90a39bf | 2004-02-15 03:57:00 +0000 | [diff] [blame] | 717 | 		Py_RETURN_NONE; | 
 | 718 | 	} | 
 | 719 |  | 
 | 720 | 	it = PyObject_GetIter(b); | 
 | 721 | 	if (it == NULL) | 
| Thomas Wouters | e289e0b | 2000-08-24 20:08:19 +0000 | [diff] [blame] | 722 | 		return NULL; | 
| Raymond Hettinger | 57c4542 | 2004-03-11 09:48:18 +0000 | [diff] [blame] | 723 | 	iternext = *it->ob_type->tp_iternext; | 
| Thomas Wouters | e289e0b | 2000-08-24 20:08:19 +0000 | [diff] [blame] | 724 |  | 
| Raymond Hettinger | 90a39bf | 2004-02-15 03:57:00 +0000 | [diff] [blame] | 725 | 	/* Guess a result list size. */ | 
 | 726 | 	n = PyObject_Size(b); | 
 | 727 | 	if (n < 0) { | 
 | 728 | 		PyErr_Clear(); | 
 | 729 | 		n = 8;	/* arbitrary */ | 
 | 730 | 	} | 
 | 731 | 	m = self->ob_size; | 
 | 732 | 	mn = m + n; | 
| Raymond Hettinger | d4ff741 | 2004-03-15 09:01:31 +0000 | [diff] [blame] | 733 | 	if (list_resize(self, mn) == -1) | 
| Raymond Hettinger | 90a39bf | 2004-02-15 03:57:00 +0000 | [diff] [blame] | 734 | 		goto error; | 
 | 735 | 	memset(&(self->ob_item[m]), 0, sizeof(*self->ob_item) * n); | 
| Thomas Wouters | e289e0b | 2000-08-24 20:08:19 +0000 | [diff] [blame] | 736 |  | 
| Raymond Hettinger | 90a39bf | 2004-02-15 03:57:00 +0000 | [diff] [blame] | 737 | 	/* Run iterator to exhaustion. */ | 
 | 738 | 	for (i = m; ; i++) { | 
| Raymond Hettinger | 57c4542 | 2004-03-11 09:48:18 +0000 | [diff] [blame] | 739 | 		PyObject *item = iternext(it); | 
| Raymond Hettinger | 90a39bf | 2004-02-15 03:57:00 +0000 | [diff] [blame] | 740 | 		if (item == NULL) { | 
| Raymond Hettinger | 57c4542 | 2004-03-11 09:48:18 +0000 | [diff] [blame] | 741 | 			if (PyErr_Occurred()) { | 
 | 742 | 				if (PyErr_ExceptionMatches(PyExc_StopIteration)) | 
 | 743 | 					PyErr_Clear(); | 
 | 744 | 				else | 
 | 745 | 					goto error; | 
 | 746 | 			} | 
| Raymond Hettinger | 90a39bf | 2004-02-15 03:57:00 +0000 | [diff] [blame] | 747 | 			break; | 
 | 748 | 		} | 
 | 749 | 		if (i < mn) | 
 | 750 | 			PyList_SET_ITEM(self, i, item); /* steals ref */ | 
 | 751 | 		else { | 
| Raymond Hettinger | 40a0382 | 2004-04-12 13:05:09 +0000 | [diff] [blame] | 752 | 			int status = app1(self, item); | 
| Raymond Hettinger | 90a39bf | 2004-02-15 03:57:00 +0000 | [diff] [blame] | 753 | 			Py_DECREF(item);  /* append creates a new ref */ | 
 | 754 | 			if (status < 0) | 
 | 755 | 				goto error; | 
 | 756 | 		} | 
 | 757 | 	} | 
 | 758 |  | 
 | 759 | 	/* Cut back result list if initial guess was too large. */ | 
 | 760 | 	if (i < mn && self != NULL) { | 
 | 761 | 		if (list_ass_slice(self, i, mn, (PyObject *)NULL) != 0) | 
 | 762 | 			goto error; | 
 | 763 | 	} | 
 | 764 | 	Py_DECREF(it); | 
 | 765 | 	Py_RETURN_NONE; | 
 | 766 |  | 
 | 767 |   error: | 
 | 768 | 	Py_DECREF(it); | 
 | 769 | 	return NULL; | 
| Thomas Wouters | e289e0b | 2000-08-24 20:08:19 +0000 | [diff] [blame] | 770 | } | 
 | 771 |  | 
| Raymond Hettinger | 8ca92ae | 2004-03-11 09:13:12 +0000 | [diff] [blame] | 772 | PyObject * | 
 | 773 | _PyList_Extend(PyListObject *self, PyObject *b) | 
 | 774 | { | 
 | 775 | 	return listextend(self, b); | 
 | 776 | } | 
 | 777 |  | 
| Thomas Wouters | e289e0b | 2000-08-24 20:08:19 +0000 | [diff] [blame] | 778 | static PyObject * | 
| Raymond Hettinger | 97bc618 | 2004-03-11 07:34:19 +0000 | [diff] [blame] | 779 | list_inplace_concat(PyListObject *self, PyObject *other) | 
 | 780 | { | 
 | 781 | 	PyObject *result; | 
 | 782 |  | 
 | 783 | 	result = listextend(self, other); | 
 | 784 | 	if (result == NULL) | 
 | 785 | 		return result; | 
 | 786 | 	Py_DECREF(result); | 
 | 787 | 	Py_INCREF(self); | 
 | 788 | 	return (PyObject *)self; | 
 | 789 | } | 
 | 790 |  | 
 | 791 | static PyObject * | 
| Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 792 | listpop(PyListObject *self, PyObject *args) | 
| Guido van Rossum | 3dd7f3f | 1998-06-30 15:36:32 +0000 | [diff] [blame] | 793 | { | 
 | 794 | 	int i = -1; | 
| Raymond Hettinger | 9eb86b3 | 2004-02-17 11:36:16 +0000 | [diff] [blame] | 795 | 	PyObject *v, *arg = NULL; | 
 | 796 |  | 
 | 797 | 	if (!PyArg_UnpackTuple(args, "pop", 0, 1, &arg)) | 
| Guido van Rossum | 3dd7f3f | 1998-06-30 15:36:32 +0000 | [diff] [blame] | 798 | 		return NULL; | 
| Raymond Hettinger | 9eb86b3 | 2004-02-17 11:36:16 +0000 | [diff] [blame] | 799 | 	if (arg != NULL) { | 
 | 800 | 		if (PyInt_Check(arg)) | 
 | 801 | 			i = (int)(PyInt_AS_LONG((PyIntObject*) arg)); | 
| Raymond Hettinger | fa6c6f8 | 2004-02-19 06:12:06 +0000 | [diff] [blame] | 802 | 		else if (!PyArg_ParseTuple(args, "|i:pop", &i)) | 
 | 803 |    			return NULL; | 
| Raymond Hettinger | 9eb86b3 | 2004-02-17 11:36:16 +0000 | [diff] [blame] | 804 | 	} | 
| Guido van Rossum | 3dd7f3f | 1998-06-30 15:36:32 +0000 | [diff] [blame] | 805 | 	if (self->ob_size == 0) { | 
 | 806 | 		/* Special-case most common failure cause */ | 
 | 807 | 		PyErr_SetString(PyExc_IndexError, "pop from empty list"); | 
 | 808 | 		return NULL; | 
 | 809 | 	} | 
 | 810 | 	if (i < 0) | 
 | 811 | 		i += self->ob_size; | 
 | 812 | 	if (i < 0 || i >= self->ob_size) { | 
 | 813 | 		PyErr_SetString(PyExc_IndexError, "pop index out of range"); | 
 | 814 | 		return NULL; | 
 | 815 | 	} | 
 | 816 | 	v = self->ob_item[i]; | 
| Raymond Hettinger | cb3e580 | 2004-02-13 18:36:31 +0000 | [diff] [blame] | 817 | 	if (i == self->ob_size - 1) { | 
| Raymond Hettinger | d4ff741 | 2004-03-15 09:01:31 +0000 | [diff] [blame] | 818 | 		if (list_resize(self, self->ob_size - 1) == -1) | 
| Raymond Hettinger | cb3e580 | 2004-02-13 18:36:31 +0000 | [diff] [blame] | 819 | 			return NULL; | 
 | 820 | 		return v; | 
 | 821 | 	} | 
| Guido van Rossum | 3dd7f3f | 1998-06-30 15:36:32 +0000 | [diff] [blame] | 822 | 	Py_INCREF(v); | 
 | 823 | 	if (list_ass_slice(self, i, i+1, (PyObject *)NULL) != 0) { | 
 | 824 | 		Py_DECREF(v); | 
 | 825 | 		return NULL; | 
 | 826 | 	} | 
 | 827 | 	return v; | 
 | 828 | } | 
 | 829 |  | 
| Tim Peters | 8e2e7ca | 2002-07-19 02:33:08 +0000 | [diff] [blame] | 830 | /* Reverse a slice of a list in place, from lo up to (exclusive) hi. */ | 
 | 831 | static void | 
 | 832 | reverse_slice(PyObject **lo, PyObject **hi) | 
 | 833 | { | 
 | 834 | 	assert(lo && hi); | 
 | 835 |  | 
 | 836 | 	--hi; | 
 | 837 | 	while (lo < hi) { | 
 | 838 | 		PyObject *t = *lo; | 
 | 839 | 		*lo = *hi; | 
 | 840 | 		*hi = t; | 
 | 841 | 		++lo; | 
 | 842 | 		--hi; | 
 | 843 | 	} | 
 | 844 | } | 
 | 845 |  | 
| Tim Peters | a64dc24 | 2002-08-01 02:13:36 +0000 | [diff] [blame] | 846 | /* Lots of code for an adaptive, stable, natural mergesort.  There are many | 
 | 847 |  * pieces to this algorithm; read listsort.txt for overviews and details. | 
 | 848 |  */ | 
| Guido van Rossum | 3f236de | 1996-12-10 23:55:39 +0000 | [diff] [blame] | 849 |  | 
| Guido van Rossum | 3f236de | 1996-12-10 23:55:39 +0000 | [diff] [blame] | 850 | /* Comparison function.  Takes care of calling a user-supplied | 
| Tim Peters | 66860f6 | 2002-08-04 17:47:26 +0000 | [diff] [blame] | 851 |  * comparison function (any callable Python object), which must not be | 
 | 852 |  * NULL (use the ISLT macro if you don't know, or call PyObject_RichCompareBool | 
 | 853 |  * with Py_LT if you know it's NULL). | 
| Tim Peters | a64dc24 | 2002-08-01 02:13:36 +0000 | [diff] [blame] | 854 |  * Returns -1 on error, 1 if x < y, 0 if x >= y. | 
 | 855 |  */ | 
| Guido van Rossum | 3f236de | 1996-12-10 23:55:39 +0000 | [diff] [blame] | 856 | static int | 
| Tim Peters | a8c974c | 2002-07-19 03:30:57 +0000 | [diff] [blame] | 857 | islt(PyObject *x, PyObject *y, PyObject *compare) | 
| Guido van Rossum | 3f236de | 1996-12-10 23:55:39 +0000 | [diff] [blame] | 858 | { | 
| Tim Peters | f2a0473 | 2002-07-11 21:46:16 +0000 | [diff] [blame] | 859 | 	PyObject *res; | 
 | 860 | 	PyObject *args; | 
| Guido van Rossum | 3f236de | 1996-12-10 23:55:39 +0000 | [diff] [blame] | 861 | 	int i; | 
 | 862 |  | 
| Tim Peters | 66860f6 | 2002-08-04 17:47:26 +0000 | [diff] [blame] | 863 | 	assert(compare != NULL); | 
| Tim Peters | a8c974c | 2002-07-19 03:30:57 +0000 | [diff] [blame] | 864 | 	/* Call the user's comparison function and translate the 3-way | 
 | 865 | 	 * result into true or false (or error). | 
 | 866 | 	 */ | 
| Tim Peters | f2a0473 | 2002-07-11 21:46:16 +0000 | [diff] [blame] | 867 | 	args = PyTuple_New(2); | 
| Guido van Rossum | 3f236de | 1996-12-10 23:55:39 +0000 | [diff] [blame] | 868 | 	if (args == NULL) | 
| Tim Peters | a8c974c | 2002-07-19 03:30:57 +0000 | [diff] [blame] | 869 | 		return -1; | 
| Tim Peters | f2a0473 | 2002-07-11 21:46:16 +0000 | [diff] [blame] | 870 | 	Py_INCREF(x); | 
 | 871 | 	Py_INCREF(y); | 
 | 872 | 	PyTuple_SET_ITEM(args, 0, x); | 
 | 873 | 	PyTuple_SET_ITEM(args, 1, y); | 
| Tim Peters | 58cf361 | 2002-07-15 05:16:13 +0000 | [diff] [blame] | 874 | 	res = PyObject_Call(compare, args, NULL); | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 875 | 	Py_DECREF(args); | 
| Guido van Rossum | 3f236de | 1996-12-10 23:55:39 +0000 | [diff] [blame] | 876 | 	if (res == NULL) | 
| Tim Peters | a8c974c | 2002-07-19 03:30:57 +0000 | [diff] [blame] | 877 | 		return -1; | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 878 | 	if (!PyInt_Check(res)) { | 
 | 879 | 		Py_DECREF(res); | 
 | 880 | 		PyErr_SetString(PyExc_TypeError, | 
| Guido van Rossum | 9bcd1d7 | 1999-04-19 17:44:39 +0000 | [diff] [blame] | 881 | 				"comparison function must return int"); | 
| Tim Peters | a8c974c | 2002-07-19 03:30:57 +0000 | [diff] [blame] | 882 | 		return -1; | 
| Guido van Rossum | 3f236de | 1996-12-10 23:55:39 +0000 | [diff] [blame] | 883 | 	} | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 884 | 	i = PyInt_AsLong(res); | 
 | 885 | 	Py_DECREF(res); | 
| Tim Peters | a8c974c | 2002-07-19 03:30:57 +0000 | [diff] [blame] | 886 | 	return i < 0; | 
| Guido van Rossum | 3f236de | 1996-12-10 23:55:39 +0000 | [diff] [blame] | 887 | } | 
 | 888 |  | 
| Tim Peters | 66860f6 | 2002-08-04 17:47:26 +0000 | [diff] [blame] | 889 | /* If COMPARE is NULL, calls PyObject_RichCompareBool with Py_LT, else calls | 
 | 890 |  * islt.  This avoids a layer of function call in the usual case, and | 
 | 891 |  * sorting does many comparisons. | 
 | 892 |  * Returns -1 on error, 1 if x < y, 0 if x >= y. | 
 | 893 |  */ | 
 | 894 | #define ISLT(X, Y, COMPARE) ((COMPARE) == NULL ?			\ | 
 | 895 | 			     PyObject_RichCompareBool(X, Y, Py_LT) :	\ | 
 | 896 | 			     islt(X, Y, COMPARE)) | 
 | 897 |  | 
 | 898 | /* Compare X to Y via "<".  Goto "fail" if the comparison raises an | 
| Tim Peters | a8c974c | 2002-07-19 03:30:57 +0000 | [diff] [blame] | 899 |    error.  Else "k" is set to true iff X<Y, and an "if (k)" block is | 
 | 900 |    started.  It makes more sense in context <wink>.  X and Y are PyObject*s. | 
 | 901 | */ | 
| Tim Peters | 66860f6 | 2002-08-04 17:47:26 +0000 | [diff] [blame] | 902 | #define IFLT(X, Y) if ((k = ISLT(X, Y, compare)) < 0) goto fail;  \ | 
| Tim Peters | a8c974c | 2002-07-19 03:30:57 +0000 | [diff] [blame] | 903 | 		   if (k) | 
| Guido van Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 904 |  | 
 | 905 | /* binarysort is the best method for sorting small arrays: it does | 
 | 906 |    few compares, but can do data movement quadratic in the number of | 
 | 907 |    elements. | 
| Guido van Rossum | 4281258 | 1998-06-17 14:15:44 +0000 | [diff] [blame] | 908 |    [lo, hi) is a contiguous slice of a list, and is sorted via | 
| Tim Peters | a8c974c | 2002-07-19 03:30:57 +0000 | [diff] [blame] | 909 |    binary insertion.  This sort is stable. | 
| Guido van Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 910 |    On entry, must have lo <= start <= hi, and that [lo, start) is already | 
 | 911 |    sorted (pass start == lo if you don't know!). | 
| Tim Peters | a8c974c | 2002-07-19 03:30:57 +0000 | [diff] [blame] | 912 |    If islt() complains return -1, else 0. | 
| Guido van Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 913 |    Even in case of error, the output slice will be some permutation of | 
 | 914 |    the input (nothing is lost or duplicated). | 
 | 915 | */ | 
| Guido van Rossum | 3f236de | 1996-12-10 23:55:39 +0000 | [diff] [blame] | 916 | static int | 
| Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 917 | binarysort(PyObject **lo, PyObject **hi, PyObject **start, PyObject *compare) | 
 | 918 |      /* compare -- comparison function object, or NULL for default */ | 
| Guido van Rossum | 3f236de | 1996-12-10 23:55:39 +0000 | [diff] [blame] | 919 | { | 
| Guido van Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 920 | 	register int k; | 
 | 921 | 	register PyObject **l, **p, **r; | 
 | 922 | 	register PyObject *pivot; | 
| Guido van Rossum | 3f236de | 1996-12-10 23:55:39 +0000 | [diff] [blame] | 923 |  | 
| Tim Peters | a8c974c | 2002-07-19 03:30:57 +0000 | [diff] [blame] | 924 | 	assert(lo <= start && start <= hi); | 
 | 925 | 	/* assert [lo, start) is sorted */ | 
| Guido van Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 926 | 	if (lo == start) | 
 | 927 | 		++start; | 
 | 928 | 	for (; start < hi; ++start) { | 
 | 929 | 		/* set l to where *start belongs */ | 
 | 930 | 		l = lo; | 
 | 931 | 		r = start; | 
| Guido van Rossum | a119c0d | 1998-05-29 17:56:32 +0000 | [diff] [blame] | 932 | 		pivot = *r; | 
| Tim Peters | 0fe977c | 2002-07-19 06:12:32 +0000 | [diff] [blame] | 933 | 		/* Invariants: | 
 | 934 | 		 * pivot >= all in [lo, l). | 
 | 935 | 		 * pivot  < all in [r, start). | 
 | 936 | 		 * The second is vacuously true at the start. | 
 | 937 | 		 */ | 
 | 938 | 		assert(l < r); | 
| Guido van Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 939 | 		do { | 
 | 940 | 			p = l + ((r - l) >> 1); | 
| Tim Peters | a8c974c | 2002-07-19 03:30:57 +0000 | [diff] [blame] | 941 | 			IFLT(pivot, *p) | 
| Guido van Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 942 | 				r = p; | 
 | 943 | 			else | 
| Tim Peters | 0fe977c | 2002-07-19 06:12:32 +0000 | [diff] [blame] | 944 | 				l = p+1; | 
| Guido van Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 945 | 		} while (l < r); | 
| Tim Peters | 0fe977c | 2002-07-19 06:12:32 +0000 | [diff] [blame] | 946 | 		assert(l == r); | 
 | 947 | 		/* The invariants still hold, so pivot >= all in [lo, l) and | 
 | 948 | 		   pivot < all in [l, start), so pivot belongs at l.  Note | 
 | 949 | 		   that if there are elements equal to pivot, l points to the | 
 | 950 | 		   first slot after them -- that's why this sort is stable. | 
 | 951 | 		   Slide over to make room. | 
| Guido van Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 952 | 		   Caution: using memmove is much slower under MSVC 5; | 
 | 953 | 		   we're not usually moving many slots. */ | 
 | 954 | 		for (p = start; p > l; --p) | 
 | 955 | 			*p = *(p-1); | 
 | 956 | 		*l = pivot; | 
| Guido van Rossum | 3f236de | 1996-12-10 23:55:39 +0000 | [diff] [blame] | 957 | 	} | 
| Guido van Rossum | 3f236de | 1996-12-10 23:55:39 +0000 | [diff] [blame] | 958 | 	return 0; | 
| Guido van Rossum | a119c0d | 1998-05-29 17:56:32 +0000 | [diff] [blame] | 959 |  | 
 | 960 |  fail: | 
 | 961 | 	return -1; | 
| Guido van Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 962 | } | 
 | 963 |  | 
| Tim Peters | a64dc24 | 2002-08-01 02:13:36 +0000 | [diff] [blame] | 964 | /* | 
 | 965 | Return the length of the run beginning at lo, in the slice [lo, hi).  lo < hi | 
 | 966 | is required on entry.  "A run" is the longest ascending sequence, with | 
| Guido van Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 967 |  | 
| Tim Peters | a64dc24 | 2002-08-01 02:13:36 +0000 | [diff] [blame] | 968 |     lo[0] <= lo[1] <= lo[2] <= ... | 
| Guido van Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 969 |  | 
| Tim Peters | a64dc24 | 2002-08-01 02:13:36 +0000 | [diff] [blame] | 970 | or the longest descending sequence, with | 
| Guido van Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 971 |  | 
| Tim Peters | a64dc24 | 2002-08-01 02:13:36 +0000 | [diff] [blame] | 972 |     lo[0] > lo[1] > lo[2] > ... | 
| Tim Peters | 3b01a12 | 2002-07-19 02:35:45 +0000 | [diff] [blame] | 973 |  | 
| Tim Peters | a64dc24 | 2002-08-01 02:13:36 +0000 | [diff] [blame] | 974 | Boolean *descending is set to 0 in the former case, or to 1 in the latter. | 
 | 975 | For its intended use in a stable mergesort, the strictness of the defn of | 
 | 976 | "descending" is needed so that the caller can safely reverse a descending | 
 | 977 | sequence without violating stability (strict > ensures there are no equal | 
 | 978 | elements to get out of order). | 
 | 979 |  | 
 | 980 | Returns -1 in case of error. | 
| Guido van Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 981 | */ | 
| Guido van Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 982 | static int | 
| Tim Peters | a64dc24 | 2002-08-01 02:13:36 +0000 | [diff] [blame] | 983 | count_run(PyObject **lo, PyObject **hi, PyObject *compare, int *descending) | 
| Guido van Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 984 | { | 
| Tim Peters | a64dc24 | 2002-08-01 02:13:36 +0000 | [diff] [blame] | 985 | 	int k; | 
 | 986 | 	int n; | 
| Guido van Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 987 |  | 
| Tim Peters | a64dc24 | 2002-08-01 02:13:36 +0000 | [diff] [blame] | 988 | 	assert(lo < hi); | 
 | 989 | 	*descending = 0; | 
 | 990 | 	++lo; | 
 | 991 | 	if (lo == hi) | 
 | 992 | 		return 1; | 
| Guido van Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 993 |  | 
| Tim Peters | a64dc24 | 2002-08-01 02:13:36 +0000 | [diff] [blame] | 994 | 	n = 2; | 
 | 995 | 	IFLT(*lo, *(lo-1)) { | 
 | 996 | 		*descending = 1; | 
 | 997 | 		for (lo = lo+1; lo < hi; ++lo, ++n) { | 
 | 998 | 			IFLT(*lo, *(lo-1)) | 
 | 999 | 				; | 
 | 1000 | 			else | 
 | 1001 | 				break; | 
 | 1002 | 		} | 
| Guido van Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 1003 | 	} | 
| Tim Peters | a64dc24 | 2002-08-01 02:13:36 +0000 | [diff] [blame] | 1004 | 	else { | 
 | 1005 | 		for (lo = lo+1; lo < hi; ++lo, ++n) { | 
 | 1006 | 			IFLT(*lo, *(lo-1)) | 
 | 1007 | 				break; | 
| Guido van Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 1008 | 		} | 
 | 1009 | 	} | 
 | 1010 |  | 
| Tim Peters | a64dc24 | 2002-08-01 02:13:36 +0000 | [diff] [blame] | 1011 | 	return n; | 
 | 1012 | fail: | 
 | 1013 | 	return -1; | 
 | 1014 | } | 
| Guido van Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 1015 |  | 
| Tim Peters | a64dc24 | 2002-08-01 02:13:36 +0000 | [diff] [blame] | 1016 | /* | 
 | 1017 | Locate the proper position of key in a sorted vector; if the vector contains | 
 | 1018 | an element equal to key, return the position immediately to the left of | 
 | 1019 | the leftmost equal element.  [gallop_right() does the same except returns | 
 | 1020 | the position to the right of the rightmost equal element (if any).] | 
| Guido van Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 1021 |  | 
| Tim Peters | a64dc24 | 2002-08-01 02:13:36 +0000 | [diff] [blame] | 1022 | "a" is a sorted vector with n elements, starting at a[0].  n must be > 0. | 
| Guido van Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 1023 |  | 
| Tim Peters | a64dc24 | 2002-08-01 02:13:36 +0000 | [diff] [blame] | 1024 | "hint" is an index at which to begin the search, 0 <= hint < n.  The closer | 
 | 1025 | hint is to the final result, the faster this runs. | 
 | 1026 |  | 
 | 1027 | The return value is the int k in 0..n such that | 
 | 1028 |  | 
 | 1029 |     a[k-1] < key <= a[k] | 
 | 1030 |  | 
 | 1031 | pretending that *(a-1) is minus infinity and a[n] is plus infinity.  IOW, | 
 | 1032 | key belongs at index k; or, IOW, the first k elements of a should precede | 
 | 1033 | key, and the last n-k should follow key. | 
 | 1034 |  | 
 | 1035 | Returns -1 on error.  See listsort.txt for info on the method. | 
 | 1036 | */ | 
 | 1037 | static int | 
 | 1038 | gallop_left(PyObject *key, PyObject **a, int n, int hint, PyObject *compare) | 
 | 1039 | { | 
 | 1040 | 	int ofs; | 
 | 1041 | 	int lastofs; | 
 | 1042 | 	int k; | 
 | 1043 |  | 
 | 1044 | 	assert(key && a && n > 0 && hint >= 0 && hint < n); | 
 | 1045 |  | 
 | 1046 | 	a += hint; | 
 | 1047 | 	lastofs = 0; | 
 | 1048 | 	ofs = 1; | 
 | 1049 | 	IFLT(*a, key) { | 
 | 1050 | 		/* a[hint] < key -- gallop right, until | 
 | 1051 | 		 * a[hint + lastofs] < key <= a[hint + ofs] | 
 | 1052 | 		 */ | 
 | 1053 | 		const int maxofs = n - hint;	/* &a[n-1] is highest */ | 
 | 1054 | 		while (ofs < maxofs) { | 
 | 1055 | 			IFLT(a[ofs], key) { | 
 | 1056 | 				lastofs = ofs; | 
 | 1057 | 				ofs = (ofs << 1) + 1; | 
 | 1058 | 				if (ofs <= 0)	/* int overflow */ | 
 | 1059 | 					ofs = maxofs; | 
 | 1060 | 			} | 
 | 1061 |  			else	/* key <= a[hint + ofs] */ | 
 | 1062 | 				break; | 
 | 1063 | 		} | 
 | 1064 | 		if (ofs > maxofs) | 
 | 1065 | 			ofs = maxofs; | 
 | 1066 | 		/* Translate back to offsets relative to &a[0]. */ | 
 | 1067 | 		lastofs += hint; | 
 | 1068 | 		ofs += hint; | 
 | 1069 | 	} | 
 | 1070 | 	else { | 
 | 1071 | 		/* key <= a[hint] -- gallop left, until | 
 | 1072 | 		 * a[hint - ofs] < key <= a[hint - lastofs] | 
 | 1073 | 		 */ | 
 | 1074 | 		const int maxofs = hint + 1;	/* &a[0] is lowest */ | 
 | 1075 | 		while (ofs < maxofs) { | 
 | 1076 | 			IFLT(*(a-ofs), key) | 
 | 1077 | 				break; | 
 | 1078 | 			/* key <= a[hint - ofs] */ | 
 | 1079 | 			lastofs = ofs; | 
 | 1080 | 			ofs = (ofs << 1) + 1; | 
 | 1081 | 			if (ofs <= 0)	/* int overflow */ | 
 | 1082 | 				ofs = maxofs; | 
 | 1083 | 		} | 
 | 1084 | 		if (ofs > maxofs) | 
 | 1085 | 			ofs = maxofs; | 
 | 1086 | 		/* Translate back to positive offsets relative to &a[0]. */ | 
 | 1087 | 		k = lastofs; | 
 | 1088 | 		lastofs = hint - ofs; | 
 | 1089 | 		ofs = hint - k; | 
 | 1090 | 	} | 
 | 1091 | 	a -= hint; | 
 | 1092 |  | 
 | 1093 | 	assert(-1 <= lastofs && lastofs < ofs && ofs <= n); | 
 | 1094 | 	/* Now a[lastofs] < key <= a[ofs], so key belongs somewhere to the | 
 | 1095 | 	 * right of lastofs but no farther right than ofs.  Do a binary | 
 | 1096 | 	 * search, with invariant a[lastofs-1] < key <= a[ofs]. | 
 | 1097 | 	 */ | 
 | 1098 | 	++lastofs; | 
 | 1099 | 	while (lastofs < ofs) { | 
 | 1100 | 		int m = lastofs + ((ofs - lastofs) >> 1); | 
 | 1101 |  | 
 | 1102 | 		IFLT(a[m], key) | 
 | 1103 | 			lastofs = m+1;	/* a[m] < key */ | 
 | 1104 | 		else | 
 | 1105 | 			ofs = m;	/* key <= a[m] */ | 
 | 1106 | 	} | 
 | 1107 | 	assert(lastofs == ofs);		/* so a[ofs-1] < key <= a[ofs] */ | 
 | 1108 | 	return ofs; | 
 | 1109 |  | 
 | 1110 | fail: | 
 | 1111 | 	return -1; | 
 | 1112 | } | 
 | 1113 |  | 
 | 1114 | /* | 
 | 1115 | Exactly like gallop_left(), except that if key already exists in a[0:n], | 
 | 1116 | finds the position immediately to the right of the rightmost equal value. | 
 | 1117 |  | 
 | 1118 | The return value is the int k in 0..n such that | 
 | 1119 |  | 
 | 1120 |     a[k-1] <= key < a[k] | 
 | 1121 |  | 
 | 1122 | or -1 if error. | 
 | 1123 |  | 
 | 1124 | The code duplication is massive, but this is enough different given that | 
 | 1125 | we're sticking to "<" comparisons that it's much harder to follow if | 
 | 1126 | written as one routine with yet another "left or right?" flag. | 
 | 1127 | */ | 
 | 1128 | static int | 
 | 1129 | gallop_right(PyObject *key, PyObject **a, int n, int hint, PyObject *compare) | 
 | 1130 | { | 
 | 1131 | 	int ofs; | 
 | 1132 | 	int lastofs; | 
 | 1133 | 	int k; | 
 | 1134 |  | 
 | 1135 | 	assert(key && a && n > 0 && hint >= 0 && hint < n); | 
 | 1136 |  | 
 | 1137 | 	a += hint; | 
 | 1138 | 	lastofs = 0; | 
 | 1139 | 	ofs = 1; | 
 | 1140 | 	IFLT(key, *a) { | 
 | 1141 | 		/* key < a[hint] -- gallop left, until | 
 | 1142 | 		 * a[hint - ofs] <= key < a[hint - lastofs] | 
 | 1143 | 		 */ | 
 | 1144 | 		const int maxofs = hint + 1;	/* &a[0] is lowest */ | 
 | 1145 | 		while (ofs < maxofs) { | 
 | 1146 | 			IFLT(key, *(a-ofs)) { | 
 | 1147 | 				lastofs = ofs; | 
 | 1148 | 				ofs = (ofs << 1) + 1; | 
 | 1149 | 				if (ofs <= 0)	/* int overflow */ | 
 | 1150 | 					ofs = maxofs; | 
 | 1151 | 			} | 
 | 1152 | 			else	/* a[hint - ofs] <= key */ | 
 | 1153 | 				break; | 
 | 1154 | 		} | 
 | 1155 | 		if (ofs > maxofs) | 
 | 1156 | 			ofs = maxofs; | 
 | 1157 | 		/* Translate back to positive offsets relative to &a[0]. */ | 
 | 1158 | 		k = lastofs; | 
 | 1159 | 		lastofs = hint - ofs; | 
 | 1160 | 		ofs = hint - k; | 
 | 1161 | 	} | 
 | 1162 | 	else { | 
 | 1163 | 		/* a[hint] <= key -- gallop right, until | 
 | 1164 | 		 * a[hint + lastofs] <= key < a[hint + ofs] | 
| Guido van Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 1165 | 		*/ | 
| Tim Peters | a64dc24 | 2002-08-01 02:13:36 +0000 | [diff] [blame] | 1166 | 		const int maxofs = n - hint;	/* &a[n-1] is highest */ | 
 | 1167 | 		while (ofs < maxofs) { | 
 | 1168 | 			IFLT(key, a[ofs]) | 
 | 1169 | 				break; | 
 | 1170 | 			/* a[hint + ofs] <= key */ | 
 | 1171 | 			lastofs = ofs; | 
 | 1172 | 			ofs = (ofs << 1) + 1; | 
 | 1173 | 			if (ofs <= 0)	/* int overflow */ | 
 | 1174 | 				ofs = maxofs; | 
 | 1175 | 		} | 
 | 1176 | 		if (ofs > maxofs) | 
 | 1177 | 			ofs = maxofs; | 
 | 1178 | 		/* Translate back to offsets relative to &a[0]. */ | 
 | 1179 | 		lastofs += hint; | 
 | 1180 | 		ofs += hint; | 
 | 1181 | 	} | 
 | 1182 | 	a -= hint; | 
 | 1183 |  | 
 | 1184 | 	assert(-1 <= lastofs && lastofs < ofs && ofs <= n); | 
 | 1185 | 	/* Now a[lastofs] <= key < a[ofs], so key belongs somewhere to the | 
 | 1186 | 	 * right of lastofs but no farther right than ofs.  Do a binary | 
 | 1187 | 	 * search, with invariant a[lastofs-1] <= key < a[ofs]. | 
 | 1188 | 	 */ | 
 | 1189 | 	++lastofs; | 
 | 1190 | 	while (lastofs < ofs) { | 
 | 1191 | 		int m = lastofs + ((ofs - lastofs) >> 1); | 
 | 1192 |  | 
 | 1193 | 		IFLT(key, a[m]) | 
 | 1194 | 			ofs = m;	/* key < a[m] */ | 
 | 1195 | 		else | 
 | 1196 | 			lastofs = m+1;	/* a[m] <= key */ | 
 | 1197 | 	} | 
 | 1198 | 	assert(lastofs == ofs);		/* so a[ofs-1] <= key < a[ofs] */ | 
 | 1199 | 	return ofs; | 
 | 1200 |  | 
 | 1201 | fail: | 
 | 1202 | 	return -1; | 
 | 1203 | } | 
 | 1204 |  | 
 | 1205 | /* The maximum number of entries in a MergeState's pending-runs stack. | 
 | 1206 |  * This is enough to sort arrays of size up to about | 
 | 1207 |  *     32 * phi ** MAX_MERGE_PENDING | 
 | 1208 |  * where phi ~= 1.618.  85 is ridiculouslylarge enough, good for an array | 
 | 1209 |  * with 2**64 elements. | 
 | 1210 |  */ | 
 | 1211 | #define MAX_MERGE_PENDING 85 | 
 | 1212 |  | 
| Tim Peters | e05f65a | 2002-08-10 05:21:15 +0000 | [diff] [blame] | 1213 | /* When we get into galloping mode, we stay there until both runs win less | 
 | 1214 |  * often than MIN_GALLOP consecutive times.  See listsort.txt for more info. | 
| Tim Peters | a64dc24 | 2002-08-01 02:13:36 +0000 | [diff] [blame] | 1215 |  */ | 
| Tim Peters | e05f65a | 2002-08-10 05:21:15 +0000 | [diff] [blame] | 1216 | #define MIN_GALLOP 7 | 
| Tim Peters | a64dc24 | 2002-08-01 02:13:36 +0000 | [diff] [blame] | 1217 |  | 
 | 1218 | /* Avoid malloc for small temp arrays. */ | 
 | 1219 | #define MERGESTATE_TEMP_SIZE 256 | 
 | 1220 |  | 
 | 1221 | /* One MergeState exists on the stack per invocation of mergesort.  It's just | 
 | 1222 |  * a convenient way to pass state around among the helper functions. | 
 | 1223 |  */ | 
| Tim Peters | e05f65a | 2002-08-10 05:21:15 +0000 | [diff] [blame] | 1224 | struct s_slice { | 
 | 1225 | 	PyObject **base; | 
 | 1226 | 	int len; | 
 | 1227 | }; | 
 | 1228 |  | 
| Tim Peters | a64dc24 | 2002-08-01 02:13:36 +0000 | [diff] [blame] | 1229 | typedef struct s_MergeState { | 
 | 1230 | 	/* The user-supplied comparison function. or NULL if none given. */ | 
 | 1231 | 	PyObject *compare; | 
 | 1232 |  | 
| Tim Peters | e05f65a | 2002-08-10 05:21:15 +0000 | [diff] [blame] | 1233 | 	/* This controls when we get *into* galloping mode.  It's initialized | 
 | 1234 | 	 * to MIN_GALLOP.  merge_lo and merge_hi tend to nudge it higher for | 
 | 1235 | 	 * random data, and lower for highly structured data. | 
 | 1236 | 	 */ | 
 | 1237 | 	int min_gallop; | 
 | 1238 |  | 
| Tim Peters | a64dc24 | 2002-08-01 02:13:36 +0000 | [diff] [blame] | 1239 | 	/* 'a' is temp storage to help with merges.  It contains room for | 
 | 1240 | 	 * alloced entries. | 
 | 1241 | 	 */ | 
 | 1242 | 	PyObject **a;	/* may point to temparray below */ | 
 | 1243 | 	int alloced; | 
 | 1244 |  | 
 | 1245 | 	/* A stack of n pending runs yet to be merged.  Run #i starts at | 
 | 1246 | 	 * address base[i] and extends for len[i] elements.  It's always | 
 | 1247 | 	 * true (so long as the indices are in bounds) that | 
 | 1248 | 	 * | 
| Tim Peters | e05f65a | 2002-08-10 05:21:15 +0000 | [diff] [blame] | 1249 | 	 *     pending[i].base + pending[i].len == pending[i+1].base | 
| Tim Peters | a64dc24 | 2002-08-01 02:13:36 +0000 | [diff] [blame] | 1250 | 	 * | 
 | 1251 | 	 * so we could cut the storage for this, but it's a minor amount, | 
 | 1252 | 	 * and keeping all the info explicit simplifies the code. | 
 | 1253 | 	 */ | 
 | 1254 | 	int n; | 
| Tim Peters | e05f65a | 2002-08-10 05:21:15 +0000 | [diff] [blame] | 1255 | 	struct s_slice pending[MAX_MERGE_PENDING]; | 
| Tim Peters | a64dc24 | 2002-08-01 02:13:36 +0000 | [diff] [blame] | 1256 |  | 
 | 1257 | 	/* 'a' points to this when possible, rather than muck with malloc. */ | 
 | 1258 | 	PyObject *temparray[MERGESTATE_TEMP_SIZE]; | 
 | 1259 | } MergeState; | 
 | 1260 |  | 
 | 1261 | /* Conceptually a MergeState's constructor. */ | 
 | 1262 | static void | 
 | 1263 | merge_init(MergeState *ms, PyObject *compare) | 
 | 1264 | { | 
 | 1265 | 	assert(ms != NULL); | 
 | 1266 | 	ms->compare = compare; | 
 | 1267 | 	ms->a = ms->temparray; | 
 | 1268 | 	ms->alloced = MERGESTATE_TEMP_SIZE; | 
 | 1269 | 	ms->n = 0; | 
| Tim Peters | e05f65a | 2002-08-10 05:21:15 +0000 | [diff] [blame] | 1270 | 	ms->min_gallop = MIN_GALLOP; | 
| Tim Peters | a64dc24 | 2002-08-01 02:13:36 +0000 | [diff] [blame] | 1271 | } | 
 | 1272 |  | 
 | 1273 | /* Free all the temp memory owned by the MergeState.  This must be called | 
 | 1274 |  * when you're done with a MergeState, and may be called before then if | 
 | 1275 |  * you want to free the temp memory early. | 
 | 1276 |  */ | 
 | 1277 | static void | 
 | 1278 | merge_freemem(MergeState *ms) | 
 | 1279 | { | 
 | 1280 | 	assert(ms != NULL); | 
 | 1281 | 	if (ms->a != ms->temparray) | 
 | 1282 | 		PyMem_Free(ms->a); | 
 | 1283 | 	ms->a = ms->temparray; | 
 | 1284 | 	ms->alloced = MERGESTATE_TEMP_SIZE; | 
 | 1285 | } | 
 | 1286 |  | 
 | 1287 | /* Ensure enough temp memory for 'need' array slots is available. | 
 | 1288 |  * Returns 0 on success and -1 if the memory can't be gotten. | 
 | 1289 |  */ | 
 | 1290 | static int | 
 | 1291 | merge_getmem(MergeState *ms, int need) | 
 | 1292 | { | 
 | 1293 | 	assert(ms != NULL); | 
 | 1294 | 	if (need <= ms->alloced) | 
 | 1295 | 		return 0; | 
 | 1296 | 	/* Don't realloc!  That can cost cycles to copy the old data, but | 
 | 1297 | 	 * we don't care what's in the block. | 
 | 1298 | 	 */ | 
 | 1299 | 	merge_freemem(ms); | 
 | 1300 | 	ms->a = (PyObject **)PyMem_Malloc(need * sizeof(PyObject*)); | 
 | 1301 | 	if (ms->a) { | 
 | 1302 | 		ms->alloced = need; | 
 | 1303 | 		return 0; | 
 | 1304 | 	} | 
 | 1305 | 	PyErr_NoMemory(); | 
 | 1306 | 	merge_freemem(ms);	/* reset to sane state */ | 
 | 1307 | 	return -1; | 
 | 1308 | } | 
 | 1309 | #define MERGE_GETMEM(MS, NEED) ((NEED) <= (MS)->alloced ? 0 :	\ | 
 | 1310 | 				merge_getmem(MS, NEED)) | 
 | 1311 |  | 
 | 1312 | /* Merge the na elements starting at pa with the nb elements starting at pb | 
 | 1313 |  * in a stable way, in-place.  na and nb must be > 0, and pa + na == pb. | 
 | 1314 |  * Must also have that *pb < *pa, that pa[na-1] belongs at the end of the | 
 | 1315 |  * merge, and should have na <= nb.  See listsort.txt for more info. | 
 | 1316 |  * Return 0 if successful, -1 if error. | 
 | 1317 |  */ | 
 | 1318 | static int | 
 | 1319 | merge_lo(MergeState *ms, PyObject **pa, int na, PyObject **pb, int nb) | 
 | 1320 | { | 
 | 1321 | 	int k; | 
 | 1322 | 	PyObject *compare; | 
 | 1323 | 	PyObject **dest; | 
 | 1324 | 	int result = -1;	/* guilty until proved innocent */ | 
| Tim Peters | e05f65a | 2002-08-10 05:21:15 +0000 | [diff] [blame] | 1325 | 	int min_gallop = ms->min_gallop; | 
| Tim Peters | a64dc24 | 2002-08-01 02:13:36 +0000 | [diff] [blame] | 1326 |  | 
 | 1327 | 	assert(ms && pa && pb && na > 0 && nb > 0 && pa + na == pb); | 
 | 1328 | 	if (MERGE_GETMEM(ms, na) < 0) | 
 | 1329 | 		return -1; | 
 | 1330 | 	memcpy(ms->a, pa, na * sizeof(PyObject*)); | 
 | 1331 | 	dest = pa; | 
 | 1332 | 	pa = ms->a; | 
 | 1333 |  | 
 | 1334 | 	*dest++ = *pb++; | 
 | 1335 | 	--nb; | 
 | 1336 | 	if (nb == 0) | 
 | 1337 | 		goto Succeed; | 
 | 1338 | 	if (na == 1) | 
 | 1339 | 		goto CopyB; | 
 | 1340 |  | 
 | 1341 | 	compare = ms->compare; | 
 | 1342 | 	for (;;) { | 
 | 1343 | 		int acount = 0;	/* # of times A won in a row */ | 
 | 1344 | 		int bcount = 0;	/* # of times B won in a row */ | 
 | 1345 |  | 
 | 1346 | 		/* Do the straightforward thing until (if ever) one run | 
 | 1347 | 		 * appears to win consistently. | 
 | 1348 | 		 */ | 
 | 1349 |  		for (;;) { | 
| Tim Peters | e05f65a | 2002-08-10 05:21:15 +0000 | [diff] [blame] | 1350 |  			assert(na > 1 && nb > 0); | 
| Tim Peters | 66860f6 | 2002-08-04 17:47:26 +0000 | [diff] [blame] | 1351 | 	 		k = ISLT(*pb, *pa, compare); | 
| Tim Peters | a64dc24 | 2002-08-01 02:13:36 +0000 | [diff] [blame] | 1352 | 			if (k) { | 
 | 1353 | 				if (k < 0) | 
 | 1354 | 					goto Fail; | 
 | 1355 | 				*dest++ = *pb++; | 
 | 1356 | 				++bcount; | 
 | 1357 | 				acount = 0; | 
 | 1358 | 				--nb; | 
 | 1359 | 				if (nb == 0) | 
 | 1360 | 					goto Succeed; | 
| Tim Peters | e05f65a | 2002-08-10 05:21:15 +0000 | [diff] [blame] | 1361 | 				if (bcount >= min_gallop) | 
| Tim Peters | a64dc24 | 2002-08-01 02:13:36 +0000 | [diff] [blame] | 1362 | 					break; | 
| Guido van Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 1363 | 			} | 
 | 1364 | 			else { | 
| Tim Peters | a64dc24 | 2002-08-01 02:13:36 +0000 | [diff] [blame] | 1365 | 				*dest++ = *pa++; | 
 | 1366 | 				++acount; | 
 | 1367 | 				bcount = 0; | 
 | 1368 | 				--na; | 
 | 1369 | 				if (na == 1) | 
 | 1370 | 					goto CopyB; | 
| Tim Peters | e05f65a | 2002-08-10 05:21:15 +0000 | [diff] [blame] | 1371 | 				if (acount >= min_gallop) | 
| Tim Peters | a64dc24 | 2002-08-01 02:13:36 +0000 | [diff] [blame] | 1372 | 					break; | 
| Guido van Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 1373 | 			} | 
| Tim Peters | a64dc24 | 2002-08-01 02:13:36 +0000 | [diff] [blame] | 1374 |  		} | 
| Guido van Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 1375 |  | 
| Tim Peters | a64dc24 | 2002-08-01 02:13:36 +0000 | [diff] [blame] | 1376 | 		/* One run is winning so consistently that galloping may | 
 | 1377 | 		 * be a huge win.  So try that, and continue galloping until | 
 | 1378 | 		 * (if ever) neither run appears to be winning consistently | 
 | 1379 | 		 * anymore. | 
 | 1380 | 		 */ | 
| Tim Peters | e05f65a | 2002-08-10 05:21:15 +0000 | [diff] [blame] | 1381 | 		++min_gallop; | 
| Guido van Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 1382 | 		do { | 
| Tim Peters | e05f65a | 2002-08-10 05:21:15 +0000 | [diff] [blame] | 1383 |  			assert(na > 1 && nb > 0); | 
 | 1384 | 			min_gallop -= min_gallop > 1; | 
 | 1385 | 	 		ms->min_gallop = min_gallop; | 
| Tim Peters | a64dc24 | 2002-08-01 02:13:36 +0000 | [diff] [blame] | 1386 | 			k = gallop_right(*pb, pa, na, 0, compare); | 
 | 1387 | 			acount = k; | 
 | 1388 | 			if (k) { | 
 | 1389 | 				if (k < 0) | 
 | 1390 | 					goto Fail; | 
 | 1391 | 				memcpy(dest, pa, k * sizeof(PyObject *)); | 
 | 1392 | 				dest += k; | 
 | 1393 | 				pa += k; | 
 | 1394 | 				na -= k; | 
 | 1395 | 				if (na == 1) | 
 | 1396 | 					goto CopyB; | 
 | 1397 | 				/* na==0 is impossible now if the comparison | 
 | 1398 | 				 * function is consistent, but we can't assume | 
 | 1399 | 				 * that it is. | 
 | 1400 | 				 */ | 
 | 1401 | 				if (na == 0) | 
 | 1402 | 					goto Succeed; | 
| Guido van Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 1403 | 			} | 
| Tim Peters | a64dc24 | 2002-08-01 02:13:36 +0000 | [diff] [blame] | 1404 | 			*dest++ = *pb++; | 
 | 1405 | 			--nb; | 
 | 1406 | 			if (nb == 0) | 
 | 1407 | 				goto Succeed; | 
| Guido van Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 1408 |  | 
| Tim Peters | a64dc24 | 2002-08-01 02:13:36 +0000 | [diff] [blame] | 1409 |  			k = gallop_left(*pa, pb, nb, 0, compare); | 
 | 1410 |  			bcount = k; | 
 | 1411 | 			if (k) { | 
 | 1412 | 				if (k < 0) | 
 | 1413 | 					goto Fail; | 
 | 1414 | 				memmove(dest, pb, k * sizeof(PyObject *)); | 
 | 1415 | 				dest += k; | 
 | 1416 | 				pb += k; | 
 | 1417 | 				nb -= k; | 
 | 1418 | 				if (nb == 0) | 
 | 1419 | 					goto Succeed; | 
 | 1420 | 			} | 
 | 1421 | 			*dest++ = *pa++; | 
 | 1422 | 			--na; | 
 | 1423 | 			if (na == 1) | 
 | 1424 | 				goto CopyB; | 
 | 1425 |  		} while (acount >= MIN_GALLOP || bcount >= MIN_GALLOP); | 
| Tim Peters | e05f65a | 2002-08-10 05:21:15 +0000 | [diff] [blame] | 1426 |  		++min_gallop;	/* penalize it for leaving galloping mode */ | 
 | 1427 |  		ms->min_gallop = min_gallop; | 
| Tim Peters | a64dc24 | 2002-08-01 02:13:36 +0000 | [diff] [blame] | 1428 |  	} | 
 | 1429 | Succeed: | 
 | 1430 | 	result = 0; | 
 | 1431 | Fail: | 
 | 1432 | 	if (na) | 
 | 1433 | 		memcpy(dest, pa, na * sizeof(PyObject*)); | 
 | 1434 | 	return result; | 
 | 1435 | CopyB: | 
 | 1436 | 	assert(na == 1 && nb > 0); | 
 | 1437 | 	/* The last element of pa belongs at the end of the merge. */ | 
 | 1438 | 	memmove(dest, pb, nb * sizeof(PyObject *)); | 
 | 1439 | 	dest[nb] = *pa; | 
| Guido van Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 1440 | 	return 0; | 
| Tim Peters | a64dc24 | 2002-08-01 02:13:36 +0000 | [diff] [blame] | 1441 | } | 
| Guido van Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 1442 |  | 
| Tim Peters | a64dc24 | 2002-08-01 02:13:36 +0000 | [diff] [blame] | 1443 | /* Merge the na elements starting at pa with the nb elements starting at pb | 
 | 1444 |  * in a stable way, in-place.  na and nb must be > 0, and pa + na == pb. | 
 | 1445 |  * Must also have that *pb < *pa, that pa[na-1] belongs at the end of the | 
 | 1446 |  * merge, and should have na >= nb.  See listsort.txt for more info. | 
 | 1447 |  * Return 0 if successful, -1 if error. | 
 | 1448 |  */ | 
 | 1449 | static int | 
 | 1450 | merge_hi(MergeState *ms, PyObject **pa, int na, PyObject **pb, int nb) | 
 | 1451 | { | 
 | 1452 | 	int k; | 
 | 1453 | 	PyObject *compare; | 
 | 1454 | 	PyObject **dest; | 
 | 1455 | 	int result = -1;	/* guilty until proved innocent */ | 
 | 1456 | 	PyObject **basea; | 
 | 1457 | 	PyObject **baseb; | 
| Tim Peters | e05f65a | 2002-08-10 05:21:15 +0000 | [diff] [blame] | 1458 | 	int min_gallop = ms->min_gallop; | 
| Tim Peters | a64dc24 | 2002-08-01 02:13:36 +0000 | [diff] [blame] | 1459 |  | 
 | 1460 | 	assert(ms && pa && pb && na > 0 && nb > 0 && pa + na == pb); | 
 | 1461 | 	if (MERGE_GETMEM(ms, nb) < 0) | 
 | 1462 | 		return -1; | 
 | 1463 | 	dest = pb + nb - 1; | 
 | 1464 | 	memcpy(ms->a, pb, nb * sizeof(PyObject*)); | 
 | 1465 | 	basea = pa; | 
 | 1466 | 	baseb = ms->a; | 
 | 1467 | 	pb = ms->a + nb - 1; | 
 | 1468 | 	pa += na - 1; | 
 | 1469 |  | 
 | 1470 | 	*dest-- = *pa--; | 
 | 1471 | 	--na; | 
 | 1472 | 	if (na == 0) | 
 | 1473 | 		goto Succeed; | 
 | 1474 | 	if (nb == 1) | 
 | 1475 | 		goto CopyA; | 
 | 1476 |  | 
 | 1477 | 	compare = ms->compare; | 
 | 1478 | 	for (;;) { | 
 | 1479 | 		int acount = 0;	/* # of times A won in a row */ | 
 | 1480 | 		int bcount = 0;	/* # of times B won in a row */ | 
 | 1481 |  | 
 | 1482 | 		/* Do the straightforward thing until (if ever) one run | 
 | 1483 | 		 * appears to win consistently. | 
 | 1484 | 		 */ | 
 | 1485 |  		for (;;) { | 
| Tim Peters | e05f65a | 2002-08-10 05:21:15 +0000 | [diff] [blame] | 1486 |  			assert(na > 0 && nb > 1); | 
| Tim Peters | 66860f6 | 2002-08-04 17:47:26 +0000 | [diff] [blame] | 1487 | 	 		k = ISLT(*pb, *pa, compare); | 
| Tim Peters | a64dc24 | 2002-08-01 02:13:36 +0000 | [diff] [blame] | 1488 | 			if (k) { | 
 | 1489 | 				if (k < 0) | 
 | 1490 | 					goto Fail; | 
 | 1491 | 				*dest-- = *pa--; | 
 | 1492 | 				++acount; | 
 | 1493 | 				bcount = 0; | 
 | 1494 | 				--na; | 
 | 1495 | 				if (na == 0) | 
 | 1496 | 					goto Succeed; | 
| Tim Peters | e05f65a | 2002-08-10 05:21:15 +0000 | [diff] [blame] | 1497 | 				if (acount >= min_gallop) | 
| Tim Peters | a64dc24 | 2002-08-01 02:13:36 +0000 | [diff] [blame] | 1498 | 					break; | 
 | 1499 | 			} | 
 | 1500 | 			else { | 
 | 1501 | 				*dest-- = *pb--; | 
 | 1502 | 				++bcount; | 
 | 1503 | 				acount = 0; | 
 | 1504 | 				--nb; | 
 | 1505 | 				if (nb == 1) | 
 | 1506 | 					goto CopyA; | 
| Tim Peters | e05f65a | 2002-08-10 05:21:15 +0000 | [diff] [blame] | 1507 | 				if (bcount >= min_gallop) | 
| Tim Peters | a64dc24 | 2002-08-01 02:13:36 +0000 | [diff] [blame] | 1508 | 					break; | 
 | 1509 | 			} | 
 | 1510 |  		} | 
 | 1511 |  | 
 | 1512 | 		/* One run is winning so consistently that galloping may | 
 | 1513 | 		 * be a huge win.  So try that, and continue galloping until | 
 | 1514 | 		 * (if ever) neither run appears to be winning consistently | 
 | 1515 | 		 * anymore. | 
 | 1516 | 		 */ | 
| Tim Peters | e05f65a | 2002-08-10 05:21:15 +0000 | [diff] [blame] | 1517 | 		++min_gallop; | 
| Tim Peters | a64dc24 | 2002-08-01 02:13:36 +0000 | [diff] [blame] | 1518 | 		do { | 
| Tim Peters | e05f65a | 2002-08-10 05:21:15 +0000 | [diff] [blame] | 1519 |  			assert(na > 0 && nb > 1); | 
 | 1520 | 			min_gallop -= min_gallop > 1; | 
 | 1521 | 	 		ms->min_gallop = min_gallop; | 
| Tim Peters | a64dc24 | 2002-08-01 02:13:36 +0000 | [diff] [blame] | 1522 | 			k = gallop_right(*pb, basea, na, na-1, compare); | 
 | 1523 | 			if (k < 0) | 
 | 1524 | 				goto Fail; | 
 | 1525 | 			k = na - k; | 
 | 1526 | 			acount = k; | 
 | 1527 | 			if (k) { | 
 | 1528 | 				dest -= k; | 
 | 1529 | 				pa -= k; | 
 | 1530 | 				memmove(dest+1, pa+1, k * sizeof(PyObject *)); | 
 | 1531 | 				na -= k; | 
 | 1532 | 				if (na == 0) | 
 | 1533 | 					goto Succeed; | 
 | 1534 | 			} | 
 | 1535 | 			*dest-- = *pb--; | 
 | 1536 | 			--nb; | 
 | 1537 | 			if (nb == 1) | 
 | 1538 | 				goto CopyA; | 
 | 1539 |  | 
 | 1540 |  			k = gallop_left(*pa, baseb, nb, nb-1, compare); | 
 | 1541 | 			if (k < 0) | 
 | 1542 | 				goto Fail; | 
 | 1543 | 			k = nb - k; | 
 | 1544 | 			bcount = k; | 
 | 1545 | 			if (k) { | 
 | 1546 | 				dest -= k; | 
 | 1547 | 				pb -= k; | 
 | 1548 | 				memcpy(dest+1, pb+1, k * sizeof(PyObject *)); | 
 | 1549 | 				nb -= k; | 
 | 1550 | 				if (nb == 1) | 
 | 1551 | 					goto CopyA; | 
 | 1552 | 				/* nb==0 is impossible now if the comparison | 
 | 1553 | 				 * function is consistent, but we can't assume | 
 | 1554 | 				 * that it is. | 
 | 1555 | 				 */ | 
 | 1556 | 				if (nb == 0) | 
 | 1557 | 					goto Succeed; | 
 | 1558 | 			} | 
 | 1559 | 			*dest-- = *pa--; | 
 | 1560 | 			--na; | 
 | 1561 | 			if (na == 0) | 
 | 1562 | 				goto Succeed; | 
 | 1563 |  		} while (acount >= MIN_GALLOP || bcount >= MIN_GALLOP); | 
| Tim Peters | e05f65a | 2002-08-10 05:21:15 +0000 | [diff] [blame] | 1564 |  		++min_gallop;	/* penalize it for leaving galloping mode */ | 
 | 1565 |  		ms->min_gallop = min_gallop; | 
| Tim Peters | a64dc24 | 2002-08-01 02:13:36 +0000 | [diff] [blame] | 1566 |  	} | 
 | 1567 | Succeed: | 
 | 1568 | 	result = 0; | 
 | 1569 | Fail: | 
 | 1570 | 	if (nb) | 
 | 1571 | 		memcpy(dest-(nb-1), baseb, nb * sizeof(PyObject*)); | 
 | 1572 | 	return result; | 
 | 1573 | CopyA: | 
 | 1574 | 	assert(nb == 1 && na > 0); | 
 | 1575 | 	/* The first element of pb belongs at the front of the merge. */ | 
 | 1576 | 	dest -= na; | 
 | 1577 | 	pa -= na; | 
 | 1578 | 	memmove(dest+1, pa+1, na * sizeof(PyObject *)); | 
 | 1579 | 	*dest = *pb; | 
 | 1580 | 	return 0; | 
 | 1581 | } | 
 | 1582 |  | 
 | 1583 | /* Merge the two runs at stack indices i and i+1. | 
 | 1584 |  * Returns 0 on success, -1 on error. | 
 | 1585 |  */ | 
 | 1586 | static int | 
 | 1587 | merge_at(MergeState *ms, int i) | 
 | 1588 | { | 
 | 1589 | 	PyObject **pa, **pb; | 
 | 1590 | 	int na, nb; | 
 | 1591 | 	int k; | 
 | 1592 | 	PyObject *compare; | 
 | 1593 |  | 
 | 1594 | 	assert(ms != NULL); | 
 | 1595 | 	assert(ms->n >= 2); | 
 | 1596 | 	assert(i >= 0); | 
 | 1597 | 	assert(i == ms->n - 2 || i == ms->n - 3); | 
 | 1598 |  | 
| Tim Peters | e05f65a | 2002-08-10 05:21:15 +0000 | [diff] [blame] | 1599 | 	pa = ms->pending[i].base; | 
 | 1600 | 	na = ms->pending[i].len; | 
 | 1601 | 	pb = ms->pending[i+1].base; | 
 | 1602 | 	nb = ms->pending[i+1].len; | 
| Tim Peters | a64dc24 | 2002-08-01 02:13:36 +0000 | [diff] [blame] | 1603 | 	assert(na > 0 && nb > 0); | 
 | 1604 | 	assert(pa + na == pb); | 
 | 1605 |  | 
 | 1606 | 	/* Record the length of the combined runs; if i is the 3rd-last | 
 | 1607 | 	 * run now, also slide over the last run (which isn't involved | 
 | 1608 | 	 * in this merge).  The current run i+1 goes away in any case. | 
 | 1609 | 	 */ | 
| Tim Peters | e05f65a | 2002-08-10 05:21:15 +0000 | [diff] [blame] | 1610 | 	ms->pending[i].len = na + nb; | 
 | 1611 | 	if (i == ms->n - 3) | 
 | 1612 | 		ms->pending[i+1] = ms->pending[i+2]; | 
| Tim Peters | a64dc24 | 2002-08-01 02:13:36 +0000 | [diff] [blame] | 1613 | 	--ms->n; | 
 | 1614 |  | 
 | 1615 | 	/* Where does b start in a?  Elements in a before that can be | 
 | 1616 | 	 * ignored (already in place). | 
 | 1617 | 	 */ | 
 | 1618 | 	compare = ms->compare; | 
 | 1619 | 	k = gallop_right(*pb, pa, na, 0, compare); | 
 | 1620 | 	if (k < 0) | 
 | 1621 | 		return -1; | 
 | 1622 | 	pa += k; | 
 | 1623 | 	na -= k; | 
 | 1624 | 	if (na == 0) | 
 | 1625 | 		return 0; | 
 | 1626 |  | 
 | 1627 | 	/* Where does a end in b?  Elements in b after that can be | 
 | 1628 | 	 * ignored (already in place). | 
 | 1629 | 	 */ | 
 | 1630 | 	nb = gallop_left(pa[na-1], pb, nb, nb-1, compare); | 
 | 1631 | 	if (nb <= 0) | 
 | 1632 | 		return nb; | 
 | 1633 |  | 
 | 1634 | 	/* Merge what remains of the runs, using a temp array with | 
 | 1635 | 	 * min(na, nb) elements. | 
 | 1636 | 	 */ | 
 | 1637 | 	if (na <= nb) | 
 | 1638 | 		return merge_lo(ms, pa, na, pb, nb); | 
 | 1639 | 	else | 
 | 1640 | 		return merge_hi(ms, pa, na, pb, nb); | 
 | 1641 | } | 
 | 1642 |  | 
 | 1643 | /* Examine the stack of runs waiting to be merged, merging adjacent runs | 
 | 1644 |  * until the stack invariants are re-established: | 
 | 1645 |  * | 
 | 1646 |  * 1. len[-3] > len[-2] + len[-1] | 
 | 1647 |  * 2. len[-2] > len[-1] | 
 | 1648 |  * | 
 | 1649 |  * See listsort.txt for more info. | 
 | 1650 |  * | 
 | 1651 |  * Returns 0 on success, -1 on error. | 
 | 1652 |  */ | 
 | 1653 | static int | 
 | 1654 | merge_collapse(MergeState *ms) | 
 | 1655 | { | 
| Tim Peters | e05f65a | 2002-08-10 05:21:15 +0000 | [diff] [blame] | 1656 | 	struct s_slice *p = ms->pending; | 
| Tim Peters | a64dc24 | 2002-08-01 02:13:36 +0000 | [diff] [blame] | 1657 |  | 
 | 1658 | 	assert(ms); | 
 | 1659 | 	while (ms->n > 1) { | 
 | 1660 | 		int n = ms->n - 2; | 
| Tim Peters | e05f65a | 2002-08-10 05:21:15 +0000 | [diff] [blame] | 1661 | 		if (n > 0 && p[n-1].len <= p[n].len + p[n+1].len) { | 
 | 1662 | 		    	if (p[n-1].len < p[n+1].len) | 
| Tim Peters | a64dc24 | 2002-08-01 02:13:36 +0000 | [diff] [blame] | 1663 | 		    		--n; | 
 | 1664 | 			if (merge_at(ms, n) < 0) | 
 | 1665 | 				return -1; | 
 | 1666 | 		} | 
| Tim Peters | e05f65a | 2002-08-10 05:21:15 +0000 | [diff] [blame] | 1667 | 		else if (p[n].len <= p[n+1].len) { | 
| Tim Peters | a64dc24 | 2002-08-01 02:13:36 +0000 | [diff] [blame] | 1668 | 			 if (merge_at(ms, n) < 0) | 
 | 1669 | 			 	return -1; | 
 | 1670 | 		} | 
 | 1671 | 		else | 
 | 1672 | 			break; | 
 | 1673 | 	} | 
 | 1674 | 	return 0; | 
 | 1675 | } | 
 | 1676 |  | 
 | 1677 | /* Regardless of invariants, merge all runs on the stack until only one | 
 | 1678 |  * remains.  This is used at the end of the mergesort. | 
 | 1679 |  * | 
 | 1680 |  * Returns 0 on success, -1 on error. | 
 | 1681 |  */ | 
 | 1682 | static int | 
 | 1683 | merge_force_collapse(MergeState *ms) | 
 | 1684 | { | 
| Tim Peters | e05f65a | 2002-08-10 05:21:15 +0000 | [diff] [blame] | 1685 | 	struct s_slice *p = ms->pending; | 
| Tim Peters | a64dc24 | 2002-08-01 02:13:36 +0000 | [diff] [blame] | 1686 |  | 
 | 1687 | 	assert(ms); | 
 | 1688 | 	while (ms->n > 1) { | 
 | 1689 | 		int n = ms->n - 2; | 
| Tim Peters | e05f65a | 2002-08-10 05:21:15 +0000 | [diff] [blame] | 1690 | 		if (n > 0 && p[n-1].len < p[n+1].len) | 
| Tim Peters | a64dc24 | 2002-08-01 02:13:36 +0000 | [diff] [blame] | 1691 | 			--n; | 
 | 1692 | 		if (merge_at(ms, n) < 0) | 
 | 1693 | 			return -1; | 
 | 1694 | 	} | 
 | 1695 | 	return 0; | 
 | 1696 | } | 
 | 1697 |  | 
 | 1698 | /* Compute a good value for the minimum run length; natural runs shorter | 
 | 1699 |  * than this are boosted artificially via binary insertion. | 
 | 1700 |  * | 
 | 1701 |  * If n < 64, return n (it's too small to bother with fancy stuff). | 
 | 1702 |  * Else if n is an exact power of 2, return 32. | 
 | 1703 |  * Else return an int k, 32 <= k <= 64, such that n/k is close to, but | 
 | 1704 |  * strictly less than, an exact power of 2. | 
 | 1705 |  * | 
 | 1706 |  * See listsort.txt for more info. | 
 | 1707 |  */ | 
 | 1708 | static int | 
 | 1709 | merge_compute_minrun(int n) | 
 | 1710 | { | 
 | 1711 | 	int r = 0;	/* becomes 1 if any 1 bits are shifted off */ | 
 | 1712 |  | 
 | 1713 | 	assert(n >= 0); | 
 | 1714 | 	while (n >= 64) { | 
 | 1715 | 		r |= n & 1; | 
 | 1716 | 		n >>= 1; | 
 | 1717 | 	} | 
 | 1718 | 	return n + r; | 
| Guido van Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 1719 | } | 
| Guido van Rossum | a119c0d | 1998-05-29 17:56:32 +0000 | [diff] [blame] | 1720 |  | 
| Raymond Hettinger | 42b1ba3 | 2003-10-16 03:41:09 +0000 | [diff] [blame] | 1721 | /* Special wrapper to support stable sorting using the decorate-sort-undecorate | 
 | 1722 |    pattern.  Holds a key which is used for comparisions and the original record | 
 | 1723 |    which is returned during the undecorate phase.  By exposing only the key  | 
 | 1724 |    during comparisons, the underlying sort stability characteristics are left  | 
 | 1725 |    unchanged.  Also, if a custom comparison function is used, it will only see  | 
 | 1726 |    the key instead of a full record. */ | 
 | 1727 |  | 
 | 1728 | typedef struct { | 
 | 1729 | 	PyObject_HEAD | 
 | 1730 | 	PyObject *key; | 
 | 1731 | 	PyObject *value; | 
 | 1732 | } sortwrapperobject; | 
 | 1733 |  | 
 | 1734 | static PyTypeObject sortwrapper_type; | 
 | 1735 |  | 
 | 1736 | static PyObject * | 
 | 1737 | sortwrapper_richcompare(sortwrapperobject *a, sortwrapperobject *b, int op) | 
 | 1738 | { | 
 | 1739 | 	if (!PyObject_TypeCheck(b, &sortwrapper_type)) { | 
 | 1740 | 		PyErr_SetString(PyExc_TypeError,  | 
 | 1741 | 			"expected a sortwrapperobject"); | 
 | 1742 | 		return NULL; | 
 | 1743 | 	} | 
 | 1744 | 	return PyObject_RichCompare(a->key, b->key, op); | 
 | 1745 | } | 
 | 1746 |  | 
 | 1747 | static void | 
 | 1748 | sortwrapper_dealloc(sortwrapperobject *so) | 
 | 1749 | { | 
 | 1750 | 	Py_XDECREF(so->key); | 
 | 1751 | 	Py_XDECREF(so->value); | 
 | 1752 | 	PyObject_Del(so); | 
 | 1753 | } | 
 | 1754 |  | 
 | 1755 | PyDoc_STRVAR(sortwrapper_doc, "Object wrapper with a custom sort key."); | 
 | 1756 |  | 
 | 1757 | static PyTypeObject sortwrapper_type = { | 
 | 1758 | 	PyObject_HEAD_INIT(&PyType_Type) | 
 | 1759 | 	0,					/* ob_size */ | 
 | 1760 | 	"sortwrapper",				/* tp_name */ | 
 | 1761 | 	sizeof(sortwrapperobject),		/* tp_basicsize */ | 
 | 1762 | 	0,					/* tp_itemsize */ | 
 | 1763 | 	/* methods */ | 
 | 1764 | 	(destructor)sortwrapper_dealloc,	/* tp_dealloc */ | 
 | 1765 | 	0,					/* tp_print */ | 
 | 1766 | 	0,					/* tp_getattr */ | 
 | 1767 | 	0,					/* tp_setattr */ | 
 | 1768 | 	0,					/* tp_compare */ | 
 | 1769 | 	0,					/* tp_repr */ | 
 | 1770 | 	0,					/* tp_as_number */ | 
 | 1771 | 	0,					/* tp_as_sequence */ | 
 | 1772 | 	0,					/* tp_as_mapping */ | 
 | 1773 | 	0,					/* tp_hash */ | 
 | 1774 | 	0,					/* tp_call */ | 
 | 1775 | 	0,					/* tp_str */ | 
 | 1776 | 	PyObject_GenericGetAttr,		/* tp_getattro */ | 
 | 1777 | 	0,					/* tp_setattro */ | 
 | 1778 | 	0,					/* tp_as_buffer */ | 
 | 1779 | 	Py_TPFLAGS_DEFAULT |  | 
 | 1780 | 	Py_TPFLAGS_HAVE_RICHCOMPARE, 		/* tp_flags */ | 
 | 1781 | 	sortwrapper_doc,			/* tp_doc */ | 
 | 1782 | 	0,					/* tp_traverse */ | 
 | 1783 | 	0,					/* tp_clear */ | 
 | 1784 | 	(richcmpfunc)sortwrapper_richcompare,	/* tp_richcompare */ | 
 | 1785 | }; | 
 | 1786 |  | 
 | 1787 | /* Returns a new reference to a sortwrapper. | 
 | 1788 |    Consumes the references to the two underlying objects. */ | 
 | 1789 |  | 
 | 1790 | static PyObject * | 
 | 1791 | build_sortwrapper(PyObject *key, PyObject *value) | 
 | 1792 | { | 
 | 1793 | 	sortwrapperobject *so; | 
 | 1794 | 	 | 
 | 1795 | 	so = PyObject_New(sortwrapperobject, &sortwrapper_type); | 
 | 1796 | 	if (so == NULL) | 
 | 1797 | 		return NULL; | 
 | 1798 | 	so->key = key; | 
 | 1799 | 	so->value = value; | 
 | 1800 | 	return (PyObject *)so; | 
 | 1801 | } | 
 | 1802 |  | 
 | 1803 | /* Returns a new reference to the value underlying the wrapper. */ | 
 | 1804 | static PyObject * | 
 | 1805 | sortwrapper_getvalue(PyObject *so) | 
 | 1806 | { | 
 | 1807 | 	PyObject *value; | 
 | 1808 |  | 
 | 1809 | 	if (!PyObject_TypeCheck(so, &sortwrapper_type)) { | 
 | 1810 | 		PyErr_SetString(PyExc_TypeError,  | 
 | 1811 | 			"expected a sortwrapperobject"); | 
 | 1812 | 		return NULL; | 
 | 1813 | 	} | 
 | 1814 | 	value = ((sortwrapperobject *)so)->value; | 
 | 1815 | 	Py_INCREF(value); | 
 | 1816 | 	return value; | 
 | 1817 | } | 
 | 1818 |  | 
 | 1819 | /* Wrapper for user specified cmp functions in combination with a | 
 | 1820 |    specified key function.  Makes sure the cmp function is presented | 
 | 1821 |    with the actual key instead of the sortwrapper */ | 
 | 1822 |  | 
 | 1823 | typedef struct { | 
 | 1824 | 	PyObject_HEAD | 
 | 1825 | 	PyObject *func; | 
 | 1826 | } cmpwrapperobject; | 
 | 1827 |  | 
 | 1828 | static void | 
 | 1829 | cmpwrapper_dealloc(cmpwrapperobject *co) | 
 | 1830 | { | 
 | 1831 | 	Py_XDECREF(co->func); | 
 | 1832 | 	PyObject_Del(co); | 
 | 1833 | } | 
 | 1834 |  | 
 | 1835 | static PyObject * | 
 | 1836 | cmpwrapper_call(cmpwrapperobject *co, PyObject *args, PyObject *kwds) | 
 | 1837 | { | 
 | 1838 | 	PyObject *x, *y, *xx, *yy; | 
 | 1839 |  | 
 | 1840 | 	if (!PyArg_UnpackTuple(args, "", 2, 2, &x, &y)) | 
 | 1841 | 		return NULL; | 
 | 1842 | 	if (!PyObject_TypeCheck(x, &sortwrapper_type) || | 
| Raymond Hettinger | ae4a299 | 2003-10-16 17:16:30 +0000 | [diff] [blame] | 1843 | 	    !PyObject_TypeCheck(y, &sortwrapper_type)) { | 
| Raymond Hettinger | 42b1ba3 | 2003-10-16 03:41:09 +0000 | [diff] [blame] | 1844 | 		PyErr_SetString(PyExc_TypeError,  | 
 | 1845 | 			"expected a sortwrapperobject"); | 
 | 1846 | 		return NULL; | 
 | 1847 | 	} | 
 | 1848 | 	xx = ((sortwrapperobject *)x)->key; | 
 | 1849 | 	yy = ((sortwrapperobject *)y)->key; | 
 | 1850 | 	return PyObject_CallFunctionObjArgs(co->func, xx, yy, NULL); | 
 | 1851 | } | 
 | 1852 |  | 
 | 1853 | PyDoc_STRVAR(cmpwrapper_doc, "cmp() wrapper for sort with custom keys."); | 
 | 1854 |  | 
 | 1855 | static PyTypeObject cmpwrapper_type = { | 
 | 1856 | 	PyObject_HEAD_INIT(&PyType_Type) | 
 | 1857 | 	0,					/* ob_size */ | 
 | 1858 | 	"cmpwrapper",				/* tp_name */ | 
 | 1859 | 	sizeof(cmpwrapperobject),		/* tp_basicsize */ | 
 | 1860 | 	0,					/* tp_itemsize */ | 
 | 1861 | 	/* methods */ | 
 | 1862 | 	(destructor)cmpwrapper_dealloc,		/* tp_dealloc */ | 
 | 1863 | 	0,					/* tp_print */ | 
 | 1864 | 	0,					/* tp_getattr */ | 
 | 1865 | 	0,					/* tp_setattr */ | 
 | 1866 | 	0,					/* tp_compare */ | 
 | 1867 | 	0,					/* tp_repr */ | 
 | 1868 | 	0,					/* tp_as_number */ | 
 | 1869 | 	0,					/* tp_as_sequence */ | 
 | 1870 | 	0,					/* tp_as_mapping */ | 
 | 1871 | 	0,					/* tp_hash */ | 
 | 1872 | 	(ternaryfunc)cmpwrapper_call,		/* tp_call */ | 
 | 1873 | 	0,					/* tp_str */ | 
 | 1874 | 	PyObject_GenericGetAttr,		/* tp_getattro */ | 
 | 1875 | 	0,					/* tp_setattro */ | 
 | 1876 | 	0,					/* tp_as_buffer */ | 
 | 1877 | 	Py_TPFLAGS_DEFAULT,			/* tp_flags */ | 
 | 1878 | 	cmpwrapper_doc,				/* tp_doc */ | 
 | 1879 | }; | 
 | 1880 |  | 
 | 1881 | static PyObject * | 
 | 1882 | build_cmpwrapper(PyObject *cmpfunc) | 
 | 1883 | { | 
 | 1884 | 	cmpwrapperobject *co; | 
 | 1885 | 	 | 
 | 1886 | 	co = PyObject_New(cmpwrapperobject, &cmpwrapper_type); | 
 | 1887 | 	if (co == NULL) | 
 | 1888 | 		return NULL; | 
 | 1889 | 	Py_INCREF(cmpfunc); | 
 | 1890 | 	co->func = cmpfunc; | 
 | 1891 | 	return (PyObject *)co; | 
 | 1892 | } | 
 | 1893 |  | 
| Tim Peters | a64dc24 | 2002-08-01 02:13:36 +0000 | [diff] [blame] | 1894 | /* An adaptive, stable, natural mergesort.  See listsort.txt. | 
 | 1895 |  * Returns Py_None on success, NULL on error.  Even in case of error, the | 
 | 1896 |  * list will be some permutation of its input state (nothing is lost or | 
 | 1897 |  * duplicated). | 
 | 1898 |  */ | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1899 | static PyObject * | 
| Raymond Hettinger | 42b1ba3 | 2003-10-16 03:41:09 +0000 | [diff] [blame] | 1900 | listsort(PyListObject *self, PyObject *args, PyObject *kwds) | 
| Guido van Rossum | 3f236de | 1996-12-10 23:55:39 +0000 | [diff] [blame] | 1901 | { | 
| Tim Peters | a64dc24 | 2002-08-01 02:13:36 +0000 | [diff] [blame] | 1902 | 	MergeState ms; | 
 | 1903 | 	PyObject **lo, **hi; | 
 | 1904 | 	int nremaining; | 
 | 1905 | 	int minrun; | 
| Raymond Hettinger | 4bb9540 | 2004-02-13 11:36:39 +0000 | [diff] [blame] | 1906 | 	int saved_ob_size, saved_allocated; | 
| Tim Peters | b9099c3 | 2002-11-12 22:08:10 +0000 | [diff] [blame] | 1907 | 	PyObject **saved_ob_item; | 
 | 1908 | 	PyObject **empty_ob_item; | 
| Tim Peters | a64dc24 | 2002-08-01 02:13:36 +0000 | [diff] [blame] | 1909 | 	PyObject *compare = NULL; | 
 | 1910 | 	PyObject *result = NULL;	/* guilty until proved innocent */ | 
| Raymond Hettinger | 42b1ba3 | 2003-10-16 03:41:09 +0000 | [diff] [blame] | 1911 | 	int reverse = 0; | 
 | 1912 | 	PyObject *keyfunc = NULL; | 
| Michael W. Hudson | 1df0f65 | 2003-12-04 11:25:46 +0000 | [diff] [blame] | 1913 | 	int i; | 
| Raymond Hettinger | 42b1ba3 | 2003-10-16 03:41:09 +0000 | [diff] [blame] | 1914 | 	PyObject *key, *value, *kvpair; | 
 | 1915 | 	static char *kwlist[] = {"cmp", "key", "reverse", 0}; | 
| Guido van Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 1916 |  | 
| Tim Peters | a64dc24 | 2002-08-01 02:13:36 +0000 | [diff] [blame] | 1917 | 	assert(self != NULL); | 
| Raymond Hettinger | 42b1ba3 | 2003-10-16 03:41:09 +0000 | [diff] [blame] | 1918 | 	assert (PyList_Check(self)); | 
| Guido van Rossum | 4aa24f9 | 2000-02-24 15:23:03 +0000 | [diff] [blame] | 1919 | 	if (args != NULL) { | 
| Raymond Hettinger | 42b1ba3 | 2003-10-16 03:41:09 +0000 | [diff] [blame] | 1920 | 		if (!PyArg_ParseTupleAndKeywords(args, kwds, "|OOi:sort", | 
 | 1921 | 			kwlist, &compare, &keyfunc, &reverse)) | 
| Guido van Rossum | 4aa24f9 | 2000-02-24 15:23:03 +0000 | [diff] [blame] | 1922 | 			return NULL; | 
 | 1923 | 	} | 
| Skip Montanaro | 4abd5f0 | 2003-01-02 20:51:08 +0000 | [diff] [blame] | 1924 | 	if (compare == Py_None) | 
 | 1925 | 		compare = NULL; | 
| Raymond Hettinger | 42b1ba3 | 2003-10-16 03:41:09 +0000 | [diff] [blame] | 1926 | 	if (keyfunc == Py_None) | 
 | 1927 | 		keyfunc = NULL; | 
 | 1928 | 	if (compare != NULL && keyfunc != NULL) { | 
 | 1929 | 		compare = build_cmpwrapper(compare); | 
 | 1930 | 		if (compare == NULL) | 
| Hye-Shik Chang | 19cb193 | 2003-12-10 07:31:08 +0000 | [diff] [blame] | 1931 | 			return NULL; | 
| Raymond Hettinger | 42b1ba3 | 2003-10-16 03:41:09 +0000 | [diff] [blame] | 1932 | 	} else | 
 | 1933 | 		Py_XINCREF(compare); | 
 | 1934 |  | 
| Tim Peters | b9099c3 | 2002-11-12 22:08:10 +0000 | [diff] [blame] | 1935 | 	/* The list is temporarily made empty, so that mutations performed | 
 | 1936 | 	 * by comparison functions can't affect the slice of memory we're | 
 | 1937 | 	 * sorting (allowing mutations during sorting is a core-dump | 
 | 1938 | 	 * factory, since ob_item may change). | 
 | 1939 | 	 */ | 
 | 1940 | 	saved_ob_size = self->ob_size; | 
 | 1941 | 	saved_ob_item = self->ob_item; | 
| Raymond Hettinger | 4bb9540 | 2004-02-13 11:36:39 +0000 | [diff] [blame] | 1942 | 	saved_allocated = self->allocated; | 
| Tim Peters | b9099c3 | 2002-11-12 22:08:10 +0000 | [diff] [blame] | 1943 | 	self->ob_size = 0; | 
| Tim Peters | 7049d81 | 2004-01-18 20:31:02 +0000 | [diff] [blame] | 1944 | 	self->ob_item = empty_ob_item = PyMem_NEW(PyObject *, 0); | 
| Raymond Hettinger | 4bb9540 | 2004-02-13 11:36:39 +0000 | [diff] [blame] | 1945 | 	self->allocated = 0; | 
| Tim Peters | 330f9e9 | 2002-07-19 07:05:44 +0000 | [diff] [blame] | 1946 |  | 
| Michael W. Hudson | 1df0f65 | 2003-12-04 11:25:46 +0000 | [diff] [blame] | 1947 | 	if (keyfunc != NULL) { | 
 | 1948 | 		for (i=0 ; i < saved_ob_size ; i++) { | 
 | 1949 | 			value = saved_ob_item[i]; | 
 | 1950 | 			key = PyObject_CallFunctionObjArgs(keyfunc, value,  | 
 | 1951 | 							   NULL); | 
 | 1952 | 			if (key == NULL) { | 
 | 1953 | 				for (i=i-1 ; i>=0 ; i--) { | 
 | 1954 | 					kvpair = saved_ob_item[i]; | 
 | 1955 | 					value = sortwrapper_getvalue(kvpair); | 
 | 1956 | 					saved_ob_item[i] = value; | 
 | 1957 | 					Py_DECREF(kvpair); | 
 | 1958 | 				} | 
 | 1959 | 				if (self->ob_item != empty_ob_item  | 
 | 1960 | 				    || self->ob_size) { | 
 | 1961 | 					/* If the list changed *as well* we | 
 | 1962 | 					   have two errors.  We let the first | 
 | 1963 | 					   one "win", but we shouldn't let | 
 | 1964 | 					   what's in the list currently | 
 | 1965 | 					   leak. */ | 
 | 1966 | 					(void)list_ass_slice( | 
 | 1967 | 						self, 0, self->ob_size, | 
 | 1968 | 						(PyObject *)NULL); | 
 | 1969 | 				} | 
 | 1970 | 				 | 
 | 1971 | 				goto dsu_fail; | 
 | 1972 | 			} | 
 | 1973 | 			kvpair = build_sortwrapper(key, value); | 
 | 1974 | 			if (kvpair == NULL) | 
 | 1975 | 				goto dsu_fail; | 
 | 1976 | 			saved_ob_item[i] = kvpair; | 
 | 1977 | 		} | 
 | 1978 | 	} | 
 | 1979 |  | 
 | 1980 | 	/* Reverse sort stability achieved by initially reversing the list, | 
 | 1981 | 	applying a stable forward sort, then reversing the final result. */ | 
 | 1982 | 	if (reverse && saved_ob_size > 1) | 
 | 1983 | 		reverse_slice(saved_ob_item, saved_ob_item + saved_ob_size); | 
 | 1984 |  | 
 | 1985 | 	merge_init(&ms, compare); | 
 | 1986 |  | 
| Tim Peters | b9099c3 | 2002-11-12 22:08:10 +0000 | [diff] [blame] | 1987 | 	nremaining = saved_ob_size; | 
| Tim Peters | a64dc24 | 2002-08-01 02:13:36 +0000 | [diff] [blame] | 1988 | 	if (nremaining < 2) | 
 | 1989 | 		goto succeed; | 
| Tim Peters | 330f9e9 | 2002-07-19 07:05:44 +0000 | [diff] [blame] | 1990 |  | 
| Tim Peters | a64dc24 | 2002-08-01 02:13:36 +0000 | [diff] [blame] | 1991 | 	/* March over the array once, left to right, finding natural runs, | 
 | 1992 | 	 * and extending short natural runs to minrun elements. | 
 | 1993 | 	 */ | 
| Tim Peters | b9099c3 | 2002-11-12 22:08:10 +0000 | [diff] [blame] | 1994 | 	lo = saved_ob_item; | 
| Tim Peters | a64dc24 | 2002-08-01 02:13:36 +0000 | [diff] [blame] | 1995 | 	hi = lo + nremaining; | 
 | 1996 | 	minrun = merge_compute_minrun(nremaining); | 
 | 1997 | 	do { | 
 | 1998 | 		int descending; | 
 | 1999 | 		int n; | 
| Tim Peters | 330f9e9 | 2002-07-19 07:05:44 +0000 | [diff] [blame] | 2000 |  | 
| Tim Peters | a64dc24 | 2002-08-01 02:13:36 +0000 | [diff] [blame] | 2001 | 		/* Identify next run. */ | 
 | 2002 | 		n = count_run(lo, hi, compare, &descending); | 
 | 2003 | 		if (n < 0) | 
 | 2004 | 			goto fail; | 
 | 2005 | 		if (descending) | 
 | 2006 | 			reverse_slice(lo, lo + n); | 
 | 2007 | 		/* If short, extend to min(minrun, nremaining). */ | 
 | 2008 | 		if (n < minrun) { | 
 | 2009 | 			const int force = nremaining <= minrun ? | 
 | 2010 | 	 			  	  nremaining : minrun; | 
 | 2011 | 			if (binarysort(lo, lo + force, lo + n, compare) < 0) | 
 | 2012 | 				goto fail; | 
 | 2013 | 			n = force; | 
 | 2014 | 		} | 
 | 2015 | 		/* Push run onto pending-runs stack, and maybe merge. */ | 
 | 2016 | 		assert(ms.n < MAX_MERGE_PENDING); | 
| Tim Peters | e05f65a | 2002-08-10 05:21:15 +0000 | [diff] [blame] | 2017 | 		ms.pending[ms.n].base = lo; | 
 | 2018 | 		ms.pending[ms.n].len = n; | 
| Tim Peters | a64dc24 | 2002-08-01 02:13:36 +0000 | [diff] [blame] | 2019 | 		++ms.n; | 
 | 2020 | 		if (merge_collapse(&ms) < 0) | 
 | 2021 | 			goto fail; | 
 | 2022 | 		/* Advance to find next run. */ | 
 | 2023 | 		lo += n; | 
 | 2024 | 		nremaining -= n; | 
 | 2025 | 	} while (nremaining); | 
 | 2026 | 	assert(lo == hi); | 
| Tim Peters | 330f9e9 | 2002-07-19 07:05:44 +0000 | [diff] [blame] | 2027 |  | 
| Tim Peters | a64dc24 | 2002-08-01 02:13:36 +0000 | [diff] [blame] | 2028 | 	if (merge_force_collapse(&ms) < 0) | 
 | 2029 | 		goto fail; | 
 | 2030 | 	assert(ms.n == 1); | 
| Tim Peters | b9099c3 | 2002-11-12 22:08:10 +0000 | [diff] [blame] | 2031 | 	assert(ms.pending[0].base == saved_ob_item); | 
 | 2032 | 	assert(ms.pending[0].len == saved_ob_size); | 
| Tim Peters | a64dc24 | 2002-08-01 02:13:36 +0000 | [diff] [blame] | 2033 |  | 
 | 2034 | succeed: | 
 | 2035 | 	result = Py_None; | 
| Tim Peters | 330f9e9 | 2002-07-19 07:05:44 +0000 | [diff] [blame] | 2036 | fail: | 
| Michael W. Hudson | 1df0f65 | 2003-12-04 11:25:46 +0000 | [diff] [blame] | 2037 | 	if (keyfunc != NULL) { | 
 | 2038 | 		for (i=0 ; i < saved_ob_size ; i++) { | 
 | 2039 | 			kvpair = saved_ob_item[i]; | 
 | 2040 | 			value = sortwrapper_getvalue(kvpair); | 
 | 2041 | 			saved_ob_item[i] = value; | 
 | 2042 | 			Py_DECREF(kvpair); | 
 | 2043 | 		} | 
 | 2044 | 	} | 
 | 2045 |  | 
| Tim Peters | b9099c3 | 2002-11-12 22:08:10 +0000 | [diff] [blame] | 2046 | 	if (self->ob_item != empty_ob_item || self->ob_size) { | 
 | 2047 | 		/* The user mucked with the list during the sort. */ | 
 | 2048 | 		(void)list_ass_slice(self, 0, self->ob_size, (PyObject *)NULL); | 
 | 2049 | 		if (result != NULL) { | 
 | 2050 | 			PyErr_SetString(PyExc_ValueError, | 
 | 2051 | 					"list modified during sort"); | 
 | 2052 | 			result = NULL; | 
 | 2053 | 		} | 
 | 2054 | 	} | 
| Michael W. Hudson | 1df0f65 | 2003-12-04 11:25:46 +0000 | [diff] [blame] | 2055 |  | 
 | 2056 | 	if (reverse && saved_ob_size > 1) | 
 | 2057 | 		reverse_slice(saved_ob_item, saved_ob_item + saved_ob_size); | 
 | 2058 |  | 
 | 2059 | 	merge_freemem(&ms); | 
 | 2060 |  | 
 | 2061 | dsu_fail: | 
| Tim Peters | b9099c3 | 2002-11-12 22:08:10 +0000 | [diff] [blame] | 2062 | 	if (self->ob_item == empty_ob_item) | 
 | 2063 | 		PyMem_FREE(empty_ob_item); | 
 | 2064 | 	self->ob_size = saved_ob_size; | 
 | 2065 | 	self->ob_item = saved_ob_item; | 
| Raymond Hettinger | 4bb9540 | 2004-02-13 11:36:39 +0000 | [diff] [blame] | 2066 | 	self->allocated = saved_allocated; | 
| Raymond Hettinger | 42b1ba3 | 2003-10-16 03:41:09 +0000 | [diff] [blame] | 2067 | 	Py_XDECREF(compare); | 
| Tim Peters | a64dc24 | 2002-08-01 02:13:36 +0000 | [diff] [blame] | 2068 | 	Py_XINCREF(result); | 
 | 2069 | 	return result; | 
| Guido van Rossum | 3f236de | 1996-12-10 23:55:39 +0000 | [diff] [blame] | 2070 | } | 
| Tim Peters | 330f9e9 | 2002-07-19 07:05:44 +0000 | [diff] [blame] | 2071 | #undef IFLT | 
| Tim Peters | 66860f6 | 2002-08-04 17:47:26 +0000 | [diff] [blame] | 2072 | #undef ISLT | 
| Tim Peters | 330f9e9 | 2002-07-19 07:05:44 +0000 | [diff] [blame] | 2073 |  | 
| Guido van Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 2074 | int | 
| Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 2075 | PyList_Sort(PyObject *v) | 
| Guido van Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 2076 | { | 
 | 2077 | 	if (v == NULL || !PyList_Check(v)) { | 
 | 2078 | 		PyErr_BadInternalCall(); | 
 | 2079 | 		return -1; | 
 | 2080 | 	} | 
| Raymond Hettinger | 42b1ba3 | 2003-10-16 03:41:09 +0000 | [diff] [blame] | 2081 | 	v = listsort((PyListObject *)v, (PyObject *)NULL, (PyObject *)NULL); | 
| Guido van Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 2082 | 	if (v == NULL) | 
 | 2083 | 		return -1; | 
 | 2084 | 	Py_DECREF(v); | 
 | 2085 | 	return 0; | 
 | 2086 | } | 
 | 2087 |  | 
| Guido van Rossum | b86c549 | 2001-02-12 22:06:02 +0000 | [diff] [blame] | 2088 | static PyObject * | 
| Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 2089 | listreverse(PyListObject *self) | 
| Guido van Rossum | b86c549 | 2001-02-12 22:06:02 +0000 | [diff] [blame] | 2090 | { | 
| Tim Peters | 326b448 | 2002-07-19 04:04:16 +0000 | [diff] [blame] | 2091 | 	if (self->ob_size > 1) | 
 | 2092 | 		reverse_slice(self->ob_item, self->ob_item + self->ob_size); | 
| Raymond Hettinger | 45d0b5c | 2004-04-12 17:21:03 +0000 | [diff] [blame] | 2093 | 	Py_RETURN_NONE; | 
| Guido van Rossum | ed98d48 | 1991-03-06 13:07:53 +0000 | [diff] [blame] | 2094 | } | 
 | 2095 |  | 
| Guido van Rossum | 84c76f5 | 1990-10-30 13:32:20 +0000 | [diff] [blame] | 2096 | int | 
| Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 2097 | PyList_Reverse(PyObject *v) | 
| Guido van Rossum | b0fe3a9 | 1995-01-17 16:34:45 +0000 | [diff] [blame] | 2098 | { | 
| Tim Peters | 6063e26 | 2002-08-08 01:06:39 +0000 | [diff] [blame] | 2099 | 	PyListObject *self = (PyListObject *)v; | 
 | 2100 |  | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2101 | 	if (v == NULL || !PyList_Check(v)) { | 
 | 2102 | 		PyErr_BadInternalCall(); | 
| Guido van Rossum | b0fe3a9 | 1995-01-17 16:34:45 +0000 | [diff] [blame] | 2103 | 		return -1; | 
 | 2104 | 	} | 
| Tim Peters | 6063e26 | 2002-08-08 01:06:39 +0000 | [diff] [blame] | 2105 | 	if (self->ob_size > 1) | 
 | 2106 | 		reverse_slice(self->ob_item, self->ob_item + self->ob_size); | 
| Guido van Rossum | b0fe3a9 | 1995-01-17 16:34:45 +0000 | [diff] [blame] | 2107 | 	return 0; | 
 | 2108 | } | 
 | 2109 |  | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2110 | PyObject * | 
| Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 2111 | PyList_AsTuple(PyObject *v) | 
| Guido van Rossum | 6cd2fe0 | 1994-08-29 12:45:32 +0000 | [diff] [blame] | 2112 | { | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2113 | 	PyObject *w; | 
 | 2114 | 	PyObject **p; | 
| Guido van Rossum | 6cd2fe0 | 1994-08-29 12:45:32 +0000 | [diff] [blame] | 2115 | 	int n; | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2116 | 	if (v == NULL || !PyList_Check(v)) { | 
 | 2117 | 		PyErr_BadInternalCall(); | 
| Guido van Rossum | 6cd2fe0 | 1994-08-29 12:45:32 +0000 | [diff] [blame] | 2118 | 		return NULL; | 
 | 2119 | 	} | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2120 | 	n = ((PyListObject *)v)->ob_size; | 
 | 2121 | 	w = PyTuple_New(n); | 
| Guido van Rossum | 6cd2fe0 | 1994-08-29 12:45:32 +0000 | [diff] [blame] | 2122 | 	if (w == NULL) | 
 | 2123 | 		return NULL; | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2124 | 	p = ((PyTupleObject *)w)->ob_item; | 
| Thomas Wouters | 334fb89 | 2000-07-25 12:56:38 +0000 | [diff] [blame] | 2125 | 	memcpy((void *)p, | 
 | 2126 | 	       (void *)((PyListObject *)v)->ob_item, | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2127 | 	       n*sizeof(PyObject *)); | 
| Guido van Rossum | 6cd2fe0 | 1994-08-29 12:45:32 +0000 | [diff] [blame] | 2128 | 	while (--n >= 0) { | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2129 | 		Py_INCREF(*p); | 
| Guido van Rossum | 6cd2fe0 | 1994-08-29 12:45:32 +0000 | [diff] [blame] | 2130 | 		p++; | 
 | 2131 | 	} | 
 | 2132 | 	return w; | 
 | 2133 | } | 
 | 2134 |  | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2135 | static PyObject * | 
| Raymond Hettinger | d05abde | 2003-06-17 05:05:49 +0000 | [diff] [blame] | 2136 | listindex(PyListObject *self, PyObject *args) | 
| Guido van Rossum | ed98d48 | 1991-03-06 13:07:53 +0000 | [diff] [blame] | 2137 | { | 
| Raymond Hettinger | d05abde | 2003-06-17 05:05:49 +0000 | [diff] [blame] | 2138 | 	int i, start=0, stop=self->ob_size; | 
 | 2139 | 	PyObject *v; | 
| Guido van Rossum | 4aa24f9 | 2000-02-24 15:23:03 +0000 | [diff] [blame] | 2140 |  | 
| Walter Dörwald | e8049bef | 2003-06-17 19:27:39 +0000 | [diff] [blame] | 2141 | 	if (!PyArg_ParseTuple(args, "O|O&O&:index", &v, | 
 | 2142 | 	                            _PyEval_SliceIndex, &start, | 
 | 2143 | 	                            _PyEval_SliceIndex, &stop)) | 
| Raymond Hettinger | d05abde | 2003-06-17 05:05:49 +0000 | [diff] [blame] | 2144 | 		return NULL; | 
| Guido van Rossum | 2743d87 | 2003-06-17 14:25:14 +0000 | [diff] [blame] | 2145 | 	if (start < 0) { | 
 | 2146 | 		start += self->ob_size; | 
 | 2147 | 		if (start < 0) | 
 | 2148 | 			start = 0; | 
 | 2149 | 	} | 
 | 2150 | 	if (stop < 0) { | 
 | 2151 | 		stop += self->ob_size; | 
 | 2152 | 		if (stop < 0) | 
 | 2153 | 			stop = 0; | 
 | 2154 | 	} | 
 | 2155 | 	else if (stop > self->ob_size) | 
 | 2156 | 		stop = self->ob_size; | 
| Raymond Hettinger | d05abde | 2003-06-17 05:05:49 +0000 | [diff] [blame] | 2157 | 	for (i = start; i < stop; i++) { | 
| Guido van Rossum | 65e1cea | 2001-01-17 22:11:59 +0000 | [diff] [blame] | 2158 | 		int cmp = PyObject_RichCompareBool(self->ob_item[i], v, Py_EQ); | 
 | 2159 | 		if (cmp > 0) | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2160 | 			return PyInt_FromLong((long)i); | 
| Guido van Rossum | 65e1cea | 2001-01-17 22:11:59 +0000 | [diff] [blame] | 2161 | 		else if (cmp < 0) | 
| Guido van Rossum | c8b6df9 | 1997-05-23 00:06:51 +0000 | [diff] [blame] | 2162 | 			return NULL; | 
| Guido van Rossum | ed98d48 | 1991-03-06 13:07:53 +0000 | [diff] [blame] | 2163 | 	} | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2164 | 	PyErr_SetString(PyExc_ValueError, "list.index(x): x not in list"); | 
| Guido van Rossum | ed98d48 | 1991-03-06 13:07:53 +0000 | [diff] [blame] | 2165 | 	return NULL; | 
 | 2166 | } | 
 | 2167 |  | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2168 | static PyObject * | 
| Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 2169 | listcount(PyListObject *self, PyObject *v) | 
| Guido van Rossum | e6f7d18 | 1991-10-20 20:20:40 +0000 | [diff] [blame] | 2170 | { | 
 | 2171 | 	int count = 0; | 
 | 2172 | 	int i; | 
| Guido van Rossum | 4aa24f9 | 2000-02-24 15:23:03 +0000 | [diff] [blame] | 2173 |  | 
| Guido van Rossum | e6f7d18 | 1991-10-20 20:20:40 +0000 | [diff] [blame] | 2174 | 	for (i = 0; i < self->ob_size; i++) { | 
| Guido van Rossum | 65e1cea | 2001-01-17 22:11:59 +0000 | [diff] [blame] | 2175 | 		int cmp = PyObject_RichCompareBool(self->ob_item[i], v, Py_EQ); | 
 | 2176 | 		if (cmp > 0) | 
| Guido van Rossum | e6f7d18 | 1991-10-20 20:20:40 +0000 | [diff] [blame] | 2177 | 			count++; | 
| Guido van Rossum | 65e1cea | 2001-01-17 22:11:59 +0000 | [diff] [blame] | 2178 | 		else if (cmp < 0) | 
| Guido van Rossum | c8b6df9 | 1997-05-23 00:06:51 +0000 | [diff] [blame] | 2179 | 			return NULL; | 
| Guido van Rossum | e6f7d18 | 1991-10-20 20:20:40 +0000 | [diff] [blame] | 2180 | 	} | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2181 | 	return PyInt_FromLong((long)count); | 
| Guido van Rossum | e6f7d18 | 1991-10-20 20:20:40 +0000 | [diff] [blame] | 2182 | } | 
 | 2183 |  | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2184 | static PyObject * | 
| Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 2185 | listremove(PyListObject *self, PyObject *v) | 
| Guido van Rossum | ed98d48 | 1991-03-06 13:07:53 +0000 | [diff] [blame] | 2186 | { | 
 | 2187 | 	int i; | 
| Guido van Rossum | 4aa24f9 | 2000-02-24 15:23:03 +0000 | [diff] [blame] | 2188 |  | 
| Guido van Rossum | ed98d48 | 1991-03-06 13:07:53 +0000 | [diff] [blame] | 2189 | 	for (i = 0; i < self->ob_size; i++) { | 
| Guido van Rossum | 65e1cea | 2001-01-17 22:11:59 +0000 | [diff] [blame] | 2190 | 		int cmp = PyObject_RichCompareBool(self->ob_item[i], v, Py_EQ); | 
 | 2191 | 		if (cmp > 0) { | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2192 | 			if (list_ass_slice(self, i, i+1, | 
| Raymond Hettinger | 45d0b5c | 2004-04-12 17:21:03 +0000 | [diff] [blame] | 2193 | 					   (PyObject *)NULL) == 0) | 
 | 2194 | 				Py_RETURN_NONE; | 
 | 2195 | 			return NULL; | 
| Guido van Rossum | ed98d48 | 1991-03-06 13:07:53 +0000 | [diff] [blame] | 2196 | 		} | 
| Guido van Rossum | 65e1cea | 2001-01-17 22:11:59 +0000 | [diff] [blame] | 2197 | 		else if (cmp < 0) | 
| Guido van Rossum | c8b6df9 | 1997-05-23 00:06:51 +0000 | [diff] [blame] | 2198 | 			return NULL; | 
| Guido van Rossum | ed98d48 | 1991-03-06 13:07:53 +0000 | [diff] [blame] | 2199 | 	} | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2200 | 	PyErr_SetString(PyExc_ValueError, "list.remove(x): x not in list"); | 
| Guido van Rossum | ed98d48 | 1991-03-06 13:07:53 +0000 | [diff] [blame] | 2201 | 	return NULL; | 
 | 2202 | } | 
 | 2203 |  | 
| Jeremy Hylton | 8caad49 | 2000-06-23 14:18:11 +0000 | [diff] [blame] | 2204 | static int | 
 | 2205 | list_traverse(PyListObject *o, visitproc visit, void *arg) | 
 | 2206 | { | 
 | 2207 | 	int i, err; | 
 | 2208 | 	PyObject *x; | 
 | 2209 |  | 
 | 2210 | 	for (i = o->ob_size; --i >= 0; ) { | 
 | 2211 | 		x = o->ob_item[i]; | 
 | 2212 | 		if (x != NULL) { | 
 | 2213 | 			err = visit(x, arg); | 
 | 2214 | 			if (err) | 
 | 2215 | 				return err; | 
 | 2216 | 		} | 
 | 2217 | 	} | 
 | 2218 | 	return 0; | 
 | 2219 | } | 
 | 2220 |  | 
 | 2221 | static int | 
 | 2222 | list_clear(PyListObject *lp) | 
 | 2223 | { | 
 | 2224 | 	(void) PyList_SetSlice((PyObject *)lp, 0, lp->ob_size, 0); | 
 | 2225 | 	return 0; | 
 | 2226 | } | 
 | 2227 |  | 
| Guido van Rossum | 65e1cea | 2001-01-17 22:11:59 +0000 | [diff] [blame] | 2228 | static PyObject * | 
 | 2229 | list_richcompare(PyObject *v, PyObject *w, int op) | 
 | 2230 | { | 
 | 2231 | 	PyListObject *vl, *wl; | 
 | 2232 | 	int i; | 
 | 2233 |  | 
 | 2234 | 	if (!PyList_Check(v) || !PyList_Check(w)) { | 
 | 2235 | 		Py_INCREF(Py_NotImplemented); | 
 | 2236 | 		return Py_NotImplemented; | 
 | 2237 | 	} | 
 | 2238 |  | 
 | 2239 | 	vl = (PyListObject *)v; | 
 | 2240 | 	wl = (PyListObject *)w; | 
 | 2241 |  | 
 | 2242 | 	if (vl->ob_size != wl->ob_size && (op == Py_EQ || op == Py_NE)) { | 
 | 2243 | 		/* Shortcut: if the lengths differ, the lists differ */ | 
 | 2244 | 		PyObject *res; | 
 | 2245 | 		if (op == Py_EQ) | 
 | 2246 | 			res = Py_False; | 
 | 2247 | 		else | 
 | 2248 | 			res = Py_True; | 
 | 2249 | 		Py_INCREF(res); | 
 | 2250 | 		return res; | 
 | 2251 | 	} | 
 | 2252 |  | 
 | 2253 | 	/* Search for the first index where items are different */ | 
 | 2254 | 	for (i = 0; i < vl->ob_size && i < wl->ob_size; i++) { | 
 | 2255 | 		int k = PyObject_RichCompareBool(vl->ob_item[i], | 
 | 2256 | 						 wl->ob_item[i], Py_EQ); | 
 | 2257 | 		if (k < 0) | 
 | 2258 | 			return NULL; | 
 | 2259 | 		if (!k) | 
 | 2260 | 			break; | 
 | 2261 | 	} | 
 | 2262 |  | 
 | 2263 | 	if (i >= vl->ob_size || i >= wl->ob_size) { | 
 | 2264 | 		/* No more items to compare -- compare sizes */ | 
 | 2265 | 		int vs = vl->ob_size; | 
 | 2266 | 		int ws = wl->ob_size; | 
 | 2267 | 		int cmp; | 
 | 2268 | 		PyObject *res; | 
 | 2269 | 		switch (op) { | 
 | 2270 | 		case Py_LT: cmp = vs <  ws; break; | 
| Tim Peters | 6ee4234 | 2001-07-06 17:45:43 +0000 | [diff] [blame] | 2271 | 		case Py_LE: cmp = vs <= ws; break; | 
| Guido van Rossum | 65e1cea | 2001-01-17 22:11:59 +0000 | [diff] [blame] | 2272 | 		case Py_EQ: cmp = vs == ws; break; | 
 | 2273 | 		case Py_NE: cmp = vs != ws; break; | 
 | 2274 | 		case Py_GT: cmp = vs >  ws; break; | 
 | 2275 | 		case Py_GE: cmp = vs >= ws; break; | 
 | 2276 | 		default: return NULL; /* cannot happen */ | 
 | 2277 | 		} | 
 | 2278 | 		if (cmp) | 
 | 2279 | 			res = Py_True; | 
 | 2280 | 		else | 
 | 2281 | 			res = Py_False; | 
 | 2282 | 		Py_INCREF(res); | 
 | 2283 | 		return res; | 
 | 2284 | 	} | 
 | 2285 |  | 
 | 2286 | 	/* We have an item that differs -- shortcuts for EQ/NE */ | 
 | 2287 | 	if (op == Py_EQ) { | 
 | 2288 | 		Py_INCREF(Py_False); | 
 | 2289 | 		return Py_False; | 
 | 2290 | 	} | 
 | 2291 | 	if (op == Py_NE) { | 
 | 2292 | 		Py_INCREF(Py_True); | 
 | 2293 | 		return Py_True; | 
 | 2294 | 	} | 
 | 2295 |  | 
 | 2296 | 	/* Compare the final item again using the proper operator */ | 
 | 2297 | 	return PyObject_RichCompare(vl->ob_item[i], wl->ob_item[i], op); | 
 | 2298 | } | 
 | 2299 |  | 
| Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2300 | static int | 
 | 2301 | list_init(PyListObject *self, PyObject *args, PyObject *kw) | 
 | 2302 | { | 
 | 2303 | 	PyObject *arg = NULL; | 
 | 2304 | 	static char *kwlist[] = {"sequence", 0}; | 
 | 2305 |  | 
 | 2306 | 	if (!PyArg_ParseTupleAndKeywords(args, kw, "|O:list", kwlist, &arg)) | 
 | 2307 | 		return -1; | 
| Raymond Hettinger | 90a39bf | 2004-02-15 03:57:00 +0000 | [diff] [blame] | 2308 | 	/* Empty previous contents */ | 
| Raymond Hettinger | fdfe618 | 2004-05-05 06:28:16 +0000 | [diff] [blame] | 2309 | 	self->allocated = self->ob_size; | 
| Raymond Hettinger | 90a39bf | 2004-02-15 03:57:00 +0000 | [diff] [blame] | 2310 | 	if (self->ob_size != 0) { | 
 | 2311 | 		if (list_ass_slice(self, 0, self->ob_size, (PyObject *)NULL) != 0) | 
 | 2312 | 			return -1; | 
 | 2313 | 	} | 
 | 2314 | 	if (arg != NULL) { | 
 | 2315 | 		PyObject *rv = listextend(self, arg); | 
 | 2316 | 		if (rv == NULL) | 
 | 2317 | 			return -1; | 
 | 2318 | 		Py_DECREF(rv); | 
 | 2319 | 	} | 
| Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2320 | 	return 0; | 
 | 2321 | } | 
 | 2322 |  | 
| Guido van Rossum | dbb53d9 | 2001-12-03 16:32:18 +0000 | [diff] [blame] | 2323 | static long | 
 | 2324 | list_nohash(PyObject *self) | 
 | 2325 | { | 
 | 2326 | 	PyErr_SetString(PyExc_TypeError, "list objects are unhashable"); | 
 | 2327 | 	return -1; | 
 | 2328 | } | 
 | 2329 |  | 
| Raymond Hettinger | 1021c44 | 2003-11-07 15:38:09 +0000 | [diff] [blame] | 2330 | static PyObject *list_iter(PyObject *seq); | 
 | 2331 | static PyObject *list_reversed(PyListObject* seq, PyObject* unused); | 
 | 2332 |  | 
| Raymond Hettinger | 8f5cdaa | 2003-12-13 11:26:12 +0000 | [diff] [blame] | 2333 | PyDoc_STRVAR(getitem_doc, | 
 | 2334 | "x.__getitem__(y) <==> x[y]"); | 
| Raymond Hettinger | 1021c44 | 2003-11-07 15:38:09 +0000 | [diff] [blame] | 2335 | PyDoc_STRVAR(reversed_doc, | 
 | 2336 | "L.__reversed__() -- return a reverse iterator over the list"); | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2337 | PyDoc_STRVAR(append_doc, | 
 | 2338 | "L.append(object) -- append object to end"); | 
 | 2339 | PyDoc_STRVAR(extend_doc, | 
| Raymond Hettinger | f8bcfb1 | 2002-12-29 05:49:09 +0000 | [diff] [blame] | 2340 | "L.extend(iterable) -- extend list by appending elements from the iterable"); | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2341 | PyDoc_STRVAR(insert_doc, | 
 | 2342 | "L.insert(index, object) -- insert object before index"); | 
 | 2343 | PyDoc_STRVAR(pop_doc, | 
 | 2344 | "L.pop([index]) -> item -- remove and return item at index (default last)"); | 
 | 2345 | PyDoc_STRVAR(remove_doc, | 
 | 2346 | "L.remove(value) -- remove first occurrence of value"); | 
 | 2347 | PyDoc_STRVAR(index_doc, | 
| Raymond Hettinger | d05abde | 2003-06-17 05:05:49 +0000 | [diff] [blame] | 2348 | "L.index(value, [start, [stop]]) -> integer -- return first index of value"); | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2349 | PyDoc_STRVAR(count_doc, | 
 | 2350 | "L.count(value) -> integer -- return number of occurrences of value"); | 
 | 2351 | PyDoc_STRVAR(reverse_doc, | 
 | 2352 | "L.reverse() -- reverse *IN PLACE*"); | 
 | 2353 | PyDoc_STRVAR(sort_doc, | 
| Raymond Hettinger | 42b1ba3 | 2003-10-16 03:41:09 +0000 | [diff] [blame] | 2354 | "L.sort(cmp=None, key=None, reverse=False) -- stable sort *IN PLACE*;\n\ | 
 | 2355 | cmp(x, y) -> -1, 0, 1"); | 
| Guido van Rossum | 3dd7f3f | 1998-06-30 15:36:32 +0000 | [diff] [blame] | 2356 |  | 
| Raymond Hettinger | 8f5cdaa | 2003-12-13 11:26:12 +0000 | [diff] [blame] | 2357 | static PyObject *list_subscript(PyListObject*, PyObject*); | 
 | 2358 |  | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2359 | static PyMethodDef list_methods[] = { | 
| Raymond Hettinger | 8f5cdaa | 2003-12-13 11:26:12 +0000 | [diff] [blame] | 2360 | 	{"__getitem__", (PyCFunction)list_subscript, METH_O|METH_COEXIST, getitem_doc}, | 
| Raymond Hettinger | 1021c44 | 2003-11-07 15:38:09 +0000 | [diff] [blame] | 2361 | 	{"__reversed__",(PyCFunction)list_reversed, METH_NOARGS, reversed_doc}, | 
| Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 2362 | 	{"append",	(PyCFunction)listappend,  METH_O, append_doc}, | 
| Guido van Rossum | 65e1cea | 2001-01-17 22:11:59 +0000 | [diff] [blame] | 2363 | 	{"insert",	(PyCFunction)listinsert,  METH_VARARGS, insert_doc}, | 
| Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 2364 | 	{"extend",      (PyCFunction)listextend,  METH_O, extend_doc}, | 
| Guido van Rossum | 65e1cea | 2001-01-17 22:11:59 +0000 | [diff] [blame] | 2365 | 	{"pop",		(PyCFunction)listpop, 	  METH_VARARGS, pop_doc}, | 
| Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 2366 | 	{"remove",	(PyCFunction)listremove,  METH_O, remove_doc}, | 
| Raymond Hettinger | d05abde | 2003-06-17 05:05:49 +0000 | [diff] [blame] | 2367 | 	{"index",	(PyCFunction)listindex,   METH_VARARGS, index_doc}, | 
| Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 2368 | 	{"count",	(PyCFunction)listcount,   METH_O, count_doc}, | 
 | 2369 | 	{"reverse",	(PyCFunction)listreverse, METH_NOARGS, reverse_doc}, | 
| Raymond Hettinger | 42b1ba3 | 2003-10-16 03:41:09 +0000 | [diff] [blame] | 2370 | 	{"sort",	(PyCFunction)listsort, 	  METH_VARARGS | METH_KEYWORDS, sort_doc}, | 
| Tim Peters | a64dc24 | 2002-08-01 02:13:36 +0000 | [diff] [blame] | 2371 |  	{NULL,		NULL}		/* sentinel */ | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2372 | }; | 
 | 2373 |  | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2374 | static PySequenceMethods list_as_sequence = { | 
| Guido van Rossum | 65e1cea | 2001-01-17 22:11:59 +0000 | [diff] [blame] | 2375 | 	(inquiry)list_length,			/* sq_length */ | 
 | 2376 | 	(binaryfunc)list_concat,		/* sq_concat */ | 
 | 2377 | 	(intargfunc)list_repeat,		/* sq_repeat */ | 
 | 2378 | 	(intargfunc)list_item,			/* sq_item */ | 
 | 2379 | 	(intintargfunc)list_slice,		/* sq_slice */ | 
 | 2380 | 	(intobjargproc)list_ass_item,		/* sq_ass_item */ | 
 | 2381 | 	(intintobjargproc)list_ass_slice,	/* sq_ass_slice */ | 
 | 2382 | 	(objobjproc)list_contains,		/* sq_contains */ | 
 | 2383 | 	(binaryfunc)list_inplace_concat,	/* sq_inplace_concat */ | 
 | 2384 | 	(intargfunc)list_inplace_repeat,	/* sq_inplace_repeat */ | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2385 | }; | 
 | 2386 |  | 
| Neal Norwitz | 2c2e827 | 2002-06-14 02:04:18 +0000 | [diff] [blame] | 2387 | PyDoc_STRVAR(list_doc, | 
| Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2388 | "list() -> new list\n" | 
| Neal Norwitz | 2c2e827 | 2002-06-14 02:04:18 +0000 | [diff] [blame] | 2389 | "list(sequence) -> new list initialized from sequence's items"); | 
| Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2390 |  | 
| Jeremy Hylton | a4b4c3b | 2002-07-13 03:51:17 +0000 | [diff] [blame] | 2391 | static PyObject * | 
| Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 2392 | list_subscript(PyListObject* self, PyObject* item) | 
 | 2393 | { | 
 | 2394 | 	if (PyInt_Check(item)) { | 
 | 2395 | 		long i = PyInt_AS_LONG(item); | 
 | 2396 | 		if (i < 0) | 
 | 2397 | 			i += PyList_GET_SIZE(self); | 
 | 2398 | 		return list_item(self, i); | 
 | 2399 | 	} | 
 | 2400 | 	else if (PyLong_Check(item)) { | 
 | 2401 | 		long i = PyLong_AsLong(item); | 
 | 2402 | 		if (i == -1 && PyErr_Occurred()) | 
 | 2403 | 			return NULL; | 
 | 2404 | 		if (i < 0) | 
 | 2405 | 			i += PyList_GET_SIZE(self); | 
 | 2406 | 		return list_item(self, i); | 
 | 2407 | 	} | 
 | 2408 | 	else if (PySlice_Check(item)) { | 
 | 2409 | 		int start, stop, step, slicelength, cur, i; | 
 | 2410 | 		PyObject* result; | 
 | 2411 | 		PyObject* it; | 
| Raymond Hettinger | a6366fe | 2004-03-09 13:05:22 +0000 | [diff] [blame] | 2412 | 		PyObject **src, **dest; | 
| Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 2413 |  | 
 | 2414 | 		if (PySlice_GetIndicesEx((PySliceObject*)item, self->ob_size, | 
 | 2415 | 				 &start, &stop, &step, &slicelength) < 0) { | 
 | 2416 | 			return NULL; | 
 | 2417 | 		} | 
 | 2418 |  | 
 | 2419 | 		if (slicelength <= 0) { | 
 | 2420 | 			return PyList_New(0); | 
 | 2421 | 		} | 
 | 2422 | 		else { | 
 | 2423 | 			result = PyList_New(slicelength); | 
 | 2424 | 			if (!result) return NULL; | 
 | 2425 |  | 
| Raymond Hettinger | a6366fe | 2004-03-09 13:05:22 +0000 | [diff] [blame] | 2426 | 			src = self->ob_item; | 
 | 2427 | 			dest = ((PyListObject *)result)->ob_item; | 
| Tim Peters | 3b01a12 | 2002-07-19 02:35:45 +0000 | [diff] [blame] | 2428 | 			for (cur = start, i = 0; i < slicelength; | 
| Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 2429 | 			     cur += step, i++) { | 
| Raymond Hettinger | a6366fe | 2004-03-09 13:05:22 +0000 | [diff] [blame] | 2430 | 				it = src[cur]; | 
| Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 2431 | 				Py_INCREF(it); | 
| Raymond Hettinger | a6366fe | 2004-03-09 13:05:22 +0000 | [diff] [blame] | 2432 | 				dest[i] = it; | 
| Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 2433 | 			} | 
| Tim Peters | 3b01a12 | 2002-07-19 02:35:45 +0000 | [diff] [blame] | 2434 |  | 
| Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 2435 | 			return result; | 
 | 2436 | 		} | 
 | 2437 | 	} | 
 | 2438 | 	else { | 
 | 2439 | 		PyErr_SetString(PyExc_TypeError, | 
 | 2440 | 				"list indices must be integers"); | 
 | 2441 | 		return NULL; | 
 | 2442 | 	} | 
 | 2443 | } | 
 | 2444 |  | 
| Tim Peters | 3b01a12 | 2002-07-19 02:35:45 +0000 | [diff] [blame] | 2445 | static int | 
| Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 2446 | list_ass_subscript(PyListObject* self, PyObject* item, PyObject* value) | 
 | 2447 | { | 
 | 2448 | 	if (PyInt_Check(item)) { | 
 | 2449 | 		long i = PyInt_AS_LONG(item); | 
 | 2450 | 		if (i < 0) | 
 | 2451 | 			i += PyList_GET_SIZE(self); | 
 | 2452 | 		return list_ass_item(self, i, value); | 
 | 2453 | 	} | 
 | 2454 | 	else if (PyLong_Check(item)) { | 
 | 2455 | 		long i = PyLong_AsLong(item); | 
 | 2456 | 		if (i == -1 && PyErr_Occurred()) | 
 | 2457 | 			return -1; | 
 | 2458 | 		if (i < 0) | 
 | 2459 | 			i += PyList_GET_SIZE(self); | 
 | 2460 | 		return list_ass_item(self, i, value); | 
 | 2461 | 	} | 
 | 2462 | 	else if (PySlice_Check(item)) { | 
 | 2463 | 		int start, stop, step, slicelength; | 
 | 2464 |  | 
 | 2465 | 		if (PySlice_GetIndicesEx((PySliceObject*)item, self->ob_size, | 
 | 2466 | 				 &start, &stop, &step, &slicelength) < 0) { | 
 | 2467 | 			return -1; | 
 | 2468 | 		} | 
 | 2469 |  | 
| Michael W. Hudson | 9c14bad | 2002-06-19 15:44:15 +0000 | [diff] [blame] | 2470 | 		/* treat L[slice(a,b)] = v _exactly_ like L[a:b] = v */ | 
 | 2471 | 		if (step == 1 && ((PySliceObject*)item)->step == Py_None) | 
 | 2472 | 			return list_ass_slice(self, start, stop, value); | 
 | 2473 |  | 
| Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 2474 | 		if (value == NULL) { | 
 | 2475 | 			/* delete slice */ | 
| Raymond Hettinger | 4bb9540 | 2004-02-13 11:36:39 +0000 | [diff] [blame] | 2476 | 			PyObject **garbage; | 
| Raymond Hettinger | a6366fe | 2004-03-09 13:05:22 +0000 | [diff] [blame] | 2477 | 			int cur, i; | 
| Tim Peters | 3b01a12 | 2002-07-19 02:35:45 +0000 | [diff] [blame] | 2478 |  | 
| Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 2479 | 			if (slicelength <= 0) | 
 | 2480 | 				return 0; | 
 | 2481 |  | 
 | 2482 | 			if (step < 0) { | 
 | 2483 | 				stop = start + 1; | 
 | 2484 | 				start = stop + step*(slicelength - 1) - 1; | 
 | 2485 | 				step = -step; | 
 | 2486 | 			} | 
 | 2487 |  | 
 | 2488 | 			garbage = (PyObject**) | 
 | 2489 | 				PyMem_MALLOC(slicelength*sizeof(PyObject*)); | 
| Tim Peters | 3b01a12 | 2002-07-19 02:35:45 +0000 | [diff] [blame] | 2490 |  | 
 | 2491 | 			/* drawing pictures might help | 
| Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 2492 | 			   understand these for loops */ | 
| Guido van Rossum | 75a20b1 | 2002-06-11 12:22:28 +0000 | [diff] [blame] | 2493 | 			for (cur = start, i = 0; | 
 | 2494 | 			     cur < stop; | 
| Michael W. Hudson | 56796f6 | 2002-07-29 14:35:04 +0000 | [diff] [blame] | 2495 | 			     cur += step, i++) { | 
 | 2496 | 				int lim = step; | 
 | 2497 |  | 
| Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 2498 | 				garbage[i] = PyList_GET_ITEM(self, cur); | 
 | 2499 |  | 
| Michael W. Hudson | 56796f6 | 2002-07-29 14:35:04 +0000 | [diff] [blame] | 2500 | 				if (cur + step >= self->ob_size) { | 
 | 2501 | 					lim = self->ob_size - cur - 1; | 
 | 2502 | 				} | 
 | 2503 |  | 
| Raymond Hettinger | a6366fe | 2004-03-09 13:05:22 +0000 | [diff] [blame] | 2504 | 				memmove(self->ob_item + cur - i,  | 
 | 2505 | 					self->ob_item + cur + 1, | 
 | 2506 | 					lim * sizeof(PyObject *)); | 
| Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 2507 | 			} | 
| Raymond Hettinger | a6366fe | 2004-03-09 13:05:22 +0000 | [diff] [blame] | 2508 |  | 
| Michael W. Hudson | 9c14bad | 2002-06-19 15:44:15 +0000 | [diff] [blame] | 2509 | 			for (cur = start + slicelength*step + 1; | 
| Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 2510 | 			     cur < self->ob_size; cur++) { | 
 | 2511 | 				PyList_SET_ITEM(self, cur - slicelength, | 
 | 2512 | 						PyList_GET_ITEM(self, cur)); | 
 | 2513 | 			} | 
| Raymond Hettinger | a6366fe | 2004-03-09 13:05:22 +0000 | [diff] [blame] | 2514 |  | 
| Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 2515 | 			self->ob_size -= slicelength; | 
| Raymond Hettinger | d4ff741 | 2004-03-15 09:01:31 +0000 | [diff] [blame] | 2516 | 			list_resize(self, self->ob_size); | 
| Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 2517 |  | 
 | 2518 | 			for (i = 0; i < slicelength; i++) { | 
 | 2519 | 				Py_DECREF(garbage[i]); | 
 | 2520 | 			} | 
 | 2521 | 			PyMem_FREE(garbage); | 
 | 2522 |  | 
 | 2523 | 			return 0; | 
 | 2524 | 		} | 
 | 2525 | 		else { | 
 | 2526 | 			/* assign slice */ | 
| Raymond Hettinger | a6366fe | 2004-03-09 13:05:22 +0000 | [diff] [blame] | 2527 | 			PyObject **garbage, *ins, *seq, **seqitems, **selfitems; | 
| Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 2528 | 			int cur, i; | 
 | 2529 |  | 
| Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 2530 | 			/* protect against a[::-1] = a */ | 
| Tim Peters | 3b01a12 | 2002-07-19 02:35:45 +0000 | [diff] [blame] | 2531 | 			if (self == (PyListObject*)value) { | 
| Michael W. Hudson | a69c030 | 2002-12-05 21:32:32 +0000 | [diff] [blame] | 2532 | 				seq = list_slice((PyListObject*)value, 0, | 
| Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 2533 | 						   PyList_GET_SIZE(value)); | 
| Tim Peters | 3b01a12 | 2002-07-19 02:35:45 +0000 | [diff] [blame] | 2534 | 			} | 
| Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 2535 | 			else { | 
| Raymond Hettinger | a6366fe | 2004-03-09 13:05:22 +0000 | [diff] [blame] | 2536 | 				seq = PySequence_Fast(value,  | 
 | 2537 | 					"must assign iterable to extended slice"); | 
| Michael W. Hudson | a69c030 | 2002-12-05 21:32:32 +0000 | [diff] [blame] | 2538 | 				if (!seq) | 
 | 2539 | 					return -1; | 
 | 2540 | 			} | 
 | 2541 |  | 
 | 2542 | 			if (PySequence_Fast_GET_SIZE(seq) != slicelength) { | 
 | 2543 | 				PyErr_Format(PyExc_ValueError, | 
 | 2544 |             "attempt to assign sequence of size %d to extended slice of size %d", | 
 | 2545 | 					     PySequence_Fast_GET_SIZE(seq), | 
 | 2546 | 					     slicelength); | 
 | 2547 | 				Py_DECREF(seq); | 
 | 2548 | 				return -1; | 
 | 2549 | 			} | 
 | 2550 |  | 
 | 2551 | 			if (!slicelength) { | 
 | 2552 | 				Py_DECREF(seq); | 
 | 2553 | 				return 0; | 
| Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 2554 | 			} | 
 | 2555 |  | 
 | 2556 | 			garbage = (PyObject**) | 
 | 2557 | 				PyMem_MALLOC(slicelength*sizeof(PyObject*)); | 
| Tim Peters | 3b01a12 | 2002-07-19 02:35:45 +0000 | [diff] [blame] | 2558 |  | 
| Raymond Hettinger | a6366fe | 2004-03-09 13:05:22 +0000 | [diff] [blame] | 2559 | 			selfitems = self->ob_item; | 
| Raymond Hettinger | 42bec93 | 2004-03-12 16:38:17 +0000 | [diff] [blame] | 2560 | 			seqitems = PySequence_Fast_ITEMS(seq); | 
| Tim Peters | 3b01a12 | 2002-07-19 02:35:45 +0000 | [diff] [blame] | 2561 | 			for (cur = start, i = 0; i < slicelength; | 
| Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 2562 | 			     cur += step, i++) { | 
| Raymond Hettinger | a6366fe | 2004-03-09 13:05:22 +0000 | [diff] [blame] | 2563 | 				garbage[i] = selfitems[cur]; | 
 | 2564 | 				ins = seqitems[i]; | 
| Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 2565 | 				Py_INCREF(ins); | 
| Raymond Hettinger | a6366fe | 2004-03-09 13:05:22 +0000 | [diff] [blame] | 2566 | 				selfitems[cur] = ins; | 
| Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 2567 | 			} | 
 | 2568 |  | 
 | 2569 | 			for (i = 0; i < slicelength; i++) { | 
 | 2570 | 				Py_DECREF(garbage[i]); | 
 | 2571 | 			} | 
| Tim Peters | 3b01a12 | 2002-07-19 02:35:45 +0000 | [diff] [blame] | 2572 |  | 
| Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 2573 | 			PyMem_FREE(garbage); | 
| Michael W. Hudson | a69c030 | 2002-12-05 21:32:32 +0000 | [diff] [blame] | 2574 | 			Py_DECREF(seq); | 
| Tim Peters | 3b01a12 | 2002-07-19 02:35:45 +0000 | [diff] [blame] | 2575 |  | 
| Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 2576 | 			return 0; | 
 | 2577 | 		} | 
| Tim Peters | 3b01a12 | 2002-07-19 02:35:45 +0000 | [diff] [blame] | 2578 | 	} | 
| Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 2579 | 	else { | 
| Tim Peters | 3b01a12 | 2002-07-19 02:35:45 +0000 | [diff] [blame] | 2580 | 		PyErr_SetString(PyExc_TypeError, | 
| Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 2581 | 				"list indices must be integers"); | 
 | 2582 | 		return -1; | 
 | 2583 | 	} | 
 | 2584 | } | 
 | 2585 |  | 
 | 2586 | static PyMappingMethods list_as_mapping = { | 
 | 2587 | 	(inquiry)list_length, | 
 | 2588 | 	(binaryfunc)list_subscript, | 
 | 2589 | 	(objobjargproc)list_ass_subscript | 
 | 2590 | }; | 
 | 2591 |  | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2592 | PyTypeObject PyList_Type = { | 
 | 2593 | 	PyObject_HEAD_INIT(&PyType_Type) | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2594 | 	0, | 
 | 2595 | 	"list", | 
| Neil Schemenauer | e83c00e | 2001-08-29 23:54:21 +0000 | [diff] [blame] | 2596 | 	sizeof(PyListObject), | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2597 | 	0, | 
| Guido van Rossum | 65e1cea | 2001-01-17 22:11:59 +0000 | [diff] [blame] | 2598 | 	(destructor)list_dealloc,		/* tp_dealloc */ | 
 | 2599 | 	(printfunc)list_print,			/* tp_print */ | 
| Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2600 | 	0,					/* tp_getattr */ | 
| Guido van Rossum | 65e1cea | 2001-01-17 22:11:59 +0000 | [diff] [blame] | 2601 | 	0,					/* tp_setattr */ | 
 | 2602 | 	0,					/* tp_compare */ | 
 | 2603 | 	(reprfunc)list_repr,			/* tp_repr */ | 
 | 2604 | 	0,					/* tp_as_number */ | 
 | 2605 | 	&list_as_sequence,			/* tp_as_sequence */ | 
| Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 2606 | 	&list_as_mapping,			/* tp_as_mapping */ | 
| Guido van Rossum | dbb53d9 | 2001-12-03 16:32:18 +0000 | [diff] [blame] | 2607 | 	list_nohash,				/* tp_hash */ | 
| Guido van Rossum | 65e1cea | 2001-01-17 22:11:59 +0000 | [diff] [blame] | 2608 | 	0,					/* tp_call */ | 
 | 2609 | 	0,					/* tp_str */ | 
| Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2610 | 	PyObject_GenericGetAttr,		/* tp_getattro */ | 
| Guido van Rossum | 65e1cea | 2001-01-17 22:11:59 +0000 | [diff] [blame] | 2611 | 	0,					/* tp_setattro */ | 
 | 2612 | 	0,					/* tp_as_buffer */ | 
| Neil Schemenauer | e83c00e | 2001-08-29 23:54:21 +0000 | [diff] [blame] | 2613 | 	Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | | 
| Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2614 | 		Py_TPFLAGS_BASETYPE,		/* tp_flags */ | 
 | 2615 |  	list_doc,				/* tp_doc */ | 
| Guido van Rossum | 65e1cea | 2001-01-17 22:11:59 +0000 | [diff] [blame] | 2616 |  	(traverseproc)list_traverse,		/* tp_traverse */ | 
 | 2617 |  	(inquiry)list_clear,			/* tp_clear */ | 
 | 2618 | 	list_richcompare,			/* tp_richcompare */ | 
| Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2619 | 	0,					/* tp_weaklistoffset */ | 
| Raymond Hettinger | 14bd6de | 2002-05-31 21:40:38 +0000 | [diff] [blame] | 2620 | 	list_iter,				/* tp_iter */ | 
| Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2621 | 	0,					/* tp_iternext */ | 
 | 2622 | 	list_methods,				/* tp_methods */ | 
 | 2623 | 	0,					/* tp_members */ | 
 | 2624 | 	0,					/* tp_getset */ | 
 | 2625 | 	0,					/* tp_base */ | 
 | 2626 | 	0,					/* tp_dict */ | 
 | 2627 | 	0,					/* tp_descr_get */ | 
 | 2628 | 	0,					/* tp_descr_set */ | 
 | 2629 | 	0,					/* tp_dictoffset */ | 
 | 2630 | 	(initproc)list_init,			/* tp_init */ | 
 | 2631 | 	PyType_GenericAlloc,			/* tp_alloc */ | 
 | 2632 | 	PyType_GenericNew,			/* tp_new */ | 
| Guido van Rossum | 6b6272c | 2002-07-16 20:10:23 +0000 | [diff] [blame] | 2633 | 	PyObject_GC_Del,			/* tp_free */ | 
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2634 | }; | 
| Guido van Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 2635 |  | 
 | 2636 |  | 
| Raymond Hettinger | 14bd6de | 2002-05-31 21:40:38 +0000 | [diff] [blame] | 2637 | /*********************** List Iterator **************************/ | 
 | 2638 |  | 
 | 2639 | typedef struct { | 
| Guido van Rossum | 6b6272c | 2002-07-16 20:10:23 +0000 | [diff] [blame] | 2640 | 	PyObject_HEAD | 
 | 2641 | 	long it_index; | 
| Guido van Rossum | 86103ae | 2002-07-16 20:07:32 +0000 | [diff] [blame] | 2642 | 	PyListObject *it_seq; /* Set to NULL when iterator is exhausted */ | 
| Raymond Hettinger | 14bd6de | 2002-05-31 21:40:38 +0000 | [diff] [blame] | 2643 | } listiterobject; | 
 | 2644 |  | 
 | 2645 | PyTypeObject PyListIter_Type; | 
 | 2646 |  | 
| Guido van Rossum | 5086e49 | 2002-07-16 15:56:52 +0000 | [diff] [blame] | 2647 | static PyObject * | 
| Raymond Hettinger | 14bd6de | 2002-05-31 21:40:38 +0000 | [diff] [blame] | 2648 | list_iter(PyObject *seq) | 
 | 2649 | { | 
| Guido van Rossum | 6b6272c | 2002-07-16 20:10:23 +0000 | [diff] [blame] | 2650 | 	listiterobject *it; | 
| Raymond Hettinger | 14bd6de | 2002-05-31 21:40:38 +0000 | [diff] [blame] | 2651 |  | 
| Guido van Rossum | 6b6272c | 2002-07-16 20:10:23 +0000 | [diff] [blame] | 2652 | 	if (!PyList_Check(seq)) { | 
 | 2653 | 		PyErr_BadInternalCall(); | 
 | 2654 | 		return NULL; | 
 | 2655 | 	} | 
 | 2656 | 	it = PyObject_GC_New(listiterobject, &PyListIter_Type); | 
 | 2657 | 	if (it == NULL) | 
 | 2658 | 		return NULL; | 
 | 2659 | 	it->it_index = 0; | 
 | 2660 | 	Py_INCREF(seq); | 
 | 2661 | 	it->it_seq = (PyListObject *)seq; | 
 | 2662 | 	_PyObject_GC_TRACK(it); | 
 | 2663 | 	return (PyObject *)it; | 
| Raymond Hettinger | 14bd6de | 2002-05-31 21:40:38 +0000 | [diff] [blame] | 2664 | } | 
 | 2665 |  | 
 | 2666 | static void | 
 | 2667 | listiter_dealloc(listiterobject *it) | 
 | 2668 | { | 
| Guido van Rossum | 6b6272c | 2002-07-16 20:10:23 +0000 | [diff] [blame] | 2669 | 	_PyObject_GC_UNTRACK(it); | 
| Guido van Rossum | 86103ae | 2002-07-16 20:07:32 +0000 | [diff] [blame] | 2670 | 	Py_XDECREF(it->it_seq); | 
| Guido van Rossum | 6b6272c | 2002-07-16 20:10:23 +0000 | [diff] [blame] | 2671 | 	PyObject_GC_Del(it); | 
| Raymond Hettinger | 14bd6de | 2002-05-31 21:40:38 +0000 | [diff] [blame] | 2672 | } | 
 | 2673 |  | 
 | 2674 | static int | 
 | 2675 | listiter_traverse(listiterobject *it, visitproc visit, void *arg) | 
 | 2676 | { | 
| Guido van Rossum | 86103ae | 2002-07-16 20:07:32 +0000 | [diff] [blame] | 2677 | 	if (it->it_seq == NULL) | 
 | 2678 | 		return 0; | 
| Guido van Rossum | 6b6272c | 2002-07-16 20:10:23 +0000 | [diff] [blame] | 2679 | 	return visit((PyObject *)it->it_seq, arg); | 
| Raymond Hettinger | 14bd6de | 2002-05-31 21:40:38 +0000 | [diff] [blame] | 2680 | } | 
 | 2681 |  | 
| Raymond Hettinger | 14bd6de | 2002-05-31 21:40:38 +0000 | [diff] [blame] | 2682 | static PyObject * | 
| Tim Peters | 93b2cc4 | 2002-06-01 05:22:55 +0000 | [diff] [blame] | 2683 | listiter_next(listiterobject *it) | 
| Raymond Hettinger | 14bd6de | 2002-05-31 21:40:38 +0000 | [diff] [blame] | 2684 | { | 
| Guido van Rossum | 6b6272c | 2002-07-16 20:10:23 +0000 | [diff] [blame] | 2685 | 	PyListObject *seq; | 
 | 2686 | 	PyObject *item; | 
| Raymond Hettinger | 14bd6de | 2002-05-31 21:40:38 +0000 | [diff] [blame] | 2687 |  | 
| Tim Peters | 93b2cc4 | 2002-06-01 05:22:55 +0000 | [diff] [blame] | 2688 | 	assert(it != NULL); | 
| Guido van Rossum | 6b6272c | 2002-07-16 20:10:23 +0000 | [diff] [blame] | 2689 | 	seq = it->it_seq; | 
| Guido van Rossum | 86103ae | 2002-07-16 20:07:32 +0000 | [diff] [blame] | 2690 | 	if (seq == NULL) | 
 | 2691 | 		return NULL; | 
| Guido van Rossum | 6b6272c | 2002-07-16 20:10:23 +0000 | [diff] [blame] | 2692 | 	assert(PyList_Check(seq)); | 
| Raymond Hettinger | 14bd6de | 2002-05-31 21:40:38 +0000 | [diff] [blame] | 2693 |  | 
| Guido van Rossum | 6b6272c | 2002-07-16 20:10:23 +0000 | [diff] [blame] | 2694 | 	if (it->it_index < PyList_GET_SIZE(seq)) { | 
| Tim Peters | 93b2cc4 | 2002-06-01 05:22:55 +0000 | [diff] [blame] | 2695 | 		item = PyList_GET_ITEM(seq, it->it_index); | 
 | 2696 | 		++it->it_index; | 
| Guido van Rossum | 6b6272c | 2002-07-16 20:10:23 +0000 | [diff] [blame] | 2697 | 		Py_INCREF(item); | 
 | 2698 | 		return item; | 
 | 2699 | 	} | 
| Guido van Rossum | 86103ae | 2002-07-16 20:07:32 +0000 | [diff] [blame] | 2700 |  | 
 | 2701 | 	Py_DECREF(seq); | 
 | 2702 | 	it->it_seq = NULL; | 
| Guido van Rossum | 6b6272c | 2002-07-16 20:10:23 +0000 | [diff] [blame] | 2703 | 	return NULL; | 
| Raymond Hettinger | 14bd6de | 2002-05-31 21:40:38 +0000 | [diff] [blame] | 2704 | } | 
 | 2705 |  | 
| Raymond Hettinger | 435bf58 | 2004-03-18 22:43:10 +0000 | [diff] [blame] | 2706 | static int | 
 | 2707 | listiter_len(listiterobject *it) | 
 | 2708 | { | 
| Raymond Hettinger | 40a0382 | 2004-04-12 13:05:09 +0000 | [diff] [blame] | 2709 | 	int len; | 
 | 2710 | 	if (it->it_seq) { | 
 | 2711 | 		len = PyList_GET_SIZE(it->it_seq) - it->it_index; | 
 | 2712 | 		if (len >= 0) | 
 | 2713 | 			return len; | 
 | 2714 | 	} | 
| Raymond Hettinger | 435bf58 | 2004-03-18 22:43:10 +0000 | [diff] [blame] | 2715 | 	return 0; | 
 | 2716 | } | 
 | 2717 |  | 
 | 2718 | static PySequenceMethods listiter_as_sequence = { | 
 | 2719 | 	(inquiry)listiter_len,		/* sq_length */ | 
 | 2720 | 	0,				/* sq_concat */ | 
 | 2721 | }; | 
 | 2722 |  | 
| Raymond Hettinger | 14bd6de | 2002-05-31 21:40:38 +0000 | [diff] [blame] | 2723 | PyTypeObject PyListIter_Type = { | 
| Guido van Rossum | 6b6272c | 2002-07-16 20:10:23 +0000 | [diff] [blame] | 2724 | 	PyObject_HEAD_INIT(&PyType_Type) | 
 | 2725 | 	0,					/* ob_size */ | 
 | 2726 | 	"listiterator",				/* tp_name */ | 
 | 2727 | 	sizeof(listiterobject),			/* tp_basicsize */ | 
 | 2728 | 	0,					/* tp_itemsize */ | 
 | 2729 | 	/* methods */ | 
 | 2730 | 	(destructor)listiter_dealloc,		/* tp_dealloc */ | 
 | 2731 | 	0,					/* tp_print */ | 
 | 2732 | 	0,					/* tp_getattr */ | 
 | 2733 | 	0,					/* tp_setattr */ | 
 | 2734 | 	0,					/* tp_compare */ | 
 | 2735 | 	0,					/* tp_repr */ | 
 | 2736 | 	0,					/* tp_as_number */ | 
| Raymond Hettinger | 435bf58 | 2004-03-18 22:43:10 +0000 | [diff] [blame] | 2737 | 	&listiter_as_sequence,			/* tp_as_sequence */ | 
| Guido van Rossum | 6b6272c | 2002-07-16 20:10:23 +0000 | [diff] [blame] | 2738 | 	0,					/* tp_as_mapping */ | 
 | 2739 | 	0,					/* tp_hash */ | 
 | 2740 | 	0,					/* tp_call */ | 
 | 2741 | 	0,					/* tp_str */ | 
 | 2742 | 	PyObject_GenericGetAttr,		/* tp_getattro */ | 
 | 2743 | 	0,					/* tp_setattro */ | 
 | 2744 | 	0,					/* tp_as_buffer */ | 
 | 2745 | 	Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ | 
 | 2746 | 	0,					/* tp_doc */ | 
 | 2747 | 	(traverseproc)listiter_traverse,	/* tp_traverse */ | 
 | 2748 | 	0,					/* tp_clear */ | 
 | 2749 | 	0,					/* tp_richcompare */ | 
 | 2750 | 	0,					/* tp_weaklistoffset */ | 
| Raymond Hettinger | 1da1dbf | 2003-03-17 19:46:11 +0000 | [diff] [blame] | 2751 | 	PyObject_SelfIter,			/* tp_iter */ | 
| Guido van Rossum | 6b6272c | 2002-07-16 20:10:23 +0000 | [diff] [blame] | 2752 | 	(iternextfunc)listiter_next,		/* tp_iternext */ | 
| Guido van Rossum | 86103ae | 2002-07-16 20:07:32 +0000 | [diff] [blame] | 2753 | 	0,					/* tp_methods */ | 
| Guido van Rossum | 6b6272c | 2002-07-16 20:10:23 +0000 | [diff] [blame] | 2754 | 	0,					/* tp_members */ | 
 | 2755 | 	0,					/* tp_getset */ | 
 | 2756 | 	0,					/* tp_base */ | 
 | 2757 | 	0,					/* tp_dict */ | 
 | 2758 | 	0,					/* tp_descr_get */ | 
 | 2759 | 	0,					/* tp_descr_set */ | 
| Raymond Hettinger | 14bd6de | 2002-05-31 21:40:38 +0000 | [diff] [blame] | 2760 | }; | 
| Raymond Hettinger | 1021c44 | 2003-11-07 15:38:09 +0000 | [diff] [blame] | 2761 |  | 
 | 2762 | /*********************** List Reverse Iterator **************************/ | 
 | 2763 |  | 
 | 2764 | typedef struct { | 
 | 2765 | 	PyObject_HEAD | 
 | 2766 | 	long it_index; | 
| Raymond Hettinger | 001f228 | 2003-11-08 11:58:44 +0000 | [diff] [blame] | 2767 | 	PyListObject *it_seq; /* Set to NULL when iterator is exhausted */ | 
| Raymond Hettinger | 1021c44 | 2003-11-07 15:38:09 +0000 | [diff] [blame] | 2768 | } listreviterobject; | 
 | 2769 |  | 
 | 2770 | PyTypeObject PyListRevIter_Type; | 
 | 2771 |  | 
 | 2772 | static PyObject * | 
 | 2773 | list_reversed(PyListObject *seq, PyObject *unused) | 
 | 2774 | { | 
 | 2775 | 	listreviterobject *it; | 
 | 2776 |  | 
 | 2777 | 	it = PyObject_GC_New(listreviterobject, &PyListRevIter_Type); | 
 | 2778 | 	if (it == NULL) | 
 | 2779 | 		return NULL; | 
 | 2780 | 	assert(PyList_Check(seq)); | 
 | 2781 | 	it->it_index = PyList_GET_SIZE(seq) - 1; | 
 | 2782 | 	Py_INCREF(seq); | 
 | 2783 | 	it->it_seq = seq; | 
 | 2784 | 	PyObject_GC_Track(it); | 
 | 2785 | 	return (PyObject *)it; | 
 | 2786 | } | 
 | 2787 |  | 
 | 2788 | static void | 
 | 2789 | listreviter_dealloc(listreviterobject *it) | 
 | 2790 | { | 
 | 2791 | 	PyObject_GC_UnTrack(it); | 
 | 2792 | 	Py_XDECREF(it->it_seq); | 
 | 2793 | 	PyObject_GC_Del(it); | 
 | 2794 | } | 
 | 2795 |  | 
 | 2796 | static int | 
 | 2797 | listreviter_traverse(listreviterobject *it, visitproc visit, void *arg) | 
 | 2798 | { | 
 | 2799 | 	if (it->it_seq == NULL) | 
 | 2800 | 		return 0; | 
 | 2801 | 	return visit((PyObject *)it->it_seq, arg); | 
 | 2802 | } | 
 | 2803 |  | 
 | 2804 | static PyObject * | 
 | 2805 | listreviter_next(listreviterobject *it) | 
 | 2806 | { | 
| Raymond Hettinger | ef9bf40 | 2004-03-10 10:10:42 +0000 | [diff] [blame] | 2807 | 	PyObject *item; | 
 | 2808 | 	long index = it->it_index; | 
 | 2809 | 	PyListObject *seq = it->it_seq; | 
| Raymond Hettinger | 1021c44 | 2003-11-07 15:38:09 +0000 | [diff] [blame] | 2810 |  | 
| Raymond Hettinger | ef9bf40 | 2004-03-10 10:10:42 +0000 | [diff] [blame] | 2811 | 	if (index>=0 && index < PyList_GET_SIZE(seq)) { | 
 | 2812 | 		item = PyList_GET_ITEM(seq, index); | 
| Raymond Hettinger | 1021c44 | 2003-11-07 15:38:09 +0000 | [diff] [blame] | 2813 | 		it->it_index--; | 
 | 2814 | 		Py_INCREF(item); | 
| Raymond Hettinger | ef9bf40 | 2004-03-10 10:10:42 +0000 | [diff] [blame] | 2815 | 		return item; | 
| Raymond Hettinger | 1021c44 | 2003-11-07 15:38:09 +0000 | [diff] [blame] | 2816 | 	} | 
| Raymond Hettinger | ef9bf40 | 2004-03-10 10:10:42 +0000 | [diff] [blame] | 2817 | 	it->it_index = -1; | 
 | 2818 | 	if (seq != NULL) { | 
 | 2819 | 		it->it_seq = NULL; | 
 | 2820 | 		Py_DECREF(seq); | 
 | 2821 | 	} | 
 | 2822 | 	return NULL; | 
| Raymond Hettinger | 1021c44 | 2003-11-07 15:38:09 +0000 | [diff] [blame] | 2823 | } | 
 | 2824 |  | 
| Raymond Hettinger | ef9bf40 | 2004-03-10 10:10:42 +0000 | [diff] [blame] | 2825 | static int | 
 | 2826 | listreviter_len(listreviterobject *it) | 
 | 2827 | { | 
| Raymond Hettinger | 40a0382 | 2004-04-12 13:05:09 +0000 | [diff] [blame] | 2828 | 	int len = it->it_index + 1; | 
 | 2829 | 	if (it->it_seq == NULL || PyList_GET_SIZE(it->it_seq) < len) | 
 | 2830 | 		return 0; | 
 | 2831 | 	return len; | 
| Raymond Hettinger | ef9bf40 | 2004-03-10 10:10:42 +0000 | [diff] [blame] | 2832 | } | 
 | 2833 |  | 
 | 2834 | static PySequenceMethods listreviter_as_sequence = { | 
 | 2835 | 	(inquiry)listreviter_len,	/* sq_length */ | 
 | 2836 | 	0,				/* sq_concat */ | 
 | 2837 | }; | 
 | 2838 |  | 
| Raymond Hettinger | 1021c44 | 2003-11-07 15:38:09 +0000 | [diff] [blame] | 2839 | PyTypeObject PyListRevIter_Type = { | 
 | 2840 | 	PyObject_HEAD_INIT(&PyType_Type) | 
 | 2841 | 	0,					/* ob_size */ | 
 | 2842 | 	"listreverseiterator",			/* tp_name */ | 
 | 2843 | 	sizeof(listreviterobject),		/* tp_basicsize */ | 
 | 2844 | 	0,					/* tp_itemsize */ | 
 | 2845 | 	/* methods */ | 
 | 2846 | 	(destructor)listreviter_dealloc,	/* tp_dealloc */ | 
 | 2847 | 	0,					/* tp_print */ | 
 | 2848 | 	0,					/* tp_getattr */ | 
 | 2849 | 	0,					/* tp_setattr */ | 
 | 2850 | 	0,					/* tp_compare */ | 
 | 2851 | 	0,					/* tp_repr */ | 
 | 2852 | 	0,					/* tp_as_number */ | 
| Raymond Hettinger | ef9bf40 | 2004-03-10 10:10:42 +0000 | [diff] [blame] | 2853 | 	&listreviter_as_sequence,		/* tp_as_sequence */ | 
| Raymond Hettinger | 1021c44 | 2003-11-07 15:38:09 +0000 | [diff] [blame] | 2854 | 	0,					/* tp_as_mapping */ | 
 | 2855 | 	0,					/* tp_hash */ | 
 | 2856 | 	0,					/* tp_call */ | 
 | 2857 | 	0,					/* tp_str */ | 
 | 2858 | 	PyObject_GenericGetAttr,		/* tp_getattro */ | 
 | 2859 | 	0,					/* tp_setattro */ | 
 | 2860 | 	0,					/* tp_as_buffer */ | 
 | 2861 | 	Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ | 
 | 2862 | 	0,					/* tp_doc */ | 
 | 2863 | 	(traverseproc)listreviter_traverse,	/* tp_traverse */ | 
 | 2864 | 	0,					/* tp_clear */ | 
 | 2865 | 	0,					/* tp_richcompare */ | 
 | 2866 | 	0,					/* tp_weaklistoffset */ | 
 | 2867 | 	PyObject_SelfIter,			/* tp_iter */ | 
 | 2868 | 	(iternextfunc)listreviter_next,		/* tp_iternext */ | 
 | 2869 | 	0, | 
 | 2870 | }; |