source: NonGTP/Boost/boost/algorithm/string/detail/sequence.hpp @ 857

Revision 857, 7.4 KB checked in by igarcia, 18 years ago (diff)
Line 
1//  Boost string_algo library sequence.hpp header file  ---------------------------//
2
3//  Copyright Pavol Droba 2002-2003. Use, modification and
4//  distribution is subject to the Boost Software License, Version
5//  1.0. (See accompanying file LICENSE_1_0.txt or copy at
6//  http://www.boost.org/LICENSE_1_0.txt)
7
8//  See http://www.boost.org for updates, documentation, and revision history.
9
10#ifndef BOOST_STRING_DETAIL_SEQUENCE_HPP
11#define BOOST_STRING_DETAIL_SEQUENCE_HPP
12
13#include <boost/algorithm/string/config.hpp>
14#include <boost/mpl/bool.hpp>
15#include <boost/mpl/logical.hpp>
16#include <boost/range/begin.hpp>
17#include <boost/range/end.hpp>
18
19#include <boost/algorithm/string/sequence_traits.hpp>
20
21namespace boost {
22    namespace algorithm {
23        namespace detail {
24
25//  insert helpers  -------------------------------------------------//
26       
27            template< typename InputT, typename ForwardIteratorT >
28            inline void insert(
29                InputT& Input,
30                BOOST_STRING_TYPENAME InputT::iterator At,
31                ForwardIteratorT Begin,
32                ForwardIteratorT End )
33            {
34                Input.insert( At, Begin, End );
35            }
36
37            template< typename InputT, typename InsertT >
38            inline void insert(
39                InputT& Input,
40                BOOST_STRING_TYPENAME InputT::iterator At,
41                const InsertT& Insert )
42            {
43                insert( Input, At, begin(Insert), end(Insert) );
44            }
45           
46//  erase helper  ---------------------------------------------------//
47
48            // Erase a range in the sequence
49            /*
50                Returns the iterator pointing just after the erase subrange
51            */
52            template< typename InputT >
53            inline typename InputT::iterator erase(
54                InputT& Input,
55                BOOST_STRING_TYPENAME InputT::iterator From,
56                BOOST_STRING_TYPENAME InputT::iterator To )
57            {
58                return Input.erase( From, To );
59            }
60
61//  replace helper implementation  ----------------------------------//
62
63            // Optimized version of replace for generic sequence containers
64            // Assumption: insert and erase are expensive
65            template< bool HasConstTimeOperations >
66            struct replace_const_time_helper
67            {
68                template< typename InputT, typename ForwardIteratorT >
69                void operator()(
70                    InputT& Input,
71                    BOOST_STRING_TYPENAME InputT::iterator From,
72                    BOOST_STRING_TYPENAME InputT::iterator To,
73                    ForwardIteratorT Begin,
74                    ForwardIteratorT End )
75                {
76                    // Copy data to the container ( as much as possible )
77                    ForwardIteratorT InsertIt=Begin;
78                    BOOST_STRING_TYPENAME InputT::iterator InputIt=From;
79                    for(; InsertIt!=End && InputIt!=To; InsertIt++, InputIt++ )
80                    {
81                        *InputIt=*InsertIt;
82                    }
83
84                    if ( InsertIt!=End )
85                    {
86                        // Replace sequence is longer, insert it
87                        Input.insert( InputIt, InsertIt, End );
88                    }
89                    else
90                    {
91                        if ( InputIt!=To )
92                        {
93                            // Replace sequence is shorter, erase the rest
94                            Input.erase( InputIt, To );
95                        }
96                    }
97                }
98            };
99
100            template<>
101            struct replace_const_time_helper< true >
102            {
103                // Const-time erase and insert methods -> use them
104                template< typename InputT, typename ForwardIteratorT >
105                void operator()(
106                    InputT& Input,
107                    BOOST_STRING_TYPENAME InputT::iterator From,
108                    BOOST_STRING_TYPENAME InputT::iterator To,
109                    ForwardIteratorT Begin,
110                    ForwardIteratorT End )
111                {
112                    BOOST_STRING_TYPENAME InputT::iterator At=Input.erase( From, To );
113                    if ( Begin!=End )
114                    {
115                        if(!Input.empty())
116                        {
117                            Input.insert( At, Begin, End );
118                        }
119                        else
120                        {
121                            Input.insert( Input.begin(), Begin, End );
122                        }
123                    }
124                }
125            };
126
127            // No native replace method
128            template< bool HasNative >
129            struct replace_native_helper
130            {
131                template< typename InputT, typename ForwardIteratorT >
132                void operator()(
133                    InputT& Input,
134                    BOOST_STRING_TYPENAME InputT::iterator From,
135                    BOOST_STRING_TYPENAME InputT::iterator To,
136                    ForwardIteratorT Begin,
137                    ForwardIteratorT End )
138                {
139                    replace_const_time_helper<
140                        boost::mpl::and_<
141                            has_const_time_insert<InputT>,
142                            has_const_time_erase<InputT> >::value >()(
143                        Input, From, To, Begin, End );
144                }
145            };
146
147            // Container has native replace method
148            template<>
149            struct replace_native_helper< true >
150            {
151                template< typename InputT, typename ForwardIteratorT >
152                void operator()(
153                    InputT& Input,
154                    BOOST_STRING_TYPENAME InputT::iterator From,
155                    BOOST_STRING_TYPENAME InputT::iterator To,
156                    ForwardIteratorT Begin,
157                    ForwardIteratorT End )
158                {
159                    Input.replace( From, To, Begin, End );
160                }
161            };
162
163//  replace helper  -------------------------------------------------//
164       
165            template< typename InputT, typename ForwardIteratorT >
166            inline void replace(
167                InputT& Input,
168                BOOST_STRING_TYPENAME InputT::iterator From,
169                BOOST_STRING_TYPENAME InputT::iterator To,
170                ForwardIteratorT Begin,
171                ForwardIteratorT End )
172            {
173                replace_native_helper< has_native_replace<InputT>::value >()(
174                    Input, From, To, Begin, End );
175            }
176
177            template< typename InputT, typename InsertT >
178            inline void replace(
179                InputT& Input,
180                BOOST_STRING_TYPENAME InputT::iterator From,
181                BOOST_STRING_TYPENAME InputT::iterator To,
182                const InsertT& Insert )
183            {
184                if(From!=To)
185                {
186                    replace( Input, From, To, begin(Insert), end(Insert) );
187                }
188                else
189                {
190                    insert( Input, From, begin(Insert), end(Insert) );
191                }
192            }
193
194        } // namespace detail
195    } // namespace algorithm
196} // namespace boost
197
198
199#endif  // BOOST_STRING_DETAIL_SEQUENCE_HPP
Note: See TracBrowser for help on using the repository browser.