⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pylist.java

📁 无线通信的主要编程软件,是无线通信工作人员的必备工具,关天相关教程我会在后续传上.
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
// Copyright (c) Corporation for National Research Initiatives// Implementation of the standard Python list objectspackage org.python.core;import java.util.Vector;class ListFunctions extends PyBuiltinFunctionSet{    ListFunctions(String name, int index, int argcount) {        super(name, index, argcount, argcount, true, null);    }    ListFunctions(String name, int index, int minargs, int maxargs) {        super(name, index, minargs, maxargs, true, null);    }    public PyObject __call__() {        PyList list = (PyList)__self__;        switch (index) {        case 1:            list.reverse();            return Py.None;        case 2:            list.sort(null);            return Py.None;        case 3:            return new PyInteger(list.__len__());        default:            throw argCountError(0);        }    }    public PyObject __call__(PyObject arg) {        PyList list = (PyList)__self__;        switch (index) {        case 2:            list.sort(arg);            return Py.None;        case 10:            list.append(arg);            return Py.None;        case 11:            return new PyInteger(list.count(arg));        case 12:            return new PyInteger(list.index(arg));        case 13:            list.remove(arg);            return Py.None;        case 14:            list.extend(arg);            return Py.None;        case 15:            return list.__add__(arg);        default:            throw argCountError(1);        }    }    public PyObject __call__(PyObject arg1, PyObject arg2) {        PyList list = (PyList)__self__;        switch (index) {        case 20:            if (!(arg1 instanceof PyInteger)) {                throw Py.TypeError(                    "illegal argument type for built-in operation");            }            int index = ((PyInteger)arg1).getValue();            list.insert(index, arg2);            return Py.None;        default:            throw argCountError(2);        }    }}/** * A builtin python list. */public class PyList extends PySequence implements ClassDictInit{    protected PyObject[] list;    protected int length;    protected static PyObject __methods__;    static {        PyList list = new PyList();        String[] methods = {            "append", "count", "extend", "index", "insert", "pop",            "remove", "reverse", "sort"};        for (int i = 0; i < methods.length; i++)            list.append(new PyString(methods[i]));        __methods__ = list;    }    /** <i>Internal use only. Do not call this method explicit.</i> */    public static void classDictInit(PyObject dict) {        PySequence.classDictInit(dict);        dict.__setitem__("reverse", new ListFunctions("reverse", 1, 0));        dict.__setitem__("sort", new ListFunctions("sort", 2, 0, 1));        dict.__setitem__("__len__", new ListFunctions("__len__", 3, 0));        dict.__setitem__("append", new ListFunctions("append", 10, 1));        dict.__setitem__("count", new ListFunctions("count", 11, 1));        dict.__setitem__("index", new ListFunctions("index", 12, 1));        dict.__setitem__("remove", new ListFunctions("remove", 13, 1));        dict.__setitem__("extend", new ListFunctions("extend", 14, 1));        dict.__setitem__("__add__", new ListFunctions("__add__", 15, 1));        dict.__setitem__("insert", new ListFunctions("insert", 20, 2));        // hide these from Python!        dict.__setitem__("initModule", null);        dict.__setitem__("toString", null);        dict.__setitem__("hashCode", null);    }    public PyList() {        this(Py.EmptyObjects);    }    public PyList(Vector ilist) {        this(new PyObject[ilist.size()]);        for (int i=0; i<ilist.size(); i++) {            list[i] = (PyObject)ilist.elementAt(i);        }    }    public PyList(PyObject elements[]) {        list = elements;        length = elements.length;    }    public PyList(PyObject o) {        this();        PyObject iter = o.__iter__();        for (PyObject item = null; (item = iter.__iternext__()) != null; ) {            append(item);        }    }    public String safeRepr() throws PyIgnoreMethodTag {        return "'list' object";    }    public int __len__() {        return length;    }    public PyObject __findattr__(String name) {        if (name.equals("__methods__")) {            PyList mlist = (PyList)__methods__;            PyString methods[] = new PyString[mlist.length];            for (int i = 0; i < mlist.length; i++)                methods[i] = (PyString)mlist.list[i];            return new PyList(methods);        }        return super.__findattr__(name);    }    protected PyObject get(int i) {        return list[i];    }    protected PyObject getslice(int start, int stop, int step) {        if (step > 0 && stop < start)            stop = start;        int n = sliceLength(start, stop, step);        PyObject[] newList = new PyObject[n];        if (step == 1) {            System.arraycopy(list, start, newList, 0, stop-start);            return new PyList(newList);        }        int j = 0;        for (int i=start; j<n; i+=step) {            newList[j] = list[i];            j++;        }        return new PyList(newList);    }    protected void del(int i) {        length = length-1;        System.arraycopy(list, i+1, list, i, length-i);        list[length] = null;    }    protected void delRange(int start, int stop, int step) {        if (step != 1)            throw Py.ValueError("step size must be 1 for deleting "+                                "list slice");        System.arraycopy(list, stop, list, start, length-stop);        int newLength = length-(stop-start);        int oldLength = length;        for(int i = newLength; i < oldLength; i++)            list[i] = null;        length = newLength;    }    protected void set(int i, PyObject value) {        list[i] = value;    }    protected void setslice(int start, int stop, int step, PyObject value) {        if (!(value instanceof PySequence))            throw Py.TypeError("rhs of setslice must be a sequence");        if (step != 1)            throw Py.ValueError("step size must be 1 for setting list slice");        if (stop < start)            stop = start;        PySequence seq = (PySequence)value;        // Hack to make recursive setslices work correctly.        // Could avoid the copy if we wanted to be more clever about        // the code to do the moving        int n = seq.__len__();        int i;        int length = this.length;        int newLength = length-(stop-start)+n;        if (newLength > length || newLength < length) {            resize(newLength);            System.arraycopy(list, stop, list, stop+(newLength-length),                             length-stop);            if (newLength < length) {                for (i = newLength; i < length; i++)                    list[i] = null;            }        }//         else if (newLength < length) {//             System.arraycopy(list, stop, list, stop+(newLength-length),//                              length-stop);//             this.length = newLength;//         }        PyObject[] otherList = null;        if (value instanceof PyTuple)            otherList = ((PyTuple)value).list;        if (value instanceof PyList) {            otherList = ((PyList)value).list;            if (otherList == list)                otherList = (PyObject[])otherList.clone();        }        if (otherList != null) {            System.arraycopy(otherList, 0, list, start, n);        }        else {            for(i=0; i<n; i++) {                list[i+start] = seq.get(i);            }        }    }    protected PyObject repeat(int count) {        int l = length;        PyObject[] newList = new PyObject[l*count];        for (int i=0; i<count; i++) {            System.arraycopy(list, 0, newList, i*l, l);        }        return new PyList(newList);    }    public PyObject __imul__(PyObject o) {        if (!(o instanceof PyInteger || o instanceof PyLong))            throw Py.TypeError("can't multiply sequence to non-int");        int l = length;        int count = o.__int__().getValue();        resize(l * count);        for (int i=0; i<count; i++) {            System.arraycopy(list, 0, list, i*l, l);        }        return this;    }    public PyObject __add__(PyObject genericOther) {        if (genericOther instanceof PyList) {            PyList other = (PyList)genericOther;            PyObject[] newList = new PyObject[length+other.length];            System.arraycopy(list, 0, newList, 0, length);            System.arraycopy(other.list, 0, newList, length, other.length);            return new PyList(newList);        } else {            return null;        }    }    public String toString() {        ThreadState ts = Py.getThreadState();        if (!ts.enterRepr(this)) {            return "[...]";        }        StringBuffer buf = new StringBuffer("[");        for (int i=0; i<length-1; i++) {            buf.append(((PyObject)list[i]).__repr__().toString());            buf.append(", ");        }        if (length > 0)            buf.append(((PyObject)list[length-1]).__repr__().toString());        buf.append("]");        ts.exitRepr(this);        return buf.toString();    }    protected void resize(int n) {        if (list.length < n) {            PyObject[] newList = new PyObject[(int)(n*1.5)];            System.arraycopy(list, 0, newList, 0, length);            list = newList;        }        length = n;    }    /**     * Add a single element to the end of list.     *     * @param o the element to add.     */    public void append(PyObject o) {        resize(length+1);        list[length-1] = o;    }    /**     * Return the number elements in the list that equals the argument.     *     * @param o the argument to test for. Testing is done with     *          the <code>==</code> operator.     */    public int count(PyObject o) {        int count = 0;        int n = length;        PyObject[] list = this.list;        for (int i=0; i<n; i++) {            if (list[i].equals(o))                count++;        }        return count;    }    /**

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -