home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / py2s152.zip / Objects / rangeobject.c < prev    next >
C/C++ Source or Header  |  1999-06-27  |  6KB  |  266 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. /* Range object implementation */
  33.  
  34. #include "Python.h"
  35.  
  36. typedef struct {
  37.     PyObject_HEAD
  38.     long    start;
  39.     long    step;
  40.     long    len;
  41.     int    reps;
  42. } rangeobject;
  43.  
  44.  
  45. PyObject *
  46. PyRange_New(start, len, step, reps)
  47.     long start, len, step;
  48.     int reps;
  49. {
  50.     rangeobject *obj = PyObject_NEW(rangeobject, &PyRange_Type);
  51.  
  52.     obj->start = start;
  53.     obj->len   = len;
  54.     obj->step  = step;
  55.     obj->reps  = reps;
  56.  
  57.     return (PyObject *) obj;
  58. }
  59.  
  60. static void
  61. range_dealloc(r)
  62.     rangeobject *r;
  63. {
  64.     PyMem_DEL(r);
  65. }
  66.  
  67. static PyObject *
  68. range_item(r, i)
  69.     rangeobject *r;
  70.     int i;
  71. {
  72.     if (i < 0 || i >= r->len * r->reps) {
  73.         PyErr_SetString(PyExc_IndexError,
  74.                 "xrange object index out of range");
  75.         return NULL;
  76.     }
  77.  
  78.     return PyInt_FromLong(r->start + (i % r->len) * r->step);
  79. }
  80.  
  81. static int
  82. range_length(r)
  83.     rangeobject *r;
  84. {
  85.     return r->len * r->reps;
  86. }
  87.  
  88. static int
  89. range_print(r, fp, flags)
  90.     rangeobject *r;
  91.     FILE *fp;
  92.     int flags;
  93. {
  94.     int i, j;
  95.  
  96.     fprintf(fp, "(");
  97.     for (i = 0; i < r->reps; ++i)
  98.         for (j = 0; j < r->len; ++j) {
  99.             if (j > 0 || i > 0)
  100.                 fprintf(fp, ", ");
  101.  
  102.             fprintf(fp, "%ld", r->start + j * r->step);
  103.         }
  104.  
  105.     if (r->len == 1 && r->reps == 1)
  106.         fprintf(fp, ",");
  107.     fprintf(fp, ")");
  108.     return 0;
  109. }
  110.  
  111. static PyObject *
  112. range_repr(r)
  113.     rangeobject *r;
  114. {
  115.     char buf[80];
  116.     sprintf(buf, "(xrange(%ld, %ld, %ld) * %d)",
  117.             r->start,
  118.             r->start + r->len * r->step,
  119.             r->step,
  120.             r->reps);
  121.     return PyString_FromString(buf);
  122. }
  123.  
  124. static PyObject *
  125. range_concat(r, obj)
  126.     rangeobject *r;
  127.     PyObject *obj;
  128. {
  129.     PyErr_SetString(PyExc_TypeError, "cannot concatenate xrange objects");
  130.     return NULL;
  131. }
  132.  
  133. static PyObject *
  134. range_repeat(r, n)
  135.     rangeobject *r;
  136.     int n;
  137. {
  138.     if (n < 0)
  139.         return (PyObject *) PyRange_New(0, 0, 1, 1);
  140.  
  141.     else if (n == 1) {
  142.         Py_INCREF(r);
  143.         return (PyObject *) r;
  144.     }
  145.  
  146.     else
  147.         return (PyObject *) PyRange_New(
  148.                         r->start,
  149.                         r->len,
  150.                         r->step,
  151.                         r->reps * n);
  152. }
  153.  
  154. static int
  155. range_compare(r1, r2)
  156.     rangeobject *r1, *r2;
  157. {
  158.     if (r1->start != r2->start)
  159.         return r1->start - r2->start;
  160.  
  161.     else if (r1->step != r2->step)
  162.         return r1->step - r2->step;
  163.  
  164.     else if (r1->len != r2->len)
  165.         return r1->len - r2->len;
  166.  
  167.     else
  168.         return r1->reps - r2->reps;
  169. }
  170.  
  171. static PyObject *
  172. range_slice(r, low, high)
  173.     rangeobject *r;
  174.     int low, high;
  175. {
  176.     if (r->reps != 1) {
  177.         PyErr_SetString(PyExc_TypeError,
  178.                 "cannot slice a replicated xrange");
  179.         return NULL;
  180.     }
  181.     if (low < 0)
  182.         low = 0;
  183.     else if (low > r->len)
  184.         low = r->len;
  185.     if (high < 0)
  186.         high = 0;
  187.     if (high < low)
  188.         high = low;
  189.     else if (high > r->len)
  190.         high = r->len;
  191.  
  192.     if (low == 0 && high == r->len) {
  193.         Py_INCREF(r);
  194.         return (PyObject *) r;
  195.     }
  196.  
  197.     return (PyObject *) PyRange_New(
  198.                 low * r->step + r->start,
  199.                 high - low,
  200.                 r->step,
  201.                 1);
  202. }
  203.  
  204. static PyObject *
  205. range_tolist(self, args)
  206. rangeobject *self;
  207. PyObject *args;
  208. {
  209.     PyObject *thelist;
  210.     int j;
  211.     int len = self->len * self->reps;
  212.  
  213.     if (! PyArg_Parse(args, ""))
  214.         return NULL;
  215.  
  216.     if ((thelist = PyList_New(len)) == NULL)
  217.         return NULL;
  218.  
  219.     for (j = 0; j < len; ++j)
  220.         if ((PyList_SetItem(thelist, j, (PyObject *) PyInt_FromLong(
  221.             self->start + (j % self->len) * self->step))) < 0)
  222.             return NULL;
  223.  
  224.     return thelist;
  225. }
  226.  
  227. static PyObject *
  228. range_getattr(r, name)
  229.     rangeobject *r;
  230.     char *name;
  231. {
  232.     static PyMethodDef range_methods[] = {
  233.         {"tolist",    (PyCFunction)range_tolist},
  234.         {NULL,        NULL}
  235.     };
  236.  
  237.     return Py_FindMethod(range_methods, (PyObject *) r, name);
  238. }
  239.  
  240. static PySequenceMethods range_as_sequence = {
  241.     (inquiry)range_length, /*sq_length*/
  242.     (binaryfunc)range_concat, /*sq_concat*/
  243.     (intargfunc)range_repeat, /*sq_repeat*/
  244.     (intargfunc)range_item, /*sq_item*/
  245.     (intintargfunc)range_slice, /*sq_slice*/
  246.     0,        /*sq_ass_item*/
  247.     0,        /*sq_ass_slice*/
  248. };
  249.  
  250. PyTypeObject PyRange_Type = {
  251.     PyObject_HEAD_INIT(&PyType_Type)
  252.     0,            /* Number of items for varobject */
  253.     "xrange",        /* Name of this type */
  254.     sizeof(rangeobject),    /* Basic object size */
  255.     0,            /* Item size for varobject */
  256.     (destructor)range_dealloc, /*tp_dealloc*/
  257.     (printfunc)range_print, /*tp_print*/
  258.     (getattrfunc)range_getattr, /*tp_getattr*/
  259.     0,            /*tp_setattr*/
  260.     (cmpfunc)range_compare, /*tp_compare*/
  261.     (reprfunc)range_repr, /*tp_repr*/
  262.     0,            /*tp_as_number*/
  263.     &range_as_sequence,    /*tp_as_sequence*/
  264.     0,            /*tp_as_mapping*/
  265. };
  266.