flat_set.hpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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_FLAT_SET_HPP
  11. #define BOOST_COMPUTE_CONTAINER_FLAT_SET_HPP
  12. #include <cstddef>
  13. #include <utility>
  14. #include <boost/compute/algorithm/find.hpp>
  15. #include <boost/compute/algorithm/lower_bound.hpp>
  16. #include <boost/compute/algorithm/upper_bound.hpp>
  17. #include <boost/compute/container/vector.hpp>
  18. namespace boost {
  19. namespace compute {
  20. template<class T>
  21. class flat_set
  22. {
  23. public:
  24. typedef T key_type;
  25. typedef typename vector<T>::value_type value_type;
  26. typedef typename vector<T>::size_type size_type;
  27. typedef typename vector<T>::difference_type difference_type;
  28. typedef typename vector<T>::reference reference;
  29. typedef typename vector<T>::const_reference const_reference;
  30. typedef typename vector<T>::pointer pointer;
  31. typedef typename vector<T>::const_pointer const_pointer;
  32. typedef typename vector<T>::iterator iterator;
  33. typedef typename vector<T>::const_iterator const_iterator;
  34. typedef typename vector<T>::reverse_iterator reverse_iterator;
  35. typedef typename vector<T>::const_reverse_iterator const_reverse_iterator;
  36. explicit flat_set(const context &context = system::default_context())
  37. : m_vector(context)
  38. {
  39. }
  40. flat_set(const flat_set<T> &other)
  41. : m_vector(other.m_vector)
  42. {
  43. }
  44. flat_set<T>& operator=(const flat_set<T> &other)
  45. {
  46. if(this != &other){
  47. m_vector = other.m_vector;
  48. }
  49. return *this;
  50. }
  51. ~flat_set()
  52. {
  53. }
  54. iterator begin()
  55. {
  56. return m_vector.begin();
  57. }
  58. const_iterator begin() const
  59. {
  60. return m_vector.begin();
  61. }
  62. const_iterator cbegin() const
  63. {
  64. return m_vector.cbegin();
  65. }
  66. iterator end()
  67. {
  68. return m_vector.end();
  69. }
  70. const_iterator end() const
  71. {
  72. return m_vector.end();
  73. }
  74. const_iterator cend() const
  75. {
  76. return m_vector.cend();
  77. }
  78. reverse_iterator rbegin()
  79. {
  80. return m_vector.rbegin();
  81. }
  82. const_reverse_iterator rbegin() const
  83. {
  84. return m_vector.rbegin();
  85. }
  86. const_reverse_iterator crbegin() const
  87. {
  88. return m_vector.crbegin();
  89. }
  90. reverse_iterator rend()
  91. {
  92. return m_vector.rend();
  93. }
  94. const_reverse_iterator rend() const
  95. {
  96. return m_vector.rend();
  97. }
  98. const_reverse_iterator crend() const
  99. {
  100. return m_vector.crend();
  101. }
  102. size_type size() const
  103. {
  104. return m_vector.size();
  105. }
  106. size_type max_size() const
  107. {
  108. return m_vector.max_size();
  109. }
  110. bool empty() const
  111. {
  112. return m_vector.empty();
  113. }
  114. size_type capacity() const
  115. {
  116. return m_vector.capacity();
  117. }
  118. void reserve(size_type size, command_queue &queue)
  119. {
  120. m_vector.reserve(size, queue);
  121. }
  122. void reserve(size_type size)
  123. {
  124. command_queue queue = m_vector.default_queue();
  125. reserve(size, queue);
  126. queue.finish();
  127. }
  128. void shrink_to_fit()
  129. {
  130. m_vector.shrink_to_fit();
  131. }
  132. void clear()
  133. {
  134. m_vector.clear();
  135. }
  136. std::pair<iterator, bool>
  137. insert(const value_type &value, command_queue &queue)
  138. {
  139. iterator location = upper_bound(value, queue);
  140. if(location != begin()){
  141. value_type current_value;
  142. ::boost::compute::copy_n(location - 1, 1, &current_value, queue);
  143. if(value == current_value){
  144. return std::make_pair(location - 1, false);
  145. }
  146. }
  147. m_vector.insert(location, value, queue);
  148. return std::make_pair(location, true);
  149. }
  150. std::pair<iterator, bool> insert(const value_type &value)
  151. {
  152. command_queue queue = m_vector.default_queue();
  153. std::pair<iterator, bool> result = insert(value, queue);
  154. queue.finish();
  155. return result;
  156. }
  157. iterator erase(const const_iterator &position, command_queue &queue)
  158. {
  159. return erase(position, position + 1, queue);
  160. }
  161. iterator erase(const const_iterator &position)
  162. {
  163. command_queue queue = m_vector.default_queue();
  164. iterator iter = erase(position, queue);
  165. queue.finish();
  166. return iter;
  167. }
  168. iterator erase(const const_iterator &first,
  169. const const_iterator &last,
  170. command_queue &queue)
  171. {
  172. return m_vector.erase(first, last, queue);
  173. }
  174. iterator erase(const const_iterator &first, const const_iterator &last)
  175. {
  176. command_queue queue = m_vector.default_queue();
  177. iterator iter = erase(first, last, queue);
  178. queue.finish();
  179. return iter;
  180. }
  181. size_type erase(const key_type &value, command_queue &queue)
  182. {
  183. iterator position = find(value, queue);
  184. if(position == end()){
  185. return 0;
  186. }
  187. else {
  188. erase(position, queue);
  189. return 1;
  190. }
  191. }
  192. size_type erase(const key_type &value)
  193. {
  194. command_queue queue = m_vector.default_queue();
  195. size_type result = erase(value, queue);
  196. queue.finish();
  197. return result;
  198. }
  199. iterator find(const key_type &value, command_queue &queue)
  200. {
  201. return ::boost::compute::find(begin(), end(), value, queue);
  202. }
  203. iterator find(const key_type &value)
  204. {
  205. command_queue queue = m_vector.default_queue();
  206. iterator iter = find(value, queue);
  207. queue.finish();
  208. return iter;
  209. }
  210. const_iterator find(const key_type &value, command_queue &queue) const
  211. {
  212. return ::boost::compute::find(begin(), end(), value, queue);
  213. }
  214. const_iterator find(const key_type &value) const
  215. {
  216. command_queue queue = m_vector.default_queue();
  217. const_iterator iter = find(value, queue);
  218. queue.finish();
  219. return iter;
  220. }
  221. size_type count(const key_type &value, command_queue &queue) const
  222. {
  223. return find(value, queue) != end() ? 1 : 0;
  224. }
  225. size_type count(const key_type &value) const
  226. {
  227. command_queue queue = m_vector.default_queue();
  228. size_type result = count(value, queue);
  229. queue.finish();
  230. return result;
  231. }
  232. iterator lower_bound(const key_type &value, command_queue &queue)
  233. {
  234. return ::boost::compute::lower_bound(begin(), end(), value, queue);
  235. }
  236. iterator lower_bound(const key_type &value)
  237. {
  238. command_queue queue = m_vector.default_queue();
  239. iterator iter = lower_bound(value, queue);
  240. queue.finish();
  241. return iter;
  242. }
  243. const_iterator lower_bound(const key_type &value, command_queue &queue) const
  244. {
  245. return ::boost::compute::lower_bound(begin(), end(), value, queue);
  246. }
  247. const_iterator lower_bound(const key_type &value) const
  248. {
  249. command_queue queue = m_vector.default_queue();
  250. const_iterator iter = lower_bound(value, queue);
  251. queue.finish();
  252. return iter;
  253. }
  254. iterator upper_bound(const key_type &value, command_queue &queue)
  255. {
  256. return ::boost::compute::upper_bound(begin(), end(), value, queue);
  257. }
  258. iterator upper_bound(const key_type &value)
  259. {
  260. command_queue queue = m_vector.default_queue();
  261. iterator iter = upper_bound(value, queue);
  262. queue.finish();
  263. return iter;
  264. }
  265. const_iterator upper_bound(const key_type &value, command_queue &queue) const
  266. {
  267. return ::boost::compute::upper_bound(begin(), end(), value, queue);
  268. }
  269. const_iterator upper_bound(const key_type &value) const
  270. {
  271. command_queue queue = m_vector.default_queue();
  272. const_iterator iter = upper_bound(value, queue);
  273. queue.finish();
  274. return iter;
  275. }
  276. private:
  277. vector<T> m_vector;
  278. };
  279. } // end compute namespace
  280. } // end boost namespace
  281. #endif // BOOST_COMPUTE_CONTAINER_FLAT_SET_HPP