/brz/remove-bazaar

To get this branch, use:
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
0.18.3 by John Arbash Meinel
We can now add more lines to left lines, and continue to track the right info.
44
    def _update_matching_left_lines(self, new_lines):
45
        matches = self._matching_lines
46
        start_idx = len(self._left_lines)
47
        for idx, line in enumerate(new_lines):
48
            left_matches, right_matches = matches.setdefault(line, ([], []))
49
            left_matches.append(start_idx + idx)
50
0.18.1 by John Arbash Meinel
Start working on an EquivalenceTable construct.
51
    def _update_right_matches(self):
52
        matches = self._matching_lines
53
        to_remove = []
54
        for line, (left_matches, right_matches) in matches.iteritems():
55
            if not left_matches: # queue for deletion
56
                to_remove.append(line)
57
            else:
58
                del right_matches[:]
59
        for line in to_remove:
60
            del matches[line]
61
        del to_remove
62
        for idx, line in enumerate(self._right_lines):
63
            left_matches, right_matches = matches.setdefault(line, ([], []))
64
            right_matches.append(idx)
65
66
    def set_right_lines(self, right_lines):
67
        """Use new right lines, and update the equivalences."""
68
        self._right_lines = right_lines
69
        self._update_right_matches()
0.18.2 by John Arbash Meinel
we can now extract what lines in left match the right
70
71
    def get_left_matches(self, right_idx):
72
        """Return the lines which match the line in right."""
73
        right_line = self._right_lines[right_idx]
74
        left_matches, right_matches = self._matching_lines[right_line]
75
        return left_matches
0.18.3 by John Arbash Meinel
We can now add more lines to left lines, and continue to track the right info.
76
77
    def extend_left_lines(self, lines):
78
        """Add more lines to the left-lines list"""
79
        self._update_matching_left_lines(lines)
80
        self._left_lines.extend(lines)