liuxiaolong
2021-07-20 58d904a328c0d849769b483e901a0be9426b8209
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
//---------------------------------------------------------------------------//
// Copyright (c) 2013 Kyle Lutz <kyle.r.lutz@gmail.com>
//
// Distributed under the Boost Software License, Version 1.0
// See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt
//
// See http://boostorg.github.com/compute for more information.
//---------------------------------------------------------------------------//
 
#ifndef BOOST_COMPUTE_CONTAINER_BASIC_STRING_HPP
#define BOOST_COMPUTE_CONTAINER_BASIC_STRING_HPP
 
#include <string>
#include <cstring>
 
#include <boost/compute/cl.hpp>
#include <boost/compute/algorithm/find.hpp>
#include <boost/compute/algorithm/search.hpp>
#include <boost/compute/container/vector.hpp>
#include <boost/compute/system.hpp>
#include <boost/compute/command_queue.hpp>
#include <iosfwd>
 
namespace boost {
namespace compute {
 
/// \class basic_string
/// \brief A template for a dynamically-sized character sequence.
///
/// The \c basic_string class provides a generic template for a dynamically-
/// sized character sequence. This is most commonly used through the \c string
/// typedef (for \c basic_string<char>).
///
/// For example, to create a string on the device with its contents copied
/// from a C-string on the host:
/// \code
/// boost::compute::string str("hello, world!");
/// \endcode
///
/// \see \ref vector "vector<T>"
template<class CharT, class Traits = std::char_traits<CharT> >
class basic_string
{
public:
    typedef Traits traits_type;
    typedef typename Traits::char_type value_type;
    typedef size_t size_type;
    static const size_type npos = size_type(-1);
    typedef typename ::boost::compute::vector<CharT>::reference reference;
    typedef typename ::boost::compute::vector<CharT>::const_reference const_reference;
    typedef typename ::boost::compute::vector<CharT>::iterator iterator;
    typedef typename ::boost::compute::vector<CharT>::const_iterator const_iterator;
    typedef typename ::boost::compute::vector<CharT>::reverse_iterator reverse_iterator;
    typedef typename ::boost::compute::vector<CharT>::const_reverse_iterator const_reverse_iterator;
 
    basic_string()
    {
    }
 
    basic_string(size_type count, CharT ch)
        : m_data(count)
    {
        std::fill(m_data.begin(), m_data.end(), ch);
    }
 
    basic_string(const basic_string &other,
                 size_type pos,
                 size_type count = npos)
        : m_data(other.begin() + pos,
                 other.begin() + (std::min)(other.size(), count))
    {
    }
 
    basic_string(const char *s, size_type count)
        : m_data(s, s + count)
    {
    }
 
    basic_string(const char *s)
        : m_data(s, s + std::strlen(s))
    {
    }
 
    template<class InputIterator>
    basic_string(InputIterator first, InputIterator last)
        : m_data(first, last)
    {
    }
 
    basic_string(const basic_string<CharT, Traits> &other)
        : m_data(other.m_data)
    {
    }
 
    basic_string<CharT, Traits>& operator=(const basic_string<CharT, Traits> &other)
    {
        if(this != &other){
            m_data = other.m_data;
        }
 
        return *this;
    }
 
    ~basic_string()
    {
    }
 
    reference at(size_type pos)
    {
        return m_data.at(pos);
    }
 
    const_reference at(size_type pos) const
    {
        return m_data.at(pos);
    }
 
    reference operator[](size_type pos)
    {
        return m_data[pos];
    }
 
    const_reference operator[](size_type pos) const
    {
        return m_data[pos];
    }
 
    reference front()
    {
        return m_data.front();
    }
 
    const_reference front() const
    {
        return m_data.front();
    }
 
    reference back()
    {
        return m_data.back();
    }
 
    const_reference back() const
    {
        return m_data.back();
    }
 
    iterator begin()
    {
        return m_data.begin();
    }
 
    const_iterator begin() const
    {
        return m_data.begin();
    }
 
    const_iterator cbegin() const
    {
        return m_data.cbegin();
    }
 
    iterator end()
    {
        return m_data.end();
    }
 
    const_iterator end() const
    {
        return m_data.end();
    }
 
    const_iterator cend() const
    {
        return m_data.cend();
    }
 
    reverse_iterator rbegin()
    {
        return m_data.rbegin();
    }
 
    const_reverse_iterator rbegin() const
    {
        return m_data.rbegin();
    }
 
    const_reverse_iterator crbegin() const
    {
        return m_data.crbegin();
    }
 
    reverse_iterator rend()
    {
        return m_data.rend();
    }
 
    const_reverse_iterator rend() const
    {
        return m_data.rend();
    }
 
    const_reverse_iterator crend() const
    {
        return m_data.crend();
    }
 
    bool empty() const
    {
        return m_data.empty();
    }
 
    size_type size() const
    {
        return m_data.size();
    }
 
    size_type length() const
    {
        return m_data.size();
    }
 
    size_type max_size() const
    {
        return m_data.max_size();
    }
 
    void reserve(size_type size)
    {
        m_data.reserve(size);
    }
 
    size_type capacity() const
    {
        return m_data.capacity();
    }
 
    void shrink_to_fit()
    {
        m_data.shrink_to_fit();
    }
 
    void clear()
    {
        m_data.clear();
    }
 
    void swap(basic_string<CharT, Traits> &other)
    {
        if(this != &other)
        {
            ::boost::compute::vector<CharT> temp_data(other.m_data);
            other.m_data = m_data;
            m_data = temp_data;
        }
    }
 
    basic_string<CharT, Traits> substr(size_type pos = 0,
                                       size_type count = npos) const
    {
        return basic_string<CharT, Traits>(*this, pos, count);
    }
 
    /// Finds the first character \p ch
    size_type find(CharT ch, size_type pos = 0) const
    {
        const_iterator iter = ::boost::compute::find(begin() + pos, end(), ch);
        if(iter == end()){
            return npos;
        }
        else {
            return static_cast<size_type>(std::distance(begin(), iter));
        }
    }
 
    /// Finds the first substring equal to \p str
    size_type find(basic_string& str, size_type pos = 0) const
    {
        const_iterator iter = ::boost::compute::search(begin() + pos, end(),
                                                       str.begin(), str.end());
        if(iter == end()){
            return npos;
        }
        else {
            return static_cast<size_type>(std::distance(begin(), iter));
        }
    }
 
    /// Finds the first substring equal to the character string
    /// pointed to by \p s.
    /// The length of the string is determined by the first null character.
    ///
    /// For example, the following code
    /// \snippet test/test_string.cpp string_find
    ///
    /// will return 5 as position.
    size_type find(const char* s, size_type pos = 0) const
    {
        basic_string str(s);
        const_iterator iter = ::boost::compute::search(begin() + pos, end(),
                                                       str.begin(), str.end());
        if(iter == end()){
            return npos;
        }
        else {
            return static_cast<size_type>(std::distance(begin(), iter));
        }
    }
 
private:
    ::boost::compute::vector<CharT> m_data;
};
 
template<class CharT, class Traits>
std::ostream&
operator<<(std::ostream& stream,
           boost::compute::basic_string<CharT, Traits>const& outStr)
{
    command_queue queue = ::boost::compute::system::default_queue();
    boost::compute::copy(outStr.begin(),
                        outStr.end(),
                        std::ostream_iterator<CharT>(stream),
                        queue);
    return stream;
}
 
} // end compute namespace
} // end boost namespace
 
#endif // BOOST_COMPUTE_CONTAINER_BASIC_STRING_HPP