about summary refs log blame commit diff stats
path: root/html/065duplex_list.mu.html
blob: bf7634a0424472b69c04e2f4cf7ab8bec416ea1f (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15














                                                                                                 


                               
                            



                              












                                                                                    
                                                   





                                                                                                              












                                                                                                                                    


                                                                                        





                                                                                                         


                                                                                                  





                                                                                                                  


                                                                                                  





                                                                                                                  

 
                                                               

                                                                                               




















                                                                                                                                                                                              

                         












                                                                                                                                                                        




                                                                                                           







                                                                                                                           
                                                   

                                                                                                                             
                                                   

                                                                                                       
                                                          

                                                                                                       
                                                         
                                                                 
                                                          


                                                                                                                                           

 
                                                                     
       





                                                                                                                                                                                                               
                                                              














                                                                                                                                                                                              

                         







                                                                                                                                                          


   
                                                                          
       






                                                                                                                                                                                                               
                                                              














                                                                                                                                                                                              

                         







                                                                                                                                                          


   
                                                                               
       




                                                                                                                                                                                             
                                                              














                                                                                                                                                                                              

                         







                                                                                                                                                          





                                                                                                          
                              

                                                                                                           


                                                                                                         
                                                        


                                                                                                                     
                                                   




                                                                                                                             
                                                           
                                                         
                                                             



                                                                                                          
                                                           
                                                         
                                                             




                                                                                                          

 
                                                                    
       






                                                                                                                                                                                                                     
                                                              







                                                                                                                                                                                             

                         





                                                                                                                                                                              


   
                                                                             
       



                                                                                                                                                                                           
                                                                            
                                                                                                                                                            
                                                              







                                                                                                                                                                                             

                         




                                                                                                                                                                              


   
                                                                           
       



                                                                                                                                                                                           
                                                      



                                                                                                                                                                         
                                                              







                                                                                                                                                                                             

                         





                                                                                                                                                                              


   
                                                                       
       




                                                                                                                                                                                              

                         


                                                                                                                                                                    






                                                                                                                                   




                                                                                                            

                                                            






                                                                                                                                   

                                                          




                                                                                                       

 
                                                       
                                                                                                   






                                                                                                                                                                                          


                                                                      


                                                                                                                                                          
                                                     





                                                                                                                                                                                  

                         



                                                                                                       


   
                                                              
                                                                                                   






                                                                                                                                                                                          


                                                                      
                                                                                                                                                          
                                                                       




                                                                                                                                                          
                                                     





                                                                                                                                                                                  

                         



                                                                                                       


   
                                                             
                                                                                                   


                                                                                                                                                                                          
       

                                                                      

                                                                                                                                                          
                                                     



                                                                                                                                                                                  

                         


                                                                                                       

   


                                                                                                                              







                                                                                                            
                                                                               
















                                                                                                                                 

 










                                                                                                                                  

 




                                                                                                         
                                                                                  




                                                       


                                                   







                                                                                                        
                                                      








                                                                                                                                
 



                                     
<!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_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; }
.muRecipe { color: #ff8700; }
.muData { color: #ffff00; }
.muScenario { color: #00af00; }
.Comment { color: #9090ff; }
.Constant { color: #00a0a0; }
.Special { color: #ff6060; }
.Delimiter { color: #a04060; }
.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 [
  value:location
  next:address:duplex-list
  prev:address:duplex-list
]

<span class="Comment"># result:address:duplex-list &lt;- push-duplex x:location, in:address:duplex-list</span>
<span class="muRecipe">recipe</span> push-duplex [
  <span class="Constant">local-scope</span>
  x:location<span class="Special"> &lt;- </span><span class="Constant">next-ingredient</span>
  in:address:duplex-list<span class="Special"> &lt;- </span><span class="Constant">next-ingredient</span>
  result:address:duplex-list<span class="Special"> &lt;- </span>new <span class="Constant">duplex-list:type</span>
  val:address:location<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:duplex-list<span class="Special"> &lt;- </span>get-address *result, <span class="Constant">next:offset</span>
  *next<span class="Special"> &lt;- </span>copy in
  <span class="muControl">reply-unless</span> in, result
  prev:address:address:duplex-list<span class="Special"> &lt;- </span>get-address *in, <span class="Constant">prev:offset</span>
  *prev<span class="Special"> &lt;- </span>copy result
  <span class="muControl">reply</span> result
]

<span class="Comment"># result:location &lt;- first-duplex in:address:duplex-list</span>
<span class="muRecipe">recipe</span> first-duplex [
  <span class="Constant">local-scope</span>
  in:address:duplex-list<span class="Special"> &lt;- </span><span class="Constant">next-ingredient</span>
  <span class="muControl">reply-unless</span> in, <span class="Constant">0</span>
  result:location<span class="Special"> &lt;- </span>get *in, <span class="Constant">value:offset</span>
  <span class="muControl">reply</span> result
]

<span class="Comment"># result:address:duplex-list &lt;- next-duplex in:address:duplex-list</span>
<span class="muRecipe">recipe</span> next-duplex [
  <span class="Constant">local-scope</span>
  in:address:duplex-list<span class="Special"> &lt;- </span><span class="Constant">next-ingredient</span>
  <span class="muControl">reply-unless</span> in, <span class="Constant">0</span>
  result:address:duplex-list<span class="Special"> &lt;- </span>get *in, <span class="Constant">next:offset</span>
  <span class="muControl">reply</span> result
]

<span class="Comment"># result:address:duplex-list &lt;- prev-duplex in:address:duplex-list</span>
<span class="muRecipe">recipe</span> prev-duplex [
  <span class="Constant">local-scope</span>
  in:address:duplex-list<span class="Special"> &lt;- </span><span class="Constant">next-ingredient</span>
  <span class="muControl">reply-unless</span> in, <span class="Constant">0</span>
  result:address:duplex-list<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:duplex-list<span class="Special"> &lt;- </span>copy <span class="Constant">0</span>
    <span class="Constant">3</span>:address:duplex-list<span class="Special"> &lt;- </span>push-duplex <span class="Constant">3</span>, <span class="Constant">3</span>:address:duplex-list
    <span class="Constant">3</span>:address:duplex-list<span class="Special"> &lt;- </span>push-duplex <span class="Constant">4</span>, <span class="Constant">3</span>:address:duplex-list
    <span class="Constant">3</span>:address:duplex-list<span class="Special"> &lt;- </span>push-duplex <span class="Constant">5</span>, <span class="Constant">3</span>:address:duplex-list
    <span class="Constant">4</span>:address:duplex-list<span class="Special"> &lt;- </span>copy <span class="Constant">3</span>:address:duplex-list
    <span class="Constant">5</span>:number<span class="Special"> &lt;- </span>first-duplex <span class="Constant">4</span>:address:duplex-list
    <span class="Constant">4</span>:address:duplex-list<span class="Special"> &lt;- </span>next-duplex <span class="Constant">4</span>:address:duplex-list
    <span class="Constant">6</span>:number<span class="Special"> &lt;- </span>first-duplex <span class="Constant">4</span>:address:duplex-list
    <span class="Constant">4</span>:address:duplex-list<span class="Special"> &lt;- </span>next-duplex <span class="Constant">4</span>:address:duplex-list
    <span class="Constant">7</span>:number<span class="Special"> &lt;- </span>first-duplex <span class="Constant">4</span>:address:duplex-list
    <span class="Constant">8</span>:address:duplex-list<span class="Special"> &lt;- </span>next-duplex <span class="Constant">4</span>:address:duplex-list
    <span class="Constant">9</span>:number<span class="Special"> &lt;- </span>first-duplex <span class="Constant">8</span>:address:duplex-list
    <span class="Constant">10</span>:address:duplex-list<span class="Special"> &lt;- </span>next-duplex <span class="Constant">8</span>:address:duplex-list
    <span class="Constant">11</span>:address:duplex-list<span class="Special"> &lt;- </span>prev-duplex <span class="Constant">8</span>:address:duplex-list
    <span class="Constant">4</span>:address:duplex-list<span class="Special"> &lt;- </span>prev-duplex <span class="Constant">4</span>:address:duplex-list
    <span class="Constant">12</span>:number<span class="Special"> &lt;- </span>first-duplex <span class="Constant">4</span>:address:duplex-list
    <span class="Constant">4</span>:address:duplex-list<span class="Special"> &lt;- </span>prev-duplex <span class="Constant">4</span>:address:duplex-list
    <span class="Constant">13</span>:number<span class="Special"> &lt;- </span>first-duplex <span class="Constant">4</span>:address:duplex-list
    <span class="Constant">14</span>:boolean<span class="Special"> &lt;- </span>equal <span class="Constant">3</span>:address:duplex-list, <span class="Constant">4</span>:address:duplex-list
  ]
  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"># l:address:duplex-list &lt;- insert-duplex x:location, in:address:duplex-list</span>
<span class="Comment"># Inserts 'x' after 'in'. Returns some pointer into the list.</span>
<span class="muRecipe">recipe</span> insert-duplex [
  <span class="Constant">local-scope</span>
  x:location<span class="Special"> &lt;- </span><span class="Constant">next-ingredient</span>
  in:address:duplex-list<span class="Special"> &lt;- </span><span class="Constant">next-ingredient</span>
  new-node:address:duplex-list<span class="Special"> &lt;- </span>new <span class="Constant">duplex-list:type</span>
  val:address:location<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:duplex-list<span class="Special"> &lt;- </span>get *in, <span class="Constant">next:offset</span>
  <span class="Comment"># in.next = new-node</span>
  y:address:address:duplex-list<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, new-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="muControl">reply</span> new-node  <span class="Comment"># just signalling something changed; don't rely on the result</span>
]

<span class="muScenario">scenario</span> inserting-into-duplex-list [
  run [
    <span class="Constant">1</span>:address:duplex-list<span class="Special"> &lt;- </span>copy <span class="Constant">0</span>  <span class="Comment"># 1 points to head of list</span>
    <span class="Constant">1</span>:address:duplex-list<span class="Special"> &lt;- </span>push-duplex <span class="Constant">3</span>, <span class="Constant">1</span>:address:duplex-list
    <span class="Constant">1</span>:address:duplex-list<span class="Special"> &lt;- </span>push-duplex <span class="Constant">4</span>, <span class="Constant">1</span>:address:duplex-list
    <span class="Constant">1</span>:address:duplex-list<span class="Special"> &lt;- </span>push-duplex <span class="Constant">5</span>, <span class="Constant">1</span>:address:duplex-list
    <span class="Constant">2</span>:address:duplex-list<span class="Special"> &lt;- </span>next-duplex <span class="Constant">1</span>:address:duplex-list  <span class="Comment"># 2 points inside list</span>
    <span class="Constant">2</span>:address:duplex-list<span class="Special"> &lt;- </span>insert-duplex <span class="Constant">6</span>, <span class="Constant">2</span>:address:duplex-list
    <span class="Comment"># check structure like before</span>
    <span class="Constant">2</span>:address:duplex-list<span class="Special"> &lt;- </span>copy <span class="Constant">1</span>:address:duplex-list
    <span class="Constant">3</span>:number<span class="Special"> &lt;- </span>first-duplex <span class="Constant">2</span>:address:duplex-list
    <span class="Constant">2</span>:address:duplex-list<span class="Special"> &lt;- </span>next-duplex <span class="Constant">2</span>:address:duplex-list
    <span class="Constant">4</span>:number<span class="Special"> &lt;- </span>first-duplex <span class="Constant">2</span>:address:duplex-list
    <span class="Constant">2</span>:address:duplex-list<span class="Special"> &lt;- </span>next-duplex <span class="Constant">2</span>:address:duplex-list
    <span class="Constant">5</span>:number<span class="Special"> &lt;- </span>first-duplex <span class="Constant">2</span>:address:duplex-list
    <span class="Constant">2</span>:address:duplex-list<span class="Special"> &lt;- </span>next-duplex <span class="Constant">2</span>:address:duplex-list
    <span class="Constant">6</span>:number<span class="Special"> &lt;- </span>first-duplex <span class="Constant">2</span>:address:duplex-list
    <span class="Constant">2</span>:address:duplex-list<span class="Special"> &lt;- </span>prev-duplex <span class="Constant">2</span>:address:duplex-list
    <span class="Constant">7</span>:number<span class="Special"> &lt;- </span>first-duplex <span class="Constant">2</span>:address:duplex-list
    <span class="Constant">2</span>:address:duplex-list<span class="Special"> &lt;- </span>prev-duplex <span class="Constant">2</span>:address:duplex-list
    <span class="Constant">8</span>:number<span class="Special"> &lt;- </span>first-duplex <span class="Constant">2</span>:address:duplex-list
    <span class="Constant">2</span>:address:duplex-list<span class="Special"> &lt;- </span>prev-duplex <span class="Constant">2</span>:address:duplex-list
    <span class="Constant">9</span>:number<span class="Special"> &lt;- </span>first-duplex <span class="Constant">2</span>:address:duplex-list
    <span class="Constant">10</span>:boolean<span class="Special"> &lt;- </span>equal <span class="Constant">1</span>:address:duplex-list, <span class="Constant">2</span>:address:duplex-list
  ]
  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:duplex-list<span class="Special"> &lt;- </span>copy <span class="Constant">0</span>  <span class="Comment"># 1 points to head of list</span>
    <span class="Constant">1</span>:address:duplex-list<span class="Special"> &lt;- </span>push-duplex <span class="Constant">3</span>, <span class="Constant">1</span>:address:duplex-list
    <span class="Constant">1</span>:address:duplex-list<span class="Special"> &lt;- </span>push-duplex <span class="Constant">4</span>, <span class="Constant">1</span>:address:duplex-list
    <span class="Constant">1</span>:address:duplex-list<span class="Special"> &lt;- </span>push-duplex <span class="Constant">5</span>, <span class="Constant">1</span>:address:duplex-list
    <span class="Constant">2</span>:address:duplex-list<span class="Special"> &lt;- </span>next-duplex <span class="Constant">1</span>:address:duplex-list  <span class="Comment"># 2 points inside list</span>
    <span class="Constant">2</span>:address:duplex-list<span class="Special"> &lt;- </span>next-duplex <span class="Constant">2</span>:address:duplex-list  <span class="Comment"># now at end of list</span>
    <span class="Constant">2</span>:address:duplex-list<span class="Special"> &lt;- </span>insert-duplex <span class="Constant">6</span>, <span class="Constant">2</span>:address:duplex-list
    <span class="Comment"># check structure like before</span>
    <span class="Constant">2</span>:address:duplex-list<span class="Special"> &lt;- </span>copy <span class="Constant">1</span>:address:duplex-list
    <span class="Constant">3</span>:number<span class="Special"> &lt;- </span>first-duplex <span class="Constant">2</span>:address:duplex-list
    <span class="Constant">2</span>:address:duplex-list<span class="Special"> &lt;- </span>next-duplex <span class="Constant">2</span>:address:duplex-list
    <span class="Constant">4</span>:number<span class="Special"> &lt;- </span>first-duplex <span class="Constant">2</span>:address:duplex-list
    <span class="Constant">2</span>:address:duplex-list<span class="Special"> &lt;- </span>next-duplex <span class="Constant">2</span>:address:duplex-list
    <span class="Constant">5</span>:number<span class="Special"> &lt;- </span>first-duplex <span class="Constant">2</span>:address:duplex-list
    <span class="Constant">2</span>:address:duplex-list<span class="Special"> &lt;- </span>next-duplex <span class="Constant">2</span>:address:duplex-list
    <span class="Constant">6</span>:number<span class="Special"> &lt;- </span>first-duplex <span class="Constant">2</span>:address:duplex-list
    <span class="Constant">2</span>:address:duplex-list<span class="Special"> &lt;- </span>prev-duplex <span class="Constant">2</span>:address:duplex-list
    <span class="Constant">7</span>:number<span class="Special"> &lt;- </span>first-duplex <span class="Constant">2</span>:address:duplex-list
    <span class="Constant">2</span>:address:duplex-list<span class="Special"> &lt;- </span>prev-duplex <span class="Constant">2</span>:address:duplex-list
    <span class="Constant">8</span>:number<span class="Special"> &lt;- </span>first-duplex <span class="Constant">2</span>:address:duplex-list
    <span class="Constant">2</span>:address:duplex-list<span class="Special"> &lt;- </span>prev-duplex <span class="Constant">2</span>:address:duplex-list
    <span class="Constant">9</span>:number<span class="Special"> &lt;- </span>first-duplex <span class="Constant">2</span>:address:duplex-list
    <span class="Constant">10</span>:boolean<span class="Special"> &lt;- </span>equal <span class="Constant">1</span>:address:duplex-list, <span class="Constant">2</span>:address:duplex-list
  ]
  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:duplex-list<span class="Special"> &lt;- </span>copy <span class="Constant">0</span>  <span class="Comment"># 1 points to head of list</span>
    <span class="Constant">1</span>:address:duplex-list<span class="Special"> &lt;- </span>push-duplex <span class="Constant">3</span>, <span class="Constant">1</span>:address:duplex-list
    <span class="Constant">1</span>:address:duplex-list<span class="Special"> &lt;- </span>push-duplex <span class="Constant">4</span>, <span class="Constant">1</span>:address:duplex-list
    <span class="Constant">1</span>:address:duplex-list<span class="Special"> &lt;- </span>push-duplex <span class="Constant">5</span>, <span class="Constant">1</span>:address:duplex-list
    <span class="Constant">2</span>:address:duplex-list<span class="Special"> &lt;- </span>insert-duplex <span class="Constant">6</span>, <span class="Constant">1</span>:address:duplex-list
    <span class="Comment"># check structure like before</span>
    <span class="Constant">2</span>:address:duplex-list<span class="Special"> &lt;- </span>copy <span class="Constant">1</span>:address:duplex-list
    <span class="Constant">3</span>:number<span class="Special"> &lt;- </span>first-duplex <span class="Constant">2</span>:address:duplex-list
    <span class="Constant">2</span>:address:duplex-list<span class="Special"> &lt;- </span>next-duplex <span class="Constant">2</span>:address:duplex-list
    <span class="Constant">4</span>:number<span class="Special"> &lt;- </span>first-duplex <span class="Constant">2</span>:address:duplex-list
    <span class="Constant">2</span>:address:duplex-list<span class="Special"> &lt;- </span>next-duplex <span class="Constant">2</span>:address:duplex-list
    <span class="Constant">5</span>:number<span class="Special"> &lt;- </span>first-duplex <span class="Constant">2</span>:address:duplex-list
    <span class="Constant">2</span>:address:duplex-list<span class="Special"> &lt;- </span>next-duplex <span class="Constant">2</span>:address:duplex-list
    <span class="Constant">6</span>:number<span class="Special"> &lt;- </span>first-duplex <span class="Constant">2</span>:address:duplex-list
    <span class="Constant">2</span>:address:duplex-list<span class="Special"> &lt;- </span>prev-duplex <span class="Constant">2</span>:address:duplex-list
    <span class="Constant">7</span>:number<span class="Special"> &lt;- </span>first-duplex <span class="Constant">2</span>:address:duplex-list
    <span class="Constant">2</span>:address:duplex-list<span class="Special"> &lt;- </span>prev-duplex <span class="Constant">2</span>:address:duplex-list
    <span class="Constant">8</span>:number<span class="Special"> &lt;- </span>first-duplex <span class="Constant">2</span>:address:duplex-list
    <span class="Constant">2</span>:address:duplex-list<span class="Special"> &lt;- </span>prev-duplex <span class="Constant">2</span>:address:duplex-list
    <span class="Constant">9</span>:number<span class="Special"> &lt;- </span>first-duplex <span class="Constant">2</span>:address:duplex-list
    <span class="Constant">10</span>:boolean<span class="Special"> &lt;- </span>equal <span class="Constant">1</span>:address:duplex-list, <span class="Constant">2</span>:address:duplex-list
  ]
  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"># l:address:duplex-list &lt;- remove-duplex in:address:duplex-list</span>
<span class="Comment"># Removes 'in' from its surrounding list. Returns some valid pointer into the</span>
<span class="Comment"># rest of the list.</span>
<span class="Comment">#</span>
<span class="Comment"># Returns null if and only if list is empty. Beware: in that case any pointers</span>
<span class="Comment"># to the head are now invalid.</span>
<span class="muRecipe">recipe</span> remove-duplex [
  <span class="Constant">local-scope</span>
  in:address:duplex-list<span class="Special"> &lt;- </span><span class="Constant">next-ingredient</span>
  <span class="Comment"># if 'in' is null, return</span>
  <span class="muControl">reply-unless</span> in, in
  next-node:address:duplex-list<span class="Special"> &lt;- </span>get *in, <span class="Constant">next:offset</span>
  prev-node:address:duplex-list<span class="Special"> &lt;- </span>get *in, <span class="Constant">prev:offset</span>
  <span class="Comment"># null in's pointers</span>
  x:address:address:duplex-list<span class="Special"> &lt;- </span>get-address *in, <span class="Constant">next:offset</span>
  *x<span class="Special"> &lt;- </span>copy <span class="Constant">0</span>
  x<span class="Special"> &lt;- </span>get-address *in, <span class="Constant">prev:offset</span>
  *x<span class="Special"> &lt;- </span>copy <span class="Constant">0</span>
  <span class="Delimiter">{</span>
    <span class="Comment"># if next-node is not null</span>
    <span class="muControl">break-unless</span> next-node
    <span class="Comment"># next-node.prev = prev-node</span>
    x<span class="Special"> &lt;- </span>get-address *next-node, <span class="Constant">prev:offset</span>
    *x<span class="Special"> &lt;- </span>copy prev-node
  <span class="Delimiter">}</span>
  <span class="Delimiter">{</span>
    <span class="Comment"># if prev-node is not null</span>
    <span class="muControl">break-unless</span> prev-node
    <span class="Comment"># prev-node.next = next-node</span>
    x<span class="Special"> &lt;- </span>get-address *prev-node, <span class="Constant">next:offset</span>
    *x<span class="Special"> &lt;- </span>copy next-node
    <span class="muControl">reply</span> prev-node
  <span class="Delimiter">}</span>
  <span class="muControl">reply</span> next-node
]

<span class="muScenario">scenario</span> removing-from-duplex-list [
  run [
    <span class="Constant">1</span>:address:duplex-list<span class="Special"> &lt;- </span>copy <span class="Constant">0</span>  <span class="Comment"># 1 points to head of list</span>
    <span class="Constant">1</span>:address:duplex-list<span class="Special"> &lt;- </span>push-duplex <span class="Constant">3</span>, <span class="Constant">1</span>:address:duplex-list
    <span class="Constant">1</span>:address:duplex-list<span class="Special"> &lt;- </span>push-duplex <span class="Constant">4</span>, <span class="Constant">1</span>:address:duplex-list
    <span class="Constant">1</span>:address:duplex-list<span class="Special"> &lt;- </span>push-duplex <span class="Constant">5</span>, <span class="Constant">1</span>:address:duplex-list
    <span class="Constant">2</span>:address:duplex-list<span class="Special"> &lt;- </span>next-duplex <span class="Constant">1</span>:address:duplex-list  <span class="Comment"># 2 points at second element</span>
    <span class="Constant">2</span>:address:duplex-list<span class="Special"> &lt;- </span>remove-duplex <span class="Constant">2</span>:address:duplex-list
    <span class="Constant">3</span>:boolean<span class="Special"> &lt;- </span>equal <span class="Constant">2</span>:address:duplex-list, <span class="Constant">0</span>
    <span class="Comment"># check structure like before</span>
    <span class="Constant">2</span>:address:duplex-list<span class="Special"> &lt;- </span>copy <span class="Constant">1</span>:address:duplex-list
    <span class="Constant">4</span>:number<span class="Special"> &lt;- </span>first-duplex <span class="Constant">2</span>:address:duplex-list
    <span class="Constant">2</span>:address:duplex-list<span class="Special"> &lt;- </span>next-duplex <span class="Constant">2</span>:address:duplex-list
    <span class="Constant">5</span>:number<span class="Special"> &lt;- </span>first-duplex <span class="Constant">2</span>:address:duplex-list
    <span class="Constant">6</span>:address:duplex-list<span class="Special"> &lt;- </span>next-duplex <span class="Constant">2</span>:address:duplex-list
    <span class="Constant">2</span>:address:duplex-list<span class="Special"> &lt;- </span>prev-duplex <span class="Constant">2</span>:address:duplex-list
    <span class="Constant">7</span>:number<span class="Special"> &lt;- </span>first-duplex <span class="Constant">2</span>:address:duplex-list
    <span class="Constant">8</span>:boolean<span class="Special"> &lt;- </span>equal <span class="Constant">1</span>:address:duplex-list, <span class="Constant">2</span>:address:duplex-list
  ]
  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:duplex-list<span class="Special"> &lt;- </span>copy <span class="Constant">0</span>  <span class="Comment"># 1 points to head of list</span>
    <span class="Constant">1</span>:address:duplex-list<span class="Special"> &lt;- </span>push-duplex <span class="Constant">3</span>, <span class="Constant">1</span>:address:duplex-list
    <span class="Constant">1</span>:address:duplex-list<span class="Special"> &lt;- </span>push-duplex <span class="Constant">4</span>, <span class="Constant">1</span>:address:duplex-list
    <span class="Constant">1</span>:address:duplex-list<span class="Special"> &lt;- </span>push-duplex <span class="Constant">5</span>, <span class="Constant">1</span>:address:duplex-list
    <span class="Comment"># removing from head? return value matters.</span>
    <span class="Constant">1</span>:address:duplex-list<span class="Special"> &lt;- </span>remove-duplex <span class="Constant">1</span>:address:duplex-list
    <span class="Comment"># check structure like before</span>
    <span class="Constant">2</span>:address:duplex-list<span class="Special"> &lt;- </span>copy <span class="Constant">1</span>:address:duplex-list
    <span class="Constant">3</span>:number<span class="Special"> &lt;- </span>first-duplex <span class="Constant">2</span>:address:duplex-list
    <span class="Constant">2</span>:address:duplex-list<span class="Special"> &lt;- </span>next-duplex <span class="Constant">2</span>:address:duplex-list
    <span class="Constant">4</span>:number<span class="Special"> &lt;- </span>first-duplex <span class="Constant">2</span>:address:duplex-list
    <span class="Constant">5</span>:address:duplex-list<span class="Special"> &lt;- </span>next-duplex <span class="Constant">2</span>:address:duplex-list
    <span class="Constant">2</span>:address:duplex-list<span class="Special"> &lt;- </span>prev-duplex <span class="Constant">2</span>:address:duplex-list
    <span class="Constant">6</span>:number<span class="Special"> &lt;- </span>first-duplex <span class="Constant">2</span>:address:duplex-list
    <span class="Constant">7</span>:boolean<span class="Special"> &lt;- </span>equal <span class="Constant">1</span>:address:duplex-list, <span class="Constant">2</span>:address:duplex-list
  ]
  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:duplex-list<span class="Special"> &lt;- </span>copy <span class="Constant">0</span>  <span class="Comment"># 1 points to head of list</span>
    <span class="Constant">1</span>:address:duplex-list<span class="Special"> &lt;- </span>push-duplex <span class="Constant">3</span>, <span class="Constant">1</span>:address:duplex-list
    <span class="Constant">1</span>:address:duplex-list<span class="Special"> &lt;- </span>push-duplex <span class="Constant">4</span>, <span class="Constant">1</span>:address:duplex-list
    <span class="Constant">1</span>:address:duplex-list<span class="Special"> &lt;- </span>push-duplex <span class="Constant">5</span>, <span class="Constant">1</span>:address:duplex-list
    <span class="Comment"># delete last element</span>
    <span class="Constant">2</span>:address:duplex-list<span class="Special"> &lt;- </span>next-duplex <span class="Constant">1</span>:address:duplex-list
    <span class="Constant">2</span>:address:duplex-list<span class="Special"> &lt;- </span>next-duplex <span class="Constant">2</span>:address:duplex-list
    <span class="Constant">2</span>:address:duplex-list<span class="Special"> &lt;- </span>remove-duplex <span class="Constant">2</span>:address:duplex-list
    <span class="Constant">3</span>:boolean<span class="Special"> &lt;- </span>equal <span class="Constant">2</span>:address:duplex-list, <span class="Constant">0</span>
    <span class="Comment"># check structure like before</span>
    <span class="Constant">2</span>:address:duplex-list<span class="Special"> &lt;- </span>copy <span class="Constant">1</span>:address:duplex-list
    <span class="Constant">4</span>:number<span class="Special"> &lt;- </span>first-duplex <span class="Constant">2</span>:address:duplex-list
    <span class="Constant">2</span>:address:duplex-list<span class="Special"> &lt;- </span>next-duplex <span class="Constant">2</span>:address:duplex-list
    <span class="Constant">5</span>:number<span class="Special"> &lt;- </span>first-duplex <span class="Constant">2</span>:address:duplex-list
    <span class="Constant">6</span>:address:duplex-list<span class="Special"> &lt;- </span>next-duplex <span class="Constant">2</span>:address:duplex-list
    <span class="Constant">2</span>:address:duplex-list<span class="Special"> &lt;- </span>prev-duplex <span class="Constant">2</span>:address:duplex-list
    <span class="Constant">7</span>:number<span class="Special"> &lt;- </span>first-duplex <span class="Constant">2</span>:address:duplex-list
    <span class="Constant">8</span>:boolean<span class="Special"> &lt;- </span>equal <span class="Constant">1</span>:address:duplex-list, <span class="Constant">2</span>:address:duplex-list
  ]
  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:duplex-list<span class="Special"> &lt;- </span>copy <span class="Constant">0</span>  <span class="Comment"># 1 points to singleton list</span>
    <span class="Constant">1</span>:address:duplex-list<span class="Special"> &lt;- </span>push-duplex <span class="Constant">3</span>, <span class="Constant">1</span>:address:duplex-list
    <span class="Constant">2</span>:address:duplex-list<span class="Special"> &lt;- </span>remove-duplex <span class="Constant">1</span>:address:duplex-list
    <span class="Constant">3</span>:address:duplex-list<span class="Special"> &lt;- </span>get *<span class="Constant">1</span>:address:duplex-list, <span class="Constant">next:offset</span>
    <span class="Constant">4</span>:address:duplex-list<span class="Special"> &lt;- </span>get *<span class="Constant">1</span>:address:duplex-list, <span class="Constant">prev:offset</span>
  ]
  memory-should-contain [
    <span class="Constant">2</span><span class="Special"> &lt;- </span><span class="Constant">0</span>  <span class="Comment"># remove returned null</span>
    <span class="Constant">3</span><span class="Special"> &lt;- </span><span class="Constant">0</span>  <span class="Comment"># removed node is also detached</span>
    <span class="Constant">4</span><span class="Special"> &lt;- </span><span class="Constant">0</span>
  ]
]

<span class="Comment"># l:address:duplex-list &lt;- remove-duplex-between start:address:duplex-list, end:address:duplex-list</span>
<span class="Comment"># Remove values between 'start' and 'end' (both exclusive). Returns some valid</span>
<span class="Comment"># pointer into the rest of the list.</span>
<span class="Comment"># Also clear pointers back out from start/end for hygiene.</span>
<span class="muRecipe">recipe</span> remove-duplex-between [
  <span class="Constant">local-scope</span>
  start:address:duplex-list<span class="Special"> &lt;- </span><span class="Constant">next-ingredient</span>
  end:address:duplex-list<span class="Special"> &lt;- </span><span class="Constant">next-ingredient</span>
  <span class="muControl">reply-unless</span> start, start
  <span class="Comment"># start-&gt;next-&gt;prev = 0</span>
  <span class="Comment"># start-&gt;next = end</span>
  next:address:address:duplex-list<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?, start
  prev:address:address:duplex-list<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, start
  <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="muControl">reply</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">1</span>:address:duplex-list<span class="Special"> &lt;- </span>copy <span class="Constant">0</span>  <span class="Comment"># 1 points to singleton list</span>
  <span class="Constant">1</span>:address:duplex-list<span class="Special"> &lt;- </span>push-duplex <span class="Constant">18</span>, <span class="Constant">1</span>:address:duplex-list
  <span class="Constant">1</span>:address:duplex-list<span class="Special"> &lt;- </span>push-duplex <span class="Constant">17</span>, <span class="Constant">1</span>:address:duplex-list
  <span class="Constant">1</span>:address:duplex-list<span class="Special"> &lt;- </span>push-duplex <span class="Constant">16</span>, <span class="Constant">1</span>:address:duplex-list
  <span class="Constant">1</span>:address:duplex-list<span class="Special"> &lt;- </span>push-duplex <span class="Constant">15</span>, <span class="Constant">1</span>:address:duplex-list
  <span class="Constant">1</span>:address:duplex-list<span class="Special"> &lt;- </span>push-duplex <span class="Constant">14</span>, <span class="Constant">1</span>:address:duplex-list
  <span class="Constant">1</span>:address:duplex-list<span class="Special"> &lt;- </span>push-duplex <span class="Constant">13</span>, <span class="Constant">1</span>:address:duplex-list
  run [
    <span class="Comment"># delete 16 onwards</span>
    <span class="Comment"># first pointer: to the third element</span>
    <span class="Constant">2</span>:address:duplex-list<span class="Special"> &lt;- </span>next-duplex <span class="Constant">1</span>:address:duplex-list
    <span class="Constant">2</span>:address:duplex-list<span class="Special"> &lt;- </span>next-duplex <span class="Constant">2</span>:address:duplex-list
    remove-duplex-between <span class="Constant">2</span>:address:duplex-list, <span class="Constant">0</span>
    <span class="Comment"># now check the list</span>
    <span class="Constant">4</span>:number<span class="Special"> &lt;- </span>get *<span class="Constant">1</span>:address:duplex-list, <span class="Constant">value:offset</span>
    <span class="Constant">5</span>:address:duplex-list<span class="Special"> &lt;- </span>next-duplex <span class="Constant">1</span>:address:duplex-list
    <span class="Constant">6</span>:number<span class="Special"> &lt;- </span>get *<span class="Constant">5</span>:address:duplex-list, <span class="Constant">value:offset</span>
    <span class="Constant">7</span>:address:duplex-list<span class="Special"> &lt;- </span>next-duplex <span class="Constant">5</span>:address:duplex-list
    <span class="Constant">8</span>:number<span class="Special"> &lt;- </span>get *<span class="Constant">7</span>:address:duplex-list, <span class="Constant">value:offset</span>
    <span class="Constant">9</span>:address:duplex-list<span class="Special"> &lt;- </span>next-duplex <span class="Constant">7</span>:address:duplex-list
  ]
  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:duplex-list<span class="Special"> &lt;- </span>copy <span class="Constant">0</span>  <span class="Comment"># 1 points to singleton list</span>
  <span class="Constant">1</span>:address:duplex-list<span class="Special"> &lt;- </span>push-duplex <span class="Constant">18</span>, <span class="Constant">1</span>:address:duplex-list
  <span class="Constant">1</span>:address:duplex-list<span class="Special"> &lt;- </span>push-duplex <span class="Constant">17</span>, <span class="Constant">1</span>:address:duplex-list
  <span class="Constant">1</span>:address:duplex-list<span class="Special"> &lt;- </span>push-duplex <span class="Constant">16</span>, <span class="Constant">1</span>:address:duplex-list
  <span class="Constant">1</span>:address:duplex-list<span class="Special"> &lt;- </span>push-duplex <span class="Constant">15</span>, <span class="Constant">1</span>:address:duplex-list
  <span class="Constant">1</span>:address:duplex-list<span class="Special"> &lt;- </span>push-duplex <span class="Constant">14</span>, <span class="Constant">1</span>:address:duplex-list
  <span class="Constant">1</span>:address:duplex-list<span class="Special"> &lt;- </span>push-duplex <span class="Constant">13</span>, <span class="Constant">1</span>:address:duplex-list
  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:duplex-list<span class="Special"> &lt;- </span>next-duplex <span class="Constant">1</span>:address:duplex-list
    <span class="Comment"># second pointer: to the fifth element</span>
    <span class="Constant">3</span>:address:duplex-list<span class="Special"> &lt;- </span>next-duplex <span class="Constant">2</span>:address:duplex-list
    <span class="Constant">3</span>:address:duplex-list<span class="Special"> &lt;- </span>next-duplex <span class="Constant">3</span>:address:duplex-list
    <span class="Constant">3</span>:address:duplex-list<span class="Special"> &lt;- </span>next-duplex <span class="Constant">3</span>:address:duplex-list
    <span class="Constant">3</span>:address:duplex-list<span class="Special"> &lt;- </span>next-duplex <span class="Constant">3</span>:address:duplex-list
    remove-duplex-between <span class="Constant">2</span>:address:duplex-list, <span class="Constant">3</span>:address:duplex-list
    <span class="Comment"># now check the list</span>
    <span class="Constant">4</span>:number<span class="Special"> &lt;- </span>get *<span class="Constant">1</span>:address:duplex-list, <span class="Constant">value:offset</span>
    <span class="Constant">5</span>:address:duplex-list<span class="Special"> &lt;- </span>next-duplex <span class="Constant">1</span>:address:duplex-list
    <span class="Constant">6</span>:number<span class="Special"> &lt;- </span>get *<span class="Constant">5</span>:address:duplex-list, <span class="Constant">value:offset</span>
    <span class="Constant">7</span>:address:duplex-list<span class="Special"> &lt;- </span>next-duplex <span class="Constant">5</span>:address:duplex-list
    <span class="Constant">8</span>:number<span class="Special"> &lt;- </span>get *<span class="Constant">7</span>:address:duplex-list, <span class="Constant">value:offset</span>
    <span class="Constant">9</span>:address:duplex-list<span class="Special"> &lt;- </span>next-duplex <span class="Constant">7</span>:address:duplex-list
  ]
  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:duplex-list<span class="Special"> &lt;- </span>copy <span class="Constant">0</span>  <span class="Comment"># 1 points to singleton list</span>
  <span class="Constant">1</span>:address:duplex-list<span class="Special"> &lt;- </span>push-duplex <span class="Constant">14</span>, <span class="Constant">1</span>:address:duplex-list
  <span class="Constant">1</span>:address:duplex-list<span class="Special"> &lt;- </span>push-duplex <span class="Constant">13</span>, <span class="Constant">1</span>:address:duplex-list
  run [
    <span class="Comment"># delete 16 onwards</span>
    <span class="Comment"># first pointer: to the third element</span>
    <span class="Constant">2</span>:address:duplex-list<span class="Special"> &lt;- </span>next-duplex <span class="Constant">1</span>:address:duplex-list
    remove-duplex-between <span class="Constant">1</span>:address:duplex-list, <span class="Constant">2</span>:address:duplex-list
    <span class="Comment"># now check the list</span>
    <span class="Constant">4</span>:number<span class="Special"> &lt;- </span>get *<span class="Constant">1</span>:address:duplex-list, <span class="Constant">value:offset</span>
    <span class="Constant">5</span>:address:duplex-list<span class="Special"> &lt;- </span>next-duplex <span class="Constant">1</span>:address:duplex-list
    <span class="Constant">6</span>:number<span class="Special"> &lt;- </span>get *<span class="Constant">5</span>:address:duplex-list, <span class="Constant">value:offset</span>
    <span class="Constant">7</span>:address:duplex-list<span class="Special"> &lt;- </span>next-duplex <span class="Constant">5</span>:address:duplex-list
  ]
  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"># l:address:duplex-list &lt;- insert-duplex-range in:address:duplex-list, new:address:duplex-list</span>
<span class="Comment"># Inserts list beginning at 'new' after 'in'. Returns some pointer into the list.</span>
<span class="muRecipe">recipe</span> insert-duplex-range [
  <span class="Constant">local-scope</span>
  in:address:duplex-list<span class="Special"> &lt;- </span><span class="Constant">next-ingredient</span>
  start:address:duplex-list<span class="Special"> &lt;- </span><span class="Constant">next-ingredient</span>
  <span class="muControl">reply-unless</span> in, in
  <span class="muControl">reply-unless</span> start, in
  end:address:duplex-list<span class="Special"> &lt;- </span>copy start
  <span class="Delimiter">{</span>
    next:address:duplex-list<span class="Special"> &lt;- </span>next-duplex end
    <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<span class="Special"> &lt;- </span>next-duplex in
  dest:address:address:duplex-list<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="muControl">reply</span> in
]

<span class="muRecipe">recipe</span> append-duplex [
  <span class="Constant">local-scope</span>
  in:address:duplex-list<span class="Special"> &lt;- </span><span class="Constant">next-ingredient</span>
  new:address:duplex-list<span class="Special"> &lt;- </span><span class="Constant">next-ingredient</span>
  last:address:duplex-list<span class="Special"> &lt;- </span>last-duplex in
  dest:address:address:duplex-list<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, in/same-as-ingredient:<span class="Constant">0</span>
  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="muControl">reply</span> in/same-as-ingredient:<span class="Constant">0</span>
]

<span class="muRecipe">recipe</span> last-duplex [
  <span class="Constant">local-scope</span>
  in:address:duplex-list<span class="Special"> &lt;- </span><span class="Constant">next-ingredient</span>
  result:address:duplex-list<span class="Special"> &lt;- </span>copy in
  <span class="Delimiter">{</span>
    next:address:duplex-list<span class="Special"> &lt;- </span>next-duplex 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="muControl">reply</span> result
]

<span class="Comment"># helper for debugging</span>
<span class="muRecipe">recipe</span> dump-duplex-from [
  <span class="Constant">local-scope</span>
  x:address:duplex-list<span class="Special"> &lt;- </span><span class="Constant">next-ingredient</span>
  $print x, <span class="Constant">[: ]</span>
  <span class="Delimiter">{</span>
    <span class="muControl">break-unless</span> x
    c:character<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-duplex 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 : -->