private IntPtr StoreTyped(Slice slice) { IntPtr ptr = this.allocator.Alloc((uint)Marshal.SizeOf(typeof(PySliceObject))); CPyMarshal.WriteIntField(ptr, typeof(PySliceObject), "ob_refcnt", 1); CPyMarshal.WritePtrField(ptr, typeof(PySliceObject), "ob_type", this.PySlice_Type); CPyMarshal.WritePtrField(ptr, typeof(PySliceObject), "start", this.Store(slice.start)); CPyMarshal.WritePtrField(ptr, typeof(PySliceObject), "stop", this.Store(slice.stop)); CPyMarshal.WritePtrField(ptr, typeof(PySliceObject), "step", this.Store(slice.step)); this.map.Associate(ptr, slice); return ptr; }
public object this[Slice index] { get { if (index.stop == null) { throw PythonOps.ValueError("slice stop is required"); } int start = index.start != null ? (int)index.start : 0; int stop = index.stop != null ? (int)index.stop : 0; int step = index.step != null ? (int)index.step : 1; if (step < 0 && index.start == null) { throw PythonOps.ValueError("slice start is required for step < 0"); } if (start < 0) { start = 0; } INativeType type = ((PointerType)NativeType)._type; SimpleType elemType = type as SimpleType; if ((stop < start && step > 0) || (start < stop && step < 0)) { if (elemType != null && (elemType._type == SimpleTypeKind.WChar || elemType._type == SimpleTypeKind.Char)) { return String.Empty; } return new List(); } MemoryHolder address = _memHolder.ReadMemoryHolder(0); if (elemType != null && (elemType._type == SimpleTypeKind.WChar || elemType._type == SimpleTypeKind.Char)) { int elmSize = ((INativeType)elemType).Size; StringBuilder res = new StringBuilder(); for (int i = start; stop > start ? i < stop : i > stop; i += step) { res.Append( elemType.ReadChar(address, checked(i * elmSize)) ); } return res.ToString(); } else { List res = new List((stop - start) / step); for (int i = start; stop > start ? i < stop : i > stop; i += step) { res.AddNoLock( type.GetValue(address, this, checked(type.Size * i), false) ); } return res; } } }
public int __cmp__(Slice obj) { return PythonOps.CompareArrays(new object[] { _start, _stop, _step }, 3, new object[] { obj._start, obj._stop, obj._step }, 3); }
public void __delslice__(Slice slice) { using (new MmapLocker(this)) { throw PythonOps.TypeError("mmap object doesn't support slice deletion"); } }
public string this[Slice slice] { get { using (new MmapLocker(this)) { long start, stop, step, longCount; PythonOps.FixSlice( _view.Capacity, GetLong(slice.start), GetLong(slice.stop), GetLong(slice.step), out start, out stop, out step, out longCount ); int count = (int)longCount; if (count == 0) { return ""; } StringBuilder sb = new StringBuilder(count); for (; count > 0; count--) { sb.Append((char)_view.ReadByte(start)); start += step; } return sb.ToString(); } } set { using (new MmapLocker(this)) { if (value == null) { throw PythonOps.TypeError("mmap slice assignment must be a string"); } EnsureWritable(); long start, stop, step, longCount; PythonOps.FixSlice( _view.Capacity, GetLong(slice.start), GetLong(slice.stop), GetLong(slice.step), out start, out stop, out step, out longCount ); int count = (int)longCount; if (value.Length != count) { throw PythonOps.IndexError("mmap slice assignment is wrong size"); } else if (count == 0) { return; } byte[] data = value.MakeByteArray(); if (step == 1) { _view.WriteArray(start, data, 0, value.Length); } else { foreach (byte b in data) { _view.Write(start, b); start += step; } } } } }
public object this[Slice slice] { get { throw Ops.TypeError("sequence index must be integer"); } }
public object this[Slice index] { get { throw new NotImplementedException(); } }
private int Compare(Slice obj) { return(PythonOps.CompareArrays(new object[] { _start, _stop, _step }, 3, new object[] { obj._start, obj._stop, obj._step }, 3)); }
public static string __getitem__(string s, Slice slice) { if (slice == null) throw Ops.TypeError("string indicies must be slices or integers"); int start, stop, step; slice.indices(s.Length, out start, out stop, out step); if (step == 1) { return stop > start ? s.Substring(start, stop - start) : String.Empty; } else { int index = 0; char[] newData; if (step > 0) { if (start > stop) return String.Empty; int icnt = (stop - start + step - 1) / step; newData = new char[icnt]; for (int i = start; i < stop; i += step) { newData[index++] = s[i]; } } else { if (start < stop) return String.Empty; int icnt = (stop - start + step + 1) / step; newData = new char[icnt]; for (int i = start; i > stop; i += step) { newData[index++] = s[i]; } } return new string(newData); } }
public object this[Slice slice] { get { return StringOps.__getitem__(self, slice); } }
void IBufferProtocol.SetSlice(Slice index, object value) { throw new InvalidOperationException(); }
internal static Array GetSlice(Array data, int size, Slice slice) { if (data.Rank != 1) throw Ops.NotImplementedError("slice on multi-dimensional array"); int start, stop, step; slice.indices(size, out start, out stop, out step); if ((step > 0 && start >= stop) || (step < 0 && start <= stop)) { if (data.GetType().GetElementType() == typeof(object)) return Ops.EMPTY; return Array.CreateInstance(data.GetType().GetElementType(), 0); } if (step == 1) { int n = stop - start; Array ret = Array.CreateInstance(data.GetType().GetElementType(), n); Array.Copy(data, start + data.GetLowerBound(0), ret, 0, n); return ret; } else { // could cause overflow (?) int n = step > 0 ? (stop - start + step - 1) / step : (stop - start + step + 1) / step; Array ret = Array.CreateInstance(data.GetType().GetElementType(), n); int ri = 0; for (int i = 0, index = start; i < n; i++, index += step) { ret.SetValue(data.GetValue(index + data.GetLowerBound(0)), ri++); } return ret; } }
public static object GetItem(Array data, Slice slice) { return GetSlice(data, data.Length, slice); }
public void __delitem__(Slice slice) { if (slice == null) throw PythonOps.TypeError("expected Slice, got None"); int start, stop, step; // slice is sealed, indices can't be user code... slice.indices(_data.Length, out start, out stop, out step); if (step > 0 && (start >= stop)) return; if (step < 0 && (start <= stop)) return; if (step == 1) { int i = start; for (int j = stop; j < _data.Length; j++, i++) { _data.SetData(i, _data.GetData(j)); } for (i = 0; i < stop - start; i++) { _data.RemoveAt(_data.Length - 1); } return; } if (step == -1) { int i = stop + 1; for (int j = start + 1; j < _data.Length; j++, i++) { _data.SetData(i, _data.GetData(j)); } for (i = 0; i < stop - start; i++) { _data.RemoveAt(_data.Length - 1); } return; } if (step < 0) { // find "start" we will skip in the 1,2,3,... order int i = start; while (i > stop) { i += step; } i -= step; // swap start/stop, make step positive stop = start + 1; start = i; step = -step; } int curr, skip, move; // skip: the next position we should skip // curr: the next position we should fill in data // move: the next position we will check curr = skip = move = start; while (curr < stop && move < stop) { if (move != skip) { _data.SetData(curr++, _data.GetData(move)); } else skip += step; move++; } while (stop < _data.Length) { _data.SetData(curr++, _data.GetData(stop++)); } while (_data.Length > curr) { _data.RemoveAt(_data.Length - 1); } }
public void __delitem__(Slice slice) { // crashes CPython throw new NotImplementedException(); }
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[[NotNull]Slice slice] { get { if (slice.step != null) { throw PythonOps.NotImplementedError(""); } return new MemoryView( _buffer, slice.start == null ? _start : (Converter.ConvertToInt32(slice.start) + _start), slice.stop == null ? _end : (Converter.ConvertToInt32(slice.stop) + _start) ); } set { if (_start != 0 || _end != null) { slice = new Slice( slice.start == null ? _start : (Converter.ConvertToInt32(slice.start) + _start), slice.stop == null ? _end : (Converter.ConvertToInt32(slice.stop) + _start) ); } int len = PythonOps.Length(value); int start, stop, step; slice.indices(PythonOps.Length(_buffer), out start, out stop, out step); if (stop - start != len) { throw PythonOps.ValueError("cannot resize memory view"); } _buffer.SetSlice(slice, value); } }
internal static expr Convert(Compiler.Ast.Expression expr, expr_context ctx) { expr ast; if (expr is ConstantExpression) ast = Convert((ConstantExpression)expr); else if (expr is NameExpression) ast = new Name((NameExpression)expr, ctx); else if (expr is UnaryExpression) ast = new UnaryOp((UnaryExpression)expr); else if (expr is BinaryExpression) ast = Convert((BinaryExpression)expr); else if (expr is AndExpression) ast = new BoolOp((AndExpression)expr); else if (expr is OrExpression) ast = new BoolOp((OrExpression)expr); else if (expr is CallExpression) ast = new Call((CallExpression)expr); else if (expr is ParenthesisExpression) return Convert(((ParenthesisExpression)expr).Expression); else if (expr is LambdaExpression) ast = new Lambda((LambdaExpression)expr); else if (expr is ListExpression) ast = new List((ListExpression)expr, ctx); else if (expr is TupleExpression) ast = new Tuple((TupleExpression)expr, ctx); else if (expr is DictionaryExpression) ast = new Dict((DictionaryExpression)expr); else if (expr is ListComprehension) ast = new ListComp((ListComprehension)expr); else if (expr is GeneratorExpression) ast = new GeneratorExp((GeneratorExpression)expr); else if (expr is MemberExpression) ast = new Attribute((MemberExpression)expr, ctx); else if (expr is YieldExpression) ast = new Yield((YieldExpression)expr); else if (expr is ConditionalExpression) ast = new IfExp((ConditionalExpression)expr); else if (expr is IndexExpression) ast = new Subscript((IndexExpression)expr, ctx); else if (expr is SliceExpression) ast = new Slice((SliceExpression)expr); else if (expr is BackQuoteExpression) ast = new Repr((BackQuoteExpression)expr); else throw new ArgumentTypeException("Unexpected expression type: " + expr.GetType()); ast.GetSourceLocation(expr); return ast; }
void IBufferProtocol.SetSlice(Slice index, object value) { throw new NotImplementedException(); }
public object this[Slice slice] { get { return MakeTuple()[slice]; } }
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); } } }