Esempio n. 1
0
        /// <summary>
        /// Finds the offset for a single item assignment to the array.
        /// </summary>
        /// <param name="arr">The array we are assigning to.</param>
        /// <returns>The offset or -1 if this is not a single assignment.</returns>
        internal npy_intp SingleAssignOffset(ndarray arr)
        {
            // Check to see that there are just newaxis, ellipsis, intp or bool indexes
            for (int i = 0; i < num_indexes; i++)
            {
                switch (IndexType(i))
                {
                case NpyIndexType.NPY_INDEX_NEWAXIS:
                case NpyIndexType.NPY_INDEX_ELLIPSIS:
                case NpyIndexType.NPY_INDEX_INTP:
                case NpyIndexType.NPY_INDEX_BOOL:
                    break;

                default:
                    return(-1);
                }
            }

            // Bind to the array and calculate the offset.
            NpyIndexes bound = Bind(arr);
            {
                npy_intp offset = 0;
                int      nd     = 0;

                for (int i = 0; i < bound.num_indexes; i++)
                {
                    switch (bound.IndexType(i))
                    {
                    case NpyIndexType.NPY_INDEX_NEWAXIS:
                        break;

                    case NpyIndexType.NPY_INDEX_INTP:
                        offset += arr.Stride(nd++) * bound.GetIntP(i);
                        break;

                    case NpyIndexType.NPY_INDEX_SLICE:
                        // An ellipsis became a slice on binding.
                        // This is not a single item assignment.
                        return(-1);

                    default:
                        // This should never happen
                        return(-1);
                    }
                }
                if (nd != arr.ndim)
                {
                    // Not enough indexes. This is not a single item.
                    return(-1);
                }
                return(offset);
            }
        }
Esempio n. 2
0
        public Object this[params object[] args]
        {
            get
            {
                ndarray result;

                NpyIndexes indexes = new NpyIndexes();
                {
                    NpyUtil_IndexProcessing.IndexConverter(this.arr.ravel(), args, indexes);
                    result = NpyCoreApi.IterSubscript(this, indexes);
                }
                if (result.ndim == 0)
                {
                    return(result.GetItem(0));
                }
                else
                {
                    return(result);
                }
            }

            set
            {
                NpyIndexes indexes = new NpyIndexes();
                {
                    NpyUtil_IndexProcessing.IndexConverter(this.arr.ravel(), args, indexes);

                    if (indexes.NumIndexes == 1)
                    {
                        // Special cases for single assigment.
                        switch (indexes.IndexType(0))
                        {
                        case NpyIndexType.NPY_INDEX_INTP:
                            SingleAssign(indexes.GetIntP(0), value);
                            return;

                        case NpyIndexType.NPY_INDEX_BOOL:
                            if (indexes.GetBool(0))
                            {
                                SingleAssign(0, value);
                            }
                            return;

                        default:
                            break;
                        }
                    }

                    ndarray array_val = np.FromAny(value, arr.Dtype, 0, 0, 0, null);
                    NpyCoreApi.IterSubscriptAssign(this, indexes, array_val);
                }
            }
        }