[pypy-commit] pypy numpy-searchsorted: implement as applevel function

mattip noreply at buildbot.pypy.org
Sat Apr 19 23:53:30 CEST 2014


Author: Matti Picus <matti.picus at gmail.com>
Branch: numpy-searchsorted
Changeset: r70786:2bc5f1fb800d
Date: 2014-04-19 23:55 +0300
http://bitbucket.org/pypy/pypy/changeset/2bc5f1fb800d/

Log:	implement as applevel function

diff --git a/pypy/module/micronumpy/app_numpy.py b/pypy/module/micronumpy/app_numpy.py
--- a/pypy/module/micronumpy/app_numpy.py
+++ b/pypy/module/micronumpy/app_numpy.py
@@ -24,7 +24,7 @@
     return arr
 
 # How to call this from descr_searchsorted??
-def searchsort(space, arr, v, side, result):
+def searchsort(arr, v, side, result):
     def left_find_index(a, val):
         imin = 0
         imax = a.size
@@ -50,6 +50,6 @@
     else:
         func = right_find_index
     for i in range(v.get_size()):
-        result[i] = func(self, v[i])
+        result[i] = func(arr, v[i])
     return result
 
diff --git a/pypy/module/micronumpy/ndarray.py b/pypy/module/micronumpy/ndarray.py
--- a/pypy/module/micronumpy/ndarray.py
+++ b/pypy/module/micronumpy/ndarray.py
@@ -695,7 +695,6 @@
 
     @unwrap_spec(side=str, w_sorter=WrappedDefault(None))
     def descr_searchsorted(self, space, w_v, side='left', w_sorter=None):
-        from pypy.module.micronumpy.sort import searchsort
         if not space.is_none(w_sorter):
             raise OperationError(space.w_NotImplementedError, space.wrap(
                 'sorter not supported in searchsort'))
@@ -718,7 +717,8 @@
         if len(v.get_shape()) >1:
             raise OperationError(space.w_ValueError, space.wrap(
                         "v must be a 1-d array-like"))
-        return searchsort(self, space, v, side, ret)
+        app_searchsort(space, self, v, space.wrap(side), ret)
+        return ret
 
     def descr_setasflat(self, space, w_v):
         raise OperationError(space.w_NotImplementedError, space.wrap(
@@ -1271,6 +1271,37 @@
         return res
 """, filename=__file__).interphook('ptp')
 
+app_searchsort = applevel(r"""
+    def searchsort(arr, v, side, result):
+        def left_find_index(a, val):
+            imin = 0
+            imax = a.size
+            while imin < imax:
+                imid = imin + ((imax - imin) >> 1)
+                if a[imid] <= val:
+                    imin = imid +1
+                else:
+                    imax = imid
+            return imin
+        def right_find_index(a, val):
+            imin = 0
+            imax = a.size
+            while imin < imax:
+                imid = imin + ((imax - imin) >> 1)
+                if a[imid] < val:
+                    imin = imid +1
+                else:
+                    imax = imid
+            return imin
+        if side == 'l':
+            func = left_find_index
+        else:
+            func = right_find_index
+        for i in range(v.size):
+            result[i] = func(arr, v[i])
+        return result
+""", filename=__file__).interphook('searchsort')
+
 W_NDimArray.typedef = TypeDef("ndarray",
     __module__ = "numpy",
     __new__ = interp2app(descr_new_array),


More information about the pypy-commit mailing list