Commits

mattip  committed 3ec459c

do error checking, add app-level searchsort, which needs getitem().

  • Participants
  • Parent commits 1198649
  • Branches numpy-searchsorted

Comments (1)

Files changed (2)

File pypy/module/micronumpy/app_numpy.py

         arr[j] = i
         i += step
     return arr
+
+# How to call this from descr_searchsorted??
+def searchsort(space, 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.get_size()):
+        result[i] = func(self, v[i])
+    return result
+

File pypy/module/micronumpy/ndarray.py

         loop.round(space, self, calc_dtype, self.get_shape(), decimals, out)
         return out
 
-    def descr_searchsorted(self, space, w_v, w_side='left'):
-        raise OperationError(space.w_NotImplementedError, space.wrap(
-            "searchsorted not implemented yet"))
+    @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'))
+        if not side or len(side) < 1:
+            raise OperationError(space.w_ValueError, space.wrap(
+                "expected nonempty string for keyword 'side'"))
+        elif side[0] == 'l' or side[0] == 'L':
+            side = 'l'
+        elif side[0] == 'r' or side[0] == 'R':
+            side = 'r'
+        else:
+            raise oefmt(space.w_ValueError,
+                         "'%s' is an invalid value for keyword 'side'", side)
+        ret = W_NDimArray.from_shape(space, self.get_shape(),
+                       descriptor.get_dtype_cache(space).w_longdtype)
+        if len(self.get_shape()) > 1:
+            raise OperationError(space.w_ValueError, space.wrap(
+                        "a must be a 1-d array"))
+        v = convert_to_array(space, w_v)
+        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)
 
     def descr_setasflat(self, space, w_v):
         raise OperationError(space.w_NotImplementedError, space.wrap(
     dot = interp2app(W_NDimArray.descr_dot),
     var = interp2app(W_NDimArray.descr_var),
     std = interp2app(W_NDimArray.descr_std),
+    searchsorted = interp2app(W_NDimArray.descr_searchsorted),
 
     cumsum = interp2app(W_NDimArray.descr_cumsum),
     cumprod = interp2app(W_NDimArray.descr_cumprod),