protected PyObject getslice(int start, int stop, int step) { if (step > 0 && stop < start) { stop = start; } int n = sliceLength(start, stop, step); PyObject[] newArray = new PyObject[n]; if (step == 1) { System.arraycopy(array, start, newArray, 0, stop - start); return fromArrayNoCopy(newArray); } for (int i = start, j = 0; j < n; i += step, j++) { newArray[j] = array[i]; } return fromArrayNoCopy(newArray); }
@ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.tuple___add___doc) final PyObject tuple___add__(PyObject generic_other) { PyTuple sum = null; if (generic_other instanceof PyTuple) { PyTuple other = (PyTuple) generic_other; PyObject[] newArray = new PyObject[array.length + other.array.length]; System.arraycopy(array, 0, newArray, 0, array.length); System.arraycopy(other.array, 0, newArray, array.length, other.array.length); sum = fromArrayNoCopy(newArray); } return sum; }
/** * Return a new PyTuple from an iterable. * * Raises a TypeError if the object is not iterable. * * @param iterable an iterable PyObject * @return a PyTuple containing each item in the iterable */ public static PyTuple fromIterable(PyObject iterable) { return fromArrayNoCopy(Py.make_array(iterable)); }
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); }
@ExposedNew final static PyObject tuple_new(PyNewWrapper new_, boolean init, PyType subtype, PyObject[] args, String[] keywords) { ArgParser ap = new ArgParser("tuple", args, keywords, new String[] {"sequence"}, 0); PyObject S = ap.getPyObject(0, null); if (new_.for_type == subtype) { if (S == null) { return EMPTY_TUPLE; } if (S instanceof PyTupleDerived) { return new PyTuple(((PyTuple) S).getArray()); } if (S instanceof PyTuple) { return S; } return fromArrayNoCopy(Py.make_array(S)); } else { if (S == null) { return new PyTupleDerived(subtype, Py.EmptyObjects); } return new PyTupleDerived(subtype, Py.make_array(S)); } }