Topological sort: Difference between revisions

m
→‎{{header|Wren}}: Changed to Wren S/H
m (→‎{{header|Wren}}: Changed to Wren S/H)
 
(6 intermediate revisions by 4 users not shown)
Line 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 (text,(s), ,(i), j - i,(n)))
 
macdef copystr (s) =
(* string0 copy returns a linear strptr, but we want a "regular"
t: string;. *)
strptr2string (string0_copy (,(s)))
 
(*------------------------------------------------------------------*)
Line 523 ⟶ 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 656 ⟶ 685:
in
if i = n then
@(list_vt2t (reverserev row), i)
else if is_end_of_list text[i] then
@(list_vt2t (reverserev row), succ i)
else
let
val j = skip_ident (text, n, i)
val () = $effmask_exn assertloc (i < j)
val nodename = substr (text, i, j - i)
strnptr2string
(string_make_substring (text, i, j - i))
in
loop (nodename :: row, j)
Line 694 ⟶ 721:
in
if i = n then
@(list_vt2t (reverserev desc), i)
else if is_end_of_list text[i] then
@(list_vt2t (reverserev desc), succ i)
else
let
Line 729 ⟶ 756:
c := $extfcall (int, "getchar")
end;
strptr2string (string0_copycopystr ($UN.cast{string} (addr@ buf)))
end
 
Line 751 ⟶ 778:
typedef nodenames (n : int) = list (String1, n)
 
(* A more efficient representation for nodes: integers in 0..n-1. *)
typedef nodenum (n : int) = [num : nat | num <= n - 1] size_t num
 
(* A collection of directed edges. *)Edges go from the nodenum that is
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)
 
Line 820 ⟶ 849:
in
collect (desc, names, n);
@(list_vt2t (reverserev names), n)
end
 
Line 1,112 ⟶ 1,141:
nodenames_array[i]
 
macdef(* mapA =shorthand for mapping from list_map<nodenum n><String1>to string. *)
macdef map (lst) =
list_vt2t (list_map<nodenum n><String1> ,(lst))
in
case+ topological_sort topo of
| Topo_SUCCESS valid_order => Topo_SUCCESS (map valid_order)
| Topo_CYCLE Topo_SUCCESScycle (list_vt2t=> Topo_CYCLE (map valid_order)cycle)
| Topo_CYCLE cycle =>
Topo_CYCLE (list_vt2t (map cycle))
end
 
Line 1,134 ⟶ 1,163:
let
val last = list_last cycle
val cycl = list_vt2t (reverserev (last :: cycle))
in
println! ("COMPILATION CYCLE: ", cycl : List0 string)
Line 3,583 ⟶ 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,430 ⟶ 5,461:
if not TryGetValue(s, Result) then
Result := nil;
end;
 
procedure Reverse(var a: array of string);
var
I, J: SizeInt;
t: string;
begin
I := 0;
J := High(a);
while I < J do begin
t := a[I];
a[I] := a[J];
a[J] := t;
Inc(I);
Dec(J);
end;
end;
 
function TDigraph.TryToposort(out aOutSeq: TStringArray): Boolean;
var
Parents: TDictionary<string, string>;// stores the traversal tree as pairs: (Node, its predecessor)
procedure ExtractCycle(const BackPoint,: string; Prev: string);
// (Key: Node, Value: its predecessor)
begin // aOutSeq[0]just :=walk Prev;backwards //through the traversal tree, starting from Prev until BackPoint is encountered
procedure ExtractCycle(const BackPoint, Prev: string);
with TList<string>.Create do begin
var
I: SizeInt Add(Prev);
repeat
begin // just walk backwards through the traversal tree,
aOutSeq[I] Prev := Parents[aOutSeq[I-1]Prev];
aOutSeq[0] := Prev; // starting from Prev until BackPoint is encountered
I := 1 Add(Prev);
until Prev = BackPoint;
repeat
IncAdd(IItems[0]);
aOutSeq[I] := Parents[aOutSeq[I-1]];
Reverse(aOutSeq); //this is required since we moved backwards through the tree
Inc(I);
until aOutSeq[I-1] := BackPointToArray;
SetLength(aOutSeq, I+1) Free;
end;
aOutSeq[I] := Prev;
end;
Reverse(aOutSeq); //this is required since we moved backwards through the tree
end;
var
Visited, // set of already visited nodes
Line 5,481 ⟶ 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,923 ⟶ 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,723 ⟶ 7,737:
=={{header|Wren}}==
{{trans|Kotlin}}
<syntaxhighlight lang="ecmascriptwren">class Graph {
construct new(s, edges) {
_vertices = s.split(", ")
9,476

edits