示例#1
0
            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);
                    }
                }
            }
示例#2
0
            public object this[[NotNull] Slice slice] {
                get {
                    int        start, stop, step;
                    int        size     = ((ArrayType)NativeType).Length;
                    SimpleType elemType = ((ArrayType)NativeType).ElementType as SimpleType;

                    slice.indices(size, out start, out stop, out step);
                    if ((step > 0 && start >= stop) || (step < 0 && start <= stop))
                    {
                        if (elemType != null && (elemType._type == SimpleTypeKind.WChar || elemType._type == SimpleTypeKind.Char))
                        {
                            return(String.Empty);
                        }
                        return(new PythonList());
                    }

                    int n = (int)(step > 0 ? (0L + stop - start + step - 1) / step : (0L + stop - start + step + 1) / step);
                    if (elemType != null && (elemType._type == SimpleTypeKind.WChar || elemType._type == SimpleTypeKind.Char))
                    {
                        int           elmSize = ((INativeType)elemType).Size;
                        StringBuilder res     = new StringBuilder(n);

                        for (int i = 0, index = start; i < n; i++, index += step)
                        {
                            char c = elemType.ReadChar(_memHolder, checked (index * elmSize));
                            res.Append(c);
                        }

                        return(res.ToString());
                    }
                    else
                    {
                        object[] ret = new object[n];
                        int      ri  = 0;
                        for (int i = 0, index = start; i < n; i++, index += step)
                        {
                            ret[ri++] = this[index];
                        }

                        return(new PythonList(ret));
                    }
                }
                set {
                    int start, stop, step;
                    int size = ((ArrayType)NativeType).Length;

                    slice.indices(size, out start, out stop, out step);

                    int         n  = (int)(step > 0 ? (0L + stop - start + step - 1) / step : (0L + stop - start + step + 1) / step);
                    IEnumerator ie = PythonOps.GetEnumerator(value);
                    for (int i = 0, index = start; i < n; i++, index += step)
                    {
                        if (!ie.MoveNext())
                        {
                            throw PythonOps.ValueError("sequence not long enough");
                        }
                        this[index] = ie.Current;
                    }

                    if (ie.MoveNext())
                    {
                        throw PythonOps.ValueError("not all values consumed while slicing");
                    }
                }
            }