1993-10-27 01:58:25 +08:00
|
|
|
/***********************************************************
|
2000-07-01 07:50:40 +08:00
|
|
|
Copyright (c) 2000, BeOpen.com.
|
|
|
|
Copyright (c) 1995-2000, Corporation for National Research Initiatives.
|
|
|
|
Copyright (c) 1990-1995, Stichting Mathematisch Centrum.
|
|
|
|
All rights reserved.
|
1993-10-27 01:58:25 +08:00
|
|
|
|
2000-07-01 07:50:40 +08:00
|
|
|
See the file "Misc/COPYRIGHT" for information on usage and
|
|
|
|
redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
|
1993-10-27 01:58:25 +08:00
|
|
|
******************************************************************/
|
|
|
|
|
|
|
|
/* Range object implementation */
|
|
|
|
|
1997-05-02 11:12:38 +08:00
|
|
|
#include "Python.h"
|
1993-10-27 01:58:25 +08:00
|
|
|
|
|
|
|
typedef struct {
|
1997-05-02 11:12:38 +08:00
|
|
|
PyObject_HEAD
|
1993-10-27 01:58:25 +08:00
|
|
|
long start;
|
|
|
|
long step;
|
|
|
|
long len;
|
1993-12-22 06:50:31 +08:00
|
|
|
int reps;
|
1993-10-27 01:58:25 +08:00
|
|
|
} rangeobject;
|
|
|
|
|
|
|
|
|
1997-05-02 11:12:38 +08:00
|
|
|
PyObject *
|
2000-07-09 14:21:27 +08:00
|
|
|
PyRange_New(long start, long len, long step, int reps)
|
1993-10-27 01:58:25 +08:00
|
|
|
{
|
1997-05-02 11:12:38 +08:00
|
|
|
rangeobject *obj = PyObject_NEW(rangeobject, &PyRange_Type);
|
1993-10-27 01:58:25 +08:00
|
|
|
|
|
|
|
obj->start = start;
|
|
|
|
obj->len = len;
|
|
|
|
obj->step = step;
|
1993-12-22 06:50:31 +08:00
|
|
|
obj->reps = reps;
|
1993-10-27 01:58:25 +08:00
|
|
|
|
1997-05-02 11:12:38 +08:00
|
|
|
return (PyObject *) obj;
|
1993-10-27 01:58:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2000-07-09 14:21:27 +08:00
|
|
|
range_dealloc(rangeobject *r)
|
1993-10-27 01:58:25 +08:00
|
|
|
{
|
2000-05-04 07:44:39 +08:00
|
|
|
PyObject_DEL(r);
|
1993-10-27 01:58:25 +08:00
|
|
|
}
|
|
|
|
|
1997-05-02 11:12:38 +08:00
|
|
|
static PyObject *
|
2000-07-09 14:21:27 +08:00
|
|
|
range_item(rangeobject *r, int i)
|
1993-10-27 01:58:25 +08:00
|
|
|
{
|
1993-12-22 06:50:31 +08:00
|
|
|
if (i < 0 || i >= r->len * r->reps) {
|
1997-05-02 11:12:38 +08:00
|
|
|
PyErr_SetString(PyExc_IndexError,
|
1999-01-10 05:40:35 +08:00
|
|
|
"xrange object index out of range");
|
1993-10-27 01:58:25 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
1997-05-02 11:12:38 +08:00
|
|
|
return PyInt_FromLong(r->start + (i % r->len) * r->step);
|
1993-10-27 01:58:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2000-07-09 14:21:27 +08:00
|
|
|
range_length(rangeobject *r)
|
1993-10-27 01:58:25 +08:00
|
|
|
{
|
1993-12-22 06:50:31 +08:00
|
|
|
return r->len * r->reps;
|
|
|
|
}
|
|
|
|
|
1997-05-02 11:12:38 +08:00
|
|
|
static PyObject *
|
2000-07-09 14:21:27 +08:00
|
|
|
range_repr(rangeobject *r)
|
1993-10-27 01:58:25 +08:00
|
|
|
{
|
2000-08-04 11:05:40 +08: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-04 01:43:02 +08:00
|
|
|
|
2000-08-04 11:05:40 +08:00
|
|
|
if (r->start == 0 && r->step == 1)
|
2000-08-04 10:34:41 +08:00
|
|
|
sprintf(buf1, "xrange(%ld)", r->start + r->len * r->step);
|
2000-08-04 11:05:40 +08: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-04 10:34:41 +08:00
|
|
|
r->start,
|
|
|
|
r->start + r->len * r->step,
|
|
|
|
r->step);
|
2000-08-04 11:05:40 +08:00
|
|
|
|
|
|
|
if (r->reps != 1)
|
2000-08-04 10:34:41 +08:00
|
|
|
sprintf(buf2, "(%s * %d)", buf1, r->reps);
|
2000-08-04 11:05:40 +08:00
|
|
|
|
2000-08-04 01:43:02 +08:00
|
|
|
return PyString_FromString(r->reps == 1 ? buf1 : buf2);
|
1993-10-27 01:58:25 +08:00
|
|
|
}
|
|
|
|
|
1997-05-02 11:12:38 +08:00
|
|
|
static PyObject *
|
2000-07-09 14:21:27 +08:00
|
|
|
range_concat(rangeobject *r, PyObject *obj)
|
1993-12-22 06:50:31 +08:00
|
|
|
{
|
1999-01-10 05:40:35 +08:00
|
|
|
PyErr_SetString(PyExc_TypeError, "cannot concatenate xrange objects");
|
1993-12-22 06:50:31 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
1997-05-02 11:12:38 +08:00
|
|
|
static PyObject *
|
2000-07-09 14:21:27 +08:00
|
|
|
range_repeat(rangeobject *r, int n)
|
1993-12-22 06:50:31 +08:00
|
|
|
{
|
|
|
|
if (n < 0)
|
1997-05-02 11:12:38 +08:00
|
|
|
return (PyObject *) PyRange_New(0, 0, 1, 1);
|
1993-12-22 06:50:31 +08:00
|
|
|
|
|
|
|
else if (n == 1) {
|
1997-05-02 11:12:38 +08:00
|
|
|
Py_INCREF(r);
|
|
|
|
return (PyObject *) r;
|
1993-12-22 06:50:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
else
|
1997-05-02 11:12:38 +08:00
|
|
|
return (PyObject *) PyRange_New(
|
1993-12-22 06:50:31 +08:00
|
|
|
r->start,
|
|
|
|
r->len,
|
|
|
|
r->step,
|
|
|
|
r->reps * n);
|
|
|
|
}
|
|
|
|
|
1993-10-27 01:58:25 +08:00
|
|
|
static int
|
2000-07-09 14:21:27 +08:00
|
|
|
range_compare(rangeobject *r1, rangeobject *r2)
|
1993-10-27 01:58:25 +08: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-22 06:50:31 +08:00
|
|
|
|
|
|
|
else
|
|
|
|
return r1->reps - r2->reps;
|
1993-11-02 00:21:44 +08:00
|
|
|
}
|
1993-10-27 01:58:25 +08:00
|
|
|
|
1997-05-02 11:12:38 +08:00
|
|
|
static PyObject *
|
2000-07-09 14:21:27 +08:00
|
|
|
range_slice(rangeobject *r, int low, int high)
|
1993-11-02 00:21:44 +08:00
|
|
|
{
|
1993-12-22 06:50:31 +08:00
|
|
|
if (r->reps != 1) {
|
1997-05-02 11:12:38 +08:00
|
|
|
PyErr_SetString(PyExc_TypeError,
|
1999-01-10 05:40:35 +08:00
|
|
|
"cannot slice a replicated xrange");
|
1993-12-22 06:50:31 +08: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 11:12:38 +08:00
|
|
|
Py_INCREF(r);
|
|
|
|
return (PyObject *) r;
|
1993-12-22 06:50:31 +08:00
|
|
|
}
|
|
|
|
|
1997-05-02 11:12:38 +08:00
|
|
|
return (PyObject *) PyRange_New(
|
1993-12-22 06:50:31 +08:00
|
|
|
low * r->step + r->start,
|
|
|
|
high - low,
|
|
|
|
r->step,
|
|
|
|
1);
|
1993-10-27 01:58:25 +08:00
|
|
|
}
|
|
|
|
|
1997-05-02 11:12:38 +08:00
|
|
|
static PyObject *
|
2000-07-09 14:21:27 +08:00
|
|
|
range_tolist(rangeobject *self, PyObject *args)
|
1993-10-27 01:58:25 +08:00
|
|
|
{
|
1997-05-02 11:12:38 +08:00
|
|
|
PyObject *thelist;
|
1993-12-22 06:50:31 +08:00
|
|
|
int j;
|
|
|
|
int len = self->len * self->reps;
|
|
|
|
|
2000-08-04 01:43:02 +08:00
|
|
|
if (! PyArg_ParseTuple(args, ":tolist"))
|
1993-12-22 06:50:31 +08:00
|
|
|
return NULL;
|
|
|
|
|
1997-05-02 11:12:38 +08:00
|
|
|
if ((thelist = PyList_New(len)) == NULL)
|
1993-12-22 06:50:31 +08:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
for (j = 0; j < len; ++j)
|
1997-05-02 11:12:38 +08:00
|
|
|
if ((PyList_SetItem(thelist, j, (PyObject *) PyInt_FromLong(
|
|
|
|
self->start + (j % self->len) * self->step))) < 0)
|
1993-12-22 06:50:31 +08:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return thelist;
|
1993-11-02 00:21:44 +08:00
|
|
|
}
|
|
|
|
|
1997-05-02 11:12:38 +08:00
|
|
|
static PyObject *
|
2000-07-09 14:21:27 +08:00
|
|
|
range_getattr(rangeobject *r, char *name)
|
1993-11-02 00:21:44 +08:00
|
|
|
{
|
1997-05-02 11:12:38 +08:00
|
|
|
static PyMethodDef range_methods[] = {
|
2000-08-04 01:43:02 +08:00
|
|
|
{"tolist", (PyCFunction)range_tolist, METH_VARARGS,
|
|
|
|
"tolist() -> list\n"
|
|
|
|
"Return a list object with the same values."},
|
1993-12-22 06:50:31 +08:00
|
|
|
{NULL, NULL}
|
|
|
|
};
|
|
|
|
|
1997-05-02 11:12:38 +08:00
|
|
|
return Py_FindMethod(range_methods, (PyObject *) r, name);
|
1993-10-27 01:58:25 +08:00
|
|
|
}
|
|
|
|
|
2000-06-15 22:50:20 +08:00
|
|
|
static int
|
2000-07-09 14:21:27 +08:00
|
|
|
range_contains(rangeobject *r, PyObject *obj)
|
2000-06-15 22:50:20 +08:00
|
|
|
{
|
|
|
|
long num = PyInt_AsLong(obj);
|
2000-08-04 11:05:40 +08:00
|
|
|
|
2000-06-15 22:50:20 +08:00
|
|
|
if (num < 0 && PyErr_Occurred())
|
|
|
|
return -1;
|
2000-08-04 11:05:40 +08:00
|
|
|
|
2000-06-15 22:50:20 +08:00
|
|
|
if (num < r->start || (num - r->start) % r->step)
|
|
|
|
return 0;
|
|
|
|
if (num > (r->start + (r->len * r->step)))
|
|
|
|
return 0;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
1997-05-02 11:12:38 +08:00
|
|
|
static PySequenceMethods range_as_sequence = {
|
1994-09-28 23:51:32 +08:00
|
|
|
(inquiry)range_length, /*sq_length*/
|
|
|
|
(binaryfunc)range_concat, /*sq_concat*/
|
|
|
|
(intargfunc)range_repeat, /*sq_repeat*/
|
|
|
|
(intargfunc)range_item, /*sq_item*/
|
|
|
|
(intintargfunc)range_slice, /*sq_slice*/
|
1993-10-27 01:58:25 +08:00
|
|
|
0, /*sq_ass_item*/
|
|
|
|
0, /*sq_ass_slice*/
|
2000-06-15 22:50:20 +08:00
|
|
|
(objobjproc)range_contains, /*sq_contains*/
|
1993-10-27 01:58:25 +08:00
|
|
|
};
|
|
|
|
|
1997-05-02 11:12:38 +08:00
|
|
|
PyTypeObject PyRange_Type = {
|
|
|
|
PyObject_HEAD_INIT(&PyType_Type)
|
1993-10-27 01:58:25 +08:00
|
|
|
0, /* Number of items for varobject */
|
1993-11-02 00:21:44 +08:00
|
|
|
"xrange", /* Name of this type */
|
1993-10-27 01:58:25 +08:00
|
|
|
sizeof(rangeobject), /* Basic object size */
|
|
|
|
0, /* Item size for varobject */
|
1994-09-28 23:51:32 +08:00
|
|
|
(destructor)range_dealloc, /*tp_dealloc*/
|
2000-08-04 01:43:02 +08:00
|
|
|
0, /*tp_print*/
|
1994-09-28 23:51:32 +08:00
|
|
|
(getattrfunc)range_getattr, /*tp_getattr*/
|
1993-10-27 01:58:25 +08:00
|
|
|
0, /*tp_setattr*/
|
1994-09-28 23:51:32 +08:00
|
|
|
(cmpfunc)range_compare, /*tp_compare*/
|
2000-08-04 01:43:02 +08:00
|
|
|
(reprfunc)range_repr, /*tp_repr*/
|
1993-10-27 01:58:25 +08:00
|
|
|
0, /*tp_as_number*/
|
|
|
|
&range_as_sequence, /*tp_as_sequence*/
|
|
|
|
0, /*tp_as_mapping*/
|
2000-06-15 22:50:20 +08: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-27 01:58:25 +08:00
|
|
|
};
|