Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:29:58

0001 //---------------------------------------------------------------------------//
0002 // Copyright (c) 2013 Kyle Lutz <kyle.r.lutz@gmail.com>
0003 //
0004 // Distributed under the Boost Software License, Version 1.0
0005 // See accompanying file LICENSE_1_0.txt or copy at
0006 // http://www.boost.org/LICENSE_1_0.txt
0007 //
0008 // See http://boostorg.github.com/compute for more information.
0009 //---------------------------------------------------------------------------//
0010 
0011 #ifndef BOOST_COMPUTE_CONTAINER_BASIC_STRING_HPP
0012 #define BOOST_COMPUTE_CONTAINER_BASIC_STRING_HPP
0013 
0014 #include <string>
0015 #include <cstring>
0016 
0017 #include <boost/compute/cl.hpp>
0018 #include <boost/compute/algorithm/find.hpp>
0019 #include <boost/compute/algorithm/search.hpp>
0020 #include <boost/compute/container/vector.hpp>
0021 #include <boost/compute/system.hpp>
0022 #include <boost/compute/command_queue.hpp>
0023 #include <iosfwd>
0024 
0025 namespace boost {
0026 namespace compute {
0027 
0028 /// \class basic_string
0029 /// \brief A template for a dynamically-sized character sequence.
0030 ///
0031 /// The \c basic_string class provides a generic template for a dynamically-
0032 /// sized character sequence. This is most commonly used through the \c string
0033 /// typedef (for \c basic_string<char>).
0034 ///
0035 /// For example, to create a string on the device with its contents copied
0036 /// from a C-string on the host:
0037 /// \code
0038 /// boost::compute::string str("hello, world!");
0039 /// \endcode
0040 ///
0041 /// \see \ref vector "vector<T>"
0042 template<class CharT, class Traits = std::char_traits<CharT> >
0043 class basic_string
0044 {
0045 public:
0046     typedef Traits traits_type;
0047     typedef typename Traits::char_type value_type;
0048     typedef size_t size_type;
0049     static const size_type npos = size_type(-1);
0050     typedef typename ::boost::compute::vector<CharT>::reference reference;
0051     typedef typename ::boost::compute::vector<CharT>::const_reference const_reference;
0052     typedef typename ::boost::compute::vector<CharT>::iterator iterator;
0053     typedef typename ::boost::compute::vector<CharT>::const_iterator const_iterator;
0054     typedef typename ::boost::compute::vector<CharT>::reverse_iterator reverse_iterator;
0055     typedef typename ::boost::compute::vector<CharT>::const_reverse_iterator const_reverse_iterator;
0056 
0057     basic_string()
0058     {
0059     }
0060 
0061     basic_string(size_type count, CharT ch)
0062         : m_data(count)
0063     {
0064         std::fill(m_data.begin(), m_data.end(), ch);
0065     }
0066 
0067     basic_string(const basic_string &other,
0068                  size_type pos,
0069                  size_type count = npos)
0070         : m_data(other.begin() + pos,
0071                  other.begin() + (std::min)(other.size(), count))
0072     {
0073     }
0074 
0075     basic_string(const char *s, size_type count)
0076         : m_data(s, s + count)
0077     {
0078     }
0079 
0080     basic_string(const char *s)
0081         : m_data(s, s + std::strlen(s))
0082     {
0083     }
0084 
0085     template<class InputIterator>
0086     basic_string(InputIterator first, InputIterator last)
0087         : m_data(first, last)
0088     {
0089     }
0090 
0091     basic_string(const basic_string<CharT, Traits> &other)
0092         : m_data(other.m_data)
0093     {
0094     }
0095 
0096     basic_string<CharT, Traits>& operator=(const basic_string<CharT, Traits> &other)
0097     {
0098         if(this != &other){
0099             m_data = other.m_data;
0100         }
0101 
0102         return *this;
0103     }
0104 
0105     ~basic_string()
0106     {
0107     }
0108 
0109     reference at(size_type pos)
0110     {
0111         return m_data.at(pos);
0112     }
0113 
0114     const_reference at(size_type pos) const
0115     {
0116         return m_data.at(pos);
0117     }
0118 
0119     reference operator[](size_type pos)
0120     {
0121         return m_data[pos];
0122     }
0123 
0124     const_reference operator[](size_type pos) const
0125     {
0126         return m_data[pos];
0127     }
0128 
0129     reference front()
0130     {
0131         return m_data.front();
0132     }
0133 
0134     const_reference front() const
0135     {
0136         return m_data.front();
0137     }
0138 
0139     reference back()
0140     {
0141         return m_data.back();
0142     }
0143 
0144     const_reference back() const
0145     {
0146         return m_data.back();
0147     }
0148 
0149     iterator begin()
0150     {
0151         return m_data.begin();
0152     }
0153 
0154     const_iterator begin() const
0155     {
0156         return m_data.begin();
0157     }
0158 
0159     const_iterator cbegin() const
0160     {
0161         return m_data.cbegin();
0162     }
0163 
0164     iterator end()
0165     {
0166         return m_data.end();
0167     }
0168 
0169     const_iterator end() const
0170     {
0171         return m_data.end();
0172     }
0173 
0174     const_iterator cend() const
0175     {
0176         return m_data.cend();
0177     }
0178 
0179     reverse_iterator rbegin()
0180     {
0181         return m_data.rbegin();
0182     }
0183 
0184     const_reverse_iterator rbegin() const
0185     {
0186         return m_data.rbegin();
0187     }
0188 
0189     const_reverse_iterator crbegin() const
0190     {
0191         return m_data.crbegin();
0192     }
0193 
0194     reverse_iterator rend()
0195     {
0196         return m_data.rend();
0197     }
0198 
0199     const_reverse_iterator rend() const
0200     {
0201         return m_data.rend();
0202     }
0203 
0204     const_reverse_iterator crend() const
0205     {
0206         return m_data.crend();
0207     }
0208 
0209     bool empty() const
0210     {
0211         return m_data.empty();
0212     }
0213 
0214     size_type size() const
0215     {
0216         return m_data.size();
0217     }
0218 
0219     size_type length() const
0220     {
0221         return m_data.size();
0222     }
0223 
0224     size_type max_size() const
0225     {
0226         return m_data.max_size();
0227     }
0228 
0229     void reserve(size_type size)
0230     {
0231         m_data.reserve(size);
0232     }
0233 
0234     size_type capacity() const
0235     {
0236         return m_data.capacity();
0237     }
0238 
0239     void shrink_to_fit()
0240     {
0241         m_data.shrink_to_fit();
0242     }
0243 
0244     void clear()
0245     {
0246         m_data.clear();
0247     }
0248 
0249     void swap(basic_string<CharT, Traits> &other)
0250     {
0251         if(this != &other)
0252         {
0253             ::boost::compute::vector<CharT> temp_data(other.m_data);
0254             other.m_data = m_data;
0255             m_data = temp_data;
0256         }
0257     }
0258 
0259     basic_string<CharT, Traits> substr(size_type pos = 0,
0260                                        size_type count = npos) const
0261     {
0262         return basic_string<CharT, Traits>(*this, pos, count);
0263     }
0264 
0265     /// Finds the first character \p ch
0266     size_type find(CharT ch, size_type pos = 0) const
0267     {
0268         const_iterator iter = ::boost::compute::find(begin() + pos, end(), ch);
0269         if(iter == end()){
0270             return npos;
0271         }
0272         else {
0273             return static_cast<size_type>(std::distance(begin(), iter));
0274         }
0275     }
0276 
0277     /// Finds the first substring equal to \p str
0278     size_type find(basic_string& str, size_type pos = 0) const
0279     {
0280         const_iterator iter = ::boost::compute::search(begin() + pos, end(),
0281                                                        str.begin(), str.end());
0282         if(iter == end()){
0283             return npos;
0284         }
0285         else {
0286             return static_cast<size_type>(std::distance(begin(), iter));
0287         }
0288     }
0289 
0290     /// Finds the first substring equal to the character string
0291     /// pointed to by \p s.
0292     /// The length of the string is determined by the first null character.
0293     ///
0294     /// For example, the following code
0295     /// \snippet test/test_string.cpp string_find
0296     ///
0297     /// will return 5 as position.
0298     size_type find(const char* s, size_type pos = 0) const
0299     {
0300         basic_string str(s);
0301         const_iterator iter = ::boost::compute::search(begin() + pos, end(),
0302                                                        str.begin(), str.end());
0303         if(iter == end()){
0304             return npos;
0305         }
0306         else {
0307             return static_cast<size_type>(std::distance(begin(), iter));
0308         }
0309     }
0310 
0311 private:
0312     ::boost::compute::vector<CharT> m_data;
0313 };
0314 
0315 template<class CharT, class Traits>
0316 std::ostream&
0317 operator<<(std::ostream& stream,
0318            boost::compute::basic_string<CharT, Traits>const& outStr)
0319 {
0320     command_queue queue = ::boost::compute::system::default_queue();
0321     boost::compute::copy(outStr.begin(),
0322                         outStr.end(),
0323                         std::ostream_iterator<CharT>(stream),
0324                         queue);
0325     return stream;
0326 }
0327 
0328 } // end compute namespace
0329 } // end boost namespace
0330 
0331 #endif // BOOST_COMPUTE_CONTAINER_BASIC_STRING_HPP