/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
0.18.9 by John Arbash Meinel
If we are going to do it this way, we don't need to explicitly distinguish left and right
26
    :ivar lines: The 'static' lines that will be preserved between runs.
0.18.8 by John Arbash Meinel
We don't actually care much about the right-line relationships. We only need to track the left-hand side.
27
    :ival _matching_lines: A dict of {line:[matching offsets]}
0.18.1 by John Arbash Meinel
Start working on an EquivalenceTable construct.
28
    """
29
0.18.9 by John Arbash Meinel
If we are going to do it this way, we don't need to explicitly distinguish left and right
30
    def __init__(self, lines):
31
        self.lines = lines
0.18.1 by John Arbash Meinel
Start working on an EquivalenceTable construct.
32
        # For each line in 'left' give the offset to the other lines which
33
        # match it.
0.18.9 by John Arbash Meinel
If we are going to do it this way, we don't need to explicitly distinguish left and right
34
        self._generate_matching_lines()
0.18.1 by John Arbash Meinel
Start working on an EquivalenceTable construct.
35
0.18.9 by John Arbash Meinel
If we are going to do it this way, we don't need to explicitly distinguish left and right
36
    def _generate_matching_lines(self):
0.18.1 by John Arbash Meinel
Start working on an EquivalenceTable construct.
37
        matches = {}
0.18.9 by John Arbash Meinel
If we are going to do it this way, we don't need to explicitly distinguish left and right
38
        for idx, line in enumerate(self.lines):
0.18.8 by John Arbash Meinel
We don't actually care much about the right-line relationships. We only need to track the left-hand side.
39
            matches.setdefault(line, []).append(idx)
0.18.1 by John Arbash Meinel
Start working on an EquivalenceTable construct.
40
        self._matching_lines = matches
41
0.18.9 by John Arbash Meinel
If we are going to do it this way, we don't need to explicitly distinguish left and right
42
    def _update_matching_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.
43
        matches = self._matching_lines
0.18.9 by John Arbash Meinel
If we are going to do it this way, we don't need to explicitly distinguish left and right
44
        start_idx = len(self.lines)
0.18.8 by John Arbash Meinel
We don't actually care much about the right-line relationships. We only need to track the left-hand side.
45
        for idx, do_index in enumerate(index):
0.18.4 by John Arbash Meinel
Allow ignoring some of the new lines.
46
            if not do_index:
47
                continue
0.18.8 by John Arbash Meinel
We don't actually care much about the right-line relationships. We only need to track the left-hand side.
48
            matches.setdefault(new_lines[idx], []).append(start_idx + idx)
49
0.18.9 by John Arbash Meinel
If we are going to do it this way, we don't need to explicitly distinguish left and right
50
    def get_matches(self, line):
0.18.2 by John Arbash Meinel
we can now extract what lines in left match the right
51
        """Return the lines which match the line in right."""
0.18.8 by John Arbash Meinel
We don't actually care much about the right-line relationships. We only need to track the left-hand side.
52
        try:
53
            return self._matching_lines[line]
54
        except KeyError:
55
            return None
0.18.3 by John Arbash Meinel
We can now add more lines to left lines, and continue to track the right info.
56
0.18.9 by John Arbash Meinel
If we are going to do it this way, we don't need to explicitly distinguish left and right
57
    def extend_lines(self, lines, index):
0.18.4 by John Arbash Meinel
Allow ignoring some of the new lines.
58
        """Add more lines to the left-lines list.
59
60
        :param lines: A list of lines to add
61
        :param index: A True/False for each node to define if it should be
62
            indexed.
63
        """
0.18.9 by John Arbash Meinel
If we are going to do it this way, we don't need to explicitly distinguish left and right
64
        self._update_matching_lines(lines, index)
65
        self.lines.extend(lines)