about summary refs log tree commit diff stats
path: root/html/075duplex_list.mu.html
blob: d30afff6c4df5e9b9026adc93a0920a5facf6dc3 (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
88pre { line-height: 125%; }
td.linenos .normal { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
span.linenos { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
td.linenos .special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
span.linenos.special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
.highlight .hll { background-color: #ffffcc }
.highlight .c { color: #888888 } /* Comment */
.highlight .err { color: #a61717; background-color: #e3d2d2 } /* Error */
.highlight .k { color: #008800; font-weight: bold } /* Keyword */
.highlight .ch { color: #888888 } /* Comment.Hashbang */
.highlight .cm { color: #888888 } /* Comment.Multiline */
.highlight .cp { color: #cc0000; font-weight: bold } /* Comment.Preproc */
.highlight .cpf { color: #888888 } /* Comment.PreprocFile */
.highlight .c1 { color: #888888 } /* Comment.Single */
.highlight .cs { color: #cc0000; font-weight: bold; background-color: #fff0f0 } /* Comment.Special */
.highlight .gd { color: #000000; background-color: #ffdddd } /* Generic.Deleted */
.highlight .ge { font-style: italic } /* Generic.Emph */
.highlight .ges { font-weight: bold; font-style: italic } /* Generic.EmphStrong */
.highlight .gr { color: #aa0000 } /* Generic.Error */
.highlight .gh { color: #333333 } /* Generic.Heading */
.highlight .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */
.highlight .go { color: #888888 } /* Generic.Output */
.highlight .gp { color: #555555 } /* Generic.Prompt */
.highlight .gs { font-weight: bold } /* Generic.Strong */
.highlight .gu { color: #666666 } /* Generic.Subheading */
.highlight .gt { color: #aa0000 } /* Generic.Traceback */
.highlight .kc { color: #008800; font-weight: bold } /* Keyword.Constant */
.highlight .kd { color: #008800; font-weight: bold } /* Keyword.Declaration */
.highlight .kn { color: #008800; font-weight: bold } /* Keyword.Namespace */
.highlight .kp { color: #008800 } /* Keyword.Pseudo */
.highlight .kr { color: #008800; font-weight: bold } /* Keyword.Reserved */
.highlight .kt { color: #888888; font-weight: bold } /* Keyword.Type */
.highlight .m { color: #0000DD; font-weight: bold } /* Literal.Number */
.highlight .s { color: #dd2200; background-color: #fff0f0 } /* Literal.String */
.highlight .na { color: #336699 } /* Name.Attribute */
.highlight .nb { color: #003388 } /* Name.Builtin */
.highlight .nc { color: #bb0066; font-weight: bold } /* Name.Class */
.highlight .no { color: #003366; font-weight: bold } /* Name.Constant */
.highlight .nd { color: #555555 } /* Name.Decorator */
.highlight .ne { color: #bb0066; font-weight: bold } /* Name.Exception */
.highlight .nf { color: #0066bb; font-weight: bold } /* Name.Function */
.highlight .nl { color: #336699; font-style: italic } /* Name.Label */
.highlight .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */
.highlight .py { color: #336699; font-weight: bold } /* Name.Property */
.highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */
.highlight .nv { color: #336699 } /* Name.Variable */
.highlight .ow { color: #008800 } /* Operator.Word */
.highlight .w { color: #bbbbbb } /* Text.Whitespace */
.highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */
.highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */
.highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */
.highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */
.highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */
.highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */
.highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */
.highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */
.highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */
.highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */
.highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */
.highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */
.highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */
.highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */
.highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */
.highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */
.highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */
.highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */
.highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */
.highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */
.highlight .vc { color: #336699 } /* Name.Variable.Class */
.highlight .vg { color: #dd7700 } /* Name.Variable.Global */
.highlight .vi { color: #3333bb } /* Name.Variable.Instance */
.highlight .vm { color: #336699 } /* Name.Variable.Magic */
.highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */
<!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 - display.mu</title>
<meta name="Generator" content="Vim/8.0">
<meta name="plugin-version" content="vim7.4_v2">
<meta name="syntax" content="none">
<meta name="settings" content="number_lines,use_css,pre_wrap,no_foldcolumn,expand_tabs,line_ids,prevent_copy=">
<meta name="colorscheme" content="minimal">
<style type="text/css">
<!--
pre { white-space: pre-wrap; font-family: monospace; color: #aaaaaa; background-color: #080808; }
body { font-size:12pt; font-family: monospace; color: #aaaaaa; background-color: #080808; }
.subxS2Comment a { color:inherit; }
.subxS1Comment a { color:inherit; }
.subxComment a { color:inherit; }
.subxH2Comment a { color:inherit; }
.subxH1Comment a { color:inherit; }
* { font-size:12pt; font-size: 1em; }
.muRecipe { color: #ff8700; }
.LineNr { color:#444444; }
.Constant { color:#00a0a0; }
.Special { color:#c00000; }
.Comment { color: #8080ff; }
-->
</style>

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

/* function to open any folds containing a jumped-to line before jumping to it */
function JumpToLine()
{
  var lineNum;
  lineNum = window.location.hash;
  lineNum = lineNum.substr(1); /* strip off '#' */

  if (lineNum.indexOf('L') == -1) {
    lineNum = 'L'+lineNum;
  }
  lineElem = document.getElementById(lineNum);
  /* Always jump to new location even if the line was hidden inside a fold, or
   * we corrected the raw number to a line ID.
   */
  if (lineElem) {
    lineElem.scrollIntoView(true);
  }
  returnpre { line-height: 125%; }
td.linenos .normal { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
span.linenos { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
td.linenos .special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
span.linenos.special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
.highlight .hll { background-color: #ffffcc }
.highlight .c { color: #888888 } /* Comment */
.highlight .err { color: #a61717; background-color: #e3d2d2 } /* Error */
.highlight .k { color: #008800; font-weight: bold } /* Keyword */
.highlight .ch { color: #888888 } /* Comment.Hashbang */
.highlight .cm { color: #888888 } /* Comment.Multiline */
.highlight .cp { color: #cc0000; font-weight: bold } /* Comment.Preproc */
.highlight .cpf { color: #888888 } /* Comment.PreprocFile */
.highlight .c1 { color: #888888 } /* Comment.Single */
.highlight .cs { color: #cc0000; font-weight: bold; background-color: #fff0f0 } /* Comment.Special */
.highlight .gd { color: #000000; background-color: #ffdddd } /* Generic.Deleted */
.highlight .ge { font-style: italic } /* Generic.Emph */
.highlight .ges { font-weight: bold; font-style: italic } /* Generic.EmphStrong */
.highlight .gr { color: #aa0000 } /* Generic.Error */
.highlight .gh { color: #333333 } /* Generic.Heading */
.highlight .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */
.highlight .go { color: #888888 } /* Generic.Output */
.highlight .gp { color: #555555 } /* Generic.Prompt */
.highlight .gs { font-weight: bold } /* Generic.Strong */
.highlight .gu { color: #666666 } /* Generic.Subheading */
.highlight .gt { color: #aa0000 } /* Generic.Traceback */
.highlight .kc { color: #008800; font-weight: bold } /* Keyword.Constant */
.highlight .kd { color: #008800; font-weight: bold } /* Keyword.Declaration */
.highlight .kn { color: #008800; font-weight: bold } /* Keyword.Namespace */
.highlight .kp { color: #008800 } /* Keyword.Pseudo */
.highlight .kr { color: #008800; font-weight: bold } /* Keyword.Reserved */
.highlight .kt { color: #888888; font-weight: bold } /* Keyword.Type */
.highlight .m { color: #0000DD; font-weight: bold } /* Literal.Number */
.highlight .s { color: #dd2200; background-color: #fff0f0 } /* Literal.String */
.highlight .na { color: #336699 } /* Name.Attribute */
.highlight .nb { color: #003388 } /* Name.Builtin */
.highlight .nc { color: #bb0066; font-weight: bold } /* Name.Class */
.highlight .no { color: #003366; font-weight: bold } /* Name.Constant */
.highlight .nd { color: #555555 } /* Name.Decorator */
.highlight .ne { color: #bb0066; font-weight: bold } /* Name.Exception */
.highlight .nf { color: #0066bb; font-weight: bold } /* Name.Function */
.highlight .nl { color: #336699; font-style: italic } /* Name.Label */
.highlight .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */
.highlight .py { color: #336699; font-weight: bold } /* Name.Property */
.highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */
.highlight .nv { color: #336699 } /* Name.Variable */
.highlight .ow { color: #008800 } /* Operator.Word */
.highlight .w { color: #bbbbbb } /* Text.Whitespace */
.highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */
.highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */
.highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */
.highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */
.highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */
.highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */
.highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */
.highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */
.highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */
.highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */
.highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */
.highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */
.highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */
.highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */
.highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */
.highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */
.highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */
.highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */
.highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */
.highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */
.highlight .vc { color: #336699 } /* Name.Variable.Class */
.highlight .vg { color: #dd7700 } /* Name.Variable.Global */
.highlight .vi { color: #3333bb } /* Name.Variable.Instance */
.highlight .vm { color: #336699 } /* Name.Variable.Magic */
.highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */
<!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 - 075duplex_list.mu</title>
<meta name="Generator" content="Vim/7.4">
<meta name="plugin-version" content="vim7.4_v1">
<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-family: monospace; color: #eeeeee; background-color: #080808; }
* { font-size: 1.05em; }
.muControl { color: #c0a020; }
.muRecipe { color: #ff8700; }
.muScenario { color: #00af00; }
.muData { color: #ffff00; }
.Comment { color: #9090ff; }
.Constant { color: #00a0a0; }
.Special { color: #ff6060; }
.Delimiter { color: #a04060; }
-->
</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:shared:duplex-list:_elem
  prev:address:shared:duplex-list:_elem
]

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

<span class="muRecipe">recipe</span> first in:address:shared: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">reply-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">recipe</span> next in:address:shared:duplex-list:_elem<span class="muRecipe"> -&gt; </span>result:address:shared:duplex-list:_elem/contained-in:in [
  <span class="Constant">local-scope</span>
  <span class="Constant">load-ingredients</span>
  <span class="muControl">reply-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">recipe</span> prev in:address:shared:duplex-list:_elem<span class="muRecipe"> -&gt; </span>result:address:shared:duplex-list:_elem/contained-in:in [
  <span class="Constant">local-scope</span>
  <span class="Constant">load-ingredients</span>
  <span class="muControl">reply-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">reply</span> result
]

<span class="muScenario">scenario</span> duplex-list-handling [
  run [
    <span class="Comment"># reserve locations 0, 1 and 2 to check for missing null check</span>
    <span class="Constant">1</span>:number<span class="Special"> &lt;- </span>copy <span class="Constant">34</span>
    <span class="Constant">2</span>:number<span class="Special"> &lt;- </span>copy <span class="Constant">35</span>
    <span class="Constant">3</span>:address:shared:duplex-list:character<span class="Special"> &lt;- </span>push <span class="Constant">3</span>, <span class="Constant">0</span>
    <span class="Constant">3</span>:address:shared:duplex-list:character<span class="Special"> &lt;- </span>push <span class="Constant">4</span>, <span class="Constant">3</span>:address:shared:duplex-list:character
    <span class="Constant">3</span>:address:shared:duplex-list:character<span class="Special"> &lt;- </span>push <span class="Constant">5</span>, <span class="Constant">3</span>:address:shared:duplex-list:character
    <span class="Constant">4</span>:address:shared:duplex-list:character<span class="Special"> &lt;- </span>copy <span class="Constant">3</span>:address:shared:duplex-list:character
    <span class="Constant">5</span>:character<span class="Special"> &lt;- </span>first <span class="Constant">4</span>:address:shared:duplex-list:character
    <span class="Constant">4</span>:address:shared:duplex-list:character<span class="Special"> &lt;- </span>next <span class="Constant">4</span>:address:shared:duplex-list:character
    <span class="Constant">6</span>:character<span class="Special"> &lt;- </span>first <span class="Constant">4</span>:address:shared:duplex-list:character
    <span class="Constant">4</span>:address:shared:duplex-list:character<span class="Special"> &lt;- </span>next <span class="Constant">4</span>:address:shared:duplex-list:character
    <span class="Constant">7</span>:character<span class="Special"> &lt;- </span>first <span class="Constant">4</span>:address:shared:duplex-list:character
    <span class="Constant">8</span>:address:shared:duplex-list:character<span class="Special"> &lt;- </span>next <span class="Constant">4</span>:address:shared:duplex-list:character
    <span class="Constant">9</span>:character<span class="Special"> &lt;- </span>first <span class="Constant">8</span>:address:shared:duplex-list:character
    <span class="Constant">10</span>:address:shared:duplex-list:character<span class="Special"> &lt;- </span>next <span class="Constant">8</span>:address:shared:duplex-list:character
    <span class="Constant">11</span>:address:shared:duplex-list:character<span class="Special"> &lt;- </span>prev <span class="Constant">8</span>:address:shared:duplex-list:character
    <span class="Constant">4</span>:address:shared:duplex-list:character<span class="Special"> &lt;- </span>prev <span class="Constant">4</span>:address:shared:duplex-list:character
    <span class="Constant">12</span>:character<span class="Special"> &lt;- </span>first <span class="Constant">4</span>:address:shared:duplex-list:character
    <span class="Constant">4</span>:address:shared:duplex-list:character<span class="Special"> &lt;- </span>prev <span class="Constant">4</span>:address:shared:duplex-list:character
    <span class="Constant">13</span>:character<span class="Special"> &lt;- </span>first <span class="Constant">4</span>:address:shared:duplex-list:character
    <span class="Constant">14</span>:boolean<span class="Special"> &lt;- </span>equal <span class="Constant">3</span>:address:shared:duplex-list:character, <span class="Constant">4</span>:address:shared:duplex-list:character
  ]
  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">1</span><span class="Special"> &lt;- </span><span class="Constant">34</span>
    <span class="Constant">2</span><span class="Special"> &lt;- </span><span class="Constant">35</span>
    <span class="Constant">5</span><span class="Special"> &lt;- </span><span class="Constant">5</span>  <span class="Comment"># scanning next</span>
    <span class="Constant">6</span><span class="Special"> &lt;- </span><span class="Constant">4</span>
    <span class="Constant">7</span><span class="Special"> &lt;- </span><span class="Constant">3</span>
    <span class="Constant">8</span><span class="Special"> &lt;- </span><span class="Constant">0</span>  <span class="Comment"># null</span>
    <span class="Constant">9</span><span class="Special"> &lt;- </span><span class="Constant">0</span>  <span class="Comment"># first of null</span>
    <span class="Constant">10</span><span class="Special"> &lt;- </span><span class="Constant">0</span>  <span class="Comment"># next of null</span>
    <span class="Constant">11</span><span class="Special"> &lt;- </span><span class="Constant">0</span>  <span class="Comment"># prev of null</span>
    <span class="Constant">12</span><span class="Special"> &lt;- </span><span class="Constant">4</span>  <span class="Comment"># then start scanning prev</span>
    <span class="Constant">13</span><span class="Special"> &lt;- </span><span class="Constant">5</span>
    <span class="Constant">14</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">recipe</span> insert x:_elem, in:address:shared:duplex-list:_elem<span class="muRecipe"> -&gt; </span>in:address:shared:duplex-list:_elem [
  <span class="Constant">local-scope</span>
  <span class="Constant">load-ingredients</span>
  new-node:address:shared:duplex-list:_elem<span class="Special"> &lt;- </span>new <span class="Delimiter">{</span>(duplex-list _elem): type<span class="Delimiter">}</span>
  val:address:_elem<span class="Special"> &lt;- </span>get-address *new-node, <span class="Constant">value:offset</span>
  *val<span class="Special"> &lt;- </span>copy x
  next-node:address:shared:duplex-list:_elem<span class="Special"> &lt;- </span>get *in, <span class="Constant">next:offset</span>
  <span class="Comment"># in.next = new-node</span>
  y:address:address:shared:duplex-list:_elem<span class="Special"> &lt;- </span>get-address *in, <span class="Constant">next:offset</span>
  *y<span class="Special"> &lt;- </span>copy new-node
  <span class="Comment"># new-node.prev = in</span>
  y<span class="Special"> &lt;- </span>get-address *new-node, <span class="Constant">prev:offset</span>
  *y<span class="Special"> &lt;- </span>copy in
  <span class="Comment"># new-node.next = next-node</span>
  y<span class="Special"> &lt;- </span>get-address *new-node, <span class="Constant">next:offset</span>
  *y<span class="Special"> &lt;- </span>copy next-node
  <span class="Comment"># if next-node is not null</span>
  <span class="muControl">reply-unless</span> next-node
  <span class="Comment"># next-node.prev = new-node</span>
  y<span class="Special"> &lt;- </span>get-address *next-node, <span class="Constant">prev:offset</span>
  *y<span class="Special"> &lt;- </span>copy new-node
]

<span class="muScenario">scenario</span> inserting-into-duplex-list [
  run [
    <span class="Constant">1</span>:address:shared:duplex-list:character<span class="Special"> &lt;- </span>push <span class="Constant">3</span>, <span class="Constant">0</span>
    <span class="Constant">1</span>:address:shared:duplex-list:character<span class="Special"> &lt;- </span>push <span class="Constant">4</span>, <span class="Constant">1</span>:address:shared:duplex-list:character
    <span class="Constant">1</span>:address:shared:duplex-list:character<span class="Special"> &lt;- </span>push <span class="Constant">5</span>, <span class="Constant">1</span>:address:shared:duplex-list:character
    <span class="Constant">2</span>:address:shared:duplex-list:character<span class="Special"> &lt;- </span>next <span class="Constant">1</span>:address:shared:duplex-list:character  <span class="Comment"># 2 points inside list</span>
    <span class="Constant">2</span>:address:shared:duplex-list:character<span class="Special"> &lt;- </span>insert <span class="Constant">6</span>, <span class="Constant">2</span>:address:shared:duplex-list:character
    <span class="Comment"># check structure like before</span>
    <span class="Constant">2</span>:address:shared:duplex-list:character<span class="Special"> &lt;- </span>copy <span class="Constant">1</span>:address:shared:duplex-list:character
    <span class="Constant">3</span>:character<span class="Special"> &lt;- </span>first <span class="Constant">2</span>:address:shared:duplex-list:character
    <span class="Constant">2</span>:address:shared:duplex-list:character<span class="Special"> &lt;- </span>next <span class="Constant">2</span>:address:shared:duplex-list:character
    <span class="Constant">4</span>:character<span class="Special"> &lt;- </span>first <span class="Constant">2</span>:address:shared:duplex-list:character
    <span class="Constant">2</span>:address:shared:duplex-list:character<span class="Special"> &lt;- </span>next <span class="Constant">2</span>:address:shared:duplex-list:character
    <span class="Constant">5</span>:character<span class="Special"> &lt;- </span>first <span class="Constant">2</span>:address:shared:duplex-list:character
    <span class="Constant">2</span>:address:shared:duplex-list:character<span class="Special"> &lt;- </span>next <span class="Constant">2</span>:address:shared:duplex-list:character
    <span class="Constant">6</span>:character<span class="Special"> &lt;- </span>first <span class="Constant">2</span>:address:shared:duplex-list:character
    <span class="Constant">2</span>:address:shared:duplex-list:character<span class="Special"> &lt;- </span>prev <span class="Constant">2</span>:address:shared:duplex-list:character
    <span class="Constant">7</span>:character<span class="Special"> &lt;- </span>first <span class="Constant">2</span>:address:shared:duplex-list:character
    <span class="Constant">2</span>:address:shared:duplex-list:character<span class="Special"> &lt;- </span>prev <span class="Constant">2</span>:address:shared:duplex-list:character
    <span class="Constant">8</span>:character<span class="Special"> &lt;- </span>first <span class="Constant">2</span>:address:shared:duplex-list:character
    <span class="Constant">2</span>:address:shared:duplex-list:character<span class="Special"> &lt;- </span>prev <span class="Constant">2</span>:address:shared:duplex-list:character
    <span class="Constant">9</span>:character<span class="Special"> &lt;- </span>first <span class="Constant">2</span>:address:shared:duplex-list:character
    <span class="Constant">10</span>:boolean<span class="Special"> &lt;- </span>equal <span class="Constant">1</span>:address:shared:duplex-list:character, <span class="Constant">2</span>:address:shared:duplex-list:character
  ]
  memory-should-contain [
    <span class="Constant">3</span><span class="Special"> &lt;- </span><span class="Constant">5</span>  <span class="Comment"># scanning next</span>
    <span class="Constant">4</span><span class="Special"> &lt;- </span><span class="Constant">4</span>
    <span class="Constant">5</span><span class="Special"> &lt;- </span><span class="Constant">6</span>  <span class="Comment"># inserted element</span>
    <span class="Constant">6</span><span class="Special"> &lt;- </span><span class="Constant">3</span>
    <span class="Constant">7</span><span class="Special"> &lt;- </span><span class="Constant">6</span>  <span class="Comment"># then prev</span>
    <span class="Constant">8</span><span class="Special"> &lt;- </span><span class="Constant">4</span>
    <span class="Constant">9</span><span class="Special"> &lt;- </span><span class="Constant">5</span>
    <span class="Constant">10</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">1</span>:address:shared:duplex-list:character<span class="Special"> &lt;- </span>push <span class="Constant">3</span>, <span class="Constant">0</span>
    <span class="Constant">1</span>:address:shared:duplex-list:character<span class="Special"> &lt;- </span>push <span class="Constant">4</span>, <span class="Constant">1</span>:address:shared:duplex-list:character
    <span class="Constant">1</span>:address:shared:duplex-list:character<span class="Special"> &lt;- </span>push <span class="Constant">5</span>, <span class="Constant">1</span>:address:shared:duplex-list:character
    <span class="Constant">2</span>:address:shared:duplex-list:character<span class="Special"> &lt;- </span>next <span class="Constant">1</span>:address:shared:duplex-list:character  <span class="Comment"># 2 points inside list</span>
    <span class="Constant">2</span>:address:shared:duplex-list:character<span class="Special"> &lt;- </span>next <span class="Constant">2</span>:address:shared:duplex-list:character  <span class="Comment"># now at end of list</span>
    <span class="Constant">2</span>:address:shared:duplex-list:character<span class="Special"> &lt;- </span>insert <span class="Constant">6</span>, <span class="Constant">2</span>:address:shared:duplex-list:character
    <span class="Comment"># check structure like before</span>
    <span class="Constant">2</span>:address:shared:duplex-list:character<span class="Special"> &lt;- </span>copy <span class="Constant">1</span>:address:shared:duplex-list:character
    <span class="Constant">3</span>:character<span class="Special"> &lt;- </span>first <span class="Constant">2</span>:address:shared:duplex-list:character
    <span class="Constant">2</span>:address:shared:duplex-list:character<span class="Special"> &lt;- </span>next <span class="Constant">2</span>:address:shared:duplex-list:character
    <span class="Constant">4</span>:character<span class="Special"> &lt;- </span>first <span class="Constant">2</span>:address:shared:duplex-list:character
    <span class="Constant">2</span>:address:shared:duplex-list:character<span class="Special"> &lt;- </span>next <span class="Constant">2</span>:address:shared:duplex-list:character
    <span class="Constant">5</span>:character<span class="Special"> &lt;- </span>first <span class="Constant">2</span>:address:shared:duplex-list:character
    <span class="Constant">2</span>:address:shared:duplex-list:character<span class="Special"> &lt;- </span>next <span class="Constant">2</span>:address:shared:duplex-list:character
    <span class="Constant">6</span>:character<span class="Special"> &lt;- </span>first <span class="Constant">2</span>:address:shared:duplex-list:character
    <span class="Constant">2</span>:address:shared:duplex-list:character<span class="Special"> &lt;- </span>prev <span class="Constant">2</span>:address:shared:duplex-list:character
    <span class="Constant">7</span>:character<span class="Special"> &lt;- </span>first <span class="Constant">2</span>:address:shared:duplex-list:character
    <span class="Constant">2</span>:address:shared:duplex-list:character<span class="Special"> &lt;- </span>prev <span class="Constant">2</span>:address:shared:duplex-list:character
    <span class="Constant">8</span>:character<span class="Special"> &lt;- </span>first <span class="Constant">2</span>:address:shared:duplex-list:character
    <span class="Constant">2</span>:address:shared:duplex-list:character<span class="Special"> &lt;- </span>prev <span class="Constant">2</span>:address:shared:duplex-list:character
    <span class="Constant">9</span>:character<span class="Special"> &lt;- </span>first <span class="Constant">2</span>:address:shared:duplex-list:character
    <span class="Constant">10</span>:boolean<span class="Special"> &lt;- </span>equal <span class="Constant">1</span>:address:shared:duplex-list:character, <span class="Constant">2</span>:address:shared:duplex-list:character
  ]
  memory-should-contain [
    <span class="Constant">3</span><span class="Special"> &lt;- </span><span class="Constant">5</span>  <span class="Comment"># scanning next</span>
    <span class="Constant">4</span><span class="Special"> &lt;- </span><span class="Constant">4</span>
    <span class="Constant">5</span><span class="Special"> &lt;- </span><span class="Constant">3</span>
    <span class="Constant">6</span><span class="Special"> &lt;- </span><span class="Constant">6</span>  <span class="Comment"># inserted element</span>
    <span class="Constant">7</span><span class="Special"> &lt;- </span><span class="Constant">3</span>  <span class="Comment"># then prev</span>
    <span class="Constant">8</span><span class="Special"> &lt;- </span><span class="Constant">4</span>
    <span class="Constant">9</span><span class="Special"> &lt;- </span><span class="Constant">5</span>
    <span class="Constant">10</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">1</span>:address:shared:duplex-list:character<span class="Special"> &lt;- </span>push <span class="Constant">3</span>, <span class="Constant">0</span>
    <span class="Constant">1</span>:address:shared:duplex-list:character<span class="Special"> &lt;- </span>push <span class="Constant">4</span>, <span class="Constant">1</span>:address:shared:duplex-list:character
    <span class="Constant">1</span>:address:shared:duplex-list:character<span class="Special"> &lt;- </span>push <span class="Constant">5</span>, <span class="Constant">1</span>:address:shared:duplex-list:character
    <span class="Constant">1</span>:address:shared:duplex-list:character<span class="Special"> &lt;- </span>insert <span class="Constant">6</span>, <span class="Constant">1</span>:address:shared:duplex-list:character
    <span class="Comment"># check structure like before</span>
    <span class="Constant">2</span>:address:shared:duplex-list:character<span class="Special"> &lt;- </span>copy <span class="Constant">1</span>:address:shared:duplex-list:character
    <span class="Constant">3</span>:character<span class="Special"> &lt;- </span>first <span class="Constant">2</span>:address:shared:duplex-list:character
    <span class="Constant">2</span>:address:shared:duplex-list:character<span class="Special"> &lt;- </span>next <span class="Constant">2</span>:address:shared:duplex-list:character
    <span class="Constant">4</span>:character<span class="Special"> &lt;- </span>first <span class="Constant">2</span>:address:shared:duplex-list:character
    <span class="Constant">2</span>:address:shared:duplex-list:character<span class="Special"> &lt;- </span>next <span class="Constant">2</span>:address:shared:duplex-list:character
    <span class="Constant">5</span>:character<span class="Special"> &lt;- </span>first <span class="Constant">2</span>:address:shared:duplex-list:character
    <span class="Constant">2</span>:address:shared:duplex-list:character<span class="Special"> &lt;- </span>next <span class="Constant">2</span>:address:shared:duplex-list:character
    <span class="Constant">6</span>:character<span class="Special"> &lt;- </span>first <span class="Constant">2</span>:address:shared:duplex-list:character
    <span class="Constant">2</span>:address:shared:duplex-list:character<span class="Special"> &lt;- </span>prev <span class="Constant">2</span>:address:shared:duplex-list:character
    <span class="Constant">7</span>:character<span class="Special"> &lt;- </span>first <span class="Constant">2</span>:address:shared:duplex-list:character
    <span class="Constant">2</span>:address:shared:duplex-list:character<span class="Special"> &lt;- </span>prev <span class="Constant">2</span>:address:shared:duplex-list:character
    <span class="Constant">8</span>:character<span class="Special"> &lt;- </span>first <span class="Constant">2</span>:address:shared:duplex-list:character
    <span class="Constant">2</span>:address:shared:duplex-list:character<span class="Special"> &lt;- </span>prev <span class="Constant">2</span>:address:shared:duplex-list:character
    <span class="Constant">9</span>:character<span class="Special"> &lt;- </span>first <span class="Constant">2</span>:address:shared:duplex-list:character
    <span class="Constant">10</span>:boolean<span class="Special"> &lt;- </span>equal <span class="Constant">1</span>:address:shared:duplex-list:character, <span class="Constant">2</span>:address:shared:duplex-list:character
  ]
  memory-should-contain [
    <span class="Constant">3</span><span class="Special"> &lt;- </span><span class="Constant">5</span>  <span class="Comment"># scanning next</span>
    <span class="Constant">4</span><span class="Special"> &lt;- </span><span class="Constant">6</span>  <span class="Comment"># inserted element</span>
    <span class="Constant">5</span><span class="Special"> &lt;- </span><span class="Constant">4</span>
    <span class="Constant">6</span><span class="Special"> &lt;- </span><span class="Constant">3</span>
    <span class="Constant">7</span><span class="Special"> &lt;- </span><span class="Constant">4</span>  <span class="Comment"># then prev</span>
    <span class="Constant">8</span><span class="Special"> &lt;- </span><span class="Constant">6</span>
    <span class="Constant">9</span><span class="Special"> &lt;- </span><span class="Constant">5</span>
    <span class="Constant">10</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">recipe</span> remove x:address:shared:duplex-list:_elem/contained-in:in, in:address:shared:duplex-list:_elem<span class="muRecipe"> -&gt; </span>in:address:shared: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">reply-unless</span> x
  next-node:address:shared:duplex-list:_elem<span class="Special"> &lt;- </span>get *x, <span class="Constant">next:offset</span>
  prev-node:address:shared:duplex-list:_elem<span class="Special"> &lt;- </span>get *x, <span class="Constant">prev:offset</span>
  <span class="Comment"># null x's pointers</span>
  tmp:address:address:shared:duplex-list:_elem<span class="Special"> &lt;- </span>get-address *x, <span class="Constant">next:offset</span>
  *tmp<span class="Special"> &lt;- </span>copy <span class="Constant">0</span>
  tmp<span class="Special"> &lt;- </span>get-address *x, <span class="Constant">prev:offset</span>
  *tmp<span class="Special"> &lt;- </span>copy <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
    tmp<span class="Special"> &lt;- </span>get-address *next-node, <span class="Constant">prev:offset</span>
    *tmp<span class="Special"> &lt;- </span>copy 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
    tmp<span class="Special"> &lt;- </span>get-address *prev-node, <span class="Constant">next:offset</span>
    *tmp<span class="Special"> &lt;- </span>copy next-node
    <span class="muControl">reply</span>
  <span class="Delimiter">}</span>
  <span class="Comment"># if prev-node is null, then we removed the node at 'in'</span>
  <span class="Comment"># return the new head rather than the old 'in'</span>
  <span class="muControl">reply</span> next-node
]

<span class="muScenario">scenario</span> removing-from-duplex-list [
  run [
    <span class="Constant">1</span>:address:shared:duplex-list:character<span class="Special"> &lt;- </span>push <span class="Constant">3</span>, <span class="Constant">0</span>
    <span class="Constant">1</span>:address:shared:duplex-list:character<span class="Special"> &lt;- </span>push <span class="Constant">4</span>, <span class="Constant">1</span>:address:shared:duplex-list:character
    <span class="Constant">1</span>:address:shared:duplex-list:character<span class="Special"> &lt;- </span>push <span class="Constant">5</span>, <span class="Constant">1</span>:address:shared:duplex-list:character
    <span class="Constant">2</span>:address:shared:duplex-list:character<span class="Special"> &lt;- </span>next <span class="Constant">1</span>:address:shared:duplex-list:character  <span class="Comment"># 2 points at second element</span>
    <span class="Constant">1</span>:address:shared:duplex-list:character<span class="Special"> &lt;- </span>remove <span class="Constant">2</span>:address:shared:duplex-list:character, <span class="Constant">1</span>:address:shared:duplex-list:character
    <span class="Constant">3</span>:boolean<span class="Special"> &lt;- </span>equal <span class="Constant">2</span>:address:shared:duplex-list:character, <span class="Constant">0</span>
    <span class="Comment"># check structure like before</span>
    <span class="Constant">2</span>:address:shared:duplex-list:character<span class="Special"> &lt;- </span>copy <span class="Constant">1</span>:address:shared:duplex-list:character
    <span class="Constant">4</span>:character<span class="Special"> &lt;- </span>first <span class="Constant">2</span>:address:shared:duplex-list:character
    <span class="Constant">2</span>:address:shared:duplex-list:character<span class="Special"> &lt;- </span>next <span class="Constant">2</span>:address:shared:duplex-list:character
    <span class="Constant">5</span>:character<span class="Special"> &lt;- </span>first <span class="Constant">2</span>:address:shared:duplex-list:character
    <span class="Constant">6</span>:address:shared:duplex-list:character<span class="Special"> &lt;- </span>next <span class="Constant">2</span>:address:shared:duplex-list:character
    <span class="Constant">2</span>:address:shared:duplex-list:character<span class="Special"> &lt;- </span>prev <span class="Constant">2</span>:address:shared:duplex-list:character
    <span class="Constant">7</span>:character<span class="Special"> &lt;- </span>first <span class="Constant">2</span>:address:shared:duplex-list:character
    <span class="Constant">8</span>:boolean<span class="Special"> &lt;- </span>equal <span class="Constant">1</span>:address:shared:duplex-list:character, <span class="Constant">2</span>:address:shared:duplex-list:character
  ]
  memory-should-contain [
    <span class="Constant">3</span><span class="Special"> &lt;- </span><span class="Constant">0</span>  <span class="Comment"># remove returned non-null</span>
    <span class="Constant">4</span><span class="Special"> &lt;- </span><span class="Constant">5</span>  <span class="Comment"># scanning next, skipping deleted element</span>
    <span class="Constant">5</span><span class="Special"> &lt;- </span><span class="Constant">3</span>
    <span class="Constant">6</span><span class="Special"> &lt;- </span><span class="Constant">0</span>  <span class="Comment"># no more elements</span>
    <span class="Constant">7</span><span class="Special"> &lt;- </span><span class="Constant">5</span>  <span class="Comment"># prev of final element</span>
    <span class="Constant">8</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">1</span>:address:shared:duplex-list:character<span class="Special"> &lt;- </span>push <span class="Constant">3</span>, <span class="Constant">0</span>
    <span class="Constant">1</span>:address:shared:duplex-list:character<span class="Special"> &lt;- </span>push <span class="Constant">4</span>, <span class="Constant">1</span>:address:shared:duplex-list:character
    <span class="Constant">1</span>:address:shared:duplex-list:character<span class="Special"> &lt;- </span>push <span class="Constant">5</span>, <span class="Constant">1</span>:address:shared:duplex-list:character
    <span class="Constant">1</span>:address:shared:duplex-list:character<span class="Special"> &lt;- </span>remove <span class="Constant">1</span>:address:shared:duplex-list:character, <span class="Constant">1</span>:address:shared:duplex-list:character
    <span class="Comment"># check structure like before</span>
    <span class="Constant">2</span>:address:shared:duplex-list:character<span class="Special"> &lt;- </span>copy <span class="Constant">1</span>:address:shared:duplex-list:character
    <span class="Constant">3</span>:character<span class="Special"> &lt;- </span>first <span class="Constant">2</span>:address:shared:duplex-list:character
    <span class="Constant">2</span>:address:shared:duplex-list:character<span class="Special"> &lt;- </span>next <span class="Constant">2</span>:address:shared:duplex-list:character
    <span class="Constant">4</span>:character<span class="Special"> &lt;- </span>first <span class="Constant">2</span>:address:shared:duplex-list:character
    <span class="Constant">5</span>:address:shared:duplex-list:character<span class="Special"> &lt;- </span>next <span class="Constant">2</span>:address:shared:duplex-list:character
    <span class="Constant">2</span>:address:shared:duplex-list:character<span class="Special"> &lt;- </span>prev <span class="Constant">2</span>:address:shared:duplex-list:character
    <span class="Constant">6</span>:character<span class="Special"> &lt;- </span>first <span class="Constant">2</span>:address:shared:duplex-list:character
    <span class="Constant">7</span>:boolean<span class="Special"> &lt;- </span>equal <span class="Constant">1</span>:address:shared:duplex-list:character, <span class="Constant">2</span>:address:shared:duplex-list:character
  ]
  memory-should-contain [
    <span class="Constant">3</span><span class="Special"> &lt;- </span><span class="Constant">4</span>  <span class="Comment"># scanning next, skipping deleted element</span>
    <span class="Constant">4</span><span class="Special"> &lt;- </span><span class="Constant">3</span>
    <span class="Constant">5</span><span class="Special"> &lt;- </span><span class="Constant">0</span>  <span class="Comment"># no more elements</span>
    <span class="Constant">6</span><span class="Special"> &lt;- </span><span class="Constant">4</span>  <span class="Comment"># prev of final element</span>
    <span class="Constant">7</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">1</span>:address:shared:duplex-list:character<span class="Special"> &lt;- </span>push <span class="Constant">3</span>, <span class="Constant">0</span>
    <span class="Constant">1</span>:address:shared:duplex-list:character<span class="Special"> &lt;- </span>push <span class="Constant">4</span>, <span class="Constant">1</span>:address:shared:duplex-list:character
    <span class="Constant">1</span>:address:shared:duplex-list:character<span class="Special"> &lt;- </span>push <span class="Constant">5</span>, <span class="Constant">1</span>:address:shared:duplex-list:character
    <span class="Comment"># delete last element</span>
    <span class="Constant">2</span>:address:shared:duplex-list:character<span class="Special"> &lt;- </span>next <span class="Constant">1</span>:address:shared:duplex-list:character
    <span class="Constant">2</span>:address:shared:duplex-list:character<span class="Special"> &lt;- </span>next <span class="Constant">2</span>:address:shared:duplex-list:character
    <span class="Constant">1</span>:address:shared:duplex-list:character<span class="Special"> &lt;- </span>remove <span class="Constant">2</span>:address:shared:duplex-list:character, <span class="Constant">1</span>:address:shared:duplex-list:character
    <span class="Constant">3</span>:boolean<span class="Special"> &lt;- </span>equal <span class="Constant">2</span>:address:shared:duplex-list:character, <span class="Constant">0</span>
    <span class="Comment"># check structure like before</span>
    <span class="Constant">2</span>:address:shared:duplex-list:character<span class="Special"> &lt;- </span>copy <span class="Constant">1</span>:address:shared:duplex-list:character
    <span class="Constant">4</span>:character<span class="Special"> &lt;- </span>first <span class="Constant">2</span>:address:shared:duplex-list:character
    <span class="Constant">2</span>:address:shared:duplex-list:character<span class="Special"> &lt;- </span>next <span class="Constant">2</span>:address:shared:duplex-list:character
    <span class="Constant">5</span>:character<span class="Special"> &lt;- </span>first <span class="Constant">2</span>:address:shared:duplex-list:character
    <span class="Constant">6</span>:address:shared:duplex-list:character<span class="Special"> &lt;- </span>next <span class="Constant">2</span>:address:shared:duplex-list:character
    <span class="Constant">2</span>:address:shared:duplex-list:character<span class="Special"> &lt;- </span>prev <span class="Constant">2</span>:address:shared:duplex-list:character
    <span class="Constant">7</span>:character<span class="Special"> &lt;- </span>first <span class="Constant">2</span>:address:shared:duplex-list:character
    <span class="Constant">8</span>:boolean<span class="Special"> &lt;- </span>equal <span class="Constant">1</span>:address:shared:duplex-list:character, <span class="Constant">2</span>:address:shared:duplex-list:character
  ]
  memory-should-contain [
    <span class="Constant">3</span><span class="Special"> &lt;- </span><span class="Constant">0</span>  <span class="Comment"># remove returned non-null</span>
    <span class="Constant">4</span><span class="Special"> &lt;- </span><span class="Constant">5</span>  <span class="Comment"># scanning next, skipping deleted element</span>
    <span class="Constant">5</span><span class="Special"> &lt;- </span><span class="Constant">4</span>
    <span class="Constant">6</span><span class="Special"> &lt;- </span><span class="Constant">0</span>  <span class="Comment"># no more elements</span>
    <span class="Constant">7</span><span class="Special"> &lt;- </span><span class="Constant">5</span>  <span class="Comment"># prev of final element</span>
    <span class="Constant">8</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-list [
  run [
    <span class="Constant">1</span>:address:shared:duplex-list:character<span class="Special"> &lt;- </span>push <span class="Constant">3</span>, <span class="Constant">0</span>
    <span class="Constant">1</span>:address:shared:duplex-list:character<span class="Special"> &lt;- </span>remove <span class="Constant">1</span>:address:shared:duplex-list:character, <span class="Constant">1</span>:address:shared:duplex-list:character
  ]
  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="muRecipe">recipe</span> remove-between start:address:shared:duplex-list:_elem, end:address:shared:duplex-list:_elem/contained-in:start<span class="muRecipe"> -&gt; </span>start:address:shared:duplex-list:_elem [
  <span class="Constant">local-scope</span>
  <span class="Constant">load-ingredients</span>
  <span class="muControl">reply-unless</span> start
  <span class="Comment"># start-&gt;next-&gt;prev = 0</span>
  <span class="Comment"># start-&gt;next = end</span>
  next:address:address:shared:duplex-list:_elem<span class="Special"> &lt;- </span>get-address *start, <span class="Constant">next:offset</span>
  nothing-to-delete?:boolean<span class="Special"> &lt;- </span>equal *next, end
  <span class="muControl">reply-if</span> nothing-to-delete?
  prev:address:address:shared:duplex-list:_elem<span class="Special"> &lt;- </span>get-address **next, <span class="Constant">prev:offset</span>
  *prev<span class="Special"> &lt;- </span>copy <span class="Constant">0</span>
  *next<span class="Special"> &lt;- </span>copy end
  <span class="muControl">reply-unless</span> end
  <span class="Comment"># end-&gt;prev-&gt;next = 0</span>
  <span class="Comment"># end-&gt;prev = start</span>
  prev<span class="Special"> &lt;- </span>get-address *end, <span class="Constant">prev:offset</span>
  next<span class="Special"> &lt;- </span>get-address **prev, <span class="Constant">next:offset</span>
  *next<span class="Special"> &lt;- </span>copy <span class="Constant">0</span>
  *prev<span class="Special"> &lt;- </span>copy 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">1</span>:address:shared:duplex-list:character<span class="Special"> &lt;- </span>push <span class="Constant">18</span>, <span class="Constant">0</span>
  <span class="Constant">1</span>:address:shared:duplex-list:character<span class="Special"> &lt;- </span>push <span class="Constant">17</span>, <span class="Constant">1</span>:address:shared:duplex-list:character
  <span class="Constant">1</span>:address:shared:duplex-list:character<span class="Special"> &lt;- </span>push <span class="Constant">16</span>, <span class="Constant">1</span>:address:shared:duplex-list:character
  <span class="Constant">1</span>:address:shared:duplex-list:character<span class="Special"> &lt;- </span>push <span class="Constant">15</span>, <span class="Constant">1</span>:address:shared:duplex-list:character
  <span class="Constant">1</span>:address:shared:duplex-list:character<span class="Special"> &lt;- </span>push <span class="Constant">14</span>, <span class="Constant">1</span>:address:shared:duplex-list:character
  <span class="Constant">1</span>:address:shared:duplex-list:character<span class="Special"> &lt;- </span>push <span class="Constant">13</span>, <span class="Constant">1</span>:address:shared:duplex-list:character
  run [
    <span class="Comment"># delete 16 onwards</span>
    <span class="Comment"># first pointer: to the third element</span>
    <span class="Constant">2</span>:address:shared:duplex-list:character<span class="Special"> &lt;- </span>next <span class="Constant">1</span>:address:shared:duplex-list:character
    <span class="Constant">2</span>:address:shared:duplex-list:character<span class="Special"> &lt;- </span>next <span class="Constant">2</span>:address:shared:duplex-list:character
    <span class="Constant">2</span>:address:shared:duplex-list:character<span class="Special"> &lt;- </span>remove-between <span class="Constant">2</span>:address:shared:duplex-list:character, <span class="Constant">0</span>
    <span class="Comment"># now check the list</span>
    <span class="Constant">4</span>:character<span class="Special"> &lt;- </span>get *<span class="Constant">1</span>:address:shared:duplex-list:character, <span class="Constant">value:offset</span>
    <span class="Constant">5</span>:address:shared:duplex-list:character<span class="Special"> &lt;- </span>next <span class="Constant">1</span>:address:shared:duplex-list:character
    <span class="Constant">6</span>:character<span class="Special"> &lt;- </span>get *<span class="Constant">5</span>:address:shared:duplex-list:character, <span class="Constant">value:offset</span>
    <span class="Constant">7</span>:address:shared:duplex-list:character<span class="Special"> &lt;- </span>next <span class="Constant">5</span>:address:shared:duplex-list:character
    <span class="Constant">8</span>:character<span class="Special"> &lt;- </span>get *<span class="Constant">7</span>:address:shared:duplex-list:character, <span class="Constant">value:offset</span>
    <span class="Constant">9</span>:address:shared:duplex-list:character<span class="Special"> &lt;- </span>next <span class="Constant">7</span>:address:shared:duplex-list:character
  ]
  memory-should-contain [
    <span class="Constant">4</span><span class="Special"> &lt;- </span><span class="Constant">13</span>
    <span class="Constant">6</span><span class="Special"> &lt;- </span><span class="Constant">14</span>
    <span class="Constant">8</span><span class="Special"> &lt;- </span><span class="Constant">15</span>
    <span class="Constant">9</span><span class="Special"> &lt;- </span><span class="Constant">0</span>
  ]
]

<span class="muScenario">scenario</span> remove-range-to-end [
  <span class="Comment"># construct a duplex list with six elements [13, 14, 15, 16, 17, 18]</span>
  <span class="Constant">1</span>:address:shared:duplex-list:character<span class="Special"> &lt;- </span>push <span class="Constant">18</span>, <span class="Constant">0</span>
  <span class="Constant">1</span>:address:shared:duplex-list:character<span class="Special"> &lt;- </span>push <span class="Constant">17</span>, <span class="Constant">1</span>:address:shared:duplex-list:character
  <span class="Constant">1</span>:address:shared:duplex-list:character<span class="Special"> &lt;- </span>push <span class="Constant">16</span>, <span class="Constant">1</span>:address:shared:duplex-list:character
  <span class="Constant">1</span>:address:shared:duplex-list:character<span class="Special"> &lt;- </span>push <span class="Constant">15</span>, <span class="Constant">1</span>:address:shared:duplex-list:character
  <span class="Constant">1</span>:address:shared:duplex-list:character<span class="Special"> &lt;- </span>push <span class="Constant">14</span>, <span class="Constant">1</span>:address:shared:duplex-list:character
  <span class="Constant">1</span>:address:shared:duplex-list:character<span class="Special"> &lt;- </span>push <span class="Constant">13</span>, <span class="Constant">1</span>:address:shared:duplex-list:character
  run [
    <span class="Comment"># delete 15, 16 and 17</span>
    <span class="Comment"># first pointer: to the third element</span>
    <span class="Constant">2</span>:address:shared:duplex-list:character<span class="Special"> &lt;- </span>next <span class="Constant">1</span>:address:shared:duplex-list:character
    <span class="Comment"># second pointer: to the fifth element</span>
    <span class="Constant">3</span>:address:shared:duplex-list:character<span class="Special"> &lt;- </span>next <span class="Constant">2</span>:address:shared:duplex-list:character
    <span class="Constant">3</span>:address:shared:duplex-list:character<span class="Special"> &lt;- </span>next <span class="Constant">3</span>:address:shared:duplex-list:character
    <span class="Constant">3</span>:address:shared:duplex-list:character<span class="Special"> &lt;- </span>next <span class="Constant">3</span>:address:shared:duplex-list:character
    <span class="Constant">3</span>:address:shared:duplex-list:character<span class="Special"> &lt;- </span>next <span class="Constant">3</span>:address:shared:duplex-list:character
    remove-between <span class="Constant">2</span>:address:shared:duplex-list:character, <span class="Constant">3</span>:address:shared:duplex-list:character
    <span class="Comment"># now check the list</span>
    <span class="Constant">4</span>:character<span class="Special"> &lt;- </span>get *<span class="Constant">1</span>:address:shared:duplex-list:character, <span class="Constant">value:offset</span>
    <span class="Constant">5</span>:address:shared:duplex-list:character<span class="Special"> &lt;- </span>next <span class="Constant">1</span>:address:shared:duplex-list:character
    <span class="Constant">6</span>:character<span class="Special"> &lt;- </span>get *<span class="Constant">5</span>:address:shared:duplex-list:character, <span class="Constant">value:offset</span>
    <span class="Constant">7</span>:address:shared:duplex-list:character<span class="Special"> &lt;- </span>next <span class="Constant">5</span>:address:shared:duplex-list:character
    <span class="Constant">8</span>:character<span class="Special"> &lt;- </span>get *<span class="Constant">7</span>:address:shared:duplex-list:character, <span class="Constant">value:offset</span>
    <span class="Constant">9</span>:address:shared:duplex-list:character<span class="Special"> &lt;- </span>next <span class="Constant">7</span>:address:shared:duplex-list:character
  ]
  memory-should-contain [
    <span class="Constant">4</span><span class="Special"> &lt;- </span><span class="Constant">13</span>
    <span class="Constant">6</span><span class="Special"> &lt;- </span><span class="Constant">14</span>
    <span class="Constant">8</span><span class="Special"> &lt;- </span><span class="Constant">18</span>
    <span class="Constant">9</span><span class="Special"> &lt;- </span><span class="Constant">0</span>
  ]
]

<span class="muScenario">scenario</span> remove-range-empty [
  <span class="Comment"># construct a duplex list with six elements [13, 14, 15, 16, 17, 18]</span>
  <span class="Constant">1</span>:address:shared:duplex-list:character<span class="Special"> &lt;- </span>push <span class="Constant">14</span>, <span class="Constant">0</span>
  <span class="Constant">1</span>:address:shared:duplex-list:character<span class="Special"> &lt;- </span>push <span class="Constant">13</span>, <span class="Constant">1</span>:address:shared:duplex-list:character
  run [
    <span class="Comment"># delete 16 onwards</span>
    <span class="Comment"># first pointer: to the third element</span>
    <span class="Constant">2</span>:address:shared:duplex-list:character<span class="Special"> &lt;- </span>next <span class="Constant">1</span>:address:shared:duplex-list:character
    remove-between <span class="Constant">1</span>:address:shared:duplex-list:character, <span class="Constant">2</span>:address:shared:duplex-list:character
    <span class="Comment"># now check the list</span>
    <span class="Constant">4</span>:character<span class="Special"> &lt;- </span>get *<span class="Constant">1</span>:address:shared:duplex-list:character, <span class="Constant">value:offset</span>
    <span class="Constant">5</span>:address:shared:duplex-list:character<span class="Special"> &lt;- </span>next <span class="Constant">1</span>:address:shared:duplex-list:character
    <span class="Constant">6</span>:character<span class="Special"> &lt;- </span>get *<span class="Constant">5</span>:address:shared:duplex-list:character, <span class="Constant">value:offset</span>
    <span class="Constant">7</span>:address:shared:duplex-list:character<span class="Special"> &lt;- </span>next <span class="Constant">5</span>:address:shared:duplex-list:character
  ]
  memory-should-contain [
    <span class="Constant">4</span><span class="Special"> &lt;- </span><span class="Constant">13</span>
    <span class="Constant">6</span><span class="Special"> &lt;- </span><span class="Constant">14</span>
    <span class="Constant">7</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">recipe</span> insert-range in:address:shared:duplex-list:_elem, start:address:shared:duplex-list:_elem/contained-in:in<span class="muRecipe"> -&gt; </span>in:address:shared:duplex-list:_elem [
  <span class="Constant">local-scope</span>
  <span class="Constant">load-ingredients</span>
  <span class="muControl">reply-unless</span> in
  <span class="muControl">reply-unless</span> start
  end:address:shared:duplex-list:_elem<span class="Special"> &lt;- </span>copy start
  <span class="Delimiter">{</span>
    next:address:shared: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:shared:duplex-list:_elem<span class="Special"> &lt;- </span>next in
  dest:address:address:shared:duplex-list:_elem<span class="Special"> &lt;- </span>get-address *end, <span class="Constant">next:offset</span>
  *dest<span class="Special"> &lt;- </span>copy next
  <span class="Delimiter">{</span>
    <span class="muControl">break-unless</span> next
    dest<span class="Special"> &lt;- </span>get-address *next, <span class="Constant">prev:offset</span>
    *dest<span class="Special"> &lt;- </span>copy end
  <span class="Delimiter">}</span>
  dest<span class="Special"> &lt;- </span>get-address *in, <span class="Constant">next:offset</span>
  *dest<span class="Special"> &lt;- </span>copy start
  dest<span class="Special"> &lt;- </span>get-address *start, <span class="Constant">prev:offset</span>
  *dest<span class="Special"> &lt;- </span>copy in
]

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

<span class="muRecipe">recipe</span> last in:address:shared:duplex-list:_elem<span class="muRecipe"> -&gt; </span>result:address:shared: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:shared: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">recipe</span> dump-from x:address:shared: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 : -->