File: _ba.pyx

package info (click to toggle)
python-iow 1.0.8-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 660 kB
  • sloc: python: 2,322; makefile: 24; sh: 12
file content (71 lines) | stat: -rw-r--r-- 1,693 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
cimport libc.stdlib
from cpython cimport Py_INCREF, Py_DECREF, bool
cimport numpy as np
np.import_array()

cdef class bitarray:
    cdef:
        BIT_ARRAY* bitarr
        bit_index_t nbits   ### REDUNDANT as the BIT_ARRAY struct has this

    def __cinit__(self, bit_index_t n):
        self.nbits = n
        self.bitarr = bit_array_create(n)

    def __dealloc__(self):
        bit_array_free(self.bitarr)

    def __str__(self):
        cdef char* str_
        cdef object result

        str_ = <char*>libc.stdlib.malloc(self.nbits + 1)
        result = tounicode(bit_array_to_str(self.bitarr, str_))
        libc.stdlib.free(str_)

        return result

    def __getitem__(self, bit_index_t i):
        # could inline the macro version as well, less safe but should be 
        # thinner code
        return bit_array_get_bit(self.bitarr, i)

    def __setitem__(self, bit_index_t i, bool v):
        if v:
            bit_array_set_bit(self.bitarr, i)
        else:
            bit_array_clear_bit(self.bitarr, i)

    

cdef unicode tounicode(char* s):
    # from http://docs.cython.org/en/latest/src/tutorial/strings.html
    return s.decode('UTF-8', 'strict')


cpdef bitarray bitarray_factory(object vec):
    """Construct a bitarray from a vector

    A value at a position in the vector is considered set if the value 
    evaluates True.

    Parameters
    ----------
    vec : object
        An iterable

    Returns
    -------
    bitarray
        The bitarray based off of topo
    """
    cdef int i
    cdef int n = len(vec)
    cdef bitarray result
    
    result = bitarray(n)
    for i in range(n):
        if vec[i]:
            result[i] = True

    return result