home *** CD-ROM | disk | FTP | other *** search
/ linuxmafia.com 2016 / linuxmafia.com.tar / linuxmafia.com / pub / palmos / pippy-0.6beta-src.tar.gz / pippy-0.6beta-src.tar / pippy-0.6beta-src / src / Objects / tupleobject.c < prev    next >
C/C++ Source or Header  |  2000-12-21  |  11KB  |  514 lines

  1. /***********************************************************
  2. Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
  3. The Netherlands.
  4.  
  5.                         All Rights Reserved
  6.  
  7. Permission to use, copy, modify, and distribute this software and its
  8. documentation for any purpose and without fee is hereby granted,
  9. provided that the above copyright notice appear in all copies and that
  10. both that copyright notice and this permission notice appear in
  11. supporting documentation, and that the names of Stichting Mathematisch
  12. Centrum or CWI or Corporation for National Research Initiatives or
  13. CNRI not be used in advertising or publicity pertaining to
  14. distribution of the software without specific, written prior
  15. permission.
  16.  
  17. While CWI is the initial source for this software, a modified version
  18. is made available by the Corporation for National Research Initiatives
  19. (CNRI) at the Internet address ftp://ftp.python.org.
  20.  
  21. STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
  22. REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
  23. MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
  24. CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
  25. DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
  26. PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  27. TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  28. PERFORMANCE OF THIS SOFTWARE.
  29.  
  30. ******************************************************************/
  31.  
  32. /* Tuple object implementation */
  33.  
  34. #include "Python.h"
  35. #include "other/tupleobject_c.h"
  36.  
  37. #ifndef MAXSAVESIZE
  38. #define MAXSAVESIZE    20
  39. #endif
  40.  
  41. #if MAXSAVESIZE > 0
  42. /* Entries 1 upto MAXSAVESIZE are free lists, entry 0 is the empty
  43.    tuple () of which at most one instance will be allocated.
  44. */
  45. static PyTupleObject *free_tuples[MAXSAVESIZE];
  46. #endif
  47. #ifdef COUNT_ALLOCS
  48. int fast_tuple_allocs;
  49. int tuple_zero_allocs;
  50. #endif
  51.  
  52. PyObject *
  53. PyTuple_New(size)
  54.     register int size;
  55. {
  56.     register int i;
  57.     register PyTupleObject *op;
  58.     if (size < 0) {
  59.         PyErr_BadInternalCall();
  60.         return NULL;
  61.     }
  62. #if MAXSAVESIZE > 0
  63.     if (size == 0 && free_tuples[0]) {
  64.         op = free_tuples[0];
  65.         Py_INCREF(op);
  66. #ifdef COUNT_ALLOCS
  67.         tuple_zero_allocs++;
  68. #endif
  69.         return (PyObject *) op;
  70.     }
  71.     if (0 < size && size < MAXSAVESIZE &&
  72.         (op = free_tuples[size]) != NULL)
  73.     {
  74.         free_tuples[size] = (PyTupleObject *) op->ob_item[0];
  75. #ifdef COUNT_ALLOCS
  76.         fast_tuple_allocs++;
  77. #endif
  78. #ifdef Py_TRACE_REFS
  79.         op->ob_type = &PyTuple_Type;
  80.         op->ob_size = size;
  81. #endif
  82.     }
  83.     else
  84. #endif
  85.     {
  86.         int nbytes = size * sizeof(PyObject *);
  87.         /* Check for overflow */
  88.         if (nbytes / sizeof(PyObject *) != (size_t)size ||
  89.             (nbytes += sizeof(PyTupleObject) - sizeof(PyObject *))
  90.             <= 0)
  91.         {
  92.             return PyErr_NoMemory();
  93.         }
  94.         ;
  95.         op = (PyTupleObject *) malloc(nbytes);
  96.         if (op == NULL)
  97.             return PyErr_NoMemory();
  98.  
  99.         op->ob_type = &PyTuple_Type;
  100.         op->ob_size = size;
  101.     }
  102.     for (i = 0; i < size; i++)
  103.         op->ob_item[i] = NULL;
  104.     _Py_NewReference((PyObject *)op);
  105. #if MAXSAVESIZE > 0
  106.     if (size == 0) {
  107.         free_tuples[0] = op;
  108.         Py_INCREF(op);    /* extra INCREF so that this is never freed */
  109.     }
  110. #endif
  111.     return (PyObject *) op;
  112. }
  113.  
  114. int
  115. PyTuple_Size(op)
  116.     register PyObject *op;
  117. {
  118.     if (!PyTuple_Check(op)) {
  119.         PyErr_BadInternalCall();
  120.         return -1;
  121.     }
  122.     else
  123.         return ((PyTupleObject *)op)->ob_size;
  124. }
  125.  
  126. PyObject *
  127. PyTuple_GetItem(op, i)
  128.     register PyObject *op;
  129.     register int i;
  130. {
  131.     if (!PyTuple_Check(op)) {
  132.         PyErr_BadInternalCall();
  133.         return NULL;
  134.     }
  135.     if (i < 0 || i >= ((PyTupleObject *)op) -> ob_size) {
  136.         PyErr_SetString(PyExc_IndexError, "tuple index out of range");
  137.         return NULL;
  138.     }
  139.     return ((PyTupleObject *)op) -> ob_item[i];
  140. }
  141.  
  142. int
  143. PyTuple_SetItem(op, i, newitem)
  144.     register PyObject *op;
  145.     register int i;
  146.     PyObject *newitem;
  147. {
  148.     register PyObject *olditem;
  149.     register PyObject **p;
  150.     if (!PyTuple_Check(op) || op->ob_refcnt != 1) {
  151.         Py_XDECREF(newitem);
  152.         PyErr_BadInternalCall();
  153.         return -1;
  154.     }
  155.     if (i < 0 || i >= ((PyTupleObject *)op) -> ob_size) {
  156.         Py_XDECREF(newitem);
  157.         PyErr_SetString(PyExc_IndexError,
  158.                 "tuple assignment index out of range");
  159.         return -1;
  160.     }
  161.     p = ((PyTupleObject *)op) -> ob_item + i;
  162.     olditem = *p;
  163.     *p = newitem;
  164.     Py_XDECREF(olditem);
  165.     return 0;
  166. }
  167.  
  168. /* Methods */
  169.  
  170. static void
  171. tupledealloc(op)
  172.     register PyTupleObject *op;
  173. {
  174.     register int i;
  175.  
  176.     if (op->ob_size > 0) {
  177.         i = op->ob_size;
  178.         while (--i >= 0)
  179.             Py_XDECREF(op->ob_item[i]);
  180. #if MAXSAVESIZE > 0
  181.         if (op->ob_size < MAXSAVESIZE) {
  182.             op->ob_item[0] = (PyObject *) free_tuples[op->ob_size];
  183.             free_tuples[op->ob_size] = op;
  184.             return;
  185.         }
  186. #endif
  187.     }
  188.     free((ANY *)op);
  189. }
  190.  
  191. static int
  192. tupleprint(op, fp, flags)
  193.     PyTupleObject *op;
  194.     FILE *fp;
  195.     int flags;
  196. {
  197.     int i;
  198.     fprintf(fp, "(");
  199.     for (i = 0; i < op->ob_size; i++) {
  200.         if (i > 0)
  201.             fprintf(fp, ", ");
  202.         if (PyObject_Print(op->ob_item[i], fp, 0) != 0)
  203.             return -1;
  204.     }
  205.     if (op->ob_size == 1)
  206.         fprintf(fp, ",");
  207.     fprintf(fp, ")");
  208.     return 0;
  209. }
  210.  
  211. static PyObject *
  212. tuplerepr(v)
  213.     PyTupleObject *v;
  214. {
  215.     PyObject *s, *comma;
  216.     int i;
  217.     s = PyString_FromString("(");
  218.     comma = PyString_FromString(", ");
  219.     for (i = 0; i < v->ob_size && s != NULL; i++) {
  220.         if (i > 0)
  221.             PyString_Concat(&s, comma);
  222.         PyString_ConcatAndDel(&s, PyObject_Repr(v->ob_item[i]));
  223.     }
  224.     Py_DECREF(comma);
  225.     if (v->ob_size == 1)
  226.         PyString_ConcatAndDel(&s, PyString_FromString(","));
  227.     PyString_ConcatAndDel(&s, PyString_FromString(")"));
  228.     return s;
  229. }
  230.  
  231. static int
  232. tuplecompare(v, w)
  233.     register PyTupleObject *v, *w;
  234. {
  235.     register int len =
  236.         (v->ob_size < w->ob_size) ? v->ob_size : w->ob_size;
  237.     register int i;
  238.     for (i = 0; i < len; i++) {
  239.         int cmp = PyObject_Compare(v->ob_item[i], w->ob_item[i]);
  240.         if (cmp != 0)
  241.             return cmp;
  242.     }
  243.     return v->ob_size - w->ob_size;
  244. }
  245.  
  246. static long
  247. tuplehash(v)
  248.     PyTupleObject *v;
  249. {
  250.     register long x, y;
  251.     register int len = v->ob_size;
  252.     register PyObject **p;
  253.     x = 0x345678L;
  254.     p = v->ob_item;
  255.     while (--len >= 0) {
  256.         y = PyObject_Hash(*p++);
  257.         if (y == -1)
  258.             return -1;
  259.         x = (1000003*x) ^ y;
  260.     }
  261.     x ^= v->ob_size;
  262.     if (x == -1)
  263.         x = -2;
  264.     return x;
  265. }
  266.  
  267. static int
  268. tuplelength(a)
  269.     PyTupleObject *a;
  270. {
  271.     return a->ob_size;
  272. }
  273.  
  274. static PyObject *
  275. tupleitem(a, i)
  276.     register PyTupleObject *a;
  277.     register int i;
  278. {
  279.     if (i < 0 || i >= a->ob_size) {
  280.         PyErr_SetString(PyExc_IndexError, "tuple index out of range");
  281.         return NULL;
  282.     }
  283.     Py_INCREF(a->ob_item[i]);
  284.     return a->ob_item[i];
  285. }
  286.  
  287. static PyObject *
  288. tupleslice(a, ilow, ihigh)
  289.     register PyTupleObject *a;
  290.     register int ilow, ihigh;
  291. {
  292.     register PyTupleObject *np;
  293.     register int i;
  294.     if (ilow < 0)
  295.         ilow = 0;
  296.     if (ihigh > a->ob_size)
  297.         ihigh = a->ob_size;
  298.     if (ihigh < ilow)
  299.         ihigh = ilow;
  300.     if (ilow == 0 && ihigh == a->ob_size) {
  301.         /* XXX can only do this if tuples are immutable! */
  302.         Py_INCREF(a);
  303.         return (PyObject *)a;
  304.     }
  305.     np = (PyTupleObject *)PyTuple_New(ihigh - ilow);
  306.     if (np == NULL)
  307.         return NULL;
  308.     for (i = ilow; i < ihigh; i++) {
  309.         PyObject *v = a->ob_item[i];
  310.         Py_INCREF(v);
  311.         np->ob_item[i - ilow] = v;
  312.     }
  313.     return (PyObject *)np;
  314. }
  315.  
  316. PyObject *
  317. PyTuple_GetSlice(op, i, j)
  318.     PyObject *op;
  319.     int i, j;
  320. {
  321.     if (op == NULL || !PyTuple_Check(op)) {
  322.         PyErr_BadInternalCall();
  323.         return NULL;
  324.     }
  325.     return tupleslice((PyTupleObject *)op, i, j);
  326. }
  327.  
  328. static PyObject *
  329. tupleconcat(a, bb)
  330.     register PyTupleObject *a;
  331.     register PyObject *bb;
  332. {
  333.     register int size;
  334.     register int i;
  335.     PyTupleObject *np;
  336.     if (!PyTuple_Check(bb)) {
  337.         PyErr_BadArgument();
  338.         return NULL;
  339.     }
  340. #define b ((PyTupleObject *)bb)
  341.     size = a->ob_size + b->ob_size;
  342.     np = (PyTupleObject *) PyTuple_New(size);
  343.     if (np == NULL) {
  344.         return NULL;
  345.     }
  346.     for (i = 0; i < a->ob_size; i++) {
  347.         PyObject *v = a->ob_item[i];
  348.         Py_INCREF(v);
  349.         np->ob_item[i] = v;
  350.     }
  351.     for (i = 0; i < b->ob_size; i++) {
  352.         PyObject *v = b->ob_item[i];
  353.         Py_INCREF(v);
  354.         np->ob_item[i + a->ob_size] = v;
  355.     }
  356.     return (PyObject *)np;
  357. #undef b
  358. }
  359.  
  360. static PyObject *
  361. tuplerepeat(a, n)
  362.     PyTupleObject *a;
  363.     int n;
  364. {
  365.     int i, j;
  366.     int size;
  367.     PyTupleObject *np;
  368.     PyObject **p;
  369.     if (n < 0)
  370.         n = 0;
  371.     if (a->ob_size == 0 || n == 1) {
  372.         /* Since tuples are immutable, we can return a shared
  373.            copy in this case */
  374.         Py_INCREF(a);
  375.         return (PyObject *)a;
  376.     }
  377.     size = a->ob_size * n;
  378.     if (size/a->ob_size != n)
  379.         return PyErr_NoMemory();
  380.     np = (PyTupleObject *) PyTuple_New(size);
  381.     if (np == NULL)
  382.         return NULL;
  383.     p = np->ob_item;
  384.     for (i = 0; i < n; i++) {
  385.         for (j = 0; j < a->ob_size; j++) {
  386.             *p = a->ob_item[j];
  387.             Py_INCREF(*p);
  388.             p++;
  389.         }
  390.     }
  391.     return (PyObject *) np;
  392. }
  393.  
  394. static PySequenceMethods tuple_as_sequence = {
  395.     (inquiry)tuplelength, /*sq_length*/
  396.     (binaryfunc)tupleconcat, /*sq_concat*/
  397.     (intargfunc)tuplerepeat, /*sq_repeat*/
  398.     (intargfunc)tupleitem, /*sq_item*/
  399.     (intintargfunc)tupleslice, /*sq_slice*/
  400.     0,        /*sq_ass_item*/
  401.     0,        /*sq_ass_slice*/
  402. };
  403.  
  404. PyTypeObject PyTuple_Type = {
  405.     PyObject_HEAD_INIT(&PyType_Type)
  406.     0,
  407.     "tuple",
  408.     sizeof(PyTupleObject) - sizeof(PyObject *),
  409.     sizeof(PyObject *),
  410.     (destructor)tupledealloc, /*tp_dealloc*/
  411.     (printfunc)tupleprint, /*tp_print*/
  412.     0,        /*tp_getattr*/
  413.     0,        /*tp_setattr*/
  414.     (cmpfunc)tuplecompare, /*tp_compare*/
  415.     (reprfunc)tuplerepr, /*tp_repr*/
  416.     0,        /*tp_as_number*/
  417.     &tuple_as_sequence,    /*tp_as_sequence*/
  418.     0,        /*tp_as_mapping*/
  419.     (hashfunc)tuplehash, /*tp_hash*/
  420. };
  421.  
  422. /* The following function breaks the notion that tuples are immutable:
  423.    it changes the size of a tuple.  We get away with this only if there
  424.    is only one module referencing the object.  You can also think of it
  425.    as creating a new tuple object and destroying the old one, only
  426.    more efficiently.  In any case, don't use this if the tuple may
  427.    already be known to some other part of the code...
  428.    If last_is_sticky is set, the tuple will grow or shrink at the
  429.    front, otherwise it will grow or shrink at the end. */
  430.  
  431. int
  432. _PyTuple_Resize(pv, newsize, last_is_sticky)
  433.     PyObject **pv;
  434.     int newsize;
  435.     int last_is_sticky;
  436. {
  437.     register PyTupleObject *v;
  438.     register PyTupleObject *sv;
  439.     int i;
  440.     int sizediff;
  441.  
  442.     v = (PyTupleObject *) *pv;
  443.     if (v == NULL || !PyTuple_Check(v) || v->ob_refcnt != 1) {
  444.         *pv = 0;
  445.         Py_DECREF(v);
  446.         PyErr_BadInternalCall();
  447.         return -1;
  448.     }
  449.     sizediff = newsize - v->ob_size;
  450.     if (sizediff == 0)
  451.         return 0;
  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.     if (last_is_sticky && sizediff < 0) {
  458.         /* shrinking:
  459.            move entries to the front and zero moved entries */
  460.         for (i = 0; i < newsize; i++) {
  461.             Py_XDECREF(v->ob_item[i]);
  462.             v->ob_item[i] = v->ob_item[i - sizediff];
  463.             v->ob_item[i - sizediff] = NULL;
  464.         }
  465.     }
  466.     for (i = newsize; i < v->ob_size; i++) {
  467.         Py_XDECREF(v->ob_item[i]);
  468.         v->ob_item[i] = NULL;
  469.     }
  470.     sv = (PyTupleObject *)
  471.         realloc((char *)v,
  472.             sizeof(PyTupleObject) + newsize * sizeof(PyObject *));
  473.     *pv = (PyObject *) sv;
  474.     if (sv == NULL) {
  475.         PyMem_DEL(v);
  476.         PyErr_NoMemory();
  477.         return -1;
  478.     }
  479.     _Py_NewReference((PyObject *)sv);
  480.     for (i = sv->ob_size; i < newsize; i++)
  481.         sv->ob_item[i] = NULL;
  482.     if (last_is_sticky && sizediff > 0) {
  483.         /* growing: move entries to the end and zero moved entries */
  484.         for (i = newsize - 1; i >= sizediff; i--) {
  485.             sv->ob_item[i] = sv->ob_item[i - sizediff];
  486.             sv->ob_item[i - sizediff] = NULL;
  487.         }
  488.     }
  489.     sv->ob_size = newsize;
  490.     return 0;
  491. }
  492.  
  493. void
  494. PyTuple_Fini()
  495. {
  496. #if MAXSAVESIZE > 0
  497.     int i;
  498.  
  499.     Py_XDECREF(free_tuples[0]);
  500.     free_tuples[0] = NULL;
  501.  
  502.     for (i = 1; i < MAXSAVESIZE; i++) {
  503.         PyTupleObject *p, *q;
  504.         p = free_tuples[i];
  505.         free_tuples[i] = NULL;
  506.         while (p) {
  507.             q = p;
  508.             p = (PyTupleObject *)(p->ob_item[0]);
  509.             PyMem_DEL(q);
  510.         }
  511.     }
  512. #endif
  513. }
  514.