/
BaseAvlTree.mo
374 lines (325 loc) · 10.2 KB
/
BaseAvlTree.mo
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
/*
* This file is part of OpenModelica.
*
* Copyright (c) 1998-2014, Open Source Modelica Consortium (OSMC),
* c/o Linköpings universitet, Department of Computer and Information Science,
* SE-58183 Linköping, Sweden.
*
* All rights reserved.
*
* THIS PROGRAM IS PROVIDED UNDER THE TERMS OF GPL VERSION 3 LICENSE OR
* THIS OSMC PUBLIC LICENSE (OSMC-PL) VERSION 1.2.
* ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS PROGRAM CONSTITUTES
* RECIPIENT'S ACCEPTANCE OF THE OSMC PUBLIC LICENSE OR THE GPL VERSION 3,
* ACCORDING TO RECIPIENTS CHOICE.
*
* The OpenModelica software and the Open Source Modelica
* Consortium (OSMC) Public License (OSMC-PL) are obtained
* from OSMC, either from the above address,
* from the URLs: http://www.ida.liu.se/projects/OpenModelica or
* http://www.openmodelica.org, and in the OpenModelica distribution.
* GNU version 3 is obtained from: http://www.gnu.org/copyleft/gpl.html.
*
* This program is distributed WITHOUT ANY WARRANTY; without
* even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE, EXCEPT AS EXPRESSLY SET FORTH
* IN THE BY RECIPIENT SELECTED SUBSIDIARY LICENSE CONDITIONS OF OSMC-PL.
*
* See the full OSMC Public License conditions for more details.
*
*/
encapsulated partial package BaseAvlTree
import BaseAvlSet;
extends BaseAvlSet;
replaceable type Value = Integer; // TODO: We should have an Any type
redeclare uniontype Tree
"The binary tree data structure."
record NODE
Key key "The key of the node.";
Value value;
Integer height "Height of tree, used for balancing";
Tree left "Left subtree.";
Tree right "Right subtree.";
end NODE;
record EMPTY end EMPTY;
end Tree;
replaceable partial function valueStr
"Prints a Value to a string."
input Value inValue;
output String outString;
end valueStr;
redeclare function printNodeStr
input Tree inNode;
output String outString;
algorithm
outString := match inNode
case NODE() then "(" + keyStr(inNode.key) + ", " + valueStr(inNode.value) + ")";
end match;
end printNodeStr;
replaceable function addConflictDefault = addConflictFail
"Default conflict resolving function for add.";
function addConflictFail
"Conflict resolving function for add which fails on conflict."
input Value newValue;
input Value oldValue;
output Value value;
algorithm
fail();
end addConflictFail;
function addConflictReplace
"Conflict resolving function for add which replaces the old value with the new."
input Value newValue;
input Value oldValue;
output Value value = newValue;
end addConflictReplace;
function addConflictKeep
"Conflict resolving function for add which keeps the old value."
input Value newValue;
input Value oldValue;
output Value value = oldValue;
end addConflictKeep;
redeclare function add
"Inserts a new node in the tree."
input Tree inTree;
input Key inKey;
input Value inValue;
input ConflictFunc conflictFunc = addConflictDefault "Used to resolve conflicts.";
output Tree tree=inTree;
partial function ConflictFunc
input Value newValue "The value given by the caller.";
input Value oldValue "The value already in the tree.";
output Value value "The value that will replace the existing value.";
end ConflictFunc;
algorithm
tree := match tree
local
Key key;
Value value;
Integer key_comp;
// Empty tree.
case EMPTY()
then NODE(inKey, inValue, 1, EMPTY(), EMPTY());
case NODE(key = key)
algorithm
key_comp := keyCompare(inKey, key);
if key_comp == -1 then
// Replace left branch.
tree.left := add(tree.left, inKey, inValue, conflictFunc);
elseif key_comp == 1 then
// Replace right branch.
tree.right := add(tree.right, inKey, inValue, conflictFunc);
else
// Use the given function to resolve the conflict.
value := conflictFunc(inValue, tree.value);
if not referenceEq(tree.value, value) then
tree.value := value;
end if;
end if;
then
if key_comp == 0 then tree else balance(tree);
end match;
end add;
redeclare function addList
"Adds a list of key-value pairs to the tree."
input output Tree tree;
input list<tuple<Key,Value>> inValues;
input ConflictFunc conflictFunc = addConflictDefault "Used to resolve conflicts.";
partial function ConflictFunc
input Value newValue "The value given by the caller.";
input Value oldValue "The value already in the tree.";
output Value value "The value that will replace the existing value.";
end ConflictFunc;
protected
Key key;
Value value;
algorithm
for t in inValues loop
(key, value) := t;
tree := add(tree, key, value, conflictFunc);
end for;
end addList;
function get
"Gets a value from the tree given a key."
input Tree inTree;
input Key inKey;
output Value outValue;
protected
Key key;
Integer key_comp;
Tree tree;
algorithm
NODE(key = key) := inTree;
key_comp := keyCompare(inKey, key);
outValue := match (key_comp, inTree)
case ( 0, NODE(value = outValue)) then outValue;
case ( 1, NODE(right = tree)) then get(tree, inKey);
case (-1, NODE(left = tree)) then get(tree, inKey);
end match;
end get;
function toList
"Converts the tree to a flat list of key-value tuples."
input Tree inTree;
input list<tuple<Key, Value>> inAccum = {};
output list<tuple<Key, Value>> outList;
algorithm
outList := match inTree
local
Key key;
Value value;
case NODE(key=key, value=value)
algorithm
outList := (key, value) :: inAccum;
outList := toList(inTree.left, outList);
outList := toList(inTree.right, outList);
then
outList;
else inAccum;
end match;
end toList;
function listValues
"Constructs a list of all the values in the tree."
input Tree tree;
input list<Value> accum = {};
output list<Value> values;
algorithm
values := match tree
local
Value value;
case NODE(value = value)
algorithm
values := value :: accum;
values := listValues(tree.left, values);
values := listValues(tree.right, values);
then
values;
else accum;
end match;
end listValues;
redeclare function join
"Joins two trees by adding the second one to the first."
input output Tree tree;
input Tree treeToJoin;
input ConflictFunc conflictFunc = addConflictDefault "Used to resolve conflicts.";
partial function ConflictFunc
input Value newValue "The value given by the caller.";
input Value oldValue "The value already in the tree.";
output Value value "The value that will replace the existing value.";
end ConflictFunc;
algorithm
tree := match treeToJoin
case EMPTY() then tree;
case NODE()
algorithm
tree := add(tree, treeToJoin.key, treeToJoin.value, conflictFunc=conflictFunc);
tree := join(tree, treeToJoin.left, conflictFunc=conflictFunc);
tree := join(tree, treeToJoin.right, conflictFunc=conflictFunc);
then tree;
end match;end join;
function map
"Traverses the tree in depth-first pre-order and applies the given function to
each node, constructing a new tree with the resulting nodes."
input Tree inTree;
input MapFunc inFunc;
output Tree outTree = inTree;
partial function MapFunc
input Key inKey;
input Value inValue;
output Value outValue;
end MapFunc;
algorithm
outTree := match outTree
local
Key key;
Value value, new_value;
Tree branch, new_branch;
case NODE(key=key, value=value)
algorithm
new_value := inFunc(key, value);
if not referenceEq(value, new_value) then
outTree.value := new_value;
end if;
new_branch := map(outTree.left, inFunc);
if not referenceEq(new_branch, outTree.left) then
outTree.left := new_branch;
end if;
new_branch := map(outTree.right, inFunc);
if not referenceEq(new_branch, outTree.right) then
outTree.right := new_branch;
end if;
then
outTree;
else inTree;
end match;
end map;
function fold<FT>
"Traverses the tree in depth-first pre-order and applies the given function to
each node, in the process updating the given argument."
input Tree inTree;
input FoldFunc inFunc;
input FT inStartValue;
output FT outResult = inStartValue;
partial function FoldFunc
input Key inKey;
input Value inValue;
input FT inFoldArg;
output FT outFoldArg;
end FoldFunc;
algorithm
outResult := match inTree
local
Key key;
Value value;
Tree branch;
case NODE(key=key, value=value)
algorithm
outResult := inFunc(key, value, outResult);
outResult := fold(inTree.left, inFunc, outResult);
outResult := fold(inTree.right, inFunc, outResult);
then
outResult;
else outResult;
end match;
end fold;
function mapFold<FT>
"Traverses the tree in depth-first pre-order and applies the given function to
each node, constructing a new tree with the resulting nodes. mapFold also
takes an extra argument which is updated on each call to the given function."
input Tree inTree;
input MapFunc inFunc;
input FT inStartValue;
output Tree outTree = inTree;
output FT outResult = inStartValue;
partial function MapFunc
input Key inKey;
input Value inValue;
input FT inFoldArg;
output Value outValue;
output FT outFoldArg;
end MapFunc;
algorithm
outTree := match outTree
local
Key key;
Value value, new_value;
Tree branch, new_branch;
case NODE(key=key, value=value)
algorithm
(new_value, outResult) := inFunc(key, value, outResult);
if not referenceEq(value, new_value) then
outTree.value := new_value;
end if;
(new_branch, outResult) := mapFold(outTree.left, inFunc, outResult);
if not referenceEq(new_branch, outTree.left) then
outTree.left := new_branch;
end if;
(new_branch, outResult) := mapFold(outTree.right, inFunc, outResult);
if not referenceEq(new_branch, outTree.right) then
outTree.right := new_branch;
end if;
then
outTree;
else inTree;
end match;
end mapFold;
annotation(__OpenModelica_Interface="util", __OpenModelica_isBaseClass=true);
end BaseAvlTree;