1 //---------------------------------------------------------------------------//
2 // Copyright (c) 2013 Kyle Lutz <kyle.r.lutz@gmail.com>
3 //
4 // Distributed under the Boost Software License, Version 1.0
5 // See accompanying file LICENSE_1_0.txt or copy at
6 // http://www.boost.org/LICENSE_1_0.txt
7 //
8 // See http://boostorg.github.com/compute for more information.
9 //---------------------------------------------------------------------------//
10
11 #ifndef BOOST_COMPUTE_CONTAINER_BASIC_STRING_HPP
12 #define BOOST_COMPUTE_CONTAINER_BASIC_STRING_HPP
13
14 #include <string>
15 #include <cstring>
16
17 #include <boost/compute/cl.hpp>
18 #include <boost/compute/algorithm/find.hpp>
19 #include <boost/compute/algorithm/search.hpp>
20 #include <boost/compute/container/vector.hpp>
21 #include <boost/compute/system.hpp>
22 #include <boost/compute/command_queue.hpp>
23 #include <iosfwd>
24
25 namespace boost {
26 namespace compute {
27
28 /// \class basic_string
29 /// \brief A template for a dynamically-sized character sequence.
30 ///
31 /// The \c basic_string class provides a generic template for a dynamically-
32 /// sized character sequence. This is most commonly used through the \c string
33 /// typedef (for \c basic_string<char>).
34 ///
35 /// For example, to create a string on the device with its contents copied
36 /// from a C-string on the host:
37 /// \code
38 /// boost::compute::string str("hello, world!");
39 /// \endcode
40 ///
41 /// \see \ref vector "vector<T>"
42 template<class CharT, class Traits = std::char_traits<CharT> >
43 class basic_string
44 {
45 public:
46 typedef Traits traits_type;
47 typedef typename Traits::char_type value_type;
48 typedef size_t size_type;
49 static const size_type npos = size_type(-1);
50 typedef typename ::boost::compute::vector<CharT>::reference reference;
51 typedef typename ::boost::compute::vector<CharT>::const_reference const_reference;
52 typedef typename ::boost::compute::vector<CharT>::iterator iterator;
53 typedef typename ::boost::compute::vector<CharT>::const_iterator const_iterator;
54 typedef typename ::boost::compute::vector<CharT>::reverse_iterator reverse_iterator;
55 typedef typename ::boost::compute::vector<CharT>::const_reverse_iterator const_reverse_iterator;
56
basic_string()57 basic_string()
58 {
59 }
60
basic_string(size_type count,CharT ch)61 basic_string(size_type count, CharT ch)
62 : m_data(count)
63 {
64 std::fill(m_data.begin(), m_data.end(), ch);
65 }
66
basic_string(const basic_string & other,size_type pos,size_type count=npos)67 basic_string(const basic_string &other,
68 size_type pos,
69 size_type count = npos)
70 : m_data(other.begin() + pos,
71 other.begin() + (std::min)(other.size(), count))
72 {
73 }
74
basic_string(const char * s,size_type count)75 basic_string(const char *s, size_type count)
76 : m_data(s, s + count)
77 {
78 }
79
basic_string(const char * s)80 basic_string(const char *s)
81 : m_data(s, s + std::strlen(s))
82 {
83 }
84
85 template<class InputIterator>
basic_string(InputIterator first,InputIterator last)86 basic_string(InputIterator first, InputIterator last)
87 : m_data(first, last)
88 {
89 }
90
basic_string(const basic_string<CharT,Traits> & other)91 basic_string(const basic_string<CharT, Traits> &other)
92 : m_data(other.m_data)
93 {
94 }
95
operator =(const basic_string<CharT,Traits> & other)96 basic_string<CharT, Traits>& operator=(const basic_string<CharT, Traits> &other)
97 {
98 if(this != &other){
99 m_data = other.m_data;
100 }
101
102 return *this;
103 }
104
~basic_string()105 ~basic_string()
106 {
107 }
108
at(size_type pos)109 reference at(size_type pos)
110 {
111 return m_data.at(pos);
112 }
113
at(size_type pos) const114 const_reference at(size_type pos) const
115 {
116 return m_data.at(pos);
117 }
118
operator [](size_type pos)119 reference operator[](size_type pos)
120 {
121 return m_data[pos];
122 }
123
operator [](size_type pos) const124 const_reference operator[](size_type pos) const
125 {
126 return m_data[pos];
127 }
128
front()129 reference front()
130 {
131 return m_data.front();
132 }
133
front() const134 const_reference front() const
135 {
136 return m_data.front();
137 }
138
back()139 reference back()
140 {
141 return m_data.back();
142 }
143
back() const144 const_reference back() const
145 {
146 return m_data.back();
147 }
148
begin()149 iterator begin()
150 {
151 return m_data.begin();
152 }
153
begin() const154 const_iterator begin() const
155 {
156 return m_data.begin();
157 }
158
cbegin() const159 const_iterator cbegin() const
160 {
161 return m_data.cbegin();
162 }
163
end()164 iterator end()
165 {
166 return m_data.end();
167 }
168
end() const169 const_iterator end() const
170 {
171 return m_data.end();
172 }
173
cend() const174 const_iterator cend() const
175 {
176 return m_data.cend();
177 }
178
rbegin()179 reverse_iterator rbegin()
180 {
181 return m_data.rbegin();
182 }
183
rbegin() const184 const_reverse_iterator rbegin() const
185 {
186 return m_data.rbegin();
187 }
188
crbegin() const189 const_reverse_iterator crbegin() const
190 {
191 return m_data.crbegin();
192 }
193
rend()194 reverse_iterator rend()
195 {
196 return m_data.rend();
197 }
198
rend() const199 const_reverse_iterator rend() const
200 {
201 return m_data.rend();
202 }
203
crend() const204 const_reverse_iterator crend() const
205 {
206 return m_data.crend();
207 }
208
empty() const209 bool empty() const
210 {
211 return m_data.empty();
212 }
213
size() const214 size_type size() const
215 {
216 return m_data.size();
217 }
218
length() const219 size_type length() const
220 {
221 return m_data.size();
222 }
223
max_size() const224 size_type max_size() const
225 {
226 return m_data.max_size();
227 }
228
reserve(size_type size)229 void reserve(size_type size)
230 {
231 m_data.reserve(size);
232 }
233
capacity() const234 size_type capacity() const
235 {
236 return m_data.capacity();
237 }
238
shrink_to_fit()239 void shrink_to_fit()
240 {
241 m_data.shrink_to_fit();
242 }
243
clear()244 void clear()
245 {
246 m_data.clear();
247 }
248
swap(basic_string<CharT,Traits> & other)249 void swap(basic_string<CharT, Traits> &other)
250 {
251 if(this != &other)
252 {
253 ::boost::compute::vector<CharT> temp_data(other.m_data);
254 other.m_data = m_data;
255 m_data = temp_data;
256 }
257 }
258
substr(size_type pos=0,size_type count=npos) const259 basic_string<CharT, Traits> substr(size_type pos = 0,
260 size_type count = npos) const
261 {
262 return basic_string<CharT, Traits>(*this, pos, count);
263 }
264
265 /// Finds the first character \p ch
find(CharT ch,size_type pos=0) const266 size_type find(CharT ch, size_type pos = 0) const
267 {
268 const_iterator iter = ::boost::compute::find(begin() + pos, end(), ch);
269 if(iter == end()){
270 return npos;
271 }
272 else {
273 return static_cast<size_type>(std::distance(begin(), iter));
274 }
275 }
276
277 /// Finds the first substring equal to \p str
find(basic_string & str,size_type pos=0) const278 size_type find(basic_string& str, size_type pos = 0) const
279 {
280 const_iterator iter = ::boost::compute::search(begin() + pos, end(),
281 str.begin(), str.end());
282 if(iter == end()){
283 return npos;
284 }
285 else {
286 return static_cast<size_type>(std::distance(begin(), iter));
287 }
288 }
289
290 /// Finds the first substring equal to the character string
291 /// pointed to by \p s.
292 /// The length of the string is determined by the first null character.
293 ///
294 /// For example, the following code
295 /// \snippet test/test_string.cpp string_find
296 ///
297 /// will return 5 as position.
find(const char * s,size_type pos=0) const298 size_type find(const char* s, size_type pos = 0) const
299 {
300 basic_string str(s);
301 const_iterator iter = ::boost::compute::search(begin() + pos, end(),
302 str.begin(), str.end());
303 if(iter == end()){
304 return npos;
305 }
306 else {
307 return static_cast<size_type>(std::distance(begin(), iter));
308 }
309 }
310
311 private:
312 ::boost::compute::vector<CharT> m_data;
313 };
314
315 template<class CharT, class Traits>
316 std::ostream&
operator <<(std::ostream & stream,boost::compute::basic_string<CharT,Traits> const & outStr)317 operator<<(std::ostream& stream,
318 boost::compute::basic_string<CharT, Traits>const& outStr)
319 {
320 command_queue queue = ::boost::compute::system::default_queue();
321 boost::compute::copy(outStr.begin(),
322 outStr.end(),
323 std::ostream_iterator<CharT>(stream),
324 queue);
325 return stream;
326 }
327
328 } // end compute namespace
329 } // end boost namespace
330
331 #endif // BOOST_COMPUTE_CONTAINER_BASIC_STRING_HPP
332