From: <zy...@us...> - 2009-04-15 06:18:59
|
Revision: 6228 http://jython.svn.sourceforge.net/jython/?rev=6228&view=rev Author: zyasoft Date: 2009-04-15 06:18:52 +0000 (Wed, 15 Apr 2009) Log Message: ----------- Slicing is faster. Modified Paths: -------------- branches/newlist/src/org/python/core/PyList.java Modified: branches/newlist/src/org/python/core/PyList.java =================================================================== --- branches/newlist/src/org/python/core/PyList.java 2009-04-15 05:58:59 UTC (rev 6227) +++ branches/newlist/src/org/python/core/PyList.java 2009-04-15 06:18:52 UTC (rev 6228) @@ -130,10 +130,12 @@ if (stop < start) { stop = start; } - if (value instanceof PySequence) { + if (value instanceof PyList) { if (value == this) { // copy value = new PyList((PySequence) 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)) { //XXX: can we avoid copying here? Needed to pass test_userlist @@ -161,16 +163,14 @@ 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)); + List<PyObject> insertion = new ArrayList<PyObject>(); if (iter != null) { while (iter.hasNext()) { - copy.add(iter.next()); + insertion.add(iter.next()); } } - copy.addAll(this.list.subList(stop, this.list.size())); - this.list.clear(); - this.list.addAll(copy); + list.subList(start, stop).clear(); + list.addAll(start, insertion); } else { int size = list.size(); for (int j = start; iter.hasNext(); j += step) { @@ -184,6 +184,24 @@ } } + protected void setslicePyList(int start, int stop, int step, PyList other) { + if(step == 1) { + list.subList(start, stop).clear(); + list.addAll(start, other.list); + } else { + int size = list.size(); + Iterator<PyObject> iter = other.listIterator(); + 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) { This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |