home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 15 / AACD15.ISO / AACD / Programming / Python2 / Python20_source / Objects / tupleobject.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-10-25  |  10.9 KB  |  505 lines

  1.  
  2. /* Tuple object implementation */
  3.  
  4. #include "Python.h"
  5.  
  6. /* Speed optimization to avoid frequent malloc/free of small tuples */
  7. #ifndef MAXSAVESIZE
  8. #define MAXSAVESIZE    20  /* Largest tuple to save on free list */
  9. #endif
  10. #ifndef MAXSAVEDTUPLES 
  11. #define MAXSAVEDTUPLES  2000  /* Maximum number of tuples of each size to save */
  12. #endif
  13.  
  14. #if MAXSAVESIZE > 0
  15. /* Entries 1 up to MAXSAVESIZE are free lists, entry 0 is the empty
  16.    tuple () of which at most one instance will be allocated.
  17. */
  18. static PyTupleObject *free_tuples[MAXSAVESIZE];
  19. static int num_free_tuples[MAXSAVESIZE];
  20. #endif
  21. #ifdef COUNT_ALLOCS
  22. int fast_tuple_allocs;
  23. int tuple_zero_allocs;
  24. #endif
  25.  
  26. PyObject *
  27. PyTuple_New(register int size)
  28. {
  29.     register int i;
  30.     register PyTupleObject *op;
  31.     if (size < 0) {
  32.         PyErr_BadInternalCall();
  33.         return NULL;
  34.     }
  35. #if MAXSAVESIZE > 0
  36.     if (size == 0 && free_tuples[0]) {
  37.         op = free_tuples[0];
  38.         Py_INCREF(op);
  39. #ifdef COUNT_ALLOCS
  40.         tuple_zero_allocs++;
  41. #endif
  42.         return (PyObject *) op;
  43.     }
  44.     if (0 < size && size < MAXSAVESIZE &&
  45.         (op = free_tuples[size]) != NULL)
  46.     {
  47.         free_tuples[size] = (PyTupleObject *) op->ob_item[0];
  48.         num_free_tuples[size]--;
  49. #ifdef COUNT_ALLOCS
  50.         fast_tuple_allocs++;
  51. #endif
  52.         /* PyObject_InitVar is inlined */
  53. #ifdef Py_TRACE_REFS
  54.         op->ob_size = size;
  55.         op->ob_type = &PyTuple_Type;
  56. #endif
  57.         _Py_NewReference((PyObject *)op);
  58.     }
  59.     else
  60. #endif
  61.     {
  62.         int nbytes = size * sizeof(PyObject *);
  63.         /* Check for overflow */
  64.         if (nbytes / sizeof(PyObject *) != (size_t)size ||
  65.             (nbytes += sizeof(PyTupleObject) - sizeof(PyObject *)
  66.                      + PyGC_HEAD_SIZE)
  67.             <= 0)
  68.         {
  69.             return PyErr_NoMemory();
  70.         }
  71.         /* PyObject_NewVar is inlined */
  72.         op = (PyTupleObject *) PyObject_MALLOC(nbytes);
  73.         if (op == NULL)
  74.             return PyErr_NoMemory();
  75.         op = (PyTupleObject *) PyObject_FROM_GC(op);
  76.         PyObject_INIT_VAR(op, &PyTuple_Type, size);
  77.     }
  78.     for (i = 0; i < size; i++)
  79.         op->ob_item[i] = NULL;
  80. #if MAXSAVESIZE > 0
  81.     if (size == 0) {
  82.         free_tuples[0] = op;
  83.         ++num_free_tuples[0];
  84.         Py_INCREF(op);    /* extra INCREF so that this is never freed */
  85.     }
  86. #endif
  87.     PyObject_GC_Init(op);
  88.     return (PyObject *) op;
  89. }
  90.  
  91. int
  92. PyTuple_Size(register PyObject *op)
  93. {
  94.     if (!PyTuple_Check(op)) {
  95.         PyErr_BadInternalCall();
  96.         return -1;
  97.     }
  98.     else
  99.         return ((PyTupleObject *)op)->ob_size;
  100. }
  101.  
  102. PyObject *
  103. PyTuple_GetItem(register PyObject *op, register int i)
  104. {
  105.     if (!PyTuple_Check(op)) {
  106.         PyErr_BadInternalCall();
  107.         return NULL;
  108.     }
  109.     if (i < 0 || i >= ((PyTupleObject *)op) -> ob_size) {
  110.         PyErr_SetString(PyExc_IndexError, "tuple index out of range");
  111.         return NULL;
  112.     }
  113.     return ((PyTupleObject *)op) -> ob_item[i];
  114. }
  115.  
  116. int
  117. PyTuple_SetItem(register PyObject *op, register int i, PyObject *newitem)
  118. {
  119.     register PyObject *olditem;
  120.     register PyObject **p;
  121.     if (!PyTuple_Check(op) || op->ob_refcnt != 1) {
  122.         Py_XDECREF(newitem);
  123.         PyErr_BadInternalCall();
  124.         return -1;
  125.     }
  126.     if (i < 0 || i >= ((PyTupleObject *)op) -> ob_size) {
  127.         Py_XDECREF(newitem);
  128.         PyErr_SetString(PyExc_IndexError,
  129.                 "tuple assignment index out of range");
  130.         return -1;
  131.     }
  132.     p = ((PyTupleObject *)op) -> ob_item + i;
  133.     olditem = *p;
  134.     *p = newitem;
  135.     Py_XDECREF(olditem);
  136.     return 0;
  137. }
  138.  
  139. /* Methods */
  140.  
  141. static void
  142. tupledealloc(register PyTupleObject *op)
  143. {
  144.     register int i;
  145.     register int len =  op->ob_size;
  146.     Py_TRASHCAN_SAFE_BEGIN(op)
  147.     PyObject_GC_Fini(op);
  148.     if (len > 0) {
  149.         i = len;
  150.         while (--i >= 0)
  151.             Py_XDECREF(op->ob_item[i]);
  152. #if MAXSAVESIZE > 0
  153.         if (len < MAXSAVESIZE && num_free_tuples[len] < MAXSAVEDTUPLES) {
  154.             op->ob_item[0] = (PyObject *) free_tuples[len];
  155.             num_free_tuples[len]++;
  156.             free_tuples[len] = op;
  157.             goto done; /* return */
  158.         }
  159. #endif
  160.     }
  161.     op = (PyTupleObject *) PyObject_AS_GC(op);
  162.     PyObject_DEL(op);
  163. done:
  164.     Py_TRASHCAN_SAFE_END(op)
  165. }
  166.  
  167. static int
  168. tupleprint(PyTupleObject *op, FILE *fp, int flags)
  169. {
  170.     int i;
  171.     fprintf(fp, "(");
  172.     for (i = 0; i < op->ob_size; i++) {
  173.         if (i > 0)
  174.             fprintf(fp, ", ");
  175.         if (PyObject_Print(op->ob_item[i], fp, 0) != 0)
  176.             return -1;
  177.     }
  178.     if (op->ob_size == 1)
  179.         fprintf(fp, ",");
  180.     fprintf(fp, ")");
  181.     return 0;
  182. }
  183.  
  184. static PyObject *
  185. tuplerepr(PyTupleObject *v)
  186. {
  187.     PyObject *s, *comma;
  188.     int i;
  189.     s = PyString_FromString("(");
  190.     comma = PyString_FromString(", ");
  191.     for (i = 0; i < v->ob_size && s != NULL; i++) {
  192.         if (i > 0)
  193.             PyString_Concat(&s, comma);
  194.         PyString_ConcatAndDel(&s, PyObject_Repr(v->ob_item[i]));
  195.     }
  196.     Py_DECREF(comma);
  197.     if (v->ob_size == 1)
  198.         PyString_ConcatAndDel(&s, PyString_FromString(","));
  199.     PyString_ConcatAndDel(&s, PyString_FromString(")"));
  200.     return s;
  201. }
  202.  
  203. static int
  204. tuplecompare(register PyTupleObject *v, register PyTupleObject *w)
  205. {
  206.     register int len =
  207.         (v->ob_size < w->ob_size) ? v->ob_size : w->ob_size;
  208.     register int i;
  209.     for (i = 0; i < len; i++) {
  210.         int cmp = PyObject_Compare(v->ob_item[i], w->ob_item[i]);
  211.         if (cmp != 0)
  212.             return cmp;
  213.     }
  214.     return v->ob_size - w->ob_size;
  215. }
  216.  
  217. static long
  218. tuplehash(PyTupleObject *v)
  219. {
  220.     register long x, y;
  221.     register int len = v->ob_size;
  222.     register PyObject **p;
  223.     x = 0x345678L;
  224.     p = v->ob_item;
  225.     while (--len >= 0) {
  226.         y = PyObject_Hash(*p++);
  227.         if (y == -1)
  228.             return -1;
  229.         x = (1000003*x) ^ y;
  230.     }
  231.     x ^= v->ob_size;
  232.     if (x == -1)
  233.         x = -2;
  234.     return x;
  235. }
  236.  
  237. static int
  238. tuplelength(PyTupleObject *a)
  239. {
  240.     return a->ob_size;
  241. }
  242.  
  243. static int
  244. tuplecontains(PyTupleObject *a, PyObject *el)
  245. {
  246.     int i, cmp;
  247.  
  248.     for (i = 0; i < a->ob_size; ++i) {
  249.         cmp = PyObject_Compare(el, PyTuple_GET_ITEM(a, i));
  250.         if (cmp == 0)
  251.             return 1;
  252.         if (PyErr_Occurred())
  253.             return -1;
  254.     }
  255.     return 0;
  256. }
  257.  
  258. static PyObject *
  259. tupleitem(register PyTupleObject *a, register int i)
  260. {
  261.     if (i < 0 || i >= a->ob_size) {
  262.         PyErr_SetString(PyExc_IndexError, "tuple index out of range");
  263.         return NULL;
  264.     }
  265.     Py_INCREF(a->ob_item[i]);
  266.     return a->ob_item[i];
  267. }
  268.  
  269. static PyObject *
  270. tupleslice(register PyTupleObject *a, register int ilow, register int ihigh)
  271. {
  272.     register PyTupleObject *np;
  273.     register int i;
  274.     if (ilow < 0)
  275.         ilow = 0;
  276.     if (ihigh > a->ob_size)
  277.         ihigh = a->ob_size;
  278.     if (ihigh < ilow)
  279.         ihigh = ilow;
  280.     if (ilow == 0 && ihigh == a->ob_size) {
  281.         /* XXX can only do this if tuples are immutable! */
  282.         Py_INCREF(a);
  283.         return (PyObject *)a;
  284.     }
  285.     np = (PyTupleObject *)PyTuple_New(ihigh - ilow);
  286.     if (np == NULL)
  287.         return NULL;
  288.     for (i = ilow; i < ihigh; i++) {
  289.         PyObject *v = a->ob_item[i];
  290.         Py_INCREF(v);
  291.         np->ob_item[i - ilow] = v;
  292.     }
  293.     return (PyObject *)np;
  294. }
  295.  
  296. PyObject *
  297. PyTuple_GetSlice(PyObject *op, int i, int j)
  298. {
  299.     if (op == NULL || !PyTuple_Check(op)) {
  300.         PyErr_BadInternalCall();
  301.         return NULL;
  302.     }
  303.     return tupleslice((PyTupleObject *)op, i, j);
  304. }
  305.  
  306. static PyObject *
  307. tupleconcat(register PyTupleObject *a, register PyObject *bb)
  308. {
  309.     register int size;
  310.     register int i;
  311.     PyTupleObject *np;
  312.     if (!PyTuple_Check(bb)) {
  313.         PyErr_Format(PyExc_TypeError,
  314.                     "can only concatenate tuple (not \"%.200s\") to tuple",
  315.                  bb->ob_type->tp_name);
  316.         return NULL;
  317.     }
  318. #define b ((PyTupleObject *)bb)
  319.     size = a->ob_size + b->ob_size;
  320.     np = (PyTupleObject *) PyTuple_New(size);
  321.     if (np == NULL) {
  322.         return NULL;
  323.     }
  324.     for (i = 0; i < a->ob_size; i++) {
  325.         PyObject *v = a->ob_item[i];
  326.         Py_INCREF(v);
  327.         np->ob_item[i] = v;
  328.     }
  329.     for (i = 0; i < b->ob_size; i++) {
  330.         PyObject *v = b->ob_item[i];
  331.         Py_INCREF(v);
  332.         np->ob_item[i + a->ob_size] = v;
  333.     }
  334.     return (PyObject *)np;
  335. #undef b
  336. }
  337.  
  338. static PyObject *
  339. tuplerepeat(PyTupleObject *a, int n)
  340. {
  341.     int i, j;
  342.     int size;
  343.     PyTupleObject *np;
  344.     PyObject **p;
  345.     if (n < 0)
  346.         n = 0;
  347.     if (a->ob_size == 0 || n == 1) {
  348.         /* Since tuples are immutable, we can return a shared
  349.            copy in this case */
  350.         Py_INCREF(a);
  351.         return (PyObject *)a;
  352.     }
  353.     size = a->ob_size * n;
  354.     if (size/a->ob_size != n)
  355.         return PyErr_NoMemory();
  356.     np = (PyTupleObject *) PyTuple_New(size);
  357.     if (np == NULL)
  358.         return NULL;
  359.     p = np->ob_item;
  360.     for (i = 0; i < n; i++) {
  361.         for (j = 0; j < a->ob_size; j++) {
  362.             *p = a->ob_item[j];
  363.             Py_INCREF(*p);
  364.             p++;
  365.         }
  366.     }
  367.     return (PyObject *) np;
  368. }
  369.  
  370. static int
  371. tupletraverse(PyTupleObject *o, visitproc visit, void *arg)
  372. {
  373.     int i, err;
  374.     PyObject *x;
  375.  
  376.     for (i = o->ob_size; --i >= 0; ) {
  377.         x = o->ob_item[i];
  378.         if (x != NULL) {
  379.             err = visit(x, arg);
  380.             if (err)
  381.                 return err;
  382.         }
  383.     }
  384.     return 0;
  385. }
  386.  
  387. static PySequenceMethods tuple_as_sequence = {
  388.     (inquiry)tuplelength, /*sq_length*/
  389.     (binaryfunc)tupleconcat, /*sq_concat*/
  390.     (intargfunc)tuplerepeat, /*sq_repeat*/
  391.     (intargfunc)tupleitem, /*sq_item*/
  392.     (intintargfunc)tupleslice, /*sq_slice*/
  393.     0,        /*sq_ass_item*/
  394.     0,        /*sq_ass_slice*/
  395.     (objobjproc)tuplecontains, /*sq_contains*/
  396. };
  397.  
  398. PyTypeObject PyTuple_Type = {
  399.     PyObject_HEAD_INIT(&PyType_Type)
  400.     0,
  401.     "tuple",
  402.     sizeof(PyTupleObject) - sizeof(PyObject *) + PyGC_HEAD_SIZE,
  403.     sizeof(PyObject *),
  404.     (destructor)tupledealloc, /*tp_dealloc*/
  405.     (printfunc)tupleprint, /*tp_print*/
  406.     0,        /*tp_getattr*/
  407.     0,        /*tp_setattr*/
  408.     (cmpfunc)tuplecompare, /*tp_compare*/
  409.     (reprfunc)tuplerepr, /*tp_repr*/
  410.     0,        /*tp_as_number*/
  411.     &tuple_as_sequence,    /*tp_as_sequence*/
  412.     0,        /*tp_as_mapping*/
  413.     (hashfunc)tuplehash, /*tp_hash*/
  414.     0,        /*tp_call*/
  415.     0,        /*tp_str*/
  416.     0,        /*tp_getattro*/
  417.     0,        /*tp_setattro*/
  418.     0,        /*tp_as_buffer*/
  419.     Py_TPFLAGS_DEFAULT | Py_TPFLAGS_GC, /*tp_flags*/
  420.     0,              /*tp_doc*/
  421.      (traverseproc)tupletraverse,    /* tp_traverse */
  422. };
  423.  
  424. /* The following function breaks the notion that tuples are immutable:
  425.    it changes the size of a tuple.  We get away with this only if there
  426.    is only one module referencing the object.  You can also think of it
  427.    as creating a new tuple object and destroying the old one, only more
  428.    efficiently.  In any case, don't use this if the tuple may already be
  429.    known to some other part of the code.  The last_is_sticky is not used
  430.    and must always be false. */
  431.  
  432. int
  433. _PyTuple_Resize(PyObject **pv, int newsize, int last_is_sticky)
  434. {
  435.     register PyTupleObject *v;
  436.     register PyTupleObject *sv;
  437.     int i;
  438.     int sizediff;
  439.  
  440.     v = (PyTupleObject *) *pv;
  441.     if (v == NULL || !PyTuple_Check(v) || v->ob_refcnt != 1 ||
  442.              last_is_sticky) {
  443.         *pv = 0;
  444.         Py_XDECREF(v);
  445.         PyErr_BadInternalCall();
  446.         return -1;
  447.     }
  448.     sizediff = newsize - v->ob_size;
  449.     if (sizediff == 0)
  450.         return 0;
  451.  
  452.     /* XXX UNREF/NEWREF interface should be more symmetrical */
  453. #ifdef Py_REF_DEBUG
  454.     --_Py_RefTotal;
  455. #endif
  456.     _Py_ForgetReference((PyObject *) v);
  457.     for (i = newsize; i < v->ob_size; i++) {
  458.         Py_XDECREF(v->ob_item[i]);
  459.         v->ob_item[i] = NULL;
  460.     }
  461.     PyObject_GC_Fini(v);
  462.     v = (PyTupleObject *) PyObject_AS_GC(v);
  463.     sv = (PyTupleObject *) PyObject_REALLOC((char *)v,
  464.                         sizeof(PyTupleObject)
  465.                         + PyGC_HEAD_SIZE
  466.                         + newsize * sizeof(PyObject *));
  467.     if (sv == NULL) {
  468.         *pv = NULL;
  469.         PyObject_DEL(v);
  470.         PyErr_NoMemory();
  471.         return -1;
  472.     }
  473.     sv = (PyTupleObject *) PyObject_FROM_GC(sv);
  474.     _Py_NewReference((PyObject *) sv);
  475.     for (i = sv->ob_size; i < newsize; i++)
  476.         sv->ob_item[i] = NULL;
  477.     sv->ob_size = newsize;
  478.     *pv = (PyObject *) sv;
  479.     PyObject_GC_Init(sv);
  480.     return 0;
  481. }
  482.  
  483. void
  484. PyTuple_Fini(void)
  485. {
  486. #if MAXSAVESIZE > 0
  487.     int i;
  488.  
  489.     Py_XDECREF(free_tuples[0]);
  490.     free_tuples[0] = NULL;
  491.  
  492.     for (i = 1; i < MAXSAVESIZE; i++) {
  493.         PyTupleObject *p, *q;
  494.         p = free_tuples[i];
  495.         free_tuples[i] = NULL;
  496.         while (p) {
  497.             q = p;
  498.             p = (PyTupleObject *)(p->ob_item[0]);
  499.             q = (PyTupleObject *) PyObject_AS_GC(q);
  500.             PyObject_DEL(q);
  501.         }
  502.     }
  503. #endif
  504. }
  505.