split_list_set.h 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979
  1. // Copyright (c) 2006-2018 Maxim Khizhinsky
  2. //
  3. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  4. // file LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. #ifndef CDSLIB_CONTAINER_SPLIT_LIST_SET_H
  6. #define CDSLIB_CONTAINER_SPLIT_LIST_SET_H
  7. #include <cds/intrusive/split_list.h>
  8. #include <cds/container/details/make_split_list_set.h>
  9. #include <cds/container/details/guarded_ptr_cast.h>
  10. namespace cds { namespace container {
  11. /// Split-ordered list set
  12. /** @ingroup cds_nonintrusive_set
  13. \anchor cds_nonintrusive_SplitListSet_hp
  14. Hash table implementation based on split-ordered list algorithm discovered by Ori Shalev and Nir Shavit, see
  15. - [2003] Ori Shalev, Nir Shavit "Split-Ordered Lists - Lock-free Resizable Hash Tables"
  16. - [2008] Nir Shavit "The Art of Multiprocessor Programming"
  17. See \p intrusive::SplitListSet for a brief description of the split-list algorithm.
  18. Template parameters:
  19. - \p GC - Garbage collector used
  20. - \p T - type to be stored in the split-list.
  21. - \p Traits - type traits, default is \p split_list::traits. Instead of declaring \p split_list::traits -based
  22. struct you may apply option-based notation with \p split_list::make_traits metafunction.
  23. There are the specializations:
  24. - for \ref cds_urcu_desc "RCU" - declared in <tt>cd/container/split_list_set_rcu.h</tt>,
  25. see \ref cds_nonintrusive_SplitListSet_rcu "SplitListSet<RCU>".
  26. - for \ref cds::gc::nogc declared in <tt>cds/container/split_list_set_nogc.h</tt>,
  27. see \ref cds_nonintrusive_SplitListSet_nogc "SplitListSet<gc::nogc>".
  28. \par Usage
  29. You should decide what garbage collector you want, and what ordered list you want to use as a base. Split-ordered list
  30. is original data structure based on an ordered list.
  31. Suppose, you want construct split-list set based on \p gc::DHP GC
  32. and \p LazyList as ordered list implementation. So, you beginning your program with following include:
  33. \code
  34. #include <cds/container/lazy_list_dhp.h>
  35. #include <cds/container/split_list_set.h>
  36. namespace cc = cds::container;
  37. // The data belonged to split-ordered list
  38. sturuct foo {
  39. int nKey; // key field
  40. std::string strValue ; // value field
  41. };
  42. \endcode
  43. The inclusion order is important: first, include header for ordered-list implementation (for this example, <tt>cds/container/lazy_list_dhp.h</tt>),
  44. then the header for split-list set <tt>cds/container/split_list_set.h</tt>.
  45. Now, you should declare traits for split-list set. The main parts of traits are a hash functor for the set and a comparing functor for ordered list.
  46. Note that we define several function in <tt>foo_hash</tt> and <tt>foo_less</tt> functors for different argument types since we want call our \p %SplitListSet
  47. object by the key of type <tt>int</tt> and by the value of type <tt>foo</tt>.
  48. The second attention: instead of using \p %LazyList in \p %SplitListSet traits we use a tag \p cds::contaner::lazy_list_tag for the lazy list.
  49. The split-list requires significant support from underlying ordered list class and it is not good idea to dive you
  50. into deep implementation details of split-list and ordered list interrelations. The tag paradigm simplifies split-list interface.
  51. \code
  52. // foo hash functor
  53. struct foo_hash {
  54. size_t operator()( int key ) const { return std::hash( key ) ; }
  55. size_t operator()( foo const& item ) const { return std::hash( item.nKey ) ; }
  56. };
  57. // foo comparator
  58. struct foo_less {
  59. bool operator()(int i, foo const& f ) const { return i < f.nKey ; }
  60. bool operator()(foo const& f, int i ) const { return f.nKey < i ; }
  61. bool operator()(foo const& f1, foo const& f2) const { return f1.nKey < f2.nKey; }
  62. };
  63. // SplitListSet traits
  64. struct foo_set_traits: public cc::split_list::traits
  65. {
  66. typedef cc::lazy_list_tag ordered_list; // what type of ordered list we want to use
  67. typedef foo_hash hash; // hash functor for our data stored in split-list set
  68. // Type traits for our LazyList class
  69. struct ordered_list_traits: public cc::lazy_list::traits
  70. {
  71. typedef foo_less less ; // use our foo_less as comparator to order list nodes
  72. };
  73. };
  74. \endcode
  75. Now you are ready to declare our set class based on \p %SplitListSet:
  76. \code
  77. typedef cc::SplitListSet< cds::gc::DHP, foo, foo_set_traits > foo_set;
  78. \endcode
  79. You may use the modern option-based declaration instead of classic traits-based one:
  80. \code
  81. typedef cc::SplitListSet<
  82. cs::gc::DHP // GC used
  83. ,foo // type of data stored
  84. ,cc::split_list::make_traits< // metafunction to build split-list traits
  85. cc::split_list::ordered_list<cc::lazy_list_tag> // tag for underlying ordered list implementation
  86. ,cc::opt::hash< foo_hash > // hash functor
  87. ,cc::split_list::ordered_list_traits< // ordered list traits desired
  88. cc::lazy_list::make_traits< // metafunction to build lazy list traits
  89. cc::opt::less< foo_less > // less-based compare functor
  90. >::type
  91. >
  92. >::type
  93. > foo_set;
  94. \endcode
  95. In case of option-based declaration using split_list::make_traits metafunction
  96. the struct \p foo_set_traits is not required.
  97. Now, the set of type \p foo_set is ready to use in your program.
  98. Note that in this example we show only mandatory \p traits parts, optional ones is the default and they are inherited
  99. from \p cds::container::split_list::traits.
  100. There are many other options for deep tuning the split-list and ordered-list containers.
  101. */
  102. template <
  103. class GC,
  104. class T,
  105. #ifdef CDS_DOXYGEN_INVOKED
  106. class Traits = split_list::traits
  107. #else
  108. class Traits
  109. #endif
  110. >
  111. class SplitListSet:
  112. #ifdef CDS_DOXYGEN_INVOKED
  113. protected intrusive::SplitListSet<GC, typename Traits::ordered_list, Traits>
  114. #else
  115. protected details::make_split_list_set< GC, T, typename Traits::ordered_list, split_list::details::wrap_set_traits<T, Traits> >::type
  116. #endif
  117. {
  118. protected:
  119. //@cond
  120. typedef details::make_split_list_set< GC, T, typename Traits::ordered_list, split_list::details::wrap_set_traits<T, Traits> > maker;
  121. typedef typename maker::type base_class;
  122. //@endcond
  123. public:
  124. typedef GC gc; ///< Garbage collector
  125. typedef T value_type; ///< Type of vlue to be stored in split-list
  126. typedef Traits traits; ///< \p Traits template argument
  127. typedef typename maker::ordered_list ordered_list; ///< Underlying ordered list class
  128. typedef typename base_class::key_comparator key_comparator; ///< key compare functor
  129. /// Hash functor for \p %value_type and all its derivatives that you use
  130. typedef typename base_class::hash hash;
  131. typedef typename base_class::item_counter item_counter; ///< Item counter type
  132. typedef typename base_class::stat stat; ///< Internal statistics
  133. /// Count of hazard pointer required
  134. static constexpr const size_t c_nHazardPtrCount = base_class::c_nHazardPtrCount;
  135. protected:
  136. //@cond
  137. typedef typename maker::cxx_node_allocator cxx_node_allocator;
  138. typedef typename maker::node_type node_type;
  139. //@endcond
  140. public:
  141. /// Guarded pointer
  142. typedef typename gc::template guarded_ptr< node_type, value_type, details::guarded_ptr_cast_set<node_type, value_type> > guarded_ptr;
  143. protected:
  144. //@cond
  145. template <bool IsConst>
  146. class iterator_type: protected base_class::template iterator_type<IsConst>
  147. {
  148. typedef typename base_class::template iterator_type<IsConst> iterator_base_class;
  149. friend class SplitListSet;
  150. public:
  151. /// Value pointer type (const for const iterator)
  152. typedef typename cds::details::make_const_type<value_type, IsConst>::pointer value_ptr;
  153. /// Value reference type (const for const iterator)
  154. typedef typename cds::details::make_const_type<value_type, IsConst>::reference value_ref;
  155. public:
  156. /// Default ctor
  157. iterator_type()
  158. {}
  159. /// Copy ctor
  160. iterator_type( iterator_type const& src )
  161. : iterator_base_class( src )
  162. {}
  163. protected:
  164. explicit iterator_type( iterator_base_class const& src )
  165. : iterator_base_class( src )
  166. {}
  167. public:
  168. /// Dereference operator
  169. value_ptr operator ->() const
  170. {
  171. return &(iterator_base_class::operator->()->m_Value);
  172. }
  173. /// Dereference operator
  174. value_ref operator *() const
  175. {
  176. return iterator_base_class::operator*().m_Value;
  177. }
  178. /// Pre-increment
  179. iterator_type& operator ++()
  180. {
  181. iterator_base_class::operator++();
  182. return *this;
  183. }
  184. /// Assignment operator
  185. iterator_type& operator = (iterator_type const& src)
  186. {
  187. iterator_base_class::operator=(src);
  188. return *this;
  189. }
  190. /// Equality operator
  191. template <bool C>
  192. bool operator ==(iterator_type<C> const& i ) const
  193. {
  194. return iterator_base_class::operator==(i);
  195. }
  196. /// Equality operator
  197. template <bool C>
  198. bool operator !=(iterator_type<C> const& i ) const
  199. {
  200. return iterator_base_class::operator!=(i);
  201. }
  202. };
  203. //@endcond
  204. public:
  205. /// Initializes split-ordered list of default capacity
  206. /**
  207. The default capacity is defined in bucket table constructor.
  208. See \p intrusive::split_list::expandable_bucket_table, \p intrusive::split_list::static_bucket_table
  209. which selects by \p split_list::dynamic_bucket_table option.
  210. */
  211. SplitListSet()
  212. : base_class()
  213. {}
  214. /// Initializes split-ordered list
  215. SplitListSet(
  216. size_t nItemCount ///< estimated average of item count
  217. , size_t nLoadFactor = 1 ///< the load factor - average item count per bucket. Small integer up to 8, default is 1.
  218. )
  219. : base_class( nItemCount, nLoadFactor )
  220. {}
  221. public:
  222. ///@name Forward iterators (only for debugging purpose)
  223. //@{
  224. /// Forward iterator
  225. /**
  226. The forward iterator for a split-list has the following features:
  227. - it has no post-increment operator
  228. - it depends on underlying ordered list iterator
  229. - The iterator object cannot be moved across thread boundary because it contains GC's guard that is thread-private GC data.
  230. - Iterator ensures thread-safety even if you delete the item that iterator points to. However, in case of concurrent
  231. deleting operations it is no guarantee that you iterate all item in the split-list.
  232. Moreover, a crash is possible when you try to iterate the next element that has been deleted by concurrent thread.
  233. @warning Use this iterator on the concurrent container for debugging purpose only.
  234. The iterator interface:
  235. \code
  236. class iterator {
  237. public:
  238. // Default constructor
  239. iterator();
  240. // Copy construtor
  241. iterator( iterator const& src );
  242. // Dereference operator
  243. value_type * operator ->() const;
  244. // Dereference operator
  245. value_type& operator *() const;
  246. // Preincrement operator
  247. iterator& operator ++();
  248. // Assignment operator
  249. iterator& operator = (iterator const& src);
  250. // Equality operators
  251. bool operator ==(iterator const& i ) const;
  252. bool operator !=(iterator const& i ) const;
  253. };
  254. \endcode
  255. */
  256. typedef iterator_type<false> iterator;
  257. /// Const forward iterator
  258. typedef iterator_type<true> const_iterator;
  259. /// Returns a forward iterator addressing the first element in a set
  260. /**
  261. For empty set \code begin() == end() \endcode
  262. */
  263. iterator begin()
  264. {
  265. return iterator( base_class::begin());
  266. }
  267. /// Returns an iterator that addresses the location succeeding the last element in a set
  268. /**
  269. Do not use the value returned by <tt>end</tt> function to access any item.
  270. The returned value can be used only to control reaching the end of the set.
  271. For empty set \code begin() == end() \endcode
  272. */
  273. iterator end()
  274. {
  275. return iterator( base_class::end());
  276. }
  277. /// Returns a forward const iterator addressing the first element in a set
  278. const_iterator begin() const
  279. {
  280. return cbegin();
  281. }
  282. /// Returns a forward const iterator addressing the first element in a set
  283. const_iterator cbegin() const
  284. {
  285. return const_iterator( base_class::cbegin());
  286. }
  287. /// Returns an const iterator that addresses the location succeeding the last element in a set
  288. const_iterator end() const
  289. {
  290. return cend();
  291. }
  292. /// Returns an const iterator that addresses the location succeeding the last element in a set
  293. const_iterator cend() const
  294. {
  295. return const_iterator( base_class::cend());
  296. }
  297. //@}
  298. public:
  299. /// Inserts new node
  300. /**
  301. The function creates a node with copy of \p val value
  302. and then inserts the node created into the set.
  303. The type \p Q should contain as minimum the complete key for the node.
  304. The object of \ref value_type should be constructible from a value of type \p Q.
  305. In trivial case, \p Q is equal to \ref value_type.
  306. Returns \p true if \p val is inserted into the set, \p false otherwise.
  307. */
  308. template <typename Q>
  309. bool insert( Q&& val )
  310. {
  311. return insert_node( alloc_node( std::forward<Q>( val )));
  312. }
  313. /// Inserts new node
  314. /**
  315. The function allows to split creating of new item into two part:
  316. - create item with key only
  317. - insert new item into the set
  318. - if inserting is success, calls \p f functor to initialize value-field of \p val.
  319. The functor signature is:
  320. \code
  321. void func( value_type& val );
  322. \endcode
  323. where \p val is the item inserted.
  324. The user-defined functor is called only if the inserting is success.
  325. @warning For \ref cds_intrusive_MichaelList_hp "MichaelList" as the bucket see \ref cds_intrusive_item_creating "insert item troubleshooting".
  326. \ref cds_intrusive_LazyList_hp "LazyList" provides exclusive access to inserted item and does not require any node-level
  327. synchronization.
  328. */
  329. template <typename Q, typename Func>
  330. bool insert( Q&& val, Func f )
  331. {
  332. scoped_node_ptr pNode( alloc_node( std::forward<Q>( val )));
  333. if ( base_class::insert( *pNode, [&f](node_type& node) { f( node.m_Value ) ; } )) {
  334. pNode.release();
  335. return true;
  336. }
  337. return false;
  338. }
  339. /// Inserts data of type \p value_type created from \p args
  340. /**
  341. Returns \p true if inserting successful, \p false otherwise.
  342. */
  343. template <typename... Args>
  344. bool emplace( Args&&... args )
  345. {
  346. return insert_node( alloc_node( std::forward<Args>(args)...));
  347. }
  348. /// Inserts or updates the node (only for \p IterableList -based set)
  349. /**
  350. The operation performs inserting or changing data with lock-free manner.
  351. If the item \p val is not found in the set, then \p val is inserted iff \p bAllowInsert is \p true.
  352. Otherwise, the current element is changed to \p val, the old element will be retired later.
  353. Returns std::pair<bool, bool> where \p first is \p true if operation is successful,
  354. \p second is \p true if \p val has been added or \p false if the item with that key
  355. already in the set.
  356. */
  357. template <typename Q>
  358. #ifdef CDS_DOXYGEN_INVOKED
  359. std::pair<bool, bool>
  360. #else
  361. typename std::enable_if<
  362. std::is_same< Q, Q>::value && is_iterable_list< ordered_list >::value,
  363. std::pair<bool, bool>
  364. >::type
  365. #endif
  366. upsert( Q&& val, bool bAllowInsert = true )
  367. {
  368. scoped_node_ptr pNode( alloc_node( std::forward<Q>( val )));
  369. auto bRet = base_class::upsert( *pNode, bAllowInsert );
  370. if ( bRet.first )
  371. pNode.release();
  372. return bRet;
  373. }
  374. /// Updates the node
  375. /**
  376. The operation performs inserting or changing data with lock-free manner.
  377. If \p key is not found in the set, then \p key is inserted iff \p bAllowInsert is \p true.
  378. Otherwise, the functor \p func is called with item found.
  379. The functor \p func signature depends of ordered list:
  380. <b>for \p MichaelList, \p LazyList</b>
  381. \code
  382. struct functor {
  383. void operator()( bool bNew, value_type& item, Q const& val );
  384. };
  385. \endcode
  386. with arguments:
  387. - \p bNew - \p true if the item has been inserted, \p false otherwise
  388. - \p item - item of the set
  389. - \p val - argument \p val passed into the \p %update() function
  390. The functor may change non-key fields of the \p item.
  391. <b>for \p IterableList</b>
  392. \code
  393. void func( value_type& val, value_type * old );
  394. \endcode
  395. where
  396. - \p val - a new data constructed from \p key
  397. - \p old - old value that will be retired. If new item has been inserted then \p old is \p nullptr.
  398. Returns <tt> std::pair<bool, bool> </tt> where \p first is true if operation is successful,
  399. \p second is true if new item has been added or \p false if the item with \p key
  400. already is in the set.
  401. @warning For \ref cds_intrusive_MichaelList_hp "MichaelList" and \ref cds_nonintrusive_IterableList_gc "IterableList"
  402. as the bucket see \ref cds_intrusive_item_creating "insert item troubleshooting".
  403. \ref cds_intrusive_LazyList_hp "LazyList" provides exclusive access to inserted item and does not require any node-level
  404. synchronization.
  405. */
  406. template <typename Q, typename Func>
  407. #ifdef CDS_DOXYGEN_INVOKED
  408. std::pair<bool, bool>
  409. #else
  410. typename std::enable_if<
  411. std::is_same<Q, Q>::value && !is_iterable_list<ordered_list>::value,
  412. std::pair<bool, bool>
  413. >::type
  414. #endif
  415. update( Q&& val, Func func, bool bAllowInsert = true )
  416. {
  417. scoped_node_ptr pNode( alloc_node( std::forward<Q>( val )));
  418. auto bRet = base_class::update( *pNode,
  419. [&func, &val]( bool bNew, node_type& item, node_type const& /*val*/ ) {
  420. func( bNew, item.m_Value, val );
  421. }, bAllowInsert );
  422. if ( bRet.first && bRet.second )
  423. pNode.release();
  424. return bRet;
  425. }
  426. //@cond
  427. template <typename Q, typename Func>
  428. typename std::enable_if<
  429. std::is_same<Q, Q>::value && is_iterable_list<ordered_list>::value,
  430. std::pair<bool, bool>
  431. >::type
  432. update( Q&& val, Func func, bool bAllowInsert = true )
  433. {
  434. scoped_node_ptr pNode( alloc_node( std::forward<Q>( val )));
  435. auto bRet = base_class::update( *pNode,
  436. [&func]( node_type& item, node_type* old ) {
  437. func( item.m_Value, old ? &old->m_Value : nullptr );
  438. }, bAllowInsert );
  439. if ( bRet.first )
  440. pNode.release();
  441. return bRet;
  442. }
  443. //@endcond
  444. //@cond
  445. template <typename Q, typename Func>
  446. CDS_DEPRECATED("ensure() is deprecated, use update()")
  447. std::pair<bool, bool> ensure( Q const& val, Func func )
  448. {
  449. return update( val, func, true );
  450. }
  451. //@endcond
  452. /// Deletes \p key from the set
  453. /** \anchor cds_nonintrusive_SplitListSet_erase_val
  454. The item comparator should be able to compare the values of type \p value_type
  455. and the type \p Q.
  456. Return \p true if key is found and deleted, \p false otherwise
  457. */
  458. template <typename Q>
  459. bool erase( Q const& key )
  460. {
  461. return base_class::erase( key );
  462. }
  463. /// Deletes the item from the set using \p pred predicate for searching
  464. /**
  465. The function is an analog of \ref cds_nonintrusive_SplitListSet_erase_val "erase(Q const&)"
  466. but \p pred is used for key comparing.
  467. \p Less functor has the interface like \p std::less.
  468. \p Less must imply the same element order as the comparator used for building the set.
  469. */
  470. template <typename Q, typename Less>
  471. bool erase_with( Q const& key, Less pred )
  472. {
  473. CDS_UNUSED( pred );
  474. return base_class::erase_with( key, typename maker::template predicate_wrapper<Less>());
  475. }
  476. /// Deletes \p key from the set
  477. /** \anchor cds_nonintrusive_SplitListSet_erase_func
  478. The function searches an item with key \p key, calls \p f functor
  479. and deletes the item. If \p key is not found, the functor is not called.
  480. The functor \p Func interface:
  481. \code
  482. struct extractor {
  483. void operator()(value_type const& val);
  484. };
  485. \endcode
  486. Since the key of split-list \p value_type is not explicitly specified,
  487. template parameter \p Q defines the key type searching in the list.
  488. The list item comparator should be able to compare the values of the type \p value_type
  489. and the type \p Q.
  490. Return \p true if key is found and deleted, \p false otherwise
  491. */
  492. template <typename Q, typename Func>
  493. bool erase( Q const& key, Func f )
  494. {
  495. return base_class::erase( key, [&f](node_type& node) { f( node.m_Value ); } );
  496. }
  497. /// Deletes the item from the set using \p pred predicate for searching
  498. /**
  499. The function is an analog of \ref cds_nonintrusive_SplitListSet_erase_func "erase(Q const&, Func)"
  500. but \p pred is used for key comparing.
  501. \p Less functor has the interface like \p std::less.
  502. \p Less must imply the same element order as the comparator used for building the set.
  503. */
  504. template <typename Q, typename Less, typename Func>
  505. bool erase_with( Q const& key, Less pred, Func f )
  506. {
  507. CDS_UNUSED( pred );
  508. return base_class::erase_with( key, typename maker::template predicate_wrapper<Less>(),
  509. [&f](node_type& node) { f( node.m_Value ); } );
  510. }
  511. /// Deletes the item pointed by iterator \p iter (only for \p IterableList based set)
  512. /**
  513. Returns \p true if the operation is successful, \p false otherwise.
  514. The function can return \p false if the node the iterator points to has already been deleted
  515. by other thread.
  516. The function does not invalidate the iterator, it remains valid and can be used for further traversing.
  517. @note \p %erase_at() is supported only for \p %SplitListSet based on \p IterableList.
  518. */
  519. #ifdef CDS_DOXYGEN_INVOKED
  520. bool erase_at( iterator const& iter )
  521. #else
  522. template <typename Iterator>
  523. typename std::enable_if< std::is_same<Iterator, iterator>::value && is_iterable_list< ordered_list >::value, bool >::type
  524. erase_at( Iterator const& iter )
  525. #endif
  526. {
  527. return base_class::erase_at( static_cast<typename iterator::iterator_base_class const&>( iter ));
  528. }
  529. /// Extracts the item with specified \p key
  530. /** \anchor cds_nonintrusive_SplitListSet_hp_extract
  531. The function searches an item with key equal to \p key,
  532. unlinks it from the set, and returns it as \p guarded_ptr.
  533. If \p key is not found the function returns an empty guarded pointer.
  534. Note the compare functor should accept a parameter of type \p Q that may be not the same as \p value_type.
  535. The extracted item is freed automatically when returned \p guarded_ptr object will be destroyed or released.
  536. @note Each \p guarded_ptr object uses the GC's guard that can be limited resource.
  537. Usage:
  538. \code
  539. typedef cds::container::SplitListSet< your_template_args > splitlist_set;
  540. splitlist_set theSet;
  541. // ...
  542. {
  543. splitlist_set::guarded_ptr gp(theSet.extract( 5 ));
  544. if ( gp ) {
  545. // Deal with gp
  546. // ...
  547. }
  548. // Destructor of gp releases internal HP guard
  549. }
  550. \endcode
  551. */
  552. template <typename Q>
  553. guarded_ptr extract( Q const& key )
  554. {
  555. return extract_( key );
  556. }
  557. /// Extracts the item using compare functor \p pred
  558. /**
  559. The function is an analog of \ref cds_nonintrusive_SplitListSet_hp_extract "extract(Q const&)"
  560. but \p pred predicate is used for key comparing.
  561. \p Less functor has the semantics like \p std::less but should take arguments of type \ref value_type and \p Q
  562. in any order.
  563. \p pred must imply the same element order as the comparator used for building the set.
  564. */
  565. template <typename Q, typename Less>
  566. guarded_ptr extract_with( Q const& key, Less pred )
  567. {
  568. return extract_with_( key, pred );
  569. }
  570. /// Finds the key \p key
  571. /** \anchor cds_nonintrusive_SplitListSet_find_func
  572. The function searches the item with key equal to \p key and calls the functor \p f for item found.
  573. The interface of \p Func functor is:
  574. \code
  575. struct functor {
  576. void operator()( value_type& item, Q& key );
  577. };
  578. \endcode
  579. where \p item is the item found, \p key is the <tt>find</tt> function argument.
  580. The functor may change non-key fields of \p item. Note that the functor is only guarantee
  581. that \p item cannot be disposed during functor is executing.
  582. The functor does not serialize simultaneous access to the set's \p item. If such access is
  583. possible you must provide your own synchronization schema on item level to exclude unsafe item modifications.
  584. The \p key argument is non-const since it can be used as \p f functor destination i.e., the functor
  585. may modify both arguments.
  586. Note the hash functor specified for class \p Traits template parameter
  587. should accept a parameter of type \p Q that can be not the same as \p value_type.
  588. The function returns \p true if \p key is found, \p false otherwise.
  589. */
  590. template <typename Q, typename Func>
  591. bool find( Q& key, Func f )
  592. {
  593. return find_( key, f );
  594. }
  595. //@cond
  596. template <typename Q, typename Func>
  597. bool find( Q const& key, Func f )
  598. {
  599. return find_( key, f );
  600. }
  601. //@endcond
  602. /// Finds \p key and returns iterator pointed to the item found (only for \p IterableList -based set)
  603. /**
  604. If \p key is not found the function returns \p end().
  605. @note This function is supported only for the set based on \p IterableList
  606. */
  607. template <typename Q>
  608. #ifdef CDS_DOXYGEN_INVOKED
  609. iterator
  610. #else
  611. typename std::enable_if< std::is_same<Q,Q>::value && is_iterable_list< ordered_list >::value, iterator >::type
  612. #endif
  613. find( Q& key )
  614. {
  615. return find_iterator_( key );
  616. }
  617. //@cond
  618. template <typename Q>
  619. typename std::enable_if< std::is_same<Q, Q>::value && is_iterable_list< ordered_list >::value, iterator >::type
  620. find( Q const& key )
  621. {
  622. return find_iterator_( key );
  623. }
  624. //@endcond
  625. /// Finds the key \p key using \p pred predicate for searching
  626. /**
  627. The function is an analog of \ref cds_nonintrusive_SplitListSet_find_func "find(Q&, Func)"
  628. but \p pred is used for key comparing.
  629. \p Less functor has the interface like \p std::less.
  630. \p Less must imply the same element order as the comparator used for building the set.
  631. */
  632. template <typename Q, typename Less, typename Func>
  633. bool find_with( Q& key, Less pred, Func f )
  634. {
  635. return find_with_( key, pred, f );
  636. }
  637. //@cond
  638. template <typename Q, typename Less, typename Func>
  639. bool find_with( Q const& key, Less pred, Func f )
  640. {
  641. return find_with_( key, pred, f );
  642. }
  643. //@endcond
  644. /// Finds \p key using \p pred predicate and returns iterator pointed to the item found (only for \p IterableList -based set)
  645. /**
  646. The function is an analog of \p find(Q&) but \p pred is used for key comparing.
  647. \p Less functor has the interface like \p std::less.
  648. \p pred must imply the same element order as the comparator used for building the set.
  649. If \p key is not found the function returns \p end().
  650. @note This function is supported only for the set based on \p IterableList
  651. */
  652. template <typename Q, typename Less>
  653. #ifdef CDS_DOXYGEN_INVOKED
  654. iterator
  655. #else
  656. typename std::enable_if< std::is_same<Q, Q>::value && is_iterable_list< ordered_list >::value, iterator >::type
  657. #endif
  658. find_with( Q& key, Less pred )
  659. {
  660. return find_iterator_with_( key, pred );
  661. }
  662. //@cond
  663. template <typename Q, typename Less>
  664. typename std::enable_if< std::is_same<Q, Q>::value && is_iterable_list< ordered_list >::value, iterator >::type
  665. find_with( Q const& key, Less pred )
  666. {
  667. return find_iterator_with_( key, pred );
  668. }
  669. //@endcond
  670. /// Checks whether the set contains \p key
  671. /**
  672. The function searches the item with key equal to \p key
  673. and returns \p true if it is found, and \p false otherwise.
  674. Note the hash functor specified for class \p Traits template parameter
  675. should accept a parameter of type \p Q that can be not the same as \p value_type.
  676. Otherwise, you may use \p contains( Q const&, Less pred ) functions with explicit predicate for key comparing.
  677. */
  678. template <typename Q>
  679. bool contains( Q const& key )
  680. {
  681. return base_class::contains( key );
  682. }
  683. /// Checks whether the map contains \p key using \p pred predicate for searching
  684. /**
  685. The function is similar to <tt>contains( key )</tt> but \p pred is used for key comparing.
  686. \p Less functor has the interface like \p std::less.
  687. \p Less must imply the same element order as the comparator used for building the map.
  688. */
  689. template <typename Q, typename Less>
  690. bool contains( Q const& key, Less pred )
  691. {
  692. CDS_UNUSED( pred );
  693. return base_class::contains( key, typename maker::template predicate_wrapper<Less>());
  694. }
  695. /// Finds the key \p key and return the item found
  696. /** \anchor cds_nonintrusive_SplitListSet_hp_get
  697. The function searches the item with key equal to \p key
  698. and returns the item found as \p guarded_ptr.
  699. If \p key is not found the function returns an empty guarded pointer.
  700. @note Each \p guarded_ptr object uses one GC's guard which can be limited resource.
  701. Usage:
  702. \code
  703. typedef cds::container::SplitListSet< your_template_params > splitlist_set;
  704. splitlist_set theSet;
  705. // ...
  706. {
  707. splitlist_set::guarded_ptr gp(theSet.get( 5 ));
  708. if ( gp ) {
  709. // Deal with gp
  710. //...
  711. }
  712. // Destructor of guarded_ptr releases internal HP guard
  713. }
  714. \endcode
  715. Note the compare functor specified for split-list set
  716. should accept a parameter of type \p Q that can be not the same as \p value_type.
  717. */
  718. template <typename Q>
  719. guarded_ptr get( Q const& key )
  720. {
  721. return get_( key );
  722. }
  723. /// Finds \p key and return the item found
  724. /**
  725. The function is an analog of \ref cds_nonintrusive_SplitListSet_hp_get "get( Q const&)"
  726. but \p pred is used for comparing the keys.
  727. \p Less functor has the semantics like \p std::less but should take arguments of type \ref value_type and \p Q
  728. in any order.
  729. \p pred must imply the same element order as the comparator used for building the set.
  730. */
  731. template <typename Q, typename Less>
  732. guarded_ptr get_with( Q const& key, Less pred )
  733. {
  734. return get_with_( key, pred );
  735. }
  736. /// Clears the set (not atomic)
  737. void clear()
  738. {
  739. base_class::clear();
  740. }
  741. /// Checks if the set is empty
  742. /**
  743. Emptiness is checked by item counting: if item count is zero then assume that the set is empty.
  744. Thus, the correct item counting feature is an important part of split-list set implementation.
  745. */
  746. bool empty() const
  747. {
  748. return base_class::empty();
  749. }
  750. /// Returns item count in the set
  751. size_t size() const
  752. {
  753. return base_class::size();
  754. }
  755. /// Returns internal statistics
  756. stat const& statistics() const
  757. {
  758. return base_class::statistics();
  759. }
  760. /// Returns internal statistics for \p ordered_list
  761. typename ordered_list::stat const& list_statistics() const
  762. {
  763. return base_class::list_statistics();
  764. }
  765. protected:
  766. //@cond
  767. using base_class::extract_;
  768. using base_class::get_;
  769. template <typename... Args>
  770. static node_type * alloc_node( Args&&... args )
  771. {
  772. return cxx_node_allocator().MoveNew( std::forward<Args>( args )... );
  773. }
  774. static void free_node( node_type * pNode )
  775. {
  776. cxx_node_allocator().Delete( pNode );
  777. }
  778. template <typename Q, typename Func>
  779. bool find_( Q& val, Func f )
  780. {
  781. return base_class::find( val, [&f]( node_type& item, Q& v ) { f( item.m_Value, v ); } );
  782. }
  783. template <typename Q>
  784. typename std::enable_if< std::is_same<Q,Q>::value && is_iterable_list< ordered_list >::value, iterator>::type
  785. find_iterator_( Q& val )
  786. {
  787. return iterator( base_class::find( val ));
  788. }
  789. template <typename Q, typename Less, typename Func>
  790. bool find_with_( Q& val, Less pred, Func f )
  791. {
  792. CDS_UNUSED( pred );
  793. return base_class::find_with( val, typename maker::template predicate_wrapper<Less>(),
  794. [&f]( node_type& item, Q& v ) { f( item.m_Value, v ); } );
  795. }
  796. template <typename Q, typename Less>
  797. typename std::enable_if< std::is_same<Q, Q>::value && is_iterable_list< ordered_list >::value, iterator>::type
  798. find_iterator_with_( Q& val, Less pred )
  799. {
  800. CDS_UNUSED( pred );
  801. return iterator( base_class::find_with( val, typename maker::template predicate_wrapper<Less>()));
  802. }
  803. struct node_disposer {
  804. void operator()( node_type * pNode )
  805. {
  806. free_node( pNode );
  807. }
  808. };
  809. typedef std::unique_ptr< node_type, node_disposer > scoped_node_ptr;
  810. bool insert_node( node_type * pNode )
  811. {
  812. assert( pNode != nullptr );
  813. scoped_node_ptr p( pNode );
  814. if ( base_class::insert( *pNode )) {
  815. p.release();
  816. return true;
  817. }
  818. return false;
  819. }
  820. template <typename Q, typename Less>
  821. guarded_ptr extract_with_( Q const& key, Less pred )
  822. {
  823. CDS_UNUSED( pred );
  824. return base_class::extract_with_( key, typename maker::template predicate_wrapper<Less>());
  825. }
  826. template <typename Q, typename Less>
  827. guarded_ptr get_with_( Q const& key, Less pred )
  828. {
  829. CDS_UNUSED( pred );
  830. return base_class::get_with_( key, typename maker::template predicate_wrapper<Less>());
  831. }
  832. //@endcond
  833. };
  834. }} // namespace cds::container
  835. #endif // #ifndef CDSLIB_CONTAINER_SPLIT_LIST_SET_H