home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C / Applications / Python 1.4 / Python 1.4 source / Objects / tupleobject.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-10-28  |  10.1 KB  |  466 lines  |  [TEXT/CWIE]

  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 "allobjects.h"
  35.  
  36. #ifndef MAXSAVESIZE
  37. #define MAXSAVESIZE    20
  38. #endif
  39.  
  40. #if MAXSAVESIZE > 0
  41. /* Entries 1 upto MAXSAVESIZE are free lists, entry 0 is the empty
  42.    tuple () of which at most one instance will be allocated.
  43. */
  44. static tupleobject *free_tuples[MAXSAVESIZE];
  45. #endif
  46. #ifdef COUNT_ALLOCS
  47. int fast_tuple_allocs;
  48. int tuple_zero_allocs;
  49. #endif
  50.  
  51. object *
  52. newtupleobject(size)
  53.     register int size;
  54. {
  55.     register int i;
  56.     register tupleobject *op;
  57.     if (size < 0) {
  58.         err_badcall();
  59.         return NULL;
  60.     }
  61. #if MAXSAVESIZE > 0
  62.     if (size == 0 && free_tuples[0]) {
  63.         op = free_tuples[0];
  64.         INCREF(op);
  65. #ifdef COUNT_ALLOCS
  66.         tuple_zero_allocs++;
  67. #endif
  68.         return (object *) op;
  69.     }
  70.     if (0 < size && size < MAXSAVESIZE && (op = free_tuples[size]) != NULL) {
  71.         free_tuples[size] = (tupleobject *) op->ob_item[0];
  72. #ifdef COUNT_ALLOCS
  73.         fast_tuple_allocs++;
  74. #endif
  75.     } else
  76. #endif
  77.     {
  78.         op = (tupleobject *)
  79.             malloc(sizeof(tupleobject) + size * sizeof(object *));
  80.         if (op == NULL)
  81.             return err_nomem();
  82.     }
  83.     op->ob_type = &Tupletype;
  84.     op->ob_size = size;
  85.     for (i = 0; i < size; i++)
  86.         op->ob_item[i] = NULL;
  87.     NEWREF(op);
  88. #if MAXSAVESIZE > 0
  89.     if (size == 0) {
  90.         free_tuples[0] = op;
  91.         INCREF(op);    /* extra INCREF so that this is never freed */
  92.     }
  93. #endif
  94.     return (object *) op;
  95. }
  96.  
  97. int
  98. gettuplesize(op)
  99.     register object *op;
  100. {
  101.     if (!is_tupleobject(op)) {
  102.         err_badcall();
  103.         return -1;
  104.     }
  105.     else
  106.         return ((tupleobject *)op)->ob_size;
  107. }
  108.  
  109. object *
  110. gettupleitem(op, i)
  111.     register object *op;
  112.     register int i;
  113. {
  114.     if (!is_tupleobject(op)) {
  115.         err_badcall();
  116.         return NULL;
  117.     }
  118.     if (i < 0 || i >= ((tupleobject *)op) -> ob_size) {
  119.         err_setstr(IndexError, "tuple index out of range");
  120.         return NULL;
  121.     }
  122.     return ((tupleobject *)op) -> ob_item[i];
  123. }
  124.  
  125. int
  126. settupleitem(op, i, newitem)
  127.     register object *op;
  128.     register int i;
  129.     object *newitem;
  130. {
  131.     register object *olditem;
  132.     register object **p;
  133.     if (!is_tupleobject(op)) {
  134.         XDECREF(newitem);
  135.         err_badcall();
  136.         return -1;
  137.     }
  138.     if (i < 0 || i >= ((tupleobject *)op) -> ob_size) {
  139.         XDECREF(newitem);
  140.         err_setstr(IndexError, "tuple assignment index out of range");
  141.         return -1;
  142.     }
  143.     p = ((tupleobject *)op) -> ob_item + i;
  144.     olditem = *p;
  145.     *p = newitem;
  146.     XDECREF(olditem);
  147.     return 0;
  148. }
  149.  
  150. /* Methods */
  151.  
  152. static void
  153. tupledealloc(op)
  154.     register tupleobject *op;
  155. {
  156.     register int i;
  157.     for (i = 0; i < op->ob_size; i++)
  158.         XDECREF(op->ob_item[i]);
  159. #if MAXSAVESIZE > 0
  160.     if (0 < op->ob_size && op->ob_size < MAXSAVESIZE) {
  161.         op->ob_item[0] = (object *) free_tuples[op->ob_size];
  162.         free_tuples[op->ob_size] = op;
  163.     } else
  164. #endif
  165.         free((ANY *)op);
  166. }
  167.  
  168. static int
  169. tupleprint(op, fp, flags)
  170.     tupleobject *op;
  171.     FILE *fp;
  172.     int flags;
  173. {
  174.     int i;
  175.     fprintf(fp, "(");
  176.     for (i = 0; i < op->ob_size; i++) {
  177.         if (i > 0)
  178.             fprintf(fp, ", ");
  179.         if (printobject(op->ob_item[i], fp, 0) != 0)
  180.             return -1;
  181.     }
  182.     if (op->ob_size == 1)
  183.         fprintf(fp, ",");
  184.     fprintf(fp, ")");
  185.     return 0;
  186. }
  187.  
  188. static object *
  189. tuplerepr(v)
  190.     tupleobject *v;
  191. {
  192.     object *s, *comma;
  193.     int i;
  194.     s = newstringobject("(");
  195.     comma = newstringobject(", ");
  196.     for (i = 0; i < v->ob_size && s != NULL; i++) {
  197.         if (i > 0)
  198.             joinstring(&s, comma);
  199.         joinstring_decref(&s, reprobject(v->ob_item[i]));
  200.     }
  201.     DECREF(comma);
  202.     if (v->ob_size == 1)
  203.         joinstring_decref(&s, newstringobject(","));
  204.     joinstring_decref(&s, newstringobject(")"));
  205.     return s;
  206. }
  207.  
  208. static int
  209. tuplecompare(v, w)
  210.     register tupleobject *v, *w;
  211. {
  212.     register int len =
  213.         (v->ob_size < w->ob_size) ? v->ob_size : w->ob_size;
  214.     register int i;
  215.     for (i = 0; i < len; i++) {
  216.         int cmp = cmpobject(v->ob_item[i], w->ob_item[i]);
  217.         if (cmp != 0)
  218.             return cmp;
  219.     }
  220.     return v->ob_size - w->ob_size;
  221. }
  222.  
  223. static long
  224. tuplehash(v)
  225.     tupleobject *v;
  226. {
  227.     register long x, y;
  228.     register int len = v->ob_size;
  229.     register object **p;
  230.     x = 0x345678L;
  231.     p = v->ob_item;
  232.     while (--len >= 0) {
  233.         y = hashobject(*p++);
  234.         if (y == -1)
  235.             return -1;
  236.         x = (x + x + x) ^ y;
  237.     }
  238.     x ^= v->ob_size;
  239.     if (x == -1)
  240.         x = -2;
  241.     return x;
  242. }
  243.  
  244. static int
  245. tuplelength(a)
  246.     tupleobject *a;
  247. {
  248.     return a->ob_size;
  249. }
  250.  
  251. static object *
  252. tupleitem(a, i)
  253.     register tupleobject *a;
  254.     register int i;
  255. {
  256.     if (i < 0 || i >= a->ob_size) {
  257.         err_setstr(IndexError, "tuple index out of range");
  258.         return NULL;
  259.     }
  260.     INCREF(a->ob_item[i]);
  261.     return a->ob_item[i];
  262. }
  263.  
  264. static object *
  265. tupleslice(a, ilow, ihigh)
  266.     register tupleobject *a;
  267.     register int ilow, ihigh;
  268. {
  269.     register tupleobject *np;
  270.     register int i;
  271.     if (ilow < 0)
  272.         ilow = 0;
  273.     if (ihigh > a->ob_size)
  274.         ihigh = a->ob_size;
  275.     if (ihigh < ilow)
  276.         ihigh = ilow;
  277.     if (ilow == 0 && ihigh == a->ob_size) {
  278.         /* XXX can only do this if tuples are immutable! */
  279.         INCREF(a);
  280.         return (object *)a;
  281.     }
  282.     np = (tupleobject *)newtupleobject(ihigh - ilow);
  283.     if (np == NULL)
  284.         return NULL;
  285.     for (i = ilow; i < ihigh; i++) {
  286.         object *v = a->ob_item[i];
  287.         INCREF(v);
  288.         np->ob_item[i - ilow] = v;
  289.     }
  290.     return (object *)np;
  291. }
  292.  
  293. object *
  294. gettupleslice(op, i, j)
  295.     object *op;
  296.     int i, j;
  297. {
  298.     if (op == NULL || !is_tupleobject(op)) {
  299.         err_badcall();
  300.         return NULL;
  301.     }
  302.     return tupleslice((tupleobject *)op, i, j);
  303. }
  304.  
  305. static object *
  306. tupleconcat(a, bb)
  307.     register tupleobject *a;
  308.     register object *bb;
  309. {
  310.     register int size;
  311.     register int i;
  312.     tupleobject *np;
  313.     if (!is_tupleobject(bb)) {
  314.         err_badarg();
  315.         return NULL;
  316.     }
  317. #define b ((tupleobject *)bb)
  318.     size = a->ob_size + b->ob_size;
  319.     np = (tupleobject *) newtupleobject(size);
  320.     if (np == NULL) {
  321.         return NULL;
  322.     }
  323.     for (i = 0; i < a->ob_size; i++) {
  324.         object *v = a->ob_item[i];
  325.         INCREF(v);
  326.         np->ob_item[i] = v;
  327.     }
  328.     for (i = 0; i < b->ob_size; i++) {
  329.         object *v = b->ob_item[i];
  330.         INCREF(v);
  331.         np->ob_item[i + a->ob_size] = v;
  332.     }
  333.     return (object *)np;
  334. #undef b
  335. }
  336.  
  337. static object *
  338. tuplerepeat(a, n)
  339.     tupleobject *a;
  340.     int n;
  341. {
  342.     int i, j;
  343.     int size;
  344.     tupleobject *np;
  345.     object **p;
  346.     if (n < 0)
  347.         n = 0;
  348.     if (a->ob_size*n == a->ob_size) {
  349.         /* Since tuples are immutable, we can return a shared
  350.            copy in this case */
  351.         INCREF(a);
  352.         return (object *)a;
  353.     }
  354.     size = a->ob_size * n;
  355.     np = (tupleobject *) newtupleobject(size);
  356.     if (np == NULL)
  357.         return NULL;
  358.     p = np->ob_item;
  359.     for (i = 0; i < n; i++) {
  360.         for (j = 0; j < a->ob_size; j++) {
  361.             *p = a->ob_item[j];
  362.             INCREF(*p);
  363.             p++;
  364.         }
  365.     }
  366.     return (object *) np;
  367. }
  368.  
  369. static sequence_methods tuple_as_sequence = {
  370.     (inquiry)tuplelength, /*sq_length*/
  371.     (binaryfunc)tupleconcat, /*sq_concat*/
  372.     (intargfunc)tuplerepeat, /*sq_repeat*/
  373.     (intargfunc)tupleitem, /*sq_item*/
  374.     (intintargfunc)tupleslice, /*sq_slice*/
  375.     0,        /*sq_ass_item*/
  376.     0,        /*sq_ass_slice*/
  377. };
  378.  
  379. typeobject Tupletype = {
  380.     OB_HEAD_INIT(&Typetype)
  381.     0,
  382.     "tuple",
  383.     sizeof(tupleobject) - sizeof(object *),
  384.     sizeof(object *),
  385.     (destructor)tupledealloc, /*tp_dealloc*/
  386.     (printfunc)tupleprint, /*tp_print*/
  387.     0,        /*tp_getattr*/
  388.     0,        /*tp_setattr*/
  389.     (cmpfunc)tuplecompare, /*tp_compare*/
  390.     (reprfunc)tuplerepr, /*tp_repr*/
  391.     0,        /*tp_as_number*/
  392.     &tuple_as_sequence,    /*tp_as_sequence*/
  393.     0,        /*tp_as_mapping*/
  394.     (hashfunc)tuplehash, /*tp_hash*/
  395. };
  396.  
  397. /* The following function breaks the notion that tuples are immutable:
  398.    it changes the size of a tuple.  We get away with this only if there
  399.    is only one module referencing the object.  You can also think of it
  400.    as creating a new tuple object and destroying the old one, only
  401.    more efficiently.  In any case, don't use this if the tuple may
  402.    already be known to some other part of the code...
  403.    If last_is_sticky is set, the tuple will grow or shrink at the
  404.    front, otherwise it will grow or shrink at the end. */
  405.  
  406. int
  407. resizetuple(pv, newsize, last_is_sticky)
  408.     object **pv;
  409.     int newsize;
  410.     int last_is_sticky;
  411. {
  412.     register tupleobject *v;
  413.     register tupleobject *sv;
  414.     int i;
  415.     int sizediff;
  416.  
  417.     v = (tupleobject *) *pv;
  418.     if (v == NULL || !is_tupleobject(v) || v->ob_refcnt != 1) {
  419.         *pv = 0;
  420.         DECREF(v);
  421.         err_badcall();
  422.         return -1;
  423.     }
  424.     sizediff = newsize - v->ob_size;
  425.     if (sizediff == 0)
  426.         return 0;
  427.     /* XXX UNREF/NEWREF interface should be more symmetrical */
  428. #ifdef Py_REF_DEBUG
  429.     --_Py_RefTotal;
  430. #endif
  431.     UNREF(v);
  432.     if (last_is_sticky && sizediff < 0) {
  433.         /* shrinking: move entries to the front and zero moved entries */
  434.         for (i = 0; i < newsize; i++) {
  435.             XDECREF(v->ob_item[i]);
  436.             v->ob_item[i] = v->ob_item[i - sizediff];
  437.             v->ob_item[i - sizediff] = NULL;
  438.         }
  439.     }
  440.     for (i = newsize; i < v->ob_size; i++) {
  441.         XDECREF(v->ob_item[i]);
  442.         v->ob_item[i] = NULL;
  443.     }
  444.     sv = (tupleobject *)
  445.         realloc((char *)v,
  446.             sizeof(tupleobject) + newsize * sizeof(object *));
  447.     *pv = (object *) sv;
  448.     if (sv == NULL) {
  449.         DEL(v);
  450.         err_nomem();
  451.         return -1;
  452.     }
  453.     NEWREF(sv);
  454.     for (i = sv->ob_size; i < newsize; i++)
  455.         sv->ob_item[i] = NULL;
  456.     if (last_is_sticky && sizediff > 0) {
  457.         /* growing: move entries to the end and zero moved entries */
  458.         for (i = newsize - 1; i >= sizediff; i--) {
  459.             sv->ob_item[i] = sv->ob_item[i - sizediff];
  460.             sv->ob_item[i - sizediff] = NULL;
  461.         }
  462.     }
  463.     sv->ob_size = newsize;
  464.     return 0;
  465. }
  466.