Exemplo n.º 1
0
        static int NpyArray_IterSubscriptAssignSlice(NpyArrayIterObject self, NpyIndexSlice slice, NpyArray value)
        {
            NpyArray converted_value;
            npy_intp steps, start, step_size;
            bool     swap;
            NpyArray_CopySwapFunc copyswap;
            NpyArrayIterObject    value_iter = null;


            Npy_INCREF(self.ao.descr);
            converted_value = NpyArray_FromArray(value, self.ao.descr, 0);
            if (converted_value == null)
            {
                return(-1);
            }

            /* Copy in the data. */
            value_iter = NpyArray_IterNew(converted_value);
            if (value_iter == null)
            {
                Npy_DECREF(converted_value);
                return(-1);
            }

            if (value_iter.size > 0)
            {
                steps     = NpyArray_SliceSteps(slice);
                copyswap  = self.ao.descr.f.copyswap;
                start     = slice.start;
                step_size = slice.step;
                swap      = (NpyArray_ISNOTSWAPPED(self.ao) !=
                             NpyArray_ISNOTSWAPPED(converted_value));

                NpyArray_ITER_RESET(self);
                while (steps-- > 0)
                {
                    NpyArray_ITER_GOTO1D(self, start);

                    copyswap(self.dataptr, value_iter.dataptr, swap, self.ao);
                    NpyArray_ITER_NEXT(value_iter);
                    if (!NpyArray_ITER_NOTDONE(value_iter))
                    {
                        NpyArray_ITER_RESET(value_iter);
                    }
                    start += step_size;
                }
                NpyArray_ITER_RESET(self);
            }

            Npy_DECREF(value_iter);
            Npy_DECREF(converted_value);

            return(0);
        }
Exemplo n.º 2
0
 internal static npy_intp NpyArray_SliceSteps(NpyIndexSlice slice)
 {
     if ((slice.step < 0 && slice.stop >= slice.start) || (slice.step > 0 && slice.start >= slice.stop))
     {
         return(0);
     }
     else if (slice.step < 0)
     {
         return(((slice.stop - slice.start + 1) / slice.step) + 1);
     }
     else
     {
         return(((slice.stop - slice.start - 1) / slice.step) + 1);
     }
 }
Exemplo n.º 3
0
 internal static npy_intp NpyArray_SliceSteps(NpyIndexSlice slice)
 {
     return(numpyinternal.NpyArray_SliceSteps(slice));
 }
Exemplo n.º 4
0
 public NpyIndex()
 {
     slice        = new NpyIndexSlice();
     slice_nostop = new NpyIndexSliceNoStop();
 }
Exemplo n.º 5
0
        internal static int NpyArray_IndexToDimsEtc(NpyArray array, NpyIndex[] indexes, int n,
                                                    npy_intp[] dimensions, npy_intp[] strides, ref npy_intp offset_ptr, bool allow_arrays)
        {
            int      i;
            int      iDim   = 0;
            int      nd_new = 0;
            npy_intp offset = 0;

            for (i = 0; i < n; i++)
            {
                switch (indexes[i].type)
                {
                case NpyIndexType.NPY_INDEX_INTP:
                    if (iDim >= array.nd)
                    {
                        NpyErr_SetString(npyexc_type.NpyExc_IndexError,
                                         "too many indices");
                        return(-1);
                    }
                    offset += array.strides[iDim] * indexes[i].intp;
                    iDim++;
                    break;

                case NpyIndexType.NPY_INDEX_SLICE:
                {
                    NpyIndexSlice slice = indexes[i].slice;

                    if (iDim >= array.nd)
                    {
                        NpyErr_SetString(npyexc_type.NpyExc_IndexError,
                                         "too many indices");
                        return(-1);
                    }
                    dimensions[nd_new] = NpyArray_SliceSteps(slice);
                    strides[nd_new]    = slice.step * array.strides[iDim];
                    offset            += array.strides[iDim] * slice.start;
                    iDim++;
                    nd_new++;
                }
                break;

                case NpyIndexType.NPY_INDEX_INTP_ARRAY:
                    if (allow_arrays)
                    {
                        /* Treat arrays as a 0 index to get the subspace. */
                        if (iDim >= array.nd)
                        {
                            NpyErr_SetString(npyexc_type.NpyExc_IndexError,
                                             "too many indices");
                            return(-1);
                        }
                        iDim++;
                        break;
                    }
                    else
                    {
                        NpyErr_SetString(npyexc_type.NpyExc_IndexError,
                                         "Array indices are not allowed.");
                        return(-1);
                    }

                case NpyIndexType.NPY_INDEX_NEWAXIS:
                    dimensions[nd_new] = 1;
                    strides[nd_new]    = 0;
                    nd_new++;
                    break;

                case NpyIndexType.NPY_INDEX_SLICE_NOSTOP:
                case NpyIndexType.NPY_INDEX_BOOL_ARRAY:
                case NpyIndexType.NPY_INDEX_ELLIPSIS:
                    NpyErr_SetString(npyexc_type.NpyExc_IndexError,
                                     "Index is not bound to an array.");
                    return(-1);

                case NpyIndexType.NPY_INDEX_STRING:
                    NpyErr_SetString(npyexc_type.NpyExc_IndexError,
                                     "String indices not allowed.");
                    return(-1);

                default:
                    Debug.Assert(false);
                    NpyErr_SetString(npyexc_type.NpyExc_IndexError,
                                     "Illegal index type.");
                    return(-1);
                }
            }

            /* Add full slices for the rest of the array indices. */
            for (; iDim < array.nd; iDim++)
            {
                dimensions[nd_new] = array.dimensions[iDim];
                strides[nd_new]    = array.strides[iDim];
                nd_new++;
            }

            offset_ptr = offset;
            return(nd_new);
        }
Exemplo n.º 6
0
        internal static NpyArray NpyArray_IterSubscriptSlice(NpyArrayIterObject self, NpyIndexSlice slice)
        {
            NpyArray result;

            npy_intp[] steps = new npy_intp[1] {
                0
            };
            npy_intp start, step_size;
            int      elsize;
            bool     swap;
            VoidPtr  dptr;
            NpyArray_CopySwapFunc copyswap;


            /* Build the result. */
            steps[0] = NpyArray_SliceSteps(slice);

            Npy_INCREF(self.ao.descr);
            result = NpyArray_Alloc(self.ao.descr, 1, steps,
                                    false, Npy_INTERFACE(self.ao));
            if (result == null)
            {
                return(result);
            }

            /* Copy in the data. */
            copyswap  = result.descr.f.copyswap;
            start     = slice.start;
            step_size = slice.step;
            elsize    = result.descr.elsize;
            swap      = (NpyArray_ISNOTSWAPPED(self.ao) != NpyArray_ISNOTSWAPPED(result));
            dptr      = new VoidPtr(result);

            NpyArray_ITER_RESET(self);

            while (steps[0]-- > 0)
            {
                NpyArray_ITER_GOTO1D(self, start);

                copyswap(dptr, self.dataptr, swap, result);
                dptr.data_offset += elsize;
                start            += step_size;
            }
            NpyArray_ITER_RESET(self);

            return(result);
        }
Exemplo n.º 7
0
        internal static int NpyArray_IterSubscriptAssign(NpyArrayIterObject self, NpyIndex [] indexes, int n, NpyArray value)
        {
            NpyIndex index;

            if (n > 1)
            {
                NpyErr_SetString(npyexc_type.NpyExc_IndexError, "unsupported iterator index.");
                return(-1);
            }

            if (n == 0 || (n == 1 && indexes[0].type == NpyIndexType.NPY_INDEX_ELLIPSIS))
            {
                /* Assign to the whole iter using a slice. */
                NpyIndexSlice slice = new NpyIndexSlice();

                slice.start = 0;
                slice.stop  = self.size;
                slice.step  = 1;
                return(NpyArray_IterSubscriptAssignSlice(self, slice, value));
            }

            index = indexes[0];

            switch (index.type)
            {
            case NpyIndexType.NPY_INDEX_BOOL:
                if (index.boolean)
                {
                    return(NpyArray_IterSubscriptAssignIntp(self, 0, value));
                }
                else
                {
                    return(0);
                }

            case NpyIndexType.NPY_INDEX_INTP:
                return(NpyArray_IterSubscriptAssignIntp(self, index.intp, value));

            case NpyIndexType.NPY_INDEX_SLICE:
            case NpyIndexType.NPY_INDEX_SLICE_NOSTOP:
            {
                npy_intp[] new_size = new npy_intp[1] {
                    self.size
                };
                NpyIndex [] new_index = new NpyIndex[1] {
                    new NpyIndex()
                };

                /* Bind the slice. */
                if (NpyArray_IndexBind(indexes, 1,
                                       new_size, 1,
                                       new_index) < 0)
                {
                    return(-1);
                }
                Debug.Assert(new_index[0].type == NpyIndexType.NPY_INDEX_SLICE);

                self.size = new_size[0];
                return(NpyArray_IterSubscriptAssignSlice(self, new_index[0].slice, value));
            }

            case NpyIndexType.NPY_INDEX_BOOL_ARRAY:
                return(NpyArray_IterSubscriptAssignBoolArray(self,
                                                             index.bool_array,
                                                             value));

            case NpyIndexType.NPY_INDEX_INTP_ARRAY:
                return(NpyArray_IterSubscriptAssignIntpArray(self,
                                                             index.intp_array,
                                                             value));

            case NpyIndexType.NPY_INDEX_NEWAXIS:
            case NpyIndexType.NPY_INDEX_ELLIPSIS:
                NpyErr_SetString(npyexc_type.NpyExc_IndexError,
                                 "cannot use Ellipsis or newaxes here");
                return(-1);

            default:
                NpyErr_SetString(npyexc_type.NpyExc_IndexError, "unsupported iterator index");
                return(-1);
            }
        }