<s> import seq <EOL> class Chunk ( object ) : <EOL> '''<STR_LIT>''' <EOL> def __init__ ( self ) : <EOL> '''<STR_LIT>''' <EOL> self . data = '<STR_LIT>' <EOL> self . seq_start = None <EOL> self . seq_end = None <EOL> def merge ( self , new , new_seq_callback = None ) : <EOL> '''<STR_LIT>''' <EOL> if new . data : <EOL> if self . data : <EOL> return self . inner_merge ( ( new . seq_start , new . seq_end ) , <EOL> new . data , new_seq_callback ) <EOL> else : <EOL> self . data = new . data <EOL> self . seq_start = new . seq_start <EOL> self . seq_end = new . seq_end <EOL> if new_seq_callback : <EOL> new_seq_callback ( new . seq_start ) <EOL> return ( True , ( True , True ) ) <EOL> return ( False , ( False , False ) ) <EOL> def inner_merge ( self , newseq , newdata , callback ) : <EOL> '''<STR_LIT>''' <EOL> overlapped = False <EOL> added_front_data = False <EOL> added_back_data = False <EOL> if ( seq . lt ( newseq [ <NUM_LIT:0> ] , self . seq_start ) and <EOL> seq . lte ( self . seq_start , newseq [ <NUM_LIT:1> ] ) ) : <EOL> new_data_length = seq . subtract ( self . seq_start , newseq [ <NUM_LIT:0> ] ) <EOL> self . data = newdata [ : new_data_length ] + self . data <EOL> self . seq_start = newseq [ <NUM_LIT:0> ] <EOL> overlapped = True <EOL> added_front_data = True <EOL> if callback : <EOL> callback ( newseq [ <NUM_LIT:0> ] ) <EOL> if seq . lte ( newseq [ <NUM_LIT:0> ] , self . seq_end ) and seq . lt ( self . seq_end , newseq [ <NUM_LIT:1> ] ) : <EOL> new_data_length = seq . subtract ( newseq [ <NUM_LIT:1> ] , self . seq_end ) <EOL> self . data += newdata [ - new_data_length : ] <EOL> self . seq_end += new_data_length <EOL> overlapped = True <EOL> added_back_data = True <EOL> if callback : <EOL> back_seq_start = newseq [ <NUM_LIT:1> ] - new_data_length <EOL> callback ( back_seq_start ) <EOL> if ( seq . lte ( self . seq_start , newseq [ <NUM_LIT:0> ] ) and <EOL> seq . lte ( newseq [ <NUM_LIT:1> ] , self . seq_end ) ) : <EOL> overlapped = True <EOL> return ( overlapped , ( added_front_data , added_back_data ) ) </s>
