Ejemplo n.º 1
0
    def __getitem__(self, ind):
        if ind is Ellipsis:
            return self[:]

        if isinstance(ind, tuple):
            raise IndexError('unsupported iterator index')

        if isinstance(ind, bool):
            raise IndexError('unsupported iterator index')

        if numpy.isscalar(ind):
            ind = int(ind)
            base = self._base
            size = base.size
            indices = []
            for s in base.shape:
                size = size // s
                indices.append(ind // size)
                ind %= size
            return base[tuple(indices)].copy()

        if isinstance(ind, slice):
            base = self._base
            s = internal.complete_slice(ind, base.size)
            s_start = s.start
            s_step = s.step
            size = s.stop - s.start
            if s_step > 0:
                size = (size - 1) // s_step + 1
            else:
                size = (size + 1) // s_step + 1
            return _flatiter_getitem_slice(base, s_start, s_step, size=size)

        raise IndexError('unsupported iterator index')
Ejemplo n.º 2
0
    def __setitem__(self, ind, value):
        if ind is Ellipsis:
            self[:] = value
            return

        if isinstance(ind, tuple):
            raise IndexError('unsupported iterator index')

        if isinstance(ind, bool):
            raise IndexError('unsupported iterator index')

        if numpy.isscalar(ind):
            ind = int(ind)
            base = self._base
            size = base.size
            indices = []
            for s in base.shape:
                size = size // s
                indices.append(ind // size)
                ind %= size
            base[tuple(indices)] = value
            return

        if isinstance(ind, slice):
            base = self._base
            s = internal.complete_slice(ind, base.size)
            s_start = s.start
            s_step = s.step
            size = s.stop - s.start
            if s_step > 0:
                size = (size - 1) // s_step + 1
            else:
                size = (size + 1) // s_step + 1
            value = cupy.asarray(value, dtype=base.dtype)
            _flatiter_setitem_slice(value, s_start, s_step, base, size=size)
            return

        raise IndexError('unsupported iterator index')
Ejemplo n.º 3
0
 def test_invalid_stop_type(self):
     with self.assertRaises(TypeError):
         internal.complete_slice(slice((1, 2), 1, 1), 1)
     with self.assertRaises(TypeError):
         internal.complete_slice(slice((1, 2), 1, -1), 1)
Ejemplo n.º 4
0
 def test_invalid_step_value(self):
     with self.assertRaises(ValueError):
         internal.complete_slice(slice(1, 1, 0), 1)
Ejemplo n.º 5
0
 def test_complete_slice(self):
     assert internal.complete_slice(slice(*self.slice),
                                    10) == slice(*self.expect)