1993-10-26 14:58:25 -03:00
|
|
|
|
|
|
|
/* Range object implementation */
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
#include "Python.h"
|
2000-11-08 15:42:43 -04:00
|
|
|
#include "structmember.h"
|
|
|
|
#include <string.h>
|
1993-10-26 14:58:25 -03:00
|
|
|
|
|
|
|
typedef struct {
|
1997-05-02 00:12:38 -03:00
|
|
|
PyObject_HEAD
|
1993-10-26 14:58:25 -03:00
|
|
|
long start;
|
|
|
|
long step;
|
|
|
|
long len;
|
1993-12-21 18:50:31 -04:00
|
|
|
int reps;
|
1993-10-26 14:58:25 -03:00
|
|
|
} rangeobject;
|
|
|
|
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
PyObject *
|
2000-07-09 03:21:27 -03:00
|
|
|
PyRange_New(long start, long len, long step, int reps)
|
1993-10-26 14:58:25 -03:00
|
|
|
{
|
1997-05-02 00:12:38 -03:00
|
|
|
rangeobject *obj = PyObject_NEW(rangeobject, &PyRange_Type);
|
1993-10-26 14:58:25 -03:00
|
|
|
|
|
|
|
obj->start = start;
|
|
|
|
obj->len = len;
|
|
|
|
obj->step = step;
|
1993-12-21 18:50:31 -04:00
|
|
|
obj->reps = reps;
|
1993-10-26 14:58:25 -03:00
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
return (PyObject *) obj;
|
1993-10-26 14:58:25 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2000-07-09 03:21:27 -03:00
|
|
|
range_dealloc(rangeobject *r)
|
1993-10-26 14:58:25 -03:00
|
|
|
{
|
2000-05-03 20:44:39 -03:00
|
|
|
PyObject_DEL(r);
|
1993-10-26 14:58:25 -03:00
|
|
|
}
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
static PyObject *
|
2000-07-09 03:21:27 -03:00
|
|
|
range_item(rangeobject *r, int i)
|
1993-10-26 14:58:25 -03:00
|
|
|
{
|
1993-12-21 18:50:31 -04:00
|
|
|
if (i < 0 || i >= r->len * r->reps) {
|
1997-05-02 00:12:38 -03:00
|
|
|
PyErr_SetString(PyExc_IndexError,
|
1999-01-09 17:40:35 -04:00
|
|
|
"xrange object index out of range");
|
1993-10-26 14:58:25 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
return PyInt_FromLong(r->start + (i % r->len) * r->step);
|
1993-10-26 14:58:25 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2000-07-09 03:21:27 -03:00
|
|
|
range_length(rangeobject *r)
|
1993-10-26 14:58:25 -03:00
|
|
|
{
|
1993-12-21 18:50:31 -04:00
|
|
|
return r->len * r->reps;
|
|
|
|
}
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
static PyObject *
|
2000-07-09 03:21:27 -03:00
|
|
|
range_repr(rangeobject *r)
|
1993-10-26 14:58:25 -03:00
|
|
|
{
|
2000-08-04 00:05:40 -03:00
|
|
|
/* buffers must be big enough to hold 3 longs + an int +
|
|
|
|
* a bit of "(xrange(...) * ...)" text.
|
|
|
|
*/
|
|
|
|
char buf1[250];
|
|
|
|
char buf2[250];
|
2000-08-03 14:43:02 -03:00
|
|
|
|
2000-08-04 00:05:40 -03:00
|
|
|
if (r->start == 0 && r->step == 1)
|
2000-08-03 23:34:41 -03:00
|
|
|
sprintf(buf1, "xrange(%ld)", r->start + r->len * r->step);
|
2000-08-04 00:05:40 -03:00
|
|
|
|
|
|
|
else if (r->step == 1)
|
|
|
|
sprintf(buf1, "xrange(%ld, %ld)",
|
|
|
|
r->start,
|
|
|
|
r->start + r->len * r->step);
|
|
|
|
|
|
|
|
else
|
|
|
|
sprintf(buf1, "xrange(%ld, %ld, %ld)",
|
2000-08-03 23:34:41 -03:00
|
|
|
r->start,
|
|
|
|
r->start + r->len * r->step,
|
|
|
|
r->step);
|
2000-08-04 00:05:40 -03:00
|
|
|
|
|
|
|
if (r->reps != 1)
|
2000-08-03 23:34:41 -03:00
|
|
|
sprintf(buf2, "(%s * %d)", buf1, r->reps);
|
2000-08-04 00:05:40 -03:00
|
|
|
|
2000-08-03 14:43:02 -03:00
|
|
|
return PyString_FromString(r->reps == 1 ? buf1 : buf2);
|
1993-10-26 14:58:25 -03:00
|
|
|
}
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
static PyObject *
|
2000-07-09 03:21:27 -03:00
|
|
|
range_concat(rangeobject *r, PyObject *obj)
|
1993-12-21 18:50:31 -04:00
|
|
|
{
|
1999-01-09 17:40:35 -04:00
|
|
|
PyErr_SetString(PyExc_TypeError, "cannot concatenate xrange objects");
|
1993-12-21 18:50:31 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
static PyObject *
|
2000-07-09 03:21:27 -03:00
|
|
|
range_repeat(rangeobject *r, int n)
|
1993-12-21 18:50:31 -04:00
|
|
|
{
|
|
|
|
if (n < 0)
|
1997-05-02 00:12:38 -03:00
|
|
|
return (PyObject *) PyRange_New(0, 0, 1, 1);
|
1993-12-21 18:50:31 -04:00
|
|
|
|
|
|
|
else if (n == 1) {
|
1997-05-02 00:12:38 -03:00
|
|
|
Py_INCREF(r);
|
|
|
|
return (PyObject *) r;
|
1993-12-21 18:50:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
else
|
1997-05-02 00:12:38 -03:00
|
|
|
return (PyObject *) PyRange_New(
|
1993-12-21 18:50:31 -04:00
|
|
|
r->start,
|
|
|
|
r->len,
|
|
|
|
r->step,
|
|
|
|
r->reps * n);
|
|
|
|
}
|
|
|
|
|
1993-10-26 14:58:25 -03:00
|
|
|
static int
|
2000-07-09 03:21:27 -03:00
|
|
|
range_compare(rangeobject *r1, rangeobject *r2)
|
1993-10-26 14:58:25 -03:00
|
|
|
{
|
|
|
|
if (r1->start != r2->start)
|
|
|
|
return r1->start - r2->start;
|
|
|
|
|
|
|
|
else if (r1->step != r2->step)
|
|
|
|
return r1->step - r2->step;
|
|
|
|
|
|
|
|
else if (r1->len != r2->len)
|
|
|
|
return r1->len - r2->len;
|
1993-12-21 18:50:31 -04:00
|
|
|
|
|
|
|
else
|
|
|
|
return r1->reps - r2->reps;
|
1993-11-01 12:21:44 -04:00
|
|
|
}
|
1993-10-26 14:58:25 -03:00
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
static PyObject *
|
2000-07-09 03:21:27 -03:00
|
|
|
range_slice(rangeobject *r, int low, int high)
|
1993-11-01 12:21:44 -04:00
|
|
|
{
|
1993-12-21 18:50:31 -04:00
|
|
|
if (r->reps != 1) {
|
1997-05-02 00:12:38 -03:00
|
|
|
PyErr_SetString(PyExc_TypeError,
|
1999-01-09 17:40:35 -04:00
|
|
|
"cannot slice a replicated xrange");
|
1993-12-21 18:50:31 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (low < 0)
|
|
|
|
low = 0;
|
|
|
|
else if (low > r->len)
|
|
|
|
low = r->len;
|
|
|
|
if (high < 0)
|
|
|
|
high = 0;
|
|
|
|
if (high < low)
|
|
|
|
high = low;
|
|
|
|
else if (high > r->len)
|
|
|
|
high = r->len;
|
|
|
|
|
|
|
|
if (low == 0 && high == r->len) {
|
1997-05-02 00:12:38 -03:00
|
|
|
Py_INCREF(r);
|
|
|
|
return (PyObject *) r;
|
1993-12-21 18:50:31 -04:00
|
|
|
}
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
return (PyObject *) PyRange_New(
|
1993-12-21 18:50:31 -04:00
|
|
|
low * r->step + r->start,
|
|
|
|
high - low,
|
|
|
|
r->step,
|
|
|
|
1);
|
1993-10-26 14:58:25 -03:00
|
|
|
}
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
static PyObject *
|
2000-07-09 03:21:27 -03:00
|
|
|
range_tolist(rangeobject *self, PyObject *args)
|
1993-10-26 14:58:25 -03:00
|
|
|
{
|
1997-05-02 00:12:38 -03:00
|
|
|
PyObject *thelist;
|
1993-12-21 18:50:31 -04:00
|
|
|
int j;
|
|
|
|
int len = self->len * self->reps;
|
|
|
|
|
2000-08-03 14:43:02 -03:00
|
|
|
if (! PyArg_ParseTuple(args, ":tolist"))
|
1993-12-21 18:50:31 -04:00
|
|
|
return NULL;
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
if ((thelist = PyList_New(len)) == NULL)
|
1993-12-21 18:50:31 -04:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
for (j = 0; j < len; ++j)
|
1997-05-02 00:12:38 -03:00
|
|
|
if ((PyList_SetItem(thelist, j, (PyObject *) PyInt_FromLong(
|
|
|
|
self->start + (j % self->len) * self->step))) < 0)
|
1993-12-21 18:50:31 -04:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return thelist;
|
1993-11-01 12:21:44 -04:00
|
|
|
}
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
static PyObject *
|
2000-07-09 03:21:27 -03:00
|
|
|
range_getattr(rangeobject *r, char *name)
|
1993-11-01 12:21:44 -04:00
|
|
|
{
|
2000-11-08 15:42:43 -04:00
|
|
|
PyObject *result;
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
static PyMethodDef range_methods[] = {
|
2000-08-03 14:43:02 -03:00
|
|
|
{"tolist", (PyCFunction)range_tolist, METH_VARARGS,
|
|
|
|
"tolist() -> list\n"
|
|
|
|
"Return a list object with the same values."},
|
1993-12-21 18:50:31 -04:00
|
|
|
{NULL, NULL}
|
|
|
|
};
|
2000-11-08 15:42:43 -04:00
|
|
|
static struct memberlist range_members[] = {
|
|
|
|
{"step", T_LONG, offsetof(rangeobject, step), RO},
|
|
|
|
{"start", T_LONG, offsetof(rangeobject, start), RO},
|
|
|
|
{"stop", T_LONG, 0, RO},
|
|
|
|
{NULL, 0, 0, 0}
|
|
|
|
};
|
1993-12-21 18:50:31 -04:00
|
|
|
|
2000-11-08 15:42:43 -04:00
|
|
|
result = Py_FindMethod(range_methods, (PyObject *) r, name);
|
|
|
|
if (result == NULL) {
|
|
|
|
PyErr_Clear();
|
|
|
|
if (strcmp("stop", name) == 0)
|
|
|
|
result = PyInt_FromLong(r->start + (r->len * r->step));
|
|
|
|
else
|
|
|
|
result = PyMember_Get((char *)r, range_members, name);
|
|
|
|
}
|
|
|
|
return result;
|
1993-10-26 14:58:25 -03:00
|
|
|
}
|
|
|
|
|
2000-06-15 11:50:20 -03:00
|
|
|
static int
|
2000-07-09 03:21:27 -03:00
|
|
|
range_contains(rangeobject *r, PyObject *obj)
|
2000-06-15 11:50:20 -03:00
|
|
|
{
|
|
|
|
long num = PyInt_AsLong(obj);
|
2000-08-04 00:05:40 -03:00
|
|
|
|
2000-06-15 11:50:20 -03:00
|
|
|
if (num < 0 && PyErr_Occurred())
|
|
|
|
return -1;
|
2000-08-04 00:05:40 -03:00
|
|
|
|
2000-11-08 15:42:43 -04:00
|
|
|
if (r->step > 0) {
|
|
|
|
if ((num < r->start) || ((num - r->start) % r->step))
|
|
|
|
return 0;
|
|
|
|
if (num >= (r->start + (r->len * r->step)))
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if ((num > r->start) || ((num - r->start) % r->step))
|
|
|
|
return 0;
|
|
|
|
if (num <= (r->start + (r->len * r->step)))
|
|
|
|
return 0;
|
|
|
|
}
|
2000-06-15 11:50:20 -03:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
static PySequenceMethods range_as_sequence = {
|
2000-11-08 15:42:43 -04:00
|
|
|
(inquiry)range_length, /*sq_length*/
|
1994-09-28 12:51:32 -03:00
|
|
|
(binaryfunc)range_concat, /*sq_concat*/
|
|
|
|
(intargfunc)range_repeat, /*sq_repeat*/
|
|
|
|
(intargfunc)range_item, /*sq_item*/
|
|
|
|
(intintargfunc)range_slice, /*sq_slice*/
|
2000-11-08 15:42:43 -04:00
|
|
|
0, /*sq_ass_item*/
|
|
|
|
0, /*sq_ass_slice*/
|
2000-06-15 11:50:20 -03:00
|
|
|
(objobjproc)range_contains, /*sq_contains*/
|
1993-10-26 14:58:25 -03:00
|
|
|
};
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
PyTypeObject PyRange_Type = {
|
|
|
|
PyObject_HEAD_INIT(&PyType_Type)
|
1993-10-26 14:58:25 -03:00
|
|
|
0, /* Number of items for varobject */
|
1993-11-01 12:21:44 -04:00
|
|
|
"xrange", /* Name of this type */
|
1993-10-26 14:58:25 -03:00
|
|
|
sizeof(rangeobject), /* Basic object size */
|
|
|
|
0, /* Item size for varobject */
|
1994-09-28 12:51:32 -03:00
|
|
|
(destructor)range_dealloc, /*tp_dealloc*/
|
2000-08-03 14:43:02 -03:00
|
|
|
0, /*tp_print*/
|
1994-09-28 12:51:32 -03:00
|
|
|
(getattrfunc)range_getattr, /*tp_getattr*/
|
1993-10-26 14:58:25 -03:00
|
|
|
0, /*tp_setattr*/
|
1994-09-28 12:51:32 -03:00
|
|
|
(cmpfunc)range_compare, /*tp_compare*/
|
2000-08-03 14:43:02 -03:00
|
|
|
(reprfunc)range_repr, /*tp_repr*/
|
1993-10-26 14:58:25 -03:00
|
|
|
0, /*tp_as_number*/
|
|
|
|
&range_as_sequence, /*tp_as_sequence*/
|
|
|
|
0, /*tp_as_mapping*/
|
2000-06-15 11:50:20 -03:00
|
|
|
0, /*tp_hash*/
|
|
|
|
0, /*tp_call*/
|
|
|
|
0, /*tp_str*/
|
|
|
|
0, /*tp_getattro*/
|
|
|
|
0, /*tp_setattro*/
|
|
|
|
0, /*tp_as_buffer*/
|
|
|
|
Py_TPFLAGS_DEFAULT, /*tp_flags*/
|
1993-10-26 14:58:25 -03:00
|
|
|
};
|