changeset 22813:5a96df266b2b

filteredset: implement `first` and `last`
author Pierre-Yves David <pierre-yves.david@fb.com>
date Tue, 07 Oct 2014 00:18:08 -0700
parents fcd12b310148
children 8110405cf8ae
files mercurial/revset.py
diffstat 1 files changed, 19 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/revset.py	Mon Oct 06 14:42:00 2014 -0700
+++ b/mercurial/revset.py	Tue Oct 07 00:18:08 2014 -0700
@@ -2482,6 +2482,25 @@
     def isdescending(self):
         return self._ascending is not None and not self._ascending
 
+    def first(self):
+        for x in self:
+            return x
+        return None
+
+    def last(self):
+        it = None
+        if self._ascending is not None:
+            if self._ascending:
+                it = self.fastdesc
+            else:
+                it = self.fastasc
+        if it is None:
+            # slowly consume everything. This needs improvement
+            it = lambda: reversed(list(self))
+        for x in it():
+            return x
+        return None
+
 class addset(abstractsmartset):
     """Represent the addition of two sets