[Numpy-svn] r3202 - in trunk/numpy/core: code_generators include/numpy src
numpy-svn at scipy.org
numpy-svn at scipy.org
Thu Sep 21 03:58:45 EDT 2006
Author: charris
Date: 2006-09-21 02:58:41 -0500 (Thu, 21 Sep 2006)
New Revision: 3202
Modified:
trunk/numpy/core/code_generators/multiarray_api_order.txt
trunk/numpy/core/include/numpy/ndarrayobject.h
trunk/numpy/core/src/arraymethods.c
trunk/numpy/core/src/multiarraymodule.c
Log:
Make a PyArray_SearchsideConverter for the side keyword in searchsorted.
Rename some searchsorted variables with more descriptive names.
Do some documentation markup in case we ever run doxygen over the source.
Modified: trunk/numpy/core/code_generators/multiarray_api_order.txt
===================================================================
--- trunk/numpy/core/code_generators/multiarray_api_order.txt 2006-09-21 00:02:22 UTC (rev 3201)
+++ trunk/numpy/core/code_generators/multiarray_api_order.txt 2006-09-21 07:58:41 UTC (rev 3202)
@@ -79,4 +79,5 @@
_PyArray_SigintHandler
_PyArray_GetSigintBuf
PyArray_DescrAlignConverter
-PyArray_DescrAlignConverter2
\ No newline at end of file
+PyArray_DescrAlignConverter2
+PyArray_SearchsideConverter
Modified: trunk/numpy/core/include/numpy/ndarrayobject.h
===================================================================
--- trunk/numpy/core/include/numpy/ndarrayobject.h 2006-09-21 00:02:22 UTC (rev 3201)
+++ trunk/numpy/core/include/numpy/ndarrayobject.h 2006-09-21 07:58:41 UTC (rev 3202)
@@ -185,8 +185,8 @@
typedef enum {
NPY_SEARCHLEFT=0,
NPY_SEARCHRIGHT=1,
-} NPY_SEARCHKIND;
-#define NPY_NSEARCHKINDS NPY_SEARCHRIGHT + 1
+} NPY_SEARCHSIDE;
+#define NPY_NSEARCHSIDES NPY_SEARCHRIGHT + 1
typedef enum {
Modified: trunk/numpy/core/src/arraymethods.c
===================================================================
--- trunk/numpy/core/src/arraymethods.c 2006-09-21 00:02:22 UTC (rev 3201)
+++ trunk/numpy/core/src/arraymethods.c 2006-09-21 07:58:41 UTC (rev 3202)
@@ -887,29 +887,14 @@
array_searchsorted(PyArrayObject *self, PyObject *args, PyObject *kwds)
{
PyObject *keys;
- char *side = "left";
static char *kwlist[] = {"keys", "side", NULL};
- NPY_SEARCHKIND which;
+ NPY_SEARCHSIDE side = NPY_SEARCHLEFT;
- if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|s", kwlist, &keys, &side))
- return NULL;
- if (strlen(side) < 1) {
- PyErr_SetString(PyExc_ValueError,
- "Searchsorted: side must be nonempty string");
- return PY_FAIL;
- }
+ if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|O&", kwlist, &keys,
+ PyArray_SearchsideConverter, &side))
+ return NULL;
- if (side[0] == 'l' || side[0] == 'L')
- which = NPY_SEARCHLEFT;
- else if (side[0] == 'r' || side[0] == 'R')
- which = NPY_SEARCHRIGHT;
- else {
- PyErr_Format(PyExc_ValueError,
- "%s is not a valid value of side", side);
- return PY_FAIL;
- }
-
- return _ARET(PyArray_SearchSorted(self, keys, which));
+ return _ARET(PyArray_SearchSorted(self, keys, side));
}
static void
Modified: trunk/numpy/core/src/multiarraymodule.c
===================================================================
--- trunk/numpy/core/src/multiarraymodule.c 2006-09-21 00:02:22 UTC (rev 3201)
+++ trunk/numpy/core/src/multiarraymodule.c 2006-09-21 07:58:41 UTC (rev 3202)
@@ -2523,33 +2523,28 @@
}
-/* local_search_left
+/** @brief Use bisection of sorted array to find first entries >= keys.
*
- * Use bisection to find the indices i into ap1 s.t.
+ * For each key use bisection to find the first index i s.t. key <= arr[i].
+ * When there is no such index i, set i = len(arr). Return the results in ret.
+ * All arrays are assumed contiguous on entry and both arr and key must be of
+ * the same comparable type.
*
- * ap1[j] < key <= ap1[i] for all 0 <= j < i and all keys in ap2,
- *
- * When there is no such index i, set i = len(ap1). Return the results in ret. All
- * arrays are assumed contiguous on entry and both ap1 and ap2 are assumed to
- * be of the same comparable type.
- *
- * Arguments:
- *
- * ap1 -- array to be searched, contiguous on entry and assumed sorted.
- * ap2 -- array of keys, contiguous on entry.
- * ret -- return array of intp, contiguous on entry.
- *
+ * @param arr contiguous sorted array to be searched.
+ * @param key contiguous array of keys.
+ * @param ret contiguous array of intp for returned indices.
+ * @return void
*/
static void
-local_search_left(PyArrayObject *ap1, PyArrayObject *ap2, PyArrayObject *ret)
+local_search_left(PyArrayObject *arr, PyArrayObject *key, PyArrayObject *ret)
{
- PyArray_CompareFunc *compare = ap2->descr->f->compare;
- intp nelts = ap1->dimensions[ap1->nd - 1];
- intp nkeys = PyArray_SIZE(ap2);
- char *p1 = ap1->data;
- char *p2 = ap2->data;
- intp *pr = (intp *)ret->data;
- int elsize = ap1->descr->elsize;
+ PyArray_CompareFunc *compare = key->descr->f->compare;
+ intp nelts = arr->dimensions[arr->nd - 1];
+ intp nkeys = PyArray_SIZE(key);
+ char *parr = arr->data;
+ char *pkey = key->data;
+ intp *pret = (intp *)ret->data;
+ int elsize = arr->descr->elsize;
intp i;
for(i = 0; i < nkeys; ++i) {
@@ -2557,45 +2552,40 @@
intp imax = nelts;
while (imin < imax) {
intp imid = imin + ((imax - imin) >> 2);
- if (compare(p1 + elsize*imid, p2, ap2) < 0)
+ if (compare(parr + elsize*imid, pkey, key) < 0)
imin = imid + 1;
else
imax = imid;
}
- *pr = imin;
- pr += 1;
- p2 += elsize;
+ *pret = imin;
+ pret += 1;
+ pkey += elsize;
}
}
-/* local_search_right
+/** @brief Use bisection of sorted array to find first entries > keys.
*
- * Use bisection to find the indices i into ap1 s.t.
+ * For each key use bisection to find the first index i s.t. key < arr[i].
+ * When there is no such index i, set i = len(arr). Return the results in ret.
+ * All arrays are assumed contiguous on entry and both arr and key must be of
+ * the same comparable type.
*
- * ap1[j] <= key < ap1[i] for all 0 <= j < i and all keys in ap2.
- *
- * When there is no such index i, set i = len(ap1). Return the results in ret. All
- * arrays are assumed contiguous on entry and both ap1 and ap2 are assumed to
- * be of the same comparable type.
- *
- * Arguments:
- *
- * ap1 -- array to be searched, contiguous on entry and assumed sorted.
- * ap2 -- array of keys, contiguous on entry.
- * ret -- return array of intp, contiguous on entry.
- *
+ * @param arr contiguous sorted array to be searched.
+ * @param key contiguous array of keys.
+ * @param ret contiguous array of intp for returned indices.
+ * @return void
*/
static void
-local_search_right(PyArrayObject *ap1, PyArrayObject *ap2, PyArrayObject *ret)
+local_search_right(PyArrayObject *arr, PyArrayObject *key, PyArrayObject *ret)
{
- PyArray_CompareFunc *compare = ap2->descr->f->compare;
- intp nelts = ap1->dimensions[ap1->nd - 1];
- intp nkeys = PyArray_SIZE(ap2);
- char *p1 = ap1->data;
- char *p2 = ap2->data;
- intp *pr = (intp *)ret->data;
- int elsize = ap1->descr->elsize;
+ PyArray_CompareFunc *compare = key->descr->f->compare;
+ intp nelts = arr->dimensions[arr->nd - 1];
+ intp nkeys = PyArray_SIZE(key);
+ char *parr = arr->data;
+ char *pkey = key->data;
+ intp *pret = (intp *)ret->data;
+ int elsize = arr->descr->elsize;
intp i;
for(i = 0; i < nkeys; ++i) {
@@ -2603,22 +2593,52 @@
intp imax = nelts;
while (imin < imax) {
intp imid = imin + ((imax - imin) >> 2);
- if (compare(p1 + elsize*imid, p2, ap2) <= 0)
+ if (compare(parr + elsize*imid, pkey, key) <= 0)
imin = imid + 1;
else
imax = imid;
}
- *pr = imin;
- pr += 1;
- p2 += elsize;
+ *pret = imin;
+ pret += 1;
+ pkey += elsize;
}
}
+
/*MULTIARRAY_API
+ Convert object to searchsorted side
+*/
+static int
+PyArray_SearchsideConverter(PyObject *obj, NPY_SEARCHSIDE *side)
+{
+ char *str = PyString_AsString(obj);
+
+ if (!str)
+ return PY_FAIL;
+ if (strlen(str) < 1) {
+ PyErr_SetString(PyExc_ValueError,
+ "side must be nonempty string");
+ return PY_FAIL;
+ }
+
+ if (str[0] == 'l' || str[0] == 'L')
+ *side = NPY_SEARCHLEFT;
+ else if (str[0] == 'r' || str[0] == 'R')
+ *side = NPY_SEARCHRIGHT;
+ else {
+ PyErr_Format(PyExc_ValueError,
+ "side has invalid value '%s'", str);
+ return PY_FAIL;
+ }
+ return PY_SUCCEED;
+}
+
+
+/*MULTIARRAY_API
Numeric.searchsorted(a,v)
*/
static PyObject *
-PyArray_SearchSorted(PyArrayObject *op1, PyObject *op2, NPY_SEARCHKIND which)
+PyArray_SearchSorted(PyArrayObject *op1, PyObject *op2, NPY_SEARCHSIDE side)
{
PyArrayObject *ap1=NULL;
PyArrayObject *ap2=NULL;
@@ -2657,12 +2677,12 @@
goto fail;
}
- if (which == NPY_SEARCHLEFT) {
+ if (side == NPY_SEARCHLEFT) {
NPY_BEGIN_THREADS_DESCR(ap2->descr)
local_search_left(ap1, ap2, ret);
NPY_END_THREADS_DESCR(ap2->descr)
}
- else if (which == NPY_SEARCHRIGHT) {
+ else if (side == NPY_SEARCHRIGHT) {
NPY_BEGIN_THREADS_DESCR(ap2->descr)
local_search_right(ap1, ap2, ret);
NPY_END_THREADS_DESCR(ap2->descr)
More information about the Numpy-svn
mailing list