Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_33_1/libs/algorithm/string/doc/design.xml @ 20

Last change on this file since 20 was 12, checked in by landauf, 18 years ago

added boost

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