Topological sort: Difference between revisions
m
→{{header|Wren}}: Changed to Wren S/H
m (→{{header|Wren}}: Changed to Wren S/H) |
|||
(10 intermediate revisions by 4 users not shown) | |||
Line 497:
=={{header|ATS}}==
For ATS2 (patsopt/patscc) and a garbage collector (Boehm GC). The algorithm used is depth-first search.
You can compile this program with something like
"patscc -o topo -DATS_MEMALLOC_GCBDW topo.dats -lgc"
(Or you can use the libc malloc and just let the memory leak: "patscc -o topo -DATS_MEMALLOC_LIBC topo.dats")
<syntaxhighlight lang="ATS">
Line 510 ⟶ 512:
staload UN = "prelude/SATS/unsafe.sats"
(* Macros for list construction. *)
#define NIL list_nil ()
#define :: list_cons
(*------------------------------------------------------------------*)
(* A shorthand for list reversal. This could also have been written as
a macro. *)
fn {a : t@ype}
rev {n : int}
(lst : list (INV(a), n))
:<!wrt> list (a, n) =
(* The list_reverse template function returns a linear list. Convert
that result to a "regular" list. *)
list_vt2t (list_reverse<a> lst)
(*------------------------------------------------------------------*)
(* Some shorthands for string operations. These are written as
macros. *)
macdef substr (s, i, n) =
(* string_make_substring returns a linear strnptr, but we want a
"regular" string. *)
strnptr2string (string_make_substring (,(s), ,(i), ,(n)))
macdef copystr (s) =
(* string0 copy returns a linear strptr, but we want a "regular"
string. *)
strptr2string (string0_copy (,(s)))
(*------------------------------------------------------------------*)
Line 521 ⟶ 550:
typedef _marksvec_t (n : int) = arrayref (_mark_t, n)
(* Some type casts that seem not to be implemented in the
prelude. *)
implement g1int2uint<intknd, uint8knd> i = $UN.cast i
implement g1uint2int<uint8knd, intknd> i = $UN.cast i
Line 526 ⟶ 557:
in
abstype
assume
fn
{n : nat}
{b : int | b == 0 || b == 1}
Line 538 ⟶ 569:
fn
marks_set_at
{n : int}
{i : nat | i < n}
Line 549 ⟶ 580:
fn
marks_get_at
{n : int}
{i : nat | i < n}
Line 559 ⟶ 590:
fn
marks_setall
{n : int}
{b : int | b == 0 || b == 1}
Line 577 ⟶ 608:
end
overload [] with
overload [] with
overload setall with
end
Line 592 ⟶ 623:
entries of each sublist forms the list of dependencies of the first
entry. Thus this is a kind of association list. *)
typedef
typedef
typedef
{n : int}
{i : nat | i <= n}
Line 607 ⟶ 638:
make_char_skipper
(match : char -<> bool)
:<>
let
fun
Line 654 ⟶ 685:
in
if i = n then
@(
else if is_end_of_list text[i] then
@(
else
let
val j = skip_ident (text, n, i)
val () = $effmask_exn assertloc (i < j)
val nodename = substr (text, i, j - i)
in
loop (nodename :: row, j)
Line 679 ⟶ 708:
i : size_t i)
:<!wrt> [j : int | i <= j; j <= n]
@(
let
fun
loop {i : nat | i <= n}
.<n - i>.
(desc :
i : size_t i)
:<!wrt> [j : int | i <= j; j <= n]
@(
let
val i = skip_spaces (text, n, i)
in
if i = n then
@(
else if is_end_of_list text[i] then
@(
else
let
Line 727 ⟶ 756:
c := $extfcall (int, "getchar")
end;
end
fn
read_depdesc ()
:
let
val text = read_to_string ()
Line 747 ⟶ 776:
(* An ordered list of the node names. *)
typedef
(* A more efficient representation for nodes: integers in 0..n-1. *)
typedef
(* A collection of directed edges.
represented by the array index, to each of the nodenums listed in
the corresponding array entry. *)
typedef edges (n : int) = arrayref (List0 (nodenum n), n)
(* An internal representation of data for a topological sort. *)
typedef
'{
n = size_t n, (* The number of nodes. *)
edges =
tempmarks =
permmarks =
}
fn
collect_nodenames
(desc :
:<!wrt> [n : nat]
@(
size_t n) =
let
Line 777 ⟶ 808:
.<m>.
(row : list (String1, m),
names : &
n : &size_t n0 >> size_t n1)
:<!wrt> #[n1 : int | n0 <= n1]
Line 802 ⟶ 833:
.<m>.
(desc : list (List1 String1, m),
names : &
n : &size_t n0 >> size_t n1)
:<!wrt> #[n1 : int | n0 <= n1]
Line 818 ⟶ 849:
in
collect (desc, names, n);
@(
end
Line 824 ⟶ 855:
nodename_number
{n : int}
(nodenames :
name : String1)
:<> Option (
let
fun
loop {i : nat | i <= n}
.<n - i>.
(names :
i : size_t i)
:<> Option (
case+ names of
| NIL => None ()
Line 849 ⟶ 880:
fn
add_edge {n : int}
(edges :
from :
to :
:<!refwrt> void =
(* This implementation does not store duplicate edges. *)
let
val old_edges = edges[from]
implement list_find$pred<
in
case+ list_find_opt<
| ~ None_vt () => edges[from] := to :: old_edges
| ~ Some_vt _ => ()
Line 867 ⟶ 898:
{n : int}
{m : int}
(edges :
n : size_t n,
desc :
nodenames :
:<!refwrt> void =
let
Line 893 ⟶ 924:
{m1 : nat}
.<m1>.
(headnum :
lst : list (String1, m1))
:<!refwrt> void =
Line 929 ⟶ 960:
fn
topodata_make
(desc :
:<!refwrt> [n : nat]
@(
let
val @(nodenames, n) = collect_nodenames desc
Line 939 ⟶ 970:
prval [n : int] EQINT () = eqint_make_guint n
val edges = arrayref_make_elt<List0 (
val () = fill_edges {n} (edges, n, desc, nodenames)
val tempmarks =
and permmarks =
val topo =
Line 963 ⟶ 994:
*)
(* What return values are made from. *)
datatype toporesult (a : t@ype, n : int) =
| {0 <= n}
Topo_SUCCESS (a, n) of list (a, n)
| Topo_CYCLE (a, n) of List1 a
typedef toporesult (a : t@ype) = [n : int] toporesult (a, n)
fn
find_unmarked_node
{n : int}
(topo :
:<!ref> Option (
let
val n = topo.n
Line 979 ⟶ 1,017:
.<n - i>.
(i : size_t i)
:<!ref> Option (
if i = n then
None ()
Line 992 ⟶ 1,030:
fun
visit {n : int}
(topo :
nodenum :
accum : List0 (
: toporesult (nodenum n) =
(* Probably it is cumbersome to include a proof this routine
terminates. Thus I will not try to
let
val
and edges = topo.edges
and tempmarks = topo.tempmarks
and permmarks = topo.permmarks
in
if permmarks[nodenum] = 1 then
else if tempmarks[nodenum] = 1 then
val () = assertloc (isneqz path)
in
Topo_CYCLE path
end
else
let
Line 1,013 ⟶ 1,057:
{k : nat}
.<k>.
(topo :
to_visit : list (
accum : List0 (
: toporesult (nodenum n) =
case+ to_visit of
| NIL =>
| node_to_visit :: tail =>
begin
case+ visit (topo, node_to_visit, accum, path) of
|
| other => other
end
in
tempmarks[nodenum] := 1;
case+ recursive_visits (topo, edges[nodenum], accum
|
begin
tempmarks[nodenum] := 0;
permmarks[nodenum] := 1;
end
| other => other
end
end
Line 1,041 ⟶ 1,088:
topological_sort
{n : int}
(topo :
: toporesult (nodenum n, n) =
let
prval () = lemma_arrayref_param (topo.edges)
fun
sort (accum : List0 (
:
case+ find_unmarked_node topo of
| None () =>
Line 1,056 ⟶ 1,102:
val () = assertloc (i2sz (length accum) = topo.n)
in
end
| Some nodenum =>
begin
case+ visit (topo, nodenum, accum, NIL) of
|
|
end
Line 1,081 ⟶ 1,127:
fn
find_a_valid_order
(desc :
: toporesult String1 =
let
val @(topo, nodenames) =
prval [n : int] EQINT () = eqint_make_guint (topo.n)
val nodenames_array =
arrayref_make_list<String1> (sz2i (topo.n), nodenames)
implement
list_map$fopr<nodenum n><String1> i =
nodenames_array[i]
(* A shorthand for mapping from nodenum to string. *)
macdef map (lst) =
list_vt2t (list_map<nodenum n><String1> ,(lst))
in
case+ topological_sort topo of
|
| Topo_CYCLE cycle => Topo_CYCLE (map cycle)
end
Line 1,112 ⟶ 1,158:
in
case+ find_a_valid_order desc of
| Topo_SUCCESS valid_order =>
| Topo_CYCLE cycle =>
let
val last = list_last cycle
val cycl = rev (last :: cycle)
in
println! ("COMPILATION CYCLE: ", cycl : List0 string)
end
end
Line 1,142 ⟶ 1,195:
ieee, dware, dw05, dw06, dw07, gtech, dw01, dw04, dw02, std_cell_lib, synopsys, std, dw03, ramlib, des_system_lib
</pre>
AND ...
Data fed to standard input:
<pre>
a b; b d; d a e; e a;
</pre>
Data from standard output:
<pre>
COMPILATION CYCLE: a, e, d, b, a
</pre>
=={{header|Bracmat}}==
Line 3,571 ⟶ 3,612:
=={{header|J}}==
(see [[Talk:Topological_sort#J_implementation|talk page]] for some details about what happens here.)
<syntaxhighlight lang="j">dependencySort=: monad define
Line 5,418 ⟶ 5,461:
if not TryGetValue(s, Result) then
Result := nil;
end;
function TDigraph.TryToposort(out aOutSeq: TStringArray): Boolean;
var
Parents: TDictionary<string, string>;// stores the traversal tree as pairs
procedure ExtractCycle(const BackPoint: string; Prev: string);
begin // just walk backwards through the traversal tree, starting from Prev until BackPoint is encountered
with TList<string>.Create do begin
repeat
Prev := Parents[Prev];
until Prev = BackPoint;
Add(Items[0]);
Reverse; //this is required since we moved backwards through the tree
end
end;
var
Visited, // set of already visited nodes
Line 5,469 ⟶ 5,495:
end else
if not Closed.Contains(Next) then begin//back edge found(i.e. cycle)
ExtractCycle(Next, aNode);
exit(False);
end;
Line 6,911 ⟶ 6,937:
composer depRecord
(<WS>? def node: <~WS>; <WS>? <dep>* <WS>? $node -> ..|@collectDeps.v: {node: $};)
rule dep: (<~WS> -> ..|@collectDeps.e: {from: node´$node, to: node´$}; <WS>?)
end depRecord
$(3..last)... -> !depRecord
Line 7,711 ⟶ 7,737:
=={{header|Wren}}==
{{trans|Kotlin}}
<syntaxhighlight lang="
construct new(s, edges) {
_vertices = s.split(", ")
|