fold_tree.xml 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <!--
  3. Copyright 2012 Eric Niebler
  4. Distributed under the Boost
  5. Software License, Version 1.0. (See accompanying
  6. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  7. -->
  8. <header name="boost/proto/transform/fold_tree.hpp">
  9. <para>
  10. Contains definition of the
  11. <computeroutput>
  12. <classname alt="boost::proto::fold_tree">proto::fold_tree&lt;&gt;</classname>
  13. </computeroutput> and
  14. <computeroutput>
  15. <classname alt="boost::proto::reverse_fold_tree">proto::reverse_fold_tree&lt;&gt;</classname>
  16. </computeroutput>
  17. transforms.
  18. </para>
  19. <namespace name="boost">
  20. <namespace name="proto">
  21. <struct name="fold_tree">
  22. <template>
  23. <template-type-parameter name="Sequence"/>
  24. <template-type-parameter name="State0"/>
  25. <template-type-parameter name="Fun"/>
  26. </template>
  27. <inherit><classname>proto::transform</classname>&lt; fold_tree&lt;Sequence, State0, Fun&gt; &gt;</inherit>
  28. <purpose>A <conceptname>PrimitiveTransform</conceptname> that recursively applies the
  29. <computeroutput><classname>proto::fold</classname>&lt;&gt;</computeroutput> transform to sub-trees
  30. that all share a common tag type.</purpose>
  31. <description>
  32. <para>
  33. <computeroutput>proto::fold_tree&lt;&gt;</computeroutput> is useful for flattening trees into lists;
  34. for example, you might use <computeroutput>proto::fold_tree&lt;&gt;</computeroutput> to flatten an
  35. expression tree like <computeroutput>a | b | c</computeroutput> into a Fusion list like
  36. <computeroutput>cons(c, cons(b, cons(a)))</computeroutput>.
  37. </para>
  38. <para>
  39. <computeroutput>proto::fold_tree&lt;&gt;</computeroutput> is easily understood in terms of a
  40. <computeroutput>recurse_if_&lt;&gt;</computeroutput> helper, defined as follows:
  41. <programlisting> template&lt;typename Tag, typename Fun&gt;
  42. struct recurse_if_ :
  43. <classname>proto::if_</classname>&lt;
  44. // If the current node has type type "Tag" ...
  45. boost::is_same&lt;<classname>proto::tag_of</classname>&lt;<classname>proto::_</classname>&gt;, Tag&gt;(),
  46. // ... recurse, otherwise ...
  47. <classname>proto::fold</classname>&lt;<classname>proto::_</classname>, <classname>proto::_state</classname>, recurse_if_&lt;Tag, Fun&gt; &gt;,
  48. // ... apply the Fun transform.
  49. Fun
  50. &gt;
  51. {};</programlisting>
  52. </para>
  53. <para>
  54. With <computeroutput>recurse_if_&lt;&gt;</computeroutput> as defined above,
  55. <computeroutput>proto::fold_tree&lt;Sequence, State0, Fun&gt;()(expr, state, data)</computeroutput>
  56. is equivalent to:
  57. <programlisting><classname>proto::fold</classname>&lt;
  58. Sequence,
  59. State0,
  60. recurse_if_&lt;typename Expr::proto_tag, Fun&gt;
  61. &gt;()(expr, state, data).</programlisting>
  62. It has the effect of folding a tree front-to-back, recursing into child nodes that share a
  63. tag type with the parent node.
  64. </para>
  65. </description>
  66. <struct name="impl">
  67. <template>
  68. <template-type-parameter name="Expr"/>
  69. <template-type-parameter name="State"/>
  70. <template-type-parameter name="Data"/>
  71. </template>
  72. <inherit>
  73. <type>
  74. <classname>proto::fold</classname>&lt;Sequence, State0, recurse_if_&lt;typename Expr::proto_tag, Fun&gt; &gt;
  75. ::template impl&lt;Expr, State, Data&gt;</type>
  76. </inherit>
  77. </struct>
  78. </struct>
  79. <struct name="reverse_fold_tree">
  80. <template>
  81. <template-type-parameter name="Sequence"/>
  82. <template-type-parameter name="State0"/>
  83. <template-type-parameter name="Fun"/>
  84. </template>
  85. <inherit><classname>proto::transform</classname>&lt; reverse_fold_tree&lt;Sequence, State0, Fun&gt; &gt;</inherit>
  86. <purpose>A <conceptname>PrimitiveTransform</conceptname> that recursively applies the
  87. <computeroutput><classname>proto::reverse_fold&lt;&gt;</classname></computeroutput> transform to
  88. sub-trees that all share a common tag type.</purpose>
  89. <description>
  90. <para>
  91. <computeroutput>proto::reverse_fold_tree&lt;&gt;</computeroutput> is useful for flattening trees
  92. into lists; for example, you might use <computeroutput>proto::reverse_fold_tree&lt;&gt;</computeroutput>
  93. to flatten an expression tree like <computeroutput>a | b | c</computeroutput> into a Fusion list like
  94. <computeroutput>cons(a, cons(b, cons(c)))</computeroutput>.
  95. </para>
  96. <para>
  97. <computeroutput>proto::reverse_fold_tree&lt;&gt;</computeroutput> is easily understood in terms of
  98. a <computeroutput>recurse_if_&lt;&gt;</computeroutput> helper, defined as follows:
  99. <programlisting> template&lt;typename Tag, typename Fun&gt;
  100. struct recurse_if_ :
  101. <classname>proto::if_</classname>&lt;
  102. // If the current node has type type "Tag" ...
  103. boost::is_same&lt;<classname>proto::tag_of</classname>&lt;<classname>proto::_</classname>&gt;, Tag&gt;(),
  104. // ... recurse, otherwise ...
  105. <classname>proto::reverse_fold</classname>&lt;<classname>proto::_</classname>, <classname>proto::_state</classname>, recurse_if_&lt;Tag, Fun&gt; &gt;,
  106. // ... apply the Fun transform.
  107. Fun
  108. &gt;
  109. {};</programlisting>
  110. </para>
  111. <para>
  112. With <computeroutput>recurse_if_&lt;&gt;</computeroutput> as defined above,
  113. <computeroutput>proto::reverse_fold_tree&lt;Sequence, State0, Fun&gt;()(expr, state, data)</computeroutput>
  114. is equivalent to:
  115. <programlisting><classname>proto::reverse_fold</classname>&lt;
  116. Sequence,
  117. State0,
  118. recurse_if_&lt;typename Expr::proto_tag, Fun&gt;
  119. &gt;()(expr, state, data).</programlisting>
  120. It has the effect of folding a tree back-to-front, recursing into child nodes that share a
  121. tag type with the parent node.
  122. </para>
  123. </description>
  124. <struct name="impl">
  125. <template>
  126. <template-type-parameter name="Expr"/>
  127. <template-type-parameter name="State"/>
  128. <template-type-parameter name="Data"/>
  129. </template>
  130. <inherit>
  131. <type>
  132. <classname>proto::reverse_fold</classname>&lt;Sequence, State0, recurse_if_&lt;typename Expr::proto_tag, Fun&gt; &gt;
  133. ::template impl&lt;Expr, State, Data&gt;</type>
  134. </inherit>
  135. </struct>
  136. </struct>
  137. </namespace>
  138. </namespace>
  139. </header>