| |
Methods defined here:
- __add__(self, other)
- Concatenate the argument bit vector with the bit vector on which the method
is invoked. Return the concatenated bit vector as a new BitVector object.
- __and__(self, other)
- Take a bitwise 'AND' of the bit vector on which the method is invoked with
the argument bit vector. Return the result as a new bit vector. If the two
bit vectors are not of the same size, pad the shorter one with zeros from the
left.
- __contains__(self, otherBitVec)
- This supports 'if x in y' and 'if x not in y' syntax for bit vectors.
- __eq__(self, other)
- # Compare two bit vectors:
- __ge__(self, other)
- __getitem__ = _getbit(self, pos)
- __getslice__(self, i, j)
- Fetch slices with [i:j], [:], etc.
- __gt__(self, other)
- __init__(self, *args, **kwargs)
- __int__ = int_val(self)
- __invert__(self)
- Invert the bits in the bit vector on which the method is invoked
and return the result as a new bit vector.
- __iter__(self)
- To allow iterations over a bit vector by supporting the 'for bit in
bit_vector' syntax:
- __le__(self, other)
- __len__ = _getsize(self)
- __lshift__(self, n)
- For an in-place left circular shift by n bit positions
- __lt__(self, other)
- __ne__(self, other)
- __or__(self, other)
- Take a bitwise 'OR' of the bit vector on which the method is invoked with the
argument bit vector. Return the result as a new bit vector. If the two bit
vectors are not of the same size, pad the shorter one with zero's from the
left.
- __rshift__(self, n)
- For an in-place right circular shift by n bit positions.
- __setitem__(self, pos, item)
- This is needed for both slice assignments and for index assignments. It
checks the types of pos and item to see if the call is for slice assignment.
For slice assignment, pos must be of type 'slice' and item of type BitVector.
For index assignment, the argument types are checked in the _setbit() method.
- __str__(self)
- To create a print representation
- __xor__(self, other)
- Take a bitwise 'XOR' of the bit vector on which the method is invoked with
the argument bit vector. Return the result as a new bit vector. If the two
bit vectors are not of the same size, pad the shorter one with zeros from the
left.
- circular_rot_left(self)
- This is merely another implementation of the method
circular_rotate_left_by_one() shown above. This one does NOT use map
functions. This method carries out a one-bit left circular shift of a bit
vector.
- circular_rot_right(self)
- This is merely another implementation of the method
circular_rotate_right_by_one() shown above. This one does NOT use map
functions. This method does a one-bit right circular shift of a bit vector.
- circular_rotate_left_by_one(self)
- For a one-bit in-place left circular shift
- circular_rotate_right_by_one(self)
- For a one-bit in-place right circular shift
- close_file_object(self)
- For closing a file object that was used for reading the bits into one or more
BitVector objects.
- count_bits(self)
- Return the number of bits set in a BitVector instance.
- count_bits_sparse(self)
- For sparse bit vectors, this method, contributed by Rhiannon, will be much
faster. She estimates that if a bit vector with over 2 millions bits has
only five bits set, this will return the answer in 1/18 of the time taken by
the count_bits() method. Note however, that count_bits() may work much
faster for dense-packed bit vectors. Rhianon's implementation is based on an
algorithm generally known as the Brian Kernighan's way, although its
antecedents predate its mention by Kernighan and Ritchie.
- deep_copy(self)
- Make a deep copy of a bit vector
- divide_into_two(self)
- Divides an even-sized bit vector into two and returns the two halves as a
list of two bit vectors.
- gcd(self, other)
- Using Euclid's Algorithm, returns the greatest common divisor of the integer
value of the bit vector on which the method is invoked and the integer value
of the argument bit vector.
- gen_rand_bits_for_prime(self, width)
- The bulk of the work here is done by calling random.getrandbits( width) which
returns an integer whose binary code representation will not be larger than
the argument 'width'. However, when random numbers are generated as
candidates for primes, you often want to make sure that the random number
thus created spans the full width specified by 'width' and that the number is
odd. This we do by setting the two most significant bits and the least
significant bit.
- getHexStringFromBitVector = get_hex_string_from_bitvector(self)
- getTextFromBitVector = get_text_from_bitvector(self)
- get_hex_string_from_bitvector(self)
- Return a string of hex digits by scanning the bits from the left and
replacing each sequence of 4 bits by its corresponding hex digit (this is a
useful thing to do only if the length of the vector is an integral multiple
of 4)
- get_text_from_bitvector(self)
- Return the text string formed by dividing the bitvector into bytes from the
left and replacing each byte by its ASCII character (this is a useful thing
to do only if the length of the vector is an integral multiple of 8 and every
byte in your bitvector has a print representation)
- gf_MI(self, mod, n)
- Returns the multiplicative inverse of a vector in the GF(2^n) finite field
with the modulus polynomial set to mod
- gf_divide(self, mod, n)
- Carries out modular division of a bitvector by the modulus bitvector mod in
GF(2^n) finite field. Returns both the quotient and the remainder.
- gf_multiply(self, b)
- In the set of polynomials defined over GF(2), multiplies the bitvector on
which the method is invoked with the bitvector b. Returns the product
bitvector.
- gf_multiply_modular(self, b, mod, n)
- Multiplies a bitvector with the bitvector b in GF(2^n) finite field with the
modulus bit pattern set to mod
- hamming_distance(self, other)
- Computes the Hamming distance between two bit vectors
- intValue = int_val(self)
- int_val(self)
- Return the integer value of a bitvector
- isPowerOf2 = is_power_of_2(self)
- isPowerOf2_sparse = is_power_of_2_sparse(self)
- is_power_of_2(self)
- Determines whether the integer value of a bit vector is a power of
2.
- is_power_of_2_sparse(self)
- Faster version of is_power_of2() for sparse bit vectors
- jaccard_distance(self, other)
- Computes the Jaccard distance between two bit vectors
- jaccard_similarity(self, other)
- Computes the Jaccard similarity coefficient between two bit vectors
- length(self)
- multiplicative_inverse(self, modulus)
- Calculates the multiplicative inverse of a bit vector modulo the bit vector
that is supplied as the argument. Code based on the Extended Euclid's
Algorithm.
- next_set_bit(self, from_index=0)
- This method, contributed originally by Jason Allum and updated subsequently
by John Gleeson, calculates the position of the next set bit at or after the
current position index. It returns -1 if there is no next set bit.
- pad_from_left(self, n)
- Pad a bit vector with n zeros from the left
- pad_from_right(self, n)
- Pad a bit vector with n zeros from the right
- permute(self, permute_list)
- Permute a bit vector according to the indices shown in the second argument
list. Return the permuted bit vector as a new bit vector.
- rank_of_bit_set_at_index(self, position)
- For a bit that is set at the argument 'position', this method returns how
many bits are set to the left of that bit. For example, in the bit pattern
000101100100, a call to this method with position set to 9 will return 4.
- read_bits_from_file(self, blocksize)
- Read blocksize bits from a disk file and return a BitVector object containing
the bits. If the file contains fewer bits than blocksize, construct the
BitVector object from however many bits there are in the file. If the file
contains zero bits, return a BitVector object of size attribute set to 0.
- read_bits_from_fileobject(self, fp)
- This function is meant to read a bit string from a file like
object.
- reset(self, val)
- Resets a previously created BitVector to either all zeros or all ones
depending on the argument val. Returns self to allow for syntax like
bv = bv1[3:6].reset(1)
or
bv = bv1[:].reset(1)
- reverse(self)
- Returns a new bit vector by reversing the bits in the bit vector on which the
method is invoked.
- runs(self)
- Returns a list of the consecutive runs of 1's and 0's in the bit vector.
Each run is either a string of all 1's or a string of all 0's.
- setValue = set_value(self, *args, **kwargs)
- set_value(self, *args, **kwargs)
- Changes the bit pattern associated with a previously constructed BitVector
instance. The allowable modes for changing the internally stored bit pattern
are the same as for the constructor.
- shift_left(self, n)
- For an in-place left non-circular shift by n bit positions
- shift_left_by_one(self)
- For a one-bit in-place left non-circular shift. Note that bitvector size
does not change. The leftmost bit that moves past the first element of the
bitvector is discarded and rightmost bit of the returned vector is set to
zero.
- shift_right(self, n)
- For an in-place right non-circular shift by n bit positions.
- shift_right_by_one(self)
- For a one-bit in-place right non-circular shift. Note that bitvector size
does not change. The rightmost bit that moves past the last element of the
bitvector is discarded and leftmost bit of the returned vector is set to
zero.
- test_for_primality(self)
- Check if the integer value of the bitvector is a prime through the
Miller-Rabin probabilistic test of primality. If not found to be a
composite, estimate the probability of the bitvector being a prime using this
test.
- unpermute(self, permute_list)
- Unpermute the bit vector according to the permutation list supplied as the
second argument. If you first permute a bit vector by using permute() and
then unpermute() it using the same permutation list, you will get back the
original bit vector.
- write_bits_to_fileobject(self, fp)
- This function is meant to write a bit vector directly to a file like object.
Note that whereas 'write_to_file' method creates a memory footprint that
corresponds exactly to the bit vector, the 'write_bits_to_fileobject'
actually writes out the 1's and 0's as individual items to the file object.
That makes this method convenient for creating a string representation of a
bit vector, especially if you use the StringIO class, as shown in the test
code.
- write_to_file(self, file_out)
- Write the bitvector to the file object file_out. (A file object is returned
by a call to open()). Since all file I/O is byte oriented, the bitvector must
be multiple of 8 bits. Each byte treated as MSB first (0th index).
Data descriptors defined here:
- __dict__
- dictionary for instance variables (if defined)
- __weakref__
- list of weak references to the object (if defined)
|