examples.html 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0.1 Transitional//EN">
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
  5. <title>Boost.Flyweight Documentation - Examples</title>
  6. <link rel="stylesheet" href="style.css" type="text/css">
  7. <link rel="start" href="index.html">
  8. <link rel="prev" href="performance.html">
  9. <link rel="up" href="index.html">
  10. <link rel="next" href="tests.html">
  11. </head>
  12. <body>
  13. <h1><img src="../../../boost.png" alt="Boost logo" align=
  14. "middle" width="277" height="86">Boost.Flyweight Examples</h1>
  15. <div class="prev_link"><a href="performance.html"><img src="prev.gif" alt="performance" border="0"><br>
  16. Performance
  17. </a></div>
  18. <div class="up_link"><a href="index.html"><img src="up.gif" alt="index" border="0"><br>
  19. Index
  20. </a></div>
  21. <div class="next_link"><a href="tests.html"><img src="next.gif" alt="tests" border="0"><br>
  22. Tests
  23. </a></div><br clear="all" style="clear: all;">
  24. <br clear="all" style="clear: all;">
  25. <hr>
  26. <h2>Contents</h2>
  27. <ul>
  28. <li><a href="#example1">Example 1: basic usage</a></li>
  29. <li><a href="#example2">Example 2: key-value flyweights</a></li>
  30. <li><a href="#example3">Example 3: flyweights and the composite pattern</a></li>
  31. <li><a href="#example4">Example 4: formatted text processing</a></li>
  32. <li><a href="#example5">Example 5: flyweight-based memoization</a></li>
  33. <li><a href="#example6">Example 6: serialization</a></li>
  34. <li><a href="#example7">Example 7: performance comparison</a></li>
  35. <li><a href="#example8">Example 8: custom factory</a></li>
  36. </ul>
  37. <h2><a name="example1">Example 1: basic usage</a></h2>
  38. <p>
  39. See <a href="../example/basic.cpp">source code</a>.
  40. </p>
  41. <p>
  42. Dummy program showing the basic capabilities of <code>flyweight</code>
  43. explained at the <a href="tutorial/basics.html">tutorial</a>.
  44. </p>
  45. <h2><a name="example2">Example 2: key-value flyweights</a></h2>
  46. <p>
  47. See <a href="../example/key_value.cpp">source code</a>.
  48. </p>
  49. <p>
  50. The program simulates the scenario described at the tutorial section on
  51. <a href="tutorial/key_value.html">key-value flyweights</a>: The class
  52. <code>texture</code> manages some texture rendering data stored in
  53. a file whose location is given at construction time. The program
  54. handles large quantities of objects of this class by encapsulating
  55. them into key-value flyweights keyed by filename. Observe how the
  56. execution of the program results in no extra constructions or copies
  57. of objects of type <code>texture</code> except those absolutely
  58. necessary.
  59. </p>
  60. <h2><a name="example3">Example 3: flyweights and the composite pattern</a></h2>
  61. <p>
  62. See <a href="../example/composite.cpp">source code</a>.
  63. </p>
  64. <p>
  65. The <a href="http://c2.com/cgi/wiki?CompositePattern"><i>composite
  66. design pattern</i></a> revolves about the idea that a tree data structure
  67. can be easily constructed and manipulated by defining the tree node type
  68. polymorphically so that either is a leaf node or else contains a list of
  69. pointers to their child nodes.
  70. This way, a tree is the exact same entity as its root node, which allows
  71. for very simple recursive tree-handling algorithms. Large composite trees
  72. having a high degree of duplication of nodes and subtrees (as for instance
  73. those generated when parsing a computer program) are a natural fit for the
  74. flyweight idiom: simply turning the node type into a flyweight
  75. automatically deals with duplication at the node and subtree level.
  76. </p>
  77. <p>
  78. The example program parses Lisp-like lists of the form
  79. <code>(a<sub>1</sub> ... a<sub><i>n</i></sub>)</code> where each
  80. <code>a<sub>i</sub></code> is a terminal string or a list. The parsed
  81. data structure is a composite type defined using Boost.Flyweight in conjunction
  82. with the recursive facilities of
  83. <a href="../../variant/index.html">Boost.Variant</a>. So, given the list
  84. </p>
  85. <blockquote><pre>
  86. (= (tan (+ x y))(/ (+ (tan x)(tan y))(- 1 (* (tan x)(tan y)))))
  87. </pre></blockquote>
  88. <p>
  89. the resulting data structure implicitly detects the duplicated
  90. occurrences of <code>+</code>, <code>x</code>, <code>y</code>,
  91. <code>tan</code>, <code>(tan x)</code> and <code>(tan y)</code>.
  92. </p>
  93. <h2><a name="example4">Example 4: formatted text processing</a></h2>
  94. <p>
  95. See <a href="../example/html.cpp">source code</a>.
  96. </p>
  97. <p>
  98. A classic example of application of the flyweight pattern is that of a
  99. text processor which handles characters with rich formatting information,
  100. like font type, size, color and special options (boldness, italics, etc.)
  101. Coding the formatting information of each character takes considerable
  102. space, but, given the high degree of repetition typical in a document,
  103. maintaining formatted characters as flyweight objects drastically reduces
  104. memory consumption.
  105. </p>
  106. <p>
  107. The example program parses, manipulates and stores HTML documents following
  108. flyweight-based representation techniques. Given the hierarchical nature
  109. of HTML markup, a crude approximation to the formatting options of a given
  110. character is just to equate them with the stack of tag contexts to which
  111. the character belongs, as the figure shows.
  112. </p>
  113. <p align="center">
  114. <img src="html.png"
  115. alt="formatting contexts of characters in an HTML document"
  116. width="320" height="275"><br>
  117. <b>Fig. 1: Formatting contexts of characters in an HTML document.</b>
  118. </p>
  119. <p>
  120. HTML documents are then parsed as arrays of (character, format)
  121. pairs, where the format is the tag context as described above. The very high
  122. degree of redundancy in formatting information is taken care of by the
  123. use of Boost.Flyweight. This character-based representation makes it
  124. easy to manipulate the document: transposition and elimination of
  125. portions of text are trivial operations. As an example, the program
  126. reverses the text occupying the central portion of the document.
  127. Saving the result in HTML reduces to traversing the array of formatted
  128. characters and emitting opening/closing HTML tags as the context of adjacent
  129. characters varies.
  130. </p>
  131. <p>
  132. For the sake of brevity, the HTML parsing capabilities of this program
  133. are coarse: for instance, elements without end-tag (like &lt;BR&gt;), character
  134. encoding and HTML entities (e.g. "&amp;copy;" for &copy;) are not properly
  135. handled. Improving the parsing code is left as an exercise to the reader.
  136. </p>
  137. <h2><a name="example5">Example 5: flyweight-based memoization</a></h2>
  138. <p>
  139. See <a href="../example/fibonacci.cpp">source code</a>.
  140. </p>
  141. <p>
  142. <a href="http://en.wikipedia.org/wiki/Memoization">Memoization</a>
  143. is an optimization technique consisting in caching
  144. the results of a computation for later reuse; this can dramatically
  145. improve performance when calculating recursive numerical functions,
  146. for instance. <a href="tutorial/key_value.html">Key-value flyweights</a>
  147. can be used to implement memoization for a numerical function <i>f</i>
  148. by modeling a memoized invocation of the function as a value of
  149. type <code>flyweight&lt;key_value&lt;int,compute_f&gt; &gt;</code>, where
  150. <code>compute_f</code> is a type that does the computation of
  151. <i>f</i>(<i>n</i>) at its <code>compute_f::compute_f(int)</code> constructor.
  152. For instance, the <a href="http://mathworld.wolfram.com/FibonacciNumber.html">Fibonacci
  153. numbers</a> can be computed with memoization like this:
  154. </p>
  155. <blockquote><pre>
  156. <span class=keyword>typedef</span> <span class=identifier>flyweight</span><span class=special>&lt;</span><span class=identifier>key_value</span><span class=special>&lt;</span><span class=keyword>int</span><span class=special>,</span><span class=identifier>compute_fibonacci</span><span class=special>&gt;,</span><span class=identifier>no_tracking</span><span class=special>&gt;</span> <span class=identifier>fibonacci</span><span class=special>;</span>
  157. <span class=keyword>struct</span> <span class=identifier>compute_fibonacci</span>
  158. <span class=special>{</span>
  159. <span class=identifier>compute_fibonacci</span><span class=special>(</span><span class=keyword>int</span> <span class=identifier>n</span><span class=special>):</span>
  160. <span class=identifier>result</span><span class=special>(</span><span class=identifier>n</span><span class=special>==</span><span class=number>0</span><span class=special>?</span><span class=number>0</span><span class=special>:</span><span class=identifier>n</span><span class=special>==</span><span class=number>1</span><span class=special>?</span><span class=number>1</span><span class=special>:</span><span class=identifier>fibonacci</span><span class=special>(</span><span class=identifier>n</span><span class=special>-</span><span class=number>2</span><span class=special>).</span><span class=identifier>get</span><span class=special>()+</span><span class=identifier>fibonacci</span><span class=special>(</span><span class=identifier>n</span><span class=special>-</span><span class=number>1</span><span class=special>).</span><span class=identifier>get</span><span class=special>())</span>
  161. <span class=special>{}</span>
  162. <span class=keyword>operator</span> <span class=keyword>int</span><span class=special>()</span><span class=keyword>const</span><span class=special>{</span><span class=keyword>return</span> <span class=identifier>result</span><span class=special>;}</span>
  163. <span class=keyword>int</span> <span class=identifier>result</span><span class=special>;</span>
  164. <span class=special>};</span>
  165. </pre></blockquote>
  166. <p>
  167. The <a href="tutorial/configuration.html#no_tracking"><code>no_tracking</code></a>
  168. policy is used so that the memoized computations persist for future
  169. use throughout the program. The provided program develops this example in full.
  170. </p>
  171. <h2><a name="example6">Example 6: serialization</a></h2>
  172. <p>
  173. See <a href="../example/serialization.cpp">source code</a>.
  174. </p>
  175. <p>
  176. If <code>T</code> is serializable (using
  177. <a href="../../serialization/index.html">Boost.Serialization</a>),
  178. <code>flyweight&lt;T&gt;</code> is automatically
  179. serializable as well. The example program performs the following two
  180. complementary procedures:
  181. <ul>
  182. <li>Read a text file as a <code>std::vector&lt;flyweight&lt;std::string&gt; &gt;</code>
  183. and save the structure to a serialization file.
  184. </li>
  185. <li>Load a <code>std::vector&lt;flyweight&lt;std::string&gt; &gt;</code> from a
  186. serialization file and write it as a text file.
  187. </li>
  188. </ul>
  189. If you visually inspect the contents of any of the generated serialization files
  190. you can notice that no word appears twice; Boost.Flyweight implements some internal
  191. machinery that avoids duplicating output information when saving equal
  192. <code>flyweight</code> objects.
  193. </p>
  194. <h2><a name="example7">Example 7: performance comparison</a></h2>
  195. <p>
  196. See <a href="../example/perf.cpp">source code</a>.
  197. </p>
  198. <p>
  199. This program measures the time and space performances of a simple
  200. string type against several differently configured <code>flyweight</code>
  201. instantations as used in a conventional task involving parsing a file and
  202. doing some manipulations on the parsed text.
  203. Memory consumption is computed by instrumenting the relevant
  204. components (the string type itself, flyweight factories, etc.) with custom
  205. allocators that keep track of the allocations and deallocations requested.
  206. The program has been used to produce the experimental results given
  207. at the <a href="performance.html#results">performance section</a>.
  208. </p>
  209. <h2><a name="example8">Example 8: custom factory</a></h2>
  210. <p>
  211. See <a href="../example/custom_factory.cpp">source code</a>.
  212. </p>
  213. <p>
  214. The example shows how to write and use a custom factory class. This
  215. "verbose" factory outputs messages tracing the invocations of its public interface
  216. by Boost.Flyweight, so helping the user visualize factory usage patterns.
  217. </p>
  218. <hr>
  219. <div class="prev_link"><a href="performance.html"><img src="prev.gif" alt="performance" border="0"><br>
  220. Performance
  221. </a></div>
  222. <div class="up_link"><a href="index.html"><img src="up.gif" alt="index" border="0"><br>
  223. Index
  224. </a></div>
  225. <div class="next_link"><a href="tests.html"><img src="next.gif" alt="tests" border="0"><br>
  226. Tests
  227. </a></div><br clear="all" style="clear: all;">
  228. <br clear="all" style="clear: all;">
  229. <br>
  230. <p>Revised October 14th 2014</p>
  231. <p>&copy; Copyright 2006-2014 Joaqu&iacute;n M L&oacute;pez Mu&ntilde;oz.
  232. Distributed under the Boost Software
  233. License, Version 1.0. (See accompanying file <a href="../../../LICENSE_1_0.txt">
  234. LICENSE_1_0.txt</a> or copy at <a href="http://www.boost.org/LICENSE_1_0.txt">
  235. http://www.boost.org/LICENSE_1_0.txt</a>)
  236. </p>
  237. </body>
  238. </html>