From: <zy...@us...> - 2009-04-05 19:27:21
|
Revision: 6168 http://jython.svn.sourceforge.net/jython/?rev=6168&view=rev Author: zyasoft Date: 2009-04-05 19:27:20 +0000 (Sun, 05 Apr 2009) Log Message: ----------- Shim in PySequenceObjectList so that we can begin a gradual replacement of the underlying collection support for list (and probably tuple too). test_itertools and test_javalist currently fail, due to a slightly deeper stack exceeding the recursion limit. That's fine for now. Modified Paths: -------------- branches/newlist/src/org/python/core/PyList.java branches/newlist/src/org/python/core/PySequenceList.java branches/newlist/src/org/python/core/PyTuple.java Added Paths: ----------- branches/newlist/src/org/python/core/PySequenceObjectList.java Modified: branches/newlist/src/org/python/core/PyList.java =================================================================== --- branches/newlist/src/org/python/core/PyList.java 2009-04-05 19:15:19 UTC (rev 6167) +++ branches/newlist/src/org/python/core/PyList.java 2009-04-05 19:27:20 UTC (rev 6168) @@ -15,7 +15,7 @@ * A builtin python list. */ @ExposedType(name = "list", base = PyObject.class) -public class PyList extends PySequenceList { +public class PyList extends PySequenceObjectList { public static final PyType TYPE = PyType.fromClass(PyList.class); @@ -70,8 +70,8 @@ if(seq == null) { return; } - if(seq instanceof PySequenceList) { - PySequenceList p = (PySequenceList)seq.__getslice__(Py.None, Py.None, Py.One); + if(seq instanceof PySequenceObjectList) { + PySequenceObjectList p = (PySequenceObjectList)seq.__getslice__(Py.None, Py.None, Py.One); this.list = p.list; } else { for (PyObject item : seq.asIterable()) { Modified: branches/newlist/src/org/python/core/PySequenceList.java =================================================================== --- branches/newlist/src/org/python/core/PySequenceList.java 2009-04-05 19:15:19 UTC (rev 6167) +++ branches/newlist/src/org/python/core/PySequenceList.java 2009-04-05 19:27:20 UTC (rev 6168) @@ -5,171 +5,81 @@ import java.util.List; import java.util.ListIterator; -public abstract class PySequenceList extends PySequence implements List { +public abstract class PySequenceList extends PySequence { - protected PyObjectList list; - public PySequenceList() { - list = new PyObjectList(); } protected PySequenceList(PyType type) { super(type); - list = new PyObjectList(); } - protected PySequenceList(PyType type, PyObject[] elements) { - super(type); - list = new PyObjectList(elements); - } + public abstract void add(int index, Object element); - /** - * Creates an instance directly backed by the array of PyObject elements. - */ - public PySequenceList(PyObject[] elements) { - list = new PyObjectList(elements); - } + public abstract boolean add(Object o); - public PySequenceList(PyType type, Collection<PyObject> c) { - super(type); - list = new PyObjectList(c); - } + public abstract boolean addAll(int index, Collection c); - public void add(int index, Object element) { - list.add(index, element); - } + public abstract boolean addAll(Collection c); - public boolean add(Object o) { - return list.add(o); - } + public abstract void clear(); - public boolean addAll(int index, Collection c) { - return list.addAll(index, c); - } + public abstract boolean contains(Object o); - public boolean addAll(Collection c) { - return list.addAll(c); - } + public abstract boolean containsAll(Collection c); - public void clear() { - list.clear(); - } + public abstract boolean equals(Object o); - public boolean contains(Object o) { - return list.contains(o); - } + public abstract Object get(int index); - public boolean containsAll(Collection c) { - return list.containsAll(c); - } + /** + * Get the backing array. The array should not be modified. To get a copy of the array, see + * {@link #toArray()}. + */ + public abstract PyObject[] getArray(); - public Object get(int index) { - return list.get(index); - } + public abstract int hashCode(); - public int indexOf(Object o) { - return list.indexOf(o); - } + public abstract int indexOf(Object o); - public boolean isEmpty() { - return list.isEmpty(); - } + public abstract boolean isEmpty(); - public Iterator iterator() { - return list.iterator(); - } + public abstract Iterator iterator(); - public int lastIndexOf(Object o) { - return list.lastIndexOf(o); - } + public abstract int lastIndexOf(Object o); - public ListIterator listIterator() { - return list.listIterator(); - } + public abstract ListIterator listIterator(); - public ListIterator listIterator(int index) { - return list.listIterator(index); - } + public abstract ListIterator listIterator(int index); - public void pyadd(int index, PyObject element) { - list.pyadd(index, element); - } + public abstract void pyadd(int index, PyObject element); - public PyObject pyget(int index) { - return list.pyget(index); - } + public abstract boolean pyadd(PyObject o); - public void pyset(int index, PyObject element) { - list.pyset(index, element); - } + public abstract PyObject pyget(int index); - public Object remove(int index) { - return list.remove(index); - } + public abstract void pyset(int index, PyObject element); - public void remove(int start, int stop) { - list.remove(start, stop); - } + public abstract Object remove(int index); - public boolean remove(Object o) { - return list.remove(o); - } + public abstract void remove(int start, int stop); - public boolean removeAll(Collection c) { - return list.removeAll(c); - } + public abstract boolean remove(Object o); - public boolean retainAll(Collection c) { - return list.retainAll(c); - } + public abstract boolean removeAll(Collection c); - public Object set(int index, Object element) { - return list.set(index, element); - } + public abstract boolean retainAll(Collection c); - public int size() { - return list.size(); - } + public abstract Object set(int index, Object element); - public List subList(int fromIndex, int toIndex) { - return list.subList(fromIndex, toIndex); - } + public abstract int size(); - public Object[] toArray() { - return list.toArray(); - } + public abstract List subList(int fromIndex, int toIndex); - public Object[] toArray(Object[] a) { - return list.toArray(a); - } + public abstract Object[] toArray(); - public String toString() { - return list.toString(); - } + public abstract Object[] toArray(Object[] a); - public boolean pyadd(PyObject o) { - return list.pyadd(o); - } + public abstract String toString(); - public boolean equals(Object o) { - if(o instanceof PySequenceList) { - return list.equals(((PySequenceList)o).list); - } else if(o instanceof List) { - return o.equals(this); - } else { - return super.equals(o); - } - } - - public int hashCode() { - return list.hashCode(); - } - - /** - * Get the backing array. The array should not be modified. To get a copy of the array, see - * {@link #toArray()}. - */ - public PyObject[] getArray() { - return list.getArray(); - } } Added: branches/newlist/src/org/python/core/PySequenceObjectList.java =================================================================== --- branches/newlist/src/org/python/core/PySequenceObjectList.java (rev 0) +++ branches/newlist/src/org/python/core/PySequenceObjectList.java 2009-04-05 19:27:20 UTC (rev 6168) @@ -0,0 +1,175 @@ +package org.python.core; + +import java.util.Collection; +import java.util.Iterator; +import java.util.List; +import java.util.ListIterator; + +public abstract class PySequenceObjectList extends PySequenceList implements List { + + protected PyObjectList list; + + public PySequenceObjectList() { + list = new PyObjectList(); + } + + protected PySequenceObjectList(PyType type) { + super(type); + list = new PyObjectList(); + } + + protected PySequenceObjectList(PyType type, PyObject[] elements) { + super(type); + list = new PyObjectList(elements); + } + + /** + * Creates an instance directly backed by the array of PyObject elements. + */ + public PySequenceObjectList(PyObject[] elements) { + list = new PyObjectList(elements); + } + + public PySequenceObjectList(PyType type, Collection<PyObject> c) { + super(type); + list = new PyObjectList(c); + } + + public void add(int index, Object element) { + list.add(index, element); + } + + public boolean add(Object o) { + return list.add(o); + } + + public boolean addAll(int index, Collection c) { + return list.addAll(index, c); + } + + public boolean addAll(Collection c) { + return list.addAll(c); + } + + public void clear() { + list.clear(); + } + + public boolean contains(Object o) { + return list.contains(o); + } + + public boolean containsAll(Collection c) { + return list.containsAll(c); + } + + public Object get(int index) { + return list.get(index); + } + + public int indexOf(Object o) { + return list.indexOf(o); + } + + public boolean isEmpty() { + return list.isEmpty(); + } + + public Iterator iterator() { + return list.iterator(); + } + + public int lastIndexOf(Object o) { + return list.lastIndexOf(o); + } + + public ListIterator listIterator() { + return list.listIterator(); + } + + public ListIterator listIterator(int index) { + return list.listIterator(index); + } + + public void pyadd(int index, PyObject element) { + list.pyadd(index, element); + } + + public PyObject pyget(int index) { + return list.pyget(index); + } + + public void pyset(int index, PyObject element) { + list.pyset(index, element); + } + + public Object remove(int index) { + return list.remove(index); + } + + public void remove(int start, int stop) { + list.remove(start, stop); + } + + public boolean remove(Object o) { + return list.remove(o); + } + + public boolean removeAll(Collection c) { + return list.removeAll(c); + } + + public boolean retainAll(Collection c) { + return list.retainAll(c); + } + + public Object set(int index, Object element) { + return list.set(index, element); + } + + public int size() { + return list.size(); + } + + public List subList(int fromIndex, int toIndex) { + return list.subList(fromIndex, toIndex); + } + + public Object[] toArray() { + return list.toArray(); + } + + public Object[] toArray(Object[] a) { + return list.toArray(a); + } + + public String toString() { + return list.toString(); + } + + public boolean pyadd(PyObject o) { + return list.pyadd(o); + } + + public boolean equals(Object o) { + if(o instanceof PySequenceObjectList) { + return list.equals(((PySequenceObjectList)o).list); + } else if(o instanceof List) { + return o.equals(this); + } else { + return ((PySequence)this).equals(o); + } + } + + public int hashCode() { + return list.hashCode(); + } + + /** + * Get the backing array. The array should not be modified. To get a copy of the array, see + * {@link #toArray()}. + */ + public PyObject[] getArray() { + return list.getArray(); + } +} Modified: branches/newlist/src/org/python/core/PyTuple.java =================================================================== --- branches/newlist/src/org/python/core/PyTuple.java 2009-04-05 19:15:19 UTC (rev 6167) +++ branches/newlist/src/org/python/core/PyTuple.java 2009-04-05 19:27:20 UTC (rev 6168) @@ -17,7 +17,7 @@ */ @ExposedType(name = "tuple", base = PyObject.class) -public class PyTuple extends PySequenceList +public class PyTuple extends PySequenceObjectList { public static final PyType TYPE = PyType.fromClass(PyTuple.class); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <fwi...@us...> - 2009-04-08 19:51:05
|
Revision: 6191 http://jython.svn.sourceforge.net/jython/?rev=6191&view=rev Author: fwierzbicki Date: 2009-04-08 19:50:54 +0000 (Wed, 08 Apr 2009) Log Message: ----------- PyList is now former PyNewList, PyObject now implements Comparable<PyObject> and sort() method in PyList now uses Collections.sort, but does not yet handle cmp or key args. Modified Paths: -------------- branches/newlist/src/org/python/core/PyList.java branches/newlist/src/org/python/core/PyObject.java Removed Paths: ------------- branches/newlist/src/org/python/core/PyNewList.java Modified: branches/newlist/src/org/python/core/PyList.java =================================================================== --- branches/newlist/src/org/python/core/PyList.java 2009-04-08 06:48:21 UTC (rev 6190) +++ branches/newlist/src/org/python/core/PyList.java 2009-04-08 19:50:54 UTC (rev 6191) @@ -1,38 +1,58 @@ -// Copyright (c) Corporation for National Research Initiatives + // Copyright (c) Corporation for National Research Initiatives package org.python.core; -import java.util.Collection; -import java.util.Iterator; -import java.util.List; - +import java.util.ArrayList; +import java.util.Arrays; import org.python.expose.ExposedMethod; import org.python.expose.ExposedNew; import org.python.expose.ExposedType; import org.python.expose.MethodType; import org.python.util.Generic; -/** - * A builtin python list. - */ +import java.util.Collection; +import java.util.Collections; +import java.util.Iterator; +import java.util.List; +import java.util.ListIterator; + @ExposedType(name = "list", base = PyObject.class) -public class PyList extends PySequenceObjectList { +public class PyList extends PySequenceList implements List { public static final PyType TYPE = PyType.fromClass(PyList.class); + protected final List<PyObject> list; public PyList() { - this(TYPE, Py.EmptyObjects); + this(TYPE); } public PyList(PyType type) { super(type); + list = Generic.list(); } + public PyList(List list, boolean convert) { + super(TYPE); + if (!convert) { + this.list = list; + } else { + this.list = Generic.list(); + for (Object o : list) { + add(o); + } + } + } + public PyList(PyType type, PyObject[] elements) { - super(type, elements); + super(type); + list = new ArrayList<PyObject>(Arrays.asList(elements)); } public PyList(PyType type, Collection c) { - super(type, c); + super(type); + list = new ArrayList<PyObject>(c.size()); + for (Object o : c) { + add(o); + } } public PyList(PyObject[] elements) { @@ -40,23 +60,28 @@ } public PyList(Collection c) { - super(TYPE, c); + this(TYPE, c); } public PyList(PyObject o) { this(TYPE); for (PyObject item : o.asIterable()) { - append(item); + list.add(item); } } + public PyList fromList(List list) { + return new PyList(list, false); + } + private static List<PyObject> listify(Iterator<PyObject> iter) { - List<PyObject> list = Generic.list(); - while (iter.hasNext()) { + List<PyObject> list = Generic.list(); + while (iter.hasNext()) { list.add(iter.next()); - } - return list; + } + return list; } + public PyList(Iterator<PyObject> iter) { this(TYPE, listify(iter)); } @@ -64,15 +89,14 @@ @ExposedNew @ExposedMethod(doc = BuiltinDocs.list___init___doc) final void list___init__(PyObject[] args, String[] kwds) { - ArgParser ap = new ArgParser("list", args, kwds, new String[] {"sequence"}, 0); + ArgParser ap = new ArgParser("list", args, kwds, new String[]{"sequence"}, 0); PyObject seq = ap.getPyObject(0, null); clear(); - if(seq == null) { + if (seq == null) { return; } - if(seq instanceof PySequenceObjectList) { - PySequenceObjectList p = (PySequenceObjectList)seq.__getslice__(Py.None, Py.None, Py.One); - this.list = p.list; + if (seq instanceof PyList) { + list.addAll((PyList) seq); // don't convert } else { for (PyObject item : seq.asIterable()) { append(item); @@ -80,6 +104,7 @@ } } + @Override public int __len__() { return list___len__(); } @@ -89,105 +114,59 @@ return size(); } - 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]; - PyObject[] array = getArray(); - if(step == 1) { - System.arraycopy(array, start, newList, 0, stop - start); - return new PyList(newList); - } - int j = 0; - for(int i = start; j < n; i += step) { - newList[j] = array[i]; - j++; - } - return new PyList(newList); - } - + @Override protected void del(int i) { remove(i); } + @Override protected void delRange(int start, int stop) { remove(start, stop); } + @Override protected void setslice(int start, int stop, int step, PyObject value) { - if(stop < start) { + if (stop < start) { stop = start; } - if(value instanceof PySequence) { - PySequence sequence = (PySequence) value; - setslicePySequence(start, stop, step, sequence); - } else if(value instanceof List) { - List list = (List)value.__tojava__(List.class); - if(list != null && list != Py.NoConversion) { - setsliceList(start, stop, step, list); + if ((value instanceof PySequence) || (!(value instanceof List))) { + if (value == this) { // copy + value = new PyList((PySequence) value); } + setsliceIterator(start, stop, step, value.asIterable().iterator()); } else { - setsliceIterable(start, stop, step, value); - } - } - - protected void setslicePySequence(int start, int stop, int step, PySequence value) { - if(step == 1) { - PyObject[] otherArray; - PyObject[] array = getArray(); - - int n = value.__len__(); - if (value instanceof PySequenceList) { - otherArray = ((PySequenceList)value).getArray(); - } else { - otherArray = Py.unpackSequence(value, value.__len__()); + System.err.println("List"); + List valueList = (List) value.__tojava__(List.class); + if (valueList != null && valueList != Py.NoConversion) { + setsliceList(start, stop, step, valueList); } - if (otherArray == array) { - otherArray = otherArray.clone(); - } - list.replaceSubArray(start, stop, otherArray, 0, n); - } else if(step != 0) { - if(value == this) { - PyList newseq = new PyList(); - PyObject iter = value.__iter__(); - for(PyObject item = null; (item = iter.__iternext__()) != null;) { - newseq.append(item); - } - value = newseq; - } - int n = value.__len__(); - for (int i = 0, j = start; i < n; i++, j += step) { - list.pyset(j, value.pyget(i)); - } } } protected void setsliceList(int start, int stop, int step, List value) { - if(step != 1) { - throw Py.TypeError("setslice with java.util.List and step != 1 not supported yet"); + int n = sliceLength(start, stop, step); + if (list instanceof ArrayList) { + ((ArrayList) list).ensureCapacity(start + n); } - int n = value.size(); - list.ensureCapacity(start + n); - for(int i = 0; i < n; i++) { - list.add(i + start, value.get(i)); + ListIterator src = value.listIterator(); + for (int j = start; src.hasNext(); j += step) { + set(j, src.next()); } } - protected void setsliceIterable(int start, int stop, int step, PyObject value) { - PyObject[] seq; - try { - seq = Py.make_array(value); - } catch (PyException pye) { - if (Py.matchException(pye, Py.TypeError)) { - throw Py.TypeError("can only assign an iterable"); + protected void setsliceIterator(int start, int stop, int step, Iterator<PyObject> iter) { + int size = list.size(); + for (int j = start; iter.hasNext(); j += step) { + PyObject item = iter.next(); + if (j >= size) { + list.add(item); + } else { + list.set(j, item); } - throw pye; } - setslicePySequence(start, stop, step, new PyList(seq)); } + @Override protected PyObject repeat(int count) { if (count < 0) { count = 0; @@ -198,12 +177,11 @@ throw Py.MemoryError(""); } - PyObject[] array = getArray(); - PyObject[] newArray = new PyObject[newSize]; - for(int i = 0; i < count; i++) { - System.arraycopy(array, 0, newArray, i * size, size); + PyList newList = new PyList(); + for (int i = 0; i < count; i++) { + newList.addAll(this); } - return new PyList(newArray); + return newList; } @ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.list___ne___doc) @@ -236,6 +214,7 @@ return seq___ge__(o); } + @Override public PyObject __imul__(PyObject o) { return list___imul__(o); } @@ -262,12 +241,14 @@ } int newSize = size * count; - list.setSize(newSize); - PyObject[] array = getArray(); + if (list instanceof ArrayList) { + ((ArrayList) list).ensureCapacity(newSize); + } + List oldList = new ArrayList<PyObject>(list); for (int i = 1; i < count; i++) { - System.arraycopy(array, 0, array, i * size, size); + list.addAll(oldList); } - gListAllocatedStatus = __len__(); + gListAllocatedStatus = __len__(); // now omit? return this; } @@ -297,6 +278,7 @@ return repeat(o.asIndex(Py.OverflowError)); } + @Override public PyObject __add__(PyObject o) { return list___add__(o); } @@ -304,22 +286,22 @@ @ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.list___add___doc) final PyObject list___add__(PyObject o) { PyList sum = null; - if(o instanceof PyList) { - PyList other = (PyList)o; - int thisLen = size(); - int otherLen = other.size(); - PyObject[] newList = new PyObject[thisLen + otherLen]; - System.arraycopy(getArray(), 0, newList, 0, thisLen); - System.arraycopy(other.getArray(), 0, newList, thisLen, otherLen); - sum = new PyList(newList); - } else if(!(o instanceof PySequenceList)) { + if (o instanceof PySequenceList && !(o instanceof PyTuple)) { + if (o instanceof PyList) { + List oList = ((PyList) o).list; + List newList = new ArrayList(list.size() + oList.size()); + newList.addAll(list); + newList.addAll(oList); + sum = fromList(newList); + } + } else if (!(o instanceof PySequenceList)) { // also support adding java lists (but not PyTuple!) Object oList = o.__tojava__(List.class); - if(oList != Py.NoConversion && oList != null) { + if (oList != Py.NoConversion && oList != null) { List otherList = (List) oList; sum = new PyList(); sum.list_extend(this); - for(Iterator i = otherList.iterator(); i.hasNext();) { + for (Iterator i = otherList.iterator(); i.hasNext();) { sum.add(i.next()); } } @@ -327,6 +309,7 @@ return sum; } + @Override public PyObject __radd__(PyObject o) { return list___radd__(o); } @@ -335,9 +318,9 @@ @ExposedMethod(type = MethodType.BINARY) final PyObject list___radd__(PyObject o) { // Support adding java.util.List, but prevent adding PyTuple. - // 'o' should never be a PyList since __add__ is defined. + // 'o' should never be a PyNewList since __add__ is defined. PyList sum = null; - if(o instanceof PySequence) { + if (o instanceof PySequence) { return null; } Object oList = o.__tojava__(List.class); @@ -367,12 +350,13 @@ @ExposedMethod(doc = BuiltinDocs.list___getitem___doc) final PyObject list___getitem__(PyObject o) { PyObject ret = seq___finditem__(o); - if(ret == null) { + if (ret == null) { throw Py.IndexError("index out of range: " + o); } return ret; } + @Override public PyObject __iter__() { return list___iter__(); } @@ -397,8 +381,9 @@ seq___delslice__(start, stop, step); } + @Override protected String unsupportedopMessage(String op, PyObject o2) { - if(op.equals("+")) { + if (op.equals("+")) { return "can only concatenate list (not \"{2}\") to list"; } return super.unsupportedopMessage(op, o2); @@ -412,19 +397,19 @@ @ExposedMethod(names = "__repr__") final String list_toString() { ThreadState ts = Py.getThreadState(); - if(!ts.enterRepr(this)) { + if (!ts.enterRepr(this)) { return "[...]"; } StringBuilder buf = new StringBuilder("["); int length = size(); - PyObject[] array = getArray(); - for(int i = 0; i < length - 1; i++) { - buf.append((array[i]).__repr__().toString()); - buf.append(", "); + int i = 0; + for (PyObject item : list) { + buf.append(item.__repr__().toString()); + if (i < length - 1) { + buf.append(", "); + } + i++; } - if(length > 0) { - buf.append((array[length - 1]).__repr__().toString()); - } buf.append("]"); ts.exitRepr(this); return buf.toString(); @@ -460,8 +445,8 @@ final int list_count(PyObject o) { int count = 0; PyObject[] array = getArray(); - for(int i = 0, n = size(); i < n; i++) { - if(array[i].equals(o)) { + for (int i = 0, n = size(); i < n; i++) { + if (array[i].equals(o)) { count++; } } @@ -510,8 +495,8 @@ int validStop = boundToSequence(stop); int validStart = boundToSequence(start); PyObject[] array = getArray(); - for(int i = validStart; i < validStop && i < size(); i++) { - if(array[i].equals(o)) { + for (int i = validStart; i < validStop && i < size(); i++) { + if (array[i].equals(o)) { return i; } } @@ -533,13 +518,13 @@ @ExposedMethod(doc = BuiltinDocs.list_insert_doc) final void list_insert(int index, PyObject o) { - if(index < 0) { + if (index < 0) { index = Math.max(0, size() + index); } - if(index > size()) { + if (index > size()) { index = size(); } - list.pyadd(index, o); + pyadd(index, o); gListAllocatedStatus = __len__(); } @@ -572,15 +557,7 @@ @ExposedMethod(doc = BuiltinDocs.list_reverse_doc) final void list_reverse() { - PyObject tmp; - int n = size(); - PyObject[] array = getArray(); - int j = n - 1; - for(int i = 0; i < n / 2; i++, j--) { - tmp = array[i]; - array[i] = array[j]; - array[j] = tmp; - } + Collections.reverse(list); gListAllocatedStatus = __len__(); } @@ -604,17 +581,16 @@ @ExposedMethod(defaults = "-1", doc = BuiltinDocs.list_pop_doc) final PyObject list_pop(int n) { int length = size(); - if(length == 0) { + if (length == 0) { throw Py.IndexError("pop from empty list"); } - if(n < 0) { + if (n < 0) { n += length; } - if(n < 0 || n >= length) { + if (n < 0 || n >= length) { throw Py.IndexError("pop index out of range"); } - PyObject v = pyget(n); - setslice(n, n + 1, 1, Py.EmptyTuple); + PyObject v = list.remove(n); return v; } @@ -631,11 +607,22 @@ @ExposedMethod(doc = BuiltinDocs.list_extend_doc) final void list_extend(PyObject o) { - int length = size(); - setslice(length, length, 1, o); + if (o instanceof PyList) { + list.addAll(((PyList) o).list); + } else if (o instanceof PySequenceObjectList) { + PyObject other[] = ((PySequenceObjectList) o).getArray(); + for (int i = 0; i < other.length; i++) { + list.add(other[i]); + } + } else { + for (PyObject item : o.asIterable()) { + list.add(item); + } + } gListAllocatedStatus = __len__(); } + @Override public PyObject __iadd__(PyObject o) { return list___iadd__(o); } @@ -672,13 +659,10 @@ * @param compare * the comparison function. */ - - /** + /** * Sort the items of the list in place. Items is compared with the normal relative comparison * operators. */ - - @ExposedMethod(doc = BuiltinDocs.list_sort_doc) final void list_sort(PyObject[] args, String[] kwds) { ArgParser ap = new ArgParser("list", args, kwds, new String[]{"cmp", "key", "reverse"}, 0); @@ -697,8 +681,10 @@ } public void sort(PyObject cmp, PyObject key, PyObject reverse) { - MergeState ms = new MergeState(this, cmp, key, reverse.__nonzero__()); - ms.sort(); + //System.out.println("sorting with " + cmp + ":" + key + ":" + reverse); + //MergeState ms = new MergeState(this, cmp, key, reverse.__nonzero__()); + //ms.sort(); + Collections.sort(list); } public int hashCode() { @@ -710,7 +696,210 @@ throw Py.TypeError(String.format("unhashable type: '%.200s'", getType().fastGetName())); } + @Override public PyTuple __getnewargs__() { - return new PyTuple(new PyTuple(list.getArray())); + return new PyTuple(new PyTuple(getArray())); } + + @Override + public void add(int index, Object element) { + pyadd(index, Py.java2py(element)); + } + + @Override + public boolean add(Object o) { + pyadd(Py.java2py(o)); + return true; + } + + @Override + public boolean addAll(int index, Collection c) { + if (c instanceof PySequenceList) { + list.addAll(index, c); + } else { + // need to use add to convert anything pulled from a collection into a PyObject + for (Object element : c) { + add(element); + } + } + return c.size() > 0; + } + + @Override + public boolean addAll(Collection c) { + if (c instanceof PySequenceList) { + list.addAll(c); + } else { + // need to use add to convert anything pulled from a collection into a PyObject + for (Object element : c) { + add(element); + } + } + return c.size() > 0; + } + + @Override + public void clear() { + list.clear(); + } + + @Override + public boolean contains(Object o) { + return list.contains(Py.java2py(o)); + } + + @Override + public boolean containsAll(Collection c) { + if (c instanceof PySequenceList) { + return list.containsAll(c); + } else { + //XXX: FJW this seems unnecessary. + return list.containsAll(new PyList(c)); + } + } + + @Override + public boolean equals(Object o) { + if (o instanceof PyList) { + return (((PyList) o).list.equals(list)); + } + return false; + } + + @Override + public Object get(int index) { + return list.get(index).__tojava__(Object.class); + } + + /** @deprecated */ + @Override + public PyObject[] getArray() { + PyObject a[] = new PyObject[list.size()]; + return list.toArray(a); + } + + @Override + public int indexOf(Object o) { + return list.indexOf(o); + } + + @Override + public boolean isEmpty() { + return list.isEmpty(); + } + + @Override + public Iterator iterator() { + return list.iterator(); + } + + @Override + public int lastIndexOf(Object o) { + return list.lastIndexOf(o); + } + + @Override + public ListIterator listIterator() { + return list.listIterator(); + } + + @Override + public ListIterator listIterator(int index) { + return list.listIterator(index); + } + + @Override + public void pyadd(int index, PyObject element) { + list.add(index, element); + } + + @Override + public boolean pyadd(PyObject o) { + list.add(o); + return true; + } + + @Override + public PyObject pyget(int index) { + return list.get(index); + } + + public void pyset(int index, PyObject element) { + list.set(index, element); + } + + @Override + public Object remove(int index) { + return list.remove(index); + } + + @Override + public void remove(int start, int stop) { + list.subList(start, stop).clear(); + } + + @Override + public boolean removeAll(Collection c) { + if (c instanceof PySequenceList) { + return list.removeAll(c); + } else { + return list.removeAll(new PyList(c)); + } + } + + @Override + public boolean retainAll(Collection c) { + if (c instanceof PySequenceList) { + return list.retainAll(c); + } else { + return list.retainAll(new PyList(c)); + } + } + + @Override + public Object set(int index, Object element) { + return list.set(index, Py.java2py(element)).__tojava__(Object.class); + } + + @Override + public int size() { + return list.size(); + } + + @Override + public List subList(int fromIndex, int toIndex) { + return list.subList(fromIndex, toIndex); + } + + @Override + public Object[] toArray() { + return list.toArray(); + } + + @Override + public Object[] toArray(Object[] a) { + return list.toArray(a); + } + + protected PyObject getslice(int start, int stop, int step) { + if (step > 0 && stop < start) { + stop = start; + } + int n = sliceLength(start, stop, step); + List newList; + if (step == 1) { + newList = new ArrayList<PyObject>(list.subList(start, stop)); + } else { + newList = new ArrayList<PyObject>(n); + for (int i = start, j = 0; j < n; i += step, j++) { + newList.add(list.get(i)); + } + } + return fromList(newList); + } + + @Override + public boolean remove(Object o) { + return list.remove(Py.java2py(o)); + } } Deleted: branches/newlist/src/org/python/core/PyNewList.java =================================================================== --- branches/newlist/src/org/python/core/PyNewList.java 2009-04-08 06:48:21 UTC (rev 6190) +++ branches/newlist/src/org/python/core/PyNewList.java 2009-04-08 19:50:54 UTC (rev 6191) @@ -1,902 +0,0 @@ - // Copyright (c) Corporation for National Research Initiatives -package org.python.core; - -import java.util.ArrayList; -import java.util.Arrays; -import org.python.expose.ExposedMethod; -import org.python.expose.ExposedNew; -import org.python.expose.ExposedType; -import org.python.expose.MethodType; -import org.python.util.Generic; - -import java.util.Collection; -import java.util.Collections; -import java.util.Iterator; -import java.util.List; -import java.util.ListIterator; - -@ExposedType(name = "newlist", base = PyObject.class) -public class PyNewList extends PySequenceList implements List { - - public static final PyType TYPE = PyType.fromClass(PyNewList.class); - protected final List<PyObject> list; - - public PyNewList() { - this(TYPE); - } - - public PyNewList(PyType type) { - super(type); - list = Generic.list(); - } - - public PyNewList(List list, boolean convert) { - super(TYPE); - if (!convert) { - this.list = list; - } else { - this.list = Generic.list(); - for (Object o : list) { - add(o); - } - } - } - - public PyNewList(PyType type, PyObject[] elements) { - super(type); - list = new ArrayList<PyObject>(Arrays.asList(elements)); - } - - public PyNewList(PyType type, Collection c) { - super(type); - list = new ArrayList<PyObject>(c.size()); - for (Object o : c) { - add(o); - } - } - - public PyNewList(PyObject[] elements) { - this(TYPE, elements); - } - - public PyNewList(Collection c) { - this(TYPE, c); - } - - public PyNewList(PyObject o) { - this(TYPE); - for (PyObject item : o.asIterable()) { - list.add(item); - } - } - - public PyNewList fromList(List list) { - return new PyNewList(list, false); - } - - private static List<PyObject> listify(Iterator<PyObject> iter) { - List<PyObject> list = Generic.list(); - while (iter.hasNext()) { - list.add(iter.next()); - } - return list; - } - - public PyNewList(Iterator<PyObject> iter) { - this(TYPE, listify(iter)); - } - - @ExposedNew - @ExposedMethod(doc = BuiltinDocs.list___init___doc) - final void newlist___init__(PyObject[] args, String[] kwds) { - ArgParser ap = new ArgParser("newlist", args, kwds, new String[]{"sequence"}, 0); - PyObject seq = ap.getPyObject(0, null); - clear(); - if (seq == null) { - return; - } - if (seq instanceof PyNewList) { - list.addAll((PyNewList) seq); // don't convert - } else { - for (PyObject item : seq.asIterable()) { - append(item); - } - } - } - - @Override - public int __len__() { - return newlist___len__(); - } - - @ExposedMethod(doc = BuiltinDocs.list___len___doc) - final int newlist___len__() { - return size(); - } - - @Override - protected void del(int i) { - remove(i); - } - - @Override - protected void delRange(int start, int stop) { - remove(start, stop); - } - - @Override - protected void setslice(int start, int stop, int step, PyObject value) { - if (stop < start) { - stop = start; - } - if ((value instanceof PySequence) || (!(value instanceof List))) { - if (value == this) { // copy - value = new PyNewList((PySequence) value); - } - setsliceIterator(start, stop, step, value.asIterable().iterator()); - } else { - System.err.println("List"); - List valueList = (List) value.__tojava__(List.class); - if (valueList != null && valueList != Py.NoConversion) { - setsliceList(start, stop, step, valueList); - } - } - } - - protected void setsliceList(int start, int stop, int step, List value) { - int n = sliceLength(start, stop, step); - if (list instanceof ArrayList) { - ((ArrayList) list).ensureCapacity(start + n); - } - ListIterator src = value.listIterator(); - for (int j = start; src.hasNext(); j += step) { - set(j, src.next()); - } - } - - protected void setsliceIterator(int start, int stop, int step, Iterator<PyObject> iter) { - int size = list.size(); - for (int j = start; iter.hasNext(); j += step) { - PyObject item = iter.next(); - if (j >= size) { - list.add(item); - } else { - list.set(j, item); - } - } - } - - @Override - protected PyObject repeat(int count) { - if (count < 0) { - count = 0; - } - int size = size(); - int newSize = size * count; - if (count != 0 && newSize / count != size) { - throw Py.MemoryError(""); - } - - PyNewList newList = new PyNewList(); - for (int i = 0; i < count; i++) { - newList.addAll(this); - } - return newList; - } - - @ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.list___ne___doc) - final PyObject newlist___ne__(PyObject o) { - return seq___ne__(o); - } - - @ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.list___eq___doc) - final PyObject newlist___eq__(PyObject o) { - return seq___eq__(o); - } - - @ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.list___lt___doc) - final PyObject newlist___lt__(PyObject o) { - return seq___lt__(o); - } - - @ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.list___le___doc) - final PyObject newlist___le__(PyObject o) { - return seq___le__(o); - } - - @ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.list___gt___doc) - final PyObject newlist___gt__(PyObject o) { - return seq___gt__(o); - } - - @ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.list___ge___doc) - final PyObject newlist___ge__(PyObject o) { - return seq___ge__(o); - } - - @Override - public PyObject __imul__(PyObject o) { - return newlist___imul__(o); - } - - @ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.list___imul___doc) - final PyObject newlist___imul__(PyObject o) { - if (!o.isIndex()) { - return null; - } - int count = o.asIndex(Py.OverflowError); - - int size = size(); - if (size == 0 || count == 1) { - return this; - } - - if (count < 1) { - clear(); - return this; - } - - if (size > Integer.MAX_VALUE / count) { - throw Py.MemoryError(""); - } - - int newSize = size * count; - if (list instanceof ArrayList) { - ((ArrayList) list).ensureCapacity(newSize); - } - List oldList = new ArrayList<PyObject>(list); - for (int i = 1; i < count; i++) { - list.addAll(oldList); - } - gListAllocatedStatus = __len__(); // now omit? - return this; - } - - @Override - public PyObject __mul__(PyObject o) { - return newlist___mul__(o); - } - - @ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.list___mul___doc) - final PyObject newlist___mul__(PyObject o) { - if (!o.isIndex()) { - return null; - } - return repeat(o.asIndex(Py.OverflowError)); - } - - @Override - public PyObject __rmul__(PyObject o) { - return newlist___rmul__(o); - } - - @ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.list___rmul___doc) - final PyObject newlist___rmul__(PyObject o) { - if (!o.isIndex()) { - return null; - } - return repeat(o.asIndex(Py.OverflowError)); - } - - @Override - public PyObject __add__(PyObject o) { - return newlist___add__(o); - } - - @ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.list___add___doc) - final PyObject newlist___add__(PyObject o) { - PyNewList sum = null; - if (o instanceof PySequenceList && !(o instanceof PyTuple)) { - if (o instanceof PyNewList) { - List oList = ((PyNewList) o).list; - List newList = new ArrayList(list.size() + oList.size()); - newList.addAll(list); - newList.addAll(oList); - sum = fromList(newList); - } - } else if (!(o instanceof PySequenceList)) { - // also support adding java lists (but not PyTuple!) - Object oList = o.__tojava__(List.class); - if (oList != Py.NoConversion && oList != null) { - List otherList = (List) oList; - sum = new PyNewList(); - sum.newlist_extend(this); - for (Iterator i = otherList.iterator(); i.hasNext();) { - sum.add(i.next()); - } - } - } - return sum; - } - - @Override - public PyObject __radd__(PyObject o) { - return newlist___radd__(o); - } - - //XXX: needs __doc__ - @ExposedMethod(type = MethodType.BINARY) - final PyObject newlist___radd__(PyObject o) { - // Support adding java.util.List, but prevent adding PyTuple. - // 'o' should never be a PyNewList since __add__ is defined. - PyNewList sum = null; - if (o instanceof PySequence) { - return null; - } - Object oList = o.__tojava__(List.class); - if (oList != Py.NoConversion && oList != null) { - sum = new PyNewList(); - sum.addAll((List) oList); - sum.extend(this); - } - return sum; - } - - @ExposedMethod(doc = BuiltinDocs.list___contains___doc) - final boolean newlist___contains__(PyObject o) { - return object___contains__(o); - } - - @ExposedMethod(doc = BuiltinDocs.list___delitem___doc) - final void newlist___delitem__(PyObject index) { - seq___delitem__(index); - } - - @ExposedMethod(doc = BuiltinDocs.list___setitem___doc) - final void newlist___setitem__(PyObject o, PyObject def) { - seq___setitem__(o, def); - } - - @ExposedMethod(doc = BuiltinDocs.list___getitem___doc) - final PyObject newlist___getitem__(PyObject o) { - PyObject ret = seq___finditem__(o); - if (ret == null) { - throw Py.IndexError("index out of range: " + o); - } - return ret; - } - - @Override - public PyObject __iter__() { - return newlist___iter__(); - } - - @ExposedMethod(doc = BuiltinDocs.list___iter___doc) - public PyObject newlist___iter__() { - return new PyFastSequenceIter(this); - } - - @ExposedMethod(defaults = "null", doc = BuiltinDocs.list___getslice___doc) - final PyObject newlist___getslice__(PyObject start, PyObject stop, PyObject step) { - return seq___getslice__(start, stop, step); - } - - @ExposedMethod(defaults = "null", doc = BuiltinDocs.list___setslice___doc) - final void newlist___setslice__(PyObject start, PyObject stop, PyObject step, PyObject value) { - seq___setslice__(start, stop, step, value); - } - - @ExposedMethod(defaults = "null", doc = BuiltinDocs.list___delslice___doc) - final void newlist___delslice__(PyObject start, PyObject stop, PyObject step) { - seq___delslice__(start, stop, step); - } - - @Override - protected String unsupportedopMessage(String op, PyObject o2) { - if (op.equals("+")) { - return "can only concatenate list (not \"{2}\") to list"; - } - return super.unsupportedopMessage(op, o2); - } - - public String toString() { - return newlist_toString(); - } - - //XXX: needs __doc__ - @ExposedMethod(names = "__repr__") - final String newlist_toString() { - ThreadState ts = Py.getThreadState(); - if (!ts.enterRepr(this)) { - return "[...]"; - } - StringBuilder buf = new StringBuilder("["); - int length = size(); - int i = 0; - for (PyObject item : list) { - buf.append(item.__repr__().toString()); - if (i < length - 1) { - buf.append(", "); - } - i++; - } - buf.append("]"); - ts.exitRepr(this); - return buf.toString(); - } - - /** - * Add a single element to the end of list. - * - * @param o - * the element to add. - */ - public void append(PyObject o) { - newlist_append(o); - } - - @ExposedMethod(doc = BuiltinDocs.list_append_doc) - final void newlist_append(PyObject o) { - pyadd(o); - gListAllocatedStatus = __len__(); - } - - /** - * 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) { - return newlist_count(o); - } - - @ExposedMethod(doc = BuiltinDocs.list_count_doc) - final int newlist_count(PyObject o) { - int count = 0; - PyObject[] array = getArray(); - for (int i = 0, n = size(); i < n; i++) { - if (array[i].equals(o)) { - count++; - } - } - return count; - } - - /** - * return smallest index where an element in the list equals the argument. - * - * @param o - * the argument to test for. Testing is done with the <code>==</code> operator. - */ - public int index(PyObject o) { - return index(o, 0); - } - - public int index(PyObject o, int start) { - return newlist_index(o, start, size()); - } - - public int index(PyObject o, int start, int stop) { - return newlist_index(o, start, stop); - } - - @ExposedMethod(defaults = {"null", "null"}, doc = BuiltinDocs.list_index_doc) - final int newlist_index(PyObject o, PyObject start, PyObject stop) { - int startInt = start == null ? 0 : PySlice.calculateSliceIndex(start); - int stopInt = stop == null ? size() : PySlice.calculateSliceIndex(stop); - return newlist_index(o, startInt, stopInt); - } - - final int newlist_index(PyObject o, int start, int stop) { - return _index(o, "list.index(x): x not in list", start, stop); - } - - final int newlist_index(PyObject o, int start) { - return _index(o, "list.index(x): x not in list", start, size()); - } - - final int newlist_index(PyObject o) { - return _index(o, "list.index(x): x not in list", 0, size()); - } - - private int _index(PyObject o, String message, int start, int stop) { - // Follow Python 2.3+ behavior - int validStop = boundToSequence(stop); - int validStart = boundToSequence(start); - PyObject[] array = getArray(); - for (int i = validStart; i < validStop && i < size(); i++) { - if (array[i].equals(o)) { - return i; - } - } - throw Py.ValueError(message); - } - - /** - * Insert the argument element into the list at the specified index. <br> - * Same as <code>s[index:index] = [o] if index >= 0</code>. - * - * @param index - * the position where the element will be inserted. - * @param o - * the element to insert. - */ - public void insert(int index, PyObject o) { - newlist_insert(index, o); - } - - @ExposedMethod(doc = BuiltinDocs.list_insert_doc) - final void newlist_insert(int index, PyObject o) { - if (index < 0) { - index = Math.max(0, size() + index); - } - if (index > size()) { - index = size(); - } - pyadd(index, o); - gListAllocatedStatus = __len__(); - } - - /** - * Remove the first occurence of the argument from the list. The elements arecompared with the - * <code>==</code> operator. <br> - * Same as <code>del s[s.index(x)]</code> - * - * @param o - * the element to search for and remove. - */ - public void remove(PyObject o) { - newlist_remove(o); - } - - @ExposedMethod(doc = BuiltinDocs.list_remove_doc) - final void newlist_remove(PyObject o) { - del(_index(o, "list.remove(x): x not in list", 0, size())); - gListAllocatedStatus = __len__(); - } - - /** - * Reverses the items of s in place. The reverse() methods modify the list in place for economy - * of space when reversing a large list. It doesn't return the reversed list to remind you of - * this side effect. - */ - public void reverse() { - newlist_reverse(); - } - - @ExposedMethod(doc = BuiltinDocs.list_reverse_doc) - final void newlist_reverse() { - Collections.reverse(list); - gListAllocatedStatus = __len__(); - } - - /** - * Removes and return the last element in the list. - */ - public PyObject pop() { - return pop(-1); - } - - /** - * Removes and return the <code>n</code> indexed element in the list. - * - * @param n - * the index of the element to remove and return. - */ - public PyObject pop(int n) { - return newlist_pop(n); - } - - @ExposedMethod(defaults = "-1", doc = BuiltinDocs.list_pop_doc) - final PyObject newlist_pop(int n) { - int length = size(); - if (length == 0) { - throw Py.IndexError("pop from empty list"); - } - if (n < 0) { - n += length; - } - if (n < 0 || n >= length) { - throw Py.IndexError("pop index out of range"); - } - PyObject v = list.remove(n); - return v; - } - - /** - * Append the elements in the argument sequence to the end of the list. <br> - * Same as <code>s[len(s):len(s)] = o</code>. - * - * @param o - * the sequence of items to append to the list. - */ - public void extend(PyObject o) { - newlist_extend(o); - } - - @ExposedMethod(doc = BuiltinDocs.list_extend_doc) - final void newlist_extend(PyObject o) { - if (o instanceof PyNewList) { - list.addAll(((PyNewList) o).list); - } else if (o instanceof PySequenceObjectList) { - PyObject other[] = ((PySequenceObjectList) o).getArray(); - for (int i = 0; i < other.length; i++) { - list.add(other[i]); - } - } else { - for (PyObject item : o.asIterable()) { - list.add(item); - } - } - gListAllocatedStatus = __len__(); - } - - @Override - public PyObject __iadd__(PyObject o) { - return newlist___iadd__(o); - } - - @ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.list___iadd___doc) - final PyObject newlist___iadd__(PyObject o) { - PyType oType = o.getType(); - if (oType == TYPE || oType == PyTuple.TYPE || this == o) { - extend(fastSequence(o, "argument must be iterable")); - return this; - } - - PyObject it; - try { - it = o.__iter__(); - } catch (PyException pye) { - if (!Py.matchException(pye, Py.TypeError)) { - throw pye; - } - return null; - } - extend(it); - return this; - } - - /** - * Sort the items of the list in place. The compare argument is a function of two arguments - * (list items) which should return -1, 0 or 1 depending on whether the first argument is - * considered smaller than, equal to, or larger than the second argument. Note that this slows - * the sorting process down considerably; e.g. to sort a list in reverse order it is much faster - * to use calls to the methods sort() and reverse() than to use the built-in function sort() - * with a comparison function that reverses the ordering of the elements. - * - * @param compare - * the comparison function. - */ - /** - * Sort the items of the list in place. Items is compared with the normal relative comparison - * operators. - */ - @ExposedMethod(doc = BuiltinDocs.list_sort_doc) - final void newlist_sort(PyObject[] args, String[] kwds) { - ArgParser ap = new ArgParser("list", args, kwds, new String[]{"cmp", "key", "reverse"}, 0); - PyObject cmp = ap.getPyObject(0, Py.None); - PyObject key = ap.getPyObject(1, Py.None); - PyObject reverse = ap.getPyObject(2, Py.False); - sort(cmp, key, reverse); - } - - public void sort(PyObject compare) { - sort(compare, Py.None, Py.False); - } - - public void sort() { - sort(Py.None, Py.None, Py.False); - } - - public void sort(PyObject cmp, PyObject key, PyObject reverse) { - MergeState ms = new MergeState(new PyList((Collection) this), cmp, key, reverse.__nonzero__()); - ms.sort(); - } - - public int hashCode() { - return newlist___hash__(); - } - - @ExposedMethod(doc = BuiltinDocs.list___hash___doc) - final int newlist___hash__() { - throw Py.TypeError(String.format("unhashable type: '%.200s'", getType().fastGetName())); - } - - @Override - public PyTuple __getnewargs__() { - return new PyTuple(new PyTuple(getArray())); - } - - @Override - public void add(int index, Object element) { - pyadd(index, Py.java2py(element)); - } - - @Override - public boolean add(Object o) { - pyadd(Py.java2py(o)); - return true; - } - - @Override - public boolean addAll(int index, Collection c) { - if (c instanceof PySequenceList) { - list.addAll(index, c); - } else { - // need to use add to convert anything pulled from a collection into a PyObject - for (Object element : c) { - add(element); - } - } - return c.size() > 0; - } - - @Override - public boolean addAll(Collection c) { - if (c instanceof PySequenceList) { - list.addAll(c); - } else { - // need to use add to convert anything pulled from a collection into a PyObject - for (Object element : c) { - add(element); - } - } - return c.size() > 0; - } - - @Override - public void clear() { - list.clear(); - } - - @Override - public boolean contains(Object o) { - return list.contains(Py.java2py(o)); - } - - @Override - public boolean containsAll(Collection c) { - if (c instanceof PySequenceList) { - return list.containsAll(c); - } else { - return list.containsAll(new PyList(c)); - } - } - - @Override - public boolean equals(Object o) { - if (o instanceof PyNewList) { - return (((PyNewList) o).list.equals(list)); - } - return false; - } - - @Override - public Object get(int index) { - return list.get(index).__tojava__(Object.class); - } - - /** @deprecated */ - @Override - public PyObject[] getArray() { - PyObject a[] = new PyObject[list.size()]; - return list.toArray(a); - } - - @Override - public int indexOf(Object o) { - return list.indexOf(o); - } - - @Override - public boolean isEmpty() { - return list.isEmpty(); - } - - @Override - public Iterator iterator() { - return list.iterator(); - } - - @Override - public int lastIndexOf(Object o) { - return list.lastIndexOf(o); - } - - @Override - public ListIterator listIterator() { - return list.listIterator(); - } - - @Override - public ListIterator listIterator(int index) { - return list.listIterator(index); - } - - @Override - public void pyadd(int index, PyObject element) { - list.add(index, element); - } - - @Override - public boolean pyadd(PyObject o) { - list.add(o); - return true; - } - - @Override - public PyObject pyget(int index) { - return list.get(index); - } - - public void pyset(int index, PyObject element) { - list.set(index, element); - } - - @Override - public Object remove(int index) { - return list.remove(index); - } - - @Override - public void remove(int start, int stop) { - list.subList(start, stop).clear(); - } - - @Override - public boolean removeAll(Collection c) { - if (c instanceof PySequenceList) { - return list.removeAll(c); - } else { - return list.removeAll(new PyNewList(c)); - } - } - - @Override - public boolean retainAll(Collection c) { - if (c instanceof PySequenceList) { - return list.retainAll(c); - } else { - return list.retainAll(new PyNewList(c)); - } - } - - @Override - public Object set(int index, Object element) { - return list.set(index, Py.java2py(element)).__tojava__(Object.class); - } - - @Override - public int size() { - return list.size(); - } - - @Override - public List subList(int fromIndex, int toIndex) { - return list.subList(fromIndex, toIndex); - } - - @Override - public Object[] toArray() { - return list.toArray(); - } - - @Override - public Object[] toArray(Object[] a) { - return list.toArray(a); - } - - protected PyObject getslice(int start, int stop, int step) { - if (step > 0 && stop < start) { - stop = start; - } - int n = sliceLength(start, stop, step); - List newList; - if (step == 1) { - newList = new ArrayList<PyObject>(list.subList(start, stop)); - } else { - newList = new ArrayList<PyObject>(n); - for (int i = start, j = 0; j < n; i += step, j++) { - newList.add(list.get(i)); - } - } - return fromList(newList); - } - - @Override - public boolean remove(Object o) { - return list.remove(Py.java2py(o)); - } -} Modified: branches/newlist/src/org/python/core/PyObject.java =================================================================== --- branches/newlist/src/org/python/core/PyObject.java 2009-04-08 06:48:21 UTC (rev 6190) +++ branches/newlist/src/org/python/core/PyObject.java 2009-04-08 19:50:54 UTC (rev 6191) @@ -20,7 +20,7 @@ * of the class <code>PyObject</code> or one of its subclasses. */ @ExposedType(name = "object") -public class PyObject implements Serializable { +public class PyObject implements Comparable<PyObject>, Serializable { public static final PyType TYPE = PyType.fromClass(PyObject.class); @@ -1284,6 +1284,10 @@ } } + public int compareTo(PyObject o) { + return this._cmp(o); + } + private PyObject make_pair(PyObject o) { if (System.identityHashCode(this) < System.identityHashCode(o)) return new PyIdentityTuple(new PyObject[] { this, o }); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <fwi...@us...> - 2009-04-08 21:45:41
|
Revision: 6192 http://jython.svn.sourceforge.net/jython/?rev=6192&view=rev Author: fwierzbicki Date: 2009-04-08 21:45:35 +0000 (Wed, 08 Apr 2009) Log Message: ----------- Added a Comparator implementation for sorting. Removed Comparable interface from PyObject since it wasn't there before and I don't appear to need it yet. Modified Paths: -------------- branches/newlist/src/org/python/core/PyList.java branches/newlist/src/org/python/core/PyObject.java Added Paths: ----------- branches/newlist/src/org/python/core/PyComparator.java Added: branches/newlist/src/org/python/core/PyComparator.java =================================================================== --- branches/newlist/src/org/python/core/PyComparator.java (rev 0) +++ branches/newlist/src/org/python/core/PyComparator.java 2009-04-08 21:45:35 UTC (rev 6192) @@ -0,0 +1,48 @@ +package org.python.core; + +import java.util.Comparator; + +public class PyComparator implements Comparator<PyObject> { + + protected PyObject cmp = null; + protected PyObject key = null; + protected boolean reverse = false; + + PyComparator(PyObject cmp, PyObject key, boolean reverse) { + this.cmp = cmp; + this.key = key; + this.reverse = reverse; + } + + // First cut at an implementation. FIXME: In CPython key is supposed to + // make things fast, accessing each element once. For this first cut I am + // cheating and calling the key function on every pass to get something + // that works right away. + public int compare(PyObject o1, PyObject o2) { + int result; + if (key != null && key != Py.None) { + o1 = key.__call__(o1); + o2 = key.__call__(o2); + } + if (cmp != null && cmp != Py.None) { + result = cmp.__call__(o1, o2).asInt(); + } else { + result = o1._cmp(o2); + } + if (reverse) { + return -result; + } + return result; + } + + public boolean equals(Object o) { + if(o == this) { + return true; + } + + if (o instanceof PyComparator) { + return key.equals(((PyComparator)o).key) && cmp.equals(((PyComparator)o).cmp); + } + return false; + } +} Modified: branches/newlist/src/org/python/core/PyList.java =================================================================== --- branches/newlist/src/org/python/core/PyList.java 2009-04-08 19:50:54 UTC (rev 6191) +++ branches/newlist/src/org/python/core/PyList.java 2009-04-08 21:45:35 UTC (rev 6192) @@ -684,7 +684,8 @@ //System.out.println("sorting with " + cmp + ":" + key + ":" + reverse); //MergeState ms = new MergeState(this, cmp, key, reverse.__nonzero__()); //ms.sort(); - Collections.sort(list); + PyComparator c = new PyComparator(cmp, key, reverse.__nonzero__()); + Collections.sort(list, c); } public int hashCode() { Modified: branches/newlist/src/org/python/core/PyObject.java =================================================================== --- branches/newlist/src/org/python/core/PyObject.java 2009-04-08 19:50:54 UTC (rev 6191) +++ branches/newlist/src/org/python/core/PyObject.java 2009-04-08 21:45:35 UTC (rev 6192) @@ -20,7 +20,7 @@ * of the class <code>PyObject</code> or one of its subclasses. */ @ExposedType(name = "object") -public class PyObject implements Comparable<PyObject>, Serializable { +public class PyObject implements Serializable { public static final PyType TYPE = PyType.fromClass(PyObject.class); @@ -1284,10 +1284,6 @@ } } - public int compareTo(PyObject o) { - return this._cmp(o); - } - private PyObject make_pair(PyObject o) { if (System.identityHashCode(this) < System.identityHashCode(o)) return new PyIdentityTuple(new PyObject[] { this, o }); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <fwi...@us...> - 2009-04-10 02:22:12
|
Revision: 6195 http://jython.svn.sourceforge.net/jython/?rev=6195&view=rev Author: fwierzbicki Date: 2009-04-10 02:22:11 +0000 (Fri, 10 Apr 2009) Log Message: ----------- Throw exception on mutate during sort. Modified Paths: -------------- branches/newlist/src/org/python/core/PyComparator.java branches/newlist/src/org/python/core/PyList.java Modified: branches/newlist/src/org/python/core/PyComparator.java =================================================================== --- branches/newlist/src/org/python/core/PyComparator.java 2009-04-10 01:09:14 UTC (rev 6194) +++ branches/newlist/src/org/python/core/PyComparator.java 2009-04-10 02:22:11 UTC (rev 6195) @@ -4,11 +4,13 @@ public class PyComparator implements Comparator<PyObject> { - protected PyObject cmp = null; - protected PyObject key = null; + protected PyList list; + protected PyObject cmp; + protected PyObject key; protected boolean reverse = false; - PyComparator(PyObject cmp, PyObject key, boolean reverse) { + PyComparator(PyList list, PyObject cmp, PyObject key, boolean reverse) { + this.list = list; this.cmp = cmp; this.key = key; this.reverse = reverse; @@ -32,6 +34,9 @@ if (reverse) { return -result; } + if (this.list.gListAllocatedStatus >= 0) { + throw Py.ValueError("list modified during sort"); + } return result; } Modified: branches/newlist/src/org/python/core/PyList.java =================================================================== --- branches/newlist/src/org/python/core/PyList.java 2009-04-10 01:09:14 UTC (rev 6194) +++ branches/newlist/src/org/python/core/PyList.java 2009-04-10 02:22:11 UTC (rev 6195) @@ -680,12 +680,11 @@ sort(Py.None, Py.None, Py.False); } - public void sort(PyObject cmp, PyObject key, PyObject reverse) { - //System.out.println("sorting with " + cmp + ":" + key + ":" + reverse); - //MergeState ms = new MergeState(this, cmp, key, reverse.__nonzero__()); - //ms.sort(); - PyComparator c = new PyComparator(cmp, key, reverse.__nonzero__()); + public synchronized void sort(PyObject cmp, PyObject key, PyObject reverse) { + gListAllocatedStatus = -1; + PyComparator c = new PyComparator(this, cmp, key, reverse.__nonzero__()); Collections.sort(list, c); + gListAllocatedStatus = __len__(); } public int hashCode() { This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <zy...@us...> - 2009-04-11 07:03:00
|
Revision: 6217 http://jython.svn.sourceforge.net/jython/?rev=6217&view=rev Author: zyasoft Date: 2009-04-11 07:02:56 +0000 (Sat, 11 Apr 2009) Log Message: ----------- Fixed PyTuple#index, #toArray Modified Paths: -------------- branches/newlist/src/org/python/core/PyList.java branches/newlist/src/org/python/core/PyTuple.java Modified: branches/newlist/src/org/python/core/PyList.java =================================================================== --- branches/newlist/src/org/python/core/PyList.java 2009-04-11 05:58:57 UTC (rev 6216) +++ branches/newlist/src/org/python/core/PyList.java 2009-04-11 07:02:56 UTC (rev 6217) @@ -11,6 +11,7 @@ import java.util.Collection; import java.util.Collections; +import java.util.ConcurrentModificationException; import java.util.Iterator; import java.util.List; import java.util.ListIterator; @@ -510,12 +511,18 @@ // Follow Python 2.3+ behavior int validStop = boundToSequence(stop); int validStart = boundToSequence(start); - int i = 0; - for (PyObject item : list.subList(validStart, validStop)) { - if (item.equals(o)) { - return i; + int i = validStart; + if (validStart <= validStop) { + try { + for (PyObject item : list.subList(validStart, validStop)) { + if (item.equals(o)) { + return i; + } + i++; + } + } catch (ConcurrentModificationException ex) { + throw Py.ValueError(message); } - i++; } throw Py.ValueError(message); } Modified: branches/newlist/src/org/python/core/PyTuple.java =================================================================== --- branches/newlist/src/org/python/core/PyTuple.java 2009-04-11 05:58:57 UTC (rev 6216) +++ branches/newlist/src/org/python/core/PyTuple.java 2009-04-11 07:02:56 UTC (rev 6217) @@ -541,12 +541,26 @@ @Override public Object[] toArray() { - return array; + Object[] converted = new Object[array.length]; + for (int i = 0; i < array.length; i++) { + converted[i] = array[i].__tojava__(Object.class); + } + return converted; } @Override - public Object[] toArray(Object[] a) { - System.arraycopy(array, 0, a, 0, array.length); - return a; + public Object[] toArray(Object[] converted) { + if (converted.length != array.length) { + converted = new Object[array.length]; + } + for (int i = 0; i < array.length; i++) { + converted[i] = array[i].__tojava__(Object.class); + } + if (array.length < converted.length) { + for (int i = array.length; i < converted.length; i++) { + converted[i] = null; + } + } + return converted; } } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <zy...@us...> - 2009-04-13 17:12:04
|
Revision: 6223 http://jython.svn.sourceforge.net/jython/?rev=6223&view=rev Author: zyasoft Date: 2009-04-13 17:12:00 +0000 (Mon, 13 Apr 2009) Log Message: ----------- Changed protocol from SequenceIndexDelegate such that PyList#setslice is ultimately called with the sliceLength and valueLength, or -1 for those fields. This enables access to whether extended slicing was used or not. Modified Paths: -------------- branches/newlist/src/org/python/core/AstList.java branches/newlist/src/org/python/core/PyArray.java branches/newlist/src/org/python/core/PyJavaType.java branches/newlist/src/org/python/core/PyList.java branches/newlist/src/org/python/core/PySequence.java branches/newlist/src/org/python/core/SequenceIndexDelegate.java Modified: branches/newlist/src/org/python/core/AstList.java =================================================================== --- branches/newlist/src/org/python/core/AstList.java 2009-04-12 02:14:00 UTC (rev 6222) +++ branches/newlist/src/org/python/core/AstList.java 2009-04-13 17:12:00 UTC (rev 6223) @@ -361,7 +361,7 @@ @ExposedMethod final void astlist_extend(PyObject iterable){ int length = size(); - setslice(length, length, 1, iterable); + setslice(length, length, 1, iterable,-1,-1); } public void extend(PyObject iterable) { @@ -454,7 +454,7 @@ return new AstList(newList); } - protected void setslice(int start, int stop, int step, PyObject value) { + protected void setslice(int start, int stop, int step, PyObject value,int sliceLength,int valueLength) { if(stop < start) { stop = start; } Modified: branches/newlist/src/org/python/core/PyArray.java =================================================================== --- branches/newlist/src/org/python/core/PyArray.java 2009-04-12 02:14:00 UTC (rev 6222) +++ branches/newlist/src/org/python/core/PyArray.java 2009-04-13 17:12:00 UTC (rev 6223) @@ -1528,7 +1528,7 @@ * @param step * stepping increment of the slice */ - protected void setslice(int start, int stop, int step, PyObject value) { + protected void setslice(int start, int stop, int step, PyObject value,int sliceLength,int valueLength) { if (stop < start) { stop = start; } Modified: branches/newlist/src/org/python/core/PyJavaType.java =================================================================== --- branches/newlist/src/org/python/core/PyJavaType.java 2009-04-12 02:14:00 UTC (rev 6222) +++ branches/newlist/src/org/python/core/PyJavaType.java 2009-04-13 17:12:00 UTC (rev 6223) @@ -877,7 +877,7 @@ } @Override - public void setSlice(int start, int stop, int step, PyObject value) { + public void setSlice(int start, int stop, int step, PyObject value, int sliceLength,int valueLength) { if (stop < start) { stop = start; } Modified: branches/newlist/src/org/python/core/PyList.java =================================================================== --- branches/newlist/src/org/python/core/PyList.java 2009-04-12 02:14:00 UTC (rev 6222) +++ branches/newlist/src/org/python/core/PyList.java 2009-04-13 17:12:00 UTC (rev 6223) @@ -20,7 +20,7 @@ public class PyList extends PySequenceList implements List { public static final PyType TYPE = PyType.fromClass(PyList.class); - protected final List<PyObject> list; + protected List<PyObject> list; public PyList() { this(TYPE); @@ -126,21 +126,76 @@ } @Override - protected void setslice(int start, int stop, int step, PyObject value) { + protected void setslice(int start, int stop, int step, PyObject value, int sliceLength, int valueLength) { if (stop < start) { stop = start; } - if (value instanceof PySequence) { - if (value == this) { // copy - value = new PyList((PySequence) value); + int size = list.size(); +// System.err.println("start=" + start + ",stop=" + stop + ",step=" + step + ",sliceLength=" + sliceLength); + + + // optimize copy into: x[:] = ... + if (start == 0 && stop == size && step == 1) { // x[:] = ... + Iterator<PyObject> src; + if (value == this) { + src = ((PyList) value).safeIterator(); + } else if (value instanceof PySequenceList) { + src = ((PySequenceList) value).iterator(); + } else { + src = value.asIterable().iterator(); } - setsliceIterator(start, stop, step, value.asIterable().iterator()); - } else if (value != null && !(value instanceof List)) { - //XXX: can we avoid copying here? Needed to pass test_userlist - value = new PyList(value); - setsliceIterator(start, stop, step, value.asIterable().iterator()); - } else { - System.err.println("List"); + List<PyObject> copy = new ArrayList<PyObject>(valueLength > 0 ? valueLength : 4); + if (src != null) { + while (src.hasNext()) { + copy.add(src.next()); + } + } + list = copy; + return; + } + + if (value instanceof PyObject) { + Iterator<PyObject> src; + if (value == this) { + PyList valueList = (PyList) value; + if (start == 0 && stop == 0 && step == -1) { // x[::-1] = x + valueList.reverse(); + return; + } + src = ((PyList) value).safeIterator(); + } else if (value instanceof PySequenceList) { + src = ((PySequenceList) value).iterator(); + } else if (value instanceof PySequence) { + src = value.asIterable().iterator(); + } else { + src = new PyList(value).iterator(); + } + + if (sliceLength != -1) { + int srcRange = stop - start; + int destRange = step * sliceLength; + +// System.err.println("start=" + start + ",stop=" + stop + ",step=" + step + +// ",sliceLength=" + sliceLength + ",srcRange=" + srcRange + ",destRange=" + destRange); + + if ((destRange >= srcRange && srcRange > destRange - step)) { + setsliceSimple(start, stop, step, src); + return; + } + } + if (stop == start && step < 0) { + setsliceSimple(start, stop, step, src); + } else { + setsliceIterator(start, stop, src); + } + + } // else if (value != null && !(value instanceof List)) { + // //XXX: can we avoid copying here? Needed to pass test_userlist + // value = new PyList(value); + // setsliceIterator(start, stop, value.asIterable().iterator()); + // } + else { +// System.err.println("List"); List valueList = (List) value.__tojava__(List.class); if (valueList != null && valueList != Py.NoConversion) { setsliceList(start, stop, step, valueList); @@ -148,40 +203,53 @@ } } - protected void setsliceList(int start, int stop, int step, List value) { - int n = sliceLength(start, stop, step); - if (list instanceof ArrayList) { - ((ArrayList) list).ensureCapacity(start + n); + private final Iterator<PyObject> safeIterator() { + return new Iterator<PyObject>() { + + private int i = 0; + private final PyObject elements[] = getArray(); + + public boolean hasNext() { + return (i < elements.length); + } + + public PyObject next() { + return elements[i++]; + } + + public void remove() { + throw new UnsupportedOperationException("Immutable"); + } + }; + } + + private final void setsliceSimple(int start, int stop, int step, Iterator<PyObject> src) { + for (int i = start; src.hasNext(); i += step) { + list.set(i, src.next()); } + } + + // XXX needs to follow below logic for setsliceIterator + private final void setsliceList(int start, int stop, int step, List value) { ListIterator src = value.listIterator(); for (int j = start; src.hasNext(); j += step) { set(j, src.next()); } } - protected void setsliceIterator(int start, int stop, int step, Iterator<PyObject> iter) { - if(step == 1) { - List<PyObject> copy = new ArrayList<PyObject>(); - copy.addAll(this.list.subList(0, start)); - if (iter != null) { - while (iter.hasNext()) { - copy.add(iter.next()); - } + + // XXX need to support prepending via ops like x[:0] = whatever; + // note that step must equal 1 in this case + private final void setsliceIterator(int start, int stop, Iterator<PyObject> iter) { + List<PyObject> copy = new ArrayList<PyObject>(); + copy.addAll(this.list.subList(0, start)); + if (iter != null) { + while (iter.hasNext()) { + copy.add(iter.next()); } - copy.addAll(this.list.subList(stop, this.list.size())); - this.list.clear(); - this.list.addAll(copy); - } else { - int size = list.size(); - for (int j = start; iter.hasNext(); j += step) { - PyObject item = iter.next(); - if (j >= size) { - list.add(item); - } else { - list.set(j, item); - } - } } + copy.addAll(this.list.subList(stop, this.list.size())); + this.list = copy; } @Override @@ -786,7 +854,7 @@ public boolean equals(Object o) { if (o instanceof PyList) { return (((PyList) o).list.equals(list)); - } else if(o instanceof List) { // XXX copied from PyList, but... + } else if (o instanceof List) { // XXX copied from PyList, but... return o.equals(this); // XXX shouldn't this compare using py2java? } return false; @@ -797,7 +865,6 @@ return list.get(index).__tojava__(Object.class); } - /** @deprecated */ @Override public PyObject[] getArray() { PyObject a[] = new PyObject[list.size()]; @@ -924,6 +991,27 @@ return fromList(newList); } + // NOTE: this attempt would seem faster, but the JVM is better optimized for + // the ArrayList implementation in limited testing + // keeping around for now... +// protected PyObject getslice(int start, int stop, int step) { +// if (step > 0 && stop < start) { +// stop = start; +// } +// int n = sliceLength(start, stop, step); +// PyObject elements[] = new PyObject[n]; +// if (step == 1) { +// ListIterator<PyObject> iter = list.listIterator(start); +// for (int i = 0; i < n; i++) { +// elements[i] = iter.next(); +// } +// } else { +// for (int i = start, j = 0; j < n; i += step, j++) { +// elements[j] = list.get(i); +// } +// } +// return new PyList(elements); +// } @Override public boolean remove(Object o) { return list.remove(Py.java2py(o)); Modified: branches/newlist/src/org/python/core/PySequence.java =================================================================== --- branches/newlist/src/org/python/core/PySequence.java 2009-04-12 02:14:00 UTC (rev 6222) +++ branches/newlist/src/org/python/core/PySequence.java 2009-04-13 17:12:00 UTC (rev 6223) @@ -67,7 +67,7 @@ /** * Sets the given range of elements. */ - protected void setslice(int start, int stop, int step, PyObject value) { + protected void setslice(int start, int stop, int step, PyObject value,int sliceLength,int valueLength) { throw Py.TypeError(String.format("'%s' object does not support item assignment", getType().fastGetName())); } @@ -400,8 +400,8 @@ } @Override - public void setSlice(int start, int stop, int step, PyObject value) { - setslice(start, stop, step, value); + public void setSlice(int start, int stop, int step, PyObject value, int sliceLength,int valueLength) { + setslice(start, stop, step, value, sliceLength, valueLength); } @Override Modified: branches/newlist/src/org/python/core/SequenceIndexDelegate.java =================================================================== --- branches/newlist/src/org/python/core/SequenceIndexDelegate.java 2009-04-12 02:14:00 UTC (rev 6222) +++ branches/newlist/src/org/python/core/SequenceIndexDelegate.java 2009-04-13 17:12:00 UTC (rev 6223) @@ -17,7 +17,7 @@ public abstract PyObject getSlice(int start, int stop, int step); - public abstract void setSlice(int start, int stop, int step, PyObject value); + public abstract void setSlice(int start, int stop, int step, PyObject value, int sliceLength, int valueLength); public abstract void delItems(int start, int stop); @@ -35,11 +35,29 @@ public void checkIdxAndSetSlice(PySlice slice, PyObject value) { int[] indices = slice.indicesEx(len()); - if ((slice.step != Py.None) && value.__len__() != indices[3]) { + final int valueLength; + if (slice.step != Py.None) { + valueLength = value.__len__(); + if (valueLength != indices[3]) { throw Py.ValueError(String.format("attempt to assign sequence of size %d to extended " + "slice of size %d", value.__len__(), indices[3])); + } + } else { + valueLength = -1; } - setSlice(indices[0], indices[1], indices[2], value); + + final int sliceLength; + // this enables PyList to use a simpler implementation to avoid List.add for inserts + if (slice.step == Py.None) { + if (valueLength == indices[3]) { + sliceLength = valueLength; + } else { + sliceLength = -1; + } + } else { + sliceLength = valueLength; + } + setSlice(indices[0], indices[1], indices[2], value, sliceLength, valueLength); } public void checkIdxAndSetItem(int idx, PyObject value) { This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <zy...@us...> - 2009-04-15 05:59:10
|
Revision: 6227 http://jython.svn.sourceforge.net/jython/?rev=6227&view=rev Author: zyasoft Date: 2009-04-15 05:58:59 +0000 (Wed, 15 Apr 2009) Log Message: ----------- Reverted to r6222 Modified Paths: -------------- branches/newlist/src/org/python/core/AstList.java branches/newlist/src/org/python/core/PyArray.java branches/newlist/src/org/python/core/PyJavaType.java branches/newlist/src/org/python/core/PyList.java branches/newlist/src/org/python/core/PySequence.java branches/newlist/src/org/python/core/SequenceIndexDelegate.java Modified: branches/newlist/src/org/python/core/AstList.java =================================================================== --- branches/newlist/src/org/python/core/AstList.java 2009-04-14 23:00:40 UTC (rev 6226) +++ branches/newlist/src/org/python/core/AstList.java 2009-04-15 05:58:59 UTC (rev 6227) @@ -361,7 +361,7 @@ @ExposedMethod final void astlist_extend(PyObject iterable){ int length = size(); - setslice(length, length, 1, iterable,-1,-1); + setslice(length, length, 1, iterable); } public void extend(PyObject iterable) { @@ -454,7 +454,7 @@ return new AstList(newList); } - protected void setslice(int start, int stop, int step, PyObject value,int sliceLength,int valueLength) { + protected void setslice(int start, int stop, int step, PyObject value) { if(stop < start) { stop = start; } Modified: branches/newlist/src/org/python/core/PyArray.java =================================================================== --- branches/newlist/src/org/python/core/PyArray.java 2009-04-14 23:00:40 UTC (rev 6226) +++ branches/newlist/src/org/python/core/PyArray.java 2009-04-15 05:58:59 UTC (rev 6227) @@ -1528,7 +1528,7 @@ * @param step * stepping increment of the slice */ - protected void setslice(int start, int stop, int step, PyObject value,int sliceLength,int valueLength) { + protected void setslice(int start, int stop, int step, PyObject value) { if (stop < start) { stop = start; } Modified: branches/newlist/src/org/python/core/PyJavaType.java =================================================================== --- branches/newlist/src/org/python/core/PyJavaType.java 2009-04-14 23:00:40 UTC (rev 6226) +++ branches/newlist/src/org/python/core/PyJavaType.java 2009-04-15 05:58:59 UTC (rev 6227) @@ -877,7 +877,7 @@ } @Override - public void setSlice(int start, int stop, int step, PyObject value, int sliceLength,int valueLength) { + public void setSlice(int start, int stop, int step, PyObject value) { if (stop < start) { stop = start; } Modified: branches/newlist/src/org/python/core/PyList.java =================================================================== --- branches/newlist/src/org/python/core/PyList.java 2009-04-14 23:00:40 UTC (rev 6226) +++ branches/newlist/src/org/python/core/PyList.java 2009-04-15 05:58:59 UTC (rev 6227) @@ -20,7 +20,7 @@ public class PyList extends PySequenceList implements List { public static final PyType TYPE = PyType.fromClass(PyList.class); - protected List<PyObject> list; + protected final List<PyObject> list; public PyList() { this(TYPE); @@ -126,76 +126,21 @@ } @Override - protected void setslice(int start, int stop, int step, PyObject value, int sliceLength, int valueLength) { + protected void setslice(int start, int stop, int step, PyObject value) { if (stop < start) { stop = start; } - int size = list.size(); -// System.err.println("start=" + start + ",stop=" + stop + ",step=" + step + ",sliceLength=" + sliceLength); - - - // optimize copy into: x[:] = ... - if (start == 0 && stop == size && step == 1) { // x[:] = ... - Iterator<PyObject> src; - if (value == this) { - src = ((PyList) value).safeIterator(); - } else if (value instanceof PySequenceList) { - src = ((PySequenceList) value).iterator(); - } else { - src = value.asIterable().iterator(); + if (value instanceof PySequence) { + if (value == this) { // copy + value = new PyList((PySequence) value); } - List<PyObject> copy = new ArrayList<PyObject>(valueLength > 0 ? valueLength : 4); - if (src != null) { - while (src.hasNext()) { - copy.add(src.next()); - } - } - list = copy; - return; - } - - if (value instanceof PyObject) { - Iterator<PyObject> src; - if (value == this) { - PyList valueList = (PyList) value; - if (start == 0 && stop == 0 && step == -1) { // x[::-1] = x - valueList.reverse(); - return; - } - src = ((PyList) value).safeIterator(); - } else if (value instanceof PySequenceList) { - src = ((PySequenceList) value).iterator(); - } else if (value instanceof PySequence) { - src = value.asIterable().iterator(); - } else { - src = new PyList(value).iterator(); - } - - if (sliceLength != -1) { - int srcRange = stop - start; - int destRange = step * sliceLength; - -// System.err.println("start=" + start + ",stop=" + stop + ",step=" + step + -// ",sliceLength=" + sliceLength + ",srcRange=" + srcRange + ",destRange=" + destRange); - - if ((destRange >= srcRange && srcRange > destRange - step)) { - setsliceSimple(start, stop, step, src); - return; - } - } - if (stop == start && step < 0) { - setsliceSimple(start, stop, step, src); - } else { - setsliceIterator(start, stop, src); - } - - } // else if (value != null && !(value instanceof List)) { - // //XXX: can we avoid copying here? Needed to pass test_userlist - // value = new PyList(value); - // setsliceIterator(start, stop, value.asIterable().iterator()); - // } - else { -// System.err.println("List"); + setsliceIterator(start, stop, step, value.asIterable().iterator()); + } else if (value != null && !(value instanceof List)) { + //XXX: can we avoid copying here? Needed to pass test_userlist + value = new PyList(value); + setsliceIterator(start, stop, step, value.asIterable().iterator()); + } else { + System.err.println("List"); List valueList = (List) value.__tojava__(List.class); if (valueList != null && valueList != Py.NoConversion) { setsliceList(start, stop, step, valueList); @@ -203,53 +148,40 @@ } } - private final Iterator<PyObject> safeIterator() { - return new Iterator<PyObject>() { - - private int i = 0; - private final PyObject elements[] = getArray(); - - public boolean hasNext() { - return (i < elements.length); - } - - public PyObject next() { - return elements[i++]; - } - - public void remove() { - throw new UnsupportedOperationException("Immutable"); - } - }; - } - - private final void setsliceSimple(int start, int stop, int step, Iterator<PyObject> src) { - for (int i = start; src.hasNext(); i += step) { - list.set(i, src.next()); + protected void setsliceList(int start, int stop, int step, List value) { + int n = sliceLength(start, stop, step); + if (list instanceof ArrayList) { + ((ArrayList) list).ensureCapacity(start + n); } - } - - // XXX needs to follow below logic for setsliceIterator - private final void setsliceList(int start, int stop, int step, List value) { ListIterator src = value.listIterator(); for (int j = start; src.hasNext(); j += step) { set(j, src.next()); } } - - // XXX need to support prepending via ops like x[:0] = whatever; - // note that step must equal 1 in this case - private final void setsliceIterator(int start, int stop, Iterator<PyObject> iter) { - List<PyObject> copy = new ArrayList<PyObject>(); - copy.addAll(this.list.subList(0, start)); - if (iter != null) { - while (iter.hasNext()) { - copy.add(iter.next()); + protected void setsliceIterator(int start, int stop, int step, Iterator<PyObject> iter) { + if(step == 1) { + List<PyObject> copy = new ArrayList<PyObject>(); + copy.addAll(this.list.subList(0, start)); + if (iter != null) { + while (iter.hasNext()) { + copy.add(iter.next()); + } } + copy.addAll(this.list.subList(stop, this.list.size())); + this.list.clear(); + this.list.addAll(copy); + } else { + int size = list.size(); + for (int j = start; iter.hasNext(); j += step) { + PyObject item = iter.next(); + if (j >= size) { + list.add(item); + } else { + list.set(j, item); + } + } } - copy.addAll(this.list.subList(stop, this.list.size())); - this.list = copy; } @Override @@ -854,7 +786,7 @@ public boolean equals(Object o) { if (o instanceof PyList) { return (((PyList) o).list.equals(list)); - } else if (o instanceof List) { // XXX copied from PyList, but... + } else if(o instanceof List) { // XXX copied from PyList, but... return o.equals(this); // XXX shouldn't this compare using py2java? } return false; @@ -865,6 +797,7 @@ return list.get(index).__tojava__(Object.class); } + /** @deprecated */ @Override public PyObject[] getArray() { PyObject a[] = new PyObject[list.size()]; @@ -991,27 +924,6 @@ return fromList(newList); } - // NOTE: this attempt would seem faster, but the JVM is better optimized for - // the ArrayList implementation in limited testing - // keeping around for now... -// protected PyObject getslice(int start, int stop, int step) { -// if (step > 0 && stop < start) { -// stop = start; -// } -// int n = sliceLength(start, stop, step); -// PyObject elements[] = new PyObject[n]; -// if (step == 1) { -// ListIterator<PyObject> iter = list.listIterator(start); -// for (int i = 0; i < n; i++) { -// elements[i] = iter.next(); -// } -// } else { -// for (int i = start, j = 0; j < n; i += step, j++) { -// elements[j] = list.get(i); -// } -// } -// return new PyList(elements); -// } @Override public boolean remove(Object o) { return list.remove(Py.java2py(o)); Modified: branches/newlist/src/org/python/core/PySequence.java =================================================================== --- branches/newlist/src/org/python/core/PySequence.java 2009-04-14 23:00:40 UTC (rev 6226) +++ branches/newlist/src/org/python/core/PySequence.java 2009-04-15 05:58:59 UTC (rev 6227) @@ -67,7 +67,7 @@ /** * Sets the given range of elements. */ - protected void setslice(int start, int stop, int step, PyObject value,int sliceLength,int valueLength) { + protected void setslice(int start, int stop, int step, PyObject value) { throw Py.TypeError(String.format("'%s' object does not support item assignment", getType().fastGetName())); } @@ -400,8 +400,8 @@ } @Override - public void setSlice(int start, int stop, int step, PyObject value, int sliceLength,int valueLength) { - setslice(start, stop, step, value, sliceLength, valueLength); + public void setSlice(int start, int stop, int step, PyObject value) { + setslice(start, stop, step, value); } @Override Modified: branches/newlist/src/org/python/core/SequenceIndexDelegate.java =================================================================== --- branches/newlist/src/org/python/core/SequenceIndexDelegate.java 2009-04-14 23:00:40 UTC (rev 6226) +++ branches/newlist/src/org/python/core/SequenceIndexDelegate.java 2009-04-15 05:58:59 UTC (rev 6227) @@ -17,7 +17,7 @@ public abstract PyObject getSlice(int start, int stop, int step); - public abstract void setSlice(int start, int stop, int step, PyObject value, int sliceLength, int valueLength); + public abstract void setSlice(int start, int stop, int step, PyObject value); public abstract void delItems(int start, int stop); @@ -35,29 +35,11 @@ public void checkIdxAndSetSlice(PySlice slice, PyObject value) { int[] indices = slice.indicesEx(len()); - final int valueLength; - if (slice.step != Py.None) { - valueLength = value.__len__(); - if (valueLength != indices[3]) { + if ((slice.step != Py.None) && value.__len__() != indices[3]) { throw Py.ValueError(String.format("attempt to assign sequence of size %d to extended " + "slice of size %d", value.__len__(), indices[3])); - } - } else { - valueLength = -1; } - - final int sliceLength; - // this enables PyList to use a simpler implementation to avoid List.add for inserts - if (slice.step == Py.None) { - if (valueLength == indices[3]) { - sliceLength = valueLength; - } else { - sliceLength = -1; - } - } else { - sliceLength = valueLength; - } - setSlice(indices[0], indices[1], indices[2], value, sliceLength, valueLength); + setSlice(indices[0], indices[1], indices[2], value); } public void checkIdxAndSetItem(int idx, PyObject value) { This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <zy...@us...> - 2009-04-16 06:18:48
|
Revision: 6231 http://jython.svn.sourceforge.net/jython/?rev=6231&view=rev Author: zyasoft Date: 2009-04-16 06:18:40 +0000 (Thu, 16 Apr 2009) Log Message: ----------- Fixed JavaImportHelper#getFromListAsStrings, which was assuming that the PyTuple it was iterating over would contain String, not PyString. Now allow both, just in case, since either should be applicable anyway. Modified Paths: -------------- branches/newlist/src/org/python/core/JavaImportHelper.java branches/newlist/src/org/python/core/PyList.java branches/newlist/src/org/python/core/packagecache/PackageManager.java Modified: branches/newlist/src/org/python/core/JavaImportHelper.java =================================================================== --- branches/newlist/src/org/python/core/JavaImportHelper.java 2009-04-16 05:48:35 UTC (rev 6230) +++ branches/newlist/src/org/python/core/JavaImportHelper.java 2009-04-16 06:18:40 UTC (rev 6231) @@ -120,6 +120,9 @@ Iterator iterator = ((PyTuple) fromlist).iterator(); while (iterator.hasNext()) { Object obj = iterator.next(); + if (obj instanceof PyString) { + obj = ((PyString)obj).string; + } if (obj instanceof String) { String fromName = (String) obj; if (!"*".equals(fromName)) { Modified: branches/newlist/src/org/python/core/PyList.java =================================================================== --- branches/newlist/src/org/python/core/PyList.java 2009-04-16 05:48:35 UTC (rev 6230) +++ branches/newlist/src/org/python/core/PyList.java 2009-04-16 06:18:40 UTC (rev 6231) @@ -142,7 +142,7 @@ value = new PyList(value); setsliceIterator(start, stop, step, value.asIterable().iterator()); } else { - System.err.println("List"); +// System.err.println("List"); List valueList = (List) value.__tojava__(List.class); if (valueList != null && valueList != Py.NoConversion) { setsliceList(start, stop, step, valueList); Modified: branches/newlist/src/org/python/core/packagecache/PackageManager.java =================================================================== --- branches/newlist/src/org/python/core/packagecache/PackageManager.java 2009-04-16 05:48:35 UTC (rev 6230) +++ branches/newlist/src/org/python/core/packagecache/PackageManager.java 2009-04-16 06:18:40 UTC (rev 6231) @@ -88,11 +88,9 @@ if (!instantiate) { PyList ret = cls.keys(); - PyList dictKeys = dict.keys(); - for (int i = 0; i < dictKeys.__len__(); i++) { - PyObject name = dictKeys.pyget(i); + for (PyObject name : dictKeys.asIterable()) { if (!cls.has_key(name)) { if (exclpkgs && dict.get(name) instanceof PyJavaPackage) continue; @@ -103,7 +101,6 @@ return ret; } - for (PyObject pyname : cls.keys().asIterable()) { if (!dict.has_key(pyname)) { String name = pyname.toString(); @@ -118,8 +115,7 @@ * Helper merging list2 into list1. Returns list1. */ protected PyList merge(PyList list1, PyList list2) { - for (int i = 0; i < list2.__len__(); i++) { - PyObject name = list2.pyget(i); + for (PyObject name : list2.asIterable()) { list1.append(name); } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <zy...@us...> - 2009-04-17 23:50:26
|
Revision: 6237 http://jython.svn.sourceforge.net/jython/?rev=6237&view=rev Author: zyasoft Date: 2009-04-17 23:50:17 +0000 (Fri, 17 Apr 2009) Log Message: ----------- Additional proxying fixes Modified Paths: -------------- branches/newlist/src/org/python/core/PyList.java branches/newlist/src/org/python/core/PyTuple.java Modified: branches/newlist/src/org/python/core/PyList.java =================================================================== --- branches/newlist/src/org/python/core/PyList.java 2009-04-17 14:06:01 UTC (rev 6236) +++ branches/newlist/src/org/python/core/PyList.java 2009-04-17 23:50:17 UTC (rev 6237) @@ -31,7 +31,7 @@ list = Generic.list(); } - public PyList(List list, boolean convert) { + private PyList(List list, boolean convert) { super(TYPE); if (!convert) { this.list = list; @@ -71,7 +71,7 @@ } } - public static PyList fromList(List list) { + public static PyList fromList(List<PyObject> list) { return new PyList(list, false); } @@ -97,7 +97,9 @@ return; } if (seq instanceof PyList) { - list.addAll((PyList) seq); // don't convert + list.addAll(((PyList) seq).list); // don't convert + } else if (seq instanceof PyTuple) { + list.addAll(((PyTuple) seq).getList()); } else { for (PyObject item : seq.asIterable()) { append(item); @@ -134,7 +136,7 @@ if (value == this) { // copy value = new PyList((PySequence) value); } - setslicePyList(start, stop, step, (PyList)value); + setslicePyList(start, stop, step, (PyList) value); } else if (value instanceof PySequence) { setsliceIterator(start, stop, step, value.asIterable().iterator()); } else if (value != null && !(value instanceof List)) { @@ -162,7 +164,7 @@ } protected void setsliceIterator(int start, int stop, int step, Iterator<PyObject> iter) { - if(step == 1) { + if (step == 1) { List<PyObject> insertion = new ArrayList<PyObject>(); if (iter != null) { while (iter.hasNext()) { @@ -185,12 +187,12 @@ } protected void setslicePyList(int start, int stop, int step, PyList other) { - if(step == 1) { + if (step == 1) { list.subList(start, stop).clear(); list.addAll(start, other.list); } else { int size = list.size(); - Iterator<PyObject> iter = other.listIterator(); + Iterator<PyObject> iter = other.list.listIterator(); for (int j = start; iter.hasNext(); j += step) { PyObject item = iter.next(); if (j >= size) { @@ -215,7 +217,7 @@ PyList newList = new PyList(); for (int i = 0; i < count; i++) { - newList.addAll(this); + newList.addAll(list); } return newList; } @@ -804,7 +806,7 @@ public boolean equals(Object o) { if (o instanceof PyList) { return (((PyList) o).list.equals(list)); - } else if(o instanceof List) { // XXX copied from PyList, but... + } else if (o instanceof List) { // XXX copied from PyList, but... return o.equals(this); // XXX shouldn't this compare using py2java? } return false; @@ -818,7 +820,7 @@ /** @deprecated */ @Override public PyObject[] getArray() { - PyObject a[] = new PyObject[list.size()]; + PyObject a[] = null; // = new PyObject[list.size()]; return list.toArray(a); } @@ -839,19 +841,64 @@ @Override public int lastIndexOf(Object o) { - return list.lastIndexOf(o); + return list.lastIndexOf(Py.java2py(o)); } @Override public ListIterator listIterator() { - return list.listIterator(); + return new PyListIterator(0); } @Override public ListIterator listIterator(int index) { - return list.listIterator(index); + return new PyListIterator(index); } + public class PyListIterator implements ListIterator { + + private final ListIterator<PyObject> iter; + + PyListIterator(int index) { + iter = list.listIterator(index); + } + + public boolean hasNext() { + return iter.hasNext(); + } + + public Object next() { + return iter.next().__tojava__(Object.class); + } + + public boolean hasPrevious() { + return iter.hasPrevious(); + } + + public Object previous() { + return iter.previous().__tojava__(Object.class); + } + + public int nextIndex() { + return iter.nextIndex(); + } + + public int previousIndex() { + return iter.previousIndex(); + } + + public void remove() { + iter.remove(); + } + + public void set(Object o) { + iter.set(Py.java2py(o)); + } + + public void add(Object o) { + iter.add(Py.java2py(o)); + } + } + @Override public void pyadd(int index, PyObject element) { list.add(index, element); @@ -912,17 +959,75 @@ @Override public List subList(int fromIndex, int toIndex) { - return list.subList(fromIndex, toIndex); + return fromList(list.subList(fromIndex, toIndex)); } @Override public Object[] toArray() { - return list.toArray(); + Object copy[] = list.toArray(); + for (int i = 0; i < copy.length; i++) { + copy[i] = ((PyObject) copy[i]).__tojava__(Object.class); + } + return copy; +// System.err.println("toArray:" + this); +// System.err.println(CallStackUtil.getCallStackAsString()); +// return list.toArray(); } + // from http://roncox.org/20 - XXX just here for debugging + static class CallStackUtil { + + public synchronized static String getCallStackAsString() { + StringBuilder sb = new StringBuilder(); + + StackTraceElement[] stackTraceElements = + Thread.currentThread().getStackTrace(); + + String[] array = getCallStackAsStringArray(stackTraceElements); + + for (int i = 0; i < array.length; i++) { + sb.append(array[i] + "\n"); + } + return sb.toString(); + } + + public synchronized static String[] getCallStackAsStringArray() { + StackTraceElement[] stackTraceElements = + Thread.currentThread().getStackTrace(); + + String[] array = getCallStackAsStringArray(stackTraceElements); + + return array; + } + + private synchronized static String[] getCallStackAsStringArray(StackTraceElement[] stackTraceElements) { + ArrayList<String> list = new ArrayList<String>(); + String[] array = new String[1]; + + for (int i = 0; i < stackTraceElements.length; i++) { + StackTraceElement element = stackTraceElements[i]; + String classname = element.getClassName(); + String methodName = element.getMethodName(); + int lineNumber = element.getLineNumber(); + list.add(classname + "." + methodName + ":" + lineNumber); + } + return list.toArray(array); + } + } + @Override public Object[] toArray(Object[] a) { - return list.toArray(a); + Object copy[] = list.toArray(); + if (a.length != copy.length) { + a = copy; + } + for (int i = 0; i < copy.length; i++) { + a[i] = ((PyObject) copy[i]).__tojava__(Object.class); + } + for (int i = copy.length; i < a.length; i++) { + a[i] = null; + } + return a; } protected PyObject getslice(int start, int stop, int step) { Modified: branches/newlist/src/org/python/core/PyTuple.java =================================================================== --- branches/newlist/src/org/python/core/PyTuple.java 2009-04-17 14:06:01 UTC (rev 6236) +++ branches/newlist/src/org/python/core/PyTuple.java 2009-04-17 23:50:17 UTC (rev 6237) @@ -63,7 +63,7 @@ } private volatile List<PyObject> cachedList = null; - private List<PyObject> getList() { + List<PyObject> getList() { if (cachedList == null) { cachedList = Arrays.asList(array); } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <zy...@us...> - 2009-04-18 01:06:27
|
Revision: 6238 http://jython.svn.sourceforge.net/jython/?rev=6238&view=rev Author: zyasoft Date: 2009-04-18 01:06:26 +0000 (Sat, 18 Apr 2009) Log Message: ----------- All tests now pass, so just need some minor cleanup. Modified Paths: -------------- branches/newlist/src/org/python/core/PyList.java branches/newlist/src/org/python/core/PyTuple.java Modified: branches/newlist/src/org/python/core/PyList.java =================================================================== --- branches/newlist/src/org/python/core/PyList.java 2009-04-17 23:50:17 UTC (rev 6237) +++ branches/newlist/src/org/python/core/PyList.java 2009-04-18 01:06:26 UTC (rev 6238) @@ -758,28 +758,13 @@ @Override public boolean addAll(int index, Collection c) { - if (c instanceof PySequenceList) { - list.addAll(index, c); - } else { - // need to use add to convert anything pulled from a collection into a PyObject - for (Object element : c) { - add(element); - } - } - return c.size() > 0; + PyList elements = new PyList(c); + return list.addAll(index, elements.list); } @Override public boolean addAll(Collection c) { - if (c instanceof PySequenceList) { - list.addAll(c); - } else { - // need to use add to convert anything pulled from a collection into a PyObject - for (Object element : c) { - add(element); - } - } - return c.size() > 0; + return addAll(0, c); } @Override @@ -846,57 +831,51 @@ @Override public ListIterator listIterator() { - return new PyListIterator(0); + return listIterator(0); } @Override - public ListIterator listIterator(int index) { - return new PyListIterator(index); - } + public ListIterator listIterator(final int index) { + return new ListIterator() { - public class PyListIterator implements ListIterator { + private final ListIterator<PyObject> iter = list.listIterator(index); - private final ListIterator<PyObject> iter; + public boolean hasNext() { + return iter.hasNext(); + } - PyListIterator(int index) { - iter = list.listIterator(index); - } + public Object next() { + return iter.next().__tojava__(Object.class); + } - public boolean hasNext() { - return iter.hasNext(); - } + public boolean hasPrevious() { + return iter.hasPrevious(); + } - public Object next() { - return iter.next().__tojava__(Object.class); - } + public Object previous() { + return iter.previous().__tojava__(Object.class); + } - public boolean hasPrevious() { - return iter.hasPrevious(); - } + public int nextIndex() { + return iter.nextIndex(); + } - public Object previous() { - return iter.previous().__tojava__(Object.class); - } + public int previousIndex() { + return iter.previousIndex(); + } - public int nextIndex() { - return iter.nextIndex(); - } + public void remove() { + iter.remove(); + } - public int previousIndex() { - return iter.previousIndex(); - } + public void set(Object o) { + iter.set(Py.java2py(o)); + } - public void remove() { - iter.remove(); - } - - public void set(Object o) { - iter.set(Py.java2py(o)); - } - - public void add(Object o) { - iter.add(Py.java2py(o)); - } + public void add(Object o) { + iter.add(Py.java2py(o)); + } + }; } @Override Modified: branches/newlist/src/org/python/core/PyTuple.java =================================================================== --- branches/newlist/src/org/python/core/PyTuple.java 2009-04-17 23:50:17 UTC (rev 6237) +++ branches/newlist/src/org/python/core/PyTuple.java 2009-04-18 01:06:26 UTC (rev 6238) @@ -322,7 +322,17 @@ } public List subList(int fromIndex, int toIndex) { - return Collections.unmodifiableList(getList().subList(fromIndex, toIndex)); + if (fromIndex < 0 || toIndex > size()) { + throw new IndexOutOfBoundsException(); + } else if (fromIndex > toIndex) { + throw new IllegalArgumentException(); + } + System.err.println("subList" + fromIndex + "," + toIndex); + 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); } // Make PyTuple immutable from the collections interfaces by overriding @@ -331,18 +341,18 @@ public Iterator iterator() { return new Iterator() { - Iterator i = getList().iterator(); + private final Iterator<PyObject> iter = getList().iterator(); public void remove() { throw new UnsupportedOperationException(); } public boolean hasNext() { - return i.hasNext(); + return iter.hasNext(); } public Object next() { - return i.next(); + return iter.next().__tojava__(Object.class); } }; } @@ -394,30 +404,30 @@ public ListIterator listIterator(final int index) { return new ListIterator() { - ListIterator i = getList().listIterator(index); + private final ListIterator<PyObject> iter = getList().listIterator(index); public boolean hasNext() { - return i.hasNext(); + return iter.hasNext(); } public Object next() { - return i.next(); + return iter.next().__tojava__(Object.class); } public boolean hasPrevious() { - return i.hasPrevious(); + return iter.hasPrevious(); } public Object previous() { - return i.previous(); + return iter.previous().__tojava__(Object.class); } public int nextIndex() { - return i.nextIndex(); + return iter.nextIndex(); } public int previousIndex() { - return i.previousIndex(); + return iter.previousIndex(); } public void remove() { @@ -447,12 +457,12 @@ @Override public boolean contains(Object o) { - return getList().contains(o); + return getList().contains(Py.java2py(o)); } @Override public boolean containsAll(Collection c) { - return getList().containsAll(c); + return getList().containsAll(new PyList(c)); } @Override @@ -487,7 +497,7 @@ @Override public int lastIndexOf(Object o) { - return getList().lastIndexOf(o); + return getList().lastIndexOf(Py.java2py(o)); } @Override This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <zy...@us...> - 2009-04-18 02:31:20
|
Revision: 6239 http://jython.svn.sourceforge.net/jython/?rev=6239&view=rev Author: zyasoft Date: 2009-04-18 02:31:16 +0000 (Sat, 18 Apr 2009) Log Message: ----------- Cleanup. Modified Paths: -------------- branches/newlist/src/org/python/core/PyList.java branches/newlist/src/org/python/core/PyTuple.java Modified: branches/newlist/src/org/python/core/PyList.java =================================================================== --- branches/newlist/src/org/python/core/PyList.java 2009-04-18 01:06:26 UTC (rev 6238) +++ branches/newlist/src/org/python/core/PyList.java 2009-04-18 02:31:16 UTC (rev 6239) @@ -1,4 +1,4 @@ - // Copyright (c) Corporation for National Research Initiatives +// Copyright (c) Corporation for National Research Initiatives package org.python.core; import java.util.ArrayList; @@ -140,11 +140,9 @@ } else if (value instanceof PySequence) { setsliceIterator(start, stop, step, value.asIterable().iterator()); } else if (value != null && !(value instanceof List)) { - //XXX: can we avoid copying here? Needed to pass test_userlist value = new PyList(value); setsliceIterator(start, stop, step, value.asIterable().iterator()); } else { -// System.err.println("List"); List valueList = (List) value.__tojava__(List.class); if (valueList != null && valueList != Py.NoConversion) { setsliceList(start, stop, step, valueList); @@ -782,7 +780,6 @@ if (c instanceof PySequenceList) { return list.containsAll(c); } else { - //XXX: FJW this seems unnecessary. return list.containsAll(new PyList(c)); } } @@ -948,52 +945,8 @@ copy[i] = ((PyObject) copy[i]).__tojava__(Object.class); } return copy; -// System.err.println("toArray:" + this); -// System.err.println(CallStackUtil.getCallStackAsString()); -// return list.toArray(); } - // from http://roncox.org/20 - XXX just here for debugging - static class CallStackUtil { - - public synchronized static String getCallStackAsString() { - StringBuilder sb = new StringBuilder(); - - StackTraceElement[] stackTraceElements = - Thread.currentThread().getStackTrace(); - - String[] array = getCallStackAsStringArray(stackTraceElements); - - for (int i = 0; i < array.length; i++) { - sb.append(array[i] + "\n"); - } - return sb.toString(); - } - - public synchronized static String[] getCallStackAsStringArray() { - StackTraceElement[] stackTraceElements = - Thread.currentThread().getStackTrace(); - - String[] array = getCallStackAsStringArray(stackTraceElements); - - return array; - } - - private synchronized static String[] getCallStackAsStringArray(StackTraceElement[] stackTraceElements) { - ArrayList<String> list = new ArrayList<String>(); - String[] array = new String[1]; - - for (int i = 0; i < stackTraceElements.length; i++) { - StackTraceElement element = stackTraceElements[i]; - String classname = element.getClassName(); - String methodName = element.getMethodName(); - int lineNumber = element.getLineNumber(); - list.add(classname + "." + methodName + ":" + lineNumber); - } - return list.toArray(array); - } - } - @Override public Object[] toArray(Object[] a) { Object copy[] = list.toArray(); Modified: branches/newlist/src/org/python/core/PyTuple.java =================================================================== --- branches/newlist/src/org/python/core/PyTuple.java 2009-04-18 01:06:26 UTC (rev 6238) +++ branches/newlist/src/org/python/core/PyTuple.java 2009-04-18 02:31:16 UTC (rev 6239) @@ -33,7 +33,6 @@ public PyTuple(PyType subtype, PyObject[] elements) { super(subtype); -// System.err.println("Initializing from " + Arrays.toString(elements)); if (elements == null) { array = new PyObject[0]; } else { @@ -58,7 +57,6 @@ } private static PyTuple fromArrayNoCopy(PyObject[] elements) { -// System.err.println("newtuple (no copy):" + Arrays.toString(elements)); return new PyTuple(elements, false); } private volatile List<PyObject> cachedList = null; @@ -73,10 +71,8 @@ @ExposedNew final static PyObject tuple_new(PyNewWrapper new_, boolean init, PyType subtype, PyObject[] args, String[] keywords) { -// System.err.println("tuple_new"); ArgParser ap = new ArgParser("newtuple", args, keywords, new String[]{"sequence"}, 0); PyObject S = ap.getPyObject(0, null); -// System.err.println("newtuple: new_=" + new_ + ",S=" + S); if (new_.for_type == subtype) { if (S == null) { return EMPTY_TUPLE; @@ -154,6 +150,7 @@ return fromArrayNoCopy(newArray); } + @Override public int __len__() { return tuple___len__(); } @@ -198,6 +195,7 @@ return super.__le__(o); } + @Override public PyObject __add__(PyObject generic_other) { return tuple___add__(generic_other); } @@ -241,6 +239,7 @@ return repeat(o.asIndex(Py.OverflowError)); } + @Override public PyObject __iter__() { return tuple___iter__(); } @@ -269,10 +268,12 @@ return new PyTuple(new PyTuple(getArray())); } + @Override public PyTuple __getnewargs__() { return tuple___getnewargs__(); } + @Override public int hashCode() { return tuple___hash__(); } @@ -300,6 +301,7 @@ return o.__repr__().toString(); } + @Override public String toString() { return tuple___repr__(); } @@ -335,9 +337,6 @@ return new PyTuple(elements); } - // Make PyTuple immutable from the collections interfaces by overriding - // all the mutating methods to throw UnsupportedOperationException exception. - // This is how Collections.unmodifiableList() does it. public Iterator iterator() { return new Iterator() { @@ -469,8 +468,8 @@ public boolean equals(Object o) { if (o instanceof PyTuple) { return Arrays.equals(array, ((PyTuple) o).array); - } else if (o instanceof List) { // XXX copied from PyList, but... - return o.equals(this); // XXX shouldn't this compare using py2java? + } else if (o instanceof List) { + return o.equals(this); } return false; } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |