Data update
This commit is contained in:
parent
4bb20c9b71
commit
cbaf4c4b64
12390 changed files with 318560 additions and 27248 deletions
52
Task/Topological-sort/Ada/topological-sort-1.adb
Normal file
52
Task/Topological-sort/Ada/topological-sort-1.adb
Normal file
|
|
@ -0,0 +1,52 @@
|
|||
with Ada.Containers.Vectors; use Ada.Containers;
|
||||
|
||||
package Digraphs is
|
||||
|
||||
type Node_Idx_With_Null is new Natural;
|
||||
subtype Node_Index is Node_Idx_With_Null range 1 .. Node_Idx_With_Null'Last;
|
||||
-- a Node_Index is a number from 1, 2, 3, ... and the representative of a node
|
||||
|
||||
type Graph_Type is tagged private;
|
||||
|
||||
-- make sure Node is in Graph (possibly without connections)
|
||||
procedure Add_Node
|
||||
(Graph: in out Graph_Type'Class; Node: Node_Index);
|
||||
|
||||
-- insert an edge From->To into Graph; do nothing if already there
|
||||
procedure Add_Connection
|
||||
(Graph: in out Graph_Type'Class; From, To: Node_Index);
|
||||
|
||||
-- get the largest Node_Index used in any Add_Node or Add_Connection op.
|
||||
-- iterate over all nodes of Graph: "for I in 1 .. Graph.Node_Count loop ..."
|
||||
function Node_Count(Graph: Graph_Type) return Node_Idx_With_Null;
|
||||
|
||||
-- remove an edge From->To from Fraph; do nothing if not there
|
||||
-- Graph.Node_Count is not changed
|
||||
procedure Del_Connection
|
||||
(Graph: in out Graph_Type'Class; From, To: Node_Index);
|
||||
|
||||
-- check if an edge From->to exists in Graph
|
||||
function Connected
|
||||
(Graph: Graph_Type; From, To: Node_Index) return Boolean;
|
||||
|
||||
-- data structure to store a list of nodes
|
||||
package Node_Vec is new Vectors(Positive, Node_Index);
|
||||
|
||||
-- get a list of all nodes From->Somewhere in Graph
|
||||
function All_Connections
|
||||
(Graph: Graph_Type; From: Node_Index) return Node_Vec.Vector;
|
||||
|
||||
Graph_Is_Cyclic: exception;
|
||||
|
||||
-- a depth-first search to find a topological sorting of the nodes
|
||||
-- raises Graph_Is_Cyclic if no topological sorting is possible
|
||||
function Top_Sort
|
||||
(Graph: Graph_Type) return Node_Vec.Vector;
|
||||
|
||||
private
|
||||
|
||||
package Conn_Vec is new Vectors(Node_Index, Node_Vec.Vector, Node_Vec."=");
|
||||
|
||||
type Graph_Type is new Conn_Vec.Vector with null record;
|
||||
|
||||
end Digraphs;
|
||||
106
Task/Topological-sort/Ada/topological-sort-2.adb
Normal file
106
Task/Topological-sort/Ada/topological-sort-2.adb
Normal file
|
|
@ -0,0 +1,106 @@
|
|||
package body Digraphs is
|
||||
|
||||
function Node_Count(Graph: Graph_Type) return Node_Idx_With_Null is
|
||||
begin
|
||||
return Node_Idx_With_Null(Graph.Length);
|
||||
end Node_Count;
|
||||
|
||||
procedure Add_Node(Graph: in out Graph_Type'Class; Node: Node_Index) is
|
||||
begin
|
||||
for I in Node_Index range Graph.Node_Count+1 .. Node loop
|
||||
Graph.Append(Node_Vec.Empty_Vector);
|
||||
end loop;
|
||||
end Add_Node;
|
||||
|
||||
procedure Add_Connection
|
||||
(Graph: in out Graph_Type'Class; From, To: Node_Index) is
|
||||
begin
|
||||
Graph.Add_Node(Node_Index'Max(From, To));
|
||||
declare
|
||||
Connection_List: Node_Vec.Vector := Graph.Element(From);
|
||||
begin
|
||||
for I in Connection_List.First_Index .. Connection_List.Last_Index loop
|
||||
if Connection_List.Element(I) >= To then
|
||||
if Connection_List.Element(I) = To then
|
||||
return; -- if To is already there, don't add it a second time
|
||||
else -- I is the first index with Element(I)>To, insert To here
|
||||
Connection_List.Insert(Before => I, New_Item => To);
|
||||
Graph.Replace_Element(From, Connection_List);
|
||||
return;
|
||||
end if;
|
||||
end if;
|
||||
end loop;
|
||||
-- there was no I with no Element(I) > To, so insert To at the end
|
||||
Connection_List.Append(To);
|
||||
Graph.Replace_Element(From, Connection_List);
|
||||
return;
|
||||
end;
|
||||
end Add_Connection;
|
||||
|
||||
procedure Del_Connection
|
||||
(Graph: in out Graph_Type'Class; From, To: Node_Index) is
|
||||
Connection_List: Node_Vec.Vector := Graph.Element(From);
|
||||
begin
|
||||
for I in Connection_List.First_Index .. Connection_List.Last_Index loop
|
||||
if Connection_List.Element(I) = To then
|
||||
Connection_List.Delete(I);
|
||||
Graph.Replace_Element(From, Connection_List);
|
||||
return; -- we are done
|
||||
end if;
|
||||
end loop;
|
||||
end Del_Connection;
|
||||
|
||||
function Connected
|
||||
(Graph: Graph_Type; From, To: Node_Index) return Boolean is
|
||||
Connection_List: Node_Vec.Vector renames Graph.Element(From);
|
||||
begin
|
||||
for I in Connection_List.First_Index .. Connection_List.Last_Index loop
|
||||
if Connection_List.Element(I) = To then
|
||||
return True;
|
||||
end if;
|
||||
end loop;
|
||||
return False;
|
||||
end Connected;
|
||||
|
||||
function All_Connections
|
||||
(Graph: Graph_Type; From: Node_Index) return Node_Vec.Vector is
|
||||
begin
|
||||
return Graph.Element(From);
|
||||
end All_Connections;
|
||||
|
||||
function Top_Sort
|
||||
(Graph: Graph_Type) return Node_Vec.Vector is
|
||||
|
||||
Result: Node_Vec.Vector;
|
||||
Visited: array(1 .. Graph.Node_Count) of Boolean := (others => False);
|
||||
Active: array(1 .. Graph.Node_Count) of Boolean := (others => False);
|
||||
|
||||
procedure Visit(Node: Node_Index) is
|
||||
begin
|
||||
if not Visited(Node) then
|
||||
Visited(Node) := True;
|
||||
Active(Node) := True;
|
||||
declare
|
||||
Cons: Node_Vec.Vector := All_Connections(Graph, Node);
|
||||
begin
|
||||
for Idx in Cons.First_Index .. Cons.Last_Index loop
|
||||
Visit(Cons.Element(Idx));
|
||||
end loop;
|
||||
end;
|
||||
Active(Node) := False;
|
||||
Result.Append(Node);
|
||||
else
|
||||
if Active(Node) then
|
||||
raise Constraint_Error with "Graph is Cyclic";
|
||||
end if;
|
||||
end if;
|
||||
end Visit;
|
||||
|
||||
begin
|
||||
for Some_Node in Visited'Range loop
|
||||
Visit(Some_Node);
|
||||
end loop;
|
||||
return Result;
|
||||
end Top_Sort;
|
||||
|
||||
end Digraphs;
|
||||
42
Task/Topological-sort/Ada/topological-sort-3.adb
Normal file
42
Task/Topological-sort/Ada/topological-sort-3.adb
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
private with Ada.Containers.Indefinite_Vectors;
|
||||
|
||||
generic
|
||||
type Index_Type_With_Null is new Natural;
|
||||
package Set_Of_Names is
|
||||
subtype Index_Type is Index_Type_With_Null
|
||||
range 1 .. Index_Type_With_Null'Last;
|
||||
-- manage a set of strings;
|
||||
-- each string in the set is assigned a unique index of type Index_Type
|
||||
|
||||
type Set is tagged private;
|
||||
|
||||
-- inserts Name into Names; do nothing if already there;
|
||||
procedure Add(Names: in out Set; Name: String);
|
||||
|
||||
-- Same operation, additionally emiting Index=Names.Idx(Name)
|
||||
procedure Add(Names: in out Set; Name: String; Index: out Index_Type);
|
||||
|
||||
-- remove Name from Names; do nothing if not found
|
||||
-- the removal may change the index of other strings in Names
|
||||
procedure Sub(Names: in out Set; Name: String);
|
||||
|
||||
-- returns the unique index of Name in Set; or 0 if Name is not there
|
||||
function Idx(Names: Set; Name: String) return Index_Type_With_Null;
|
||||
|
||||
-- returns the unique name of Index;
|
||||
function Name(Names: Set; Index: Index_Type) return String;
|
||||
|
||||
-- first index, last index and total number of names in set
|
||||
-- to iterate over Names, use "for I in Names.Start .. Names.Stop loop ...
|
||||
function Start(Names: Set) return Index_Type;
|
||||
function Stop(Names: Set) return Index_Type_With_Null;
|
||||
function Size(Names: Set) return Index_Type_With_Null;
|
||||
|
||||
private
|
||||
|
||||
package Vecs is new Ada.Containers.Indefinite_Vectors
|
||||
(Index_Type => Index_Type, Element_Type => String);
|
||||
|
||||
type Set is new Vecs.Vector with null record;
|
||||
|
||||
end Set_Of_Names;
|
||||
68
Task/Topological-sort/Ada/topological-sort-4.adb
Normal file
68
Task/Topological-sort/Ada/topological-sort-4.adb
Normal file
|
|
@ -0,0 +1,68 @@
|
|||
package body Set_Of_Names is
|
||||
|
||||
use type Ada.Containers.Count_Type, Vecs.Cursor;
|
||||
|
||||
function Start(Names: Set) return Index_Type is
|
||||
begin
|
||||
if Names.Length = 0 then
|
||||
return 1;
|
||||
else
|
||||
return Names.First_Index;
|
||||
end if;
|
||||
end Start;
|
||||
|
||||
function Stop(Names: Set) return Index_Type_With_Null is
|
||||
begin
|
||||
if Names.Length=0 then
|
||||
return 0;
|
||||
else
|
||||
return Names.Last_Index;
|
||||
end if;
|
||||
end Stop;
|
||||
|
||||
function Size(Names: Set) return Index_Type_With_Null is
|
||||
begin
|
||||
return Index_Type_With_Null(Names.Length);
|
||||
end Size;
|
||||
|
||||
procedure Add(Names: in out Set; Name: String; Index: out Index_Type) is
|
||||
I: Index_Type_With_Null := Names.Idx(Name);
|
||||
begin
|
||||
if I = 0 then -- Name is not yet in Set
|
||||
Names.Append(Name);
|
||||
Index := Names.Stop;
|
||||
else
|
||||
Index := I;
|
||||
end if;
|
||||
end Add;
|
||||
|
||||
procedure Add(Names: in out Set; Name: String) is
|
||||
I: Index_Type;
|
||||
begin
|
||||
Names.Add(Name, I);
|
||||
end Add;
|
||||
|
||||
procedure Sub(Names: in out Set; Name: String) is
|
||||
I: Index_Type_With_Null := Names.Idx(Name);
|
||||
begin
|
||||
if I /= 0 then -- Name is in set
|
||||
Names.Delete(I);
|
||||
end if;
|
||||
end Sub;
|
||||
|
||||
function Idx(Names: Set; Name: String) return Index_Type_With_Null is
|
||||
begin
|
||||
for I in Names.First_Index .. Names.Last_Index loop
|
||||
if Names.Element(I) = Name then
|
||||
return I;
|
||||
end if;
|
||||
end loop;
|
||||
return 0;
|
||||
end Idx;
|
||||
|
||||
function Name(Names: Set; Index: Index_Type) return String is
|
||||
begin
|
||||
return Names.Element(Index);
|
||||
end Name;
|
||||
|
||||
end Set_Of_Names;
|
||||
83
Task/Topological-sort/Ada/topological-sort-5.adb
Normal file
83
Task/Topological-sort/Ada/topological-sort-5.adb
Normal file
|
|
@ -0,0 +1,83 @@
|
|||
with Ada.Text_IO, Digraphs, Set_Of_Names, Ada.Command_Line;
|
||||
|
||||
procedure Toposort is
|
||||
|
||||
-- shortcuts for package names, intantiation of generic package
|
||||
package TIO renames Ada.Text_IO;
|
||||
package DG renames Digraphs;
|
||||
package SN is new Set_Of_Names(DG.Node_Idx_With_Null);
|
||||
|
||||
-- reat the graph from the file with the given Filename
|
||||
procedure Read(Filename: String; G: out DG.Graph_Type; N: out SN.Set) is
|
||||
|
||||
-- finds the first word in S(Start .. S'Last), delimited by spaces
|
||||
procedure Find_Token(S: String; Start: Positive;
|
||||
First: out Positive; Last: out Natural) is
|
||||
|
||||
begin
|
||||
First := Start;
|
||||
while First <= S'Last and then S(First)= ' ' loop
|
||||
First := First + 1;
|
||||
end loop;
|
||||
Last := First-1;
|
||||
while Last < S'Last and then S(Last+1) /= ' ' loop
|
||||
Last := Last + 1;
|
||||
end loop;
|
||||
end Find_Token;
|
||||
|
||||
File: TIO.File_Type;
|
||||
begin
|
||||
TIO.Open(File, TIO.In_File, Filename);
|
||||
TIO.Skip_Line(File, 2);
|
||||
-- the first two lines contain header and "===...==="
|
||||
while not TIO.End_Of_File(File) loop
|
||||
declare
|
||||
Line: String := TIO.Get_Line(File);
|
||||
First: Positive;
|
||||
Last: Natural;
|
||||
To, From: DG.Node_Index;
|
||||
begin
|
||||
Find_Token(Line, Line'First, First, Last);
|
||||
if Last >= First then
|
||||
N.Add(Line(First .. Last), From);
|
||||
G.Add_Node(From);
|
||||
loop
|
||||
Find_Token(Line, Last+1, First, Last);
|
||||
exit when Last < First;
|
||||
N.Add(Line(First .. Last), To);
|
||||
G.Add_Connection(From, To);
|
||||
end loop;
|
||||
end if;
|
||||
end;
|
||||
end loop;
|
||||
TIO.Close(File);
|
||||
end Read;
|
||||
|
||||
Graph: DG.Graph_Type;
|
||||
Names: SN.Set;
|
||||
|
||||
begin
|
||||
Read(Ada.Command_Line.Argument(1), Graph, Names);
|
||||
|
||||
-- eliminat self-cycles
|
||||
for Start in 1 .. Graph.Node_Count loop
|
||||
Graph.Del_Connection(Start, Start);
|
||||
end loop;
|
||||
|
||||
-- perform the topological sort and output the result
|
||||
declare
|
||||
Result: DG.Node_Vec.Vector;
|
||||
begin
|
||||
Result := Graph.Top_Sort;
|
||||
for Index in Result.First_Index .. Result.Last_Index loop
|
||||
TIO.Put(Names.Name(Result.Element(Index)));
|
||||
if Index < Result.Last_Index then
|
||||
TIO.Put(" -> ");
|
||||
end if;
|
||||
end loop;
|
||||
TIO.New_Line;
|
||||
exception
|
||||
when DG.Graph_Is_Cyclic =>
|
||||
TIO.Put_Line("There is no topological sorting -- the Graph is cyclic!");
|
||||
end;
|
||||
end Toposort;
|
||||
179
Task/Topological-sort/Dart/topological-sort.dart
Normal file
179
Task/Topological-sort/Dart/topological-sort.dart
Normal file
|
|
@ -0,0 +1,179 @@
|
|||
import 'dart:io';
|
||||
|
||||
class Relations<T> {
|
||||
int dependencies = 0;
|
||||
Set<T> dependents = <T>{};
|
||||
}
|
||||
|
||||
class TopologicalSorter<T> {
|
||||
Map<T, Relations<T>> _map = <T, Relations<T>>{};
|
||||
|
||||
void addGoal(T goal) {
|
||||
_map.putIfAbsent(goal, () => Relations<T>());
|
||||
}
|
||||
|
||||
void addDependency(T goal, T dependency) {
|
||||
if (dependency == goal) return;
|
||||
|
||||
var dependencyRelations = _map.putIfAbsent(dependency, () => Relations<T>());
|
||||
var goalRelations = _map.putIfAbsent(goal, () => Relations<T>());
|
||||
|
||||
if (!dependencyRelations.dependents.contains(goal)) {
|
||||
dependencyRelations.dependents.add(goal);
|
||||
goalRelations.dependencies++;
|
||||
}
|
||||
}
|
||||
|
||||
void addDependencies(T goal, Iterable<T> dependencies) {
|
||||
for (var dependency in dependencies) {
|
||||
addDependency(goal, dependency);
|
||||
}
|
||||
}
|
||||
|
||||
void destructiveSort(List<T> sorted, List<T> unsortable) {
|
||||
sorted.clear();
|
||||
unsortable.clear();
|
||||
|
||||
// Find all goals with no dependencies
|
||||
for (var entry in _map.entries) {
|
||||
var goal = entry.key;
|
||||
var relations = entry.value;
|
||||
if (relations.dependencies == 0) {
|
||||
sorted.add(goal);
|
||||
}
|
||||
}
|
||||
|
||||
// Process goals in topological order
|
||||
for (int index = 0; index < sorted.length; index++) {
|
||||
var currentGoal = sorted[index];
|
||||
var currentRelations = _map[currentGoal]!;
|
||||
|
||||
for (var dependentGoal in currentRelations.dependents) {
|
||||
var dependentRelations = _map[dependentGoal]!;
|
||||
dependentRelations.dependencies--;
|
||||
if (dependentRelations.dependencies == 0) {
|
||||
sorted.add(dependentGoal);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Find cyclic dependencies
|
||||
for (var entry in _map.entries) {
|
||||
var goal = entry.key;
|
||||
var relations = entry.value;
|
||||
if (relations.dependencies != 0) {
|
||||
unsortable.add(goal);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void sort(List<T> sorted, List<T> unsortable) {
|
||||
var temporary = TopologicalSorter<T>();
|
||||
|
||||
// Deep copy the current state
|
||||
for (var entry in _map.entries) {
|
||||
var goal = entry.key;
|
||||
var relations = entry.value;
|
||||
temporary.addGoal(goal);
|
||||
temporary._map[goal]!.dependencies = relations.dependencies;
|
||||
temporary._map[goal]!.dependents = Set<T>.from(relations.dependents);
|
||||
}
|
||||
|
||||
temporary.destructiveSort(sorted, unsortable);
|
||||
}
|
||||
|
||||
void clear() {
|
||||
_map.clear();
|
||||
}
|
||||
}
|
||||
|
||||
void displayHeading(String message) {
|
||||
print('\n~ $message ~');
|
||||
}
|
||||
|
||||
void displayResults(String input) {
|
||||
var sorter = TopologicalSorter<String>();
|
||||
var sorted = <String>[];
|
||||
var unsortable = <String>[];
|
||||
|
||||
var lines = input.trim().split('\n');
|
||||
for (var line in lines) {
|
||||
if (line.trim().isEmpty) continue;
|
||||
|
||||
var parts = line.trim().split(RegExp(r'\s+'));
|
||||
if (parts.isEmpty) continue;
|
||||
|
||||
var goal = parts[0];
|
||||
sorter.addGoal(goal);
|
||||
|
||||
for (int i = 1; i < parts.length; i++) {
|
||||
sorter.addDependency(goal, parts[i]);
|
||||
}
|
||||
}
|
||||
|
||||
sorter.destructiveSort(sorted, unsortable);
|
||||
|
||||
if (sorted.isEmpty) {
|
||||
displayHeading("Error: no independent variables found!");
|
||||
} else {
|
||||
displayHeading("Result");
|
||||
for (var goal in sorted) {
|
||||
print(goal);
|
||||
}
|
||||
}
|
||||
|
||||
if (unsortable.isNotEmpty) {
|
||||
displayHeading("Error: cyclic dependencies detected!");
|
||||
for (var goal in unsortable) {
|
||||
print(goal);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void main(List<String> arguments) {
|
||||
if (arguments.isEmpty) {
|
||||
var example = '''des_system_lib std synopsys std_cell_lib des_system_lib dw02 dw01 ramlib ieee
|
||||
dw01 ieee dw01 dware gtech
|
||||
dw02 ieee dw02 dware
|
||||
dw03 std synopsys dware dw03 dw02 dw01 ieee gtech
|
||||
dw04 dw04 ieee dw01 dware gtech
|
||||
dw05 dw05 ieee dware
|
||||
dw06 dw06 ieee dware
|
||||
dw07 ieee dware
|
||||
dware ieee dware
|
||||
gtech ieee gtech
|
||||
ramlib std ieee
|
||||
std_cell_lib ieee std_cell_lib
|
||||
synopsys
|
||||
cycle_11 cycle_12
|
||||
cycle_12 cycle_11
|
||||
cycle_21 dw01 cycle_22 dw02 dw03
|
||||
cycle_22 cycle_21 dw01 dw04''';
|
||||
|
||||
displayHeading("Example: each line starts with a goal followed by its dependencies");
|
||||
print(example);
|
||||
displayResults(example);
|
||||
|
||||
displayHeading("Enter lines of data (press enter when finished)");
|
||||
var lines = <String>[];
|
||||
while (true) {
|
||||
var line = stdin.readLineSync();
|
||||
if (line == null || line.isEmpty) break;
|
||||
lines.add(line);
|
||||
}
|
||||
|
||||
if (lines.isNotEmpty) {
|
||||
displayResults(lines.join('\n'));
|
||||
}
|
||||
} else {
|
||||
for (var filename in arguments) {
|
||||
try {
|
||||
var file = File(filename);
|
||||
var content = file.readAsStringSync();
|
||||
displayResults(content);
|
||||
} catch (e) {
|
||||
print('Error reading file $filename: $e');
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
66
Task/Topological-sort/EasyLang/topological-sort.easy
Normal file
66
Task/Topological-sort/EasyLang/topological-sort.easy
Normal file
|
|
@ -0,0 +1,66 @@
|
|||
global name$[] g[][] .
|
||||
func n2id n$ .
|
||||
for id to len name$[] : if name$[id] = n$ : return id
|
||||
name$[] &= n$
|
||||
return id
|
||||
.
|
||||
proc init .
|
||||
repeat
|
||||
s$ = input
|
||||
until s$ = ""
|
||||
a$[] = strtok s$ " "
|
||||
left = n2id a$[1]
|
||||
if left > len g[][] : len g[][] left
|
||||
for i = 2 to len a$[]
|
||||
id = n2id a$[i]
|
||||
if id <> left
|
||||
g[left][] &= id
|
||||
.
|
||||
.
|
||||
.
|
||||
.
|
||||
init
|
||||
#
|
||||
lng = len g[][]
|
||||
len perm[] lng
|
||||
len temp[] lng
|
||||
global cycle result[] .
|
||||
#
|
||||
proc visit n .
|
||||
if perm[n] = 1 : return
|
||||
if temp[n] = 1
|
||||
cycle = 1
|
||||
return
|
||||
.
|
||||
temp[n] = 1
|
||||
for m in g[n][] : visit m
|
||||
perm[n] = 1
|
||||
result[] &= n
|
||||
.
|
||||
repeat
|
||||
for i to lng
|
||||
if perm[i] = 0 : break 1
|
||||
.
|
||||
until i > lng
|
||||
visit i
|
||||
.
|
||||
if cycle = 1
|
||||
print "un-orderable"
|
||||
else
|
||||
for r in result[] : print name$[r]
|
||||
.
|
||||
#
|
||||
input_data
|
||||
des_system_lib std synopsys std_cell_lib des_system_lib dw02 dw01 ramlib ieee
|
||||
dw01 ieee dw01 dware gtech
|
||||
dw02 ieee dw02 dware
|
||||
dw03 std synopsys dware dw03 dw02 dw01 ieee gtech
|
||||
dw04 dw04 ieee dw01 dware gtech
|
||||
dw05 dw05 ieee dware
|
||||
dw06 dw06 ieee dware
|
||||
dw07 ieee dware
|
||||
dware ieee dware
|
||||
gtech ieee gtech
|
||||
ramlib std ieee
|
||||
std_cell_lib ieee std_cell_lib
|
||||
synopsys
|
||||
99
Task/Topological-sort/PowerShell/topological-sort.ps1
Normal file
99
Task/Topological-sort/PowerShell/topological-sort.ps1
Normal file
|
|
@ -0,0 +1,99 @@
|
|||
#Input Data
|
||||
$a=@"
|
||||
des_system_lib std synopsys std_cell_lib des_system_lib dw02 dw01 ramlib ieee
|
||||
dw01 ieee dw01 dware gtech
|
||||
dw02 ieee dw02 dware
|
||||
dw03 std synopsys dware dw03 dw02 dw01 ieee gtech
|
||||
dw04 dw04 ieee dw01 dware gtech
|
||||
dw05 dw05 ieee dware
|
||||
dw06 dw06 ieee dware
|
||||
dw07 ieee dware
|
||||
dware ieee dware
|
||||
gtech ieee gtech
|
||||
ramlib std ieee
|
||||
std_cell_lib ieee std_cell_lib
|
||||
synopsys
|
||||
"@
|
||||
#Convert to Object[]
|
||||
$c = switch ( $a.split([char] 10) ) {
|
||||
$_ {
|
||||
$b=$_.split(' ')
|
||||
New-Object PSObject -Property @{
|
||||
Library = $b[0]
|
||||
"Library Dependencies" = @( $( $b[1..($b.length-1)] | Where-Object { $_ -match '\w' } ) )
|
||||
}
|
||||
}
|
||||
}
|
||||
#Add pure dependencies
|
||||
$c | ForEach-Object {
|
||||
$_."Library Dependencies" | Where-Object {
|
||||
$d=$_
|
||||
$(:andl foreach($i in $c) {
|
||||
if($d -match $i.Library) {
|
||||
$false
|
||||
break andl
|
||||
}
|
||||
}) -eq $null
|
||||
} | ForEach-Object {
|
||||
$c+=New-Object PSObject -Property @{
|
||||
Library=$_
|
||||
"Library Dependencies"=@()
|
||||
}
|
||||
}
|
||||
}
|
||||
#Associate with a dependency value
|
||||
##Initial Dependency Value
|
||||
$d = $c | Sort Library | Select-Object Library,"Library Dependencies",@{
|
||||
Name="Dep Value"
|
||||
Expression={
|
||||
1
|
||||
}
|
||||
}
|
||||
##Modify Dependency Value, perform check for incorrect dependency
|
||||
##Dep Value is determined by a parent child relationship, if a library is a parent, all libraries dependant on it are children
|
||||
for( $i=0; $i -lt $d.count; $i++ ) {
|
||||
$errmsg=""
|
||||
foreach( $j in ( 0..( $d.count - 1 ) | Where-Object { $_ -ne $i } ) ) {
|
||||
#Foreach other Child Library where this is a dependency, increase the Dep Value of the Child
|
||||
if( $( :orl foreach( $k in $d[$j]."Library Dependencies" ) {
|
||||
if( $k -match $d[$i].Library ) {
|
||||
foreach( $n in $d[$i]."Library Dependencies" ) {
|
||||
if( $n -match $d[$j].Library ) {
|
||||
$errmsg="Error Cyclic Dependency {0}<->{1}" -f $d[$i].Library, $d[$j].Library
|
||||
break
|
||||
}
|
||||
}
|
||||
$true
|
||||
break orl
|
||||
}
|
||||
} ) ) {
|
||||
#If the child has already been processed, increase the Dep Value of its children
|
||||
if( $j -lt $i ) {
|
||||
foreach( $l in ( 0..( $d.count - 1 ) | Where-Object { $_ -ne $j } ) ) {
|
||||
if( $( :orl2 foreach( $m in $d[$l]."Library Dependencies" ) {
|
||||
if( $m -match $d[$j].Library ) {
|
||||
$true
|
||||
break orl2
|
||||
}
|
||||
} ) ) {
|
||||
$d[$l]."Dep Value"+=$d[$i]."Dep Value"
|
||||
}
|
||||
}
|
||||
}
|
||||
$d[$j]."Dep Value"+=$d[$i]."Dep Value"
|
||||
}
|
||||
if( $errmsg -ne "" ) {
|
||||
$errmsg
|
||||
$d=$null
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
#Sort and Display
|
||||
if( $d ) {
|
||||
$d | Sort "Dep Value",Library | ForEach-Object {
|
||||
"{0,-14} LIBRARY DEPENDENCIES`n{1,-14} ====================" -f "LIBRARY", "======="
|
||||
} {
|
||||
"{0,-14} $($_."Library Dependencies")" -f $_.Library
|
||||
}
|
||||
}
|
||||
79
Task/Topological-sort/VBScript/topological-sort-1.vbs
Normal file
79
Task/Topological-sort/VBScript/topological-sort-1.vbs
Normal file
|
|
@ -0,0 +1,79 @@
|
|||
class topological
|
||||
dim dictDependencies
|
||||
dim dictReported
|
||||
dim depth
|
||||
|
||||
sub class_initialize
|
||||
set dictDependencies = createobject("Scripting.Dictionary")
|
||||
set dictReported = createobject("Scripting.Dictionary")
|
||||
depth = 0
|
||||
end sub
|
||||
|
||||
sub reset
|
||||
dictReported.removeall
|
||||
end sub
|
||||
|
||||
property let dependencies( s )
|
||||
'assuming token tab token-list newline
|
||||
dim i, j ,k
|
||||
dim aList
|
||||
dim dep
|
||||
dim a1
|
||||
aList = Split( s, vbNewLine )
|
||||
'~ remove empty lines at end
|
||||
do while aList( UBound( aList ) ) = vbnullstring
|
||||
redim preserve aList( UBound( aList ) - 1 )
|
||||
loop
|
||||
|
||||
for i = lbound( aList ) to ubound( aList )
|
||||
aList( i ) = Split( aList( i ), vbTab, 2 )
|
||||
a1 = Split( aList( i )( 1 ), " " )
|
||||
k = 0
|
||||
for j = lbound( a1) to ubound(a1)
|
||||
if a1(j) <> aList(i)(0) then
|
||||
a1(k) = a1(j)
|
||||
k = k + 1
|
||||
end if
|
||||
next
|
||||
redim preserve a1(k-1)
|
||||
aList(i)(1) = a1
|
||||
next
|
||||
for i = lbound( aList ) to ubound( aList )
|
||||
dep = aList(i)(0)
|
||||
if not dictDependencies.Exists( dep ) then
|
||||
dictDependencies.add dep, aList(i)(1)
|
||||
end if
|
||||
next
|
||||
|
||||
end property
|
||||
|
||||
sub resolve( s )
|
||||
dim i
|
||||
dim deps
|
||||
'~ wscript.echo string(depth,"!"),s
|
||||
depth = depth + 1
|
||||
if dictDependencies.Exists(s) then
|
||||
deps = dictDependencies(s)
|
||||
for i = lbound(deps) to ubound(deps)
|
||||
resolve deps(i)
|
||||
next
|
||||
end if
|
||||
if not seen(s) then
|
||||
wscript.echo s
|
||||
see s
|
||||
end if
|
||||
depth = depth - 1
|
||||
end sub
|
||||
|
||||
function seen( key )
|
||||
seen = dictReported.Exists( key )
|
||||
end function
|
||||
|
||||
sub see( key )
|
||||
dictReported.add key, ""
|
||||
end sub
|
||||
|
||||
property get keys
|
||||
keys = dictDependencies.keys
|
||||
end property
|
||||
end class
|
||||
23
Task/Topological-sort/VBScript/topological-sort-2.vbs
Normal file
23
Task/Topological-sort/VBScript/topological-sort-2.vbs
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
dim toposort
|
||||
set toposort = new topological
|
||||
toposort.dependencies = "des_system_lib std synopsys std_cell_lib des_system_lib dw02 dw01 ramlib ieee" & vbNewLine & _
|
||||
"dw01 ieee dw01 dware gtech" & vbNewLine & _
|
||||
"dw02 ieee dw02 dware" & vbNewLine & _
|
||||
"dw03 std synopsys dware dw03 dw02 dw01 ieee gtech" & vbNewLine & _
|
||||
"dw04 dw04 ieee dw01 dware gtech" & vbNewLine & _
|
||||
"dw05 dw05 ieee dware" & vbNewLine & _
|
||||
"dw06 dw06 ieee dware" & vbNewLine & _
|
||||
"dw07 ieee dware" & vbNewLine & _
|
||||
"dware ieee dware" & vbNewLine & _
|
||||
"gtech ieee gtech" & vbNewLine & _
|
||||
"ramlib std ieee" & vbNewLine & _
|
||||
"std_cell_lib ieee std_cell_lib" & vbNewLine & _
|
||||
"synopsys "
|
||||
|
||||
dim k
|
||||
for each k in toposort.keys
|
||||
wscript.echo "----- " & k
|
||||
toposort.resolve k
|
||||
wscript.echo "-----"
|
||||
toposort.reset
|
||||
next
|
||||
Loading…
Add table
Add a link
Reference in a new issue