~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/graph.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2007-10-19 04:28:39 UTC
  • mfrom: (2911.4.3 graph)
  • Revision ID: pqm@pqm.ubuntu.com-20071019042839-xwvsz0loa77yokxm
(robertc) Create a reusable HeadsCache in graph.HeadsCache. (Robert Collins)

Show diffs side-by-side

added added

removed removed

Lines of Context:
371
371
        return False
372
372
 
373
373
 
 
374
class HeadsCache(object):
 
375
    """A cache of results for graph heads calls."""
 
376
 
 
377
    def __init__(self, graph):
 
378
        self.graph = graph
 
379
        self._heads = {}
 
380
 
 
381
    def heads(self, keys):
 
382
        """Return the heads of keys.
 
383
 
 
384
        This matches the API of Graph.heads(), specifically the return value is
 
385
        a set which can be mutated, and ordering of the input is not preserved
 
386
        in the output.
 
387
 
 
388
        :see also: Graph.heads.
 
389
        :param keys: The keys to calculate heads for.
 
390
        :return: A set containing the heads, which may be mutated without
 
391
            affecting future lookups.
 
392
        """
 
393
        keys = frozenset(keys)
 
394
        try:
 
395
            return set(self._heads[keys])
 
396
        except KeyError:
 
397
            heads = self.graph.heads(keys)
 
398
            self._heads[keys] = heads
 
399
            return set(heads)
 
400
 
 
401
 
374
402
class _BreadthFirstSearcher(object):
375
403
    """Parallel search the breadth-first the ancestry of revisions.
376
404