listobject.h 2.38 KB
Newer Older
1

Guido van Rossum's avatar
Guido van Rossum committed
2 3 4 5 6 7 8
/* List object interface */

/*
Another generally useful object type is an list of object pointers.
This is a mutable type: the list items can be changed, and items can be
added or removed.  Out-of-range indices or non-list objects are ignored.

9
*** WARNING *** PyList_SetItem does not increment the new item's reference
Guido van Rossum's avatar
Guido van Rossum committed
10 11
count, but does decrement the reference count of the item it replaces,
if not nil.  It does *decrement* the reference count if it is *not*
12
inserted in the list.  Similarly, PyList_GetItem does not increment the
Guido van Rossum's avatar
Guido van Rossum committed
13 14 15
returned item's reference count.
*/

16 17 18 19 20 21
#ifndef Py_LISTOBJECT_H
#define Py_LISTOBJECT_H
#ifdef __cplusplus
extern "C" {
#endif

Guido van Rossum's avatar
Guido van Rossum committed
22
typedef struct {
23
    PyObject_VAR_HEAD
Neal Norwitz's avatar
Neal Norwitz committed
24
    /* Vector of pointers to list elements.  list[0] is ob_item[0], etc. */
25
    PyObject **ob_item;
26 27 28 29 30 31 32

    /* ob_item contains space for 'allocated' elements.  The number
     * currently in use is ob_size.
     * Invariants:
     *     0 <= ob_size <= allocated
     *     len(list) == ob_size
     *     ob_item == NULL implies ob_size == allocated == 0
33
     * list.sort() temporarily sets allocated to -1 to detect mutations.
34 35 36
     *
     * Items must normally not be NULL, except during construction when
     * the list is not yet visible outside the function that builds it.
37
     */
38
    int allocated;
39
} PyListObject;
Guido van Rossum's avatar
Guido van Rossum committed
40

41
PyAPI_DATA(PyTypeObject) PyList_Type;
Guido van Rossum's avatar
Guido van Rossum committed
42

43
#define PyList_Check(op) PyObject_TypeCheck(op, &PyList_Type)
44
#define PyList_CheckExact(op) ((op)->ob_type == &PyList_Type)
Guido van Rossum's avatar
Guido van Rossum committed
45

46 47 48 49 50 51 52 53 54 55 56
PyAPI_FUNC(PyObject *) PyList_New(int size);
PyAPI_FUNC(int) PyList_Size(PyObject *);
PyAPI_FUNC(PyObject *) PyList_GetItem(PyObject *, int);
PyAPI_FUNC(int) PyList_SetItem(PyObject *, int, PyObject *);
PyAPI_FUNC(int) PyList_Insert(PyObject *, int, PyObject *);
PyAPI_FUNC(int) PyList_Append(PyObject *, PyObject *);
PyAPI_FUNC(PyObject *) PyList_GetSlice(PyObject *, int, int);
PyAPI_FUNC(int) PyList_SetSlice(PyObject *, int, int, PyObject *);
PyAPI_FUNC(int) PyList_Sort(PyObject *);
PyAPI_FUNC(int) PyList_Reverse(PyObject *);
PyAPI_FUNC(PyObject *) PyList_AsTuple(PyObject *);
57
PyAPI_FUNC(PyObject *) _PyList_Extend(PyListObject *, PyObject *);
Guido van Rossum's avatar
Guido van Rossum committed
58 59

/* Macro, trading safety for speed */
60
#define PyList_GET_ITEM(op, i) (((PyListObject *)(op))->ob_item[i])
61
#define PyList_SET_ITEM(op, i, v) (((PyListObject *)(op))->ob_item[i] = (v))
62
#define PyList_GET_SIZE(op)    (((PyListObject *)(op))->ob_size)
63 64 65 66 67

#ifdef __cplusplus
}
#endif
#endif /* !Py_LISTOBJECT_H */