6.28.2011

Glancing at the code of 'bitarray'

Python: bitarray パッケージ

bitarray のメインモジュールは以下の2つ。

・__init__.py

インポート

13
from _bitarray import _bitarray, bits2bytes, _sysinfo
ローカルな関数の定義
16
def _btree_insert(tree, sym, ba):
30
def _mk_tree(codedict):
37
def _check_codedict(codedict):
クラス定義
48
class bitarray(_bitarray):
クラス関数(一部)
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
    def decode(self, codedict):
        """decode(code)
 
Given a prefix code (a dict mapping symbols to bitarrays),
decode the content of the bitarray and return the list of symbols."""
        _check_codedict(codedict)
        return self._decode(_mk_tree(codedict))
 
    def encode(self, codedict, iterable):
        """encode(code, iterable)
 
Given a prefix code (a dict mapping symbols to bitarrays),
iterates over iterable object with symbols, and extends the bitarray
with the corresponding bitarray for each symbols."""
        _check_codedict(codedict)
        return self._encode(codedict, iterable)
 
    def search(self, x, limit=-1):
        """search(x[, limit])
 
Given a bitarray x (or an object which can be converted to a bitarray),
returns the start positions of x matching self as a list.
The optional argument limits the number of search results to the integer
specified.  By default, all search results are returned."""
        return self._search(bitarray(x), limit)

・bitarray.c

Python.hのインクルード

11
#include "Python.h"
グローバル変数
33
34
35
36
37
38
39
40
41
42
typedef struct {
    PyObject_VAR_HEAD
    char *ob_item;
    Py_ssize_t allocated;
    idx_t nbits;
    int endian;
    PyObject *weakreflist;   /* List of weak references */
} bitarrayobject;
 
static PyTypeObject Bitarraytype;
スタティックな関数(ごくごく一部)
57
58
59
60
61
62
63
64
65
66
67
68
static void
setbit(bitarrayobject *self, idx_t i, int bit)
{
    char *cp, mask;
 
    mask = BITMASK(self->endian, i);
    cp = self->ob_item + i / 8;
    if (bit)
        *cp |= mask;
    else
        *cp &= ~mask;
}
インストール
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
/*********************** Install Module **************************/
 
PyMODINIT_FUNC
init_bitarray(void)
{
    PyObject *m;
 
    Bitarraytype.ob_type = &PyType_Type;
    BitarrayIter_Type.ob_type = &PyType_Type;
    m = Py_InitModule3("_bitarray", module_functions, 0);
    if (m == NULL)
        return;
 
    Py_INCREF((PyObject *) &Bitarraytype);
    PyModule_AddObject(m, "_bitarray", (PyObject *) &Bitarraytype);
}

http://pypi.python.org/pypi/bitarray

0 件のコメント:

コメントを投稿