Skip to content
This repository has been archived by the owner on Jan 30, 2023. It is now read-only.

Commit

Permalink
Change inlining and signal handling; use freelist more efficiently
Browse files Browse the repository at this point in the history
  • Loading branch information
simon-king-jena committed Aug 27, 2015
1 parent 0b9ecbc commit 8d5d846
Show file tree
Hide file tree
Showing 4 changed files with 144 additions and 173 deletions.
55 changes: 19 additions & 36 deletions src/sage/data_structures/bitset.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ cdef inline mp_limb_t limb_lower_bits_up(mp_bitcnt_t n):
#############################################################################
# Bitset Initalization
#############################################################################
cdef bint bitset_init(bitset_t bits, mp_bitcnt_t size) except -1:
cdef inline bint bitset_init(bitset_t bits, mp_bitcnt_t size) except -1:
"""
Allocate an empty bitset of size ``size``.
Expand All @@ -80,11 +80,9 @@ cdef bint bitset_init(bitset_t bits, mp_bitcnt_t size) except -1:

bits.size = size
bits.limbs = (size - 1) / (8 * sizeof(mp_limb_t)) + 1
bits.bits = <mp_limb_t*>sage_calloc(bits.limbs, sizeof(mp_limb_t))
if bits.bits == NULL:
raise MemoryError
bits.bits = <mp_limb_t*>check_calloc(bits.limbs, sizeof(mp_limb_t))

cdef bint bitset_realloc(bitset_t bits, mp_bitcnt_t size) except -1:
cdef inline bint bitset_realloc(bitset_t bits, mp_bitcnt_t size) except -1:
"""
Reallocate a bitset to size size. If reallocation is larger, new bitset
does not contain any of the extra bits.
Expand Down Expand Up @@ -150,11 +148,10 @@ cdef inline void bitset_fix(bitset_t bits):
# Bitset Comparison
#############################################################################

cdef bint mpn_equal_bits(mp_srcptr b1, mp_srcptr b2, mp_bitcnt_t n) except -1:
cdef inline bint mpn_equal_bits(mp_srcptr b1, mp_srcptr b2, mp_bitcnt_t n):
"""
Return ``True`` iff the first n bits of *b1 and *b2 agree.
"""
sig_check()
cdef mp_size_t nlimbs = n // GMP_LIMB_BITS
cdef mp_limb_t mask = limb_lower_bits_down(n)
if nlimbs > 0 and mpn_cmp(b1, b2, nlimbs) != 0:
Expand All @@ -166,12 +163,11 @@ cdef bint mpn_equal_bits(mp_srcptr b1, mp_srcptr b2, mp_bitcnt_t n) except -1:
cdef mp_limb_t b2h = b2[nlimbs]
return (b1h ^ b2h) & mask == 0

cdef bint mpn_equal_bits_shifted(mp_srcptr b1, mp_srcptr b2, mp_bitcnt_t n, mp_bitcnt_t offset) except -1:
cdef bint mpn_equal_bits_shifted(mp_srcptr b1, mp_srcptr b2, mp_bitcnt_t n, mp_bitcnt_t offset):
"""
Return ``True`` iff the first n bits of *b1 and the bits ranging from
offset to offset+n of *b2 agree.
"""
sig_check()
cdef mp_bitcnt_t bit_offset = offset % GMP_LIMB_BITS
cdef mp_size_t i2 = offset//GMP_LIMB_BITS
if bit_offset==0:
Expand All @@ -198,7 +194,7 @@ cdef bint mpn_equal_bits_shifted(mp_srcptr b1, mp_srcptr b2, mp_bitcnt_t n, mp_b
tmp_limb |= (b2[preinc(i2)] << neg_bit_offset)
return (b1h ^ tmp_limb) & mask == 0

cdef inline bint bitset_isempty(bitset_t bits) except -1:
cdef inline bint bitset_isempty(bitset_t bits):
"""
Test whether bits is empty. Return True (i.e., 1) if the set is
empty, False (i.e., 0) otherwise.
Expand All @@ -210,10 +206,9 @@ cdef inline bint bitset_isempty(bitset_t bits) except -1:
return True
# Compare bits to itself shifted by 1 limb. If these compare equal,
# all limbs must be 0.
sig_check()
return mpn_cmp(bits.bits+1, bits.bits, bits.limbs-1) == 0

cdef inline bint bitset_is_zero(bitset_t bits) except -1:
cdef inline bint bitset_is_zero(bitset_t bits):
"""
Test whether bits is empty (i.e., zero). Return True (1) if
the set is empty, False (0) otherwise.
Expand All @@ -222,28 +217,26 @@ cdef inline bint bitset_is_zero(bitset_t bits) except -1:
"""
return bitset_isempty(bits)

cdef inline bint bitset_eq(bitset_t a, bitset_t b) except -1:
cdef inline bint bitset_eq(bitset_t a, bitset_t b):
"""
Compare bitset a and b. Return True (i.e., 1) if the sets are
equal, and False (i.e., 0) otherwise.
We assume ``a.limbs >= b.limbs``.
"""
sig_check()
return mpn_cmp(a.bits, b.bits, b.limbs) == 0

cdef inline int bitset_cmp(bitset_t a, bitset_t b) except -2:
cdef inline int bitset_cmp(bitset_t a, bitset_t b):
"""
Compare bitsets a and b. Returns 0 if the two sets are
identical, and consistently return -1 or 1 for two sets that are
not equal.
We assume ``a.limbs >= b.limbs``.
"""
sig_check()
return mpn_cmp(a.bits, b.bits, b.limbs)

cdef inline int bitset_lex_cmp(bitset_t a, bitset_t b) except -2:
cdef inline int bitset_lex_cmp(bitset_t a, bitset_t b):
"""
Compare bitsets ``a`` and ``b`` using lexicographical ordering.
Expand All @@ -264,7 +257,6 @@ cdef inline int bitset_lex_cmp(bitset_t a, bitset_t b) except -2:
Return ``0`` if the two sets are identical, return ``1`` if ``a > b``,
and return ``-1`` if ``a < b``.
"""
sig_check()
cdef long i = bitset_first_diff(a, b)
if i == -1:
return 0
Expand All @@ -273,7 +265,7 @@ cdef inline int bitset_lex_cmp(bitset_t a, bitset_t b) except -2:
else:
return -1

cdef inline bint bitset_issubset(bitset_t a, bitset_t b) except -1:
cdef inline bint bitset_issubset(bitset_t a, bitset_t b):
"""
Test whether a is a subset of b (i.e., every element in a is also
in b).
Expand All @@ -282,12 +274,11 @@ cdef inline bint bitset_issubset(bitset_t a, bitset_t b) except -1:
"""
cdef mp_size_t i
for i from 0 <= i < a.limbs:
sig_check()
if (a.bits[i] & ~b.bits[i]) != 0:
return False
return True

cdef inline bint bitset_issuperset(bitset_t a, bitset_t b) except -1:
cdef inline bint bitset_issuperset(bitset_t a, bitset_t b):
"""
Test whether a is a superset of b (i.e., every element in b is also
in a).
Expand All @@ -297,15 +288,14 @@ cdef inline bint bitset_issuperset(bitset_t a, bitset_t b) except -1:
return bitset_issubset(b, a)


cdef inline bint bitset_are_disjoint(bitset_t a, bitset_t b) except -1:
cdef inline bint bitset_are_disjoint(bitset_t a, bitset_t b):
"""
Tests whether ``a`` and ``b`` have an empty intersection.
We assume ``a.limbs <= b.limbs``.
"""
cdef mp_size_t i
for i from 0 <= i < a.limbs:
sig_check()
if (a.bits[i]&b.bits[i]) != 0:
return False
return True
Expand Down Expand Up @@ -344,7 +334,6 @@ cdef inline bint bitset_remove(bitset_t bits, mp_bitcnt_t n) except -1:
"""
if not bitset_in(bits, n):
raise KeyError(n)
sig_check()
bitset_discard(bits, n)

cdef inline void bitset_discard(bitset_t bits, mp_bitcnt_t n):
Expand Down Expand Up @@ -390,7 +379,7 @@ cdef inline void bitset_flip(bitset_t bits, mp_bitcnt_t n):
"""
bits.bits[n >> index_shift] ^= limb_one_set_bit(n)

cdef void bitset_set_first_n(bitset_t bits, mp_bitcnt_t n):
cdef inline void bitset_set_first_n(bitset_t bits, mp_bitcnt_t n):
"""
Set exactly the first n bits.
"""
Expand Down Expand Up @@ -423,26 +412,24 @@ cdef inline long _bitset_first_in_limb(mp_limb_t limb):
return -1
return mpn_scan1(&limb, 0)

cdef long bitset_first(bitset_t a) except -2:
cdef inline long bitset_first(bitset_t a):
"""
Calculate the index of the first element in the set. If the set
is empty, returns -1.
"""
cdef mp_size_t i
for i from 0 <= i < a.limbs:
sig_check()
if a.bits[i]:
return (i << index_shift) | _bitset_first_in_limb_nonzero(a.bits[i])
return -1

cdef long bitset_first_in_complement(bitset_t a) except -2:
cdef inline long bitset_first_in_complement(bitset_t a):
"""
Calculate the index of the first element not in the set. If the set
is full, returns -1.
"""
cdef mp_size_t i, j
for i from 0 <= i < a.limbs:
sig_check()
if ~a.bits[i]:
j = (i << index_shift) | _bitset_first_in_limb_nonzero(~a.bits[i])
if j >= a.size:
Expand All @@ -461,7 +448,7 @@ cdef inline long bitset_pop(bitset_t a) except -1:
bitset_discard(a, i)
return i

cdef inline long bitset_first_diff(bitset_t a, bitset_t b) except -2:
cdef inline long bitset_first_diff(bitset_t a, bitset_t b):
"""
Calculate the index of the first difference between a and b. If a
and b are equal, then return -1.
Expand All @@ -470,12 +457,11 @@ cdef inline long bitset_first_diff(bitset_t a, bitset_t b) except -2:
"""
cdef mp_size_t i
for i from 0 <= i < a.limbs:
sig_check()
if a.bits[i] != b.bits[i]:
return (i << index_shift) | _bitset_first_in_limb_nonzero(a.bits[i] ^ b.bits[i])
return -1

cdef long bitset_next(bitset_t a, mp_bitcnt_t n) except -2:
cdef inline long bitset_next(bitset_t a, mp_bitcnt_t n):
"""
Calculate the index of the next element in the set, starting at
(and including) n. Return -1 if there are no elements from n
Expand All @@ -489,12 +475,11 @@ cdef long bitset_next(bitset_t a, mp_bitcnt_t n) except -2:
if ret != -1:
return (i << index_shift) | ret
for i from (n >> index_shift) < i < a.limbs:
sig_check()
if a.bits[i]:
return (i << index_shift) | _bitset_first_in_limb_nonzero(a.bits[i])
return -1

cdef long bitset_next_diff(bitset_t a, bitset_t b, mp_bitcnt_t n) except -2:
cdef inline long bitset_next_diff(bitset_t a, bitset_t b, mp_bitcnt_t n):
"""
Calculate the index of the next element that differs between a and
b, starting at (and including) n. Return -1 if there are no
Expand All @@ -510,7 +495,6 @@ cdef long bitset_next_diff(bitset_t a, bitset_t b, mp_bitcnt_t n) except -2:
if ret != -1:
return (i << index_shift) | ret
for i from (n >> index_shift) < i < a.limbs:
sig_check()
if a.bits[i] != b.bits[i]:
return (i << index_shift) | _bitset_first_in_limb(a.bits[i] ^ b.bits[i])
return -1
Expand Down Expand Up @@ -721,7 +705,6 @@ cdef int bitset_map(bitset_t r, bitset_t a, m) except -1:
bitset_clear(r)
i = bitset_first(a)
while i >= 0:
sig_check()
bitset_add(r, m[i])
i = bitset_next(a, i + 1)
return 0
Expand Down
22 changes: 11 additions & 11 deletions src/sage/data_structures/bounded_integer_sequences.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -34,22 +34,22 @@ ctypedef struct biseq_s:
ctypedef biseq_s biseq_t[1]

cdef bint biseq_init(biseq_t R, mp_size_t l, mp_bitcnt_t itemsize) except -1
cdef void biseq_dealloc(biseq_t S)
cdef bint biseq_init_copy(biseq_t R, biseq_t S) except -1
cdef inline void biseq_dealloc(biseq_t S)
cdef inline bint biseq_init_copy(biseq_t R, biseq_t S) except -1
cdef tuple biseq_pickle(biseq_t S)
cdef bint biseq_unpickle(biseq_t R, tuple bitset_data, mp_bitcnt_t itembitsize, mp_size_t length) except -1
cdef bint biseq_init_list(biseq_t R, list data, size_t bound) except -1
cdef inline Py_hash_t biseq_hash(biseq_t S)
cdef int biseq_cmp(biseq_t S1, biseq_t S2) except -2
cdef bint biseq_init_concat(biseq_t R, biseq_t S1, biseq_t S2) except -1
cdef bint biseq_startswith(biseq_t S1, biseq_t S2) except -1
cdef mp_size_t biseq_contains(biseq_t S1, biseq_t S2, mp_size_t start) except -2
cdef mp_size_t biseq_startswith_tail(biseq_t S1, biseq_t S2, mp_size_t start) except -2
cdef mp_size_t biseq_index(biseq_t S, size_t item, mp_size_t start) except -2
cdef size_t biseq_getitem(biseq_t S, mp_size_t index)
cdef inline int biseq_cmp(biseq_t S1, biseq_t S2)
cdef inline bint biseq_init_concat(biseq_t R, biseq_t S1, biseq_t S2) except -1
cdef inline bint biseq_startswith(biseq_t S1, biseq_t S2)
cdef mp_size_t biseq_contains(biseq_t S1, biseq_t S2, mp_size_t start)
cdef mp_size_t biseq_startswith_tail(biseq_t S1, biseq_t S2, mp_size_t start)
cdef inline mp_size_t biseq_index(biseq_t S, size_t item, mp_size_t start)
cdef inline size_t biseq_getitem(biseq_t S, mp_size_t index)
cdef biseq_getitem_py(biseq_t S, mp_size_t index)
cdef void biseq_inititem(biseq_t S, mp_size_t index, size_t item)
cdef void biseq_clearitem(biseq_t S, mp_size_t index)
cdef inline void biseq_inititem(biseq_t S, mp_size_t index, size_t item)
cdef inline void biseq_clearitem(biseq_t S, mp_size_t index)
cdef bint biseq_init_slice(biseq_t R, biseq_t S, mp_size_t start, mp_size_t stop, mp_size_t step) except -1

cdef class BoundedIntegerSequence:
Expand Down

0 comments on commit 8d5d846

Please sign in to comment.