Data update
This commit is contained in:
parent
4bb20c9b71
commit
cbaf4c4b64
12390 changed files with 318560 additions and 27248 deletions
|
|
@ -0,0 +1,14 @@
|
|||
generic
|
||||
type Element_Type is private;
|
||||
package Container is
|
||||
type Tree is tagged private;
|
||||
procedure Replace_All(The_Tree : in out Tree; New_Value : Element_Type);
|
||||
private
|
||||
type Node;
|
||||
type Node_Access is access Node;
|
||||
type Tree tagged record
|
||||
Value : Element_type;
|
||||
Left : Node_Access := null;
|
||||
Right : Node_Access := null;
|
||||
end record;
|
||||
end Container;
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
package body Container is
|
||||
procedure Replace_All(The_Tree : in out Tree; New_Value : Element_Type) is
|
||||
begin
|
||||
The_Tree.Value := New_Value;
|
||||
If The_Tree.Left /= null then
|
||||
The_Tree.Left.all.Replace_All(New_Value);
|
||||
end if;
|
||||
if The_tree.Right /= null then
|
||||
The_Tree.Right.all.Replace_All(New_Value);
|
||||
end if;
|
||||
end Replace_All;
|
||||
end Container;
|
||||
|
|
@ -0,0 +1,34 @@
|
|||
>>SOURCE FORMAT IS FREE
|
||||
IDENTIFICATION DIVISION.
|
||||
CLASS-ID. Tree INHERITS FROM Base USING X.
|
||||
DATE-WRITTEN. 20240702.
|
||||
ENVIRONMENT DIVISION.
|
||||
CONFIGURATION SECTION.
|
||||
OBJECT-COMPUTER.
|
||||
MEMORY SIZE IS 1073741824 CHARACTERS.
|
||||
REPOSITORY.
|
||||
CLASS Base.
|
||||
CLASS X.
|
||||
IDENTIFICATION DIVISION.
|
||||
OBJECT.
|
||||
DATA DIVISION.
|
||||
WORKING-STORAGE SECTION.
|
||||
77 ws-value USAGE IS OBJECT REFERENCE X ONLY.
|
||||
77 ws-left USAGE IS OBJECT REFERENCE Tree ONLY.
|
||||
77 ws-right USAGE IS OBJECT REFERENCE Tree ONLY.
|
||||
PROCEDURE DIVISION.
|
||||
IDENTIFICATION DIVISION.
|
||||
METHOD-ID. replace_all.
|
||||
DATA DIVISION.
|
||||
LINKAGE SECTION.
|
||||
77 new_value USAGE IS OBJECT REFERENCE X ONLY.
|
||||
PROCEDURE DIVISION USING BY REFERENCE new_value.
|
||||
MOVE new_value TO ws-value.
|
||||
IF ws-left IS NOT EQUAL TO NULL THEN
|
||||
INVOKE ws-left "replace_all" USING BY REFERENCE new_value.
|
||||
IF ws-right IS NOT EQUAL TO NULL THEN
|
||||
INVOKE ws-right "replace_all" USING BY REFERENCE new_value.
|
||||
GOBACK.
|
||||
END METHOD replace_all.
|
||||
END OBJECT.
|
||||
END CLASS Tree.
|
||||
|
|
@ -1,3 +1 @@
|
|||
(phixonline)-->
|
||||
<span style="color: #0000FF;">?</span><span style="color: #7060A8;">sort</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">shuffle</span><span style="color: #0000FF;">({</span><span style="color: #000000;">5</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"oranges"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">6</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"apples"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">7</span><span style="color: #0000FF;">}))</span>
|
||||
<!--
|
||||
?sort(shuffle({5,"oranges",6,"apples",7}))
|
||||
|
|
|
|||
|
|
@ -1,45 +1,43 @@
|
|||
(phixonline)-->
|
||||
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
|
||||
<span style="color: #008080;">enum</span> <span style="color: #000000;">data</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">left</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">right</span>
|
||||
with javascript_semantics
|
||||
enum data, left, right
|
||||
|
||||
<span style="color: #008080;">function</span> <span style="color: #000000;">tmap</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">tree</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">integer</span> <span style="color: #000000;">rid</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #000000;">tree</span><span style="color: #0000FF;">[</span><span style="color: #000000;">data</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">rid</span><span style="color: #0000FF;">(</span><span style="color: #000000;">tree</span><span style="color: #0000FF;">[</span><span style="color: #000000;">data</span><span style="color: #0000FF;">])</span>
|
||||
<span style="color: #008080;">if</span> <span style="color: #000000;">tree</span><span style="color: #0000FF;">[</span><span style="color: #000000;">left</span><span style="color: #0000FF;">]!=</span><span style="color: #004600;">null</span> <span style="color: #008080;">then</span> <span style="color: #000000;">tree</span><span style="color: #0000FF;">[</span><span style="color: #000000;">left</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">tmap</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">deep_copy</span><span style="color: #0000FF;">(</span><span style="color: #000000;">tree</span><span style="color: #0000FF;">[</span><span style="color: #000000;">left</span><span style="color: #0000FF;">],</span><span style="color: #000000;">1</span><span style="color: #0000FF;">),</span><span style="color: #000000;">rid</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
|
||||
<span style="color: #008080;">if</span> <span style="color: #000000;">tree</span><span style="color: #0000FF;">[</span><span style="color: #000000;">right</span><span style="color: #0000FF;">]!=</span><span style="color: #004600;">null</span> <span style="color: #008080;">then</span> <span style="color: #000000;">tree</span><span style="color: #0000FF;">[</span><span style="color: #000000;">right</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">tmap</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">deep_copy</span><span style="color: #0000FF;">(</span><span style="color: #000000;">tree</span><span style="color: #0000FF;">[</span><span style="color: #000000;">right</span><span style="color: #0000FF;">],</span><span style="color: #000000;">1</span><span style="color: #0000FF;">),</span><span style="color: #000000;">rid</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
|
||||
<span style="color: #008080;">return</span> <span style="color: #000000;">tree</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
|
||||
function tmap(sequence tree, integer rid)
|
||||
tree[data] = rid(tree[data])
|
||||
if tree[left]!=null then tree[left] = tmap(deep_copy(tree[left],1),rid) end if
|
||||
if tree[right]!=null then tree[right] = tmap(deep_copy(tree[right],1),rid) end if
|
||||
return tree
|
||||
end function
|
||||
|
||||
<span style="color: #008080;">function</span> <span style="color: #000000;">newnode</span><span style="color: #0000FF;">(</span><span style="color: #004080;">object</span> <span style="color: #000000;">v</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #008080;">return</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">v</span><span style="color: #0000FF;">,</span><span style="color: #004600;">null</span><span style="color: #0000FF;">,</span><span style="color: #004600;">null</span><span style="color: #0000FF;">}</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
|
||||
function newnode(object v)
|
||||
return {v,null,null}
|
||||
end function
|
||||
|
||||
<span style="color: #008080;">function</span> <span style="color: #000000;">add10</span><span style="color: #0000FF;">(</span><span style="color: #004080;">atom</span> <span style="color: #000000;">x</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">return</span> <span style="color: #000000;">x</span><span style="color: #0000FF;">+</span><span style="color: #000000;">10</span> <span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
|
||||
function add10(atom x) return x+10 end function
|
||||
|
||||
<span style="color: #008080;">procedure</span> <span style="color: #000000;">main</span><span style="color: #0000FF;">()</span>
|
||||
<span style="color: #004080;">object</span> <span style="color: #000000;">root</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">newnode</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1.00</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #000080;font-style:italic;">-- Add some nodes.</span>
|
||||
<span style="color: #000000;">root</span><span style="color: #0000FF;">[</span><span style="color: #000000;">left</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">newnode</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1.10</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #000000;">root</span><span style="color: #0000FF;">[</span><span style="color: #000000;">left</span><span style="color: #0000FF;">][</span><span style="color: #000000;">left</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">newnode</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1.11</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #000000;">root</span><span style="color: #0000FF;">[</span><span style="color: #000000;">left</span><span style="color: #0000FF;">][</span><span style="color: #000000;">right</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">newnode</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1.12</span><span style="color: #0000FF;">)</span>
|
||||
procedure main()
|
||||
object root = newnode(1.00)
|
||||
-- Add some nodes.
|
||||
root[left] = newnode(1.10)
|
||||
root[left][left] = newnode(1.11)
|
||||
root[left][right] = newnode(1.12)
|
||||
|
||||
<span style="color: #000000;">root</span><span style="color: #0000FF;">[</span><span style="color: #000000;">right</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">newnode</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1.20</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #000000;">root</span><span style="color: #0000FF;">[</span><span style="color: #000000;">right</span><span style="color: #0000FF;">][</span><span style="color: #000000;">left</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">newnode</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1.21</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #000000;">root</span><span style="color: #0000FF;">[</span><span style="color: #000000;">right</span><span style="color: #0000FF;">][</span><span style="color: #000000;">right</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">newnode</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1.22</span><span style="color: #0000FF;">)</span>
|
||||
root[right] = newnode(1.20)
|
||||
root[right][left] = newnode(1.21)
|
||||
root[right][right] = newnode(1.22)
|
||||
|
||||
<span style="color: #000080;font-style:italic;">-- Now the tree has seven nodes.
|
||||
-- Now the tree has seven nodes.
|
||||
|
||||
-- Show the whole tree.</span>
|
||||
<span style="color: #7060A8;">ppOpt</span><span style="color: #0000FF;">({</span><span style="color: #004600;">pp_Nest</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">})</span>
|
||||
<span style="color: #7060A8;">pp</span><span style="color: #0000FF;">(</span><span style="color: #000000;">root</span><span style="color: #0000FF;">)</span>
|
||||
-- Show the whole tree.
|
||||
ppOpt({pp_Nest,2})
|
||||
pp(root)
|
||||
|
||||
<span style="color: #000080;font-style:italic;">-- Modify the whole tree.</span>
|
||||
<span style="color: #000000;">root</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">tmap</span><span style="color: #0000FF;">(</span><span style="color: #000000;">root</span><span style="color: #0000FF;">,</span><span style="color: #000000;">add10</span><span style="color: #0000FF;">)</span>
|
||||
-- Modify the whole tree.
|
||||
root = tmap(root,add10)
|
||||
|
||||
<span style="color: #000080;font-style:italic;">-- Create a whole new tree.</span>
|
||||
<span style="color: #004080;">object</span> <span style="color: #000000;">root2</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">tmap</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">deep_copy</span><span style="color: #0000FF;">(</span><span style="color: #000000;">root</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">),</span><span style="color: #000000;">newnode</span><span style="color: #0000FF;">)</span>
|
||||
-- Create a whole new tree.
|
||||
object root2 = tmap(deep_copy(root,1),newnode)
|
||||
|
||||
<span style="color: #000080;font-style:italic;">-- Show the whole tree again.</span>
|
||||
<span style="color: #7060A8;">pp</span><span style="color: #0000FF;">(</span><span style="color: #000000;">root</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
|
||||
<span style="color: #000000;">main</span><span style="color: #0000FF;">()</span>
|
||||
<!--
|
||||
-- Show the whole tree again.
|
||||
pp(root)
|
||||
end procedure
|
||||
main()
|
||||
|
|
|
|||
|
|
@ -0,0 +1,55 @@
|
|||
class BinaryTree
|
||||
function __construct(T, value)
|
||||
assert(type(T) == "string", "T must be the name of a class")
|
||||
assert(type(value) == T, "Value must be of type T.")
|
||||
self.kind = T
|
||||
self.value = value
|
||||
self.left = nil
|
||||
self.right = nil
|
||||
end
|
||||
|
||||
-- Alternative constructor to enable kind to be inferred from type of value.
|
||||
static function of(value) return new BinaryTree(type(value), value) end
|
||||
|
||||
function getKind() return self.kind end
|
||||
function getValue() return self.value end
|
||||
function getLeft() return self.left end
|
||||
function getRight() return self.right end
|
||||
|
||||
function setValue(v)
|
||||
assert(type(v) == self.kind, $"Value must be of type {self.kind}")
|
||||
self.value = v
|
||||
end
|
||||
|
||||
function setLeft(b)
|
||||
assert(b instanceof BinaryTree and b:getKind() == self.kind,
|
||||
$"Argument must be a BinaryTree of type {self.kind}")
|
||||
self.left = b
|
||||
end
|
||||
|
||||
function setRight(b)
|
||||
assert(b instanceof BinaryTree and b:getKind() == self.kind,
|
||||
$"Argument must be a BinaryTree of type {self.kind}")
|
||||
self.right = b
|
||||
end
|
||||
|
||||
function map(f)
|
||||
local tree = BinaryTree.of(f(self.value))
|
||||
if self.left then tree:setLeft(self.left:map(f)) end
|
||||
if self.right then tree:setRight(self.right:map(f)) end
|
||||
return tree
|
||||
end
|
||||
|
||||
function show_top_three()
|
||||
return $"({self.left:getValue()}, {self.value}, {self.right:getValue()})"
|
||||
end
|
||||
end
|
||||
|
||||
local b = BinaryTree.of(6)
|
||||
b:setLeft (BinaryTree.of(5))
|
||||
b:setRight(BinaryTree.of(7))
|
||||
print(b:show_top_three())
|
||||
|
||||
local b2 = b:map(|i| -> i * 10)
|
||||
print(b2:show_top_three())
|
||||
b2:setValue("six") -- generates an error because "six" is not a number
|
||||
|
|
@ -0,0 +1,53 @@
|
|||
interface IntCollection {
|
||||
map_elements(fn (int))
|
||||
}
|
||||
|
||||
struct BinaryTree {
|
||||
left bool
|
||||
right bool
|
||||
}
|
||||
|
||||
fn (t &BinaryTree) map_elements(visit fn (int)) {
|
||||
if t.left == t.right {
|
||||
visit(3)
|
||||
visit(1)
|
||||
visit(4)
|
||||
}
|
||||
}
|
||||
|
||||
struct BTree {
|
||||
buckets int
|
||||
}
|
||||
|
||||
fn (t &BTree) map_elements(visit fn (int)) {
|
||||
if t.buckets >= 0 {
|
||||
visit(1)
|
||||
visit(5)
|
||||
visit(9)
|
||||
}
|
||||
}
|
||||
|
||||
struct Accumulator {
|
||||
mut:
|
||||
sum int
|
||||
count int
|
||||
}
|
||||
|
||||
fn average(cal IntCollection) f64 {
|
||||
mut acc := &Accumulator{}
|
||||
acc.sum, acc.count = 0, 0
|
||||
cal.map_elements(fn [mut acc] (n int) {
|
||||
acc.sum += n
|
||||
acc.count++
|
||||
})
|
||||
return f64(acc.sum) / f64(acc.count)
|
||||
}
|
||||
|
||||
fn main() {
|
||||
t1 := &BinaryTree{}
|
||||
t2 := &BTree{}
|
||||
a1 := average(t1)
|
||||
a2 := average(t2)
|
||||
println('binary tree average: ${a1}')
|
||||
println('b-tree average: ${a2}')
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue