bzr branch
http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
|
0.18.1
by John Arbash Meinel
Start working on an EquivalenceTable construct. |
1 |
# Copyright (C) 2008 Canonical Limited.
|
2 |
#
|
|
3 |
# This program is free software; you can redistribute it and/or modify
|
|
4 |
# it under the terms of the GNU General Public License version 2 as published
|
|
5 |
# by the Free Software Foundation.
|
|
6 |
#
|
|
7 |
# This program is distributed in the hope that it will be useful,
|
|
8 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
9 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
10 |
# GNU General Public License for more details.
|
|
11 |
#
|
|
12 |
# You should have received a copy of the GNU General Public License
|
|
13 |
# along with this program; if not, write to the Free Software
|
|
14 |
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
15 |
#
|
|
16 |
||
17 |
"""Functions for dealing with a persistent equivalency table."""
|
|
18 |
||
19 |
||
20 |
SENTINEL = -1 |
|
21 |
||
22 |
||
23 |
class EquivalenceTable(object): |
|
24 |
"""This class tracks equivalencies between lists of hashable objects. |
|
25 |
||
26 |
:ivar _left_lines: The 'static' lines that will be preserved between runs.
|
|
27 |
:ivar _right_lines: The current set of lines that we are matching against
|
|
28 |
"""
|
|
29 |
||
30 |
def __init__(self, left_lines): |
|
31 |
self._left_lines = left_lines |
|
32 |
self._right_lines = None |
|
33 |
# For each line in 'left' give the offset to the other lines which
|
|
34 |
# match it.
|
|
35 |
self._generate_matching_left_lines() |
|
36 |
||
37 |
def _generate_matching_left_lines(self): |
|
38 |
matches = {} |
|
39 |
for idx, line in enumerate(self._left_lines): |
|
40 |
left_matches, right_matches = matches.setdefault(line, ([], [])) |
|
41 |
left_matches.append(idx) |
|
42 |
self._matching_lines = matches |
|
43 |
||
44 |
def _update_right_matches(self): |
|
45 |
matches = self._matching_lines |
|
46 |
to_remove = [] |
|
47 |
for line, (left_matches, right_matches) in matches.iteritems(): |
|
48 |
if not left_matches: # queue for deletion |
|
49 |
to_remove.append(line) |
|
50 |
else: |
|
51 |
del right_matches[:] |
|
52 |
for line in to_remove: |
|
53 |
del matches[line] |
|
54 |
del to_remove |
|
55 |
for idx, line in enumerate(self._right_lines): |
|
56 |
left_matches, right_matches = matches.setdefault(line, ([], [])) |
|
57 |
right_matches.append(idx) |
|
58 |
||
59 |
def set_right_lines(self, right_lines): |
|
60 |
"""Use new right lines, and update the equivalences.""" |
|
61 |
self._right_lines = right_lines |
|
62 |
self._update_right_matches() |
|
|
0.18.2
by John Arbash Meinel
we can now extract what lines in left match the right |
63 |
|
64 |
def get_left_matches(self, right_idx): |
|
65 |
"""Return the lines which match the line in right.""" |
|
66 |
right_line = self._right_lines[right_idx] |
|
67 |
left_matches, right_matches = self._matching_lines[right_line] |
|
68 |
return left_matches |