Python: bitarray パッケージ
bitarray のメインモジュールは以下の2つ。
・__init__.py
インポート
from _bitarray import _bitarray, bits2bytes, _sysinfoローカルな関数の定義
def _btree_insert(tree, sym, ba):
def _mk_tree(codedict):
def _check_codedict(codedict):クラス定義
class bitarray(_bitarray):クラス関数(一部)
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のインクルード
#include "Python.h"グローバル変数
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;スタティックな関数(ごくごく一部)
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; }インストール
/*********************** 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); }