]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/compute/include/boost/compute/container/basic_string.hpp
bump version to 12.2.2-pve1
[ceph.git] / ceph / src / boost / libs / compute / include / boost / compute / container / basic_string.hpp
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
57 basic_string()
58 {
59 }
60
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
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
75 basic_string(const char *s, size_type count)
76 : m_data(s, s + count)
77 {
78 }
79
80 basic_string(const char *s)
81 : m_data(s, s + std::strlen(s))
82 {
83 }
84
85 template<class InputIterator>
86 basic_string(InputIterator first, InputIterator last)
87 : m_data(first, last)
88 {
89 }
90
91 basic_string(const basic_string<CharT, Traits> &other)
92 : m_data(other.m_data)
93 {
94 }
95
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
105 ~basic_string()
106 {
107 }
108
109 reference at(size_type pos)
110 {
111 return m_data.at(pos);
112 }
113
114 const_reference at(size_type pos) const
115 {
116 return m_data.at(pos);
117 }
118
119 reference operator[](size_type pos)
120 {
121 return m_data[pos];
122 }
123
124 const_reference operator[](size_type pos) const
125 {
126 return m_data[pos];
127 }
128
129 reference front()
130 {
131 return m_data.front();
132 }
133
134 const_reference front() const
135 {
136 return m_data.front();
137 }
138
139 reference back()
140 {
141 return m_data.back();
142 }
143
144 const_reference back() const
145 {
146 return m_data.back();
147 }
148
149 iterator begin()
150 {
151 return m_data.begin();
152 }
153
154 const_iterator begin() const
155 {
156 return m_data.begin();
157 }
158
159 const_iterator cbegin() const
160 {
161 return m_data.cbegin();
162 }
163
164 iterator end()
165 {
166 return m_data.end();
167 }
168
169 const_iterator end() const
170 {
171 return m_data.end();
172 }
173
174 const_iterator cend() const
175 {
176 return m_data.cend();
177 }
178
179 reverse_iterator rbegin()
180 {
181 return m_data.rbegin();
182 }
183
184 const_reverse_iterator rbegin() const
185 {
186 return m_data.rbegin();
187 }
188
189 const_reverse_iterator crbegin() const
190 {
191 return m_data.crbegin();
192 }
193
194 reverse_iterator rend()
195 {
196 return m_data.rend();
197 }
198
199 const_reverse_iterator rend() const
200 {
201 return m_data.rend();
202 }
203
204 const_reverse_iterator crend() const
205 {
206 return m_data.crend();
207 }
208
209 bool empty() const
210 {
211 return m_data.empty();
212 }
213
214 size_type size() const
215 {
216 return m_data.size();
217 }
218
219 size_type length() const
220 {
221 return m_data.size();
222 }
223
224 size_type max_size() const
225 {
226 return m_data.max_size();
227 }
228
229 void reserve(size_type size)
230 {
231 m_data.reserve(size);
232 }
233
234 size_type capacity() const
235 {
236 return m_data.capacity();
237 }
238
239 void shrink_to_fit()
240 {
241 m_data.shrink_to_fit();
242 }
243
244 void clear()
245 {
246 m_data.clear();
247 }
248
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
259 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
266 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
278 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.
298 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&
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