[Pkg-bazaar-commits] ./bzr/unstable r925: - add len, and, or methods for intset

Martin Pool mbp at sourcefrog.net
Fri Apr 10 08:21:28 UTC 2009


------------------------------------------------------------
revno: 925
committer: Martin Pool <mbp at sourcefrog.net>
timestamp: Sun 2005-07-17 15:06:38 -0300
message:
  - add len, and, or methods for intset
modified:
  bzrlib/intset.py
-------------- next part --------------
=== modified file 'bzrlib/intset.py'
--- a/bzrlib/intset.py	2005-07-17 17:53:39 +0000
+++ b/bzrlib/intset.py	2005-07-17 18:06:38 +0000
@@ -61,13 +61,13 @@
     """
     # __slots__ = ['_val']
 
-    def __init__(self, values=None):
+    def __init__(self, values=None, bitmask=0L):
         """Create a new intset.
 
         values
             If specified, an initial collection of values.
         """
-        self._val = 0
+        self._val = bitmask
         if values != None:
             self.update(values)
 
@@ -84,8 +84,57 @@
         return bool(self._val)
 
 
+    def __len__(self):
+        """Number of elements in set.
+
+        >>> len(IntSet(xrange(20000)))
+        20000
+        """
+        v = self._val
+        c = 0
+        while v:
+            if v & 1:
+                c += 1
+            v = v >> 1
+        return c
+
+
+    def __and__(self, other):
+        """Set intersection.
+
+        >>> a = IntSet(range(10))
+        >>> len(a)
+        10
+        >>> b = a & a
+        >>> b == a
+        True
+        >>> a = a & IntSet([5, 7, 11, 13])
+        >>> list(a)
+        [5, 7]
+        """
+        if not isinstance(other, IntSet):
+            raise NotImplementedError(type(other))
+        return IntSet(bitmask=(self._val & other._val))
+
+
+    def __or__(self, other):
+        """Set union.
+
+        >>> a = IntSet(range(10)) | IntSet([5, 15, 25])
+        >>> len(a)
+        12
+        """
+        if not isinstance(other, IntSet):
+            raise NotImplementedError(type(other))
+        return IntSet(bitmask=(self._val | other._val))        
+
+
     def __eq__(self, other):
-        """Comparison."""
+        """Comparison.
+
+        >>> IntSet(range(3)) == IntSet([2, 0, 1])
+        True
+        """
         if isinstance(other, IntSet):
             return self._val == other._val
         else:



More information about the Pkg-bazaar-commits mailing list