[Python-Dev] decorate-sort-undecorate

Raymond Hettinger python at rcn.com
Tue Oct 14 22:40:44 EDT 2003


[Greg Ewing]
> > The only issue then is to avoid comparing the whole
> > record, and this presumably should be non-optional
> > as well.

[Guido]
> Right.  I think we've settled on using small wrapper objects instead
> of tuples, whose comparison *only* compares the key value, and whose
> other field contains a reference to the full record.  When passing
> both cmp and key, cmp is passed the key field from the wrapper.

Here is an implementation to try out.  This second patch includes
unittests and docs.  The reference counts work out file for repeated
test runs and the rest of the test suite passes just fine:

   www.python.org/sf/823292

* The optional keywords arguments are:  cmp, key, reverse.

* The key function triggers a DSU step with a wrapper object that holds
the full record but returns only the key for a comparison.  This is
fast, memory efficient, and doesn't change the underlying stability
characteristics of the sort. (I think this was Neil's idea -- and it
works like a charm.)

* If the key function is not specified, no wrapping occurs so that sort
performance is not affected.


Raymond Hettinger


#################################################################
#################################################################
#################################################################
#####
#####
#####
#################################################################
#################################################################
#################################################################



More information about the Python-Dev mailing list