Ticket #13352: trac_13352_bitset_len_v4.patch
File trac_13352_bitset_len_v4.patch, 56.5 KB (added by , 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 73 73 ham_wts[i] = ham_wts[i & 255] + ham_wts[(i>>8) & 255] 74 74 return ham_wts 75 75 76 include 'sage/misc/bitset_pxd.pxi'77 76 include 'sage/misc/bitset.pxi' 78 77 def weight_dist(M): 79 78 """ -
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 36 36 from sage.rings.finite_rings.finite_field_givaro import FiniteField_givaro 37 37 from sage.rings.polynomial.polynomial_element import is_Polynomial 38 38 39 include "sage/misc/bitset_pxd.pxi"40 39 include "sage/misc/bitset.pxi" 41 40 from cpython.string cimport * 42 41 -
sage/doctest/sources.py
diff --git a/sage/doctest/sources.py b/sage/doctest/sources.py
a b 668 668 ....: dirs.sort(); files.sort() 669 669 ....: for F in files: 670 670 ....: _, ext = os.path.splitext(F) 671 ....: if ext in ('.py', '.pyx', '. sage', '.spyx', '.rst'):671 ....: if ext in ('.py', '.pyx', '.pxi', '.sage', '.spyx', '.rst'): 672 672 ....: filename = os.path.join(path, F) 673 673 ....: FDS = FileDocTestSource(filename, DocTestDefaults(long=True,optional=True)) 674 674 ....: FDS._test_enough_doctests(verbose=False) -
sage/ext/cdefs.pxi
diff --git a/sage/ext/cdefs.pxi b/sage/ext/cdefs.pxi
a b 3 3 # 4 4 5 5 from libc.stdio cimport * 6 from libc.string cimport strlen, strcpy, memset, memcpy 6 from libc.string cimport strlen, strcpy, memset, memcpy, memcmp 7 7 8 8 from libc.math cimport sqrt, frexp, ldexp 9 9 -
sage/graphs/base/c_graph.pxd
diff --git a/sage/graphs/base/c_graph.pxd b/sage/graphs/base/c_graph.pxd
a b 5 5 # http://www.gnu.org/licenses/ 6 6 #************************************************************************** 7 7 8 include "sage/misc/bitset_pxd.pxi" 8 from sage.misc.bitset cimport bitset_t 9 9 10 10 cdef class CGraph: 11 11 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 8 8 9 9 from c_graph cimport CGraph 10 10 include 'sage/ext/stdsage.pxi' 11 include 'sage/misc/bitset_pxd.pxi'12 11 13 12 cdef class DenseGraph(CGraph): 14 13 # 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 8 8 9 9 from c_graph cimport CGraph 10 10 include 'sage/ext/stdsage.pxi' 11 include 'sage/misc/bitset_pxd.pxi'12 11 13 12 cdef struct SparseGraphLLNode: 14 13 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 1 1 ctypedef unsigned short ushort 2 2 3 include "sage/misc/bitset_pxd.pxi" 3 from cpython cimport PyObject 4 4 5 5 cdef extern from "stdlib.h": 6 6 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 264 264 g.neighbors[v_id][i] = j 265 265 edge_labels[(g.neighbors[v_id]+i)-g.edges] = label 266 266 267 g.edge_labels = < void *> edge_labels267 g.edge_labels = <PyObject *> <void *> edge_labels 268 268 cpython.Py_XINCREF(g.edge_labels) 269 269 270 270 cdef inline int n_edges(short_digraph g): … … 291 291 292 292 if src.edge_labels != NULL: 293 293 edge_labels = [None]*n_edges(src) 294 dst.edge_labels = < void *> edge_labels294 dst.edge_labels = <PyObject *> <void *> edge_labels 295 295 cpython.Py_XINCREF(dst.edge_labels) 296 296 297 297 cdef 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" 1 from sage.misc.bitset cimport bitset_t 2 2 3 3 cdef class ConvexityProperties: 4 4 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 127 127 # http://www.gnu.org/licenses/ 128 128 ############################################################################## 129 129 130 include "sage/misc/bitset_pxd.pxi"131 130 include "sage/misc/bitset.pxi" 132 131 from libc.stdint cimport uint64_t 133 132 from 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 16 16 #***************************************************************************** 17 17 18 18 include "sage/ext/interrupt.pxi" 19 include 'sage/ext/cdefs.pxi' 19 include 'sage/ext/cdefs.pxi' 20 20 include 'sage/ext/stdsage.pxi' 21 21 22 22 # import from Python standard library 23 23 from sage.misc.prandom import random 24 24 25 # import from third-party library 26 from sage.graphs.base.sparse_graph cimport SparseGraph 25 # import from third-party library 26 from sage.graphs.base.sparse_graph cimport SparseGraph 27 27 28 cdef extern from *:29 double sqrt(double)30 28 31 29 cdef class GenericGraph_pyx(SageObject): 32 30 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 124 124 125 125 126 126 include 'sage/ext/stdsage.pxi' 127 include 'sage/ misc/bitset_pxd.pxi'127 include 'sage/ext/cdefs.pxi' 128 128 include "sage/ext/interrupt.pxi" 129 129 130 130 cdef 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 39 39 # http://www.gnu.org/licenses/ 40 40 ############################################################################## 41 41 42 include "sage/misc/bitset_pxd.pxi"43 42 include "sage/misc/bitset.pxi" 44 43 45 44 cdef 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 7 7 8 8 include 'sage/ext/cdefs.pxi' 9 9 include 'sage/ext/stdsage.pxi' 10 include 'sage/misc/bitset_pxd.pxi' 11 12 cdef extern from "stdlib.h": 13 int rand() 10 from sage.misc.bitset cimport * 11 from libc.stdlib cimport rand 14 12 15 13 cdef extern from "flint/ulong_extras.h": 16 14 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' 1 from sage.misc.bitset cimport * 2 2 3 3 DEF BINT_EXCEPT = -2**31 - 1 # def. repeated in .pyx file 4 4 -
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' 1 from sage.misc.bitset cimport bitset_t 2 2 from matroid cimport Matroid 3 3 from basis_exchange_matroid cimport BasisExchangeMatroid 4 4 from 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' 1 from sage.misc.bitset cimport bitset_t 2 2 from basis_matroid cimport BasisMatroid 3 3 4 4 cdef 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' 1 from sage.misc.bitset cimport bitset_t 2 2 3 3 cdef class LeanMatrix: 4 4 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' 1 from sage.misc.bitset cimport bitset_t 2 2 3 3 from matroid cimport Matroid 4 4 from 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' 1 from sage.misc.bitset cimport bitset_t 2 2 3 3 cdef class SetSystem: 4 4 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 """ 2 Cython 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 #***************************************************************************** 2 12 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 18 cdef 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 27 cdef 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 52 ctypedef bitset_s bitset_t[1] 53 54 55 # Python layer over bitset_t 3 56 cdef class FrozenBitset: 4 57 cdef bitset_t _bitset 5 58 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 16 16 # Copyright (C) 2008 Robert Bradshaw <robertwb@math.washington.edu> 17 17 # 18 18 # 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. 27 21 # http://www.gnu.org/licenses/ 28 22 #***************************************************************************** 29 23 24 include 'sage/ext/cdefs.pxi' 25 include 'sage/ext/stdsage.pxi' 26 from sage.libs.gmp.mpn cimport * 27 from sage.misc.bitset cimport * 30 28 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 33 30 34 31 ############################################################################# 35 32 # Bitset Initalization … … 437 434 """ 438 435 Calculate the number of items in the set (i.e., the number of nonzero bits). 439 436 """ 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) 446 438 447 439 cdef inline long bitset_hash(bitset_t bits): 448 440 """ … … 645 637 # Hamming Weights 646 638 ############################################################################# 647 639 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 640 cdef inline long bitset_hamming_weight(bitset_t a): 641 return bitset_len(a) 682 642 683 643 ############################################################################# 684 644 # Bitset Conversion -
sage/misc/bitset.pyx
diff --git a/sage/misc/bitset.pyx b/sage/misc/bitset.pyx
a b 1407 1407 set([]) 1408 1408 """ 1409 1409 bitset_clear(self._bitset) 1410 1411 1412 ############################################################################# 1413 # Bitset Testing: test basic Cython bitsets 1414 ############################################################################# 1415 1416 def 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 1767 def 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 1787 def 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 1819 def 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 1842 def 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 of8 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU9 # 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 literals27 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 the31 # corresponding limb number, while (n & offset_mask) gives the bit32 # 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 can36 # hold. This size is independent of how many elements of B are toggled to37 # 1. For example, say B is the bitset 1001. Then B has size 4, with the38 # 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 size41 42 # A limb is that part of a bitset that can fit into a machine word. On an43 # n-bit machine, a word is usually taken to be n bits in length. Thus on a44 # 32-bit machine, a word usually is 32 bits in length. Similarly, on a45 # 64-bit machine, a word is usually 64 bits in length. If a bitset has46 # size <= n, then the whole bitset fits into a limb and we only require47 # 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, if49 # a limb is 64 bits in length and the bitset has size 96 bits, then we50 # require at most two limbs to represent the bitset.51 long limbs52 53 # The individual bits of a bitset.54 unsigned long *bits55 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 431 431 return balanced_list_sum(L, offset, k, cutoff) + balanced_list_sum(L, offset + k, count - k, cutoff) 432 432 433 433 434 #############################################################################435 # Bitset Testing436 #############################################################################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_bitset449 sage: test_bitset('00101', '01110', 4)450 a 00101451 list a [2, 4]452 a.size 5453 len(a) 2454 a.limbs 1455 b 01110456 a.in(n) True457 a.not_in(n) False458 a.add(n) 00101459 a.discard(n) 00100460 a.set_to(n) 00101461 a.flip(n) 00100462 a.set_first_n(n) 11110463 a.first_in_complement() 4464 a.isempty() False465 a.eq(b) False466 a.cmp(b) 1467 a.lex_cmp(b) -1468 a.issubset(b) False469 a.issuperset(b) False470 a.copy() 00101471 r.clear() 00000472 complement a 11010473 a intersect b 00100474 a union b 01111475 a minus b 00001476 a symmetric_difference b 01011477 a.rshift(n) 10000478 a.lshift(n) 00000479 a.first() 2480 a.next(n) 4481 a.first_diff(b) 1482 a.next_diff(b, n) 4483 a.hamming_weight() 2484 a.hamming_weight_sparse() 2485 a.map(m) 10100486 a == loads(dumps(a)) True487 reallocating a 00101488 to size 4 0010489 to size 8 00100000490 to original size 00100491 492 ::493 494 sage: test_bitset('11101', '11001', 2)495 a 11101496 list a [0, 1, 2, 4]497 a.size 5498 len(a) 4499 a.limbs 1500 b 11001501 a.in(n) True502 a.not_in(n) False503 a.add(n) 11101504 a.discard(n) 11001505 a.set_to(n) 11101506 a.flip(n) 11001507 a.set_first_n(n) 11000508 a.first_in_complement() 2509 a.isempty() False510 a.eq(b) False511 a.cmp(b) 1512 a.lex_cmp(b) 1513 a.issubset(b) False514 a.issuperset(b) True515 a.copy() 11101516 r.clear() 00000517 complement a 00010518 a intersect b 11001519 a union b 11101520 a minus b 00100521 a symmetric_difference b 00100522 a.rshift(n) 10100523 a.lshift(n) 00111524 a.first() 0525 a.next(n) 2526 a.first_diff(b) 2527 a.next_diff(b, n) 2528 a.hamming_weight() 4529 a.hamming_weight_sparse() 4530 a.map(m) 10111531 a == loads(dumps(a)) True532 reallocating a 11101533 to size 2 11534 to size 4 1100535 to original size 11000536 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 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000541 list a []542 a.size 128543 len(a) 0544 a.limbs ...545 b 01010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101546 a.in(n) False547 a.not_in(n) True548 a.add(n) 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001549 a.discard(n) 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000550 a.set_to(n) 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001551 a.flip(n) 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001552 a.set_first_n(n) 11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111110553 a.first_in_complement() 127554 a.isempty() True555 a.eq(b) False556 a.cmp(b) -1557 a.lex_cmp(b) -1558 a.issubset(b) True559 a.issuperset(b) False560 a.copy() 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000561 r.clear() 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000562 complement a 11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111563 a intersect b 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000564 a union b 01010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101565 a minus b 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000566 a symmetric_difference b 01010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101567 a.rshift(n) 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000568 a.lshift(n) 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000569 a.first() -1570 a.next(n) -1571 a.first_diff(b) 1572 a.next_diff(b, n) 127573 a.hamming_weight() 0574 a.hamming_weight_sparse() 0575 a.map(m) 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000576 a == loads(dumps(a)) True577 rshifts add True578 lshifts add True579 intersection commutes True580 union commutes True581 not not = id True582 flipped bit 127583 add bit 127584 discard bit 127585 lshift add unset ok True586 rshift set unset ok True587 reallocating a 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000588 to size 127 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000589 to size 254 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000590 to original size 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000591 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 111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001596 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 150598 len(a) 100599 a.limbs ...600 b 000100100001111110110101010001000100001011010001100001000110100110001001100011001100010100010111000000011011100000111001101000100101001000000100100111601 a.in(n) False602 a.not_in(n) True603 a.add(n) 111001111001111001111001111001111001111001111001111001111001111001111101111001111001111001111001111001111001111001111001111001111001111001111001111001604 a.discard(n) 111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001605 a.set_to(n) 111001111001111001111001111001111001111001111001111001111001111001111101111001111001111001111001111001111001111001111001111001111001111001111001111001606 a.flip(n) 111001111001111001111001111001111001111001111001111001111001111001111101111001111001111001111001111001111001111001111001111001111001111001111001111001607 a.set_first_n(n) 111111111111111111111111111111111111111111111111111111111111111111111000000000000000000000000000000000000000000000000000000000000000000000000000000000608 a.first_in_complement() 69609 a.isempty() False610 a.eq(b) False611 a.cmp(b) -1612 a.lex_cmp(b) 1613 a.issubset(b) False614 a.issuperset(b) False615 a.copy() 111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001616 r.clear() 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000617 complement a 000110000110000110000110000110000110000110000110000110000110000110000110000110000110000110000110000110000110000110000110000110000110000110000110000110618 a intersect b 000000100001111000110001010001000000001001010001100001000000100000001001100001001000010000010001000000011001100000111001101000100001001000000000100001619 a union b 111101111001111111111101111001111101111011111001111001111111111111111001111011111101111101111111111001111011111001111001111001111101111001111101111111620 a minus b 111001011000000001001000101000111001110000101000011000111001011001110000011000110001101001101000111001100000011001000000010001011000110001111001011000621 a symmetric_difference b 111101011000000111001100101000111101110010101000011000111111011111110000011010110101101101101110111001100010011001000000010001011100110001111101011110622 a.rshift(n) 001111001111001111001111001111001111001111001111001111001111001111001111001111001000000000000000000000000000000000000000000000000000000000000000000000623 a.lshift(n) 000000000000000000000000000000000000000000000000000000000000000000000111001111001111001111001111001111001111001111001111001111001111001111001111001111624 a.first() 0625 a.next(n) 71626 a.first_diff(b) 0627 a.next_diff(b, n) 73628 a.hamming_weight() 100629 a.hamming_weight_sparse() 100630 a.map(m) 100111100111100111100111100111100111100111100111100111100111100111100111100111100111100111100111100111100111100111100111100111100111100111100111100111631 a == loads(dumps(a)) True632 rshifts add True633 lshifts add True634 intersection commutes True635 union commutes True636 not not = id True637 flipped bit 69638 add bit 69639 discard bit 69640 lshift add unset ok True641 rshift set unset ok True642 reallocating a 111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001643 to size 69 111001111001111001111001111001111001111001111001111001111001111001111644 to size 138 111001111001111001111001111001111001111001111001111001111001111001111000000000000000000000000000000000000000000000000000000000000000000000645 to original size 111001111001111001111001111001111001111001111001111001111001111001111000000000000000000000000000000000000000000000000000000000000000000000000000000000646 647 """648 cdef bint bit = True649 cdef bitset_t a, b, r650 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.size660 print "len(a)", bitset_len(a)661 print "a.limbs", a.limbs662 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 - 1725 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 s733 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_n805 sage: test_bitset_set_first_n('00'*64, 128)806 a.set_first_n(n) 11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111807 808 """809 cdef bint bit = True810 cdef bitset_t a811 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_remove825 sage: test_bitset_remove('01', 0)826 Traceback (most recent call last):827 ...828 KeyError: 0L829 sage: test_bitset_remove('01', 1)830 a 01831 a.size 2832 a.limbs 1833 n 1834 a.remove(n) 00835 """836 cdef bitset_t a837 bitset_from_str(a, py_a)838 839 print "a", bitset_string(a)840 print "a.size", a.size841 print "a.limbs", a.limbs842 print "n", n843 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_pop857 sage: test_bitset_pop('0101')858 a.pop() 1859 new set: 0001860 sage: test_bitset_pop('0000')861 Traceback (most recent call last):862 ...863 KeyError: 'pop from an empty set'864 """865 cdef bitset_t a866 bitset_from_str(a, py_a)867 i = bitset_pop(a)868 print "a.pop() ", i869 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_unpickle890 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 bs896 bitset_init(bs, 1)897 bitset_unpickle(bs, data)898 L = bitset_list(bs)899 bitset_free(bs)900 return L901 902 434 ################################################################# 903 435 # 32/64-bit computer? 904 436 #################################################################