This repository was archived by the owner on Apr 5, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMap.php
More file actions
388 lines (346 loc) · 10.3 KB
/
Copy pathMap.php
File metadata and controls
388 lines (346 loc) · 10.3 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
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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
<?php
class MapOne extends MapTree {
public $key;
public $value;
function __construct($key, $value)
{
$this->key = $key;
$this->value = $value;
}
}
class MapNode extends MapOne {
public $left;
public $right;
public $height;
function __construct($key, $value, $left, $right, $height)
{
parent::__construct($key, $value);
$this->left = $left;
$this->right = $right;
$this->height = $height;
}
}
class MapTree {
static function sizeAux($acc,$m)
{
if (is_null($m))
return $acc;
if ($m instanceof MapNode)
return MapTree::sizeAux(MapTree::sizeAux($acc+1, $m->left), $m->right);
return $acc + 1;
}
static function size($x) {
return MapTree::sizeAux(0, $x);
}
static function height ($m)
{
if (is_null($m))
return 0;
if ($m instanceof MapNode)
return $m->height;
return 1;
}
static function isEmpty($m)
{
return is_null($m);
}
static function mk($l, $k, $v, $r)
{
if(is_null($l) && is_null($r))
return new MapOne ($k, $v);
$hl = MapTree::height($l);
$hr = MapTree::height($r);
$m = $hl < $hr ? $hr : $hl;
return new MapNode ($k, $v, $l, $r, $m+1);
}
static function rebalance($t1, $k, $v, $t2)
{
$t1h = MapTree::height($t1);
$t2h = MapTree::height($t2);
if ($t2h > $t1h + 2)
{
// right is heavier than left
if ($t2 instanceof MapNode)
{
$t2k = $t2->key;
$t2v = $t2->value;
$t2l = $t2->left;
$t2r = $t2->right;
// one of the nodes must have height > height t1 + 1
if (MapTree::height($t2l) > $t1h + 1)
{
// balance left: combination
if (!($t2l instanceof MapNode))
throw new Error("rebalance");
$t2lk = $t2l->key;
$t2lv = $t2l->value;
$t2ll = $t2l->left;
$t2lr = $t2l->right;
return MapTree::mk(MapTree::mk($t1, $k, $v, $t2ll), $t2lk, $t2lv, MapTree::mk($t2lr, $t2k, $t2v, $t2r));
}
else
{
// rotate left
return MapTree::mk(MapTree::mk($t1, $k, $v, $t2l), $t2k, $t2v, $t2r);
}
}
else
{
throw new Error("rebalance");
}
}
else
{
if ($t1h > $t2h + 2)
{
// left is heavier than right
if ($t1 instanceof MapNode)
{
$t1k = $t1->key;
$t1v = $t1->value;
$t1l = $t1->left;
$t1r = $t1->right;
// one of the nodes must have height > height t2 + 1
if (MapTree::height($t1r) > $t2h + 1)
{
// balance right: combination
if (!($t1r instanceof MapNode))
throw new Error("rebalance");
$t1rk = $t1r->key;
$t1rv = $t1r->value;
$t1rl = $t1r->left;
$t1rr = $t1r->right;
return MapTree::mk(MapTree::mk($t1l, $t1k, $t1v, $t1rl), $t1rk, $t1rv, MapTree::mk($t1rr, $k, $v, $t2));
}
else
return MapTree::mk($t1l, $t1k, $t1v, MapTree::mk($t1r, $k, $v, $t2));
}
else
{
throw new Error("rebalance");
}
}
else
{
return MapTree::mk($t1, $k, $v, $t2);
}
}
}
static function add($comparer, $k, $v, $m)
{
if (is_null($m))
return new MapOne($k, $v);
if ($m instanceof MapNode)
{
$k2 = $m->key;
$v2 = $m->value;
$l = $m->left;
$r = $m->right;
$h = $m->height;
$c = $comparer['Compare']($k, $k2);
if ($c < 0)
return MapTree::rebalance(MapTree::add($comparer, $k, $v, $l), $k2, $v2, $r);
elseif ($c == 0)
return new MapNode($k, $v, $l, $r, $h);
else
return MapTree::rebalance($l, $k2, $v2, MapTree::add($comparer, $k, $v, $r));
}
$k2 = $m->key;
$c = $comparer['Compare']($k, $k2);
if ($c < 0)
return new MapNode($k, $v, NULL, $m, 2);
elseif ($c == 0)
return new MapOne($k, $v);
else
return new MapNode($k, $v, $m, NULL, 2);
}
static function tryGetValue($comparer, $k, &$v, $m)
{
if (is_null($m))
return false;
if ($m instanceof MapNode)
{
$k2= $m->key;
$v2= $m->value;
$l= $m->left;
$r= $m->right;
$c = $comparer['Compare']($k, $k2);
if ($c < 0)
return MapTree::tryGetValue($comparer, $k, $v, $l);
elseif ($c == 0)
{ $v = $v2;
return true; }
else
return MapTree::tryGetValue($comparer, $k, $v, $r);
}
$k2 = $m->key;
$v2 = $m->value;
$c = $comparer['Compare']($k, $k2);
if ($c == 0)
{ $v = $v2;
return true;
}
else
return false;
}
static function find($comparer, $k, $m)
{
if (MapTree::tryGetValue($comparer, $k, $v, $m))
return $v;
else
throw new Exception("Key not found");
}
static function tryFind($comparer, $k, $m)
{
if (MapTree::tryGetValue($comparer, $k, $v, $m))
return $v;
else
return NULL;
}
static function fold($f, $x, $m)
{
if (is_null($m))
return $x;
if ($m instanceof MapNode)
{
$x = MapTree::fold($f, $x, $m->left);
$x = $f($x, $m->key, $m->value);
return MapTree::fold($f, $x, $m->right);
}
return $f($x, $m->key, $m->value);
}
static function exists($f, $m)
{
if (is_null($m))
return false;
if ($m instanceof MapNode)
{
return
MapTree::exists($f, $m->left)
|| $f($m->key, $m->value)
|| MapTree::exists($f, $m->right);
}
return $f($m->key, $m->value);
}
static function mapi($f,$m)
{
if (is_null($m))
return $m;
if ($m instanceof MapNode)
{
$k = $m->key;
$l2 = MapTree::mapi($f, $m->left);
$v2 = $f($k, $m->value);
$r2 = MapTree::mapi($f, $m->right);
return new MapNode ($k, $v2, $l2, $r2, $m->height);
}
return new MapOne ($m->key, $f($m->key, $m->value));
}
}
class Map implements IteratorAggregate
{
// This type is logically immutable. This field is only mutated during deserialization.
public $Comparer;
// This type is logically immutable. This field is only mutated during deserialization.
public $Tree;
function __construct($comparer, $tree)
{
$this->Comparer = $comparer;
$this->Tree = $tree;
}
static $empty;
static function empty($comparer)
{
return new Map($comparer, NULL);
}
static function count($table)
{
return MapTree::size($table->Tree);
}
static function ofList($list)
{
$tree = NULL;
$comparer = [ 'Compare' => 'Util::compare' ];
while ($list instanceof Cons)
{
$tree = MapTree::add($comparer, $list->value[0], $list->value[1], $tree);
$list = $list->next;
}
return new Map($comparer, $tree);
}
static function ofSeq($seq)
{
$tree = NULL;
$comparer = [ 'Compare' => 'Util::compare' ];
foreach ($seq as $item)
{
$tree = MapTree::add($comparer, $item[0], $item[1], $tree);
}
return new Map($comparer, $tree);
}
static function ofArray($seq)
{
$tree = NULL;
$comparer = [ 'Compare' => 'Util::compare' ];
foreach ($seq as $item)
{
$tree = MapTree::add($comparer, $item[0], $item[1], $tree);
}
return new Map($comparer, $tree);
}
static function add($key, $value, $table)
{
return new Map($table->Comparer, MapTree::add($table->Comparer, $key, $value, $table->Tree));
}
static function find($key, $table)
{
return MapTree::find($table->Comparer,$key,$table->Tree);
}
static function tryFind($key, $table)
{
return MapTree::tryFind($table->Comparer,$key,$table->Tree);
}
static function toSeq($table)
{
return $table;
}
static function toList($table)
{
return FSharpList::ofSeq($table);
}
static function fold($f,$acc,$table)
{
return MapTree::fold($f,$acc,$table->Tree);
}
static function exists($f, $table)
{
return MapTree::exists($f, $table->Tree);
}
static function map($f, $table)
{
return new Map($table->Comparer, MapTree::mapi($f, $table->Tree));
}
static function FSharpMap__get_Item__2B595($table, $key)
{
return MapTree::find($table->Comparer, $key, $table->Tree);
}
public function getIterator() {
$stack = [];
$tree = $this->Tree;
while(!(is_null($tree) && empty($stack)))
{
if (is_null($tree))
$tree = array_pop($stack);
elseif ($tree instanceof MapNode)
{
array_push($stack, $tree->right);
array_push($stack, new MapOne($tree->key, $tree->value));
$tree = $tree->left;
}
else {
yield [$tree->key, $tree->value];
$tree = array_pop($stack);
}
}
}
}