<s> """<STR_LIT>""" <EOL> __author__ = '<STR_LIT>' <EOL> __all__ = [ <EOL> '<STR_LIT>' , <EOL> ] <EOL> import random <EOL> from . bitstrings import BitStringBase <EOL> class BitString ( BitStringBase ) : <EOL> """<STR_LIT>""" <EOL> @ classmethod <EOL> def random ( cls , length , bit_prob = <NUM_LIT> ) : <EOL> """<STR_LIT>""" <EOL> assert isinstance ( length , int ) and length >= <NUM_LIT:0> <EOL> assert isinstance ( bit_prob , ( int , float ) ) and <NUM_LIT:0> <= bit_prob <= <NUM_LIT:1> <EOL> bits = <NUM_LIT:0> <EOL> for _ in range ( length ) : <EOL> bits <<= <NUM_LIT:1> <EOL> bits += ( random . random ( ) < bit_prob ) <EOL> return cls ( bits , length ) <EOL> @ classmethod <EOL> def crossover_template ( cls , length , points = <NUM_LIT:2> ) : <EOL> """<STR_LIT>""" <EOL> assert isinstance ( length , int ) and length >= <NUM_LIT:0> <EOL> assert isinstance ( points , int ) and points >= <NUM_LIT:0> <EOL> points = random . sample ( range ( length + <NUM_LIT:1> ) , points ) <EOL> points . sort ( ) <EOL> points . append ( length ) <EOL> previous = <NUM_LIT:0> <EOL> include_range = bool ( random . randrange ( <NUM_LIT:2> ) ) <EOL> bits = <NUM_LIT:0> <EOL> for point in points : <EOL> if point > previous : <EOL> bits <<= point - previous <EOL> if include_range : <EOL> bits += ( <NUM_LIT:1> << ( point - previous ) ) - <NUM_LIT:1> <EOL> include_range = not include_range <EOL> previous = point <EOL> return cls ( bits , length ) <EOL> def __init__ ( self , bits , length = None ) : <EOL> if isinstance ( bits , int ) : <EOL> if length is None : <EOL> length = bits . bit_length ( ) <EOL> else : <EOL> assert length >= bits . bit_length ( ) <EOL> if bits < <NUM_LIT:0> : <EOL> bits &= ( <NUM_LIT:1> << length ) - <NUM_LIT:1> <EOL> hash_value = None <EOL> elif isinstance ( bits , BitString ) : <EOL> bits , length , hash_value = bits . _bits , bits . _length , bits . _hash <EOL> elif isinstance ( bits , str ) : <EOL> bit_str = bits <EOL> bits = <NUM_LIT:0> <EOL> for char in bit_str : <EOL> bits <<= <NUM_LIT:1> <EOL> if char == '<STR_LIT:1>' : <EOL> bits += <NUM_LIT:1> <EOL> else : <EOL> assert char == '<STR_LIT:0>' <EOL> if length is None : <EOL> length = len ( bit_str ) <EOL> else : <EOL> assert length >= len ( bit_str ) <EOL> hash_value = None <EOL> else : <EOL> bit_sequence = bits <EOL> bits = <NUM_LIT:0> <EOL> count = <NUM_LIT:0> <EOL> for bit in bit_sequence : <EOL> count += <NUM_LIT:1> <EOL> bits <<= <NUM_LIT:1> <EOL> if bit : <EOL> bits += <NUM_LIT:1> <EOL> if length is None : <EOL> length = count <EOL> else : <EOL> assert length >= count <EOL> hash_value = None <EOL> super ( ) . __init__ ( bits , hash_value ) <EOL> self . _length = length <EOL> def any ( self ) : <EOL> """<STR_LIT>""" <EOL> return bool ( self . _bits ) <EOL> def count ( self ) : <EOL> """<STR_LIT>""" <EOL> result = <NUM_LIT:0> <EOL> bits = self . _bits <EOL> while bits : <EOL> result += bits % <NUM_LIT:2> <EOL> bits >>= <NUM_LIT:1> <EOL> return result <EOL> def __int__ ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _bits <EOL> def __len__ ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _length <EOL> def __iter__ ( self ) : <EOL> """<STR_LIT>""" <EOL> for index in range ( self . _length - <NUM_LIT:1> , - <NUM_LIT:1> , - <NUM_LIT:1> ) : <EOL> yield ( self . _bits >> index ) % <NUM_LIT:2> <EOL> def __getitem__ ( self , index ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( index , slice ) : <EOL> start , stop , step = index . indices ( self . _length ) <EOL> if start < <NUM_LIT:0> : <EOL> return BitString ( <NUM_LIT:0> , <NUM_LIT:0> ) <EOL> if step == - <NUM_LIT:1> : <EOL> step = <NUM_LIT:1> <EOL> start , stop = stop , start <EOL> if step == <NUM_LIT:1> : <EOL> length = stop - start <EOL> bits = ( ( self . _bits >> ( self . _length - stop ) ) % <EOL> ( <NUM_LIT:1> << length ) ) <EOL> return BitString ( bits , length ) <EOL> else : <EOL> return BitString ( [ <EOL> self [ point ] <EOL> for point in range ( start , stop , step ) <EOL> ] ) <EOL> assert isinstance ( index , int ) <EOL> if index >= <NUM_LIT:0> : <EOL> assert index < self . _length <EOL> return ( self . _bits >> ( self . _length - index - <NUM_LIT:1> ) ) % <NUM_LIT:2> <EOL> else : <EOL> assert - index <= self . _length <EOL> return ( self . _bits >> ( - index - <NUM_LIT:1> ) ) % <NUM_LIT:2> <EOL> def __hash__ ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . _hash is None : <EOL> self . _hash = hash ( self . _bits ) ^ hash ( self . _length ) <EOL> return self . _hash <EOL> def __eq__ ( self , other ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( other , BitString ) : <EOL> return NotImplemented <EOL> return self . _bits == other . _bits and self . _length == other . _length <EOL> def __and__ ( self , other ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( other , BitString ) : <EOL> assert self . _length == other . _length <EOL> else : <EOL> other = BitString ( other , self . _length ) <EOL> return BitString ( self . _bits & other . _bits , self . _length ) <EOL> def __or__ ( self , other ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( other , BitString ) : <EOL> assert self . _length == other . _length <EOL> else : <EOL> other = BitString ( other , self . _length ) <EOL> return BitString ( self . _bits | other . _bits , self . _length ) <EOL> def __xor__ ( self , other ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( other , BitString ) : <EOL> assert self . _length == other . _length <EOL> else : <EOL> other = BitString ( other , self . _length ) <EOL> return BitString ( self . _bits ^ other . _bits , self . _length ) <EOL> def __invert__ ( self ) : <EOL> """<STR_LIT>""" <EOL> return BitString ( ~ self . _bits % ( <NUM_LIT:1> << self . _length ) , self . _length ) <EOL> def __add__ ( self , other ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( other , BitString ) : <EOL> other = BitString ( other ) <EOL> return BitString ( <EOL> ( self . _bits << other . _length ) + other . _bits , <EOL> self . _length + other . _length <EOL> ) </s>
