📄 pysequence.java
字号:
// Copyright (c) Corporation for National Research Initiativespackage org.python.core;class SeqFuncs extends PyBuiltinFunctionSet{ SeqFuncs(String name, int index, int argcount) { super(name, index, argcount, argcount, true, null); } public PyObject __call__() { PySequence seq = (PySequence)__self__; switch (index) { case 1: return new PyInteger(seq.__nonzero__() ? 1 : 0); default: throw argCountError(0); } } public PyObject __call__(PyObject arg) { PySequence seq = (PySequence)__self__; switch (index) { case 11: return seq.__getitem__(arg); case 12: seq.__delitem__(arg); return Py.None; case 13: return seq.__mul__(arg); case 14: return seq.__rmul__(arg); case 15: return new PyInteger(seq.__cmp__(arg)); default: throw argCountError(1); } } public PyObject __call__(PyObject arg1, PyObject arg2) { PySequence seq = (PySequence)__self__; switch (index) { case 21: seq.__setitem__(arg1, arg2); return Py.None; default: throw argCountError(1); } } public PyObject __call__(PyObject arg1, PyObject arg2, PyObject arg3) { PySequence seq = (PySequence)__self__; switch (index) { case 31: return seq.__getslice__(arg1, arg2, arg3); case 32: seq.__delslice__(arg1, arg2, arg3); return Py.None; default: throw argCountError(3); } } public PyObject __call__(PyObject arg1, PyObject arg2, PyObject arg3, PyObject arg4) { PySequence seq = (PySequence)__self__; switch (index) { case 41: seq.__setslice__(arg1, arg2, arg3, arg4); return Py.None; default: throw argCountError(4); } }}/** * The abstract superclass of PyObjects that implements a Sequence. * Minimize the work in creating such objects. * * Method names are designed to make it possible for PySequence to * implement java.util.List interface when JDK 1.2 is ubiquitous. * * All subclasses must declare that they implement the ClassDictInit * interface, and provide an classDictInit() method that calls * PySequence.classDictInit(). * * Subclasses must also implement get, getslice, and repeat methods. * * Subclasses that are mutable should also implement: set, setslice, del, * and delRange. */// this class doesn't "implement InitModule" because otherwise// PyJavaClass.init() would try to instantiate it. That fails because this// class is abstract. TBD: is there a way to test for whether a class is// abstract?abstract public class PySequence extends PyObject{ /** * This constructor is used by PyJavaClass.init() */ public PySequence() {} /** * This constructor is used to pass on an __class__ attribute. */ public PySequence(PyClass c) { super(c); } /** <i>Internal use only. Do not call this method explicit.</i> */ public static void classDictInit(PyObject dict) { dict.__setitem__("__nonzero__", new SeqFuncs("__nonzero__", 1, 0)); dict.__setitem__("__getitem__", new SeqFuncs("__getitem__", 11, 1)); dict.__setitem__("__delitem__", new SeqFuncs("__delitem__", 12, 1)); dict.__setitem__("__mul__", new SeqFuncs("__mul__", 13, 1)); dict.__setitem__("__rmul__", new SeqFuncs("__rmul__", 14, 1)); dict.__setitem__("__cmp__", new SeqFuncs("__cmp__", 15, 1)); dict.__setitem__("__setitem__", new SeqFuncs("__setitem__", 21, 2)); dict.__setitem__("__getslice__", new SeqFuncs("__getslice__", 31, 3)); dict.__setitem__("__delslice__", new SeqFuncs("__delslice__", 32, 3)); dict.__setitem__("__setslice__", new SeqFuncs("__setslice__", 41, 4)); // TBD: __tojava__() // hide these from Python! dict.__setitem__("classDictInit", null); } // These methods must be defined for any sequence /** * @param index index of element to return. * @return the element at the given position in the list. */ abstract protected PyObject get(int index); /** * Returns a range of elements from the sequence. * * @param start the position of the first element. * @param stop one more than the position of the last element. * @param step the step size. * @return a sequence corresponding the the given range of elements. */ abstract protected PyObject getslice(int start, int stop, int step); /** * Repeats the given sequence. * * @param count the number of times to repeat the sequence. * @return this sequence repeated count times. */ abstract protected PyObject repeat(int count); // These methods only apply to mutable sequences /** * Sets the given element of the sequence. * * @param index index of the element to set. * @param value the value to set this element to. */ protected void set(int index, PyObject value) { throw Py.TypeError("can't assign to immutable object"); } /** * Sets the given range of elements. */ protected void setslice(int start, int stop, int step, PyObject value) { throw Py.TypeError("can't assign to immutable object"); } protected void del(int i) throws PyException { throw Py.TypeError("can't remove from immutable object"); } protected void delRange(int start, int stop, int step) { throw Py.TypeError("can't remove from immutable object"); } public boolean __nonzero__() { return __len__() != 0; } public PyObject __iter__() { return new PySequenceIter(this); } public synchronized PyObject __eq__(PyObject o) { if (o.__class__ != __class__) return null; int tl = __len__(); int ol = o.__len__(); if (tl != ol) return Py.Zero; int i = cmp(this, tl, o, ol); return (i < 0) ? Py.One : Py.Zero; } public synchronized PyObject __ne__(PyObject o) { if (o.__class__ != __class__) return null; int tl = __len__(); int ol = o.__len__(); if (tl != ol) return Py.One; int i = cmp(this, tl, o, ol); return (i < 0) ? Py.Zero : Py.One; } public synchronized PyObject __lt__(PyObject o) { if (o.__class__ != __class__) return null; int i = cmp(this, -1, o, -1); if (i < 0) return (i == -1) ? Py.One : Py.Zero; return __finditem__(i)._lt(o.__finditem__(i)); } public synchronized PyObject __le__(PyObject o) { if (o.__class__ != __class__) return null; int i = cmp(this, -1, o, -1); if (i < 0) return (i == -1 || i == -2) ? Py.One : Py.Zero; return __finditem__(i)._le(o.__finditem__(i)); } public synchronized PyObject __gt__(PyObject o) { if (o.__class__ != __class__) return null; int i = cmp(this, -1, o, -1); if (i < 0) return (i == -3) ? Py.One : Py.Zero; return __finditem__(i)._gt(o.__finditem__(i)); } public synchronized PyObject __ge__(PyObject o) { if (o.__class__ != __class__) return null; int i = cmp(this, -1, o, -1); if (i < 0) return (i == -3 || i == -2) ? Py.One : Py.Zero; return __finditem__(i)._ge(o.__finditem__(i)); } // Return value >= 0 is the index where the sequences differs. // -1: reached the end of o1 without a difference
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -