Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

added boost

File size: 7.8 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.concept" last-revision="$Date: 2004/07/16 09:06:39 $">
5    <title>Concepts</title>
6
7    <using-namespace name="boost"/>
8    <using-namespace name="boost::algorithm"/>
9
10    <section>   
11        <title>Definitions</title>
12       
13        <table>
14            <title>Notation</title>
15            <tgroup cols="2" align="left">
16                <tbody>
17                    <row>
18                        <entry><code>F</code></entry>
19                        <entry>A type that is a model of Finder</entry>
20                    </row>
21                    <row>
22                        <entry><code>Fmt</code></entry>
23                        <entry>A type that is a model of Formatter</entry>
24                    </row>
25                    <row>
26                        <entry><code>Iter</code></entry>
27                        <entry>
28                            Iterator Type
29                        </entry>
30                    </row>
31                    <row>
32                        <entry><code>f</code></entry>
33                        <entry>Object of type <code>F</code></entry>
34                    </row>
35                    <row>
36                        <entry><code>fmt</code></entry>
37                        <entry>Object of type <code>Fmt</code></entry>
38                    </row>
39                    <row>
40                        <entry><code>i,j</code></entry>
41                        <entry>Objects of type <code>Iter</code></entry>
42                    </row>
43                    </tbody>
44            </tgroup>
45        </table>
46    </section>
47
48    <section id="string_algo.finder_concept">
49        <title>Finder Concept</title>
50
51        <para>
52            Finder is a functor which searches for an arbitrary part of a container.
53            The result of the search is given as an <classname>iterator_range</classname> 
54            delimiting the selected part.
55        </para>
56
57        <table>             
58            <title>Valid Expressions</title>
59            <tgroup cols="3" align="left">
60                <thead>
61                    <row>   
62                        <entry>Expression</entry>
63                        <entry>Return Type</entry>
64                        <entry>Effects</entry>
65                    </row>
66                </thead>
67                <tbody>
68                    <row>
69                        <entry><code>f(i,j)</code></entry>
70                        <entry>Convertible to <code>iterator_range&lt;Iter&gt;</code></entry>
71                        <entry>Perform the search on the interval [i,j) and returns the result of the search</entry>
72                    </row>
73                </tbody>
74            </tgroup>
75        </table>
76
77        <para>
78            Various algorithms need to perform a search in a container and a Finder is a generalization of such
79            search operations that allows algorithms to abstract from searching. For instance, generic replace
80            algorithms can replace any part of the input, and the Finder is used to select the desired one.
81        </para>
82        <para>
83            Note, that it is only required that the finder works with a particular iterator type. However,
84            a Finder operation can be defined as a template, allowing the Finder to work with any iterator.
85        </para>
86        <para>
87            <emphasis role="bold">Examples</emphasis>
88        </para>
89        <para> 
90            <itemizedlist>
91                <listitem>
92                    Finder implemented as a class. This Finder always returns the whole input as a match. <code>operator()</code>
93                    is templated, so that the finder can be used on any iterator type.
94                   
95                    <programlisting>
96struct simple_finder
97{
98    template&lt;typename ForwardIteratorT&gt;
99    boost::iterator_range&lt;ForwardIterator&gt; operator()(
100        ForwardIteratorT Begin,
101        ForwardIteratorT End )
102    {
103        return boost::make_range( Begin, End );
104    }
105};
106        </programlisting>
107                </listitem>
108                <listitem>
109                    Function Finder. Finder can be any function object. That is, any ordinary function with the
110                    required signature can be used as well. However, such a function can be used only for
111                    a specific iterator type.
112                   
113                    <programlisting>
114boost::iterator_range&lt;std::string&gt; simple_finder(
115    std::string::const_iterator Begin,
116    std::string::const_iterator End )
117{
118    return boost::make_range( Begin, End );
119}
120        </programlisting>
121                </listitem>
122            </itemizedlist>
123        </para> 
124    </section>
125    <section id="string_algo.formatter_concept">
126        <title>Formatter concept</title>
127
128        <para>
129            Formatters are used by <link linkend="string_algo.replace">replace algorithms</link>.
130            They are used in close combination with finders.
131            A formatter is a functor, which takes a result from a Finder operation and transforms it in a specific way.
132            The operation of the formatter can use additional information provided by a specific finder,
133            for example <functionname>regex_formatter()</functionname> uses the match information from
134            <functionname>regex_finder()</functionname> to format the result of formatter operation.
135        </para>
136   
137        <table>
138            <title>Valid Expressions</title>
139            <tgroup cols="3" align="left">
140                <thead>
141                    <row>   
142                        <entry>Expression</entry>
143                        <entry>Return Type</entry>
144                        <entry>Effects</entry>
145                    </row>
146                </thead>
147                <tbody>
148                   <row>
149                        <entry><code>fmt(f(i,j))</code></entry>
150                        <entry>A container type, accessible using container traits</entry>
151                        <entry>Formats the result of the finder operation</entry>
152                    </row>
153                </tbody>
154            </tgroup>
155        </table>
156
157        <para>
158            Similarly to finders, formatters generalize format operations. When a finder is used to
159            select a part of the input, formatter takes this selection and performs some formating
160            on it. Algorithms can abstract from formating using a formatter.
161        </para>
162        <para>
163            <emphasis role="bold">Examples</emphasis>
164        </para>
165        <para> 
166            <itemizedlist>
167                <listitem>
168                    Formatter implemented as a class. This Formatter does not perform any formating and
169                    returns the match, repackaged. <code>operator()</code>
170                    is templated, so that the Formatter can be used on any Finder type.
171                   
172                    <programlisting>
173struct simple_formatter
174{
175    template&lt;typename FindResultT&gt;
176    std::string operator()( const FindResultT&amp; Match )
177    {
178        std::string Temp( Match.begin(), Match.end() );
179        return Temp;
180    }
181};
182                </programlisting>
183                </listitem>
184                <listitem>
185                    Function Formatter. Similarly to Finder, Formatter can be any function object.
186                    However, as a function, it can be used only with a specific Finder type.
187                 
188                    <programlisting>
189std::string simple_formatter( boost::iterator_range&lt;std::string::const_iterator&gt;&amp; Match )
190{
191    std::string Temp( Match.begin(), Match.end() );
192    return Temp;
193}
194                    </programlisting>
195                </listitem>
196            </itemizedlist>
197        </para> 
198     </section>
199</section>
Note: See TracBrowser for help on using the repository browser.