Topological sort/Extracted top item: Difference between revisions

m
syntax highlighting fixup automation
(Added 11l)
m (syntax highlighting fixup automation)
Line 46:
{{trans|Python}}
 
<langsyntaxhighlight lang="11l">F _topx(data, tops, &_sofar) -> [[String]]
‘Recursive topological extractor’
_sofar [+]= copy(tops)
Line 106:
I tops.len > 1
print("\nThe compile order for top levels: #. is...".format(sorted(Array(tops)).map(s -> String(s)).join(‘ and ’)))
printorder(topx(&data, tops))</langsyntaxhighlight>
 
{{out}}
Line 133:
=={{header|C}}==
Take code from [[Topological sort#c]] and add/change the following:
<langsyntaxhighlight lang="c">char input[] = "top1 des1 ip1 ip2\n"
"top2 des1 ip2 ip3\n"
"ip1 extra1 ip1a ipcommon\n"
Line 207:
 
return 0;
}</langsyntaxhighlight>output (the last item is just to show that it doesn't have to be top level)<syntaxhighlight lang="text">Compile order for: top1
level 1: extra1 ip1a ipcommon ip2a ip2b ip2c des1b des1a1 des1a2 des1c1
level 2: ip1 ip2 des1a des1c
Line 227:
Compile order for: ip1
level 1: extra1 ip1a ipcommon
level 2: ip1</langsyntaxhighlight>
 
=={{header|Go}}==
<langsyntaxhighlight lang="go">package main
 
import (
Line 389:
}
return L[i:], nil
}</langsyntaxhighlight>
{{out}}
<pre>
Line 405:
Derived from the [[Topological sort#J|topological sort]] implementation:
 
<langsyntaxhighlight lang="j">compileOrder=: dyad define
targets=. ;: x
parsed=. <@;:;._2 y
Line 417:
 
topLevel=: [: ({.&> -. [:;}.&.>) <@;:;._2
</syntaxhighlight>
</lang>
 
The changes include:
Line 431:
Example:
 
<langsyntaxhighlight lang="j">dependencies=: noun define
top1 des1 ip1 ip2
top2 des1 ip2 ip3
Line 462:
des1
top1 top2
</syntaxhighlight>
</lang>
 
=={{header|Java}}==
{{works with|Java|8}}
<langsyntaxhighlight lang="java">import java.util.*;
import static java.util.Arrays.asList;
import static java.util.stream.Collectors.toList;
Line 537:
return result.stream().distinct().collect(toList());
}
}</langsyntaxhighlight>
<pre>Top levels: [top1, top2]
Compile order for top1 [extra1, des1c1, des1a2, des1a1, des1c, des1b, des1a, ip2c, ip2b, ip2a, ipcommon, ip1a, des1, ip2, ip1, top1]
Line 544:
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">const topotext = """
top1 des1 ip1 ip2
top2 des1 ip2 ip3
Line 578:
println("Compile order for $f: ", compileorder("top1", topodict))
end
</langsyntaxhighlight>{{out}}
<pre>
Top level files: ["top2", "top1"]
Line 589:
=={{header|Kotlin}}==
{{trans|Java}}
<langsyntaxhighlight lang="scala">// version 1.1.51
 
import java.util.LinkedList
Line 649:
println("Top levels: ${g.topLevels()}")
for (f in files) println("\nCompile order for $f: ${g.compileOrder(f)}")
}</langsyntaxhighlight>
 
{{out}}
Line 664:
=={{header|Nim}}==
{{trans|Python}}
<langsyntaxhighlight Nimlang="nim">import algorithm, sequtils, strutils, sets, tables
 
type
Line 741:
if tops.len > 1:
echo "\nThe compile order for top levels $# is..." % topList.mapIt("“" & it & "”").join(" and ")
printOrder Data.topx(tops)</langsyntaxhighlight>
 
{{out}}
Line 765:
 
=={{header|Perl}}==
<langsyntaxhighlight lang="perl">#!/usr/bin/perl
 
use strict;
Line 789:
print "TOP LEVELS: @{[grep $deps !~ /\h$_\b/, $deps =~ /^\w+/gm]}\n";
print "\nTARGET $_ ORDER: @{[ uniq before split ]}\n"
for $deps =~ /^\w+/gm, 'top1 top2';</langsyntaxhighlight>
{{out}}
<pre>
Line 813:
=={{header|Phix}}==
Minor tweaks to the Topological_sort code: top_levels, propagate() and -1 now means "not required".
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #004080;">sequence</span> <span style="color: #000000;">names</span>
<span style="color: #008080;">enum</span> <span style="color: #000000;">RANK</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">NAME</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">DEP</span> <span style="color: #000080;font-style:italic;">-- content of names
Line 932:
<span style="color: #000000;">topsort</span><span style="color: #0000FF;">(</span><span style="color: #000000;">input</span><span style="color: #0000FF;">,{</span><span style="color: #008000;">"top1"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"top2"</span><span style="color: #0000FF;">})</span>
<span style="color: #000000;">topsort</span><span style="color: #0000FF;">(</span><span style="color: #000000;">input</span><span style="color: #0000FF;">,{</span><span style="color: #008000;">"ip1"</span><span style="color: #0000FF;">})</span>
<!--</langsyntaxhighlight>-->
{{out}}
Items on the same line can be compiled at the same time, and each line is alphabetic.
Line 959:
=={{header|Python}}==
Where the compile order between a subset of files is arbitrary, they are shown on the same line.
<langsyntaxhighlight lang="python">try:
from functools import reduce
except: pass
Line 1,022:
print("\nThe compile order for top levels: %s is..."
% ' and '.join(str(s) for s in sorted(tops)) )
printorder(topx(data, tops))</langsyntaxhighlight>
 
'''Sample Output'''
Line 1,047:
=={{header|Racket}}==
 
<langsyntaxhighlight lang="racket">#lang racket
(define dep-tree ; go straight for the hash, without parsing strings etc.
#hash((top1 . (des1 ip1 ip2))
Line 1,096:
(print-build-order dep-tree 'top2)
(with-handlers [(exn? (λ (x) (displayln (exn-message x) (current-error-port))))]
(build-tree #hash((top . (des1 ip1)) (ip1 . (net netip)) (netip . (mac ip1))) 'top))</langsyntaxhighlight>
 
{{out}}
Line 1,115:
=={{header|Raku}}==
(formerly Perl 6)
<syntaxhighlight lang="raku" perl6line>sub top_topos ( %deps, *@top ) {
my %ba;
for %deps.kv -> $after, @befores {
Line 1,180:
top_topos(%deps, 'top2');
top_topos(%deps, 'ip1');
top_topos(%deps, 'top1', 'top2');</langsyntaxhighlight>
{{out}}
<pre>Top levels are: top1 top2
Line 1,210:
Where the compile order between a subset of files is arbitrary, they are shown on the same line.
<br>This REXX version can handle multiple top levels.
<langsyntaxhighlight REXXlang="rexx">/*REXX program displays the compile order of jobs (indicating the dependencies). */
parse arg job /*obtain optional argument from the CL.*/
jobL.=; stage.=; #.=0; @.=; JL= /*define some handy─dandy variables. */
Line 1,276:
/* [↓] display the stages for the job.*/
do show=1 for s; if stage.show\=='' then say show stage.show
end /*show*/ /*stick a fork in it, we're all done. */</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default input of: &nbsp; <tt> top1 </tt>}}
<pre>
Line 1,309:
The <tt>topsort</tt> proc is taken from [[Topological sort#Tcl]] with <tt>{*}</tt> removed from the line commented so that results are returned by level:
 
<langsyntaxhighlight Tcllang="tcl">package require Tcl 8.5
proc topsort {data} {
# Clean the data
Line 1,401:
puts "\tround [incr i]:\t$deps"
}
}</langsyntaxhighlight>
 
{{out}}
Line 1,427:
{{libheader|Wren-llist}}
{{libheader|Wren-seq}}
<langsyntaxhighlight lang="ecmascript">import "/llist" for DLinkedList
import "/seq" for Lst
 
Line 1,488:
var g = Graph.new(s, deps)
System.print("Top levels: %(g.topLevels)")
for (f in files) System.print("\nCompile order for %(f): %(g.compileOrder(f))")</langsyntaxhighlight>
 
{{out}}
10,333

edits