Bit-String-Encoding.html 4.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <html lang="en">
  2. <head>
  3. <title>Bit String Encoding - avram - a virtual machine code interpreter</title>
  4. <meta http-equiv="Content-Type" content="text/html">
  5. <meta name="description" content="avram - a virtual machine code interpreter">
  6. <meta name="generator" content="makeinfo 4.13">
  7. <link title="Top" rel="start" href="index.html#Top">
  8. <link rel="up" href="Concrete-Syntax.html#Concrete-Syntax" title="Concrete Syntax">
  9. <link rel="prev" href="Concrete-Syntax.html#Concrete-Syntax" title="Concrete Syntax">
  10. <link rel="next" href="Blocking.html#Blocking" title="Blocking">
  11. <link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage">
  12. <meta http-equiv="Content-Style-Type" content="text/css">
  13. <style type="text/css"><!--
  14. pre.display { font-family:inherit }
  15. pre.format { font-family:inherit }
  16. pre.smalldisplay { font-family:inherit; font-size:smaller }
  17. pre.smallformat { font-family:inherit; font-size:smaller }
  18. pre.smallexample { font-size:smaller }
  19. pre.smalllisp { font-size:smaller }
  20. span.sc { font-variant:small-caps }
  21. span.roman { font-family:serif; font-weight:normal; }
  22. span.sansserif { font-family:sans-serif; font-weight:normal; }
  23. --></style>
  24. </head>
  25. <body>
  26. <div class="node">
  27. <a name="Bit-String-Encoding"></a>
  28. <p>
  29. Next:&nbsp;<a rel="next" accesskey="n" href="Blocking.html#Blocking">Blocking</a>,
  30. Previous:&nbsp;<a rel="previous" accesskey="p" href="Concrete-Syntax.html#Concrete-Syntax">Concrete Syntax</a>,
  31. Up:&nbsp;<a rel="up" accesskey="u" href="Concrete-Syntax.html#Concrete-Syntax">Concrete Syntax</a>
  32. <hr>
  33. </div>
  34. <h4 class="subsection">2.2.1 Bit String Encoding</h4>
  35. <p>The conversion from trees to bit strings might have been done in several
  36. <a name="index-trees-148"></a>ways, perhaps the most obvious being based on a preorder traversal with
  37. each vertex printed as it is traversed. By this method, the entire
  38. encoding of the left descendent would precede that of the right in the
  39. bit string. This alternative is therefore rejected because it imposes
  40. unnecessary serialization on communication.
  41. <p>It is preferable for the encodings of both descendents of a tree to be
  42. interleaved to allow concurrent transmission. Although there is
  43. presently no distributed implementation of the virtual machine and hence
  44. <a name="index-distributed-implementation-149"></a>none that takes advantage of this possibility, it is better to plan
  45. ahead than to be faced with backward compatibility problems later.
  46. <p>The preferred algorithm for encoding a tree as a bit string employs a
  47. queue. The queue contains trees and allows them to be processed in a
  48. <a name="index-queues-150"></a>first-in first-out order. Intuitively, the algorithm works by traversing
  49. <a name="index-printing-algorithm-151"></a>the tree in level order. To print a tree <code>T</code> as a string of
  50. <code>1</code>s and <code>0</code>s, it performs the following steps.
  51. <pre class="display">
  52. Initialize the queue to contain only <code>T</code>
  53. while the queue is not empty do
  54. if the front element of the queue is <code>nil</code> then
  55. print <code>0</code>
  56. else if the front element of the queue is of the form <code>cons(x,y)</code> then
  57. print <code>1</code>
  58. append <code>x</code> to the back of the queue
  59. append <code>y</code> to the back of the queue
  60. end if
  61. remove the front element of the queue
  62. end while
  63. </pre>
  64. <p>This algorithm presupposes that any given tree
  65. <a name="index-deconstruction-152"></a><code>cons(x,y)</code> can be &ldquo;deconstructed&rdquo; to obtain <code>x</code> and
  66. <code>y</code>. The computability of such an operation is assured in theory by
  67. the uniqueness property of the <code>cons</code> operator, regardless of the
  68. representation chosen. If the trees are implemented with pointers in the
  69. obvious way, their deconstruction is a trivial constant time operation.
  70. <p>As an example, running the following tree through the above algorithm
  71. results in the bit string <code>111111101011110010001001100010100010100100100</code>.
  72. <pre class="example">
  73. cons(
  74. cons(
  75. cons(nil,cons(nil,cons(nil,nil))),
  76. cons(nil,cons(nil,nil))),
  77. cons(
  78. cons(
  79. cons(nil,cons(nil,cons(nil,cons(nil,nil)))),
  80. cons(nil,nil)),
  81. cons(
  82. cons(
  83. cons(nil,cons(nil,cons(cons(nil,cons(nil,nil)),nil))),
  84. cons(nil,nil)),
  85. nil)))
  86. </pre>
  87. </body></html>