design.xml 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <!DOCTYPE library PUBLIC "-//Boost//DTD BoostBook XML V1.0//EN"
  3. "http://www.boost.org/tools/boostbook/dtd/boostbook.dtd">
  4. <!-- Copyright (c) 2002-2006 Pavol Droba.
  5. Subject to the Boost Software License, Version 1.0.
  6. (See accompanying file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt)
  7. -->
  8. <section id="string_algo.design" last-revision="$Date$">
  9. <title>Design Topics</title>
  10. <using-namespace name="boost"/>
  11. <using-namespace name="boost::algorithm"/>
  12. <section id="string_algo.string">
  13. <title>String Representation</title>
  14. <para>
  15. As the name suggest, this library works mainly with strings. However, in the context of this library,
  16. a string is not restricted to any particular implementation (like <code>std::basic_string</code>),
  17. rather it is a concept. This allows the algorithms in this library to be reused for any string type,
  18. that satisfies the given requirements.
  19. </para>
  20. <para>
  21. <emphasis role="bold">Definition:</emphasis> A string is a
  22. <ulink url="../../libs/range/index.html">range</ulink> of characters accessible in sequential
  23. ordered fashion. Character is any value type with "cheap" copying and assignment.
  24. </para>
  25. <para>
  26. First requirement of string-type is that it must accessible using
  27. <ulink url="../../libs/range/index.html">Boost.Range</ulink>. This facility allows to access
  28. the elements inside the string in a uniform iterator-based fashion.
  29. This is sufficient for our library
  30. </para>
  31. <para>
  32. Second requirement defines the way in which the characters are stored in the string. Algorithms in
  33. this library work with an assumption that copying a character is cheaper then allocating extra
  34. storage to cache results. This is a natural assumption for common character types. Algorithms will
  35. work even if this requirement is not satisfied, however at the cost of performance degradation.
  36. <para>
  37. </para>
  38. In addition some algorithms have additional requirements on the string-type. Particularly, it is required
  39. that an algorithm can create a new string of the given type. In this case, it is required that
  40. the type satisfies the sequence (Std &sect;23.1.1) requirements.
  41. </para>
  42. <para>
  43. In the reference and also in the code, requirement on the string type is designated by the name of
  44. template argument. <code>RangeT</code> means that the basic range requirements must hold.
  45. <code>SequenceT</code> designates extended sequence requirements.
  46. </para>
  47. </section>
  48. <section id="string_algo.sequence_traits">
  49. <title>Sequence Traits</title>
  50. <para>
  51. The major difference between <code>std::list</code> and <code>std::vector</code> is not in the interfaces
  52. they provide, but rather in the inner details of the class and the way how it performs
  53. various operations. The problem is that it is not possible to infer this difference from the
  54. definitions of classes without some special mechanism.
  55. However, some algorithms can run significantly faster with the knowledge of the properties
  56. of a particular container.
  57. </para>
  58. <para>
  59. Sequence traits allow one to specify additional properties of a sequence container (see Std.&sect;32.2).
  60. These properties are then used by algorithms to select optimized handling for some operations.
  61. The sequence traits are declared in the header
  62. <headername>boost/algorithm/string/sequence_traits.hpp</headername>.
  63. </para>
  64. <para>
  65. In the table C denotes a container and c is an object of C.
  66. </para>
  67. <table>
  68. <title>Sequence Traits</title>
  69. <tgroup cols="2" align="left">
  70. <thead>
  71. <row>
  72. <entry>Trait</entry>
  73. <entry>Description</entry>
  74. </row>
  75. </thead>
  76. <tbody>
  77. <row>
  78. <entry><classname>has_native_replace&lt;C&gt;</classname>::value</entry>
  79. <entry>Specifies that the sequence has std::string like replace method</entry>
  80. </row>
  81. <row>
  82. <entry><classname>has_stable_iterators&lt;C&gt;</classname>::value</entry>
  83. <entry>
  84. Specifies that the sequence has stable iterators. It means,
  85. that operations like <code>insert</code>/<code>erase</code>/<code>replace</code>
  86. do not invalidate iterators.
  87. </entry>
  88. </row>
  89. <row>
  90. <entry><classname>has_const_time_insert&lt;C&gt;</classname>::value</entry>
  91. <entry>
  92. Specifies that the insert method of the sequence has
  93. constant time complexity.
  94. </entry>
  95. </row>
  96. <row>
  97. <entry><classname>has_const_time_erase&lt;C&gt;</classname>::value</entry>
  98. <entry>
  99. Specifies that the erase method of the sequence has constant time complexity
  100. </entry>
  101. </row>
  102. </tbody>
  103. </tgroup>
  104. </table>
  105. <para>
  106. Current implementation contains specializations for std::list&lt;T&gt; and
  107. std::basic_string&lt;T&gt; from the standard library and SGI's std::rope&lt;T&gt; and std::slist&lt;T&gt;.
  108. </para>
  109. </section>
  110. <section id="string_algo.find">
  111. <title>Find Algorithms</title>
  112. <para>
  113. Find algorithms have similar functionality to <code>std::search()</code> algorithm. They provide a different
  114. interface which is more suitable for common string operations.
  115. Instead of returning just the start of matching subsequence they return a range which is necessary
  116. when the length of the matching subsequence is not known beforehand.
  117. This feature also allows a partitioning of the input sequence into three
  118. parts: a prefix, a substring and a suffix.
  119. </para>
  120. <para>
  121. Another difference is an addition of various searching methods besides find_first, including find_regex.
  122. </para>
  123. <para>
  124. It the library, find algorithms are implemented in terms of
  125. <link linkend="string_algo.finder_concept">Finders</link>. Finders are used also by other facilities
  126. (replace,split).
  127. For convenience, there are also function wrappers for these finders to simplify find operations.
  128. </para>
  129. <para>
  130. Currently the library contains only naive implementation of find algorithms with complexity
  131. O(n * m) where n is the size of the input sequence and m is the size of the search sequence.
  132. There are algorithms with complexity O(n), but for smaller sequence a constant overhead is
  133. rather big. For small m &lt;&lt; n (m by magnitude smaller than n) the current implementation
  134. provides acceptable efficiency.
  135. Even the C++ standard defines the required complexity for search algorithm as O(n * m).
  136. It is possible that a future version of library will also contain algorithms with linear
  137. complexity as an option
  138. </para>
  139. </section>
  140. <section id="string_algo.replace">
  141. <title>Replace Algorithms</title>
  142. <para>
  143. The implementation of replace algorithms follows the layered structure of the library. The
  144. lower layer implements generic substitution of a range in the input sequence.
  145. This layer takes a <link linkend="string_algo.finder_concept">Finder</link> object and a
  146. <link linkend="string_algo.formatter_concept">Formatter</link> object as an input. These two
  147. functors define what to replace and what to replace it with. The upper layer functions
  148. are just wrapping calls to the lower layer. Finders are shared with the find and split facility.
  149. </para>
  150. <para>
  151. As usual, the implementation of the lower layer is designed to work with a generic sequence while
  152. taking advantage of specific features if possible
  153. (by using <link linkend="string_algo.sequence_traits">Sequence traits</link>)
  154. </para>
  155. </section>
  156. <section id="string_algo.split">
  157. <title>Find Iterators &amp; Split Algorithms</title>
  158. <para>
  159. Find iterators are a logical extension of the <link linkend="string_algo.find">find facility</link>.
  160. Instead of searching for one match, the whole input can be iteratively searched for multiple matches.
  161. The result of the search is then used to partition the input. It depends on the algorithms which parts
  162. are returned as the result. They can be the matching parts (<classname>find_iterator</classname>) of the parts in
  163. between (<classname>split_iterator</classname>).
  164. </para>
  165. <para>
  166. In addition the split algorithms like <functionname>find_all()</functionname> and <functionname>split()</functionname>
  167. can simplify the common operations. They use a find iterator to search the whole input and copy the
  168. matches they found into the supplied container.
  169. </para>
  170. </section>
  171. <section id="string_algo.exception">
  172. <title>Exception Safety</title>
  173. <para>
  174. The library requires that all operations on types used as template
  175. or function arguments provide the <emphasis>basic exception-safety guarantee</emphasis>.
  176. In turn, all functions and algorithms in this library, except where stated
  177. otherwise, will provide the <emphasis>basic exception-safety guarantee</emphasis>.
  178. In other words:
  179. The library maintains its invariants and does not leak resources in
  180. the face of exceptions. Some library operations give stronger
  181. guarantees, which are documented on an individual basis.
  182. </para>
  183. <para>
  184. Some functions can provide the <emphasis>strong exception-safety guarantee</emphasis>.
  185. That means that following statements are true:
  186. <itemizedlist>
  187. <listitem>
  188. If an exception is thrown, there are no effects other than those
  189. of the function
  190. </listitem>
  191. <listitem>
  192. If an exception is thrown other than by the function, there are no effects
  193. </listitem>
  194. </itemizedlist>
  195. This guarantee can be provided under the condition that the operations
  196. on the types used for arguments for these functions either
  197. provide the strong exception guarantee or do not alter the global state .
  198. </para>
  199. <para>
  200. In the reference, under the term <emphasis>strong exception-safety guarantee</emphasis>, we mean the
  201. guarantee as defined above.
  202. </para>
  203. <para>
  204. For more information about the exception safety topics, follow this
  205. <ulink url="http://www.boost.org/more/generic_exception_safety.html">link</ulink>
  206. </para>
  207. </section>
  208. </section>