~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to groupcompress.py

Simplify output_lines.

Show diffs side-by-side

added added

removed removed

Lines of Context:
137
137
        line_locations = self.line_locations
138
138
        range_len = 0
139
139
        range_start = 0
140
 
        flush_range = self.flush_range
141
140
        copy_ends = None
142
141
        # We either copy a range (while there are reusable lines) or we 
143
142
        # insert new lines. To find reusable lines we traverse 
152
151
                else:
153
152
                    range_len += 1
154
153
            else:
155
 
                locations, next = line_locations[line]
 
154
                locations = line_locations[line]
156
155
                if copy_ends:
157
156
                    next_locations = locations.intersection(copy_ends)
158
157
                    if len(next_locations):
164
163
                if copy_ends:
165
164
                    result.append((min(copy_ends) - range_len, range_start, range_len))
166
165
                range_len = 1
167
 
                copy_ends = next
 
166
                copy_ends = set(loc + 1 for loc in locations)
168
167
                range_start = pos
169
168
            pos += 1
170
169
        if copy_ends:
197
196
        new_lines.append('sha1: %s\n' % sha1)
198
197
        index_lines = [False, False]
199
198
        pos = 0
200
 
        line_locations = self.line_locations
201
199
        range_len = 0
202
200
        range_start = 0
203
201
        flush_range = self.flush_range
209
207
        for old_start, new_start, range_len in blocks:
210
208
            if new_start != current_pos:
211
209
                # non-matching region
212
 
                flush_range(False, current_pos, None, new_start - current_pos,
 
210
                flush_range(current_pos, None, new_start - current_pos,
213
211
                    lines, new_lines, index_lines)
214
212
            current_pos = new_start + range_len
215
213
            if not range_len:
216
214
                continue
217
 
            flush_range(True, new_start, [old_start + range_len], range_len,
218
 
                lines, new_lines, index_lines)
 
215
            flush_range(new_start, old_start, range_len, lines,
 
216
                new_lines, index_lines)
219
217
        delta_start = (self.endpoint, len(self.lines))
220
218
        self.output_lines(new_lines, index_lines)
221
219
        trim_encoding_newline(lines)
240
238
        sha1 = sha_strings(lines)
241
239
        return lines, sha1
242
240
 
243
 
    def flush_range(self, copying, range_start, copy_ends, range_len, lines, new_lines, index_lines):
 
241
    def flush_range(self, range_start, copy_start, range_len, lines, new_lines, index_lines):
244
242
        if not range_len:
245
243
            return
246
244
        insert_instruction = "i,%d\n" % range_len
247
 
        if copying:
 
245
        if copy_start is not None:
248
246
            # range stops, flush and start a new copy range
249
 
            copy_start = min(copy_ends) - range_len
250
247
            stop_byte = self.line_offsets[copy_start + range_len - 1]
251
248
            if copy_start == 0:
252
249
                start_byte = 0
263
260
        new_lines.append(insert_instruction)
264
261
        new_lines.extend(lines[range_start:range_start+range_len])
265
262
        index_lines.append(False)
266
 
        index_lines.extend([not copying]*range_len)
 
263
        index_lines.extend([copy_start is None]*range_len)
267
264
 
268
265
    def output_lines(self, new_lines, index_lines):
269
266
        """Output some lines.
274
271
        """
275
272
        endpoint = self.endpoint
276
273
        offset = len(self.lines)
 
274
        line_append = self.line_offsets.append
 
275
        setdefault = self.line_locations.setdefault
277
276
        for (pos, line), index in izip(enumerate(new_lines), index_lines):
278
 
            self.lines.append(line)
279
277
            endpoint += len(line)
280
 
            self.line_offsets.append(endpoint)
 
278
            line_append(endpoint)
281
279
            if index:
282
 
                indices, next_lines = self.line_locations.setdefault(line,
283
 
                    (set(), set()))
 
280
                indices = setdefault(line, set())
284
281
                indices.add(pos + offset)
285
 
                next_lines.add(pos + offset + 1)
 
282
        self.lines.extend(new_lines)
286
283
        self.endpoint = endpoint
287
284
 
288
285
    def ratio(self):