===============
Bitarray module
===============

This (C extension) module provides an object type which can efficiently
represent a bitarray.  Bitarrays are sequence types and behave very much
like usual lists.  Each bit is represented as an actual bit in memory.
For example, this allows the storage of 8Gbits in 1GB of memory.

Requires Python 2.5 or greater, see PEP 353.


Installation
------------

bitarray can be installed from source::

   $ tar xzvf bitarray-0.1.1.tar.gz
   $ python setup.py install

On Unix systems, the latter command may be executed with root privileges.

If you have setuptools_ installed on your system, you can
easy_install bitarray.

.. _setuptools : http://pypi.python.org/pypi/setuptools


Using the module
----------------

Here are a few usage example, pointing out some differences to lists.

   >>> from bitarray import bitarray
   >>> a = bitarray()            # create empty bitarray
   >>> a.append(True)
   >>> a.extend([False, True])
   >>> a
   bitarray('101')
   >>> a[-1]
   True
   >>> del a[1]
   >>> len(a)
   2

Creating objects:

   >>> a = bitarray(1024)        # bitarray of length 1024 (uninitialized)
   >>> bitarray('1001011')       # from string
   bitarray('1001011')
   >>> lst = [True, False, False, True, False, True, True]
   >>> bitarray(lst)             # from list, tuple, sequence, iterable
   bitarray('1001011')

Bits can be assigned from any Python object.
Whenever bits are assigned, the built-in `bool()` function is used to
determine the value of the bit.  (Actually the C equivalent of `bool()`
is used, but one can think of it that way.)

   >>> a = bitarray([42, '', True, {}, 'foo', None])
   >>> a
   bitarray('101010')
   >>> a.append(a)      # note that bool(a) is True
   >>> a
   bitarray('1010101')
   >>> a.count(42)      # counts occurrences of True
   4L
   >>> a.remove('')     # removes first occurence of False
   >>> a
   bitarray('110101')

Like lists, bitarray objects support slice assignment and deletion:

   >>> a = bitarray(50)
   >>> a.setall(False)
   >>> a[11:37:3] = 9*bitarray([True])
   >>> a
   bitarray('00000000000100100100100100100100100100000000000000')
   >>> del a[12::3]
   >>> a
   bitarray('0000000000010101010101010101000000000')
   >>> a[-6:] = bitarray('10011')
   >>> a
   bitarray('000000000001010101010101010100010011')
   >>> a += bitarray('000111')
   >>> a
   bitarray('000000000001010101010101010100010011000111')


A bitarray has the following methods available:

`append(x)`

   Append new value bool(x) to the end of the bitarray.


`buffer_info()`

   Return a tuple (address, length) giving the current memory address and the
   length in bytes used to hold the bitarray's contents.  The length in bytes
   multiplied by 8 is slightly larger than the number of bits the bitarray
   holds.


`bytereverse()`

   Reverse the order of all bits for all machine values representing the
   bitarray.


`count(x)`

   Return number of occurences of x in the bitarray.


`extend(bitarray or iterable)`

   Append bits to the end of the bitarray.
   When you want to extend from a list, it's faster to use fromlist.


`fill()`

   When the length of the bitarray is not a mutiple of 8, increase the length
   slightly such that the new length is a mutiple of 8, and set the few new
   bits to zero.


`from01(string)`

   Appends items from the string (containing '0's and '1's) to the bitarray.


`fromfile(f[, n])`

   Read n bytes (not bits) from the file object f and append them to the end
   of the bitarray, i.e. 8*n bits will be added.
   When n is omitted, as many bytes are read until EOF is reached.


`fromint(n)`

   Convert the integer number n to binary, and a append the result to the
   bitarray.  For example, fromint(6) has the same effect as extend([0, 1, 1])


`fromlist(list)`

   Append bits to bitarray from list.


`fromstring(string)`

   Appends bits from the string, interpreting it as an bitarray of machine
   values, as if it had been read from a file using the fromfile() method).


`index(x)`

   Return index of first occurence of x in the bitarray.  It is an error when x
   does not occur in the bitarray


`insert(i, x)`

   Insert a new item x into the bitarray before position i.


`invert(x)`

   Invert all bits in the bitarray, i.e. convert each 1-bit into a 0-bit
   and vice versa.


`length()`

   Return the length (number of bits) of the bitarray.


`pop([i])`

   Return the i-th element and delete it from the bitarray. i defaults to -1.


`remove(x)`

   Remove the first occurence of x in the bitarray.


`reverse()`

   reverse the order of bits in the bitarray.


`setall(x)`

   Set all bits in the bitarray to x.


`to01(string)`

   Return a string containing '0's and '1's, representing the bits in the
   bitarray object.


`tofile(f)`

   Write all bits (as machine values) to the file object f.
   When the length of the bitarray is not a mutiple of 8,
   the few remaining bits are filled with zeros.


`tolist()`

   Return an ordinary list with the items in the bitarray.


`tostring()`

   Return the string representing (machine values) of the bitarray.
   When the length of the bitarray is not a mutiple of 8, the few remaining
   bits are filled with zeros.


