Data update
This commit is contained in:
parent
81fd053722
commit
52a6ef48dd
10248 changed files with 63654 additions and 6775 deletions
|
|
@ -0,0 +1,154 @@
|
|||
Type FCNode
|
||||
As String nombre
|
||||
As Integer weight
|
||||
As Double coverage
|
||||
As FCNode Ptr children(Any)
|
||||
As FCNode Ptr parent
|
||||
Declare Constructor(nombre As String, weight As Integer, coverage As Double)
|
||||
Declare Sub addChildren(nodes() As FCNode Ptr)
|
||||
Declare Sub setCoverage(value As Double)
|
||||
Declare Sub updateCoverage()
|
||||
Declare Sub show(level As Integer)
|
||||
End Type
|
||||
|
||||
Constructor FCNode(nombre As String, weight As Integer, coverage As Double)
|
||||
this.nombre = nombre
|
||||
this.weight = weight
|
||||
this.coverage = coverage
|
||||
this.parent = 0
|
||||
End Constructor
|
||||
|
||||
Sub FCNode.addChildren(nodes() As FCNode Ptr)
|
||||
For i As Integer = 0 To Ubound(nodes)
|
||||
nodes(i)->parent = @This
|
||||
Redim Preserve this.children(Ubound(this.children) + 1)
|
||||
this.children(Ubound(this.children)) = nodes(i)
|
||||
Next
|
||||
this.updateCoverage()
|
||||
End Sub
|
||||
|
||||
Sub FCNode.setCoverage(value As Double)
|
||||
If this.coverage <> value Then
|
||||
this.coverage = value
|
||||
If this.parent <> 0 Then this.parent->updateCoverage()
|
||||
End If
|
||||
End Sub
|
||||
|
||||
Sub FCNode.updateCoverage()
|
||||
Dim As Double v1 = 0
|
||||
Dim As Integer v2 = 0
|
||||
For i As Integer = 0 To Ubound(this.children)
|
||||
v1 += this.children(i)->weight * this.children(i)->coverage
|
||||
v2 += this.children(i)->weight
|
||||
Next
|
||||
If v2 <> 0 Then this.setCoverage(v1 / v2)
|
||||
End Sub
|
||||
|
||||
Sub FCNode.show(level As Integer)
|
||||
Dim As Integer indent, nl, i
|
||||
|
||||
indent = level * 4
|
||||
nl = Len(this.nombre) + indent
|
||||
Print Space(indent); this.nombre; Space(32 - nl); "|"; Using "#####"; this.weight;
|
||||
Print Using " | #.###### |"; this.coverage
|
||||
If Ubound(this.children) = -1 Then Return
|
||||
For i = 0 To Ubound(this.children)
|
||||
this.children(i)->show(level + 1)
|
||||
Next
|
||||
End Sub
|
||||
|
||||
Function newFCN(nombre As String, weight As Integer, coverage As Double) As FCNode Ptr
|
||||
Return New FCNode(nombre, weight, coverage)
|
||||
End Function
|
||||
|
||||
Dim houses(1) As FCNode Ptr
|
||||
houses(0) = newFCN("house1", 40, 0)
|
||||
houses(1) = newFCN("house2", 60, 0)
|
||||
|
||||
Dim house1(7) As FCNode Ptr
|
||||
house1(0) = newFCN("bedrooms", 1, 0.25)
|
||||
house1(1) = newFCN("bathrooms", 1, 0)
|
||||
house1(2) = newFCN("attic", 1, 0.75)
|
||||
house1(3) = newFCN("kitchen", 1, 0.1)
|
||||
house1(4) = newFCN("living_rooms", 1, 0)
|
||||
house1(5) = newFCN("basement", 1, 0)
|
||||
house1(6) = newFCN("garage", 1, 0)
|
||||
house1(7) = newFCN("garden", 1, 0.8)
|
||||
|
||||
Dim house2(2) As FCNode Ptr
|
||||
house2(0) = newFCN("upstairs", 1, 0)
|
||||
house2(1) = newFCN("groundfloor", 1, 0)
|
||||
house2(2) = newFCN("basement", 1, 0)
|
||||
|
||||
Dim h1Bathrooms(2) As FCNode Ptr
|
||||
h1Bathrooms(0) = newFCN("bathroom1", 1, 0.5)
|
||||
h1Bathrooms(1) = newFCN("bathroom2", 1, 0)
|
||||
h1Bathrooms(2) = newFCN("outside_lavatory", 1, 1)
|
||||
|
||||
Dim h1LivingRooms(3) As FCNode Ptr
|
||||
h1LivingRooms(0) = newFCN("lounge", 1, 0)
|
||||
h1LivingRooms(1) = newFCN("dining_room", 1, 0)
|
||||
h1LivingRooms(2) = newFCN("conservatory", 1, 0)
|
||||
h1LivingRooms(3) = newFCN("playroom", 1, 1)
|
||||
|
||||
Dim h2Upstairs(3) As FCNode Ptr
|
||||
h2Upstairs(0) = newFCN("bedrooms", 1, 0)
|
||||
h2Upstairs(1) = newFCN("bathroom", 1, 0)
|
||||
h2Upstairs(2) = newFCN("toilet", 1, 0)
|
||||
h2Upstairs(3) = newFCN("attics", 1, 0.6)
|
||||
|
||||
Dim h2Groundfloor(5) As FCNode Ptr
|
||||
h2Groundfloor(0) = newFCN("kitchen", 1, 0)
|
||||
h2Groundfloor(1) = newFCN("living_rooms", 1, 0)
|
||||
h2Groundfloor(2) = newFCN("wet_room_&_toilet", 1, 0)
|
||||
h2Groundfloor(3) = newFCN("garage", 1, 0)
|
||||
h2Groundfloor(4) = newFCN("garden", 1, 0.9)
|
||||
h2Groundfloor(5) = newFCN("hot_tub_suite", 1, 1)
|
||||
|
||||
Dim h2Basement(2) As FCNode Ptr
|
||||
h2Basement(0) = newFCN("cellars", 1, 1)
|
||||
h2Basement(1) = newFCN("wine_cellar", 1, 1)
|
||||
h2Basement(2) = newFCN("cinema", 1, 0.75)
|
||||
|
||||
Dim h2UpstairsBedrooms(3) As FCNode Ptr
|
||||
h2UpstairsBedrooms(0) = newFCN("suite_1", 1, 0)
|
||||
h2UpstairsBedrooms(1) = newFCN("suite_2", 1, 0)
|
||||
h2UpstairsBedrooms(2) = newFCN("bedroom_3", 1, 0)
|
||||
h2UpstairsBedrooms(3) = newFCN("bedroom_4", 1, 0)
|
||||
|
||||
Dim h2GroundfloorLivingRooms(3) As FCNode Ptr
|
||||
h2GroundfloorLivingRooms(0) = newFCN("lounge", 1, 0)
|
||||
h2GroundfloorLivingRooms(1) = newFCN("dining_room", 1, 0)
|
||||
h2GroundfloorLivingRooms(2) = newFCN("conservatory", 1, 0)
|
||||
h2GroundfloorLivingRooms(3) = newFCN("playroom", 1, 0)
|
||||
|
||||
Dim cleaning As FCNode Ptr = newFCN("cleaning", 1, 0)
|
||||
|
||||
house1(1)->addChildren(h1Bathrooms())
|
||||
house1(4)->addChildren(h1LivingRooms())
|
||||
houses(0)->addChildren(house1())
|
||||
|
||||
h2Upstairs(0)->addChildren(h2UpstairsBedrooms())
|
||||
house2(0)->addChildren(h2Upstairs())
|
||||
h2Groundfloor(1)->addChildren(h2GroundfloorLivingRooms())
|
||||
house2(1)->addChildren(h2Groundfloor())
|
||||
house2(2)->addChildren(h2Basement())
|
||||
houses(1)->addChildren(house2())
|
||||
|
||||
cleaning->addChildren(houses())
|
||||
Dim As Double topCoverage = cleaning->coverage
|
||||
Print "TOP COVERAGE = ";
|
||||
Print Using "########.######"; topCoverage
|
||||
Print
|
||||
Print "NAME HIERARCHY | WEIGHT | COVERAGE |"
|
||||
cleaning->show(0)
|
||||
|
||||
h2Basement(2)->setCoverage(1) ' change Cinema node coverage to 1
|
||||
Dim As Double diff = cleaning->coverage - topCoverage
|
||||
Print
|
||||
Print "If the coverage of the Cinema node were increased from 0.75 to 1"
|
||||
Print "the top level coverage would increase by ";
|
||||
Print Using "##.###### to ##.######"; diff; (topCoverage + diff)
|
||||
h2Basement(2)->setCoverage(0.75) ' restore to original value if required
|
||||
|
||||
Sleep
|
||||
|
|
@ -1,113 +1,108 @@
|
|||
(phixonline)-->
|
||||
<span style="color: #000080;font-style:italic;">--
|
||||
-- demo\rosetta\Functional_coverage_tree.exw
|
||||
-- =========================================
|
||||
--</span>
|
||||
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
|
||||
<span style="color: #008080;">constant</span> <span style="color: #000000;">data</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"""
|
||||
NAME_HIERARCHY | WEIGHT | COVERAGE |
|
||||
cleaning | | |
|
||||
house1 |40 | |
|
||||
bedrooms | |0.25 |
|
||||
bathrooms | | |
|
||||
bathroom1 | |0.5 |
|
||||
bathroom2 | | |
|
||||
outside_lavatory | |1 |
|
||||
attic | |0.75 |
|
||||
kitchen | |0.1 |
|
||||
living_rooms | | |
|
||||
lounge | | |
|
||||
dining_room | | |
|
||||
conservatory | | |
|
||||
playroom | |1 |
|
||||
basement | | |
|
||||
garage | | |
|
||||
garden | |0.8 |
|
||||
house2 |60 | |
|
||||
upstairs | | |
|
||||
bedrooms | | |
|
||||
suite_1 | | |
|
||||
suite_2 | | |
|
||||
bedroom_3 | | |
|
||||
bedroom_4 | | |
|
||||
bathroom | | |
|
||||
toilet | | |
|
||||
attics | |0.6 |
|
||||
groundfloor | | |
|
||||
kitchen | | |
|
||||
living_rooms | | |
|
||||
lounge | | |
|
||||
dining_room | | |
|
||||
conservatory | | |
|
||||
playroom | | |
|
||||
wet_room_&_toilet | | |
|
||||
garage | | |
|
||||
garden | |0.9 |
|
||||
hot_tub_suite | |1 |
|
||||
basement | | |
|
||||
cellars | |1 |
|
||||
wine_cellar | |1 |
|
||||
cinema | |0.75 |
|
||||
"""</span>
|
||||
<span style="color: #004080;">sequence</span> <span style="color: #000000;">lines</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">split</span><span style="color: #0000FF;">(</span><span style="color: #000000;">data</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"\n"</span><span style="color: #0000FF;">),</span>
|
||||
<span style="color: #000000;">pi</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{},</span> <span style="color: #000080;font-style:italic;">-- indents (to locate parents)</span>
|
||||
<span style="color: #000000;">pdx</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{},</span> <span style="color: #000080;font-style:italic;">-- indexes for ""</span>
|
||||
<span style="color: #000000;">children</span>
|
||||
<span style="color: #004080;">string</span> <span style="color: #000000;">desc</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">weights</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">covers</span>
|
||||
<span style="color: #004080;">integer</span> <span style="color: #000000;">parent</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">child</span>
|
||||
<span style="color: #004080;">atom</span> <span style="color: #000000;">weight</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">coverage</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">childw</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span>
|
||||
<span style="color: #008080;">enum</span> <span style="color: #000000;">DESC</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">WEIGHT</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">COVERAGE</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">PARENT</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">CHILDREN</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">CHILDW</span>
|
||||
<span style="color: #000000;">lines</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">&=</span> <span style="color: #008000;">" SHARE OF RESIDUE"</span>
|
||||
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">2</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">lines</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
|
||||
<span style="color: #000080;font-style:italic;">-- decode text to useable data, link up parents & children</span>
|
||||
<span style="color: #0000FF;">{</span><span style="color: #000000;">desc</span><span style="color: #0000FF;">,</span><span style="color: #000000;">weights</span><span style="color: #0000FF;">,</span><span style="color: #000000;">covers</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">split</span><span style="color: #0000FF;">(</span><span style="color: #000000;">lines</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">],</span><span style="color: #008000;">"|"</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #000080;font-style:italic;">-- (nb this assumes /totally/ consistent indenting)</span>
|
||||
<span style="color: #004080;">integer</span> <span style="color: #000000;">indent</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">desc</span><span style="color: #0000FF;">)-</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">trim_head</span><span style="color: #0000FF;">(</span><span style="color: #000000;">desc</span><span style="color: #0000FF;">)),</span>
|
||||
<span style="color: #000000;">k</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">find</span><span style="color: #0000FF;">(</span><span style="color: #000000;">indent</span><span style="color: #0000FF;">,</span><span style="color: #000000;">pi</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #008080;">if</span> <span style="color: #000000;">k</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span>
|
||||
<span style="color: #000000;">pi</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">append</span><span style="color: #0000FF;">(</span><span style="color: #000000;">pi</span><span style="color: #0000FF;">,</span><span style="color: #000000;">indent</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #000000;">pdx</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">append</span><span style="color: #0000FF;">(</span><span style="color: #000000;">pdx</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #000000;">k</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">pi</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
|
||||
<span style="color: #000000;">pdx</span><span style="color: #0000FF;">[</span><span style="color: #000000;">k</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">i</span>
|
||||
<span style="color: #000000;">parent</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span>
|
||||
<span style="color: #008080;">if</span> <span style="color: #000000;">k</span><span style="color: #0000FF;">></span><span style="color: #000000;">1</span> <span style="color: #008080;">then</span>
|
||||
<span style="color: #000000;">parent</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">pdx</span><span style="color: #0000FF;">[</span><span style="color: #000000;">k</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span>
|
||||
<span style="color: #000080;font-style:italic;">-- lines[parent][CHILDREN] &= i</span>
|
||||
<span style="color: #000000;">lines</span><span style="color: #0000FF;">[</span><span style="color: #000000;">parent</span><span style="color: #0000FF;">][</span><span style="color: #000000;">CHILDREN</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">deep_copy</span><span style="color: #0000FF;">(</span><span style="color: #000000;">lines</span><span style="color: #0000FF;">[</span><span style="color: #000000;">parent</span><span style="color: #0000FF;">][</span><span style="color: #000000;">CHILDREN</span><span style="color: #0000FF;">])</span> <span style="color: #0000FF;">&</span> <span style="color: #000000;">i</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
|
||||
<span style="color: #000000;">children</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{}</span>
|
||||
<span style="color: #000000;">weight</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">to_number</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">trim</span><span style="color: #0000FF;">(</span><span style="color: #000000;">weights</span><span style="color: #0000FF;">),</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #000000;">coverage</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">to_number</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">trim</span><span style="color: #0000FF;">(</span><span style="color: #000000;">covers</span><span style="color: #0000FF;">),</span><span style="color: #000000;">0</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #000000;">lines</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">desc</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">weight</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">coverage</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">parent</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">children</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">childw</span><span style="color: #0000FF;">}</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
|
||||
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">lines</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">to</span> <span style="color: #000000;">2</span> <span style="color: #008080;">by</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">1</span> <span style="color: #008080;">do</span>
|
||||
<span style="color: #000080;font-style:italic;">-- calculate the parent coverages, and save child weight sums</span>
|
||||
<span style="color: #000000;">children</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">lines</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">][</span><span style="color: #000000;">CHILDREN</span><span style="color: #0000FF;">]</span>
|
||||
<span style="color: #008080;">if</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">children</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span>
|
||||
<span style="color: #000000;">coverage</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span>
|
||||
<span style="color: #000000;">childw</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span>
|
||||
<span style="color: #008080;">for</span> <span style="color: #000000;">c</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">children</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
|
||||
<span style="color: #000000;">child</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">children</span><span style="color: #0000FF;">[</span><span style="color: #000000;">c</span><span style="color: #0000FF;">]</span>
|
||||
<span style="color: #004080;">atom</span> <span style="color: #000000;">w</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">lines</span><span style="color: #0000FF;">[</span><span style="color: #000000;">child</span><span style="color: #0000FF;">][</span><span style="color: #000000;">WEIGHT</span><span style="color: #0000FF;">]</span>
|
||||
<span style="color: #000000;">coverage</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">lines</span><span style="color: #0000FF;">[</span><span style="color: #000000;">child</span><span style="color: #0000FF;">][</span><span style="color: #000000;">COVERAGE</span><span style="color: #0000FF;">]*</span><span style="color: #000000;">w</span>
|
||||
<span style="color: #000000;">childw</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">w</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
|
||||
<span style="color: #000000;">lines</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">][</span><span style="color: #000000;">COVERAGE</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">coverage</span><span style="color: #0000FF;">/</span><span style="color: #000000;">childw</span>
|
||||
<span style="color: #000000;">lines</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">][</span><span style="color: #000000;">CHILDW</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">childw</span> <span style="color: #000080;font-style:italic;">-- (save for next loop)</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
|
||||
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">lines</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">to</span> <span style="color: #000000;">2</span> <span style="color: #008080;">by</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">1</span> <span style="color: #008080;">do</span>
|
||||
<span style="color: #000080;font-style:italic;">-- calculate the share of residue, and format lines</span>
|
||||
<span style="color: #000000;">child</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">i</span>
|
||||
<span style="color: #0000FF;">{</span><span style="color: #000000;">desc</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">weight</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">coverage</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">parent</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">lines</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span>
|
||||
<span style="color: #004080;">atom</span> <span style="color: #000000;">residue</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span><span style="color: #0000FF;">-</span><span style="color: #000000;">coverage</span>
|
||||
<span style="color: #008080;">while</span> <span style="color: #000000;">parent</span> <span style="color: #008080;">do</span>
|
||||
<span style="color: #000000;">residue</span> <span style="color: #0000FF;">*=</span> <span style="color: #000000;">lines</span><span style="color: #0000FF;">[</span><span style="color: #000000;">child</span><span style="color: #0000FF;">][</span><span style="color: #000000;">WEIGHT</span><span style="color: #0000FF;">]/</span><span style="color: #000000;">lines</span><span style="color: #0000FF;">[</span><span style="color: #000000;">parent</span><span style="color: #0000FF;">][</span><span style="color: #000000;">CHILDW</span><span style="color: #0000FF;">]</span>
|
||||
<span style="color: #0000FF;">{</span><span style="color: #000000;">child</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">parent</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">parent</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">lines</span><span style="color: #0000FF;">[</span><span style="color: #000000;">parent</span><span style="color: #0000FF;">][</span><span style="color: #000000;">PARENT</span><span style="color: #0000FF;">]}</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
|
||||
<span style="color: #000000;">lines</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">sprintf</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"%-32s| %6d | %-8g | %g"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">desc</span><span style="color: #0000FF;">,</span><span style="color: #000000;">weight</span><span style="color: #0000FF;">,</span><span style="color: #000000;">coverage</span><span style="color: #0000FF;">,</span><span style="color: #000000;">residue</span><span style="color: #0000FF;">})</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
|
||||
<span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">join</span><span style="color: #0000FF;">(</span><span style="color: #000000;">lines</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"\n"</span><span style="color: #0000FF;">)&</span><span style="color: #008000;">"\n"</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #0000FF;">{}</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">wait_key</span><span style="color: #0000FF;">()</span>
|
||||
<!--
|
||||
-- demo\rosetta\Functional_coverage_tree.exw
|
||||
with javascript_semantics
|
||||
constant data = """
|
||||
NAME_HIERARCHY | WEIGHT | COVERAGE |
|
||||
cleaning | | |
|
||||
house1 |40 | |
|
||||
bedrooms | |0.25 |
|
||||
bathrooms | | |
|
||||
bathroom1 | |0.5 |
|
||||
bathroom2 | | |
|
||||
outside_lavatory | |1 |
|
||||
attic | |0.75 |
|
||||
kitchen | |0.1 |
|
||||
living_rooms | | |
|
||||
lounge | | |
|
||||
dining_room | | |
|
||||
conservatory | | |
|
||||
playroom | |1 |
|
||||
basement | | |
|
||||
garage | | |
|
||||
garden | |0.8 |
|
||||
house2 |60 | |
|
||||
upstairs | | |
|
||||
bedrooms | | |
|
||||
suite_1 | | |
|
||||
suite_2 | | |
|
||||
bedroom_3 | | |
|
||||
bedroom_4 | | |
|
||||
bathroom | | |
|
||||
toilet | | |
|
||||
attics | |0.6 |
|
||||
groundfloor | | |
|
||||
kitchen | | |
|
||||
living_rooms | | |
|
||||
lounge | | |
|
||||
dining_room | | |
|
||||
conservatory | | |
|
||||
playroom | | |
|
||||
wet_room_&_toilet | | |
|
||||
garage | | |
|
||||
garden | |0.9 |
|
||||
hot_tub_suite | |1 |
|
||||
basement | | |
|
||||
cellars | |1 |
|
||||
wine_cellar | |1 |
|
||||
cinema | |0.75 |
|
||||
"""
|
||||
sequence lines = split(data,"\n"),
|
||||
pi = {}, -- indents (to locate parents)
|
||||
pdx = {}, -- indexes for ""
|
||||
children
|
||||
string desc, weights, covers
|
||||
integer parent, child
|
||||
atom weight, coverage, childw = 0
|
||||
enum DESC, WEIGHT, COVERAGE, PARENT, CHILDREN, CHILDW
|
||||
lines[DESC] &= " SHARE OF RESIDUE"
|
||||
for i=2 to length(lines) do
|
||||
-- decode text to useable data, link up parents & children
|
||||
{desc,weights,covers} = split(lines[i],"|")
|
||||
-- (nb this assumes /totally/ consistent indenting)
|
||||
integer indent = length(desc)-length(trim_head(desc)),
|
||||
k = find(indent,pi)
|
||||
if k=0 then
|
||||
pi = append(pi,indent)
|
||||
pdx = append(pdx,0)
|
||||
k = length(pi)
|
||||
end if
|
||||
pdx[k] = i
|
||||
parent = 0
|
||||
if k>1 then
|
||||
parent = pdx[k-1]
|
||||
-- lines[parent][CHILDREN] &= i
|
||||
lines[parent][CHILDREN] = deep_copy(lines[parent][CHILDREN]) & i
|
||||
end if
|
||||
children = {}
|
||||
weight = to_number(trim(weights),1)
|
||||
coverage = to_number(trim(covers),0)
|
||||
lines[i] = {desc, weight, coverage, parent, children, childw}
|
||||
end for
|
||||
for i=length(lines) to 2 by -1 do
|
||||
-- calculate the parent coverages, and save child weight sums
|
||||
children = lines[i][CHILDREN]
|
||||
if length(children) then
|
||||
coverage = 0
|
||||
childw = 0
|
||||
for c=1 to length(children) do
|
||||
child = children[c]
|
||||
atom w = lines[child][WEIGHT]
|
||||
coverage += lines[child][COVERAGE]*w
|
||||
childw += w
|
||||
end for
|
||||
lines[i][COVERAGE] = coverage/childw
|
||||
lines[i][CHILDW] = childw -- (save for next loop)
|
||||
end if
|
||||
end for
|
||||
for i=length(lines) to 2 by -1 do
|
||||
-- calculate the share of residue, and format lines
|
||||
child = i
|
||||
{desc, weight, coverage, parent} = lines[i]
|
||||
atom residue = 1-coverage
|
||||
while parent do
|
||||
residue *= lines[child][WEIGHT]/lines[parent][CHILDW]
|
||||
{child, parent} = {parent, lines[parent][PARENT]}
|
||||
end while
|
||||
lines[i] = sprintf("%-32s| %6d | %-8g | %g",{desc,weight,coverage,residue})
|
||||
end for
|
||||
puts(1,join(lines,"\n")&"\n")
|
||||
{} = wait_key()
|
||||
|
|
|
|||
178
Task/Functional-coverage-tree/Rust/functional-coverage-tree.rs
Normal file
178
Task/Functional-coverage-tree/Rust/functional-coverage-tree.rs
Normal file
|
|
@ -0,0 +1,178 @@
|
|||
#[derive(Clone)]
|
||||
struct FCNode {
|
||||
index: usize,
|
||||
name: String,
|
||||
weight: i32,
|
||||
coverage: f64,
|
||||
children: Vec<usize>,
|
||||
parent: Option<usize>,
|
||||
}
|
||||
impl FCNode {
|
||||
fn new() -> Self {
|
||||
Self {
|
||||
index: 0,
|
||||
name: "".to_string(),
|
||||
weight: 1,
|
||||
coverage: 0.0,
|
||||
children: vec![],
|
||||
parent: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn update_coverage(nodes: &mut Vec<FCNode>, node: usize) {
|
||||
if !nodes[node].children.is_empty() {
|
||||
let num = nodes[node]
|
||||
.children
|
||||
.iter()
|
||||
.fold(0.0, |sum, child| sum + nodes[*child].weight as f64 * nodes[*child].coverage);
|
||||
let denom = nodes[node]
|
||||
.children
|
||||
.iter()
|
||||
.fold(0.0, |sum, child| sum + nodes[*child].weight as f64);
|
||||
nodes[node].coverage = num / denom;
|
||||
}
|
||||
match nodes[node].parent {
|
||||
Some(idx) => update_coverage(nodes, idx),
|
||||
None => (),
|
||||
}
|
||||
}
|
||||
|
||||
fn add_children(nodes: &mut Vec<FCNode>, parent: usize, children: &mut Vec<usize>) {
|
||||
for child in children {
|
||||
nodes[parent].children.push(*child);
|
||||
nodes[*child].parent = Some(parent);
|
||||
}
|
||||
update_coverage(nodes, parent);
|
||||
}
|
||||
|
||||
fn show(nodes: &Vec<FCNode>, node: usize, level: i32) {
|
||||
let indent = level * 4;
|
||||
let nl = nodes[node].name.len() + indent as usize;
|
||||
print!("{:>nl$}", nodes[node].name);
|
||||
print!("{1:>0$}", 32 - nl, "|");
|
||||
print!(" {:<3} |", nodes[node].weight);
|
||||
println!(" {:<8.6} |", nodes[node].coverage);
|
||||
if !nodes[node].children.is_empty() {
|
||||
for child in nodes[node].children.iter() {
|
||||
show(nodes, *child, level + 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn node1(nodes: &mut Vec<FCNode>, name: &str) -> usize {
|
||||
let mut new_node = FCNode::new();
|
||||
let idx = nodes.len();
|
||||
new_node.index = idx;
|
||||
new_node.name = name.to_owned();
|
||||
nodes.push(new_node);
|
||||
return idx;
|
||||
}
|
||||
|
||||
fn node2(nodes: &mut Vec<FCNode>, name: &str, weight: i32) -> usize {
|
||||
let mut new_node = FCNode::new();
|
||||
let idx = nodes.len();
|
||||
new_node.index = idx;
|
||||
new_node.name = name.to_owned();
|
||||
new_node.weight = weight;
|
||||
nodes.push(new_node);
|
||||
return idx;
|
||||
}
|
||||
fn node3(nodes: &mut Vec<FCNode>, name: &str, weight: i32, coverage: f64) -> usize {
|
||||
let mut new_node = FCNode::new();
|
||||
let idx = nodes.len();
|
||||
new_node.index = idx;
|
||||
new_node.name = name.to_owned();
|
||||
new_node.weight = weight;
|
||||
new_node.coverage = coverage;
|
||||
nodes.push(new_node);
|
||||
return idx;
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let mut allnodes = vec![FCNode::new(); 1];
|
||||
let a = &mut allnodes;
|
||||
let mut houses = vec![node2(a, "house1", 40), node2(a, "house2", 60)];
|
||||
let mut house1 = vec![
|
||||
node3(a, "bedrooms", 1, 0.25),
|
||||
node1(a, "bathrooms"),
|
||||
node3(a, "attic", 1, 0.75),
|
||||
node3(a, "kitchen", 1, 0.1),
|
||||
node1(a, "living_rooms"),
|
||||
node1(a, "basement"),
|
||||
node1(a, "garage"),
|
||||
node3(a, "garden", 1, 0.8),
|
||||
];
|
||||
let mut house2 = vec![
|
||||
node1(a, "upstairs"),
|
||||
node1(a, "groundfloor"),
|
||||
node1(a, "basement"),
|
||||
];
|
||||
let mut h1_bathrooms = vec![
|
||||
node3(a, "bathroom1", 1, 0.5),
|
||||
node1(a, "bathroom2"),
|
||||
node3(a, "outside_lavatory", 1, 1.0),
|
||||
];
|
||||
let mut h1_living_rooms = vec![
|
||||
node1(a, "lounge"),
|
||||
node1(a, "dining_room"),
|
||||
node1(a, "conservatory"),
|
||||
node3(a, "playroom", 1, 1.0),
|
||||
];
|
||||
let mut h2_upstairs = vec![
|
||||
node1(a, "bedrooms"),
|
||||
node1(a, "bathroom"),
|
||||
node1(a, "toilet"),
|
||||
node3(a, "attics", 1, 0.6),
|
||||
];
|
||||
let mut h2_ground_floor = vec![
|
||||
node1(a, "kitchen"),
|
||||
node1(a, "living_rooms"),
|
||||
node1(a, "wet_room_&_toilet"),
|
||||
node1(a, "garage"),
|
||||
node3(a, "garden", 1, 0.9),
|
||||
node3(a, "hot_tub_suite", 1, 1.0),
|
||||
];
|
||||
let mut h2_basement = vec![
|
||||
node3(a, "cellars", 1, 1.0),
|
||||
node3(a, "wine_cellar", 1, 1.0),
|
||||
node3(a, "cinema", 1, 0.75),
|
||||
];
|
||||
let mut h2_upstairs_bedrooms = vec![
|
||||
node1(a, "suite_1"),
|
||||
node1(a, "suite_2"),
|
||||
node1(a, "bedroom_3"),
|
||||
node1(a, "bedroom_4"),
|
||||
];
|
||||
let mut h2_ground_floor_living_rooms = vec![
|
||||
node1(a, "lounge"),
|
||||
node1(a, "dining_room"),
|
||||
node1(a, "conservatory"),
|
||||
node1(a, "playroom"),
|
||||
];
|
||||
let cleaning = node1(a, "cleaning");
|
||||
|
||||
add_children(a, house1[1], &mut h1_bathrooms);
|
||||
add_children(a, house1[4], &mut h1_living_rooms);
|
||||
add_children(a, houses[0], &mut house1);
|
||||
|
||||
add_children(a, h2_upstairs[0], &mut h2_upstairs_bedrooms);
|
||||
add_children(a, house2[0], &mut h2_upstairs);
|
||||
add_children(a, h2_ground_floor[1], &mut h2_ground_floor_living_rooms);
|
||||
add_children(a, house2[1], &mut h2_ground_floor);
|
||||
add_children(a, house2[2], &mut h2_basement);
|
||||
add_children(a, houses[1], &mut house2);
|
||||
|
||||
add_children(a, cleaning, &mut houses);
|
||||
let top_coverage = a[cleaning].coverage;
|
||||
println!("TOP COVERAGE = {top_coverage:8.6}\n");
|
||||
println!("NAME HIERARCHY | WEIGHT | COVERAGE |");
|
||||
show(a, cleaning, 0);
|
||||
a[h2_basement[2]].coverage = 1.0; // change Cinema node coverage to 1.0
|
||||
update_coverage(a, h2_basement[2]);
|
||||
let diff = a[cleaning].coverage - top_coverage;
|
||||
println!("\nIf the coverage of the Cinema node were increased from 0.75 to 1.0");
|
||||
print!("the top level coverage would increase by ");
|
||||
println!("{:8.6} to {:8.6}", diff, top_coverage + diff);
|
||||
a[h2_basement[2]].coverage = 0.75; // restore to original value if required
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue