Ticket #13352: trac_13352_bitset_len_v4.patch

File trac_13352_bitset_len_v4.patch, 56.5 KB (added by jdemeyer, 7 years ago)
  • sage/coding/binary_code.pyx

    # HG changeset patch
    # User Jeroen Demeyer <jdemeyer@cage.ugent.be>
    # Date 1376770316 -7200
    # Node ID de0d08ac0cc6498de90694f4ac4ca7170af4ec64
    # Parent  99ad4c29d89ab7d9004251ccdeb40de0068c272e
    Use mpn_popcount() for bitset_len(), clean up bitset interface
    
    diff --git a/sage/coding/binary_code.pyx b/sage/coding/binary_code.pyx
    a b  
    7373        ham_wts[i] = ham_wts[i & 255] + ham_wts[(i>>8) & 255]
    7474    return ham_wts
    7575
    76 include 'sage/misc/bitset_pxd.pxi'
    7776include 'sage/misc/bitset.pxi'
    7877def weight_dist(M):
    7978    """
  • deleted file sage/combinat/debruijn_sequence.pxd

    diff --git a/sage/combinat/debruijn_sequence.pxd b/sage/combinat/debruijn_sequence.pxd
    deleted file mode 100644
    + -  
    1 include "sage/misc/bitset_pxd.pxi"
  • sage/crypto/boolean_function.pyx

    diff --git a/sage/crypto/boolean_function.pyx b/sage/crypto/boolean_function.pyx
    a b  
    3636from sage.rings.finite_rings.finite_field_givaro import FiniteField_givaro
    3737from sage.rings.polynomial.polynomial_element import is_Polynomial
    3838
    39 include "sage/misc/bitset_pxd.pxi"
    4039include "sage/misc/bitset.pxi"
    4140from cpython.string cimport *
    4241
  • sage/doctest/sources.py

    diff --git a/sage/doctest/sources.py b/sage/doctest/sources.py
    a b  
    668668            ....:     dirs.sort(); files.sort()
    669669            ....:     for F in files:
    670670            ....:         _, ext = os.path.splitext(F)
    671             ....:         if ext in ('.py', '.pyx', '.sage', '.spyx', '.rst'):
     671            ....:         if ext in ('.py', '.pyx', '.pxi', '.sage', '.spyx', '.rst'):
    672672            ....:             filename = os.path.join(path, F)
    673673            ....:             FDS = FileDocTestSource(filename, DocTestDefaults(long=True,optional=True))
    674674            ....:             FDS._test_enough_doctests(verbose=False)
  • sage/ext/cdefs.pxi

    diff --git a/sage/ext/cdefs.pxi b/sage/ext/cdefs.pxi
    a b  
    33#
    44
    55from libc.stdio cimport *
    6 from libc.string cimport strlen, strcpy, memset, memcpy
     6from libc.string cimport strlen, strcpy, memset, memcpy, memcmp
    77
    88from libc.math cimport sqrt, frexp, ldexp
    99
  • sage/graphs/base/c_graph.pxd

    diff --git a/sage/graphs/base/c_graph.pxd b/sage/graphs/base/c_graph.pxd
    a b  
    55#                         http://www.gnu.org/licenses/
    66#**************************************************************************
    77
    8 include "sage/misc/bitset_pxd.pxi"
     8from sage.misc.bitset cimport bitset_t
    99
    1010cdef class CGraph:
    1111    cdef int num_verts
  • sage/graphs/base/dense_graph.pxd

    diff --git a/sage/graphs/base/dense_graph.pxd b/sage/graphs/base/dense_graph.pxd
    a b  
    88
    99from c_graph cimport CGraph
    1010include 'sage/ext/stdsage.pxi'
    11 include 'sage/misc/bitset_pxd.pxi'
    1211
    1312cdef class DenseGraph(CGraph):
    1413    # Values inherited from CGraph:
  • sage/graphs/base/sparse_graph.pxd

    diff --git a/sage/graphs/base/sparse_graph.pxd b/sage/graphs/base/sparse_graph.pxd
    a b  
    88
    99from c_graph cimport CGraph
    1010include 'sage/ext/stdsage.pxi'
    11 include 'sage/misc/bitset_pxd.pxi'
    1211
    1312cdef struct SparseGraphLLNode:
    1413    int label
  • sage/graphs/base/static_sparse_graph.pxd

    diff --git a/sage/graphs/base/static_sparse_graph.pxd b/sage/graphs/base/static_sparse_graph.pxd
    a b  
    11ctypedef unsigned short ushort
    22
    3 include "sage/misc/bitset_pxd.pxi"
     3from cpython cimport PyObject
    44
    55cdef extern from "stdlib.h":
    66    ctypedef void const_void "const void"
  • sage/graphs/base/static_sparse_graph.pyx

    diff --git a/sage/graphs/base/static_sparse_graph.pyx b/sage/graphs/base/static_sparse_graph.pyx
    a b  
    264264                g.neighbors[v_id][i] = j
    265265                edge_labels[(g.neighbors[v_id]+i)-g.edges] = label
    266266
    267         g.edge_labels = <void *> edge_labels
     267        g.edge_labels = <PyObject *> <void *> edge_labels
    268268        cpython.Py_XINCREF(g.edge_labels)
    269269
    270270cdef inline int n_edges(short_digraph g):
     
    291291
    292292    if src.edge_labels != NULL:
    293293        edge_labels = [None]*n_edges(src)
    294         dst.edge_labels = <void *> edge_labels
     294        dst.edge_labels = <PyObject *> <void *> edge_labels
    295295        cpython.Py_XINCREF(dst.edge_labels)
    296296
    297297cdef int init_reverse(short_digraph dst, short_digraph src) except -1:
  • sage/graphs/convexity_properties.pxd

    diff --git a/sage/graphs/convexity_properties.pxd b/sage/graphs/convexity_properties.pxd
    a b  
    1 include "sage/misc/bitset_pxd.pxi"
     1from sage.misc.bitset cimport bitset_t
    22
    33cdef class ConvexityProperties:
    44    cdef int _n
  • sage/graphs/distances_all_pairs.pyx

    diff --git a/sage/graphs/distances_all_pairs.pyx b/sage/graphs/distances_all_pairs.pyx
    a b  
    127127#                  http://www.gnu.org/licenses/
    128128##############################################################################
    129129
    130 include "sage/misc/bitset_pxd.pxi"
    131130include "sage/misc/bitset.pxi"
    132131from libc.stdint cimport uint64_t
    133132from sage.graphs.base.c_graph cimport CGraph
  • sage/graphs/generic_graph_pyx.pyx

    diff --git a/sage/graphs/generic_graph_pyx.pyx b/sage/graphs/generic_graph_pyx.pyx
    a b  
    1616#*****************************************************************************
    1717
    1818include "sage/ext/interrupt.pxi"
    19 include 'sage/ext/cdefs.pxi'           
     19include 'sage/ext/cdefs.pxi'
    2020include 'sage/ext/stdsage.pxi'
    2121
    2222# import from Python standard library
    2323from sage.misc.prandom import random
    2424
    25 # import from third-party library 
    26 from sage.graphs.base.sparse_graph cimport SparseGraph 
     25# import from third-party library
     26from sage.graphs.base.sparse_graph cimport SparseGraph
    2727
    28 cdef extern from *:
    29     double sqrt(double)
    3028
    3129cdef class GenericGraph_pyx(SageObject):
    3230    pass
  • sage/graphs/graph_decompositions/rankwidth.pyx

    diff --git a/sage/graphs/graph_decompositions/rankwidth.pyx b/sage/graphs/graph_decompositions/rankwidth.pyx
    a b  
    124124
    125125
    126126include 'sage/ext/stdsage.pxi'
    127 include 'sage/misc/bitset_pxd.pxi'
     127include 'sage/ext/cdefs.pxi'
    128128include "sage/ext/interrupt.pxi"
    129129
    130130cdef list id_to_vertices
  • sage/graphs/weakly_chordal.pyx

    diff --git a/sage/graphs/weakly_chordal.pyx b/sage/graphs/weakly_chordal.pyx
    a b  
    3939#                  http://www.gnu.org/licenses/
    4040##############################################################################
    4141
    42 include "sage/misc/bitset_pxd.pxi"
    4342include "sage/misc/bitset.pxi"
    4443
    4544cdef inline int has_edge(bitset_t bs, int u, int v, int n):
  • sage/groups/perm_gps/partn_ref/data_structures_pxd.pxi

    diff --git a/sage/groups/perm_gps/partn_ref/data_structures_pxd.pxi b/sage/groups/perm_gps/partn_ref/data_structures_pxd.pxi
    a b  
    77
    88include 'sage/ext/cdefs.pxi'
    99include 'sage/ext/stdsage.pxi'
    10 include 'sage/misc/bitset_pxd.pxi'
    11 
    12 cdef extern from "stdlib.h":
    13     int rand()
     10from sage.misc.bitset cimport *
     11from libc.stdlib cimport rand
    1412
    1513cdef extern from "flint/ulong_extras.h":
    1614    int n_is_prime(unsigned long n)
  • sage/matroids/basis_exchange_matroid.pxd

    diff --git a/sage/matroids/basis_exchange_matroid.pxd b/sage/matroids/basis_exchange_matroid.pxd
    a b  
    1 include 'sage/misc/bitset_pxd.pxi'
     1from sage.misc.bitset cimport *
    22
    33DEF BINT_EXCEPT = -2**31 - 1  # def. repeated in .pyx file
    44
  • sage/matroids/basis_matroid.pxd

    diff --git a/sage/matroids/basis_matroid.pxd b/sage/matroids/basis_matroid.pxd
    a b  
    1 include 'sage/misc/bitset_pxd.pxi'
     1from sage.misc.bitset cimport bitset_t
    22from matroid cimport Matroid
    33from basis_exchange_matroid cimport BasisExchangeMatroid
    44from set_system cimport SetSystem
  • sage/matroids/extension.pxd

    diff --git a/sage/matroids/extension.pxd b/sage/matroids/extension.pxd
    a b  
    1 include 'sage/misc/bitset_pxd.pxi'
     1from sage.misc.bitset cimport bitset_t
    22from basis_matroid cimport BasisMatroid
    33
    44cdef class CutNode:
  • sage/matroids/lean_matrix.pxd

    diff --git a/sage/matroids/lean_matrix.pxd b/sage/matroids/lean_matrix.pxd
    a b  
    1 include 'sage/misc/bitset_pxd.pxi'
     1from sage.misc.bitset cimport bitset_t
    22
    33cdef class LeanMatrix:
    44    cdef long _nrows
  • sage/matroids/linear_matroid.pxd

    diff --git a/sage/matroids/linear_matroid.pxd b/sage/matroids/linear_matroid.pxd
    a b  
    1 include 'sage/misc/bitset_pxd.pxi'
     1from sage.misc.bitset cimport bitset_t
    22
    33from matroid cimport Matroid
    44from basis_exchange_matroid cimport BasisExchangeMatroid
  • sage/matroids/set_system.pxd

    diff --git a/sage/matroids/set_system.pxd b/sage/matroids/set_system.pxd
    a b  
    1 include 'sage/misc/bitset_pxd.pxi'
     1from sage.misc.bitset cimport bitset_t
    22
    33cdef class SetSystem:
    44    cdef long _groundset_size, _bitset_size
  • deleted file sage/matroids/unpickling.pxd

    diff --git a/sage/matroids/unpickling.pxd b/sage/matroids/unpickling.pxd
    deleted file mode 100644
    + -  
    1 include 'sage/misc/bitset_pxd.pxi'
  • sage/misc/bitset.pxd

    diff --git a/sage/misc/bitset.pxd b/sage/misc/bitset.pxd
    a b  
    1 include 'bitset_pxd.pxi'
     1"""
     2Cython bitset types
     3"""
     4#*****************************************************************************
     5#     Copyright (C) 2008 Robert Bradshaw <robertwb@math.washington.edu>
     6#
     7#  Distributed under the terms of the GNU General Public License (GPL)
     8#  as published by the Free Software Foundation; either version 2 of
     9#  the License, or (at your option) any later version.
     10#                  http://www.gnu.org/licenses/
     11#*****************************************************************************
    212
     13# This file declares the bitset types. The implementation of the basic
     14# Cython type bitset_t (inline functions) is in bitset.pxi, the
     15# implementation of the Python later is in bitset.pyx. The latter file
     16# also contains all doctests.
     17
     18cdef extern from *:
     19    # constant literals
     20    int index_shift "(sizeof(unsigned long)==8 ? 6 : 5)"
     21    unsigned long offset_mask "(sizeof(unsigned long)==8 ? 0x3F : 0x1F)"
     22
     23    # Given an element index n in a set, (n >> index_shift) gives the
     24    # corresponding limb number, while (n & offset_mask) gives the bit
     25    # number inside of the limb.
     26
     27cdef struct bitset_s:
     28    # The size of a bitset B counts the maximum number of bits that B can
     29    # hold. This size is independent of how many elements of B are toggled to
     30    # 1. For example, say B is the bitset 1001. Then B has size 4, with the
     31    # first and fourth elements toggled to 1, reading from left to right.
     32    # We can also think of the size of a bitset as its capacity.
     33    long size
     34
     35    # A limb is that part of a bitset that can fit into an unsigned long
     36    # (typically, 32 bits on a 32-bit machine and 64 bits on a 64-bit
     37    # machine). This counts the number of limbs to represent a bitset.
     38    # If a bitset has size <= n, then the whole bitset fits into a limb
     39    # and we only require one limb to represent the bitset. However, if
     40    # the bitset has size > n, we require more than one limb to
     41    # represent the bitset. For example, if a limb is 64 bits in length
     42    # and the bitset has size 96 bits, then we require at most two limbs
     43    # to represent the bitset.
     44    #
     45    # NOTE: we assume that a limb corresponds to an MPIR limb (this
     46    # assumption is always true in practice).
     47    long limbs
     48
     49    # The individual bits of a bitset.
     50    unsigned long *bits
     51
     52ctypedef bitset_s bitset_t[1]
     53
     54
     55# Python layer over bitset_t
    356cdef class FrozenBitset:
    457    cdef bitset_t _bitset
    558    cdef FrozenBitset _new(self,long int capacity)
  • sage/misc/bitset.pxi

    diff --git a/sage/misc/bitset.pxi b/sage/misc/bitset.pxi
    a b  
    1616#     Copyright (C) 2008 Robert Bradshaw <robertwb@math.washington.edu>
    1717#
    1818#  Distributed under the terms of the GNU General Public License (GPL)
    19 #
    20 #    This code is distributed in the hope that it will be useful,
    21 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
    22 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    23 #    General Public License for more details.
    24 #
    25 #  The full text of the GPL is available at:
    26 #
     19#  as published by the Free Software Foundation; either version 2 of
     20#  the License, or (at your option) any later version.
    2721#                  http://www.gnu.org/licenses/
    2822#*****************************************************************************
    2923
     24include 'sage/ext/cdefs.pxi'
     25include 'sage/ext/stdsage.pxi'
     26from sage.libs.gmp.mpn cimport *
     27from sage.misc.bitset cimport *
    3028
    31 # Doctests for the functions in this file are in misc_c.pyx
    32 
     29# Doctests for the functions in this file are in sage/misc/bitset.pyx
    3330
    3431#############################################################################
    3532# Bitset Initalization
     
    437434    """
    438435    Calculate the number of items in the set (i.e., the number of nonzero bits).
    439436    """
    440     cdef long len = 0
    441     cdef long i = bitset_first(bits)
    442     while i >= 0:
    443         len += 1
    444         i = bitset_next(bits, i + 1)
    445     return len
     437    return mpn_popcount(<mp_limb_t*>bits.bits, bits.limbs)
    446438
    447439cdef inline long bitset_hash(bitset_t bits):
    448440    """
     
    645637# Hamming Weights
    646638#############################################################################
    647639
    648 cdef enum:
    649     _bitset_hamming_table_bits = 8
    650 
    651 cdef int _bitset_hamming_table[1 << _bitset_hamming_table_bits]
    652 
    653 cdef void _bitset_fill_hamming_table():
    654     cdef int i, j
    655     for i from 0 <= i < (1 << _bitset_hamming_table_bits):
    656         _bitset_hamming_table[i] = 0
    657         for j from 0 <= j < _bitset_hamming_table_bits:
    658             _bitset_hamming_table[i] += (i >> j) & 1
    659 
    660 _bitset_fill_hamming_table()
    661 
    662 cdef inline int bitset_hamming_weight(bitset_t a):
    663     cdef long i, j
    664     cdef long w = 0
    665     cdef unsigned long limb
    666     for i from 0 <= i < a.limbs:
    667         limb = a.bits[i]
    668         for j from 0 <= j < (8 * sizeof(unsigned long) + _bitset_hamming_table_bits - 1) / _bitset_hamming_table_bits:
    669             w += _bitset_hamming_table[(limb >> (j * _bitset_hamming_table_bits)) & ((1 << _bitset_hamming_table_bits) - 1)]
    670     return w
    671 
    672 cdef inline long bitset_hamming_weight_sparse(bitset_t a):
    673     cdef long i
    674     cdef long w = 0
    675     cdef unsigned long limb
    676     for i from 0 <= i < a.limbs:
    677         limb = a.bits[i]
    678         while limb:
    679             w += _bitset_hamming_table[limb & ((1 << _bitset_hamming_table_bits) - 1)]
    680             limb = limb >> _bitset_hamming_table_bits
    681     return w
     640cdef inline long bitset_hamming_weight(bitset_t a):
     641    return bitset_len(a)
    682642
    683643#############################################################################
    684644# Bitset Conversion
  • sage/misc/bitset.pyx

    diff --git a/sage/misc/bitset.pyx b/sage/misc/bitset.pyx
    a b  
    14071407            set([])
    14081408        """
    14091409        bitset_clear(self._bitset)
     1410
     1411
     1412#############################################################################
     1413# Bitset Testing: test basic Cython bitsets
     1414#############################################################################
     1415
     1416def test_bitset(py_a, py_b, long n):
     1417    """
     1418    Test the Cython bitset functions so we can have some relevant doctests.
     1419
     1420    TESTS::
     1421
     1422        sage: from sage.misc.bitset import test_bitset
     1423        sage: test_bitset('00101', '01110', 4)
     1424        a 00101
     1425        list a [2, 4]
     1426        a.size 5
     1427        len(a) 2
     1428        a.limbs 1
     1429        b 01110
     1430        a.in(n)   True
     1431        a.not_in(n)   False
     1432        a.add(n)     00101
     1433        a.discard(n)   00100
     1434        a.set_to(n)  00101
     1435        a.flip(n)    00100
     1436        a.set_first_n(n)    11110
     1437        a.first_in_complement()    4
     1438        a.isempty()  False
     1439        a.eq(b)      False
     1440        a.cmp(b)     1
     1441        a.lex_cmp(b) -1
     1442        a.issubset(b) False
     1443        a.issuperset(b) False
     1444        a.copy()     00101
     1445        r.clear()     00000
     1446        complement a        11010
     1447        a intersect b      00100
     1448        a union b       01111
     1449        a minus b      00001
     1450        a symmetric_difference b      01011
     1451        a.rshift(n)  10000
     1452        a.lshift(n)  00000
     1453        a.first()           2
     1454        a.next(n)           4
     1455        a.first_diff(b)     1
     1456        a.next_diff(b, n)   4
     1457        a.hamming_weight()  2
     1458        a.map(m)  10100
     1459        a == loads(dumps(a))  True
     1460        reallocating a      00101
     1461        to size 4          0010
     1462        to size 8          00100000
     1463        to original size    00100
     1464
     1465    ::
     1466
     1467        sage: test_bitset('11101', '11001', 2)
     1468        a 11101
     1469        list a [0, 1, 2, 4]
     1470        a.size 5
     1471        len(a) 4
     1472        a.limbs 1
     1473        b 11001
     1474        a.in(n)   True
     1475        a.not_in(n)   False
     1476        a.add(n)     11101
     1477        a.discard(n)   11001
     1478        a.set_to(n)  11101
     1479        a.flip(n)    11001
     1480        a.set_first_n(n)    11000
     1481        a.first_in_complement()    2
     1482        a.isempty()  False
     1483        a.eq(b)      False
     1484        a.cmp(b)     1
     1485        a.lex_cmp(b) 1
     1486        a.issubset(b) False
     1487        a.issuperset(b) True
     1488        a.copy()     11101
     1489        r.clear()     00000
     1490        complement a        00010
     1491        a intersect b      11001
     1492        a union b       11101
     1493        a minus b      00100
     1494        a symmetric_difference b      00100
     1495        a.rshift(n)  10100
     1496        a.lshift(n)  00111
     1497        a.first()           0
     1498        a.next(n)           2
     1499        a.first_diff(b)     2
     1500        a.next_diff(b, n)   2
     1501        a.hamming_weight()  4
     1502        a.map(m)  10111
     1503        a == loads(dumps(a))  True
     1504        reallocating a      11101
     1505        to size 2          11
     1506        to size 4          1100
     1507        to original size    11000
     1508
     1509    Test a corner-case: a bitset that is a multiple of words::
     1510
     1511        sage: test_bitset('00'*64, '01'*64, 127)
     1512        a 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
     1513        list a []
     1514        a.size 128
     1515        len(a) 0
     1516        a.limbs ...
     1517        b 01010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101
     1518        a.in(n)   False
     1519        a.not_in(n)   True
     1520        a.add(n)     00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001
     1521        a.discard(n)   00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
     1522        a.set_to(n)  00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001
     1523        a.flip(n)    00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001
     1524        a.set_first_n(n)    11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111110
     1525        a.first_in_complement()    127
     1526        a.isempty()  True
     1527        a.eq(b)      False
     1528        a.cmp(b)     -1
     1529        a.lex_cmp(b) -1
     1530        a.issubset(b) True
     1531        a.issuperset(b) False
     1532        a.copy()     00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
     1533        r.clear()     00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
     1534        complement a        11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111
     1535        a intersect b      00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
     1536        a union b       01010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101
     1537        a minus b      00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
     1538        a symmetric_difference b      01010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101
     1539        a.rshift(n)  00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
     1540        a.lshift(n)  00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
     1541        a.first()           -1
     1542        a.next(n)           -1
     1543        a.first_diff(b)     1
     1544        a.next_diff(b, n)   127
     1545        a.hamming_weight()  0
     1546        a.map(m)  00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
     1547        a == loads(dumps(a))  True
     1548        rshifts add  True
     1549        lshifts add  True
     1550        intersection commutes True
     1551        union commutes  True
     1552        not not = id True
     1553        flipped bit  127
     1554        add bit      127
     1555        discard bit    127
     1556        lshift add unset ok True
     1557        rshift set unset ok True
     1558        reallocating a      00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
     1559        to size 127          0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
     1560        to size 254          00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
     1561        to original size    00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
     1562
     1563    Large enough to span multiple limbs.  We don't explicitly check the number of limbs below because it will be different in the 32 bit versus 64 bit cases::
     1564
     1565        sage: test_bitset('111001'*25, RealField(151)(pi).str(2)[2:], 69)
     1566        a 111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001
     1567        list a [0, 1, 2, 5, 6, 7, 8, 11, 12, 13, 14, 17, 18, 19, 20, 23, 24, 25, 26, 29, 30, 31, 32, 35, 36, 37, 38, 41, 42, 43, 44, 47, 48, 49, 50, 53, 54, 55, 56, 59, 60, 61, 62, 65, 66, 67, 68, 71, 72, 73, 74, 77, 78, 79, 80, 83, 84, 85, 86, 89, 90, 91, 92, 95, 96, 97, 98, 101, 102, 103, 104, 107, 108, 109, 110, 113, 114, 115, 116, 119, 120, 121, 122, 125, 126, 127, 128, 131, 132, 133, 134, 137, 138, 139, 140, 143, 144, 145, 146, 149]
     1568        a.size 150
     1569        len(a) 100
     1570        a.limbs ...
     1571        b 000100100001111110110101010001000100001011010001100001000110100110001001100011001100010100010111000000011011100000111001101000100101001000000100100111
     1572        a.in(n)   False
     1573        a.not_in(n)   True
     1574        a.add(n)     111001111001111001111001111001111001111001111001111001111001111001111101111001111001111001111001111001111001111001111001111001111001111001111001111001
     1575        a.discard(n)   111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001
     1576        a.set_to(n)  111001111001111001111001111001111001111001111001111001111001111001111101111001111001111001111001111001111001111001111001111001111001111001111001111001
     1577        a.flip(n)    111001111001111001111001111001111001111001111001111001111001111001111101111001111001111001111001111001111001111001111001111001111001111001111001111001
     1578        a.set_first_n(n)    111111111111111111111111111111111111111111111111111111111111111111111000000000000000000000000000000000000000000000000000000000000000000000000000000000
     1579        a.first_in_complement()    69
     1580        a.isempty()  False
     1581        a.eq(b)      False
     1582        a.cmp(b)     -1
     1583        a.lex_cmp(b) 1
     1584        a.issubset(b) False
     1585        a.issuperset(b) False
     1586        a.copy()     111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001
     1587        r.clear()     000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
     1588        complement a        000110000110000110000110000110000110000110000110000110000110000110000110000110000110000110000110000110000110000110000110000110000110000110000110000110
     1589        a intersect b      000000100001111000110001010001000000001001010001100001000000100000001001100001001000010000010001000000011001100000111001101000100001001000000000100001
     1590        a union b       111101111001111111111101111001111101111011111001111001111111111111111001111011111101111101111111111001111011111001111001111001111101111001111101111111
     1591        a minus b      111001011000000001001000101000111001110000101000011000111001011001110000011000110001101001101000111001100000011001000000010001011000110001111001011000
     1592        a symmetric_difference b      111101011000000111001100101000111101110010101000011000111111011111110000011010110101101101101110111001100010011001000000010001011100110001111101011110
     1593        a.rshift(n)  001111001111001111001111001111001111001111001111001111001111001111001111001111001000000000000000000000000000000000000000000000000000000000000000000000
     1594        a.lshift(n)  000000000000000000000000000000000000000000000000000000000000000000000111001111001111001111001111001111001111001111001111001111001111001111001111001111
     1595        a.first()           0
     1596        a.next(n)           71
     1597        a.first_diff(b)     0
     1598        a.next_diff(b, n)   73
     1599        a.hamming_weight()  100
     1600        a.map(m)  100111100111100111100111100111100111100111100111100111100111100111100111100111100111100111100111100111100111100111100111100111100111100111100111100111
     1601        a == loads(dumps(a))  True
     1602        rshifts add  True
     1603        lshifts add  True
     1604        intersection commutes True
     1605        union commutes  True
     1606        not not = id True
     1607        flipped bit  69
     1608        add bit      69
     1609        discard bit    69
     1610        lshift add unset ok True
     1611        rshift set unset ok True
     1612        reallocating a      111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001
     1613        to size 69          111001111001111001111001111001111001111001111001111001111001111001111
     1614        to size 138          111001111001111001111001111001111001111001111001111001111001111001111000000000000000000000000000000000000000000000000000000000000000000000
     1615        to original size    111001111001111001111001111001111001111001111001111001111001111001111000000000000000000000000000000000000000000000000000000000000000000000000000000000
     1616
     1617    """
     1618    cdef bint bit = True
     1619    cdef bitset_t a, b, r
     1620
     1621    bitset_from_str(a, py_a)
     1622    bitset_from_str(b, py_b)
     1623
     1624    if a.size != b.size:
     1625        raise ValueError("inputs must have same size")
     1626
     1627    print "a", bitset_string(a)
     1628    print "list a", bitset_list(a)
     1629    print "a.size", a.size
     1630    print "len(a)", bitset_len(a)
     1631    print "a.limbs", a.limbs
     1632    print "b", bitset_string(b)
     1633    print "a.in(n)  ", bitset_in(a, n)
     1634    print "a.not_in(n)  ", bitset_not_in(a, n)
     1635    bitset_add(a, n)
     1636    print "a.add(n)    ", bitset_string(a)
     1637    bitset_from_str(a, py_a)
     1638    bitset_discard(a, n)
     1639    print "a.discard(n)  ", bitset_string(a)
     1640    bitset_from_str(a, py_a)
     1641    bitset_set_to(a, n, bit)
     1642    print "a.set_to(n) ", bitset_string(a)
     1643    bitset_from_str(a, py_a)
     1644    bitset_flip(a, n)
     1645    print "a.flip(n)   ", bitset_string(a)
     1646    bitset_set_first_n(a, n)
     1647    print "a.set_first_n(n)   ", bitset_string(a)
     1648    print "a.first_in_complement()   ", bitset_first_in_complement(a)
     1649
     1650    bitset_from_str(a, py_a)
     1651    bitset_from_str(b, py_b)
     1652    print "a.isempty() ", bitset_isempty(a)
     1653    print "a.eq(b)     ", bitset_eq(a, b)
     1654    print "a.cmp(b)    ", bitset_cmp(a, b)
     1655    print "a.lex_cmp(b)", bitset_lex_cmp(a, b)
     1656    print "a.issubset(b)", bitset_issubset(a, b)
     1657    print "a.issuperset(b)", bitset_issuperset(a, b)
     1658
     1659    bitset_from_str(a, py_a)
     1660    bitset_from_str(b, py_b)
     1661
     1662    bitset_init(r, a.size)
     1663    bitset_copy(r, a)
     1664    print "a.copy()    ", bitset_string(r)
     1665    bitset_clear(r)
     1666    print "r.clear()    ", bitset_string(r)
     1667    bitset_complement(r, a)
     1668    print "complement a       ", bitset_string(r)
     1669    bitset_intersection(r, a, b)
     1670    print "a intersect b     ", bitset_string(r)
     1671    bitset_union(r, a, b)
     1672    print "a union b      ", bitset_string(r)
     1673    bitset_difference(r, a, b)
     1674    print "a minus b     ", bitset_string(r)
     1675    bitset_symmetric_difference(r, a, b)
     1676    print "a symmetric_difference b     ", bitset_string(r)
     1677
     1678    bitset_rshift(r, a, n)
     1679    print "a.rshift(n) ", bitset_string(r)
     1680
     1681    bitset_lshift(r, a, n)
     1682    print "a.lshift(n) ", bitset_string(r)
     1683
     1684    print "a.first()          ", bitset_first(a)
     1685    print "a.next(n)          ", bitset_next(a, n)
     1686    print "a.first_diff(b)    ", bitset_first_diff(a, b)
     1687    print "a.next_diff(b, n)  ", bitset_next_diff(a, b, n)
     1688
     1689    print "a.hamming_weight() ", bitset_hamming_weight(a)
     1690
     1691    morphism = {}
     1692    for i in xrange(a.size):
     1693        morphism[i] = a.size - i - 1
     1694    bitset_map(r, a, morphism)
     1695    print "a.map(m) ", bitset_string(r)
     1696
     1697    data = bitset_pickle(a)
     1698    bitset_unpickle(r, data)
     1699    print "a == loads(dumps(a)) ", bitset_eq(r, a)
     1700
     1701    cdef bitset_t s
     1702    bitset_init(s, a.size)
     1703
     1704    if a.size > 100:
     1705        bitset_rshift(r, b, 3)
     1706        bitset_rshift(r, r, 77)
     1707        bitset_rshift(s, b, 80)
     1708        print "rshifts add ", bitset_eq(s, r)
     1709
     1710        bitset_lshift(r, b, 69)
     1711        bitset_lshift(r, r, 6)
     1712        bitset_lshift(s, b, 75)
     1713        print "lshifts add ", bitset_eq(s, r)
     1714
     1715        bitset_intersection(r, a, b)
     1716        bitset_intersection(s, b, a)
     1717        print "intersection commutes", bitset_eq(s, r)
     1718
     1719        bitset_union(r, a, b)
     1720        bitset_union(s, b, a)
     1721        print "union commutes ", bitset_eq(s, r)
     1722
     1723        bitset_complement(r, b)
     1724        bitset_complement(s, r)
     1725        print "not not = id", bitset_eq(s, b)
     1726
     1727        bitset_copy(r, b)
     1728        bitset_flip(r, n)
     1729        print "flipped bit ", bitset_first_diff(b, r)
     1730
     1731        bitset_clear(r)
     1732        bitset_add(r, n)
     1733        print "add bit     ", bitset_first(r)
     1734
     1735        bitset_clear(r)
     1736        bitset_complement(r, r)
     1737        bitset_discard(r, n)
     1738        bitset_complement(r, r)
     1739        print "discard bit   ", bitset_first(r)
     1740
     1741        bitset_clear(r)
     1742        bitset_add(r, 10)
     1743        bitset_lshift(r, r, 68)
     1744        bitset_flip(r, 78)
     1745        print "lshift add unset ok", bitset_isempty(r)
     1746
     1747        bitset_clear(r)
     1748        bitset_add(r, 19)
     1749        bitset_rshift(r, r, 8)
     1750        bitset_discard(r, 11)
     1751        print "rshift set unset ok", bitset_isempty(r)
     1752
     1753    print "reallocating a     ", bitset_string(a)
     1754    bitset_realloc(a, n)
     1755    print "to size %d         " % n, bitset_string(a)
     1756    bitset_realloc(a, 2 * n)
     1757    print "to size %d         " % (2 * n), bitset_string(a)
     1758    bitset_realloc(a, b.size)
     1759    print "to original size   ", bitset_string(a)
     1760
     1761    bitset_free(a)
     1762    bitset_free(b)
     1763    bitset_free(r)
     1764    bitset_free(s)
     1765
     1766
     1767def test_bitset_set_first_n(py_a, long n):
     1768    """
     1769    Test the bitset function set_first_n.
     1770
     1771    TESTS::
     1772
     1773        sage: from sage.misc.bitset import test_bitset_set_first_n
     1774        sage: test_bitset_set_first_n('00'*64, 128)
     1775        a.set_first_n(n)    11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111
     1776
     1777    """
     1778    cdef bint bit = True
     1779    cdef bitset_t a
     1780
     1781    bitset_from_str(a, py_a)
     1782    bitset_set_first_n(a, n)
     1783    print "a.set_first_n(n)   ", bitset_string(a)
     1784    bitset_free(a)
     1785
     1786
     1787def test_bitset_remove(py_a, long n):
     1788    """
     1789    Test the bitset_remove function.
     1790
     1791    TESTS::
     1792
     1793        sage: from sage.misc.bitset import test_bitset_remove
     1794        sage: test_bitset_remove('01', 0)
     1795        Traceback (most recent call last):
     1796        ...
     1797        KeyError: 0L
     1798        sage: test_bitset_remove('01', 1)
     1799        a 01
     1800        a.size 2
     1801        a.limbs 1
     1802        n 1
     1803        a.remove(n)   00
     1804    """
     1805    cdef bitset_t a
     1806    bitset_from_str(a, py_a)
     1807
     1808    print "a", bitset_string(a)
     1809    print "a.size", a.size
     1810    print "a.limbs", a.limbs
     1811    print "n", n
     1812
     1813    bitset_remove(a, n)
     1814    print "a.remove(n)  ", bitset_string(a)
     1815
     1816    bitset_free(a)
     1817
     1818
     1819def test_bitset_pop(py_a):
     1820    """
     1821    Tests for the bitset_pop function.
     1822
     1823    TESTS::
     1824
     1825        sage: from sage.misc.bitset import test_bitset_pop
     1826        sage: test_bitset_pop('0101')
     1827        a.pop()   1
     1828        new set:  0001
     1829        sage: test_bitset_pop('0000')
     1830        Traceback (most recent call last):
     1831        ...
     1832        KeyError: 'pop from an empty set'
     1833    """
     1834    cdef bitset_t a
     1835    bitset_from_str(a, py_a)
     1836    i = bitset_pop(a)
     1837    print "a.pop()  ", i
     1838    print "new set: ", bitset_string(a)
     1839    bitset_free(a)
     1840
     1841
     1842def test_bitset_unpickle(data):
     1843    """
     1844    This (artificially) tests pickling of bitsets across systems.
     1845
     1846    INPUT:
     1847
     1848    - ``data`` -- A tuple of data as would be produced by the internal, Cython-only, method ``bitset_pickle``.
     1849
     1850    OUTPUT:
     1851
     1852    A list form of the bitset corresponding to the pickled data.
     1853
     1854    EXAMPLES:
     1855
     1856    We compare 64-bit and 32-bit encoding. Both should unpickle on any system::
     1857
     1858        sage: from sage.misc.bitset import test_bitset_unpickle
     1859        sage: test_bitset_unpickle((0, 100, 2, 8, (33, 6001)))
     1860        [0, 5, 64, 68, 69, 70, 72, 73, 74, 76]
     1861        sage: test_bitset_unpickle((0, 100, 4, 4, (33, 0, 6001, 0)))
     1862        [0, 5, 64, 68, 69, 70, 72, 73, 74, 76]
     1863    """
     1864    cdef bitset_t bs
     1865    bitset_init(bs, 1)
     1866    bitset_unpickle(bs, data)
     1867    L = bitset_list(bs)
     1868    bitset_free(bs)
     1869    return L
  • deleted file sage/misc/bitset_pxd.pxi

    diff --git a/sage/misc/bitset_pxd.pxi b/sage/misc/bitset_pxd.pxi
    deleted file mode 100644
    + -  
    1 #*****************************************************************************
    2 #     Copyright (C) 2008 Robert Bradshaw <robertwb@math.washington.edu>
    3 #
    4 #  Distributed under the terms of the GNU General Public License (GPL)
    5 #
    6 #    This code is distributed in the hope that it will be useful,
    7 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
    8 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    9 #    General Public License for more details.
    10 #
    11 #  The full text of the GPL is available at:
    12 #
    13 #                  http://www.gnu.org/licenses/
    14 #*****************************************************************************
    15 
    16 # This is a .pxi file so that one can inline functions. Doctests in misc_c.
    17 
    18 include "sage/ext/stdsage.pxi"
    19 
    20 cdef extern from *:
    21     void *memset(void *, int, size_t)
    22     void *memcpy(void *, void *, size_t)
    23     int memcmp(void *, void *, size_t)
    24     size_t strlen(char *)
    25 
    26     # constant literals
    27     int index_shift "(sizeof(unsigned long)==8 ? 6 : 5)"
    28     unsigned long offset_mask "(sizeof(unsigned long)==8 ? 0x3F : 0x1F)"
    29 
    30     # Given an element index n in a set, (n >> index_shift) gives the
    31     # corresponding limb number, while (n & offset_mask) gives the bit
    32     # number inside of the limb.
    33 
    34 cdef struct bitset_s:
    35     # The size of a bitset B counts the maximum number of bits that B can
    36     # hold. This size is independent of how many elements of B are toggled to
    37     # 1. For example, say B is the bitset 1001. Then B has size 4, with the
    38     # first and fourth elements toggled to 1, reading from left to right.
    39     # We can also think of the size of a bitset as its capacity.
    40     long size
    41 
    42     # A limb is that part of a bitset that can fit into a machine word. On an
    43     # n-bit machine, a word is usually taken to be n bits in length. Thus on a
    44     # 32-bit machine, a word usually is 32 bits in length. Similarly, on a
    45     # 64-bit machine, a word is usually 64 bits in length. If a bitset has
    46     # size <= n, then the whole bitset fits into a limb and we only require
    47     # one limb to represent the bitset. However, if the bitset has size > n,
    48     # we require more than one limb to represent the bitset. For example, if
    49     # a limb is 64 bits in length and the bitset has size 96 bits, then we
    50     # require at most two limbs to represent the bitset.
    51     long limbs
    52 
    53     # The individual bits of a bitset.
    54     unsigned long *bits
    55 
    56 ctypedef bitset_s bitset_t[1]
  • sage/misc/misc_c.pyx

    diff --git a/sage/misc/misc_c.pyx b/sage/misc/misc_c.pyx
    a b  
    431431        return balanced_list_sum(L, offset, k, cutoff) + balanced_list_sum(L, offset + k, count - k, cutoff)
    432432
    433433
    434 #############################################################################
    435 # Bitset Testing
    436 #############################################################################
    437 
    438 include "bitset_pxd.pxi"
    439 include "bitset.pxi"
    440 
    441 
    442 def test_bitset(py_a, py_b, long n):
    443     """
    444     Test the Cython bitset functions so we can have some relevant doctests.
    445 
    446     TESTS::
    447 
    448         sage: from sage.misc.misc_c import test_bitset
    449         sage: test_bitset('00101', '01110', 4)
    450         a 00101
    451         list a [2, 4]
    452         a.size 5
    453         len(a) 2
    454         a.limbs 1
    455         b 01110
    456         a.in(n)   True
    457         a.not_in(n)   False
    458         a.add(n)     00101
    459         a.discard(n)   00100
    460         a.set_to(n)  00101
    461         a.flip(n)    00100
    462         a.set_first_n(n)    11110
    463         a.first_in_complement()    4
    464         a.isempty()  False
    465         a.eq(b)      False
    466         a.cmp(b)     1
    467         a.lex_cmp(b) -1
    468         a.issubset(b) False
    469         a.issuperset(b) False
    470         a.copy()     00101
    471         r.clear()     00000
    472         complement a        11010
    473         a intersect b      00100
    474         a union b       01111
    475         a minus b      00001
    476         a symmetric_difference b      01011
    477         a.rshift(n)  10000
    478         a.lshift(n)  00000
    479         a.first()           2
    480         a.next(n)           4
    481         a.first_diff(b)     1
    482         a.next_diff(b, n)   4
    483         a.hamming_weight()  2
    484         a.hamming_weight_sparse()  2
    485         a.map(m)  10100
    486         a == loads(dumps(a))  True
    487         reallocating a      00101
    488         to size 4          0010
    489         to size 8          00100000
    490         to original size    00100
    491 
    492     ::
    493 
    494         sage: test_bitset('11101', '11001', 2)
    495         a 11101
    496         list a [0, 1, 2, 4]
    497         a.size 5
    498         len(a) 4
    499         a.limbs 1
    500         b 11001
    501         a.in(n)   True
    502         a.not_in(n)   False
    503         a.add(n)     11101
    504         a.discard(n)   11001
    505         a.set_to(n)  11101
    506         a.flip(n)    11001
    507         a.set_first_n(n)    11000
    508         a.first_in_complement()    2
    509         a.isempty()  False
    510         a.eq(b)      False
    511         a.cmp(b)     1
    512         a.lex_cmp(b) 1
    513         a.issubset(b) False
    514         a.issuperset(b) True
    515         a.copy()     11101
    516         r.clear()     00000
    517         complement a        00010
    518         a intersect b      11001
    519         a union b       11101
    520         a minus b      00100
    521         a symmetric_difference b      00100
    522         a.rshift(n)  10100
    523         a.lshift(n)  00111
    524         a.first()           0
    525         a.next(n)           2
    526         a.first_diff(b)     2
    527         a.next_diff(b, n)   2
    528         a.hamming_weight()  4
    529         a.hamming_weight_sparse()  4
    530         a.map(m)  10111
    531         a == loads(dumps(a))  True
    532         reallocating a      11101
    533         to size 2          11
    534         to size 4          1100
    535         to original size    11000
    536 
    537     Test a corner-case: a bitset that is a multiple of words::
    538 
    539         sage: test_bitset('00'*64, '01'*64, 127)
    540         a 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
    541         list a []
    542         a.size 128
    543         len(a) 0
    544         a.limbs ...
    545         b 01010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101
    546         a.in(n)   False
    547         a.not_in(n)   True
    548         a.add(n)     00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001
    549         a.discard(n)   00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
    550         a.set_to(n)  00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001
    551         a.flip(n)    00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001
    552         a.set_first_n(n)    11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111110
    553         a.first_in_complement()    127
    554         a.isempty()  True
    555         a.eq(b)      False
    556         a.cmp(b)     -1
    557         a.lex_cmp(b) -1
    558         a.issubset(b) True
    559         a.issuperset(b) False
    560         a.copy()     00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
    561         r.clear()     00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
    562         complement a        11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111
    563         a intersect b      00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
    564         a union b       01010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101
    565         a minus b      00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
    566         a symmetric_difference b      01010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101
    567         a.rshift(n)  00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
    568         a.lshift(n)  00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
    569         a.first()           -1
    570         a.next(n)           -1
    571         a.first_diff(b)     1
    572         a.next_diff(b, n)   127
    573         a.hamming_weight()  0
    574         a.hamming_weight_sparse()  0
    575         a.map(m)  00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
    576         a == loads(dumps(a))  True
    577         rshifts add  True
    578         lshifts add  True
    579         intersection commutes True
    580         union commutes  True
    581         not not = id True
    582         flipped bit  127
    583         add bit      127
    584         discard bit    127
    585         lshift add unset ok True
    586         rshift set unset ok True
    587         reallocating a      00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
    588         to size 127          0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
    589         to size 254          00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
    590         to original size    00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
    591 
    592     Large enough to span multiple limbs.  We don't explicitly check the number of limbs below because it will be different in the 32 bit versus 64 bit cases::
    593 
    594         sage: test_bitset('111001'*25, RealField(151)(pi).str(2)[2:], 69)
    595         a 111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001
    596         list a [0, 1, 2, 5, 6, 7, 8, 11, 12, 13, 14, 17, 18, 19, 20, 23, 24, 25, 26, 29, 30, 31, 32, 35, 36, 37, 38, 41, 42, 43, 44, 47, 48, 49, 50, 53, 54, 55, 56, 59, 60, 61, 62, 65, 66, 67, 68, 71, 72, 73, 74, 77, 78, 79, 80, 83, 84, 85, 86, 89, 90, 91, 92, 95, 96, 97, 98, 101, 102, 103, 104, 107, 108, 109, 110, 113, 114, 115, 116, 119, 120, 121, 122, 125, 126, 127, 128, 131, 132, 133, 134, 137, 138, 139, 140, 143, 144, 145, 146, 149]
    597         a.size 150
    598         len(a) 100
    599         a.limbs ...
    600         b 000100100001111110110101010001000100001011010001100001000110100110001001100011001100010100010111000000011011100000111001101000100101001000000100100111
    601         a.in(n)   False
    602         a.not_in(n)   True
    603         a.add(n)     111001111001111001111001111001111001111001111001111001111001111001111101111001111001111001111001111001111001111001111001111001111001111001111001111001
    604         a.discard(n)   111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001
    605         a.set_to(n)  111001111001111001111001111001111001111001111001111001111001111001111101111001111001111001111001111001111001111001111001111001111001111001111001111001
    606         a.flip(n)    111001111001111001111001111001111001111001111001111001111001111001111101111001111001111001111001111001111001111001111001111001111001111001111001111001
    607         a.set_first_n(n)    111111111111111111111111111111111111111111111111111111111111111111111000000000000000000000000000000000000000000000000000000000000000000000000000000000
    608         a.first_in_complement()    69
    609         a.isempty()  False
    610         a.eq(b)      False
    611         a.cmp(b)     -1
    612         a.lex_cmp(b) 1
    613         a.issubset(b) False
    614         a.issuperset(b) False
    615         a.copy()     111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001
    616         r.clear()     000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
    617         complement a        000110000110000110000110000110000110000110000110000110000110000110000110000110000110000110000110000110000110000110000110000110000110000110000110000110
    618         a intersect b      000000100001111000110001010001000000001001010001100001000000100000001001100001001000010000010001000000011001100000111001101000100001001000000000100001
    619         a union b       111101111001111111111101111001111101111011111001111001111111111111111001111011111101111101111111111001111011111001111001111001111101111001111101111111
    620         a minus b      111001011000000001001000101000111001110000101000011000111001011001110000011000110001101001101000111001100000011001000000010001011000110001111001011000
    621         a symmetric_difference b      111101011000000111001100101000111101110010101000011000111111011111110000011010110101101101101110111001100010011001000000010001011100110001111101011110
    622         a.rshift(n)  001111001111001111001111001111001111001111001111001111001111001111001111001111001000000000000000000000000000000000000000000000000000000000000000000000
    623         a.lshift(n)  000000000000000000000000000000000000000000000000000000000000000000000111001111001111001111001111001111001111001111001111001111001111001111001111001111
    624         a.first()           0
    625         a.next(n)           71
    626         a.first_diff(b)     0
    627         a.next_diff(b, n)   73
    628         a.hamming_weight()  100
    629         a.hamming_weight_sparse()  100
    630         a.map(m)  100111100111100111100111100111100111100111100111100111100111100111100111100111100111100111100111100111100111100111100111100111100111100111100111100111
    631         a == loads(dumps(a))  True
    632         rshifts add  True
    633         lshifts add  True
    634         intersection commutes True
    635         union commutes  True
    636         not not = id True
    637         flipped bit  69
    638         add bit      69
    639         discard bit    69
    640         lshift add unset ok True
    641         rshift set unset ok True
    642         reallocating a      111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001
    643         to size 69          111001111001111001111001111001111001111001111001111001111001111001111
    644         to size 138          111001111001111001111001111001111001111001111001111001111001111001111000000000000000000000000000000000000000000000000000000000000000000000
    645         to original size    111001111001111001111001111001111001111001111001111001111001111001111000000000000000000000000000000000000000000000000000000000000000000000000000000000
    646 
    647     """
    648     cdef bint bit = True
    649     cdef bitset_t a, b, r
    650 
    651     bitset_from_str(a, py_a)
    652     bitset_from_str(b, py_b)
    653 
    654     if a.size != b.size:
    655         raise ValueError("inputs must have same size")
    656 
    657     print "a", bitset_string(a)
    658     print "list a", bitset_list(a)
    659     print "a.size", a.size
    660     print "len(a)", bitset_len(a)
    661     print "a.limbs", a.limbs
    662     print "b", bitset_string(b)
    663     print "a.in(n)  ", bitset_in(a, n)
    664     print "a.not_in(n)  ", bitset_not_in(a, n)
    665     bitset_add(a, n)
    666     print "a.add(n)    ", bitset_string(a)
    667     bitset_from_str(a, py_a)
    668     bitset_discard(a, n)
    669     print "a.discard(n)  ", bitset_string(a)
    670     bitset_from_str(a, py_a)
    671     bitset_set_to(a, n, bit)
    672     print "a.set_to(n) ", bitset_string(a)
    673     bitset_from_str(a, py_a)
    674     bitset_flip(a, n)
    675     print "a.flip(n)   ", bitset_string(a)
    676     bitset_set_first_n(a, n)
    677     print "a.set_first_n(n)   ", bitset_string(a)
    678     print "a.first_in_complement()   ", bitset_first_in_complement(a)
    679 
    680     bitset_from_str(a, py_a)
    681     bitset_from_str(b, py_b)
    682     print "a.isempty() ", bitset_isempty(a)
    683     print "a.eq(b)     ", bitset_eq(a, b)
    684     print "a.cmp(b)    ", bitset_cmp(a, b)
    685     print "a.lex_cmp(b)", bitset_lex_cmp(a, b)
    686     print "a.issubset(b)", bitset_issubset(a, b)
    687     print "a.issuperset(b)", bitset_issuperset(a, b)
    688 
    689     bitset_from_str(a, py_a)
    690     bitset_from_str(b, py_b)
    691 
    692     bitset_init(r, a.size)
    693     bitset_copy(r, a)
    694     print "a.copy()    ", bitset_string(r)
    695     bitset_clear(r)
    696     print "r.clear()    ", bitset_string(r)
    697     bitset_complement(r, a)
    698     print "complement a       ", bitset_string(r)
    699     bitset_intersection(r, a, b)
    700     print "a intersect b     ", bitset_string(r)
    701     bitset_union(r, a, b)
    702     print "a union b      ", bitset_string(r)
    703     bitset_difference(r, a, b)
    704     print "a minus b     ", bitset_string(r)
    705     bitset_symmetric_difference(r, a, b)
    706     print "a symmetric_difference b     ", bitset_string(r)
    707 
    708     bitset_rshift(r, a, n)
    709     print "a.rshift(n) ", bitset_string(r)
    710 
    711     bitset_lshift(r, a, n)
    712     print "a.lshift(n) ", bitset_string(r)
    713 
    714     print "a.first()          ", bitset_first(a)
    715     print "a.next(n)          ", bitset_next(a, n)
    716     print "a.first_diff(b)    ", bitset_first_diff(a, b)
    717     print "a.next_diff(b, n)  ", bitset_next_diff(a, b, n)
    718 
    719     print "a.hamming_weight() ", bitset_hamming_weight(a)
    720     print "a.hamming_weight_sparse() ", bitset_hamming_weight_sparse(a)
    721 
    722     morphism = {}
    723     for i in xrange(a.size):
    724         morphism[i] = a.size - i - 1
    725     bitset_map(r, a, morphism)
    726     print "a.map(m) ", bitset_string(r)
    727 
    728     data = bitset_pickle(a)
    729     bitset_unpickle(r, data)
    730     print "a == loads(dumps(a)) ", bitset_eq(r, a)
    731 
    732     cdef bitset_t s
    733     bitset_init(s, a.size)
    734 
    735     if a.size > 100:
    736         bitset_rshift(r, b, 3)
    737         bitset_rshift(r, r, 77)
    738         bitset_rshift(s, b, 80)
    739         print "rshifts add ", bitset_eq(s, r)
    740 
    741         bitset_lshift(r, b, 69)
    742         bitset_lshift(r, r, 6)
    743         bitset_lshift(s, b, 75)
    744         print "lshifts add ", bitset_eq(s, r)
    745 
    746         bitset_intersection(r, a, b)
    747         bitset_intersection(s, b, a)
    748         print "intersection commutes", bitset_eq(s, r)
    749 
    750         bitset_union(r, a, b)
    751         bitset_union(s, b, a)
    752         print "union commutes ", bitset_eq(s, r)
    753 
    754         bitset_complement(r, b)
    755         bitset_complement(s, r)
    756         print "not not = id", bitset_eq(s, b)
    757 
    758         bitset_copy(r, b)
    759         bitset_flip(r, n)
    760         print "flipped bit ", bitset_first_diff(b, r)
    761 
    762         bitset_clear(r)
    763         bitset_add(r, n)
    764         print "add bit     ", bitset_first(r)
    765 
    766         bitset_clear(r)
    767         bitset_complement(r, r)
    768         bitset_discard(r, n)
    769         bitset_complement(r, r)
    770         print "discard bit   ", bitset_first(r)
    771 
    772         bitset_clear(r)
    773         bitset_add(r, 10)
    774         bitset_lshift(r, r, 68)
    775         bitset_flip(r, 78)
    776         print "lshift add unset ok", bitset_isempty(r)
    777 
    778         bitset_clear(r)
    779         bitset_add(r, 19)
    780         bitset_rshift(r, r, 8)
    781         bitset_discard(r, 11)
    782         print "rshift set unset ok", bitset_isempty(r)
    783 
    784     print "reallocating a     ", bitset_string(a)
    785     bitset_realloc(a, n)
    786     print "to size %d         " % n, bitset_string(a)
    787     bitset_realloc(a, 2 * n)
    788     print "to size %d         " % (2 * n), bitset_string(a)
    789     bitset_realloc(a, b.size)
    790     print "to original size   ", bitset_string(a)
    791 
    792     bitset_free(a)
    793     bitset_free(b)
    794     bitset_free(r)
    795     bitset_free(s)
    796 
    797 
    798 def test_bitset_set_first_n(py_a, long n):
    799     """
    800     Test the bitset function set_first_n.
    801 
    802     TESTS::
    803 
    804         sage: from sage.misc.misc_c import test_bitset_set_first_n
    805         sage: test_bitset_set_first_n('00'*64, 128)
    806         a.set_first_n(n)    11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111
    807 
    808     """
    809     cdef bint bit = True
    810     cdef bitset_t a
    811 
    812     bitset_from_str(a, py_a)
    813     bitset_set_first_n(a, n)
    814     print "a.set_first_n(n)   ", bitset_string(a)
    815     bitset_free(a)
    816 
    817 
    818 def test_bitset_remove(py_a, long n):
    819     """
    820     Test the bitset_remove function.
    821 
    822     TESTS::
    823 
    824         sage: from sage.misc.misc_c import test_bitset_remove
    825         sage: test_bitset_remove('01', 0)
    826         Traceback (most recent call last):
    827         ...
    828         KeyError: 0L
    829         sage: test_bitset_remove('01', 1)
    830         a 01
    831         a.size 2
    832         a.limbs 1
    833         n 1
    834         a.remove(n)   00
    835     """
    836     cdef bitset_t a
    837     bitset_from_str(a, py_a)
    838 
    839     print "a", bitset_string(a)
    840     print "a.size", a.size
    841     print "a.limbs", a.limbs
    842     print "n", n
    843 
    844     bitset_remove(a, n)
    845     print "a.remove(n)  ", bitset_string(a)
    846 
    847     bitset_free(a)
    848 
    849 
    850 def test_bitset_pop(py_a):
    851     """
    852     Tests for the bitset_pop function.
    853 
    854     TESTS::
    855 
    856         sage: from sage.misc.misc_c import test_bitset_pop
    857         sage: test_bitset_pop('0101')
    858         a.pop()   1
    859         new set:  0001
    860         sage: test_bitset_pop('0000')
    861         Traceback (most recent call last):
    862         ...
    863         KeyError: 'pop from an empty set'
    864     """
    865     cdef bitset_t a
    866     bitset_from_str(a, py_a)
    867     i = bitset_pop(a)
    868     print "a.pop()  ", i
    869     print "new set: ", bitset_string(a)
    870     bitset_free(a)
    871 
    872 
    873 def test_bitset_unpickle(data):
    874     """
    875     This (artificially) tests pickling of bitsets across systems.
    876 
    877     INPUT:
    878 
    879     - ``data`` -- A tuple of data as would be produced by the internal, Cython-only, method ``bitset_pickle``.
    880 
    881     OUTPUT:
    882 
    883     A list form of the bitset corresponding to the pickled data.
    884 
    885     EXAMPLES:
    886 
    887     We compare 64-bit and 32-bit encoding. Both should unpickle on any system::
    888 
    889         sage: from sage.misc.misc_c import test_bitset_unpickle
    890         sage: test_bitset_unpickle((0, 100, 2, 8, (33, 6001)))
    891         [0, 5, 64, 68, 69, 70, 72, 73, 74, 76]
    892         sage: test_bitset_unpickle((0, 100, 4, 4, (33, 0, 6001, 0)))
    893         [0, 5, 64, 68, 69, 70, 72, 73, 74, 76]
    894     """
    895     cdef bitset_t bs
    896     bitset_init(bs, 1)
    897     bitset_unpickle(bs, data)
    898     L = bitset_list(bs)
    899     bitset_free(bs)
    900     return L
    901 
    902434#################################################################
    903435# 32/64-bit computer?
    904436#################################################################