Commit 7eedfdbc authored by Hanno Schlichting's avatar Hanno Schlichting

Combine reverse and sort into one where possible

parent d8557642
...@@ -655,9 +655,10 @@ class Catalog(Persistent, Acquisition.Implicit, ExtensionClass.Base): ...@@ -655,9 +655,10 @@ class Catalog(Persistent, Acquisition.Implicit, ExtensionClass.Base):
append((k, intset, _self__getitem__)) append((k, intset, _self__getitem__))
# Note that sort keys are unique. # Note that sort keys are unique.
result.sort()
if reverse: if reverse:
result.reverse() result.sort(reverse=True)
else:
result.sort()
result = LazyCat(LazyValues(result), length) result = LazyCat(LazyValues(result), length)
elif limit is None or (limit * 4 > rlen): elif limit is None or (limit * 4 > rlen):
# Iterate over the result set getting sort keys from the index # Iterate over the result set getting sort keys from the index
...@@ -674,9 +675,10 @@ class Catalog(Persistent, Acquisition.Implicit, ExtensionClass.Base): ...@@ -674,9 +675,10 @@ class Catalog(Persistent, Acquisition.Implicit, ExtensionClass.Base):
# results with those of other catalogs while avoiding # results with those of other catalogs while avoiding
# the cost of instantiating a LazyMap per result # the cost of instantiating a LazyMap per result
if merge: if merge:
result.sort()
if reverse: if reverse:
result.reverse() result.sort(reverse=True)
else:
result.sort()
if limit is not None: if limit is not None:
result = result[:limit] result = result[:limit]
result = LazyValues(result) result = LazyValues(result)
......
...@@ -46,8 +46,11 @@ def createDatabase(): ...@@ -46,8 +46,11 @@ def createDatabase():
app = createDatabase() app = createDatabase()
def sort(iterable): def sort(iterable, reverse=False):
L = list(iterable) L = list(iterable)
if reverse:
L.sort(reverse=True)
else:
L.sort() L.sort()
return L return L
...@@ -532,8 +535,7 @@ class TestMerge(CatalogBase, unittest.TestCase): ...@@ -532,8 +535,7 @@ class TestMerge(CatalogBase, unittest.TestCase):
for cat in self.catalogs] for cat in self.catalogs]
merged_rids = [r.getRID() for r in mergeResults( merged_rids = [r.getRID() for r in mergeResults(
results, has_sort_keys=True, reverse=True)] results, has_sort_keys=True, reverse=True)]
expected = sort(chain(*results)) expected = sort(chain(*results), reverse=True)
expected.reverse()
expected = [rid for sortkey, rid, getitem in expected] expected = [rid for sortkey, rid, getitem in expected]
self.assertEqual(merged_rids, expected) self.assertEqual(merged_rids, expected)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment