/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.4 by John Arbash Meinel
Allow ignoring some of the new lines.
44
    def _update_matching_left_lines(self, new_lines, index):
0.18.3 by John Arbash Meinel
We can now add more lines to left lines, and continue to track the right info.
45
        matches = self._matching_lines
46
        start_idx = len(self._left_lines)
0.18.4 by John Arbash Meinel
Allow ignoring some of the new lines.
47
        for idx, (line, do_index) in enumerate(zip(new_lines, index)):
48
            if not do_index:
49
                continue
0.18.3 by John Arbash Meinel
We can now add more lines to left lines, and continue to track the right info.
50
            left_matches, right_matches = matches.setdefault(line, ([], []))
51
            left_matches.append(start_idx + idx)
52
0.18.1 by John Arbash Meinel
Start working on an EquivalenceTable construct.
53
    def _update_right_matches(self):
54
        matches = self._matching_lines
55
        to_remove = []
56
        for line, (left_matches, right_matches) in matches.iteritems():
57
            if not left_matches: # queue for deletion
58
                to_remove.append(line)
59
            else:
60
                del right_matches[:]
61
        for line in to_remove:
62
            del matches[line]
63
        del to_remove
64
        for idx, line in enumerate(self._right_lines):
65
            left_matches, right_matches = matches.setdefault(line, ([], []))
66
            right_matches.append(idx)
67
68
    def set_right_lines(self, right_lines):
69
        """Use new right lines, and update the equivalences."""
70
        self._right_lines = right_lines
71
        self._update_right_matches()
0.18.2 by John Arbash Meinel
we can now extract what lines in left match the right
72
73
    def get_left_matches(self, right_idx):
74
        """Return the lines which match the line in right."""
75
        right_line = self._right_lines[right_idx]
76
        left_matches, right_matches = self._matching_lines[right_line]
77
        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.
78
0.18.4 by John Arbash Meinel
Allow ignoring some of the new lines.
79
    def extend_left_lines(self, lines, index):
80
        """Add more lines to the left-lines list.
81
82
        :param lines: A list of lines to add
83
        :param index: A True/False for each node to define if it should be
84
            indexed.
85
        """
86
        self._update_matching_left_lines(lines, index)
0.18.3 by John Arbash Meinel
We can now add more lines to left lines, and continue to track the right info.
87
        self._left_lines.extend(lines)