split_list_map.h 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782
  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_MAP_H
  6. #define CDSLIB_CONTAINER_SPLIT_LIST_MAP_H
  7. #include <cds/container/split_list_set.h>
  8. #include <cds/details/binary_functor_wrapper.h>
  9. namespace cds { namespace container {
  10. /// Split-ordered list map
  11. /** @ingroup cds_nonintrusive_map
  12. \anchor cds_nonintrusive_SplitListMap_hp
  13. Hash table implementation based on split-ordered list algorithm discovered by Ori Shalev and Nir Shavit, see
  14. - [2003] Ori Shalev, Nir Shavit "Split-Ordered Lists - Lock-free Resizable Hash Tables"
  15. - [2008] Nir Shavit "The Art of Multiprocessor Programming"
  16. See intrusive::SplitListSet for a brief description of the split-list algorithm.
  17. Template parameters:
  18. - \p GC - Garbage collector used like \p cds::gc::HP or \p cds::gc::DHP
  19. - \p Key - key type of an item stored in the map. It should be copy-constructible
  20. - \p Value - value type stored in the map
  21. - \p Traits - map 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_map_rcu.h</tt>,
  25. see \ref cds_nonintrusive_SplitListMap_rcu "SplitListMap<RCU>".
  26. - for \ref cds::gc::nogc declared in <tt>cds/container/split_list_map_nogc.h</tt>,
  27. see \ref cds_nonintrusive_SplitListMap_nogc "SplitListMap<gc::nogc>".
  28. \par Usage
  29. You should decide what garbage collector you want, and what ordered list you want to use. Split-ordered list
  30. is original data structure based on an ordered list. Suppose, you want construct split-list map based on \p gc::HP GC
  31. and \p MichaelList as ordered list implementation. Your map should map \p int key to \p std::string value.
  32. So, you beginning your code with the following:
  33. \code
  34. #include <cds/container/michael_list_hp.h>
  35. #include <cds/container/split_list_map.h>
  36. namespace cc = cds::container;
  37. \endcode
  38. The inclusion order is important: first, include file for ordered-list implementation (for this example, <tt>cds/container/michael_list_hp.h</tt>),
  39. then the header for split-list map <tt>cds/container/split_list_map.h</tt>.
  40. Now, you should declare traits for split-list map. The main parts of traits are a hash functor and a comparing functor for the ordered list.
  41. We use <tt>std::hash<int></tt> as hash functor and <tt>std::less<int></tt> predicate as comparing functor.
  42. The second attention: instead of using \p %MichaelList in \p %SplitListMap traits we use a tag \p cds::contaner::michael_list_tag for the Michael's list.
  43. The split-list requires significant support from underlying ordered list class and it is not good idea to dive you
  44. into deep implementation details of split-list and ordered list interrelations. The tag paradigm simplifies split-list interface.
  45. \code
  46. // SplitListMap traits
  47. struct foo_set_traits: public cc::split_list::traits
  48. {
  49. typedef cc::michael_list_tag ordered_list ; // what type of ordered list we want to use
  50. typedef std::hash<int> hash ; // hash functor for the key stored in split-list map
  51. // Type traits for our MichaelList class
  52. struct ordered_list_traits: public cc::michael_list::traits
  53. {
  54. typedef std::less<int> less ; // use our std::less predicate as comparator to order list nodes
  55. };
  56. };
  57. \endcode
  58. Now you are ready to declare our map class based on \p %SplitListMap:
  59. \code
  60. typedef cc::SplitListMap< cds::gc::DHP, int, std::string, foo_set_traits > int_string_map;
  61. \endcode
  62. You may use the modern option-based declaration instead of classic type-traits-based one:
  63. \code
  64. typedef cc::SplitListMap<
  65. cs::gc::DHP // GC used
  66. ,int // key type
  67. ,std::string // value type
  68. ,cc::split_list::make_traits< // metafunction to build split-list traits
  69. cc::split_list::ordered_list<cc::michael_list_tag> // tag for underlying ordered list implementation
  70. ,cc::opt::hash< std::hash<int> > // hash functor
  71. ,cc::split_list::ordered_list_traits< // ordered list traits desired
  72. cc::michael_list::make_traits< // metafunction to build lazy list traits
  73. cc::opt::less< std::less<int> > // less-based compare functor
  74. >::type
  75. >
  76. >::type
  77. > int_string_map;
  78. \endcode
  79. In case of option-based declaration with \p split_list::make_traits metafunction the struct \p foo_set_traits is not required.
  80. Now, the map of type \p int_string_map is ready to use in your program.
  81. Note that in this example we show only mandatory \p traits parts, optional ones is the default and they are inherited
  82. from \p container::split_list::traits. There are many other options for deep tuning of the split-list and
  83. ordered-list containers.
  84. */
  85. template <
  86. class GC,
  87. typename Key,
  88. typename Value,
  89. #ifdef CDS_DOXYGEN_INVOKED
  90. class Traits = split_list::traits
  91. #else
  92. class Traits
  93. #endif
  94. >
  95. class SplitListMap:
  96. protected container::SplitListSet<
  97. GC,
  98. std::pair<Key const, Value>,
  99. split_list::details::wrap_map_traits<Key, Value, Traits>
  100. >
  101. {
  102. //@cond
  103. typedef container::SplitListSet<
  104. GC,
  105. std::pair<Key const, Value>,
  106. split_list::details::wrap_map_traits<Key, Value, Traits>
  107. > base_class;
  108. //@endcond
  109. public:
  110. typedef GC gc; ///< Garbage collector
  111. typedef Key key_type; ///< key type
  112. typedef Value mapped_type; ///< type of value to be stored in the map
  113. typedef Traits traits; ///< Map traits
  114. typedef std::pair<key_type const, mapped_type> value_type ; ///< key-value pair type
  115. typedef typename base_class::ordered_list ordered_list; ///< Underlying ordered list class
  116. typedef typename base_class::key_comparator key_comparator; ///< key compare functor
  117. typedef typename base_class::hash hash; ///< Hash functor for \ref key_type
  118. typedef typename base_class::item_counter item_counter; ///< Item counter type
  119. typedef typename base_class::stat stat; ///< Internal statistics
  120. /// Count of hazard pointer required
  121. static constexpr const size_t c_nHazardPtrCount = base_class::c_nHazardPtrCount;
  122. protected:
  123. //@cond
  124. typedef typename base_class::maker::traits::key_accessor key_accessor;
  125. typedef typename base_class::node_type node_type;
  126. //@endcond
  127. public:
  128. /// Guarded pointer
  129. typedef typename gc::template guarded_ptr< node_type, value_type, details::guarded_ptr_cast_set<node_type, value_type> > guarded_ptr;
  130. public:
  131. ///@name Forward iterators (only for debugging purpose)
  132. //@{
  133. /// Forward iterator
  134. /**
  135. The forward iterator for a split-list has the following features:
  136. - it has no post-increment operator
  137. - it depends on underlying ordered list iterator
  138. - The iterator object cannot be moved across thread boundary because it contains GC's guard that is thread-private GC data.
  139. - Iterator ensures thread-safety even if you delete the item that iterator points to. However, in case of concurrent
  140. deleting operations it is no guarantee that you iterate all item in the split-list.
  141. Moreover, a crash is possible when you try to iterate the next element that has been deleted by concurrent thread.
  142. @warning Use this iterator on the concurrent container for debugging purpose only.
  143. The iterator interface:
  144. \code
  145. class iterator {
  146. public:
  147. // Default constructor
  148. iterator();
  149. // Copy construtor
  150. iterator( iterator const& src );
  151. // Dereference operator
  152. value_type * operator ->() const;
  153. // Dereference operator
  154. value_type& operator *() const;
  155. // Preincrement operator
  156. iterator& operator ++();
  157. // Assignment operator
  158. iterator& operator = (iterator const& src);
  159. // Equality operators
  160. bool operator ==(iterator const& i ) const;
  161. bool operator !=(iterator const& i ) const;
  162. };
  163. \endcode
  164. */
  165. typedef typename base_class::iterator iterator;
  166. /// Const forward iterator
  167. typedef typename base_class::const_iterator const_iterator;
  168. /// Returns a forward iterator addressing the first element in a map
  169. /**
  170. For empty map \code begin() == end() \endcode
  171. */
  172. iterator begin()
  173. {
  174. return base_class::begin();
  175. }
  176. /// Returns an iterator that addresses the location succeeding the last element in a map
  177. /**
  178. Do not use the value returned by <tt>end</tt> function to access any item.
  179. The returned value can be used only to control reaching the end of the map.
  180. For empty map \code begin() == end() \endcode
  181. */
  182. iterator end()
  183. {
  184. return base_class::end();
  185. }
  186. /// Returns a forward const iterator addressing the first element in a map
  187. const_iterator begin() const
  188. {
  189. return base_class::begin();
  190. }
  191. /// Returns a forward const iterator addressing the first element in a map
  192. const_iterator cbegin() const
  193. {
  194. return base_class::cbegin();
  195. }
  196. /// Returns an const iterator that addresses the location succeeding the last element in a map
  197. const_iterator end() const
  198. {
  199. return base_class::end();
  200. }
  201. /// Returns an const iterator that addresses the location succeeding the last element in a map
  202. const_iterator cend() const
  203. {
  204. return base_class::cend();
  205. }
  206. //@}
  207. public:
  208. /// Initializes split-ordered map of default capacity
  209. /**
  210. The default capacity is defined in bucket table constructor.
  211. See \p intrusive::split_list::expandable_bucket_table, \p intrusive::split_list::static_bucket_table
  212. which selects by \p intrusive::split_list::traits::dynamic_bucket_table.
  213. */
  214. SplitListMap()
  215. : base_class()
  216. {}
  217. /// Initializes split-ordered map
  218. SplitListMap(
  219. size_t nItemCount ///< estimated average item count
  220. , size_t nLoadFactor = 1 ///< load factor - average item count per bucket. Small integer up to 10, default is 1.
  221. )
  222. : base_class( nItemCount, nLoadFactor )
  223. {}
  224. public:
  225. /// Inserts new node with key and default value
  226. /**
  227. The function creates a node with \p key and default value, and then inserts the node created into the map.
  228. Preconditions:
  229. - The \ref key_type should be constructible from value of type \p K.
  230. In trivial case, \p K is equal to \ref key_type.
  231. - The \ref mapped_type should be default-constructible.
  232. Returns \p true if inserting successful, \p false otherwise.
  233. */
  234. template <typename K>
  235. bool insert( K&& key )
  236. {
  237. return base_class::emplace( key_type( std::forward<K>( key )), mapped_type());
  238. }
  239. /// Inserts new node
  240. /**
  241. The function creates a node with copy of \p val value
  242. and then inserts the node created into the map.
  243. Preconditions:
  244. - The \ref key_type should be constructible from \p key of type \p K.
  245. - The \ref mapped_type should be constructible from \p val of type \p V.
  246. Returns \p true if \p val is inserted into the map, \p false otherwise.
  247. */
  248. template <typename K, typename V>
  249. bool insert( K&& key, V&& val )
  250. {
  251. return base_class::emplace( key_type( std::forward<K>( key )), mapped_type( std::forward<V>( val )));
  252. }
  253. /// Inserts new node and initialize it by a functor
  254. /**
  255. This function inserts new node with key \p key and if inserting is successful then it calls
  256. \p func functor with signature
  257. \code
  258. struct functor {
  259. void operator()( value_type& item );
  260. };
  261. \endcode
  262. The argument \p item of user-defined functor \p func is the reference
  263. to the map's item inserted:
  264. - <tt>item.first</tt> is a const reference to item's key that cannot be changed.
  265. - <tt>item.second</tt> is a reference to item's value that may be changed.
  266. It should be keep in mind that concurrent modifications of \p <tt>item.second</tt> may be possible.
  267. The key_type should be constructible from value of type \p K.
  268. The function allows to split creating of new item into two part:
  269. - create item from \p key;
  270. - insert new item into the map;
  271. - if inserting is successful, initialize the value of item by calling \p func functor
  272. This can be useful if complete initialization of object of \p mapped_type is heavyweight and
  273. it is preferable that the initialization should be completed only if inserting is successful.
  274. @warning For \ref cds_nonintrusive_MichaelKVList_gc "MichaelKVList" as the bucket see \ref cds_intrusive_item_creating "insert item troubleshooting".
  275. \ref cds_nonintrusive_LazyKVList_gc "LazyKVList" provides exclusive access to inserted item and does not require any node-level
  276. synchronization.
  277. */
  278. template <typename K, typename Func>
  279. bool insert_with( K&& key, Func func )
  280. {
  281. //TODO: pass arguments by reference (make_pair makes copy)
  282. return base_class::insert( std::make_pair( key_type( std::forward<K>( key )), mapped_type()), func );
  283. }
  284. /// For key \p key inserts data of type \p mapped_type created from \p args
  285. /**
  286. \p key_type should be constructible from type \p K
  287. Returns \p true if inserting successful, \p false otherwise.
  288. */
  289. template <typename K, typename... Args>
  290. bool emplace( K&& key, Args&&... args )
  291. {
  292. return base_class::emplace( key_type( std::forward<K>(key)), mapped_type( std::forward<Args>(args)...));
  293. }
  294. /// Updates the node
  295. /**
  296. The operation performs inserting or changing data with lock-free manner.
  297. If \p key is not found in the map, then \p key is inserted iff \p bAllowInsert is \p true.
  298. Otherwise, the functor \p func is called with item found.
  299. The functor \p func signature depends on ordered list:
  300. <b>for \p MichaelKVList, \p LazyKVList</b>
  301. \code
  302. struct my_functor {
  303. void operator()( bool bNew, value_type& item );
  304. };
  305. \endcode
  306. with arguments:
  307. - \p bNew - \p true if the item has been inserted, \p false otherwise
  308. - \p item - the item found or inserted
  309. The functor may change any fields of the \p item.second that is \p mapped_type.
  310. <b>for \p IterableKVList</b>
  311. \code
  312. void func( value_type& val, value_type * old );
  313. \endcode
  314. where
  315. - \p val - a new data constructed from \p key
  316. - \p old - old value that will be retired. If new item has been inserted then \p old is \p nullptr.
  317. Returns <tt> std::pair<bool, bool> </tt> where \p first is true if operation is successful,
  318. \p second is true if new item has been added or \p false if the item with \p key
  319. already is in the map.
  320. @warning For \ref cds_nonintrusive_MichaelKVList_gc "MichaelKVList" and \ref cds_nonintrusive_IterableKVList_gc "IterableKVList"
  321. as the ordered list see \ref cds_intrusive_item_creating "insert item troubleshooting".
  322. \ref cds_nonintrusive_LazyKVList_gc "LazyKVList" provides exclusive access to inserted item and does not require any node-level
  323. synchronization.
  324. */
  325. template <typename K, typename Func>
  326. #ifdef CDS_DOXYGE_INVOKED
  327. std::pair<bool, bool>
  328. #else
  329. typename std::enable_if<
  330. std::is_same<K,K>::value && !is_iterable_list< ordered_list >::value,
  331. std::pair<bool, bool>
  332. >::type
  333. #endif
  334. update( K&& key, Func func, bool bAllowInsert = true )
  335. {
  336. typedef decltype( std::make_pair( key_type( std::forward<K>( key )), mapped_type())) arg_pair_type;
  337. return base_class::update( std::make_pair( key_type( key ), mapped_type()),
  338. [&func]( bool bNew, value_type& item, arg_pair_type const& /*val*/ ) {
  339. func( bNew, item );
  340. },
  341. bAllowInsert );
  342. }
  343. //@cond
  344. template <typename K, typename Func>
  345. #ifdef CDS_DOXYGE_INVOKED
  346. std::pair<bool, bool>
  347. #else
  348. typename std::enable_if<
  349. std::is_same<K, K>::value && is_iterable_list< ordered_list >::value,
  350. std::pair<bool, bool>
  351. >::type
  352. #endif
  353. update( K&& key, Func func, bool bAllowInsert = true )
  354. {
  355. return base_class::update( std::make_pair( key_type( std::forward<K>( key )), mapped_type()), func, bAllowInsert );
  356. }
  357. //@endcond
  358. //@cond
  359. template <typename K, typename Func>
  360. CDS_DEPRECATED("ensure() is deprecated, use update()")
  361. std::pair<bool, bool> ensure( K const& key, Func func )
  362. {
  363. return update( key, func, true );
  364. }
  365. //@endcond
  366. /// Inserts or updates the node (only for \p IterableKVList)
  367. /**
  368. The operation performs inserting or changing data with lock-free manner.
  369. If \p key is not found in the map, then \p key is inserted iff \p bAllowInsert is \p true.
  370. Otherwise, the current element is changed to \p val, the old element will be retired later.
  371. Returns std::pair<bool, bool> where \p first is \p true if operation is successful,
  372. \p second is \p true if \p val has been added or \p false if the item with that key
  373. already in the map.
  374. */
  375. template <typename Q, typename V>
  376. #ifdef CDS_DOXYGEN_INVOKED
  377. std::pair<bool, bool>
  378. #else
  379. typename std::enable_if<
  380. std::is_same< Q, Q>::value && is_iterable_list< ordered_list >::value,
  381. std::pair<bool, bool>
  382. >::type
  383. #endif
  384. upsert( Q&& key, V&& val, bool bAllowInsert = true )
  385. {
  386. return base_class::upsert( std::make_pair( key_type( std::forward<Q>( key )), mapped_type( std::forward<V>( val ))), bAllowInsert );
  387. }
  388. /// Deletes \p key from the map
  389. /** \anchor cds_nonintrusive_SplitListMap_erase_val
  390. Return \p true if \p key is found and deleted, \p false otherwise
  391. */
  392. template <typename K>
  393. bool erase( K const& key )
  394. {
  395. return base_class::erase( key );
  396. }
  397. /// Deletes the item from the map using \p pred predicate for searching
  398. /**
  399. The function is an analog of \ref cds_nonintrusive_SplitListMap_erase_val "erase(K const&)"
  400. but \p pred is used for key comparing.
  401. \p Less functor has the interface like \p std::less.
  402. \p Less must imply the same element order as the comparator used for building the map.
  403. */
  404. template <typename K, typename Less>
  405. bool erase_with( K const& key, Less pred )
  406. {
  407. CDS_UNUSED( pred );
  408. return base_class::erase_with( key, cds::details::predicate_wrapper<value_type, Less, key_accessor>());
  409. }
  410. /// Deletes \p key from the map
  411. /** \anchor cds_nonintrusive_SplitListMap_erase_func
  412. The function searches an item with key \p key, calls \p f functor
  413. and deletes the item. If \p key is not found, the functor is not called.
  414. The functor \p Func interface is:
  415. \code
  416. struct extractor {
  417. void operator()(value_type& item) { ... }
  418. };
  419. \endcode
  420. Return \p true if key is found and deleted, \p false otherwise
  421. */
  422. template <typename K, typename Func>
  423. bool erase( K const& key, Func f )
  424. {
  425. return base_class::erase( key, f );
  426. }
  427. /// Deletes the item from the map using \p pred predicate for searching
  428. /**
  429. The function is an analog of \ref cds_nonintrusive_SplitListMap_erase_func "erase(K const&, Func)"
  430. but \p pred is used for key comparing.
  431. \p Less functor has the interface like \p std::less.
  432. \p Less must imply the same element order as the comparator used for building the map.
  433. */
  434. template <typename K, typename Less, typename Func>
  435. bool erase_with( K const& key, Less pred, Func f )
  436. {
  437. CDS_UNUSED( pred );
  438. return base_class::erase_with( key, cds::details::predicate_wrapper<value_type, Less, key_accessor>(), f );
  439. }
  440. /// Deletes the item pointed by iterator \p iter (only for \p IterableList based map)
  441. /**
  442. Returns \p true if the operation is successful, \p false otherwise.
  443. The function can return \p false if the node the iterator points to has already been deleted
  444. by other thread.
  445. The function does not invalidate the iterator, it remains valid and can be used for further traversing.
  446. @note \p %erase_at() is supported only for \p %SplitListMap based on \p IterableList.
  447. */
  448. #ifdef CDS_DOXYGEN_INVOKED
  449. bool erase_at( iterator const& iter )
  450. #else
  451. template <typename Iterator>
  452. typename std::enable_if< std::is_same<Iterator, iterator>::value && is_iterable_list< ordered_list >::value, bool >::type
  453. erase_at( Iterator const& iter )
  454. #endif
  455. {
  456. return base_class::erase_at( iter );
  457. }
  458. /// Extracts the item with specified \p key
  459. /** \anchor cds_nonintrusive_SplitListMap_hp_extract
  460. The function searches an item with key equal to \p key,
  461. unlinks it from the map, and returns it as \p guarded_ptr.
  462. If \p key is not found the function returns an empty guarded pointer.
  463. Note the compare functor should accept a parameter of type \p K that may be not the same as \p value_type.
  464. The extracted item is freed automatically when returned \p guarded_ptr object will be destroyed or released.
  465. @note Each \p guarded_ptr object uses the GC's guard that can be limited resource.
  466. Usage:
  467. \code
  468. typedef cds::container::SplitListMap< your_template_args > splitlist_map;
  469. splitlist_map theMap;
  470. // ...
  471. {
  472. splitlist_map::guarded_ptr gp(theMap.extract( 5 ));
  473. if ( gp ) {
  474. // Deal with gp
  475. // ...
  476. }
  477. // Destructor of gp releases internal HP guard
  478. }
  479. \endcode
  480. */
  481. template <typename K>
  482. guarded_ptr extract( K const& key )
  483. {
  484. return base_class::extract_( key );
  485. }
  486. /// Extracts the item using compare functor \p pred
  487. /**
  488. The function is an analog of \ref cds_nonintrusive_SplitListMap_hp_extract "extract(K const&)"
  489. but \p pred predicate is used for key comparing.
  490. \p Less functor has the semantics like \p std::less but should take arguments of type \ref value_type and \p K
  491. in any order.
  492. \p pred must imply the same element order as the comparator used for building the map.
  493. */
  494. template <typename K, typename Less>
  495. guarded_ptr extract_with( K const& key, Less pred )
  496. {
  497. CDS_UNUSED( pred );
  498. return base_class::extract_with_( key, cds::details::predicate_wrapper<value_type, Less, key_accessor>());
  499. }
  500. /// Finds the key \p key
  501. /** \anchor cds_nonintrusive_SplitListMap_find_cfunc
  502. The function searches the item with key equal to \p key and calls the functor \p f for item found.
  503. The interface of \p Func functor is:
  504. \code
  505. struct functor {
  506. void operator()( value_type& item );
  507. };
  508. \endcode
  509. where \p item is the item found.
  510. The functor may change \p item.second. Note that the functor is only guarantee
  511. that \p item cannot be disposed during functor is executing.
  512. The functor does not serialize simultaneous access to the map's \p item. If such access is
  513. possible you must provide your own synchronization schema on item level to exclude unsafe item modifications.
  514. The function returns \p true if \p key is found, \p false otherwise.
  515. */
  516. template <typename K, typename Func>
  517. bool find( K const& key, Func f )
  518. {
  519. return base_class::find( key, [&f](value_type& pair, K const&){ f( pair ); } );
  520. }
  521. /// Finds \p key and returns iterator pointed to the item found (only for \p IterableList)
  522. /**
  523. If \p key is not found the function returns \p end().
  524. @note This function is supported only for map based on \p IterableList
  525. */
  526. template <typename K>
  527. #ifdef CDS_DOXYGEN_INVOKED
  528. iterator
  529. #else
  530. typename std::enable_if< std::is_same<K,K>::value && is_iterable_list<ordered_list>::value, iterator >::type
  531. #endif
  532. find( K const& key )
  533. {
  534. return base_class::find( key );
  535. }
  536. /// Finds the key \p val using \p pred predicate for searching
  537. /**
  538. The function is an analog of \ref cds_nonintrusive_SplitListMap_find_cfunc "find(K const&, Func)"
  539. but \p pred is used for key comparing.
  540. \p Less functor has the interface like \p std::less.
  541. \p Less must imply the same element order as the comparator used for building the map.
  542. */
  543. template <typename K, typename Less, typename Func>
  544. bool find_with( K const& key, Less pred, Func f )
  545. {
  546. CDS_UNUSED( pred );
  547. return base_class::find_with( key,
  548. cds::details::predicate_wrapper<value_type, Less, key_accessor>(),
  549. [&f](value_type& pair, K const&){ f( pair ); } );
  550. }
  551. /// Finds \p key using \p pred predicate and returns iterator pointed to the item found (only for \p IterableList)
  552. /**
  553. The function is an analog of \p find(K&) but \p pred is used for key comparing.
  554. \p Less functor has interface like \p std::less.
  555. \p pred must imply the same element order as the comparator used for building the map.
  556. If \p key is not found the function returns \p end().
  557. @note This function is supported only for map based on \p IterableList
  558. */
  559. template <typename K, typename Less>
  560. #ifdef CDS_DOXYGEN_INVOKED
  561. iterator
  562. #else
  563. typename std::enable_if< std::is_same<K, K>::value && is_iterable_list< ordered_list >::value, iterator >::type
  564. #endif
  565. find_with( K const& key, Less pred )
  566. {
  567. CDS_UNUSED( pred );
  568. return base_class::find_with( key, cds::details::predicate_wrapper<value_type, Less, key_accessor>());
  569. }
  570. /// Checks whether the map contains \p key
  571. /**
  572. The function searches the item with key equal to \p key
  573. and returns \p true if it is found, and \p false otherwise.
  574. Note the hash functor specified for class \p Traits template parameter
  575. should accept a parameter of type \p Q that can be not the same as \p value_type.
  576. Otherwise, you may use \p contains( Q const&, Less pred ) functions with explicit predicate for key comparing.
  577. */
  578. template <typename K>
  579. bool contains( K const& key )
  580. {
  581. return base_class::contains( key );
  582. }
  583. /// Checks whether the map contains \p key using \p pred predicate for searching
  584. /**
  585. The function is similar to <tt>contains( key )</tt> but \p pred is used for key comparing.
  586. \p Less functor has the interface like \p std::less.
  587. \p Less must imply the same element order as the comparator used for building the map.
  588. */
  589. template <typename K, typename Less>
  590. bool contains( K const& key, Less pred )
  591. {
  592. CDS_UNUSED( pred );
  593. return base_class::contains( key, cds::details::predicate_wrapper<value_type, Less, key_accessor>());
  594. }
  595. /// Finds \p key and return the item found
  596. /** \anchor cds_nonintrusive_SplitListMap_hp_get
  597. The function searches the item with key equal to \p key
  598. and returns the item found as a guarded pointer.
  599. If \p key is not found the function returns an empty guarded pointer.
  600. @note Each \p guarded_ptr object uses one GC's guard which can be limited resource.
  601. Usage:
  602. \code
  603. typedef cds::container::SplitListMap< your_template_params > splitlist_map;
  604. splitlist_map theMap;
  605. // ...
  606. {
  607. splitlist_map::guarded_ptr gp(theMap.get( 5 ));
  608. if ( gp ) {
  609. // Deal with gp
  610. //...
  611. }
  612. // Destructor of guarded_ptr releases internal HP guard
  613. }
  614. \endcode
  615. Note the compare functor specified for split-list map
  616. should accept a parameter of type \p K that can be not the same as \p value_type.
  617. */
  618. template <typename K>
  619. guarded_ptr get( K const& key )
  620. {
  621. return base_class::get_( key );
  622. }
  623. /// Finds \p key and return the item found
  624. /**
  625. The function is an analog of \ref cds_nonintrusive_SplitListMap_hp_get "get( K const&)"
  626. but \p pred is used for comparing the keys.
  627. \p Less functor has the semantics like \p std::less but should take arguments of type \ref value_type and \p K
  628. in any order.
  629. \p pred must imply the same element order as the comparator used for building the map.
  630. */
  631. template <typename K, typename Less>
  632. guarded_ptr get_with( K const& key, Less pred )
  633. {
  634. CDS_UNUSED( pred );
  635. return base_class::get_with_( key, cds::details::predicate_wrapper<value_type, Less, key_accessor>());
  636. }
  637. /// Clears the map (not atomic)
  638. void clear()
  639. {
  640. base_class::clear();
  641. }
  642. /// Checks if the map is empty
  643. /**
  644. Emptiness is checked by item counting: if item count is zero then the map is empty.
  645. Thus, the correct item counting is an important part of the map implementation.
  646. */
  647. bool empty() const
  648. {
  649. return base_class::empty();
  650. }
  651. /// Returns item count in the map
  652. size_t size() const
  653. {
  654. return base_class::size();
  655. }
  656. /// Returns internal statistics
  657. stat const& statistics() const
  658. {
  659. return base_class::statistics();
  660. }
  661. /// Returns internal statistics for \p ordered_list
  662. typename ordered_list::stat const& list_statistics() const
  663. {
  664. return base_class::list_statistics();
  665. }
  666. };
  667. }} // namespace cds::container
  668. #endif // #ifndef CDSLIB_CONTAINER_SPLIT_LIST_MAP_H