Back to home page

EIC code displayed by LXR

 
 

    


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

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_FLAT_SET_HPP
0012 #define BOOST_COMPUTE_CONTAINER_FLAT_SET_HPP
0013 
0014 #include <cstddef>
0015 #include <utility>
0016 
0017 #include <boost/compute/algorithm/find.hpp>
0018 #include <boost/compute/algorithm/lower_bound.hpp>
0019 #include <boost/compute/algorithm/upper_bound.hpp>
0020 #include <boost/compute/container/vector.hpp>
0021 
0022 namespace boost {
0023 namespace compute {
0024 
0025 template<class T>
0026 class flat_set
0027 {
0028 public:
0029     typedef T key_type;
0030     typedef typename vector<T>::value_type value_type;
0031     typedef typename vector<T>::size_type size_type;
0032     typedef typename vector<T>::difference_type difference_type;
0033     typedef typename vector<T>::reference reference;
0034     typedef typename vector<T>::const_reference const_reference;
0035     typedef typename vector<T>::pointer pointer;
0036     typedef typename vector<T>::const_pointer const_pointer;
0037     typedef typename vector<T>::iterator iterator;
0038     typedef typename vector<T>::const_iterator const_iterator;
0039     typedef typename vector<T>::reverse_iterator reverse_iterator;
0040     typedef typename vector<T>::const_reverse_iterator const_reverse_iterator;
0041 
0042     explicit flat_set(const context &context = system::default_context())
0043         : m_vector(context)
0044     {
0045     }
0046 
0047     flat_set(const flat_set<T> &other)
0048         : m_vector(other.m_vector)
0049     {
0050     }
0051 
0052     flat_set<T>& operator=(const flat_set<T> &other)
0053     {
0054         if(this != &other){
0055             m_vector = other.m_vector;
0056         }
0057 
0058         return *this;
0059     }
0060 
0061     ~flat_set()
0062     {
0063     }
0064 
0065     iterator begin()
0066     {
0067         return m_vector.begin();
0068     }
0069 
0070     const_iterator begin() const
0071     {
0072         return m_vector.begin();
0073     }
0074 
0075     const_iterator cbegin() const
0076     {
0077         return m_vector.cbegin();
0078     }
0079 
0080     iterator end()
0081     {
0082         return m_vector.end();
0083     }
0084 
0085     const_iterator end() const
0086     {
0087         return m_vector.end();
0088     }
0089 
0090     const_iterator cend() const
0091     {
0092         return m_vector.cend();
0093     }
0094 
0095     reverse_iterator rbegin()
0096     {
0097         return m_vector.rbegin();
0098     }
0099 
0100     const_reverse_iterator rbegin() const
0101     {
0102         return m_vector.rbegin();
0103     }
0104 
0105     const_reverse_iterator crbegin() const
0106     {
0107         return m_vector.crbegin();
0108     }
0109 
0110     reverse_iterator rend()
0111     {
0112         return m_vector.rend();
0113     }
0114 
0115     const_reverse_iterator rend() const
0116     {
0117         return m_vector.rend();
0118     }
0119 
0120     const_reverse_iterator crend() const
0121     {
0122         return m_vector.crend();
0123     }
0124 
0125     size_type size() const
0126     {
0127         return m_vector.size();
0128     }
0129 
0130     size_type max_size() const
0131     {
0132         return m_vector.max_size();
0133     }
0134 
0135     bool empty() const
0136     {
0137         return m_vector.empty();
0138     }
0139 
0140     size_type capacity() const
0141     {
0142         return m_vector.capacity();
0143     }
0144 
0145     void reserve(size_type size, command_queue &queue)
0146     {
0147         m_vector.reserve(size, queue);
0148     }
0149 
0150     void reserve(size_type size)
0151     {
0152         command_queue queue = m_vector.default_queue();
0153         reserve(size, queue);
0154         queue.finish();
0155     }
0156 
0157     void shrink_to_fit()
0158     {
0159         m_vector.shrink_to_fit();
0160     }
0161 
0162     void clear()
0163     {
0164         m_vector.clear();
0165     }
0166 
0167     std::pair<iterator, bool>
0168     insert(const value_type &value, command_queue &queue)
0169     {
0170         iterator location = upper_bound(value, queue);
0171 
0172         if(location != begin()){
0173             value_type current_value;
0174             ::boost::compute::copy_n(location - 1, 1, &current_value, queue);
0175             if(value == current_value){
0176                 return std::make_pair(location - 1, false);
0177             }
0178         }
0179 
0180         m_vector.insert(location, value, queue);
0181         return std::make_pair(location, true);
0182     }
0183 
0184     std::pair<iterator, bool> insert(const value_type &value)
0185     {
0186         command_queue queue = m_vector.default_queue();
0187         std::pair<iterator, bool> result = insert(value, queue);
0188         queue.finish();
0189         return result;
0190     }
0191 
0192     iterator erase(const const_iterator &position, command_queue &queue)
0193     {
0194         return erase(position, position + 1, queue);
0195     }
0196 
0197     iterator erase(const const_iterator &position)
0198     {
0199         command_queue queue = m_vector.default_queue();
0200         iterator iter = erase(position, queue);
0201         queue.finish();
0202         return iter;
0203     }
0204 
0205     iterator erase(const const_iterator &first,
0206                    const const_iterator &last,
0207                    command_queue &queue)
0208     {
0209         return m_vector.erase(first, last, queue);
0210     }
0211 
0212     iterator erase(const const_iterator &first, const const_iterator &last)
0213     {
0214         command_queue queue = m_vector.default_queue();
0215         iterator iter = erase(first, last, queue);
0216         queue.finish();
0217         return iter;
0218     }
0219 
0220     size_type erase(const key_type &value, command_queue &queue)
0221     {
0222         iterator position = find(value, queue);
0223 
0224         if(position == end()){
0225             return 0;
0226         }
0227         else {
0228             erase(position, queue);
0229             return 1;
0230         }
0231     }
0232 
0233     size_type erase(const key_type &value)
0234     {
0235         command_queue queue = m_vector.default_queue();
0236         size_type result = erase(value, queue);
0237         queue.finish();
0238         return result;
0239     }
0240 
0241     iterator find(const key_type &value, command_queue &queue)
0242     {
0243         return ::boost::compute::find(begin(), end(), value, queue);
0244     }
0245 
0246     iterator find(const key_type &value)
0247     {
0248         command_queue queue = m_vector.default_queue();
0249         iterator iter = find(value, queue);
0250         queue.finish();
0251         return iter;
0252     }
0253 
0254     const_iterator find(const key_type &value, command_queue &queue) const
0255     {
0256         return ::boost::compute::find(begin(), end(), value, queue);
0257     }
0258 
0259     const_iterator find(const key_type &value) const
0260     {
0261         command_queue queue = m_vector.default_queue();
0262         const_iterator iter = find(value, queue);
0263         queue.finish();
0264         return iter;
0265     }
0266 
0267     size_type count(const key_type &value, command_queue &queue) const
0268     {
0269         return find(value, queue) != end() ? 1 : 0;
0270     }
0271 
0272     size_type count(const key_type &value) const
0273     {
0274         command_queue queue = m_vector.default_queue();
0275         size_type result = count(value, queue);
0276         queue.finish();
0277         return result;
0278     }
0279 
0280     iterator lower_bound(const key_type &value, command_queue &queue)
0281     {
0282         return ::boost::compute::lower_bound(begin(), end(), value, queue);
0283     }
0284 
0285     iterator lower_bound(const key_type &value)
0286     {
0287         command_queue queue = m_vector.default_queue();
0288         iterator iter = lower_bound(value, queue);
0289         queue.finish();
0290         return iter;
0291     }
0292 
0293     const_iterator lower_bound(const key_type &value, command_queue &queue) const
0294     {
0295         return ::boost::compute::lower_bound(begin(), end(), value, queue);
0296     }
0297 
0298     const_iterator lower_bound(const key_type &value) const
0299     {
0300         command_queue queue = m_vector.default_queue();
0301         const_iterator iter = lower_bound(value, queue);
0302         queue.finish();
0303         return iter;
0304     }
0305 
0306     iterator upper_bound(const key_type &value, command_queue &queue)
0307     {
0308         return ::boost::compute::upper_bound(begin(), end(), value, queue);
0309     }
0310 
0311     iterator upper_bound(const key_type &value)
0312     {
0313         command_queue queue = m_vector.default_queue();
0314         iterator iter = upper_bound(value, queue);
0315         queue.finish();
0316         return iter;
0317     }
0318 
0319     const_iterator upper_bound(const key_type &value, command_queue &queue) const
0320     {
0321         return ::boost::compute::upper_bound(begin(), end(), value, queue);
0322     }
0323 
0324     const_iterator upper_bound(const key_type &value) const
0325     {
0326         command_queue queue = m_vector.default_queue();
0327         const_iterator iter = upper_bound(value, queue);
0328         queue.finish();
0329         return iter;
0330     }
0331 
0332 private:
0333     vector<T> m_vector;
0334 };
0335 
0336 } // end compute namespace
0337 } // end boost namespace
0338 
0339 #endif // BOOST_COMPUTE_CONTAINER_FLAT_SET_HPP