1
# Copyright (C) 2005, 2008 Canonical Ltd
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.
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.
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
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
18
def max_distance(node, ancestors, distances, root_descendants):
18
"""Calculate the max distance to an ancestor.
19
"""Calculate the max distance to an ancestor.
19
20
Return None if not all possible ancestors have known distances"""
21
22
if node in distances:
34
35
best = distances[ancestor] + 1
38
39
def node_distances(graph, ancestors, start, root_descendants=None):
39
40
"""Produce a list of nodes, sorted by distance from a start node.
40
41
This is an algorithm devised by Aaron Bentley, because applying Dijkstra
58
59
line_descendants = graph[line]
59
assert line not in line_descendants, "%s refers to itself" % line
60
60
for descendant in line_descendants:
61
61
distance = max_distance(descendant, ancestors, distances,
102
102
new_lines.add(descendant)
103
103
lines = new_lines
108
"""A graph object which can memoise and cache results for performance."""
111
super(Graph, self).__init__()
113
self.ghosts = set([])
114
self._graph_ancestors = {}
115
self._graph_descendants = {}
117
def add_ghost(self, node_id):
118
"""Add a ghost to the graph."""
119
self.ghosts.add(node_id)
120
self._ensure_descendant(node_id)
122
def add_node(self, node_id, parent_ids):
123
"""Add node_id to the graph with parent_ids as its parents."""
124
if len(parent_ids) == 0:
125
self.roots.add(node_id)
126
self._graph_ancestors[node_id] = list(parent_ids)
127
self._ensure_descendant(node_id)
128
for parent in parent_ids:
129
self._ensure_descendant(parent)
130
self._graph_descendants[parent][node_id] = 1
132
def _ensure_descendant(self, node_id):
133
"""Ensure that a descendant lookup for node_id will work."""
134
if not node_id in self._graph_descendants:
135
self._graph_descendants[node_id] = {}
137
def get_ancestors(self):
138
"""Return a dictionary of graph node:ancestor_list entries."""
139
return dict(self._graph_ancestors.items())
141
def get_ancestry(self, node_id, topo_sorted=True):
142
"""Return the inclusive ancestors of node_id in topological order."""
143
# maybe optimise this ?
144
from bzrlib.tsort import topo_sort
146
pending = set([node_id])
148
current = pending.pop()
149
parents = self._graph_ancestors[current]
150
parents = [parent for parent in parents if parent not in self.ghosts]
151
result[current] = parents
152
for parent in parents:
153
if parent not in result and parent not in pending:
157
return topo_sort(result.items())
159
def get_descendants(self):
160
"""Return a dictionary of graph node:child_node:distance entries."""
161
return dict(self._graph_descendants.items())