Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 1 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2 | /* List object interface */ |
| 3 | |
| 4 | /* |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 5 | Another generally useful object type is an list of object pointers. |
| 6 | This is a mutable type: the list items can be changed, and items can be |
| 7 | added or removed. Out-of-range indices or non-list objects are ignored. |
| 8 | |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 9 | *** WARNING *** PyList_SetItem does not increment the new item's reference |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 10 | count, but does decrement the reference count of the item it replaces, |
| 11 | if not nil. It does *decrement* the reference count if it is *not* |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 12 | inserted in the list. Similarly, PyList_GetItem does not increment the |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 13 | returned item's reference count. |
| 14 | */ |
| 15 | |
Fred Drake | ea9cb5a | 2000-07-09 00:20:36 +0000 | [diff] [blame] | 16 | #ifndef Py_LISTOBJECT_H |
| 17 | #define Py_LISTOBJECT_H |
| 18 | #ifdef __cplusplus |
| 19 | extern "C" { |
| 20 | #endif |
| 21 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 22 | typedef struct { |
Fred Drake | ea9cb5a | 2000-07-09 00:20:36 +0000 | [diff] [blame] | 23 | PyObject_VAR_HEAD |
Neal Norwitz | 4ecd8cd | 2004-08-01 22:45:27 +0000 | [diff] [blame] | 24 | /* Vector of pointers to list elements. list[0] is ob_item[0], etc. */ |
Fred Drake | ea9cb5a | 2000-07-09 00:20:36 +0000 | [diff] [blame] | 25 | PyObject **ob_item; |
Tim Peters | a995a2d | 2004-07-29 03:29:15 +0000 | [diff] [blame] | 26 | |
| 27 | /* ob_item contains space for 'allocated' elements. The number |
| 28 | * currently in use is ob_size. |
| 29 | * Invariants: |
| 30 | * 0 <= ob_size <= allocated |
| 31 | * len(list) == ob_size |
| 32 | * ob_item == NULL implies ob_size == allocated == 0 |
Armin Rigo | 93677f0 | 2004-07-29 12:40:23 +0000 | [diff] [blame] | 33 | * list.sort() temporarily sets allocated to -1 to detect mutations. |
Armin Rigo | 89a3946 | 2004-10-28 16:32:00 +0000 | [diff] [blame] | 34 | * |
| 35 | * Items must normally not be NULL, except during construction when |
| 36 | * the list is not yet visible outside the function that builds it. |
Tim Peters | a995a2d | 2004-07-29 03:29:15 +0000 | [diff] [blame] | 37 | */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 38 | Py_ssize_t allocated; |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 39 | } PyListObject; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 40 | |
Mark Hammond | 91a681d | 2002-08-12 07:21:58 +0000 | [diff] [blame] | 41 | PyAPI_DATA(PyTypeObject) PyList_Type; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 42 | |
Thomas Wouters | 27d517b | 2007-02-25 20:39:11 +0000 | [diff] [blame] | 43 | #define PyList_Check(op) \ |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 44 | PyType_FastSubclass(Py_Type(op), Py_TPFLAGS_LIST_SUBCLASS) |
| 45 | #define PyList_CheckExact(op) (Py_Type(op) == &PyList_Type) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 46 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 47 | PyAPI_FUNC(PyObject *) PyList_New(Py_ssize_t size); |
| 48 | PyAPI_FUNC(Py_ssize_t) PyList_Size(PyObject *); |
| 49 | PyAPI_FUNC(PyObject *) PyList_GetItem(PyObject *, Py_ssize_t); |
| 50 | PyAPI_FUNC(int) PyList_SetItem(PyObject *, Py_ssize_t, PyObject *); |
| 51 | PyAPI_FUNC(int) PyList_Insert(PyObject *, Py_ssize_t, PyObject *); |
Mark Hammond | 91a681d | 2002-08-12 07:21:58 +0000 | [diff] [blame] | 52 | PyAPI_FUNC(int) PyList_Append(PyObject *, PyObject *); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 53 | PyAPI_FUNC(PyObject *) PyList_GetSlice(PyObject *, Py_ssize_t, Py_ssize_t); |
| 54 | PyAPI_FUNC(int) PyList_SetSlice(PyObject *, Py_ssize_t, Py_ssize_t, PyObject *); |
Mark Hammond | 91a681d | 2002-08-12 07:21:58 +0000 | [diff] [blame] | 55 | PyAPI_FUNC(int) PyList_Sort(PyObject *); |
| 56 | PyAPI_FUNC(int) PyList_Reverse(PyObject *); |
| 57 | PyAPI_FUNC(PyObject *) PyList_AsTuple(PyObject *); |
Raymond Hettinger | 8ca92ae | 2004-03-11 09:13:12 +0000 | [diff] [blame] | 58 | PyAPI_FUNC(PyObject *) _PyList_Extend(PyListObject *, PyObject *); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 59 | |
| 60 | /* Macro, trading safety for speed */ |
Barry Warsaw | 1f2bd07 | 1997-01-06 22:42:00 +0000 | [diff] [blame] | 61 | #define PyList_GET_ITEM(op, i) (((PyListObject *)(op))->ob_item[i]) |
Guido van Rossum | a937d14 | 1998-04-24 18:22:02 +0000 | [diff] [blame] | 62 | #define PyList_SET_ITEM(op, i, v) (((PyListObject *)(op))->ob_item[i] = (v)) |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 63 | #define PyList_GET_SIZE(op) Py_Size(op) |
Guido van Rossum | a330996 | 1993-07-28 09:05:47 +0000 | [diff] [blame] | 64 | |
| 65 | #ifdef __cplusplus |
| 66 | } |
| 67 | #endif |
| 68 | #endif /* !Py_LISTOBJECT_H */ |