Data update
This commit is contained in:
parent
8e4e15fa56
commit
72eb4943cb
1853 changed files with 35514 additions and 9441 deletions
150
Task/Topological-sort/FreeBASIC/topological-sort-1.basic
Normal file
150
Task/Topological-sort/FreeBASIC/topological-sort-1.basic
Normal file
|
|
@ -0,0 +1,150 @@
|
|||
Type Pair
|
||||
primero As Integer
|
||||
segundo As Integer
|
||||
End Type
|
||||
|
||||
Type Graph
|
||||
vertices(14) As String
|
||||
numVertices As Integer
|
||||
proximos(14, 14) As Boolean
|
||||
|
||||
Declare Constructor(s As String, edges() As Pair)
|
||||
Declare Function hasDependency(r As Integer, todo() As Integer, todoCount As Integer) As Boolean
|
||||
Declare Function topoSort() As String
|
||||
End Type
|
||||
|
||||
Function splitString(text As String, delimiter As String, Byref count As Integer) As String Ptr
|
||||
Dim As Integer numTokens = 0
|
||||
Dim As String tmp = text
|
||||
Dim As Long posic
|
||||
|
||||
' Count delimiters
|
||||
Do
|
||||
posic = Instr(tmp, delimiter)
|
||||
If posic = 0 Then Exit Do
|
||||
numTokens += 1
|
||||
tmp = Mid(tmp, posic + Len(delimiter))
|
||||
Loop
|
||||
numTokens += 1
|
||||
|
||||
' Allocate array
|
||||
count = numTokens
|
||||
Dim As String Ptr result = Callocate((numTokens) * Sizeof(String))
|
||||
|
||||
' Split string
|
||||
tmp = text
|
||||
numTokens = 0
|
||||
Do
|
||||
posic = Instr(tmp, delimiter)
|
||||
If posic = 0 Then
|
||||
result[numTokens] = tmp
|
||||
Exit Do
|
||||
End If
|
||||
result[numTokens] = Left(tmp, posic - 1)
|
||||
tmp = Mid(tmp, posic + Len(delimiter))
|
||||
numTokens += 1
|
||||
Loop
|
||||
|
||||
Return result
|
||||
End Function
|
||||
|
||||
Constructor Graph(s As String, edges() As Pair)
|
||||
Dim As Integer i, tokenCount
|
||||
Dim As String Ptr tokens = splitString(s, ", ", tokenCount)
|
||||
numVertices = tokenCount
|
||||
For i = 0 To numVertices - 1
|
||||
vertices(i) = tokens[i]
|
||||
Next
|
||||
Deallocate(tokens)
|
||||
|
||||
' Initialize proximos matrix
|
||||
For i = 0 To Ubound(edges)
|
||||
proximos(edges(i).primero, edges(i).segundo) = True
|
||||
Next
|
||||
End Constructor
|
||||
|
||||
Function Graph.hasDependency(r As Integer, todo() As Integer, todoCount As Integer) As Boolean
|
||||
For i As Integer = 0 To todoCount - 1
|
||||
If proximos(r, todo(i)) Then Return True
|
||||
Next
|
||||
Return False
|
||||
End Function
|
||||
|
||||
Function Graph.topoSort() As String
|
||||
Dim As String result = ""
|
||||
Dim As Integer todoCount, i, j
|
||||
Dim As Integer todo(numVertices)
|
||||
|
||||
todoCount = numVertices
|
||||
' Initialize todo list
|
||||
For i = 0 To numVertices - 1
|
||||
todo(i) = i
|
||||
Next
|
||||
|
||||
While todoCount > 0
|
||||
i = 0
|
||||
Dim As Boolean found = False
|
||||
|
||||
While i < todoCount
|
||||
If Not hasDependency(todo(i), todo(), todoCount) Then
|
||||
' Add to result
|
||||
If Len(result) > 0 Then result &= ", "
|
||||
result &= vertices(todo(i))
|
||||
|
||||
' Remove from todo
|
||||
For j = i To todoCount - 2
|
||||
todo(j) = todo(j + 1)
|
||||
Next
|
||||
todoCount -= 1
|
||||
found = True
|
||||
Exit While
|
||||
End If
|
||||
i += 1
|
||||
Wend
|
||||
|
||||
If Not found Then Return "Graph has cycles"
|
||||
Wend
|
||||
|
||||
Return "[" & result & "]"
|
||||
End Function
|
||||
|
||||
' Main program
|
||||
Dim As String s = "std, ieee, des_system_lib, dw01, dw02, dw03, dw04, dw05, " & _
|
||||
"dw06, dw07, dware, gtech, ramlib, std_cell_lib, synopsys"
|
||||
|
||||
Dim As Pair deps(33)
|
||||
' Initialize deps array
|
||||
deps(0) = Type<Pair>(2, 0) : deps(1) = Type<Pair>(2, 14)
|
||||
deps(2) = Type<Pair>(2, 13) : deps(3) = Type<Pair>(2, 4)
|
||||
deps(4) = Type<Pair>(2, 3) : deps(5) = Type<Pair>(2, 12)
|
||||
deps(6) = Type<Pair>(2, 1) : deps(7) = Type<Pair>(3, 1)
|
||||
deps(8) = Type<Pair>(3, 10) : deps(9) = Type<Pair>(3, 11)
|
||||
deps(10) = Type<Pair>(4, 1) : deps(11) = Type<Pair>(4, 10)
|
||||
deps(12) = Type<Pair>(5, 0) : deps(13) = Type<Pair>(5, 14)
|
||||
deps(14) = Type<Pair>(5, 10) : deps(15) = Type<Pair>(5, 4)
|
||||
deps(16) = Type<Pair>(5, 3) : deps(17) = Type<Pair>(5, 1)
|
||||
deps(18) = Type<Pair>(5, 11) : deps(19) = Type<Pair>(6, 1)
|
||||
deps(20) = Type<Pair>(6, 3) : deps(21) = Type<Pair>(6, 10)
|
||||
deps(22) = Type<Pair>(6, 11) : deps(23) = Type<Pair>(7, 1)
|
||||
deps(24) = Type<Pair>(7, 10) : deps(25) = Type<Pair>(8, 1)
|
||||
deps(26) = Type<Pair>(8, 10) : deps(27) = Type<Pair>(9, 1)
|
||||
deps(28) = Type<Pair>(9, 10) : deps(29) = Type<Pair>(10, 1)
|
||||
deps(30) = Type<Pair>(11, 1) : deps(31) = Type<Pair>(12, 0)
|
||||
deps(32) = Type<Pair>(12, 1) : deps(33) = Type<Pair>(13, 1)
|
||||
|
||||
Dim As Graph g = Graph(s, deps())
|
||||
Print "Topologically sorted order:"
|
||||
Print g.topoSort()
|
||||
Print
|
||||
|
||||
' Add new dependency
|
||||
For i As Integer = 33 To 11 Step -1
|
||||
deps(i) = deps(i-1)
|
||||
Next
|
||||
deps(10) = Type<Pair>(3, 6)
|
||||
|
||||
Dim As Graph g2 = Graph(s, deps())
|
||||
Print "Following the addition of dw04 to the dependencies of dw01:"
|
||||
Print g2.topoSort()
|
||||
|
||||
Sleep
|
||||
168
Task/Topological-sort/FreeBASIC/topological-sort-2.basic
Normal file
168
Task/Topological-sort/FreeBASIC/topological-sort-2.basic
Normal file
|
|
@ -0,0 +1,168 @@
|
|||
Const NULL As Any Ptr = 0
|
||||
|
||||
Type item_t
|
||||
As String nombre
|
||||
As Integer Ptr deps
|
||||
As Integer n_deps
|
||||
As Integer idx
|
||||
As Integer depth
|
||||
End Type
|
||||
|
||||
Dim Shared As String entrada
|
||||
entrada = "des_system_lib std synopsys std_cell_lib des_system_lib dw02 dw01 ramlib ieee" & Chr(10) & _
|
||||
"dw01 ieee dw01 dware gtech" & Chr(10) & _
|
||||
"dw02 ieee dw02 dware" & Chr(10) & _
|
||||
"dw03 std synopsys dware dw03 dw02 dw01 ieee gtech" & Chr(10) & _
|
||||
"dw04 dw04 ieee dw01 dware gtech" & Chr(10) & _
|
||||
"dw05 dw05 ieee dware" & Chr(10) & _
|
||||
"dw06 dw06 ieee dware" & Chr(10) & _
|
||||
"dw07 ieee dware" & Chr(10) & _
|
||||
"dware ieee dware" & Chr(10) & _
|
||||
"gtech ieee gtech" & Chr(10) & _
|
||||
"ramlib std ieee" & Chr(10) & _
|
||||
"std_cell_lib ieee std_cell_lib" & Chr(10) & _
|
||||
"synopsys" & Chr(10) & _
|
||||
"cycle_11 cycle_12" & Chr(10) & _
|
||||
"cycle_12 cycle_11" & Chr(10) & _
|
||||
"cycle_21 dw01 cycle_22 dw02 dw03" & Chr(10) & _
|
||||
"cycle_22 cycle_21 dw01 dw04" & Chr(10)
|
||||
|
||||
Function get_item(list() As item_t, Byref longi As Integer, nombre As String) As Integer
|
||||
Dim As Integer i
|
||||
For i = 0 To longi - 1
|
||||
If list(i).nombre = nombre Then Return i
|
||||
Next
|
||||
|
||||
longi += 1
|
||||
Redim Preserve list(longi - 1)
|
||||
i = longi - 1
|
||||
list(i).idx = i
|
||||
list(i).nombre = nombre
|
||||
list(i).n_deps = 0
|
||||
list(i).deps = NULL
|
||||
list(i).depth = 0
|
||||
Return i
|
||||
End Function
|
||||
|
||||
Sub add_dep(Byref it As item_t, i As Integer)
|
||||
If it.idx = i Then Return
|
||||
it.deps = Reallocate(it.deps, (it.n_deps + 1) * Sizeof(Integer))
|
||||
it.deps[it.n_deps] = i
|
||||
it.n_deps += 1
|
||||
End Sub
|
||||
|
||||
Function parse_input(ret() As item_t) As Integer
|
||||
Dim As Integer i, parent, idx, n_items, posic, nextpos
|
||||
Dim As item_t list()
|
||||
Dim As String s, linea, word
|
||||
|
||||
n_items = 0
|
||||
s = entrada
|
||||
|
||||
Do While Len(s) > 0
|
||||
posic = Instr(s, Chr(10))
|
||||
If posic = 0 Then
|
||||
linea = s
|
||||
s = ""
|
||||
Else
|
||||
linea = Left(s, posic - 1)
|
||||
s = Mid(s, posic + 1)
|
||||
End If
|
||||
|
||||
i = 0
|
||||
While Len(linea) > 0
|
||||
linea = Trim(linea)
|
||||
If Len(linea) = 0 Then Exit While
|
||||
|
||||
posic = Instr(linea, " ")
|
||||
If posic = 0 Then
|
||||
word = linea
|
||||
linea = ""
|
||||
Else
|
||||
word = Left(linea, posic - 1)
|
||||
linea = Mid(linea, posic + 1)
|
||||
End If
|
||||
|
||||
If Len(word) > 0 Then
|
||||
idx = get_item(list(), n_items, word)
|
||||
|
||||
If i = 0 Then
|
||||
parent = idx
|
||||
Else
|
||||
add_dep(list(parent), idx)
|
||||
End If
|
||||
i += 1
|
||||
End If
|
||||
Wend
|
||||
Loop
|
||||
|
||||
Redim ret(n_items - 1)
|
||||
For i = 0 To n_items - 1
|
||||
ret(i) = list(i)
|
||||
Next
|
||||
|
||||
Return n_items
|
||||
End Function
|
||||
|
||||
Function get_depth(base_() As item_t, idx As Integer, bad As Integer) As Integer
|
||||
Dim As Integer max = 1, i, t
|
||||
|
||||
If base_(idx).n_deps = 0 Then
|
||||
base_(idx).depth = 1
|
||||
Return 1
|
||||
End If
|
||||
|
||||
If base_(idx).depth < 0 Then Return base_(idx).depth
|
||||
If base_(idx).depth > 0 Then Return base_(idx).depth
|
||||
|
||||
base_(idx).depth = bad
|
||||
For i = 0 To base_(idx).n_deps - 1
|
||||
t = get_depth(base_(), base_(idx).deps[i], bad)
|
||||
If t < 0 Then
|
||||
max = t
|
||||
Exit For
|
||||
End If
|
||||
If max < t + 1 Then max = t + 1
|
||||
Next
|
||||
|
||||
base_(idx).depth = max
|
||||
Return max
|
||||
End Function
|
||||
|
||||
' Main program
|
||||
Dim As Integer i, j, n, bad = -1, max = -1000000, min = 1000000
|
||||
Dim As item_t items()
|
||||
|
||||
n = parse_input(items())
|
||||
|
||||
For i = 0 To n - 1
|
||||
If items(i).depth = 0 And get_depth(items(), i, bad) < 0 Then bad -= 1
|
||||
Next
|
||||
|
||||
For i = 0 To n - 1
|
||||
If items(i).depth > max Then max = items(i).depth
|
||||
If items(i).depth < min Then min = items(i).depth
|
||||
Next
|
||||
|
||||
Print "Compile order:"
|
||||
For i = min To max
|
||||
If i = 0 Then Continue For
|
||||
|
||||
If i < 0 Then
|
||||
Print " [unorderable]";
|
||||
Else
|
||||
Print i; ":";
|
||||
End If
|
||||
|
||||
For j = 0 To n - 1
|
||||
If items(j).depth = i Then Print " "; items(j).nombre;
|
||||
Next
|
||||
Print
|
||||
Next
|
||||
|
||||
' Clean up memory
|
||||
For i = 0 To n - 1
|
||||
If items(i).deps <> NULL Then Deallocate(items(i).deps)
|
||||
Next
|
||||
|
||||
Sleep
|
||||
Loading…
Add table
Add a link
Reference in a new issue