deque_cnc.hpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. //----------------------------------------------------------------------------
  2. /// @file deque_cnc.hpp
  3. /// @brief This file contains the implementation of the several types of
  4. /// recursive fastmutex for read and write
  5. ///
  6. /// @author Copyright (c) 2010 2015 Francisco José Tapia (fjtapia@gmail.com )\n
  7. /// Distributed under the Boost Software License, Version 1.0.\n
  8. /// ( See accompanyingfile LICENSE_1_0.txt or copy at
  9. /// http://www.boost.org/LICENSE_1_0.txt )
  10. /// @version 0.1
  11. ///
  12. /// @remarks
  13. //-----------------------------------------------------------------------------
  14. #ifndef __TOOLS_DEQUE_CNC_HPP
  15. #define __TOOLS_DEQUE_CNC_HPP
  16. #include <sort/tools/spinlock.hpp>
  17. #include <vector>
  18. #include <deque>
  19. namespace sort
  20. {
  21. namespace tools
  22. {
  23. //###########################################################################
  24. // ##
  25. // ################################################################ ##
  26. // # # ##
  27. // # C L A S S # ##
  28. // # S T A C K _ C N C # ##
  29. // # # ##
  30. // ################################################################ ##
  31. // ##
  32. //###########################################################################
  33. //
  34. //---------------------------------------------------------------------------
  35. /// @class deque_cnc
  36. /// @brief This class is a concurrent stack controled by a spin_lock
  37. /// @remarks
  38. //---------------------------------------------------------------------------
  39. template<typename T, typename Allocator = std::allocator<T> >
  40. class deque_cnc
  41. {
  42. public:
  43. //-----------------------------------------------------------------------
  44. // D E F I N I T I O N S
  45. //-----------------------------------------------------------------------
  46. typedef std::deque<T, Allocator> deque_t;
  47. typedef typename deque_t::size_type size_type;
  48. typedef typename deque_t::difference_type difference_type;
  49. typedef typename deque_t::value_type value_type;
  50. typedef typename deque_t::pointer pointer;
  51. typedef typename deque_t::const_pointer const_pointer;
  52. typedef typename deque_t::reference reference;
  53. typedef typename deque_t::const_reference const_reference;
  54. typedef typename deque_t::allocator_type allocator_type;
  55. protected:
  56. //------------------------------------------------------------------------
  57. // VARIABLES
  58. //------------------------------------------------------------------------
  59. deque_t dq;
  60. mutable spinlock spl;
  61. public:
  62. //
  63. //-----------------------------------------------------------------------
  64. // C O N S T R U C T O R S A N D D E S T R U C T O R
  65. //-----------------------------------------------------------------------
  66. //
  67. //-----------------------------------------------------------------------
  68. // function : deque_cnc
  69. /// @brief constructor
  70. //----------------------------------------------------------------------
  71. explicit inline deque_cnc(void): dq() { };
  72. //
  73. //----------------------------------------------------------------------
  74. // function : deque_cnc
  75. /// @brief constructor
  76. /// @param [in] ALLC : Allocator
  77. //----------------------------------------------------------------------
  78. explicit inline deque_cnc(const Allocator &ALLC): dq(ALLC){ };
  79. //
  80. //----------------------------------------------------------------------
  81. // function : ~deque_cnc
  82. /// @brief Destructor
  83. //----------------------------------------------------------------------
  84. virtual ~deque_cnc(void){ dq.clear(); };
  85. //
  86. //----------------------------------------------------------------------
  87. // function : clear
  88. /// @brief Delete all the elements of the deque_cnc.
  89. //----------------------------------------------------------------------
  90. void clear(void)
  91. {
  92. std::lock_guard < spinlock > S(spl);
  93. dq.clear();
  94. };
  95. //
  96. //------------------------------------------------------------------------
  97. // function : swap
  98. /// @brief swap the data between the two deque_cnc
  99. /// @param [in] A : deque_cnc to swap
  100. /// @return none
  101. //-----------------------------------------------------------------------
  102. void swap(deque_cnc & A) noexcept
  103. {
  104. if (this == &A) return;
  105. std::lock_guard < spinlock > S(spl);
  106. dq.swap(A.dq);
  107. };
  108. //
  109. //-----------------------------------------------------------------------
  110. // S I Z E , M A X _ S I Z E , R E S I Z E
  111. // C A P A C I T Y , E M P T Y , R E S E R V E
  112. //-----------------------------------------------------------------------
  113. //
  114. //------------------------------------------------------------------------
  115. // function : size
  116. /// @brief return the number of elements in the deque_cnc
  117. /// @return number of elements in the deque_cnc
  118. //------------------------------------------------------------------------
  119. size_type size(void) const noexcept
  120. {
  121. std::lock_guard < spinlock > S(spl);
  122. return dq.size();
  123. };
  124. //
  125. //------------------------------------------------------------------------
  126. // function :max_size
  127. /// @brief return the maximun size of the container
  128. /// @return maximun size of the container
  129. //------------------------------------------------------------------------
  130. size_type max_size(void) const noexcept
  131. {
  132. std::lock_guard < spinlock > S(spl);
  133. return (dq.max_size());
  134. };
  135. //
  136. //-------------------------------------------------------------------------
  137. // function : shrink_to_fit
  138. /// @brief resize the current vector size and change to size.\n
  139. /// If sz is smaller than the current size, delete elements to end\n
  140. /// If sz is greater than the current size, insert elements to the
  141. /// end with the value c
  142. /// @param [in] sz : new size of the deque_cnc after the resize
  143. /// @param [in] c : Value to insert if sz is greather than the current size
  144. /// @return none
  145. //------------------------------------------------------------------------
  146. void shrink_to_fit()
  147. {
  148. std::lock_guard < spinlock > S(spl);
  149. dq.shrink_to_fit();
  150. };
  151. //
  152. //------------------------------------------------------------------------
  153. // function : empty
  154. /// @brief indicate if the map is empty
  155. /// @return true if the map is empty, false in any other case
  156. //------------------------------------------------------------------------
  157. bool empty(void) const noexcept
  158. {
  159. std::lock_guard < spinlock > S(spl);
  160. return (dq.empty());
  161. };
  162. //---------------------------------------------------------------------------
  163. // function : push_back
  164. /// @brief Insert one element in the back of the container
  165. /// @param [in] D : value to insert. Can ve a value, a reference or an
  166. /// rvalue
  167. //---------------------------------------------------------------------------
  168. void push_back(const value_type & D)
  169. {
  170. std::lock_guard < spinlock > S(spl);
  171. dq.push_back(D);
  172. };
  173. //------------------------------------------------------------------------
  174. // function : emplace_back
  175. /// @brief Insert one element in the back of the container
  176. /// @param [in] args :group of arguments for to build the object to insert
  177. //-------------------------------------------------------------------------
  178. template<class ... Args>
  179. void emplace_back(Args && ... args)
  180. {
  181. std::lock_guard < spinlock > S(spl);
  182. dq.emplace_back(std::forward <Args>(args) ...);
  183. };
  184. //------------------------------------------------------------------------
  185. // function : push_back
  186. /// @brief Insert one element in the back of the container
  187. /// @param [in] D : deque to insert in the actual deque, inserting a copy
  188. /// of the elements
  189. /// @return reference to the deque after the insertion
  190. //------------------------------------------------------------------------
  191. template<class Allocator2>
  192. deque_cnc & push_back(const std::deque<value_type, Allocator2> & D)
  193. {
  194. std::lock_guard < spinlock > S(spl);
  195. for (size_type i = 0; i < D.size(); ++i)
  196. dq.push_back(D[i]);
  197. return *this;
  198. };
  199. //------------------------------------------------------------------------
  200. // function : push_back
  201. /// @brief Insert one element in the back of the container
  202. /// @param [in] D : deque to insert in the actual deque, inserting a move
  203. /// of the elements
  204. /// @return reference to the deque after the insertion
  205. //------------------------------------------------------------------------
  206. deque_cnc & push_back(std::deque<value_type, Allocator> && D)
  207. {
  208. std::lock_guard < spinlock > S(spl);
  209. for (size_type i = 0; i < D.size(); ++i)
  210. dq.emplace_back(std::move(D[i]));
  211. return *this;
  212. };
  213. //
  214. //------------------------------------------------------------------------
  215. // function :pop_back
  216. /// @brief erase the last element of the container
  217. //-----------------------------------------------------------------------
  218. void pop_back(void)
  219. {
  220. std::lock_guard < spinlock > S(spl);
  221. dq.pop_back();
  222. };
  223. //
  224. //------------------------------------------------------------------------
  225. // function :pop_copy_back
  226. /// @brief erase the last element and return a copy over P
  227. /// @param [out] P : reference to a variable where copy the element
  228. /// @return code of the operation
  229. /// true - Element erased
  230. /// false - Empty tree
  231. //------------------------------------------------------------------------
  232. bool pop_copy_back(value_type & P)
  233. { //-------------------------- begin -----------------------------
  234. std::lock_guard < spinlock > S(spl);
  235. if (dq.size() == 0) return false;
  236. P = dq.back();
  237. dq.pop_back();
  238. return true;
  239. };
  240. //
  241. //------------------------------------------------------------------------
  242. // function :pop_move_back
  243. /// @brief erase the last element and move over P
  244. /// @param [out] P : reference to a variable where move the element
  245. /// @return code of the operation
  246. /// true - Element erased
  247. /// false - Empty tree
  248. //------------------------------------------------------------------------
  249. bool pop_move_back(value_type & P)
  250. { //-------------------------- begin -----------------------------
  251. std::lock_guard < spinlock > S(spl);
  252. if (dq.size() == 0) return false;
  253. P = std::move(dq.back());
  254. dq.pop_back();
  255. return true;
  256. };
  257. //------------------------------------------------------------------------
  258. // function : push_front
  259. /// @brief Insert one copy of the element in the front of the container
  260. /// @param [in] D : value to insert
  261. //------------------------------------------------------------------------
  262. void push_front(const value_type & D)
  263. {
  264. std::lock_guard < spinlock > S(spl);
  265. dq.push_front(D);
  266. };
  267. //------------------------------------------------------------------------
  268. // function : emplace_front
  269. /// @brief Insert one element in the front of the container
  270. /// @param [in] args :group of arguments for to build the object to insert
  271. //-------------------------------------------------------------------------
  272. template<class ... Args>
  273. void emplace_front(Args && ... args)
  274. {
  275. std::lock_guard < spinlock > S(spl);
  276. dq.emplace_front(std::forward <Args>(args) ...);
  277. };
  278. //------------------------------------------------------------------------
  279. // function : push_front
  280. /// @brief Insert a copy of the elements of the deque V1 in the front
  281. /// of the container
  282. /// @param [in] V1 : deque with the elements to insert
  283. /// @return reference to the deque after the insertion
  284. //------------------------------------------------------------------------
  285. template<class Allocator2>
  286. deque_cnc & push_front(const std::deque<value_type, Allocator2> & V1)
  287. {
  288. std::lock_guard < spinlock > S(spl);
  289. for (size_type i = 0; i < V1.size(); ++i)
  290. dq.push_front(V1[i]);
  291. return *this;
  292. };
  293. //-----------------------------------------------------------------------
  294. // function : push_front
  295. /// @brief Insert a move of the elements of the deque V1 in the front
  296. /// of the container
  297. /// @param [in] V1 : deque with the elements to insert
  298. /// @return reference to the deque after the insertion
  299. //-----------------------------------------------------------------------
  300. deque_cnc & push_front(std::deque<value_type, Allocator> && V1)
  301. {
  302. std::lock_guard < spinlock > S(spl);
  303. for (size_type i = 0; i < V1.size(); ++i)
  304. dq.emplace_front(std::move(V1[i]));
  305. return *this;
  306. };
  307. //
  308. //-----------------------------------------------------------------------
  309. // function :pop_front
  310. /// @brief erase the first element of the container
  311. //-----------------------------------------------------------------------
  312. void pop_front(void)
  313. {
  314. std::lock_guard < spinlock > S(spl);
  315. dq.pop_front();
  316. };
  317. //
  318. //-----------------------------------------------------------------------
  319. // function :pop_copy_front
  320. /// @brief erase the first element of the tree and return a copy over P
  321. /// @param [out] P : reference to a variable where copy the element
  322. /// @return code of the operation
  323. /// true- Element erased
  324. /// false - Empty tree
  325. //-----------------------------------------------------------------------
  326. bool pop_copy_front(value_type & P)
  327. { //-------------------------- begin -----------------------------
  328. std::lock_guard < spinlock > S(spl);
  329. if (dq.size() == 0) return false;
  330. P = dq.front();
  331. dq.pop_front();
  332. return true;
  333. };
  334. //
  335. //------------------------------------------------------------------------
  336. // function :pop_move_front
  337. /// @brief erase the first element of the tree and return a move over P
  338. /// @param [out] P : reference to a variable where move the element
  339. /// @return code of the operation
  340. /// true- Element erased
  341. /// false - Empty tree
  342. //------------------------------------------------------------------------
  343. bool pop_move_front(value_type & P)
  344. { //-------------------------- begin -----------------------------
  345. std::lock_guard < spinlock > S(spl);
  346. if (dq.size() == 0) return false;
  347. P = std::move(dq.front());
  348. dq.pop_front();
  349. return true;
  350. };
  351. };
  352. // end class deque_cnc
  353. //***************************************************************************
  354. };// end namespace tools
  355. };// end namespace sort
  356. //***************************************************************************
  357. #endif