revset: added _orderedsetmixin class
authorLucas Moscovicz <lmoscovicz@fb.com>
Wed, 12 Mar 2014 16:40:18 -0700
changeset 20749 c3e49b127de0
parent 20748 6b731b29e154
child 20750 d4f2f2d74210
revset: added _orderedsetmixin class This class has utility methods for any ordered class to get the min and the max values.
mercurial/revset.py
--- a/mercurial/revset.py	Wed Feb 19 09:28:17 2014 -0800
+++ b/mercurial/revset.py	Wed Mar 12 16:40:18 2014 -0700
@@ -2254,6 +2254,37 @@
         This is part of the mandatory API for smartset."""
         return lazyset(self, condition)
 
+class _orderedsetmixin(object):
+    """Mixin class with utility methods for smartsets
+
+    This should be extended by smartsets which have the isascending(),
+    isdescending() and reverse() methods"""
+
+    def _first(self):
+        """return the first revision in the set"""
+        for r in self:
+            return r
+        return None
+
+    def _last(self):
+        """return the last revision in the set"""
+        self.reverse()
+        m = self._first()
+        self.reverse()
+        return m
+
+    def min(self):
+        """return the smallest element in the set"""
+        if self.isascending():
+            return self._first()
+        return self._last()
+
+    def max(self):
+        """return the largest element in the set"""
+        if self.isascending():
+            return self._last()
+        return self._first()
+
 class lazyset(object):
     """Duck type for baseset class which iterates lazily over the revisions in
     the subset and contains a function which tests for membership in the