1 //---------------------------------------------------------------------------//
2 // Copyright (c) 2013 Kyle Lutz <kyle.r.lutz@gmail.com>
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
8 // See http://boostorg.github.com/compute for more information.
9 //---------------------------------------------------------------------------//
11 #ifndef BOOST_COMPUTE_CONTAINER_BASIC_STRING_HPP
12 #define BOOST_COMPUTE_CONTAINER_BASIC_STRING_HPP
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>
28 /// \class basic_string
29 /// \brief A template for a dynamically-sized character sequence.
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>).
35 /// For example, to create a string on the device with its contents copied
36 /// from a C-string on the host:
38 /// boost::compute::string str("hello, world!");
41 /// \see \ref vector "vector<T>"
42 template<class CharT, class Traits = std::char_traits<CharT> >
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;
61 basic_string(size_type count, CharT ch)
64 std::fill(m_data.begin(), m_data.end(), ch);
67 basic_string(const basic_string &other,
69 size_type count = npos)
70 : m_data(other.begin() + pos,
71 other.begin() + (std::min)(other.size(), count))
75 basic_string(const char *s, size_type count)
76 : m_data(s, s + count)
80 basic_string(const char *s)
81 : m_data(s, s + std::strlen(s))
85 template<class InputIterator>
86 basic_string(InputIterator first, InputIterator last)
91 basic_string(const basic_string<CharT, Traits> &other)
92 : m_data(other.m_data)
96 basic_string<CharT, Traits>& operator=(const basic_string<CharT, Traits> &other)
99 m_data = other.m_data;
109 reference at(size_type pos)
111 return m_data.at(pos);
114 const_reference at(size_type pos) const
116 return m_data.at(pos);
119 reference operator[](size_type pos)
124 const_reference operator[](size_type pos) const
131 return m_data.front();
134 const_reference front() const
136 return m_data.front();
141 return m_data.back();
144 const_reference back() const
146 return m_data.back();
151 return m_data.begin();
154 const_iterator begin() const
156 return m_data.begin();
159 const_iterator cbegin() const
161 return m_data.cbegin();
169 const_iterator end() const
174 const_iterator cend() const
176 return m_data.cend();
179 reverse_iterator rbegin()
181 return m_data.rbegin();
184 const_reverse_iterator rbegin() const
186 return m_data.rbegin();
189 const_reverse_iterator crbegin() const
191 return m_data.crbegin();
194 reverse_iterator rend()
196 return m_data.rend();
199 const_reverse_iterator rend() const
201 return m_data.rend();
204 const_reverse_iterator crend() const
206 return m_data.crend();
211 return m_data.empty();
214 size_type size() const
216 return m_data.size();
219 size_type length() const
221 return m_data.size();
224 size_type max_size() const
226 return m_data.max_size();
229 void reserve(size_type size)
231 m_data.reserve(size);
234 size_type capacity() const
236 return m_data.capacity();
241 m_data.shrink_to_fit();
249 void swap(basic_string<CharT, Traits> &other)
253 ::boost::compute::vector<CharT> temp_data(other.m_data);
254 other.m_data = m_data;
259 basic_string<CharT, Traits> substr(size_type pos = 0,
260 size_type count = npos) const
262 return basic_string<CharT, Traits>(*this, pos, count);
265 /// Finds the first character \p ch
266 size_type find(CharT ch, size_type pos = 0) const
268 const_iterator iter = ::boost::compute::find(begin() + pos, end(), ch);
273 return static_cast<size_type>(std::distance(begin(), iter));
277 /// Finds the first substring equal to \p str
278 size_type find(basic_string& str, size_type pos = 0) const
280 const_iterator iter = ::boost::compute::search(begin() + pos, end(),
281 str.begin(), str.end());
286 return static_cast<size_type>(std::distance(begin(), iter));
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.
294 /// For example, the following code
295 /// \snippet test/test_string.cpp string_find
297 /// will return 5 as position.
298 size_type find(const char* s, size_type pos = 0) const
301 const_iterator iter = ::boost::compute::search(begin() + pos, end(),
302 str.begin(), str.end());
307 return static_cast<size_type>(std::distance(begin(), iter));
312 ::boost::compute::vector<CharT> m_data;
315 template<class CharT, class Traits>
317 operator<<(std::ostream& stream,
318 boost::compute::basic_string<CharT, Traits>const& outStr)
320 command_queue queue = ::boost::compute::system::default_queue();
321 boost::compute::copy(outStr.begin(),
323 std::ostream_iterator<CharT>(stream),
328 } // end compute namespace
329 } // end boost namespace
331 #endif // BOOST_COMPUTE_CONTAINER_BASIC_STRING_HPP