📄 pysequence.java
字号:
// -2: reached the end of both seqeunces without a difference // -3: reached the end of o2 without a difference private static int cmp(PyObject o1, int ol1, PyObject o2, int ol2) { if (ol1 < 0) ol1 = o1.__len__(); if (ol2 < 0) ol2 = o2.__len__(); int i = 0; for ( ; i < ol1 && i < ol2; i++) { if (!o1.__getitem__(i)._eq(o2.__getitem__(i)).__nonzero__()) return i; } if (ol1 == ol2) return -2; return (ol1 < ol2) ? -1 : -3; } // Return a copy of a sequence where the __len__() method is // telling the thruth. protected static PyObject fastSequence(PyObject seq, String msg) { if (seq instanceof PyList || seq instanceof PyTuple) return seq; if (!seq.isSequenceType()) throw Py.TypeError(msg); PyList list = new PyList(); PyObject iter = Py.iter(seq, msg); for (PyObject item = null; (item = iter.__iternext__()) != null; ) { list.append(item); } return list; } protected static final int sliceLength(int start, int stop, int step) { //System.err.println("slice: "+start+", "+stop+", "+step); int ret; if (step > 0) { ret = (stop-start+step-1)/step; } else { ret = (stop-start+step+1)/step; } if (ret < 0) return 0; return ret; } private static final int getIndex(PyObject index, int defaultValue) { if (index == Py.None || index == null) return defaultValue; if (index instanceof PyLong) { try { index = index.__int__(); } catch (PyException exc) { if (Py.matchException(exc, Py.OverflowError)) { if (new PyLong(0L).__cmp__(index) < 0) return Integer.MAX_VALUE; else return 0; } } } if (!(index instanceof PyInteger)) throw Py.TypeError("slice index must be int"); return ((PyInteger)index).getValue(); } protected int fixindex(int index) { int l = __len__(); if (index < 0) index += l; if (index < 0 || index >= l) return -1; //throw Py.IndexError("index out of range"); else return index; } public synchronized PyObject __finditem__(int index) { index = fixindex(index); if (index == -1) return null; else return get(index); } public PyObject __finditem__(PyObject index) { if (index instanceof PyInteger) return __finditem__(((PyInteger)index).getValue()); else if (index instanceof PySlice) { PySlice s = (PySlice)index; return __getslice__(s.start, s.stop, s.step); } else if (index instanceof PyLong) return __finditem__(index.__int__().getValue()); else throw Py.TypeError("sequence subscript must be integer or slice"); } public PyObject __getitem__(PyObject index) { PyObject ret = __finditem__(index); if (ret == null) { throw Py.IndexError("index out of range: "+index); } return ret; } public boolean isMappingType() { return false; } public boolean isNumberType() { return false; } protected static final int getStep(PyObject s_step) { int step = getIndex(s_step, 1); if (step == 0) { throw Py.TypeError("slice step of zero not allowed"); } return step; } protected static final int getStart(PyObject s_start, int step, int length) { int start; if (step < 0) { start = getIndex(s_start, length - 1); if (start < 0) start += length; if (start < 0) start = -1; if (start >= length) start = length - 1; } else { start = getIndex(s_start, 0); if (start < 0) start += length; if (start < 0) start = 0; if (start >= length) start = length; } return start; } protected static final int getStop(PyObject s_stop, int start, int step, int length) { int stop; if (step < 0) { stop = getIndex(s_stop, -1); if (stop < -1) stop = length+stop; if (stop < -1) stop = -1; } else { stop = getIndex(s_stop, length); if (stop < 0) stop = length+stop; if (stop < 0) stop = 0; } if (stop > length) stop = length; return stop; } public synchronized PyObject __getslice__(PyObject s_start, PyObject s_stop, PyObject s_step) { int length = __len__(); int step = getStep(s_step); int start = getStart(s_start, step, length); int stop = getStop(s_stop, start, step, length); return getslice(start, stop, step); } public synchronized void __setslice__(PyObject s_start, PyObject s_stop, PyObject s_step, PyObject value) { int length = __len__(); int step = getStep(s_step); int start = getStart(s_start, step, length); int stop = getStop(s_stop, start, step, length); setslice(start, stop, step, value); } public synchronized void __delslice__(PyObject s_start, PyObject s_stop, PyObject s_step) { int length = __len__(); int step = getStep(s_step); int start = getStart(s_start, step, length); int stop = getStop(s_stop, start, step, length); delRange(start, stop, step); } public synchronized void __setitem__(int index, PyObject value) { int i = fixindex(index); if (i == -1) throw Py.IndexError("index out of range: "+i); set(i, value); } public void __setitem__(PyObject index, PyObject value) { if (index instanceof PyInteger) __setitem__(((PyInteger)index).getValue(), value); else { if (index instanceof PySlice) { PySlice s = (PySlice)index; __setslice__(s.start, s.stop, s.step, value); } else if (index instanceof PyLong) { __setitem__(index.__int__().getValue(), value); } else { throw Py.TypeError( "sequence subscript must be integer or slice"); } } } public synchronized void __delitem__(PyObject index) { if (index instanceof PyInteger) { int i = fixindex(((PyInteger)index).getValue()); if (i == -1) throw Py.IndexError("index out of range: "+i); del(i); } else { if (index instanceof PySlice) { PySlice s = (PySlice)index; __delslice__(s.start, s.stop, s.step); } else if (index instanceof PyLong) { int i = fixindex(index.__int__().getValue()); if (i == -1) throw Py.IndexError("index out of range: "+i); del(i); } else { throw Py.TypeError( "sequence subscript must be integer or slice"); } } } public synchronized Object __tojava__(Class c) { if (c.isArray()) { Class component = c.getComponentType(); //System.out.println("getting: "+component); try { int n = __len__(); PyArray array = new PyArray(component, n); for (int i=0; i<n; i++) { PyObject o = get(i); array.set(i, o); } //System.out.println("getting: "+component+", "+array.data); return array.data; } catch (Throwable t) { ;//System.out.println("failed to get: "+component.getName()); } } return super.__tojava__(c); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -