~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/inventory.py

  • Committer: Martin Pool
  • Date: 2005-05-26 01:47:47 UTC
  • Revision ID: mbp@sourcefrog.net-20050526014747-ff75ca198ee02e1e
- Refactor/cleanup Inventory.entries()
- Rewrite Inventory.directories() to return a list rather than recursive 
  generators; simpler and much faster

Show diffs side-by-side

added added

removed removed

Lines of Context:
336
336
 
337
337
        This may be faster than iter_entries.
338
338
        """
339
 
        def accum(dir_ie, dir_path, a):
 
339
        accum = []
 
340
        def descend(dir_ie, dir_path):
340
341
            kids = dir_ie.children.items()
341
342
            kids.sort()
342
343
            for name, ie in kids:
343
344
                child_path = os.path.join(dir_path, name)
344
 
                a.append((child_path, ie))
 
345
                accum.append((child_path, ie))
345
346
                if ie.kind == 'directory':
346
 
                    accum(ie, child_path, a)
 
347
                    descend(ie, child_path)
347
348
 
348
 
        a = []
349
 
        accum(self.root, '', a)
350
 
        return a
 
349
        descend(self.root, '')
 
350
        return accum
351
351
 
352
352
 
353
353
    def directories(self):
354
 
        """Return (path, entry) pairs for all directories.
 
354
        """Return (path, entry) pairs for all directories, including the root.
355
355
        """
356
 
        def descend(parent_ie):
357
 
            parent_name = parent_ie.name
358
 
            yield parent_name, parent_ie
359
 
 
360
 
            # directory children in sorted order
361
 
            dn = []
362
 
            for ie in parent_ie.children.itervalues():
363
 
                if ie.kind == 'directory':
364
 
                    dn.append((ie.name, ie))
365
 
            dn.sort()
 
356
        accum = []
 
357
        def descend(parent_ie, parent_path):
 
358
            accum.append((parent_path, parent_ie))
366
359
            
367
 
            for name, child_ie in dn:
368
 
                for sub_name, sub_ie in descend(child_ie):
369
 
                    yield appendpath(parent_name, sub_name), sub_ie
 
360
            kids = [(ie.name, ie) for ie in parent_ie.children.itervalues() if ie.kind == 'directory']
 
361
            kids.sort()
370
362
 
371
 
        for name, ie in descend(self.root):
372
 
            yield name, ie
 
363
            for name, child_ie in kids:
 
364
                child_path = os.path.join(parent_path, name)
 
365
                descend(child_ie, child_path)
 
366
        descend(self.root, '')
 
367
        return accum
373
368
        
374
369
 
375
370