basic_string.hpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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. #ifndef BOOST_COMPUTE_CONTAINER_BASIC_STRING_HPP
  11. #define BOOST_COMPUTE_CONTAINER_BASIC_STRING_HPP
  12. #include <string>
  13. #include <cstring>
  14. #include <boost/compute/cl.hpp>
  15. #include <boost/compute/algorithm/find.hpp>
  16. #include <boost/compute/algorithm/search.hpp>
  17. #include <boost/compute/container/vector.hpp>
  18. #include <boost/compute/system.hpp>
  19. #include <boost/compute/command_queue.hpp>
  20. #include <iosfwd>
  21. namespace boost {
  22. namespace compute {
  23. /// \class basic_string
  24. /// \brief A template for a dynamically-sized character sequence.
  25. ///
  26. /// The \c basic_string class provides a generic template for a dynamically-
  27. /// sized character sequence. This is most commonly used through the \c string
  28. /// typedef (for \c basic_string<char>).
  29. ///
  30. /// For example, to create a string on the device with its contents copied
  31. /// from a C-string on the host:
  32. /// \code
  33. /// boost::compute::string str("hello, world!");
  34. /// \endcode
  35. ///
  36. /// \see \ref vector "vector<T>"
  37. template<class CharT, class Traits = std::char_traits<CharT> >
  38. class basic_string
  39. {
  40. public:
  41. typedef Traits traits_type;
  42. typedef typename Traits::char_type value_type;
  43. typedef size_t size_type;
  44. static const size_type npos = size_type(-1);
  45. typedef typename ::boost::compute::vector<CharT>::reference reference;
  46. typedef typename ::boost::compute::vector<CharT>::const_reference const_reference;
  47. typedef typename ::boost::compute::vector<CharT>::iterator iterator;
  48. typedef typename ::boost::compute::vector<CharT>::const_iterator const_iterator;
  49. typedef typename ::boost::compute::vector<CharT>::reverse_iterator reverse_iterator;
  50. typedef typename ::boost::compute::vector<CharT>::const_reverse_iterator const_reverse_iterator;
  51. basic_string()
  52. {
  53. }
  54. basic_string(size_type count, CharT ch)
  55. : m_data(count)
  56. {
  57. std::fill(m_data.begin(), m_data.end(), ch);
  58. }
  59. basic_string(const basic_string &other,
  60. size_type pos,
  61. size_type count = npos)
  62. : m_data(other.begin() + pos,
  63. other.begin() + (std::min)(other.size(), count))
  64. {
  65. }
  66. basic_string(const char *s, size_type count)
  67. : m_data(s, s + count)
  68. {
  69. }
  70. basic_string(const char *s)
  71. : m_data(s, s + std::strlen(s))
  72. {
  73. }
  74. template<class InputIterator>
  75. basic_string(InputIterator first, InputIterator last)
  76. : m_data(first, last)
  77. {
  78. }
  79. basic_string(const basic_string<CharT, Traits> &other)
  80. : m_data(other.m_data)
  81. {
  82. }
  83. basic_string<CharT, Traits>& operator=(const basic_string<CharT, Traits> &other)
  84. {
  85. if(this != &other){
  86. m_data = other.m_data;
  87. }
  88. return *this;
  89. }
  90. ~basic_string()
  91. {
  92. }
  93. reference at(size_type pos)
  94. {
  95. return m_data.at(pos);
  96. }
  97. const_reference at(size_type pos) const
  98. {
  99. return m_data.at(pos);
  100. }
  101. reference operator[](size_type pos)
  102. {
  103. return m_data[pos];
  104. }
  105. const_reference operator[](size_type pos) const
  106. {
  107. return m_data[pos];
  108. }
  109. reference front()
  110. {
  111. return m_data.front();
  112. }
  113. const_reference front() const
  114. {
  115. return m_data.front();
  116. }
  117. reference back()
  118. {
  119. return m_data.back();
  120. }
  121. const_reference back() const
  122. {
  123. return m_data.back();
  124. }
  125. iterator begin()
  126. {
  127. return m_data.begin();
  128. }
  129. const_iterator begin() const
  130. {
  131. return m_data.begin();
  132. }
  133. const_iterator cbegin() const
  134. {
  135. return m_data.cbegin();
  136. }
  137. iterator end()
  138. {
  139. return m_data.end();
  140. }
  141. const_iterator end() const
  142. {
  143. return m_data.end();
  144. }
  145. const_iterator cend() const
  146. {
  147. return m_data.cend();
  148. }
  149. reverse_iterator rbegin()
  150. {
  151. return m_data.rbegin();
  152. }
  153. const_reverse_iterator rbegin() const
  154. {
  155. return m_data.rbegin();
  156. }
  157. const_reverse_iterator crbegin() const
  158. {
  159. return m_data.crbegin();
  160. }
  161. reverse_iterator rend()
  162. {
  163. return m_data.rend();
  164. }
  165. const_reverse_iterator rend() const
  166. {
  167. return m_data.rend();
  168. }
  169. const_reverse_iterator crend() const
  170. {
  171. return m_data.crend();
  172. }
  173. bool empty() const
  174. {
  175. return m_data.empty();
  176. }
  177. size_type size() const
  178. {
  179. return m_data.size();
  180. }
  181. size_type length() const
  182. {
  183. return m_data.size();
  184. }
  185. size_type max_size() const
  186. {
  187. return m_data.max_size();
  188. }
  189. void reserve(size_type size)
  190. {
  191. m_data.reserve(size);
  192. }
  193. size_type capacity() const
  194. {
  195. return m_data.capacity();
  196. }
  197. void shrink_to_fit()
  198. {
  199. m_data.shrink_to_fit();
  200. }
  201. void clear()
  202. {
  203. m_data.clear();
  204. }
  205. void swap(basic_string<CharT, Traits> &other)
  206. {
  207. if(this != &other)
  208. {
  209. ::boost::compute::vector<CharT> temp_data(other.m_data);
  210. other.m_data = m_data;
  211. m_data = temp_data;
  212. }
  213. }
  214. basic_string<CharT, Traits> substr(size_type pos = 0,
  215. size_type count = npos) const
  216. {
  217. return basic_string<CharT, Traits>(*this, pos, count);
  218. }
  219. /// Finds the first character \p ch
  220. size_type find(CharT ch, size_type pos = 0) const
  221. {
  222. const_iterator iter = ::boost::compute::find(begin() + pos, end(), ch);
  223. if(iter == end()){
  224. return npos;
  225. }
  226. else {
  227. return static_cast<size_type>(std::distance(begin(), iter));
  228. }
  229. }
  230. /// Finds the first substring equal to \p str
  231. size_type find(basic_string& str, size_type pos = 0) const
  232. {
  233. const_iterator iter = ::boost::compute::search(begin() + pos, end(),
  234. str.begin(), str.end());
  235. if(iter == end()){
  236. return npos;
  237. }
  238. else {
  239. return static_cast<size_type>(std::distance(begin(), iter));
  240. }
  241. }
  242. /// Finds the first substring equal to the character string
  243. /// pointed to by \p s.
  244. /// The length of the string is determined by the first null character.
  245. ///
  246. /// For example, the following code
  247. /// \snippet test/test_string.cpp string_find
  248. ///
  249. /// will return 5 as position.
  250. size_type find(const char* s, size_type pos = 0) const
  251. {
  252. basic_string str(s);
  253. const_iterator iter = ::boost::compute::search(begin() + pos, end(),
  254. str.begin(), str.end());
  255. if(iter == end()){
  256. return npos;
  257. }
  258. else {
  259. return static_cast<size_type>(std::distance(begin(), iter));
  260. }
  261. }
  262. private:
  263. ::boost::compute::vector<CharT> m_data;
  264. };
  265. template<class CharT, class Traits>
  266. std::ostream&
  267. operator<<(std::ostream& stream,
  268. boost::compute::basic_string<CharT, Traits>const& outStr)
  269. {
  270. command_queue queue = ::boost::compute::system::default_queue();
  271. boost::compute::copy(outStr.begin(),
  272. outStr.end(),
  273. std::ostream_iterator<CharT>(stream),
  274. queue);
  275. return stream;
  276. }
  277. } // end compute namespace
  278. } // end boost namespace
  279. #endif // BOOST_COMPUTE_CONTAINER_BASIC_STRING_HPP