sbtrees_test.anubis
3.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
The Anubis Project
A test for sbtrees.
read tools/basis.anubis
read tools/sbtrees.anubis
define SBTree(String,String)
com_put
(
SBTree(String,String) t
) =
with key = prompt("key: "),
value = prompt("Value: "),
put(key,value,t).
define SBTree(String,String)
com_rput1
(
String key,
SBTree(String,String) t
) =
with value = random(100000),
put(key,to_hexa(extract(sha1(value),0,3)),t).
define SBTree(String,String)
put_several
(
String prefix,
Int n,
SBTree(String,String) t
) =
if n < 1 then t else
with t1 = com_rput1(prefix+n,t),
put_several(prefix,n-1,t1).
define SBTree(String,String)
com_rput
(
SBTree(String,String) t
) =
with sn = prompt("Number of entries: "),
pre = prompt("key prefix: "),
if decimal_scan(sn) is
{
failure then print("Incorrect number.\n"); t,
success(n) then if n < 0
then print("Enter a positive number, please.\n"); com_rput(t)
else put_several(pre,n,t)
}.
define SBTree(String,String)
com_unput
(
SBTree(String,String) t
) =
with key = prompt("Key: "),
unput(key,t).
define SBTree(String,String)
com_get
(
SBTree(String,String) t
) =
with key = prompt("Key: "),
if get_value(key,t) is
{
failure then print("No value for key: "+key+"\n"),
success(val) then print(key+" = "+val+"\n")
}; t.
define One
show
(
SBTreeC(String,String) t,
String indent
) =
if t is
{
entry(k,v) then
print(indent+((Word32)1)+" "+k+" : "+v+"\n"),
node(ne,fk,lk,lb,rb) then
print(indent+ne+"\n"); show(lb,indent+" ");
show(rb,indent+" ")
}.
define SBTree(String,String)
com_show
(
SBTree(String,String) t
) =
if t is sbtree(_,mbe_tree) then
if mbe_tree is
{
empty then print("B-tree is empty.\n"),
full(tree) then show(tree,"")
}; t.
define One
loop
(
SBTree(String,String) t
) =
if is_balanced(t) then
with com = prompt("\nEnter one character among these:\n "+
" p (put), r (random put), u (unput), g (get), s (show), q (quit)\n"),
if com = "p" then loop(com_put(t)) else
if com = "r" then loop(com_rput(t)) else
if com = "u" then loop(com_unput(t)) else
if com = "g" then loop(com_get(t)) else
if com = "s" then loop(com_show(t)) else
if com = "q" then unique else
print("Error: "+com+"\n"); loop(t)
else print("Tree not balanced.\n").
define SBTComp
compare
(
String x,
String y
) =
if x = y then same else
if x < y then before else after.
global define One
sbtrees_test
(
List(String) args
) =
with t = new_sbtree("",compare),
loop(t).