git.iko.soy/typeable/nixpkgs
on master help sign in
lib: introduce listDfs and toposort, add example to hasPrefix
commit363b0fd040ed58cbd394cebcfb6d2a77b9672d5f authorJan Malakhovski <oxij@oxij.org> dateWed 25 Nov 2015, 19:07 (11 years ago) parentc49f2a0
1 file changed+80
lib/lists.nix+80
lib/lists.nix+80
@@ -256,6 +256,86 @@ rec {
256256 reverseList = xs:
257257 let l = length xs; in genList (n: elemAt xs (l - n - 1)) l;
258258
259+ /* Depth-First Search (DFS) for lists `list != []`.
260+
261+ `before a b == true` means that `b` depends on `a` (there's an
262+ edge from `b` to `a`).
263+
264+ Examples:
265+
266+ listDfs true hasPrefix [ "/home/user" "other" "/" "/home" ]
267+ == { minimal = "/"; # minimal element
268+ visited = [ "/home/user" ]; # seen elements (in reverse order)
269+ rest = [ "/home" "other" ]; # everything else
270+ }
271+
272+ listDfs true hasPrefix [ "/home/user" "other" "/" "/home" "/" ]
273+ == { cycle = "/"; # cycle encountered at this element
274+ loops = [ "/" ]; # and continues to these elements
275+ visited = [ "/" "/home/user" ]; # elements leading to the cycle (in reverse order)
276+ rest = [ "/home" "other" ]; # everything else
277+
278+ */
279+
280+ listDfs = stopOnCycles: before: list:
281+ let
282+ dfs' = us: visited: rest:
283+ let
284+ c = filter (x: before x us) visited;
285+ b = partition (x: before x us) rest;
286+ in if stopOnCycles && (length c > 0)
287+ then { cycle = us; loops = c; inherit visited rest; }
288+ else if length b.right == 0
289+ then # nothing is before us
290+ { minimal = us; inherit visited rest; }
291+ else # grab the first one before us and continue
292+ dfs' (head b.right)
293+ ([ us ] ++ visited)
294+ (tail b.right ++ b.wrong);
295+ in dfs' (head list) [] (tail list);
296+
297+ /* Sort a list based on a partial ordering using DFS. This
298+ implementation is O(N^2), if your ordering is linear, use `sort`
299+ instead.
300+
301+ `before a b == true` means that `b` should be after `a`
302+ in the result.
303+
304+ Examples:
305+
306+ toposort hasPrefix [ "/home/user" "other" "/" "/home" ]
307+ == { result = [ "/" "/home" "/home/user" "other" ]; }
308+
309+ toposort hasPrefix [ "/home/user" "other" "/" "/home" "/" ]
310+ == { cycle = [ "/home/user" "/" "/" ]; # path leading to a cycle
311+ loops = [ "/" ]; } # loops back to these elements
312+
313+ toposort hasPrefix [ "other" "/home/user" "/home" "/" ]
314+ == { result = [ "other" "/" "/home" "/home/user" ]; }
315+
316+ toposort (a: b: a < b) [ 3 2 1 ] == { result = [ 1 2 3 ]; }
317+
318+ */
319+
320+ toposort = before: list:
321+ let
322+ dfsthis = listDfs true before list;
323+ toporest = toposort before (dfsthis.visited ++ dfsthis.rest);
324+ in
325+ if length list < 2
326+ then # finish
327+ { result = list; }
328+ else if dfsthis ? "cycle"
329+ then # there's a cycle, starting from the current vertex, return it
330+ { cycle = reverseList ([ dfsthis.cycle ] ++ dfsthis.visited);
331+ inherit (dfsthis) loops; }
332+ else if toporest ? "cycle"
333+ then # there's a cycle somewhere else in the graph, return it
334+ toporest
335+ # Slow, but short. Can be made a bit faster with an explicit stack.
336+ else # there are no cycles
337+ { result = [ dfsthis.minimal ] ++ toporest.result; };
338+
259339 /* Sort a list based on a comparator function which compares two
260340 elements and returns true if the first argument is strictly below
261341 the second argument. The returned list is sorted in an increasing