~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/graph.py

  • Committer: Martin Pool
  • Date: 2005-11-04 01:46:31 UTC
  • mto: (1185.33.49 bzr.dev)
  • mto: This revision was merged to the branch mainline in revision 1512.
  • Revision ID: mbp@sourcefrog.net-20051104014631-750e0ad4172c952c
Make biobench directly executable

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005, 2008 Canonical Ltd
2
 
#
 
1
# (C) 2005 Canonical
 
2
 
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
5
5
# the Free Software Foundation; either version 2 of the License, or
6
6
# (at your option) any later version.
7
 
#
 
7
 
8
8
# This program is distributed in the hope that it will be useful,
9
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
11
# GNU General Public License for more details.
12
 
#
 
12
 
13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
16
 
17
 
 
18
17
def max_distance(node, ancestors, distances, root_descendants):
19
18
    """Calculate the max distance to an ancestor.  
20
19
    Return None if not all possible ancestors have known distances"""
103
102
                new_lines.add(descendant)
104
103
        lines = new_lines
105
104
    return result
106
 
 
107
 
 
108
 
class Graph(object):
109
 
    """A graph object which can memoise and cache results for performance."""
110
 
 
111
 
    def __init__(self):
112
 
        super(Graph, self).__init__()
113
 
        self.roots = set([])
114
 
        self.ghosts = set([])
115
 
        self._graph_ancestors = {}
116
 
        self._graph_descendants = {}
117
 
 
118
 
    def add_ghost(self, node_id):
119
 
        """Add a ghost to the graph."""
120
 
        self.ghosts.add(node_id)
121
 
        self._ensure_descendant(node_id)
122
 
 
123
 
    def add_node(self, node_id, parent_ids):
124
 
        """Add node_id to the graph with parent_ids as its parents."""
125
 
        if len(parent_ids) == 0:
126
 
            self.roots.add(node_id)
127
 
        self._graph_ancestors[node_id] = list(parent_ids)
128
 
        self._ensure_descendant(node_id)
129
 
        for parent in parent_ids:
130
 
            self._ensure_descendant(parent)
131
 
            self._graph_descendants[parent][node_id] = 1
132
 
        
133
 
    def _ensure_descendant(self, node_id):
134
 
        """Ensure that a descendant lookup for node_id will work."""
135
 
        if not node_id in self._graph_descendants:
136
 
            self._graph_descendants[node_id] = {}
137
 
 
138
 
    def get_ancestors(self):
139
 
        """Return a dictionary of graph node:ancestor_list entries."""
140
 
        return dict(self._graph_ancestors.items())
141
 
 
142
 
    def get_ancestry(self, node_id, topo_sorted=True):
143
 
        """Return the inclusive ancestors of node_id in topological order."""
144
 
        # maybe optimise this ?
145
 
        from bzrlib.tsort import topo_sort
146
 
        result = {}
147
 
        pending = set([node_id])
148
 
        while len(pending):
149
 
            current = pending.pop()
150
 
            parents = self._graph_ancestors[current]
151
 
            parents = [parent for parent in parents if parent not in self.ghosts]
152
 
            result[current] = parents
153
 
            for parent in parents:
154
 
                if parent not in result and parent not in pending:
155
 
                    pending.add(parent)
156
 
        if not topo_sorted:
157
 
            return result.keys()
158
 
        return topo_sort(result.items())
159
 
 
160
 
    def get_descendants(self):
161
 
        """Return a dictionary of graph node:child_node:distance entries."""
162
 
        return dict(self._graph_descendants.items())