Same treatment as listobject.c:

- In count(), remove(), index(): call RichCompare(Py_EQ).

- Get rid of array_compare(), in favor of new array_richcompare() (a
  near clone of list_compare()).

- Aligned items in array_methods initializer and comments for type
  struct initializer.

- Folded a few long lines.
This commit is contained in:
Guido van Rossum 2001-01-18 01:02:55 +00:00
parent b932420cc7
commit 9d19cb8a70
1 changed files with 158 additions and 69 deletions

View File

@ -414,26 +414,90 @@ array_dealloc(arrayobject *op)
PyObject_Del(op); PyObject_Del(op);
} }
static int static PyObject *
array_compare(arrayobject *v, arrayobject *w) array_richcompare(PyObject *v, PyObject *w, int op)
{ {
int len = (v->ob_size < w->ob_size) ? v->ob_size : w->ob_size; arrayobject *va, *wa;
int i; PyObject *vi = NULL;
for (i = 0; i < len; i++) { PyObject *wi = NULL;
PyObject *ai, *bi; int i, k;
int cmp; PyObject *res;
ai = getarrayitem((PyObject *)v, i);
bi = getarrayitem((PyObject *)w, i); if (!is_arrayobject(v) || !is_arrayobject(w)) {
if (ai && bi) Py_INCREF(Py_NotImplemented);
cmp = PyObject_Compare(ai, bi); return Py_NotImplemented;
else
cmp = -1;
Py_XDECREF(ai);
Py_XDECREF(bi);
if (cmp != 0)
return cmp;
} }
return v->ob_size - w->ob_size;
va = (arrayobject *)v;
wa = (arrayobject *)w;
if (va->ob_size != wa->ob_size && (op == Py_EQ || op == Py_NE)) {
/* Shortcut: if the lengths differ, the arrays differ */
if (op == Py_EQ)
res = Py_False;
else
res = Py_True;
Py_INCREF(res);
return res;
}
/* Search for the first index where items are different */
k = 1;
for (i = 0; i < va->ob_size && i < wa->ob_size; i++) {
vi = getarrayitem(v, i);
wi = getarrayitem(w, i);
if (vi == NULL || wi == NULL) {
Py_XDECREF(vi);
Py_XDECREF(wi);
return NULL;
}
k = PyObject_RichCompareBool(vi, wi, Py_EQ);
if (k == 0)
break; /* Keeping vi and wi alive! */
Py_DECREF(vi);
Py_DECREF(wi);
if (k < 0)
return NULL;
}
if (k) {
/* No more items to compare -- compare sizes */
int vs = va->ob_size;
int ws = wa->ob_size;
int cmp;
switch (op) {
case Py_LT: cmp = vs < ws; break;
case Py_LE: cmp = ws <= ws; break;
case Py_EQ: cmp = vs == ws; break;
case Py_NE: cmp = vs != ws; break;
case Py_GT: cmp = vs > ws; break;
case Py_GE: cmp = vs >= ws; break;
default: return NULL; /* cannot happen */
}
if (cmp)
res = Py_True;
else
res = Py_False;
Py_INCREF(res);
return res;
}
/* We have an item that differs. First, shortcuts for EQ/NE */
if (op == Py_EQ) {
Py_INCREF(Py_False);
res = Py_False;
}
else if (op == Py_NE) {
Py_INCREF(Py_True);
res = Py_True;
}
else {
/* Compare the final item again using the proper operator */
res = PyObject_RichCompare(vi, wi, op);
}
Py_DECREF(vi);
Py_DECREF(wi);
return res;
} }
static int static int
@ -636,10 +700,11 @@ array_count(arrayobject *self, PyObject *args)
return NULL; return NULL;
for (i = 0; i < self->ob_size; i++) { for (i = 0; i < self->ob_size; i++) {
PyObject *selfi = getarrayitem((PyObject *)self, i); PyObject *selfi = getarrayitem((PyObject *)self, i);
if (PyObject_Compare(selfi, v) == 0) int cmp = PyObject_RichCompareBool(selfi, v, Py_EQ);
count++;
Py_DECREF(selfi); Py_DECREF(selfi);
if (PyErr_Occurred()) if (cmp > 0)
count++;
else if (cmp < 0)
return NULL; return NULL;
} }
return PyInt_FromLong((long)count); return PyInt_FromLong((long)count);
@ -660,12 +725,12 @@ array_index(arrayobject *self, PyObject *args)
return NULL; return NULL;
for (i = 0; i < self->ob_size; i++) { for (i = 0; i < self->ob_size; i++) {
PyObject *selfi = getarrayitem((PyObject *)self, i); PyObject *selfi = getarrayitem((PyObject *)self, i);
if (PyObject_Compare(selfi, v) == 0) { int cmp = PyObject_RichCompareBool(selfi, v, Py_EQ);
Py_DECREF(selfi); Py_DECREF(selfi);
if (cmp > 0) {
return PyInt_FromLong((long)i); return PyInt_FromLong((long)i);
} }
Py_DECREF(selfi); else if (cmp < 0)
if (PyErr_Occurred())
return NULL; return NULL;
} }
PyErr_SetString(PyExc_ValueError, "array.index(x): x not in list"); PyErr_SetString(PyExc_ValueError, "array.index(x): x not in list");
@ -687,16 +752,16 @@ array_remove(arrayobject *self, PyObject *args)
return NULL; return NULL;
for (i = 0; i < self->ob_size; i++) { for (i = 0; i < self->ob_size; i++) {
PyObject *selfi = getarrayitem((PyObject *)self,i); PyObject *selfi = getarrayitem((PyObject *)self,i);
if (PyObject_Compare(selfi, v) == 0) { int cmp = PyObject_RichCompareBool(selfi, v, Py_EQ);
Py_DECREF(selfi); Py_DECREF(selfi);
if (cmp > 0) {
if (array_ass_slice(self, i, i+1, if (array_ass_slice(self, i, i+1,
(PyObject *)NULL) != 0) (PyObject *)NULL) != 0)
return NULL; return NULL;
Py_INCREF(Py_None); Py_INCREF(Py_None);
return Py_None; return Py_None;
} }
Py_DECREF(selfi); else if (cmp < 0)
if (PyErr_Occurred())
return NULL; return NULL;
} }
PyErr_SetString(PyExc_ValueError, "array.remove(x): x not in list"); PyErr_SetString(PyExc_ValueError, "array.remove(x): x not in list");
@ -750,8 +815,8 @@ array_extend(arrayobject *self, PyObject *args)
if (!is_arrayobject(bb)) { if (!is_arrayobject(bb)) {
PyErr_Format(PyExc_TypeError, PyErr_Format(PyExc_TypeError,
"can only extend array with array (not \"%.200s\")", "can only extend array with array (not \"%.200s\")",
bb->ob_type->tp_name); bb->ob_type->tp_name);
return NULL; return NULL;
} }
#define b ((arrayobject *)bb) #define b ((arrayobject *)bb)
@ -1137,25 +1202,44 @@ Convert the array to an array of machine values and return the string\n\
representation."; representation.";
PyMethodDef array_methods[] = { PyMethodDef array_methods[] = {
{"append", (PyCFunction)array_append, METH_VARARGS, append_doc}, {"append", (PyCFunction)array_append, METH_VARARGS,
{"buffer_info", (PyCFunction)array_buffer_info, METH_VARARGS, buffer_info_doc}, append_doc},
{"byteswap", (PyCFunction)array_byteswap, METH_VARARGS, byteswap_doc}, {"buffer_info", (PyCFunction)array_buffer_info, METH_VARARGS,
{"count", (PyCFunction)array_count, METH_VARARGS, count_doc}, buffer_info_doc},
{"extend", (PyCFunction)array_extend, METH_VARARGS, extend_doc}, {"byteswap", (PyCFunction)array_byteswap, METH_VARARGS,
{"fromfile", (PyCFunction)array_fromfile, METH_VARARGS, fromfile_doc}, byteswap_doc},
{"fromlist", (PyCFunction)array_fromlist, METH_VARARGS, fromlist_doc}, {"count", (PyCFunction)array_count, METH_VARARGS,
{"fromstring", (PyCFunction)array_fromstring, METH_VARARGS, fromstring_doc}, count_doc},
{"index", (PyCFunction)array_index, METH_VARARGS, index_doc}, {"extend", (PyCFunction)array_extend, METH_VARARGS,
{"insert", (PyCFunction)array_insert, METH_VARARGS, insert_doc}, extend_doc},
{"pop", (PyCFunction)array_pop, METH_VARARGS, pop_doc}, {"fromfile", (PyCFunction)array_fromfile, METH_VARARGS,
{"read", (PyCFunction)array_fromfile, METH_VARARGS, fromfile_doc}, fromfile_doc},
{"remove", (PyCFunction)array_remove, METH_VARARGS, remove_doc}, {"fromlist", (PyCFunction)array_fromlist, METH_VARARGS,
{"reverse", (PyCFunction)array_reverse, METH_VARARGS, reverse_doc}, fromlist_doc},
/* {"sort", (PyCFunction)array_sort, METH_VARARGS, sort_doc},*/ {"fromstring", (PyCFunction)array_fromstring, METH_VARARGS,
{"tofile", (PyCFunction)array_tofile, METH_VARARGS, tofile_doc}, fromstring_doc},
{"tolist", (PyCFunction)array_tolist, METH_VARARGS, tolist_doc}, {"index", (PyCFunction)array_index, METH_VARARGS,
{"tostring", (PyCFunction)array_tostring, METH_VARARGS, tostring_doc}, index_doc},
{"write", (PyCFunction)array_tofile, METH_VARARGS, tofile_doc}, {"insert", (PyCFunction)array_insert, METH_VARARGS,
insert_doc},
{"pop", (PyCFunction)array_pop, METH_VARARGS,
pop_doc},
{"read", (PyCFunction)array_fromfile, METH_VARARGS,
fromfile_doc},
{"remove", (PyCFunction)array_remove, METH_VARARGS,
remove_doc},
{"reverse", (PyCFunction)array_reverse, METH_VARARGS,
reverse_doc},
/* {"sort", (PyCFunction)array_sort, METH_VARARGS,
sort_doc},*/
{"tofile", (PyCFunction)array_tofile, METH_VARARGS,
tofile_doc},
{"tolist", (PyCFunction)array_tolist, METH_VARARGS,
tolist_doc},
{"tostring", (PyCFunction)array_tostring, METH_VARARGS,
tostring_doc},
{"write", (PyCFunction)array_tofile, METH_VARARGS,
tofile_doc},
{NULL, NULL} /* sentinel */ {NULL, NULL} /* sentinel */
}; };
@ -1345,9 +1429,11 @@ a_array(PyObject *self, PyObject *args)
} }
} }
if (initial != NULL && PyString_Check(initial)) { if (initial != NULL && PyString_Check(initial)) {
PyObject *t_initial = Py_BuildValue("(O)", initial); PyObject *t_initial = Py_BuildValue("(O)",
initial);
PyObject *v = PyObject *v =
array_fromstring((arrayobject *)a, t_initial); array_fromstring((arrayobject *)a,
t_initial);
Py_DECREF(t_initial); Py_DECREF(t_initial);
if (v == NULL) { if (v == NULL) {
Py_DECREF(a); Py_DECREF(a);
@ -1442,23 +1528,26 @@ statichere PyTypeObject Arraytype = {
"array", "array",
sizeof(arrayobject), sizeof(arrayobject),
0, 0,
(destructor)array_dealloc, /*tp_dealloc*/ (destructor)array_dealloc, /* tp_dealloc */
(printfunc)array_print, /*tp_print*/ (printfunc)array_print, /* tp_print */
(getattrfunc)array_getattr, /*tp_getattr*/ (getattrfunc)array_getattr, /* tp_getattr */
0, /*tp_setattr*/ 0, /* tp_setattr */
(cmpfunc)array_compare, /*tp_compare*/ 0, /* tp_compare */
(reprfunc)array_repr, /*tp_repr*/ (reprfunc)array_repr, /* tp_repr */
0, /*tp_as_number*/ 0, /* tp_as _number*/
&array_as_sequence, /*tp_as_sequence*/ &array_as_sequence, /* tp_as _sequence*/
0, /*tp_as_mapping*/ 0, /* tp_as _mapping*/
0, /*tp_hash*/ 0, /* tp_hash */
0, /*tp_call*/ 0, /* tp_call */
0, /*tp_str*/ 0, /* tp_str */
0, /*tp_getattro*/ 0, /* tp_getattro */
0, /*tp_setattro*/ 0, /* tp_setattro */
&array_as_buffer, /*tp_as_buffer*/ &array_as_buffer, /* tp_as _buffer*/
0, /*tp_xxx4*/ 0, /* tp_flags */
arraytype_doc, /*tp_doc*/ arraytype_doc, /* tp_doc */
0, /* tp_traverse */
0, /* tp_clear */
array_richcompare, /* tp_richcompare */
}; };
DL_EXPORT(void) DL_EXPORT(void)