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. |