From 50add47748eb40371d8b88208a13e7230d15c220 Mon Sep 17 00:00:00 2001 From: Ludovic Courtès Date: Thu, 23 Jan 2014 22:13:27 +0100 Subject: store: Add 'topologically-sorted'. * guix/store.scm (topologically-sorted): New procedure. * tests/store.scm ("topologically-sorted, one item", "topologically-sorted, several items", "topologically-sorted, more difficult"): New tests. --- guix/store.scm | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) (limited to 'guix/store.scm') diff --git a/guix/store.scm b/guix/store.scm index ede64341c5..eca0de7d97 100644 --- a/guix/store.scm +++ b/guix/store.scm @@ -76,6 +76,7 @@ (define-module (guix store) references requisites referrers + topologically-sorted valid-derivers query-derivation-outputs live-paths @@ -589,6 +590,40 @@ (define (requisites store path) references, recursively)." (fold-path store cons '() path)) +(define (topologically-sorted store paths) + "Return a list containing PATHS and all their references sorted in +topological order." + (define (traverse) + ;; Do a simple depth-first traversal of all of PATHS. + (let loop ((paths paths) + (visited vlist-null) + (result '())) + (define (visit n) + (vhash-cons n #t visited)) + + (define (visited? n) + (vhash-assoc n visited)) + + (match paths + ((head tail ...) + (if (visited? head) + (loop tail visited result) + (call-with-values + (lambda () + (loop (references store head) + (visit head) + result)) + (lambda (visited result) + (loop tail + visited + (cons head result)))))) + (() + (values visited result))))) + + (call-with-values traverse + (lambda (_ result) + (reverse result)))) + (define referrers (operation (query-referrers (store-path path)) "Return the list of path that refer to PATH." -- cgit v1.2.3