summaryrefslogtreecommitdiff
path: root/libs/algorithm/string/doc/concept.xml
blob: 267ad79288b743184229c8e1f451fb1e8e22a90a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE library PUBLIC "-//Boost//DTD BoostBook XML V1.0//EN"
"http://www.boost.org/tools/boostbook/dtd/boostbook.dtd">

<!-- Copyright (c) 2002-2006 Pavol Droba.
     Subject to the Boost Software License, Version 1.0. 
     (See accompanying file LICENSE_1_0.txt or  http://www.boost.org/LICENSE_1_0.txt)
-->

<section id="string_algo.concept" last-revision="$Date: 2008-06-19 08:07:24 -0400 (Thu, 19 Jun 2008) $">
    <title>Concepts</title>

    <using-namespace name="boost"/>
    <using-namespace name="boost::algorithm"/>

    <section>   
        <title>Definitions</title>
        
        <table>
            <title>Notation</title>
            <tgroup cols="2" align="left">
                <tbody>
                    <row>
                        <entry><code>F</code></entry>
                        <entry>A type that is a model of Finder</entry>
                    </row>
                    <row>
                        <entry><code>Fmt</code></entry>
                        <entry>A type that is a model of Formatter</entry>
                    </row>
                    <row>
                        <entry><code>Iter</code></entry>
                        <entry>
                            Iterator Type
                        </entry>
                    </row>
                    <row>
                        <entry><code>f</code></entry>
                        <entry>Object of type <code>F</code></entry>
                    </row>
                    <row>
                        <entry><code>fmt</code></entry>
                        <entry>Object of type <code>Fmt</code></entry>
                    </row>
                    <row>
                        <entry><code>i,j</code></entry>
                        <entry>Objects of type <code>Iter</code></entry>
                    </row>
                    </tbody>
            </tgroup>
        </table>
    </section>

    <section id="string_algo.finder_concept">
        <title>Finder Concept</title>

        <para>
            Finder is a functor which searches for an arbitrary part of a container. 
            The result of the search is given as an <classname>iterator_range</classname> 
            delimiting the selected part.
        </para>

        <table>             
            <title>Valid Expressions</title>
            <tgroup cols="3" align="left">
                <thead>
                    <row>   
                        <entry>Expression</entry>
                        <entry>Return Type</entry>
                        <entry>Effects</entry>
                    </row>
                </thead>
                <tbody>
                    <row>
                        <entry><code>f(i,j)</code></entry>
                        <entry>Convertible to <code>iterator_range&lt;Iter&gt;</code></entry>
                        <entry>Perform the search on the interval [i,j) and returns the result of the search</entry>
                    </row>
                </tbody>
            </tgroup>
        </table>

        <para>
            Various algorithms need to perform a search in a container and a Finder is a generalization of such
            search operations that allows algorithms to abstract from searching. For instance, generic replace
            algorithms can replace any part of the input, and the Finder is used to select the desired one.
        </para>
        <para>
            Note, that it is only required that the finder works with a particular iterator type. However,
            a Finder operation can be defined as a template, allowing the Finder to work with any iterator.
        </para>
        <para>
            <emphasis role="bold">Examples</emphasis>
        </para>
        <para> 
            <itemizedlist>
                <listitem>
                    Finder implemented as a class. This Finder always returns the whole input as a match. <code>operator()</code>
                    is templated, so that the finder can be used on any iterator type.
                    
                    <programlisting>
struct simple_finder
{
    template&lt;typename ForwardIteratorT&gt;
    boost::iterator_range&lt;ForwardIteratorT&gt; operator()(
        ForwardIteratorT Begin,
        ForwardIteratorT End )
    {
        return boost::make_range( Begin, End );
    }
};
        </programlisting>
                </listitem>
                <listitem>
                    Function Finder. Finder can be any function object. That is, any ordinary function with the
                    required signature can be used as well. However, such a function can be used only for
                    a specific iterator type. 
                    
                    <programlisting>
boost::iterator_range&lt;std::string&gt; simple_finder(
    std::string::const_iterator Begin,
    std::string::const_iterator End )
{
    return boost::make_range( Begin, End );
}
        </programlisting>
                </listitem>
            </itemizedlist>
        </para> 
    </section>
    <section id="string_algo.formatter_concept">
        <title>Formatter concept</title>

        <para>
            Formatters are used by <link linkend="string_algo.replace">replace algorithms</link>.
            They are used in close combination with finders.
            A formatter is a functor, which takes a result from a Finder operation and transforms it in a specific way. 
            The operation of the formatter can use additional information provided by a specific finder,
            for example <functionname>regex_formatter()</functionname> uses the match information from
            <functionname>regex_finder()</functionname> to format the result of formatter operation.
        </para>
    
        <table>
            <title>Valid Expressions</title>
            <tgroup cols="3" align="left">
                <thead>
                    <row>   
                        <entry>Expression</entry>
                        <entry>Return Type</entry>
                        <entry>Effects</entry>
                    </row>
                </thead>
                <tbody>
                   <row>
                        <entry><code>fmt(f(i,j))</code></entry>
                        <entry>A container type, accessible using container traits</entry>
                        <entry>Formats the result of the finder operation</entry>
                    </row>
                </tbody>
            </tgroup>
        </table>

        <para>
            Similarly to finders, formatters generalize format operations. When a finder is used to 
            select a part of the input, formatter takes this selection and performs some formating
            on it. Algorithms can abstract from formating using a formatter.
        </para>
        <para>
            <emphasis role="bold">Examples</emphasis>
        </para>
        <para> 
            <itemizedlist>
                <listitem>
                    Formatter implemented as a class. This Formatter does not perform any formating and 
                    returns the match, repackaged. <code>operator()</code>
                    is templated, so that the Formatter can be used on any Finder type.
                    
                    <programlisting>
struct simple_formatter
{
    template&lt;typename FindResultT&gt;
    std::string operator()( const FindResultT&amp; Match )
    {
        std::string Temp( Match.begin(), Match.end() );
        return Temp;
    }
};
                </programlisting>
                </listitem>
                <listitem>
                    Function Formatter. Similarly to Finder, Formatter can be any function object. 
                    However, as a function, it can be used only with a specific Finder type. 
                  
                    <programlisting>
std::string simple_formatter( boost::iterator_range&lt;std::string::const_iterator&gt;&amp; Match )
{
    std::string Temp( Match.begin(), Match.end() );
    return Temp;
}
                    </programlisting>
                </listitem>
            </itemizedlist>
        </para> 
     </section>
</section>