blob: 74cf46f4f9b6c53ee6f2c93ab08296e71c237020 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002/* List object interface */
3
4/*
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005Another generally useful object type is an list of object pointers.
6This is a mutable type: the list items can be changed, and items can be
7added or removed. Out-of-range indices or non-list objects are ignored.
8
Guido van Rossumcaa63801995-01-12 11:45:45 +00009*** WARNING *** PyList_SetItem does not increment the new item's reference
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000010count, but does decrement the reference count of the item it replaces,
11if not nil. It does *decrement* the reference count if it is *not*
Guido van Rossumcaa63801995-01-12 11:45:45 +000012inserted in the list. Similarly, PyList_GetItem does not increment the
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000013returned item's reference count.
14*/
15
Fred Drakeea9cb5a2000-07-09 00:20:36 +000016#ifndef Py_LISTOBJECT_H
17#define Py_LISTOBJECT_H
18#ifdef __cplusplus
19extern "C" {
20#endif
21
Martin v. Löwis4d0d4712010-12-03 20:14:31 +000022#ifndef Py_LIMITED_API
Guido van Rossum3f5da241990-12-20 15:06:42 +000023typedef struct {
Fred Drakeea9cb5a2000-07-09 00:20:36 +000024 PyObject_VAR_HEAD
Neal Norwitz4ecd8cd2004-08-01 22:45:27 +000025 /* Vector of pointers to list elements. list[0] is ob_item[0], etc. */
Fred Drakeea9cb5a2000-07-09 00:20:36 +000026 PyObject **ob_item;
Tim Petersa995a2d2004-07-29 03:29:15 +000027
28 /* ob_item contains space for 'allocated' elements. The number
29 * currently in use is ob_size.
30 * Invariants:
31 * 0 <= ob_size <= allocated
32 * len(list) == ob_size
33 * ob_item == NULL implies ob_size == allocated == 0
Armin Rigo93677f02004-07-29 12:40:23 +000034 * list.sort() temporarily sets allocated to -1 to detect mutations.
Armin Rigo89a39462004-10-28 16:32:00 +000035 *
36 * Items must normally not be NULL, except during construction when
37 * the list is not yet visible outside the function that builds it.
Tim Petersa995a2d2004-07-29 03:29:15 +000038 */
Martin v. Löwis18e16552006-02-15 17:27:45 +000039 Py_ssize_t allocated;
Guido van Rossumcaa63801995-01-12 11:45:45 +000040} PyListObject;
Martin v. Löwis4d0d4712010-12-03 20:14:31 +000041#endif
Guido van Rossum3f5da241990-12-20 15:06:42 +000042
Mark Hammond91a681d2002-08-12 07:21:58 +000043PyAPI_DATA(PyTypeObject) PyList_Type;
Christian Heimesa22e8bd2007-11-29 22:35:39 +000044PyAPI_DATA(PyTypeObject) PyListIter_Type;
45PyAPI_DATA(PyTypeObject) PyListRevIter_Type;
46PyAPI_DATA(PyTypeObject) PySortWrapper_Type;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000047
Thomas Wouters27d517b2007-02-25 20:39:11 +000048#define PyList_Check(op) \
Benjamin Peterson1a2cf9a2014-05-26 15:12:28 -070049 PyType_FastSubclass(Py_TYPE(op), Py_TPFLAGS_LIST_SUBCLASS)
Christian Heimes90aa7642007-12-19 02:45:37 +000050#define PyList_CheckExact(op) (Py_TYPE(op) == &PyList_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000051
Martin v. Löwis18e16552006-02-15 17:27:45 +000052PyAPI_FUNC(PyObject *) PyList_New(Py_ssize_t size);
53PyAPI_FUNC(Py_ssize_t) PyList_Size(PyObject *);
54PyAPI_FUNC(PyObject *) PyList_GetItem(PyObject *, Py_ssize_t);
55PyAPI_FUNC(int) PyList_SetItem(PyObject *, Py_ssize_t, PyObject *);
56PyAPI_FUNC(int) PyList_Insert(PyObject *, Py_ssize_t, PyObject *);
Mark Hammond91a681d2002-08-12 07:21:58 +000057PyAPI_FUNC(int) PyList_Append(PyObject *, PyObject *);
Martin v. Löwis18e16552006-02-15 17:27:45 +000058PyAPI_FUNC(PyObject *) PyList_GetSlice(PyObject *, Py_ssize_t, Py_ssize_t);
59PyAPI_FUNC(int) PyList_SetSlice(PyObject *, Py_ssize_t, Py_ssize_t, PyObject *);
Mark Hammond91a681d2002-08-12 07:21:58 +000060PyAPI_FUNC(int) PyList_Sort(PyObject *);
61PyAPI_FUNC(int) PyList_Reverse(PyObject *);
62PyAPI_FUNC(PyObject *) PyList_AsTuple(PyObject *);
Martin v. Löwis4d0d4712010-12-03 20:14:31 +000063#ifndef Py_LIMITED_API
Raymond Hettinger8ca92ae2004-03-11 09:13:12 +000064PyAPI_FUNC(PyObject *) _PyList_Extend(PyListObject *, PyObject *);
Antoine Pitrou9a812cb2011-11-15 00:00:12 +010065
66PyAPI_FUNC(int) PyList_ClearFreeList(void);
David Malcolm49526f42012-06-22 14:55:41 -040067PyAPI_FUNC(void) _PyList_DebugMallocStats(FILE *out);
Martin v. Löwis4d0d4712010-12-03 20:14:31 +000068#endif
Guido van Rossum3f5da241990-12-20 15:06:42 +000069
70/* Macro, trading safety for speed */
Martin v. Löwis4d0d4712010-12-03 20:14:31 +000071#ifndef Py_LIMITED_API
Barry Warsaw1f2bd071997-01-06 22:42:00 +000072#define PyList_GET_ITEM(op, i) (((PyListObject *)(op))->ob_item[i])
Guido van Rossuma937d141998-04-24 18:22:02 +000073#define PyList_SET_ITEM(op, i, v) (((PyListObject *)(op))->ob_item[i] = (v))
Christian Heimes90aa7642007-12-19 02:45:37 +000074#define PyList_GET_SIZE(op) Py_SIZE(op)
Martin v. Löwis4d0d4712010-12-03 20:14:31 +000075#endif
Guido van Rossuma3309961993-07-28 09:05:47 +000076
77#ifdef __cplusplus
78}
79#endif
80#endif /* !Py_LISTOBJECT_H */