about summary refs log tree commit diff stats
path: root/html/065duplex_list.mu.html
blob: 8dad9fc454f66de237faba10d9d6af41ac009854 (plain) (blame)
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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>Mu - 065duplex_list.mu</title>
<meta name="Generator" content="Vim/7.4">
<meta name="plugin-version" content="vim7.4_v2">
<meta name="syntax" content="none">
<meta name="settings" content="use_css,pre_wrap,no_foldcolumn,expand_tabs,prevent_copy=">
<meta name="colorscheme" content="minimal">
<style type="text/css">
<!--
pre { white-space: pre-wrap; font-family: monospace; color: #eeeeee; background-color: #080808; }
body { font-size: 12pt; font-family: monospace; color: #eeeeee; background-color: #080808; }
* { font-size: 12pt; font-size: 1em; }
.muRecipe { color: #ff8700; }
.muData { color: #ffff00; }
.muScenario { color: #00af00; }
.Delimiter { color: #800080; }
.Comment { color: #9090ff; }
.Constant { color: #00a0a0; }
.Special { color: #c00000; }
.muControl { color: #c0a020; }
-->
</style>

<script type='text/javascript'>
<!--

-->
</script>
</head>
<body>
<pre id='vimCodeElement'>
<span class="Comment"># A doubly linked list permits bidirectional traversal.</span>

<span class="muData">container</span> duplex-list:_elem [
  value:_elem
  next:address:duplex-list:_elem
  prev:address:duplex-list:_elem
]

<span class="Comment"># should I say in/contained-in:result, allow ingredients to refer to products?</span>
<span class="muRecipe">def</span> push x:_elem, in:address:duplex-list:_elem<span class="muRecipe"> -&gt; </span>in:address:duplex-list:_elem [
  <span class="Constant">local-scope</span>
  <span class="Constant">load-ingredients</span>
  result:address:duplex-list:_elem<span class="Special"> &lt;- </span>new <span class="Delimiter">{</span>(duplex-list _elem): type<span class="Delimiter">}</span>
  *result<span class="Special"> &lt;- </span>merge x, in, <span class="Constant">0</span>
  <span class="Delimiter">{</span>
    <span class="muControl">break-unless</span> in
    *in<span class="Special"> &lt;- </span>put *in, <span class="Constant">prev:offset</span>, result
  <span class="Delimiter">}</span>
  <span class="muControl">return</span> result  <span class="Comment"># needed explicitly because we need to replace 'in' with 'result'</span>
]

<span class="muRecipe">def</span> first in:address:duplex-list:_elem<span class="muRecipe"> -&gt; </span>result:_elem [
  <span class="Constant">local-scope</span>
  <span class="Constant">load-ingredients</span>
  <span class="muControl">return-unless</span> in, <span class="Constant">0</span>
  result<span class="Special"> &lt;- </span>get *in, <span class="Constant">value:offset</span>
]

<span class="muRecipe">def</span> next in:address:duplex-list:_elem<span class="muRecipe"> -&gt; </span>result:address:duplex-list:_elem/contained-in:in [
  <span class="Constant">local-scope</span>
  <span class="Constant">load-ingredients</span>
  <span class="muControl">return-unless</span> in, <span class="Constant">0</span>
  result<span class="Special"> &lt;- </span>get *in, <span class="Constant">next:offset</span>
]

<span class="muRecipe">def</span> prev in:address:duplex-list:_elem<span class="muRecipe"> -&gt; </span>result:address:duplex-list:_elem/contained-in:in [
  <span class="Constant">local-scope</span>
  <span class="Constant">load-ingredients</span>
  <span class="muControl">return-unless</span> in, <span class="Constant">0</span>
  result<span class="Special"> &lt;- </span>get *in, <span class="Constant">prev:offset</span>
  <span class="muControl">return</span> result
]

<span class="muScenario">scenario</span> duplex-list-handling [
  run [
    <span class="Constant">local-scope</span>
    <span class="Comment"># reserve locations 0-9 to check for missing null check</span>
    <span class="Constant">10</span>:number/<span class="Special">raw &lt;- </span>copy <span class="Constant">34</span>
    <span class="Constant">11</span>:number/<span class="Special">raw &lt;- </span>copy <span class="Constant">35</span>
    list:address:duplex-list:character<span class="Special"> &lt;- </span>push <span class="Constant">3</span>, <span class="Constant">0</span>
    list<span class="Special"> &lt;- </span>push <span class="Constant">4</span>, list
    list<span class="Special"> &lt;- </span>push <span class="Constant">5</span>, list
    list2:address:duplex-list:character<span class="Special"> &lt;- </span>copy list
    <span class="Constant">20</span>:character/<span class="Special">raw &lt;- </span>first list2
    list2<span class="Special"> &lt;- </span>next list2
    <span class="Constant">21</span>:character/<span class="Special">raw &lt;- </span>first list2
    list2<span class="Special"> &lt;- </span>next list2
    <span class="Constant">22</span>:character/<span class="Special">raw &lt;- </span>first list2
    <span class="Constant">30</span>:address:duplex-list:character/<span class="Special">raw &lt;- </span>next list2
    <span class="Constant">31</span>:character/<span class="Special">raw &lt;- </span>first <span class="Constant">30</span>:address:duplex-list:character/<span class="Special">raw</span>
    <span class="Constant">32</span>:address:duplex-list:character/<span class="Special">raw &lt;- </span>next <span class="Constant">30</span>:address:duplex-list:character/<span class="Special">raw</span>
    <span class="Constant">33</span>:address:duplex-list:character/<span class="Special">raw &lt;- </span>prev <span class="Constant">30</span>:address:duplex-list:character/<span class="Special">raw</span>
    list2<span class="Special"> &lt;- </span>prev list2
    <span class="Constant">40</span>:character/<span class="Special">raw &lt;- </span>first list2
    list2<span class="Special"> &lt;- </span>prev list2
    <span class="Constant">41</span>:character/<span class="Special">raw &lt;- </span>first list2
    <span class="Constant">50</span>:boolean/<span class="Special">raw &lt;- </span>equal list, list2
  ]
  memory-should-contain [
    <span class="Constant">0</span><span class="Special"> &lt;- </span><span class="Constant">0</span>  <span class="Comment"># no modifications to null pointers</span>
    <span class="Constant">10</span><span class="Special"> &lt;- </span><span class="Constant">34</span>
    <span class="Constant">11</span><span class="Special"> &lt;- </span><span class="Constant">35</span>
    <span class="Constant">20</span><span class="Special"> &lt;- </span><span class="Constant">5</span>  <span class="Comment"># scanning next</span>
    <span class="Constant">21</span><span class="Special"> &lt;- </span><span class="Constant">4</span>
    <span class="Constant">22</span><span class="Special"> &lt;- </span><span class="Constant">3</span>
    <span class="Constant">30</span><span class="Special"> &lt;- </span><span class="Constant">0</span>  <span class="Comment"># null</span>
    <span class="Constant">31</span><span class="Special"> &lt;- </span><span class="Constant">0</span>  <span class="Comment"># first of null</span>
    <span class="Constant">32</span><span class="Special"> &lt;- </span><span class="Constant">0</span>  <span class="Comment"># next of null</span>
    <span class="Constant">33</span><span class="Special"> &lt;- </span><span class="Constant">0</span>  <span class="Comment"># prev of null</span>
    <span class="Constant">40</span><span class="Special"> &lt;- </span><span class="Constant">4</span>  <span class="Comment"># then start scanning prev</span>
    <span class="Constant">41</span><span class="Special"> &lt;- </span><span class="Constant">5</span>
    <span class="Constant">50</span><span class="Special"> &lt;- </span><span class="Constant">1</span>  <span class="Comment"># list back at start</span>
  ]
]

<span class="Comment"># insert 'x' after 'in'</span>
<span class="muRecipe">def</span> insert x:_elem, in:address:duplex-list:_elem<span class="muRecipe"> -&gt; </span>in:address:duplex-list:_elem [
  <span class="Constant">local-scope</span>
  <span class="Constant">load-ingredients</span>
  new-node:address:duplex-list:_elem<span class="Special"> &lt;- </span>new <span class="Delimiter">{</span>(duplex-list _elem): type<span class="Delimiter">}</span>
  *new-node<span class="Special"> &lt;- </span>put *new-node, <span class="Constant">value:offset</span>, x
  <span class="Comment"># save old next before changing it</span>
  next-node:address:duplex-list:_elem<span class="Special"> &lt;- </span>get *in, <span class="Constant">next:offset</span>
  *in<span class="Special"> &lt;- </span>put *in, <span class="Constant">next:offset</span>, new-node
  *new-node<span class="Special"> &lt;- </span>put *new-node, <span class="Constant">prev:offset</span>, in
  *new-node<span class="Special"> &lt;- </span>put *new-node, <span class="Constant">next:offset</span>, next-node
  <span class="muControl">return-unless</span> next-node
  *next-node<span class="Special"> &lt;- </span>put *next-node, <span class="Constant">prev:offset</span>, new-node
]

<span class="muScenario">scenario</span> inserting-into-duplex-list [
  run [
    <span class="Constant">local-scope</span>
    list:address:duplex-list:character<span class="Special"> &lt;- </span>push <span class="Constant">3</span>, <span class="Constant">0</span>
    list<span class="Special"> &lt;- </span>push <span class="Constant">4</span>, list
    list<span class="Special"> &lt;- </span>push <span class="Constant">5</span>, list
    list2:address:duplex-list:character<span class="Special"> &lt;- </span>next list  <span class="Comment"># inside list</span>
    list2<span class="Special"> &lt;- </span>insert <span class="Constant">6</span>, list2
    <span class="Comment"># check structure like before</span>
    list2<span class="Special"> &lt;- </span>copy list
    <span class="Constant">10</span>:character/<span class="Special">raw &lt;- </span>first list2
    list2<span class="Special"> &lt;- </span>next list2
    <span class="Constant">11</span>:character/<span class="Special">raw &lt;- </span>first list2
    list2<span class="Special"> &lt;- </span>next list2
    <span class="Constant">12</span>:character/<span class="Special">raw &lt;- </span>first list2
    list2<span class="Special"> &lt;- </span>next list2
    <span class="Constant">13</span>:character/<span class="Special">raw &lt;- </span>first list2
    list2<span class="Special"> &lt;- </span>prev list2
    <span class="Constant">20</span>:character/<span class="Special">raw &lt;- </span>first list2
    list2<span class="Special"> &lt;- </span>prev list2
    <span class="Constant">21</span>:character/<span class="Special">raw &lt;- </span>first list2
    list2<span class="Special"> &lt;- </span>prev list2
    <span class="Constant">22</span>:character/<span class="Special">raw &lt;- </span>first list2
    <span class="Constant">30</span>:boolean/<span class="Special">raw &lt;- </span>equal list, list2
  ]
  memory-should-contain [
    <span class="Constant">10</span><span class="Special"> &lt;- </span><span class="Constant">5</span>  <span class="Comment"># scanning next</span>
    <span class="Constant">11</span><span class="Special"> &lt;- </span><span class="Constant">4</span>
    <span class="Constant">12</span><span class="Special"> &lt;- </span><span class="Constant">6</span>  <span class="Comment"># inserted element</span>
    <span class="Constant">13</span><span class="Special"> &lt;- </span><span class="Constant">3</span>
    <span class="Constant">20</span><span class="Special"> &lt;- </span><span class="Constant">6</span>  <span class="Comment"># then prev</span>
    <span class="Constant">21</span><span class="Special"> &lt;- </span><span class="Constant">4</span>
    <span class="Constant">22</span><span class="Special"> &lt;- </span><span class="Constant">5</span>
    <span class="Constant">30</span><span class="Special"> &lt;- </span><span class="Constant">1</span>  <span class="Comment"># list back at start</span>
  ]
]

<span class="muScenario">scenario</span> inserting-at-end-of-duplex-list [
  run [
    <span class="Constant">local-scope</span>
    list:address:duplex-list:character<span class="Special"> &lt;- </span>push <span class="Constant">3</span>, <span class="Constant">0</span>
    list<span class="Special"> &lt;- </span>push <span class="Constant">4</span>, list
    list<span class="Special"> &lt;- </span>push <span class="Constant">5</span>, list
    list2:address:duplex-list:character<span class="Special"> &lt;- </span>next list  <span class="Comment"># inside list</span>
    list2<span class="Special"> &lt;- </span>next list2  <span class="Comment"># now at end of list</span>
    list2<span class="Special"> &lt;- </span>insert <span class="Constant">6</span>, list2
    <span class="Comment"># check structure like before</span>
    list2<span class="Special"> &lt;- </span>copy list
    <span class="Constant">10</span>:character/<span class="Special">raw &lt;- </span>first list2
    list2<span class="Special"> &lt;- </span>next list2
    <span class="Constant">11</span>:character/<span class="Special">raw &lt;- </span>first list2
    list2<span class="Special"> &lt;- </span>next list2
    <span class="Constant">12</span>:character/<span class="Special">raw &lt;- </span>first list2
    list2<span class="Special"> &lt;- </span>next list2
    <span class="Constant">13</span>:character/<span class="Special">raw &lt;- </span>first list2
    list2<span class="Special"> &lt;- </span>prev list2
    <span class="Constant">20</span>:character/<span class="Special">raw &lt;- </span>first list2
    list2<span class="Special"> &lt;- </span>prev list2
    <span class="Constant">21</span>:character/<span class="Special">raw &lt;- </span>first list2
    list2<span class="Special"> &lt;- </span>prev list2
    <span class="Constant">22</span>:character/<span class="Special">raw &lt;- </span>first list2
    <span class="Constant">30</span>:boolean/<span class="Special">raw &lt;- </span>equal list, list2
  ]
  memory-should-contain [
    <span class="Constant">10</span><span class="Special"> &lt;- </span><span class="Constant">5</span>  <span class="Comment"># scanning next</span>
    <span class="Constant">11</span><span class="Special"> &lt;- </span><span class="Constant">4</span>
    <span class="Constant">12</span><span class="Special"> &lt;- </span><span class="Constant">3</span>
    <span class="Constant">13</span><span class="Special"> &lt;- </span><span class="Constant">6</span>  <span class="Comment"># inserted element</span>
    <span class="Constant">20</span><span class="Special"> &lt;- </span><span class="Constant">3</span>  <span class="Comment"># then prev</span>
    <span class="Constant">21</span><span class="Special"> &lt;- </span><span class="Constant">4</span>
    <span class="Constant">22</span><span class="Special"> &lt;- </span><span class="Constant">5</span>
    <span class="Constant">30</span><span class="Special"> &lt;- </span><span class="Constant">1</span>  <span class="Comment"># list back at start</span>
  ]
]

<span class="muScenario">scenario</span> inserting-after-start-of-duplex-list [
  run [
    <span class="Constant">local-scope</span>
    list:address:duplex-list:character<span class="Special"> &lt;- </span>push <span class="Constant">3</span>, <span class="Constant">0</span>
    list<span class="Special"> &lt;- </span>push <span class="Constant">4</span>, list
    list<span class="Special"> &lt;- </span>push <span class="Constant">5</span>, list
    list<span class="Special"> &lt;- </span>insert <span class="Constant">6</span>, list
    <span class="Comment"># check structure like before</span>
    list2:address:duplex-list:character<span class="Special"> &lt;- </span>copy list
    <span class="Constant">10</span>:character/<span class="Special">raw &lt;- </span>first list2
    list2<span class="Special"> &lt;- </span>next list2
    <span class="Constant">11</span>:character/<span class="Special">raw &lt;- </span>first list2
    list2<span class="Special"> &lt;- </span>next list2
    <span class="Constant">12</span>:character/<span class="Special">raw &lt;- </span>first list2
    list2<span class="Special"> &lt;- </span>next list2
    <span class="Constant">13</span>:character/<span class="Special">raw &lt;- </span>first list2
    list2<span class="Special"> &lt;- </span>prev list2
    <span class="Constant">20</span>:character/<span class="Special">raw &lt;- </span>first list2
    list2<span class="Special"> &lt;- </span>prev list2
    <span class="Constant">21</span>:character/<span class="Special">raw &lt;- </span>first list2
    list2<span class="Special"> &lt;- </span>prev list2
    <span class="Constant">22</span>:character/<span class="Special">raw &lt;- </span>first list2
    <span class="Constant">30</span>:boolean/<span class="Special">raw &lt;- </span>equal list, list2
  ]
  memory-should-contain [
    <span class="Constant">10</span><span class="Special"> &lt;- </span><span class="Constant">5</span>  <span class="Comment"># scanning next</span>
    <span class="Constant">11</span><span class="Special"> &lt;- </span><span class="Constant">6</span>  <span class="Comment"># inserted element</span>
    <span class="Constant">12</span><span class="Special"> &lt;- </span><span class="Constant">4</span>
    <span class="Constant">13</span><span class="Special"> &lt;- </span><span class="Constant">3</span>
    <span class="Constant">20</span><span class="Special"> &lt;- </span><span class="Constant">4</span>  <span class="Comment"># then prev</span>
    <span class="Constant">21</span><span class="Special"> &lt;- </span><span class="Constant">6</span>
    <span class="Constant">22</span><span class="Special"> &lt;- </span><span class="Constant">5</span>
    <span class="Constant">30</span><span class="Special"> &lt;- </span><span class="Constant">1</span>  <span class="Comment"># list back at start</span>
  ]
]

<span class="Comment"># remove 'x' from its surrounding list 'in'</span>
<span class="Comment">#</span>
<span class="Comment"># Returns null if and only if list is empty. Beware: in that case any other</span>
<span class="Comment"># pointers to the head are now invalid.</span>
<span class="muRecipe">def</span> remove x:address:duplex-list:_elem/contained-in:in, in:address:duplex-list:_elem<span class="muRecipe"> -&gt; </span>in:address:duplex-list:_elem [
  <span class="Constant">local-scope</span>
  <span class="Constant">load-ingredients</span>
  <span class="Comment"># if 'x' is null, return</span>
  <span class="muControl">return-unless</span> x
  next-node:address:duplex-list:_elem<span class="Special"> &lt;- </span>get *x, <span class="Constant">next:offset</span>
  prev-node:address:duplex-list:_elem<span class="Special"> &lt;- </span>get *x, <span class="Constant">prev:offset</span>
  <span class="Comment"># null x's pointers</span>
  *x<span class="Special"> &lt;- </span>put *x, <span class="Constant">next:offset</span>, <span class="Constant">0</span>
  *x<span class="Special"> &lt;- </span>put *x, <span class="Constant">prev:offset</span>, <span class="Constant">0</span>
  <span class="Comment"># if next-node is not null, set its prev pointer</span>
  <span class="Delimiter">{</span>
    <span class="muControl">break-unless</span> next-node
    *next-node<span class="Special"> &lt;- </span>put *next-node, <span class="Constant">prev:offset</span>, prev-node
  <span class="Delimiter">}</span>
  <span class="Comment"># if prev-node is not null, set its next pointer and return</span>
  <span class="Delimiter">{</span>
    <span class="muControl">break-unless</span> prev-node
    *prev-node<span class="Special"> &lt;- </span>put *prev-node, <span class="Constant">next:offset</span>, next-node
    <span class="muControl">return</span>
  <span class="Delimiter">}</span>
  <span class="Comment"># if prev-node is null, then we removed the head node at 'in'</span>
  <span class="Comment"># return the new head rather than the old 'in'</span>
  <span class="muControl">return</span> next-node
]

<span class="muScenario">scenario</span> removing-from-duplex-list [
  run [
    <span class="Constant">local-scope</span>
    list:address:duplex-list:character<span class="Special"> &lt;- </span>push <span class="Constant">3</span>, <span class="Constant">0</span>
    list<span class="Special"> &lt;- </span>push <span class="Constant">4</span>, list
    list<span class="Special"> &lt;- </span>push <span class="Constant">5</span>, list
    list2:address:duplex-list:character<span class="Special"> &lt;- </span>next list  <span class="Comment"># second element</span>
    list<span class="Special"> &lt;- </span>remove list2, list
    <span class="Constant">10</span>:boolean/<span class="Special">raw &lt;- </span>equal list2, <span class="Constant">0</span>
    <span class="Comment"># check structure like before</span>
    list2<span class="Special"> &lt;- </span>copy list
    <span class="Constant">11</span>:character/<span class="Special">raw &lt;- </span>first list2
    list2<span class="Special"> &lt;- </span>next list2
    <span class="Constant">12</span>:character/<span class="Special">raw &lt;- </span>first list2
    <span class="Constant">20</span>:address:duplex-list:character/<span class="Special">raw &lt;- </span>next list2
    list2<span class="Special"> &lt;- </span>prev list2
    <span class="Constant">30</span>:character/<span class="Special">raw &lt;- </span>first list2
    <span class="Constant">40</span>:boolean/<span class="Special">raw &lt;- </span>equal list, list2
  ]
  memory-should-contain [
    <span class="Constant">10</span><span class="Special"> &lt;- </span><span class="Constant">0</span>  <span class="Comment"># remove returned non-null</span>
    <span class="Constant">11</span><span class="Special"> &lt;- </span><span class="Constant">5</span>  <span class="Comment"># scanning next, skipping deleted element</span>
    <span class="Constant">12</span><span class="Special"> &lt;- </span><span class="Constant">3</span>
    <span class="Constant">20</span><span class="Special"> &lt;- </span><span class="Constant">0</span>  <span class="Comment"># no more elements</span>
    <span class="Constant">30</span><span class="Special"> &lt;- </span><span class="Constant">5</span>  <span class="Comment"># prev of final element</span>
    <span class="Constant">40</span><span class="Special"> &lt;- </span><span class="Constant">1</span>  <span class="Comment"># list back at start</span>
  ]
]

<span class="muScenario">scenario</span> removing-from-start-of-duplex-list [
  run [
    <span class="Constant">local-scope</span>
    list:address:duplex-list:character<span class="Special"> &lt;- </span>push <span class="Constant">3</span>, <span class="Constant">0</span>
    list<span class="Special"> &lt;- </span>push <span class="Constant">4</span>, list
    list<span class="Special"> &lt;- </span>push <span class="Constant">5</span>, list
    list<span class="Special"> &lt;- </span>remove list, list
    <span class="Comment"># check structure like before</span>
    list2:address:duplex-list:character<span class="Special"> &lt;- </span>copy list
    <span class="Constant">10</span>:character/<span class="Special">raw &lt;- </span>first list2
    list2<span class="Special"> &lt;- </span>next list2
    <span class="Constant">11</span>:character/<span class="Special">raw &lt;- </span>first list2
    <span class="Constant">20</span>:address:duplex-list:character/<span class="Special">raw &lt;- </span>next list2
    list2<span class="Special"> &lt;- </span>prev list2
    <span class="Constant">30</span>:character/<span class="Special">raw &lt;- </span>first list2
    <span class="Constant">40</span>:boolean/<span class="Special">raw &lt;- </span>equal list, list2
  ]
  memory-should-contain [
    <span class="Constant">10</span><span class="Special"> &lt;- </span><span class="Constant">4</span>  <span class="Comment"># scanning next, skipping deleted element</span>
    <span class="Constant">11</span><span class="Special"> &lt;- </span><span class="Constant">3</span>
    <span class="Constant">20</span><span class="Special"> &lt;- </span><span class="Constant">0</span>  <span class="Comment"># no more elements</span>
    <span class="Constant">30</span><span class="Special"> &lt;- </span><span class="Constant">4</span>  <span class="Comment"># prev of final element</span>
    <span class="Constant">40</span><span class="Special"> &lt;- </span><span class="Constant">1</span>  <span class="Comment"># list back at start</span>
  ]
]

<span class="muScenario">scenario</span> removing-from-end-of-duplex-list [
  run [
    <span class="Constant">local-scope</span>
    list:address:duplex-list:character<span class="Special"> &lt;- </span>push <span class="Constant">3</span>, <span class="Constant">0</span>
    list<span class="Special"> &lt;- </span>push <span class="Constant">4</span>, list
    list<span class="Special"> &lt;- </span>push <span class="Constant">5</span>, list
    <span class="Comment"># delete last element</span>
    list2:address:duplex-list:character<span class="Special"> &lt;- </span>next list
    list2<span class="Special"> &lt;- </span>next list2
    list<span class="Special"> &lt;- </span>remove list2, list
    <span class="Constant">10</span>:boolean/<span class="Special">raw &lt;- </span>equal list2, <span class="Constant">0</span>
    <span class="Comment"># check structure like before</span>
    list2<span class="Special"> &lt;- </span>copy list
    <span class="Constant">11</span>:character/<span class="Special">raw &lt;- </span>first list2
    list2<span class="Special"> &lt;- </span>next list2
    <span class="Constant">12</span>:character/<span class="Special">raw &lt;- </span>first list2
    <span class="Constant">20</span>:address:duplex-list:character/<span class="Special">raw &lt;- </span>next list2
    list2<span class="Special"> &lt;- </span>prev list2
    <span class="Constant">30</span>:character/<span class="Special">raw &lt;- </span>first list2
    <span class="Constant">40</span>:boolean/<span class="Special">raw &lt;- </span>equal list, list2
  ]
  memory-should-contain [
    <span class="Constant">10</span><span class="Special"> &lt;- </span><span class="Constant">0</span>  <span class="Comment"># remove returned non-null</span>
    <span class="Constant">11</span><span class="Special"> &lt;- </span><span class="Constant">5</span>  <span class="Comment"># scanning next, skipping deleted element</span>
    <span class="Constant">12</span><span class="Special"> &lt;- </span><span class="Constant">4</span>
    <span class="Constant">20</span><span class="Special"> &lt;- </span><span class="Constant">0</span>  <span class="Comment"># no more elements</span>
    <span class="Constant">30</span><span class="Special"> &lt;- </span><span class="Constant">5</span>  <span class="Comment"># prev of final element</span>
    <span class="Constant">40</span><span class="Special"> &lt;- </span><span class="Constant">1</span>  <span class="Comment"># list back at start</span>
  ]
]

<span class="muScenario">scenario</span> removing-from-singleton-duplex-list [
  run [
    <span class="Constant">local-scope</span>
    list:address:duplex-list:character<span class="Special"> &lt;- </span>push <span class="Constant">3</span>, <span class="Constant">0</span>
    list<span class="Special"> &lt;- </span>remove list, list
    <span class="Constant">1</span>:number/<span class="Special">raw &lt;- </span>copy list
  ]
  memory-should-contain [
    <span class="Constant">1</span><span class="Special"> &lt;- </span><span class="Constant">0</span>  <span class="Comment"># back to an empty list</span>
  ]
]

<span class="Comment"># remove values between 'start' and 'end' (both exclusive).</span>
<span class="Comment"># also clear pointers back out from start/end for hygiene.</span>
<span class="Comment"># set end to 0 to delete everything past start.</span>
<span class="Comment"># can't set start to 0 to delete everything before end, because there's no</span>
<span class="Comment"># clean way to return the new head pointer.</span>
<span class="muRecipe">def</span> remove-between start:address:duplex-list:_elem, end:address:duplex-list:_elem/contained-in:start<span class="muRecipe"> -&gt; </span>start:address:duplex-list:_elem [
  <span class="Constant">local-scope</span>
  <span class="Constant">load-ingredients</span>
  next:address:duplex-list:_elem<span class="Special"> &lt;- </span>get *start, <span class="Constant">next:offset</span>
  nothing-to-delete?:boolean<span class="Special"> &lt;- </span>equal next, end
  <span class="muControl">return-if</span> nothing-to-delete?
  assert next, <span class="Constant">[malformed duplex list]</span>
  <span class="Comment"># start-&gt;next-&gt;prev = 0</span>
  <span class="Comment"># start-&gt;next = end</span>
  *next<span class="Special"> &lt;- </span>put *next, <span class="Constant">prev:offset</span>, <span class="Constant">0</span>
  *start<span class="Special"> &lt;- </span>put *start, <span class="Constant">next:offset</span>, end
  <span class="muControl">return-unless</span> end
  <span class="Comment"># end-&gt;prev-&gt;next = 0</span>
  <span class="Comment"># end-&gt;prev = start</span>
  prev:address:duplex-list:_elem<span class="Special"> &lt;- </span>get *end, <span class="Constant">prev:offset</span>
  assert prev, <span class="Constant">[malformed duplex list - 2]</span>
  *prev<span class="Special"> &lt;- </span>put *prev, <span class="Constant">next:offset</span>, <span class="Constant">0</span>
  *end<span class="Special"> &lt;- </span>put *end, <span class="Constant">prev:offset</span>, start
]

<span class="muScenario">scenario</span> remove-range [
  <span class="Comment"># construct a duplex list with six elements [13, 14, 15, 16, 17, 18]</span>
  <span class="Constant">local-scope</span>
  list:address:duplex-list:character<span class="Special"> &lt;- </span>push <span class="Constant">18</span>, <span class="Constant">0</span>
  list<span class="Special"> &lt;- </span>push <span class="Constant">17</span>, list
  list<span class="Special"> &lt;- </span>push <span class="Constant">16</span>, list
  list<span class="Special"> &lt;- </span>push <span class="Constant">15</span>, list
  list<span class="Special"> &lt;- </span>push <span class="Constant">14</span>, list
  list<span class="Special"> &lt;- </span>push <span class="Constant">13</span>, list
  <span class="Constant">1</span>:address:duplex-list:character/<span class="Special">raw &lt;- </span>copy list  <span class="Comment"># save list</span>
  run [
    <span class="Constant">local-scope</span>
    list:address:duplex-list:character<span class="Special"> &lt;- </span>copy <span class="Constant">1</span>:address:duplex-list:character/<span class="Special">raw</span>  <span class="Comment"># restore list</span>
    <span class="Comment"># delete 16 onwards</span>
    <span class="Comment"># first pointer: to the third element</span>
    list2:address:duplex-list:character<span class="Special"> &lt;- </span>next list
    list2<span class="Special"> &lt;- </span>next list2
    list2<span class="Special"> &lt;- </span>remove-between list2, <span class="Constant">0</span>
    <span class="Comment"># now check the list</span>
    <span class="Constant">10</span>:character/<span class="Special">raw &lt;- </span>get *list, <span class="Constant">value:offset</span>
    list<span class="Special"> &lt;- </span>next list
    <span class="Constant">11</span>:character/<span class="Special">raw &lt;- </span>get *list, <span class="Constant">value:offset</span>
    list<span class="Special"> &lt;- </span>next list
    <span class="Constant">12</span>:character/<span class="Special">raw &lt;- </span>get *list, <span class="Constant">value:offset</span>
    <span class="Constant">20</span>:address:duplex-list:character/<span class="Special">raw &lt;- </span>next list
  ]
  memory-should-contain [
    <span class="Constant">10</span><span class="Special"> &lt;- </span><span class="Constant">13</span>
    <span class="Constant">11</span><span class="Special"> &lt;- </span><span class="Constant">14</span>
    <span class="Constant">12</span><span class="Special"> &lt;- </span><span class="Constant">15</span>
    <span class="Constant">20</span><span class="Special"> &lt;- </span><span class="Constant">0</span>
  ]
]

<span class="muScenario">scenario</span> remove-range-to-final [
  <span class="Constant">local-scope</span>
  <span class="Comment"># construct a duplex list with six elements [13, 14, 15, 16, 17, 18]</span>
  list:address:duplex-list:character<span class="Special"> &lt;- </span>push <span class="Constant">18</span>, <span class="Constant">0</span>
  list<span class="Special"> &lt;- </span>push <span class="Constant">17</span>, list
  list<span class="Special"> &lt;- </span>push <span class="Constant">16</span>, list
  list<span class="Special"> &lt;- </span>push <span class="Constant">15</span>, list
  list<span class="Special"> &lt;- </span>push <span class="Constant">14</span>, list
  list<span class="Special"> &lt;- </span>push <span class="Constant">13</span>, list
  <span class="Constant">1</span>:address:duplex-list:character/<span class="Special">raw &lt;- </span>copy list  <span class="Comment"># save list</span>
  run [
    <span class="Constant">local-scope</span>
    list:address:duplex-list:character<span class="Special"> &lt;- </span>copy <span class="Constant">1</span>:address:duplex-list:character/<span class="Special">raw</span>  <span class="Comment"># restore list</span>
    <span class="Comment"># delete 15, 16 and 17</span>
    <span class="Comment"># start pointer: to the second element</span>
    list2:address:duplex-list:character<span class="Special"> &lt;- </span>next list
    <span class="Comment"># end pointer: to the last (sixth) element</span>
    end:address:duplex-list:character<span class="Special"> &lt;- </span>next list2
    end<span class="Special"> &lt;- </span>next end
    end<span class="Special"> &lt;- </span>next end
    end<span class="Special"> &lt;- </span>next end
    remove-between list2, end
    <span class="Comment"># now check the list</span>
    <span class="Constant">10</span>:character/<span class="Special">raw &lt;- </span>get *list, <span class="Constant">value:offset</span>
    list<span class="Special"> &lt;- </span>next list
    <span class="Constant">11</span>:character/<span class="Special">raw &lt;- </span>get *list, <span class="Constant">value:offset</span>
    list<span class="Special"> &lt;- </span>next list
    <span class="Constant">12</span>:character/<span class="Special">raw &lt;- </span>get *list, <span class="Constant">value:offset</span>
    <span class="Constant">20</span>:address:duplex-list:character/<span class="Special">raw &lt;- </span>next list
  ]
  memory-should-contain [
    <span class="Constant">10</span><span class="Special"> &lt;- </span><span class="Constant">13</span>
    <span class="Constant">11</span><span class="Special"> &lt;- </span><span class="Constant">14</span>
    <span class="Constant">12</span><span class="Special"> &lt;- </span><span class="Constant">18</span>
    <span class="Constant">20</span><span class="Special"> &lt;- </span><span class="Constant">0</span>  <span class="Comment"># no more elements</span>
  ]
]

<span class="muScenario">scenario</span> remove-range-empty [
  <span class="Constant">local-scope</span>
  <span class="Comment"># construct a duplex list with three elements [13, 14, 15]</span>
  list:address:duplex-list:character<span class="Special"> &lt;- </span>push <span class="Constant">15</span>, <span class="Constant">0</span>
  list<span class="Special"> &lt;- </span>push <span class="Constant">14</span>, list
  list<span class="Special"> &lt;- </span>push <span class="Constant">13</span>, list
  <span class="Constant">1</span>:address:duplex-list:character/<span class="Special">raw &lt;- </span>copy list  <span class="Comment"># save list</span>
  run [
    <span class="Constant">local-scope</span>
    list:address:duplex-list:character<span class="Special"> &lt;- </span>copy <span class="Constant">1</span>:address:duplex-list:character/<span class="Special">raw</span>  <span class="Comment"># restore list</span>
    <span class="Comment"># delete between first and second element (i.e. nothing)</span>
    list2:address:duplex-list:character<span class="Special"> &lt;- </span>next list
    remove-between list, list2
    <span class="Comment"># now check the list</span>
    <span class="Constant">10</span>:character/<span class="Special">raw &lt;- </span>get *list, <span class="Constant">value:offset</span>
    list<span class="Special"> &lt;- </span>next list
    <span class="Constant">11</span>:character/<span class="Special">raw &lt;- </span>get *list, <span class="Constant">value:offset</span>
    list<span class="Special"> &lt;- </span>next list
    <span class="Constant">12</span>:character/<span class="Special">raw &lt;- </span>get *list, <span class="Constant">value:offset</span>
    <span class="Constant">20</span>:address:duplex-list:character/<span class="Special">raw &lt;- </span>next list
  ]
  <span class="Comment"># no change</span>
  memory-should-contain [
    <span class="Constant">10</span><span class="Special"> &lt;- </span><span class="Constant">13</span>
    <span class="Constant">11</span><span class="Special"> &lt;- </span><span class="Constant">14</span>
    <span class="Constant">12</span><span class="Special"> &lt;- </span><span class="Constant">15</span>
    <span class="Constant">20</span><span class="Special"> &lt;- </span><span class="Constant">0</span>
  ]
]

<span class="muScenario">scenario</span> remove-range-to-end [
  <span class="Constant">local-scope</span>
  <span class="Comment"># construct a duplex list with six elements [13, 14, 15, 16, 17, 18]</span>
  list:address:duplex-list:character<span class="Special"> &lt;- </span>push <span class="Constant">18</span>, <span class="Constant">0</span>
  list<span class="Special"> &lt;- </span>push <span class="Constant">17</span>, list
  list<span class="Special"> &lt;- </span>push <span class="Constant">16</span>, list
  list<span class="Special"> &lt;- </span>push <span class="Constant">15</span>, list
  list<span class="Special"> &lt;- </span>push <span class="Constant">14</span>, list
  list<span class="Special"> &lt;- </span>push <span class="Constant">13</span>, list
  <span class="Constant">1</span>:address:duplex-list:character/<span class="Special">raw &lt;- </span>copy list  <span class="Comment"># save list</span>
  run [
    <span class="Constant">local-scope</span>
    list:address:duplex-list:character<span class="Special"> &lt;- </span>copy <span class="Constant">1</span>:address:duplex-list:character/<span class="Special">raw</span>  <span class="Comment"># restore list</span>
    <span class="Comment"># remove the third element and beyond</span>
    list2:address:duplex-list:character<span class="Special"> &lt;- </span>next list
    remove-between list2, <span class="Constant">0</span>
    <span class="Comment"># now check the list</span>
    <span class="Constant">10</span>:character/<span class="Special">raw &lt;- </span>get *list, <span class="Constant">value:offset</span>
    list<span class="Special"> &lt;- </span>next list
    <span class="Constant">11</span>:character/<span class="Special">raw &lt;- </span>get *list, <span class="Constant">value:offset</span>
    <span class="Constant">20</span>:address:duplex-list:character/<span class="Special">raw &lt;- </span>next list
  ]
  memory-should-contain [
    <span class="Constant">10</span><span class="Special"> &lt;- </span><span class="Constant">13</span>
    <span class="Constant">11</span><span class="Special"> &lt;- </span><span class="Constant">14</span>
    <span class="Constant">20</span><span class="Special"> &lt;- </span><span class="Constant">0</span>
  ]
]

<span class="Comment"># insert list beginning at 'new' after 'in'</span>
<span class="muRecipe">def</span> insert-range in:address:duplex-list:_elem, start:address:duplex-list:_elem/contained-in:in<span class="muRecipe"> -&gt; </span>in:address:duplex-list:_elem [
  <span class="Constant">local-scope</span>
  <span class="Constant">load-ingredients</span>
  <span class="muControl">return-unless</span> in
  <span class="muControl">return-unless</span> start
  end:address:duplex-list:_elem<span class="Special"> &lt;- </span>copy start
  <span class="Delimiter">{</span>
    next:address:duplex-list:_elem<span class="Special"> &lt;- </span>next end/insert-range
    <span class="muControl">break-unless</span> next
    end<span class="Special"> &lt;- </span>copy next
    <span class="muControl">loop</span>
  <span class="Delimiter">}</span>
  next:address:duplex-list:_elem<span class="Special"> &lt;- </span>next in
  *end<span class="Special"> &lt;- </span>put *end, <span class="Constant">next:offset</span>, next
  <span class="Delimiter">{</span>
    <span class="muControl">break-unless</span> next
    *next<span class="Special"> &lt;- </span>put *next, <span class="Constant">prev:offset</span>, end
  <span class="Delimiter">}</span>
  *in<span class="Special"> &lt;- </span>put *in, <span class="Constant">next:offset</span>, start
  *start<span class="Special"> &lt;- </span>put *start, <span class="Constant">prev:offset</span>, in
]

<span class="muRecipe">def</span> append in:address:duplex-list:_elem, new:address:duplex-list:_elem/contained-in:in<span class="muRecipe"> -&gt; </span>in:address:duplex-list:_elem [
  <span class="Constant">local-scope</span>
  <span class="Constant">load-ingredients</span>
  last:address:duplex-list:_elem<span class="Special"> &lt;- </span>last in
  *last<span class="Special"> &lt;- </span>put *last, <span class="Constant">next:offset</span>, new
  <span class="muControl">return-unless</span> new
  *new<span class="Special"> &lt;- </span>put *new, <span class="Constant">prev:offset</span>, last
]

<span class="muRecipe">def</span> last in:address:duplex-list:_elem<span class="muRecipe"> -&gt; </span>result:address:duplex-list:_elem [
  <span class="Constant">local-scope</span>
  <span class="Constant">load-ingredients</span>
  result<span class="Special"> &lt;- </span>copy in
  <span class="Delimiter">{</span>
    next:address:duplex-list:_elem<span class="Special"> &lt;- </span>next result
    <span class="muControl">break-unless</span> next
    result<span class="Special"> &lt;- </span>copy next
    <span class="muControl">loop</span>
  <span class="Delimiter">}</span>
]

<span class="Comment"># helper for debugging</span>
<span class="muRecipe">def</span> dump-from x:address:duplex-list:_elem [
  <span class="Constant">local-scope</span>
  <span class="Constant">load-ingredients</span>
  $print x, <span class="Constant">[: ]</span>
  <span class="Delimiter">{</span>
    <span class="muControl">break-unless</span> x
    c:_elem<span class="Special"> &lt;- </span>get *x, <span class="Constant">value:offset</span>
    $print c, <span class="Constant">[ ]</span>
    x<span class="Special"> &lt;- </span>next x
    <span class="Delimiter">{</span>
      is-newline?:boolean<span class="Special"> &lt;- </span>equal c, <span class="Constant">10/newline</span>
      <span class="muControl">break-unless</span> is-newline?
      $print <span class="Constant">10/newline</span>
      $print x, <span class="Constant">[: ]</span>
    <span class="Delimiter">}</span>
    <span class="muControl">loop</span>
  <span class="Delimiter">}</span>
  $print <span class="Constant">10/newline</span>, <span class="Constant">[---]</span>, <span class="Constant">10/newline</span>
]
</pre>
</body>
</html>
<!-- vim: set foldmethod=manual : -->