stack_cnc.hpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. //----------------------------------------------------------------------------
  2. /// @file stack_cnc.hpp
  3. /// @brief This file contains the implementation concurrent stack
  4. ///
  5. /// @author Copyright (c) 2010 2015 Francisco José Tapia ([email protected] )\n
  6. /// Distributed under the Boost Software License, Version 1.0.\n
  7. /// ( See accompanyingfile LICENSE_1_0.txt or copy at
  8. /// http://www.boost.org/LICENSE_1_0.txt )
  9. /// @version 0.1
  10. ///
  11. /// @remarks
  12. //-----------------------------------------------------------------------------
  13. #ifndef __BOOST_SORT_PARALLEL_DETAIL_UTIL_STACK_CNC_HPP
  14. #define __BOOST_SORT_PARALLEL_DETAIL_UTIL_STACK_CNC_HPP
  15. #include <ciso646>
  16. #include <vector>
  17. #include <boost/sort/common/spinlock.hpp>
  18. namespace boost
  19. {
  20. namespace sort
  21. {
  22. namespace common
  23. {
  24. //
  25. //###########################################################################
  26. // ##
  27. // ################################################################ ##
  28. // # # ##
  29. // # C L A S S # ##
  30. // # S T A C K _ C N C # ##
  31. // # # ##
  32. // ################################################################ ##
  33. // ##
  34. //###########################################################################
  35. //
  36. //---------------------------------------------------------------------------
  37. /// @class stack_cnc
  38. /// @brief This class is a concurrent stack controled by a spin_lock
  39. /// @remarks
  40. //---------------------------------------------------------------------------
  41. template<typename T, typename Allocator = std::allocator<T> >
  42. class stack_cnc
  43. {
  44. public:
  45. //------------------------------------------------------------------------
  46. // D E F I N I T I O N S
  47. //------------------------------------------------------------------------
  48. typedef std::vector<T, Allocator> vector_t;
  49. typedef typename vector_t::size_type size_type;
  50. typedef typename vector_t::difference_type difference_type;
  51. typedef typename vector_t::value_type value_type;
  52. typedef typename vector_t::pointer pointer;
  53. typedef typename vector_t::const_pointer const_pointer;
  54. typedef typename vector_t::reference reference;
  55. typedef typename vector_t::const_reference const_reference;
  56. typedef typename vector_t::allocator_type allocator_type;
  57. typedef Allocator alloc_t;
  58. protected:
  59. //-------------------------------------------------------------------------
  60. // INTERNAL VARIABLES
  61. //-------------------------------------------------------------------------
  62. vector_t v_t;
  63. mutable spinlock_t spl;
  64. public:
  65. //
  66. //-------------------------------------------------------------------------
  67. // function : stack_cnc
  68. /// @brief constructor
  69. //-------------------------------------------------------------------------
  70. explicit stack_cnc(void): v_t() { };
  71. //
  72. //-------------------------------------------------------------------------
  73. // function : stack_cnc
  74. /// @brief Move constructor
  75. //-------------------------------------------------------------------------
  76. stack_cnc(stack_cnc &&) = delete;
  77. //
  78. //-------------------------------------------------------------------------
  79. // function : ~stack_cnc
  80. /// @brief Destructor
  81. //-------------------------------------------------------------------------
  82. virtual ~stack_cnc(void) { v_t.clear(); };
  83. //-------------------------------------------------------------------------
  84. // function : emplace_back
  85. /// @brief Insert one element in the back of the container
  86. /// @param args : group of arguments for to build the object to insert. Can
  87. /// be values, references or rvalues
  88. //-------------------------------------------------------------------------
  89. template<class ... Args>
  90. void emplace_back(Args &&... args)
  91. {
  92. std::lock_guard < spinlock_t > guard(spl);
  93. v_t.emplace_back(std::forward< Args > (args)...);
  94. }
  95. //
  96. //-------------------------------------------------------------------------
  97. // function :pop_move_back
  98. /// @brief if exist, move the last element to P, and delete it
  99. /// @param P : reference to a variable where move the element
  100. /// @return true - Element moved and deleted
  101. /// false - Empty stack_cnc
  102. //-------------------------------------------------------------------------
  103. bool pop_move_back(value_type &P)
  104. {
  105. std::lock_guard < spinlock_t > S(spl);
  106. if (v_t.size() == 0) return false;
  107. P = std::move(v_t.back());
  108. v_t.pop_back();
  109. return true;
  110. }
  111. //-------------------------------------------------------------------------
  112. // function : push_back
  113. /// @brief Insert one vector at the end of the container
  114. /// @param v_other : vector to insert
  115. /// @return reference to the stack_cnc after the insertion
  116. //-------------------------------------------------------------------------
  117. template<class Allocator2>
  118. stack_cnc &push_back(const std::vector<value_type, Allocator2> &v_other)
  119. {
  120. std::lock_guard < spinlock_t > guard(spl);
  121. for (size_type i = 0; i < v_other.size(); ++i)
  122. {
  123. v_t.push_back(v_other[i]);
  124. }
  125. return *this;
  126. }
  127. };
  128. // end class stack_cnc
  129. //***************************************************************************
  130. };// end namespace common
  131. };// end namespace sort
  132. };// end namespace boost
  133. //***************************************************************************
  134. #endif