internal static void SetIndex(Array a, object index, object value) { Type t = a.GetType(); Debug.Assert(t.HasElementType); Type elm = t.GetElementType(); Conversion conv; int iindex = Converter.TryConvertToInt32(index, out conv); if (conv != Conversion.None) { a.SetValue(Ops.ConvertTo(value, elm), Ops.FixIndex(iindex, a.Length) + a.GetLowerBound(0)); return; } Tuple ituple = index as Tuple; if (ituple != null && ituple.IsExpandable) { if (a.Rank != ituple.Count) { throw Ops.ValueError("bad dimensions for array, got {0} expected {1}", ituple.Count, a.Rank); } int[] indices = TupleToIndices(a, ituple); for (int i = 0; i < indices.Length; i++) { indices[i] += a.GetLowerBound(i); } a.SetValue(value, indices); return; } Slice slice = index as Slice; if (slice != null) { if (a.Rank != 1) { throw Ops.NotImplementedError("slice on multi-dimensional array"); } slice.DoSliceAssign( delegate(int idx, object val) { a.SetValue(Ops.ConvertTo(val, elm), idx + a.GetLowerBound(0)); }, a.Length, value); return; } throw Ops.TypeError("bad type for array index: {0}", Ops.GetDynamicType(value).__name__); }
public object?this[[NotNull] Slice slice] { get { CheckBuffer(); if (_numDims == 0) { throw PythonOps.TypeError("invalid indexing of 0-dim memory"); } int start, stop, step, count; FixSlice(slice, __len__(), out start, out stop, out step, out count); return(new MemoryView(this, start, stop, step, count)); } set { CheckBuffer(); if (_isReadOnly) { throw PythonOps.TypeError("cannot modify read-only memory"); } if (_numDims == 0) { throw PythonOps.TypeError("invalid indexing of 0-dim memory"); } if (_numDims != 1) { throw PythonOps.NotImplementedError("memoryview assignments are restricted to ndim = 1"); } int start, stop, step, sliceCnt; FixSlice(slice, __len__(), out start, out stop, out step, out sliceCnt); int newLen = PythonOps.Length(value); if (sliceCnt != newLen) { throw PythonOps.ValueError("cannot resize memory view"); } // TODO: treat value as bytes-like object, not enumerable slice = new Slice(start, stop, step); slice.DoSliceAssign(SliceAssign, __len__(), value); } }
public object this[Slice index] { get { if (index == null) throw PythonOps.TypeError("expected Slice, got None"); int start, stop, step; index.indices(_data.Length, out start, out stop, out step); PythonArray pa = new PythonArray(new string(_typeCode, 1), Type.Missing); if (step < 0) { for (int i = start; i > stop; i += step) { pa._data.Append(_data.GetData(i)); } } else { for (int i = start; i < stop; i += step) { pa._data.Append(_data.GetData(i)); } } return pa; } set { if (index == null) throw PythonOps.TypeError("expected Slice, got None"); PythonArray pa = value as PythonArray; if (pa != null && pa._typeCode != _typeCode) { throw PythonOps.TypeError("bad array type"); } if (index.step != null) { if (Object.ReferenceEquals(value, this)) value = this.tolist(); index.DoSliceAssign(SliceAssign, _data.Length, value); } else { int start, stop, step; index.indices(_data.Length, out start, out stop, out step); if (stop < start) { stop = start; } // replace between start & stop w/ values IEnumerator ie = PythonOps.GetEnumerator(value); ArrayData newData = CreateData(_typeCode); for (int i = 0; i < start; i++) { newData.Append(_data.GetData(i)); } while (ie.MoveNext()) { newData.Append(ie.Current); } for (int i = stop; i < _data.Length; i++) { newData.Append(_data.GetData(i)); } _data = newData; } } }
public object this[Slice slice] { get { if (slice == null) { throw Ops.TypeError("list indicies must be integer or slice, not None"); } int start, stop, step; slice.indices(size, out start, out stop, out step); if ((step > 0 && start >= stop) || (step < 0 && start <= stop)) { return(new List()); } if (step == 1) { int n = Math.Max(0, stop - start); object[] ret = new object[n]; lock (this) Array.Copy(data, start, ret, 0, n); return(new List(ret)); } else { // start/stop/step could be near Int32.MaxValue, and simply addition could cause overflow int n = (int)(step > 0 ? (0L + stop - start + step - 1) / step : (0L + stop - start + step + 1) / step); object[] ret = new object[n]; lock (this) { int ri = 0; for (int i = 0, index = start; i < n; i++, index += step) { ret[ri++] = data[index]; } } return(new List(ret)); } } set { if (slice == null) { throw Ops.TypeError("list indicies must be integer or slice, not None"); } if (slice.step != null) { // try to assign back to self: make a copy first if (this == value) { value = new List(value); } slice.DoSliceAssign(this.SliceAssign, size, value); } else { int start, stop, step; slice.indices(size, out start, out stop, out step); if (start > stop) { return; } SliceNoStep(start, stop, value); } } }
public object this[Slice index] { get { if (index == null) throw PythonOps.TypeError("expected Slice, got None"); int start, stop, step; index.indices(_data.Length, out start, out stop, out step); PythonArray pa = new PythonArray(new string(_typeCode, 1), Type.Missing); if (step < 0) { for (int i = start; i > stop; i += step) { pa._data.Append(_data.GetData(i)); } } else { for (int i = start; i < stop; i += step) { pa._data.Append(_data.GetData(i)); } } return pa; } set { if (index == null) throw PythonOps.TypeError("expected Slice, got None"); CheckSliceAssignType(value); if (index.step != null) { if (Object.ReferenceEquals(value, this)) value = this.tolist(); index.DoSliceAssign(SliceAssign, _data.Length, value); } else { int start, stop, step; index.indices(_data.Length, out start, out stop, out step); if (stop < start) { stop = start; } SliceNoStep(value, start, stop); } } }