stack.hpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  1. #ifndef __XRCU_STACK_HPP__
  2. #define __XRCU_STACK_HPP__ 1
  3. #include "xrcu.hpp"
  4. #include "optional.hpp"
  5. #include <atomic>
  6. #include <cstddef>
  7. #include <utility>
  8. #include <initializer_list>
  9. namespace std
  10. {
  11. struct forward_iterator_tag;
  12. }
  13. namespace xrcu
  14. {
  15. namespace detail
  16. {
  17. struct stack_node_base
  18. {
  19. stack_node_base *next = nullptr;
  20. typedef std::atomic<stack_node_base *> ptr_type;
  21. static stack_node_base* root (const ptr_type& head);
  22. static void push (ptr_type& head, stack_node_base *nodep);
  23. static void push (ptr_type& head,
  24. stack_node_base *nodep, stack_node_base **outp);
  25. static stack_node_base* pop (ptr_type& head);
  26. static void swap (ptr_type& h1, ptr_type& h2);
  27. static stack_node_base* clear (ptr_type& head);
  28. static size_t size (const ptr_type& head);
  29. };
  30. struct stack_iter_base : public cs_guard
  31. {
  32. stack_node_base *runp;
  33. typedef std::forward_iterator_tag iterator_category;
  34. typedef ptrdiff_t difference_type;
  35. typedef size_t size_type;
  36. stack_iter_base (stack_node_base *rp) : runp (rp)
  37. {
  38. }
  39. stack_iter_base (const stack_iter_base& right) : runp (right.runp)
  40. {
  41. }
  42. stack_iter_base (stack_iter_base&& right) : runp (right.runp)
  43. {
  44. right.runp = nullptr;
  45. }
  46. void _Adv ()
  47. {
  48. this->runp = this->runp->next;
  49. }
  50. bool operator== (const stack_iter_base& right) const
  51. {
  52. return (this->runp == right.runp);
  53. }
  54. bool operator!= (const stack_iter_base& right) const
  55. {
  56. return (this->runp != right.runp);
  57. }
  58. };
  59. } // namespace detail.
  60. template <class T>
  61. struct stack
  62. {
  63. std::atomic<detail::stack_node_base *> hnode;
  64. struct _Stknode : public detail::stack_node_base, finalizable
  65. {
  66. T value;
  67. _Stknode (const T& v) : value (v)
  68. {
  69. }
  70. _Stknode (T&& v) : value (std::move (v))
  71. {
  72. }
  73. template <class ...Args>
  74. _Stknode (Args&&... args) : value (std::forward<Args>(args)...)
  75. {
  76. }
  77. void safe_destroy ()
  78. {
  79. auto runp = this->next;
  80. delete this;
  81. stack<T>::_Clean_nodes (runp);
  82. }
  83. };
  84. typedef _Stknode node_type;
  85. typedef T value_type;
  86. typedef T& reference;
  87. typedef const T& const_reference;
  88. typedef T* pointer;
  89. typedef const T* const_pointer;
  90. typedef ptrdiff_t difference_type;
  91. typedef size_t size_type;
  92. void _Reset (detail::stack_node_base *ptr)
  93. {
  94. this->hnode.store (ptr, std::memory_order_relaxed);
  95. }
  96. static void _Clean_nodes (detail::stack_node_base *runp)
  97. {
  98. while (runp)
  99. {
  100. auto tmp = runp->next;
  101. delete (node_type *)runp;
  102. runp = tmp;
  103. }
  104. }
  105. template <class T1>
  106. void _Init (T1 first, T1 last, std::false_type)
  107. {
  108. detail::stack_node_base *runp = nullptr, **outp = &runp;
  109. try
  110. {
  111. for (; first != last; ++first)
  112. {
  113. *outp = new node_type (*first);
  114. outp = &(*outp)->next;
  115. }
  116. }
  117. catch (...)
  118. {
  119. _Clean_nodes (runp);
  120. throw;
  121. }
  122. this->_Reset (runp);
  123. }
  124. template <class T1, class T2>
  125. void _Init (T1 n, const T2& value, std::true_type)
  126. {
  127. detail::stack_node_base *runp = nullptr, **outp = &runp;
  128. try
  129. {
  130. for (T1 x = 0; x != n; ++x)
  131. {
  132. *outp = new node_type (value);
  133. outp = &(*outp)->next;
  134. }
  135. }
  136. catch (...)
  137. {
  138. _Clean_nodes (runp);
  139. throw;
  140. }
  141. this->_Reset (runp);
  142. }
  143. stack ()
  144. {
  145. this->_Reset (nullptr);
  146. }
  147. template <class T1, class T2>
  148. stack (T1 first, T2 last)
  149. {
  150. this->_Reset (nullptr);
  151. this->_Init (first, last, typename std::is_integral<T1>::type ());
  152. }
  153. stack (std::initializer_list<T> lst) : stack (lst.begin (), lst.end ())
  154. {
  155. }
  156. stack (const stack<T>& right) : stack (right.begin (), right.end ())
  157. {
  158. }
  159. stack (stack<T>&& right)
  160. {
  161. this->_Reset (right._Root ());
  162. right._Reset (nullptr);
  163. }
  164. void push (const T& value)
  165. {
  166. cs_guard g;
  167. detail::stack_node_base::push (this->hnode, new node_type (value));
  168. }
  169. void push (T&& value)
  170. {
  171. cs_guard g;
  172. detail::stack_node_base::push (this->hnode,
  173. new node_type (std::move (value)));
  174. }
  175. template <class Iter>
  176. void _Push (Iter first, Iter last, std::false_type)
  177. {
  178. if (first == last)
  179. return;
  180. node_type *np;
  181. try
  182. {
  183. np = new node_type (*first);
  184. auto **outp = &np->next;
  185. for (++first; first != last; ++first)
  186. {
  187. node_type *tmp = new node_type (*first);
  188. *outp = tmp, outp = &tmp->next;
  189. }
  190. detail::stack_node_base::push (this->hnode, np, outp);
  191. }
  192. catch (...)
  193. {
  194. _Clean_nodes (np);
  195. throw;
  196. }
  197. }
  198. template <class T1, class T2>
  199. void _Push (T1 n, const T2& value, std::true_type)
  200. {
  201. if (n == 0)
  202. return;
  203. node_type *np;
  204. try
  205. {
  206. np = new node_type (value);
  207. auto **outp = &np->next;
  208. for (; n != 0; --n)
  209. {
  210. node_type *tmp = new node_type (value);
  211. *outp = tmp, outp = &tmp->next;
  212. }
  213. detail::stack_node_base::push (this->hnode, np, outp);
  214. }
  215. catch (...)
  216. {
  217. _Clean_nodes (np);
  218. throw;
  219. }
  220. }
  221. template <class T1, class T2>
  222. void push (T1 first, T2 last)
  223. {
  224. this->_Push (first, last, typename std::is_integral<T1>::type ());
  225. }
  226. template <class ...Args>
  227. void emplace (Args&& ...args)
  228. {
  229. detail::stack_node_base::push (this->hnode,
  230. new node_type (std::forward<Args>(args)...));
  231. }
  232. optional<T> pop ()
  233. {
  234. cs_guard g;
  235. auto node = detail::stack_node_base::pop (this->hnode);
  236. if (!node)
  237. return (optional<T> ());
  238. optional<T> ret { ((node_type *)node)->value };
  239. finalize ((node_type *)node);
  240. return (ret);
  241. }
  242. node_type* _Root ()
  243. {
  244. return ((node_type *)detail::stack_node_base::root (this->hnode));
  245. }
  246. node_type* _Root () const
  247. {
  248. return ((node_type *)detail::stack_node_base::root (this->hnode));
  249. }
  250. optional<T> top ()
  251. {
  252. cs_guard g;
  253. auto node = this->_Root ();
  254. return (node ? optional<T> { node->value } : optional<T> ());
  255. }
  256. struct iterator : public detail::stack_iter_base
  257. {
  258. typedef T value_type;
  259. typedef T& reference;
  260. typedef T* pointer;
  261. iterator (detail::stack_node_base *rp = nullptr) :
  262. detail::stack_iter_base (rp)
  263. {
  264. }
  265. iterator (const iterator& it) : detail::stack_iter_base (it)
  266. {
  267. }
  268. iterator (iterator&& it) : detail::stack_iter_base (std::move (it))
  269. {
  270. }
  271. T& operator* ()
  272. {
  273. return (((node_type *)this->runp)->value);
  274. }
  275. const T& operator* () const
  276. {
  277. return (((const node_type *)this->runp)->value);
  278. }
  279. T* operator-> ()
  280. {
  281. return (&**this);
  282. }
  283. const T* operator-> () const
  284. {
  285. return (&**this);
  286. }
  287. iterator& operator++ ()
  288. {
  289. this->_Adv ();
  290. return (*this);
  291. }
  292. iterator operator++ (int)
  293. {
  294. iterator tmp = { this->runp };
  295. this->_Adv ();
  296. return (tmp);
  297. }
  298. };
  299. typedef const iterator const_iterator;
  300. iterator begin ()
  301. {
  302. return (iterator (this->_Root ()));
  303. }
  304. iterator end ()
  305. {
  306. return (iterator ());
  307. }
  308. const_iterator cbegin () const
  309. {
  310. return (const_iterator (this->_Root ()));
  311. }
  312. const_iterator cend () const
  313. {
  314. return (const_iterator ());
  315. }
  316. const_iterator begin () const
  317. {
  318. return (this->cbegin ());
  319. }
  320. const_iterator end () const
  321. {
  322. return (this->cend ());
  323. }
  324. size_t size () const
  325. {
  326. cs_guard g;
  327. return (detail::stack_node_base::size (this->hnode));
  328. }
  329. size_t max_size () const
  330. {
  331. return (~(size_t)0);
  332. }
  333. bool empty () const
  334. {
  335. return (this->_Root () == nullptr);
  336. }
  337. void swap (stack<T>& right)
  338. {
  339. cs_guard g;
  340. if (this != &right)
  341. detail::stack_node_base::swap (this->hnode, right.hnode);
  342. }
  343. stack<T>& operator= (const stack<T>& right)
  344. {
  345. cs_guard g;
  346. if (this != &right)
  347. this->assign (right.begin (), right.end ());
  348. return (*this);
  349. }
  350. stack<T>& operator= (stack<T>&& right)
  351. {
  352. this->swap (right);
  353. finalize (right._Root ());
  354. right._Reset (nullptr);
  355. return (*this);
  356. }
  357. bool operator== (const stack<T>& right) const
  358. {
  359. auto x1 = this->cbegin (), x2 = this->cend ();
  360. auto y1 = right.cbegin (), y2 = right.cend ();
  361. for (; x1 != x2 && y1 != y2; ++x1, ++y1)
  362. if (*x1 != *y1)
  363. return (false);
  364. return (x1 == x2 && y1 == y2);
  365. }
  366. bool operator!= (const stack<T>& right) const
  367. {
  368. return (!(*this == right));
  369. }
  370. bool operator< (const stack<T>& right) const
  371. {
  372. auto x1 = this->cbegin (), x2 = this->cend ();
  373. auto y1 = right.cbegin (), y2 = right.cend ();
  374. for (; x1 != x2; ++x1, ++y1)
  375. {
  376. if (y1 == y2 || *y1 < *x1)
  377. return (false);
  378. else if (*x1 < *y1)
  379. return (true);
  380. }
  381. return (y1 != y2);
  382. }
  383. bool operator> (const stack<T>& right) const
  384. {
  385. return (right < *this);
  386. }
  387. bool operator<= (const stack<T>& right) const
  388. {
  389. return (!(right < *this));
  390. }
  391. bool operator>= (const stack<T>& right) const
  392. {
  393. return (!(*this < right));
  394. }
  395. void clear ()
  396. {
  397. auto prev = detail::stack_node_base::clear (this->hnode);
  398. finalize ((node_type *)prev);
  399. }
  400. template <class T1, class T2>
  401. void assign (T1 first, T2 last)
  402. {
  403. auto tmp = stack<T> (first, last);
  404. this->swap (tmp);
  405. finalize (tmp._Root ());
  406. tmp._Reset (nullptr);
  407. }
  408. void assign (std::initializer_list<T> lst)
  409. {
  410. this->assign (lst.begin (), lst.end ());
  411. }
  412. ~stack ()
  413. {
  414. auto tmp = this->_Root ();
  415. if (!tmp)
  416. return;
  417. _Clean_nodes (tmp);
  418. this->_Reset (nullptr);
  419. }
  420. };
  421. } // namespace xrcu
  422. namespace std
  423. {
  424. template <class T>
  425. void swap (xrcu::stack<T>& left, xrcu::stack<T>& right)
  426. {
  427. return (left.swap (right));
  428. }
  429. } // namespace std
  430. #endif