| 1 | <html> |
|---|
| 2 | <head> |
|---|
| 3 | <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> |
|---|
| 4 | <title>Design Topics</title> |
|---|
| 5 | <link rel="stylesheet" href="../boostbook.css" type="text/css"> |
|---|
| 6 | <meta name="generator" content="DocBook XSL Stylesheets V1.68.1"> |
|---|
| 7 | <link rel="start" href="../index.html" title="The Boost C++ Libraries BoostBook Documentation Subset"> |
|---|
| 8 | <link rel="up" href="../string_algo.html" title="Chapter 14. Boost String Algorithms Library"> |
|---|
| 9 | <link rel="prev" href="quickref.html" title="Quick Reference"> |
|---|
| 10 | <link rel="next" href="concept.html" title="Concepts"> |
|---|
| 11 | </head> |
|---|
| 12 | <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"> |
|---|
| 13 | <table cellpadding="2" width="100%"> |
|---|
| 14 | <td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../boost.png"></td> |
|---|
| 15 | <td align="center"><a href="../../../index.htm">Home</a></td> |
|---|
| 16 | <td align="center"><a href="../../../libs/libraries.htm">Libraries</a></td> |
|---|
| 17 | <td align="center"><a href="../../../people/people.htm">People</a></td> |
|---|
| 18 | <td align="center"><a href="../../../more/faq.htm">FAQ</a></td> |
|---|
| 19 | <td align="center"><a href="../../../more/index.htm">More</a></td> |
|---|
| 20 | </table> |
|---|
| 21 | <hr> |
|---|
| 22 | <div class="spirit-nav"> |
|---|
| 23 | <a accesskey="p" href="quickref.html"><img src="../images/prev.png" alt="Prev"></a><a accesskey="u" href="../string_algo.html"><img src="../images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../images/home.png" alt="Home"></a><a accesskey="n" href="concept.html"><img src="../images/next.png" alt="Next"></a> |
|---|
| 24 | </div> |
|---|
| 25 | <div class="section" lang="en"> |
|---|
| 26 | <div class="titlepage"><div><div><h2 class="title" style="clear: both"> |
|---|
| 27 | <a name="string_algo.design"></a>Design Topics</h2></div></div></div> |
|---|
| 28 | <div class="toc"><dl> |
|---|
| 29 | <dt><span class="section"><a href="design.html#string_algo.string">String Representation</a></span></dt> |
|---|
| 30 | <dt><span class="section"><a href="design.html#string_algo.sequence_traits">Sequence Traits</a></span></dt> |
|---|
| 31 | <dt><span class="section"><a href="design.html#string_algo.find">Find Algorithms</a></span></dt> |
|---|
| 32 | <dt><span class="section"><a href="design.html#string_algo.replace">Replace Algorithms</a></span></dt> |
|---|
| 33 | <dt><span class="section"><a href="design.html#string_algo.split">Find Iterators & Split Algorithms</a></span></dt> |
|---|
| 34 | <dt><span class="section"><a href="design.html#string_algo.exception">Exception Safety</a></span></dt> |
|---|
| 35 | </dl></div> |
|---|
| 36 | <div class="section" lang="en"> |
|---|
| 37 | <div class="titlepage"><div><div><h3 class="title"> |
|---|
| 38 | <a name="string_algo.string"></a>String Representation</h3></div></div></div> |
|---|
| 39 | <p> |
|---|
| 40 | As the name suggest, this library works mainly with strings. However, in the context of this library, |
|---|
| 41 | a string is not restricted to any particular implementation (like <code class="computeroutput">std::basic_string</code>), |
|---|
| 42 | rather it is a concept. This allows the algorithms in this library to be reused for any string type, |
|---|
| 43 | that satisfies the given requirements. |
|---|
| 44 | </p> |
|---|
| 45 | <p> |
|---|
| 46 | <span class="bold"><strong>Definition:</strong></span> A string is a |
|---|
| 47 | <a href="../../../libs/range/doc/range.html" target="_top">range</a> of characters accessible in sequential |
|---|
| 48 | ordered fashion. Character is any value type with "cheap" copying and assignment. |
|---|
| 49 | </p> |
|---|
| 50 | <p> |
|---|
| 51 | First requirement of string-type is that it must accessible using |
|---|
| 52 | <a href="../../../libs/range/index.html" target="_top">Boost.Range</a>. This facility allows to access |
|---|
| 53 | the elements inside the string in a uniform iterator-based fashion. |
|---|
| 54 | This is sufficient for our library |
|---|
| 55 | </p> |
|---|
| 56 | <p> |
|---|
| 57 | Second requirement defines the way in which the characters are stored in the string. Algorithms in |
|---|
| 58 | this library work with an assumption that copying a character is cheaper then allocating extra |
|---|
| 59 | storage to cache results. This is a natural assumption for common character types. Algorithms will |
|---|
| 60 | work even if this requirement is not satisfied, however at the cost of performance degradation. |
|---|
| 61 | </p> |
|---|
| 62 | <p> |
|---|
| 63 | </p> |
|---|
| 64 | <p> |
|---|
| 65 | In addition some algorithms have additional requirements on the string-type. Particularly, it is required |
|---|
| 66 | that an algorithm can create a new string of the given type. In this case, it is required that |
|---|
| 67 | the type satisfies the sequence (Std §23.1.1) requirements. |
|---|
| 68 | </p> |
|---|
| 69 | <p> |
|---|
| 70 | In the reference and also in the code, requirement on the string type is designated by the name of |
|---|
| 71 | template argument. <code class="computeroutput">RangeT</code> means that the basic range requirements must hold. |
|---|
| 72 | <code class="computeroutput">SequenceT</code> designates extended sequence requirements. |
|---|
| 73 | </p> |
|---|
| 74 | </div> |
|---|
| 75 | <div class="section" lang="en"> |
|---|
| 76 | <div class="titlepage"><div><div><h3 class="title"> |
|---|
| 77 | <a name="string_algo.sequence_traits"></a>Sequence Traits</h3></div></div></div> |
|---|
| 78 | <p> |
|---|
| 79 | The major difference between <code class="computeroutput">std::list</code> and <code class="computeroutput">std::vector</code> is not in the interfaces |
|---|
| 80 | they provide, but rather in the inner details of the class and the way how it performs |
|---|
| 81 | various operations. The problem is that it is not possible to infer this difference from the |
|---|
| 82 | definitions of classes without some special mechanism. |
|---|
| 83 | However, some algorithms can run significantly faster with the knowledge of the properties |
|---|
| 84 | of a particular container. |
|---|
| 85 | </p> |
|---|
| 86 | <p> |
|---|
| 87 | Sequence traits allow one to specify additional properties of a sequence container (see Std.§32.2). |
|---|
| 88 | These properties are then used by algorithms to select optimized handling for some operations. |
|---|
| 89 | The sequence traits are declared in the header |
|---|
| 90 | <code class="computeroutput"><a href="reference.html#header.boost.algorithm.string.sequence_traits.hpp" title="Header <boost/algorithm/string/sequence_traits.hpp>">boost/algorithm/string/sequence_traits.hpp</a></code>. |
|---|
| 91 | </p> |
|---|
| 92 | <p> |
|---|
| 93 | In the table C denotes a container and c is an object of C. |
|---|
| 94 | </p> |
|---|
| 95 | <div class="table"> |
|---|
| 96 | <a name="id1641718"></a><p class="title"><b>Table 14.12. Sequence Traits</b></p> |
|---|
| 97 | <table class="table" summary="Sequence Traits"> |
|---|
| 98 | <colgroup> |
|---|
| 99 | <col> |
|---|
| 100 | <col> |
|---|
| 101 | </colgroup> |
|---|
| 102 | <thead><tr> |
|---|
| 103 | <th align="left">Trait</th> |
|---|
| 104 | <th align="left">Description</th> |
|---|
| 105 | </tr></thead> |
|---|
| 106 | <tbody> |
|---|
| 107 | <tr> |
|---|
| 108 | <td align="left"> |
|---|
| 109 | <code class="computeroutput"><a href="../boost/algorithm/has_native_replace.html" title="Class template has_native_replace">has_native_replace<C></a></code>::value</td> |
|---|
| 110 | <td align="left">Specifies that the sequence has std::string like replace method</td> |
|---|
| 111 | </tr> |
|---|
| 112 | <tr> |
|---|
| 113 | <td align="left"> |
|---|
| 114 | <code class="computeroutput"><a href="../boost/algorithm/has_stable_iterators.html" title="Class template has_stable_iterators">has_stable_iterators<C></a></code>::value</td> |
|---|
| 115 | <td align="left"> |
|---|
| 116 | Specifies that the sequence has stable iterators. It means, |
|---|
| 117 | that operations like <code class="computeroutput">insert</code>/<code class="computeroutput">erase</code>/<code class="computeroutput">replace</code> |
|---|
| 118 | do not invalidate iterators. |
|---|
| 119 | </td> |
|---|
| 120 | </tr> |
|---|
| 121 | <tr> |
|---|
| 122 | <td align="left"> |
|---|
| 123 | <code class="computeroutput"><a href="../boost/algorithm/has_const_time_insert.html" title="Class template has_const_time_insert">has_const_time_insert<C></a></code>::value</td> |
|---|
| 124 | <td align="left"> |
|---|
| 125 | Specifies that the insert method of the sequence has |
|---|
| 126 | constant time complexity. |
|---|
| 127 | </td> |
|---|
| 128 | </tr> |
|---|
| 129 | <tr> |
|---|
| 130 | <td align="left"> |
|---|
| 131 | <code class="computeroutput"><a href="../boost/algorithm/has_const_time_erase.html" title="Class template has_const_time_erase">has_const_time_erase<C></a></code>::value</td> |
|---|
| 132 | <td align="left"> |
|---|
| 133 | Specifies that the erase method of the sequence has constant time complexity |
|---|
| 134 | </td> |
|---|
| 135 | </tr> |
|---|
| 136 | </tbody> |
|---|
| 137 | </table> |
|---|
| 138 | </div> |
|---|
| 139 | <p> |
|---|
| 140 | Current implementation contains specializations for std::list<T> and |
|---|
| 141 | std::basic_string<T> from the standard library and SGI's std::rope<T> and std::slist<T>. |
|---|
| 142 | </p> |
|---|
| 143 | </div> |
|---|
| 144 | <div class="section" lang="en"> |
|---|
| 145 | <div class="titlepage"><div><div><h3 class="title"> |
|---|
| 146 | <a name="string_algo.find"></a>Find Algorithms</h3></div></div></div> |
|---|
| 147 | <p> |
|---|
| 148 | Find algorithms have similar functionality to <code class="computeroutput">std::search()</code> algorithm. They provide a different |
|---|
| 149 | interface which is more suitable for common string operations. |
|---|
| 150 | Instead of returning just the start of matching subsequence they return a range which is necessary |
|---|
| 151 | when the length of the matching subsequence is not known beforehand. |
|---|
| 152 | This feature also allows a partitioning of the input sequence into three |
|---|
| 153 | parts: a prefix, a substring and a suffix. |
|---|
| 154 | </p> |
|---|
| 155 | <p> |
|---|
| 156 | Another difference is an addition of various searching methods besides find_first, including find_regex. |
|---|
| 157 | </p> |
|---|
| 158 | <p> |
|---|
| 159 | It the library, find algorithms are implemented in terms of |
|---|
| 160 | <a href="concept.html#string_algo.finder_concept" title="Finder Concept">Finders</a>. Finders are used also by other facilities |
|---|
| 161 | (replace,split). |
|---|
| 162 | For convenience, there are also function wrappers for these finders to simplify find operations. |
|---|
| 163 | </p> |
|---|
| 164 | <p> |
|---|
| 165 | Currently the library contains only naive implementation of find algorithms with complexity |
|---|
| 166 | O(n * m) where n is the size of the input sequence and m is the size of the search sequence. |
|---|
| 167 | There are algorithms with complexity O(n), but for smaller sequence a constant overhead is |
|---|
| 168 | rather big. For small m << n (m by magnitude smaller than n) the current implementation |
|---|
| 169 | provides acceptable efficiency. |
|---|
| 170 | Even the C++ standard defines the required complexity for search algorithm as O(n * m). |
|---|
| 171 | It is possible that a future version of library will also contain algorithms with linear |
|---|
| 172 | complexity as an option |
|---|
| 173 | </p> |
|---|
| 174 | </div> |
|---|
| 175 | <div class="section" lang="en"> |
|---|
| 176 | <div class="titlepage"><div><div><h3 class="title"> |
|---|
| 177 | <a name="string_algo.replace"></a>Replace Algorithms</h3></div></div></div> |
|---|
| 178 | <p> |
|---|
| 179 | The implementation of replace algorithms follows the layered structure of the library. The |
|---|
| 180 | lower layer implements generic substitution of a range in the input sequence. |
|---|
| 181 | This layer takes a <a href="concept.html#string_algo.finder_concept" title="Finder Concept">Finder</a> object and a |
|---|
| 182 | <a href="concept.html#string_algo.formatter_concept" title="Formatter concept">Formatter</a> object as an input. These two |
|---|
| 183 | functors define what to replace and what to replace it with. The upper layer functions |
|---|
| 184 | are just wrapping calls to the lower layer. Finders are shared with the find and split facility. |
|---|
| 185 | </p> |
|---|
| 186 | <p> |
|---|
| 187 | As usual, the implementation of the lower layer is designed to work with a generic sequence while |
|---|
| 188 | taking advantage of specific features if possible |
|---|
| 189 | (by using <a href="design.html#string_algo.sequence_traits" title="Sequence Traits">Sequence traits</a>) |
|---|
| 190 | </p> |
|---|
| 191 | </div> |
|---|
| 192 | <div class="section" lang="en"> |
|---|
| 193 | <div class="titlepage"><div><div><h3 class="title"> |
|---|
| 194 | <a name="string_algo.split"></a>Find Iterators & Split Algorithms</h3></div></div></div> |
|---|
| 195 | <p> |
|---|
| 196 | Find iterators are a logical extension of the <a href="design.html#string_algo.find" title="Find Algorithms">find facility</a>. |
|---|
| 197 | Instead of searching for one match, the whole input can be iteratively searched for multiple matches. |
|---|
| 198 | The result of the search is then used to partition the input. It depends on the algorithms which parts |
|---|
| 199 | are returned as the result. They can be the matching parts (<code class="computeroutput"><a href="../boost/algorithm/find_iterator.html" title="Class template find_iterator">find_iterator</a></code>) of the parts in |
|---|
| 200 | between (<code class="computeroutput"><a href="../boost/algorithm/split_iterator.html" title="Class template split_iterator">split_iterator</a></code>). |
|---|
| 201 | </p> |
|---|
| 202 | <p> |
|---|
| 203 | In addition the split algorithms like <code class="computeroutput"><a href="../boost/algorithm/find_all.html" title="Function template find_all">find_all()</a></code> and <code class="computeroutput"><a href="../id691162-bb.html" title="Function template split">split()</a></code> |
|---|
| 204 | can simplify the common operations. They use a find iterator to search the whole input and copy the |
|---|
| 205 | matches they found into the supplied container. |
|---|
| 206 | </p> |
|---|
| 207 | </div> |
|---|
| 208 | <div class="section" lang="en"> |
|---|
| 209 | <div class="titlepage"><div><div><h3 class="title"> |
|---|
| 210 | <a name="string_algo.exception"></a>Exception Safety</h3></div></div></div> |
|---|
| 211 | <p> |
|---|
| 212 | The library requires that all operations on types used as template |
|---|
| 213 | or function arguments provide the <span class="emphasis"><em>basic exception-safety guarantee</em></span>. |
|---|
| 214 | In turn, all functions and algorithms in this library, except where stated |
|---|
| 215 | otherwise, will provide the <span class="emphasis"><em>basic exception-safety guarantee</em></span>. |
|---|
| 216 | In other words: |
|---|
| 217 | The library maintains its invariants and does not leak resources in |
|---|
| 218 | the face of exceptions. Some library operations give stronger |
|---|
| 219 | guarantees, which are documented on an individual basis. |
|---|
| 220 | </p> |
|---|
| 221 | <p> |
|---|
| 222 | Some functions can provide the <span class="emphasis"><em>strong exception-safety guarantee</em></span>. |
|---|
| 223 | That means that following statements are true: |
|---|
| 224 | </p> |
|---|
| 225 | <div class="itemizedlist"><ul type="disc"> |
|---|
| 226 | <li> |
|---|
| 227 | If an exception is thrown, there are no effects other than those |
|---|
| 228 | of the function |
|---|
| 229 | </li> |
|---|
| 230 | <li> |
|---|
| 231 | If an exception is thrown other than by the function, there are no effects |
|---|
| 232 | </li> |
|---|
| 233 | </ul></div> |
|---|
| 234 | <p> |
|---|
| 235 | This guarantee can be provided under the condition that the operations |
|---|
| 236 | on the types used for arguments for these functions either |
|---|
| 237 | provide the strong exception guarantee or do not alter the global state . |
|---|
| 238 | </p> |
|---|
| 239 | <p> |
|---|
| 240 | In the reference, under the term <span class="emphasis"><em>strong exception-safety guarantee</em></span>, we mean the |
|---|
| 241 | guarantee as defined above. |
|---|
| 242 | </p> |
|---|
| 243 | <p> |
|---|
| 244 | For more information about the exception safety topics, follow this |
|---|
| 245 | <a href="../../../more/generic_exception_safety.html" target="_top">link</a> |
|---|
| 246 | </p> |
|---|
| 247 | </div> |
|---|
| 248 | </div> |
|---|
| 249 | <table width="100%"><tr> |
|---|
| 250 | <td align="left"><small><p>Last revised: August 16, 2006 at 07:10:48 GMT</p></small></td> |
|---|
| 251 | <td align="right"><small>Copyright © 2002-2004 Pavol Droba</small></td> |
|---|
| 252 | </tr></table> |
|---|
| 253 | <hr> |
|---|
| 254 | <div class="spirit-nav"> |
|---|
| 255 | <a accesskey="p" href="quickref.html"><img src="../images/prev.png" alt="Prev"></a><a accesskey="u" href="../string_algo.html"><img src="../images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../images/home.png" alt="Home"></a><a accesskey="n" href="concept.html"><img src="../images/next.png" alt="Next"></a> |
|---|
| 256 | </div> |
|---|
| 257 | </body> |
|---|
| 258 | </html> |
|---|