# HG changeset patch
# User Jason Grout <jason-sage@creativetrax.com>
# Date 1240250040 18000
# Node ID fa3211a74fc2748b95c70ab0f0564d3a205d2077
# Parent  3314db3a6b61d2c95e46e9a289a9d24c8c01e283
[mq]: bitset-bugfix-len-function.patch

diff -r 3314db3a6b61 -r fa3211a74fc2 sage/misc/bitset.pxi
--- a/sage/misc/bitset.pxi	Wed Apr 15 22:18:33 2009 -0500
+++ b/sage/misc/bitset.pxi	Mon Apr 20 12:54:00 2009 -0500
@@ -276,6 +276,7 @@
     cdef long i = bitset_first(a)
     if i == -1:
         raise KeyError, 'pop from an empty set'
+    bitset_discard(a, i)
     return i
     
 cdef inline long bitset_first_diff(bitset_t a, bitset_t b):
@@ -335,6 +336,18 @@
     return -1
 
 
+
+cdef inline long bitset_len(bitset_t bits):
+    """
+    Calculate the number of items in the set (i.e., the number of nonzero bits).
+    """
+    cdef long len = 0
+    cdef long i = bitset_first(bits)
+    while i>=0:
+        len += 1
+        i=bitset_next(bits, i+1)
+    return len
+
     
 #############################################################################
 # Bitset Arithmetic
diff -r 3314db3a6b61 -r fa3211a74fc2 sage/misc/misc_c.pyx
--- a/sage/misc/misc_c.pyx	Wed Apr 15 22:18:33 2009 -0500
+++ b/sage/misc/misc_c.pyx	Mon Apr 20 12:54:00 2009 -0500
@@ -290,6 +290,7 @@
         sage: test_bitset('00101', '01110', 4)
         a 00101
         a.size 5
+        len(a) 2
         a.limbs 1
         b 01110
         a.in(n)   True
@@ -322,6 +323,7 @@
         sage: test_bitset('11101', '11001', 2)
         a 11101
         a.size 5
+        len(a) 4
         a.limbs 1
         b 11001
         a.in(n)   True
@@ -355,6 +357,7 @@
         sage: test_bitset('111001'*25, RealField(151)(pi).str(2)[2:], 69)
         a 111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001
         a.size 150
+        len(a) 100
         a.limbs 5
         b 000100100001111110110101010001000100001011010001100001000110100110001001100011001100010100010111000000011011100000111001101000100101001000000100100111
         a.in(n)   False
@@ -405,6 +408,7 @@
 
     print "a", bitset_string(a)
     print "a.size", a.size
+    print "len(a)" , bitset_len(a)
     print "a.limbs", a.limbs
     print "b", bitset_string(b)
     print "a.in(n)  ", bitset_in(a, n)
@@ -559,6 +563,7 @@
         sage: from sage.misc.misc_c import test_bitset_pop
         sage: test_bitset_pop('0101')
         a.pop()   1
+        new set:  0001
         sage: test_bitset_pop('0000') 
         Traceback (most recent call last):
         ...
@@ -568,6 +573,7 @@
     bitset_from_str(a, py_a)
     i=bitset_pop(a)
     print "a.pop()  ", i
+    print "new set: ", bitset_string(a)
     bitset_free(a)
 
 
