public List subList(int fromIndex, int toIndex) { if (fromIndex < 0 || toIndex > size()) { throw new IndexOutOfBoundsException(); } else if (fromIndex > toIndex) { throw new IllegalArgumentException(); } PyObject elements[] = new PyObject[toIndex - fromIndex]; for (int i = 0, j = fromIndex; i < elements.length; i++, j++) { elements[i] = array[j]; } return new PyTuple(elements); }
@ExposedMethod(doc = BuiltinDocs.tuple___len___doc) final int tuple___len__() { return size(); }
public int index(PyObject value, int start) { return index(value, start, size()); }
@ExposedMethod(doc = BuiltinDocs.tuple___hash___doc) final int tuple___hash__() { // strengthened hash to avoid common collisions. from CPython // tupleobject.tuplehash. See http://bugs.python.org/issue942952 int y; int len = size(); int mult = 1000003; int x = 0x345678; while (--len >= 0) { y = array[len].hashCode(); x = (x ^ y) * mult; mult += 82520 + len + len; } return x + 97531; }
@ExposedMethod(doc = BuiltinDocs.set___contains___doc) final boolean dict_items___contains__(PyObject item) { if (item instanceof PyTuple) { PyTuple tupleItem = (PyTuple)item; if (tupleItem.size() == 2) { SimpleEntry entry = new SimpleEntry(tupleItem.get(0), tupleItem.get(1)); return dvDict.entrySet().contains(entry); } } return false; }
protected PyObject repeat(int count) { if (count < 0) { count = 0; } int size = size(); if (size == 0 || count == 1) { if (getType() == TYPE) { // Since tuples are immutable, we can return a shared copy in this case return this; } if (size == 0) { return EMPTY_TUPLE; } } int newSize = size * count; if (newSize / size != count) { throw Py.MemoryError(""); } PyObject[] newArray = new PyObject[newSize]; for (int i = 0; i < count; i++) { System.arraycopy(array, 0, newArray, i * size, size); } return fromArrayNoCopy(newArray); }
@ExposedMethod(defaults = {"null", "null"}, doc = BuiltinDocs.tuple_index_doc) final int tuple_index(PyObject value, PyObject start, PyObject stop) { int startInt = start == null ? 0 : PySlice.calculateSliceIndex(start); int stopInt = stop == null ? size() : PySlice.calculateSliceIndex(stop); return tuple_index(value, startInt, stopInt); }
@Override public Map<String, Parameter<?>> getOutputs(ScriptEngine engine) throws ScriptException { PyDictionary outputs = (PyDictionary) process(engine).__getattr__("outputs"); Map<String, Parameter<?>> map = new TreeMap<String, Parameter<?>>(); for (String name : (List<String>) outputs.keys()) { PyTuple output = (PyTuple) outputs.get(name); Object type = output.__getitem__(0); Object desc = output.__getitem__(1); // map every key in the optional dictionary as parameter metadata entry Map<String, Object> metadata = null; if (output.size() == 3) { PyDictionary meta = (PyDictionary) output.get(2); if (meta != null && !meta.isEmpty()) { metadata = new HashMap<String, Object>(); for (Object key : meta.keySet()) { metadata.put((String) key, meta.get(key)); } } } map.put(name, parameter(name, type, 1, 1, desc, null, metadata)); } return map; }
private static boolean abstractIsSubClass(PyObject derived, PyObject cls) { while (true) { if (derived == cls) { return true; } PyTuple bases = abstractGetBases(derived); if (bases == null) { return false; } int basesSize = bases.size(); if (basesSize == 0) { return false; } if (basesSize == 1) { // Avoid recursivity in the single inheritance case derived = bases.pyget(0); continue; } for (PyObject base : bases.asIterable()) { if (abstractIsSubClass(base, cls)) { return true; } } return false; } }
Object[] tuple = new Object[pyTuple.size()]; int i = 0; for (PyObject tupleObject : pyTuple.getArray()) {
public static PyObject filterTuple(PyObject func, PyTuple seq) { int len = seq.size(); if (len == 0) { if (seq.getType() != PyTuple.TYPE) { return Py.EmptyTuple; } return seq; } PyList list = new PyList(); PyObject item; for (int i = 0; i < len; i++) { item = seq.__finditem__(i); if (func == Py.None) { if (!item.__nonzero__()) { continue; } } else if (!func.__call__(item).__nonzero__()) { continue; } list.append(item); } return PyTuple.fromIterable(list); }
while (type instanceof PyTuple && ((PyTuple)type).size() > 0) { type = type.__getitem__(0);
PyObject newobj = copyreg.__findattr__("__newobj__"); n = ((PyTuple)args).size(); PyObject args2[] = new PyObject[n+1]; args2[0] = cls;