multiset.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  1. // Profiling multiset implementation -*- C++ -*-
  2. // Copyright (C) 2009-2015 Free Software Foundation, Inc.
  3. //
  4. // This file is part of the GNU ISO C++ Library. This library is free
  5. // software; you can redistribute it and/or modify it under the
  6. // terms of the GNU General Public License as published by the
  7. // Free Software Foundation; either version 3, or (at your option)
  8. // any later version.
  9. // This library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // Under Section 7 of GPL version 3, you are granted additional
  14. // permissions described in the GCC Runtime Library Exception, version
  15. // 3.1, as published by the Free Software Foundation.
  16. // You should have received a copy of the GNU General Public License and
  17. // a copy of the GCC Runtime Library Exception along with this program;
  18. // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. // <http://www.gnu.org/licenses/>.
  20. /** @file profile/multiset.h
  21. * This file is a GNU profile extension to the Standard C++ Library.
  22. */
  23. #ifndef _GLIBCXX_PROFILE_MULTISET_H
  24. #define _GLIBCXX_PROFILE_MULTISET_H 1
  25. #include <profile/base.h>
  26. #include <profile/ordered_base.h>
  27. namespace std _GLIBCXX_VISIBILITY(default)
  28. {
  29. namespace __profile
  30. {
  31. /// Class std::multiset wrapper with performance instrumentation.
  32. template<typename _Key, typename _Compare = std::less<_Key>,
  33. typename _Allocator = std::allocator<_Key> >
  34. class multiset
  35. : public _GLIBCXX_STD_C::multiset<_Key, _Compare, _Allocator>,
  36. public _Ordered_profile<multiset<_Key, _Compare, _Allocator> >
  37. {
  38. typedef _GLIBCXX_STD_C::multiset<_Key, _Compare, _Allocator> _Base;
  39. typedef typename _Base::iterator _Base_iterator;
  40. typedef typename _Base::const_iterator _Base_const_iterator;
  41. public:
  42. // types:
  43. typedef _Key key_type;
  44. typedef _Key value_type;
  45. typedef _Compare key_compare;
  46. typedef _Compare value_compare;
  47. typedef _Allocator allocator_type;
  48. typedef typename _Base::reference reference;
  49. typedef typename _Base::const_reference const_reference;
  50. typedef __iterator_tracker<_Base_iterator,
  51. multiset> iterator;
  52. typedef __iterator_tracker<_Base_const_iterator,
  53. multiset> const_iterator;
  54. typedef std::reverse_iterator<iterator> reverse_iterator;
  55. typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
  56. typedef typename _Base::size_type size_type;
  57. typedef typename _Base::difference_type difference_type;
  58. // 23.3.3.1 construct/copy/destroy:
  59. #if __cplusplus < 201103L
  60. multiset()
  61. : _Base() { }
  62. multiset(const multiset& __x)
  63. : _Base(__x) { }
  64. ~multiset() { }
  65. #else
  66. multiset() = default;
  67. multiset(const multiset&) = default;
  68. multiset(multiset&&) = default;
  69. ~multiset() = default;
  70. #endif
  71. explicit multiset(const _Compare& __comp,
  72. const _Allocator& __a = _Allocator())
  73. : _Base(__comp, __a) { }
  74. #if __cplusplus >= 201103L
  75. template<typename _InputIterator,
  76. typename = std::_RequireInputIter<_InputIterator>>
  77. #else
  78. template<typename _InputIterator>
  79. #endif
  80. multiset(_InputIterator __first, _InputIterator __last,
  81. const _Compare& __comp = _Compare(),
  82. const _Allocator& __a = _Allocator())
  83. : _Base(__first, __last, __comp, __a) { }
  84. #if __cplusplus >= 201103L
  85. multiset(initializer_list<value_type> __l,
  86. const _Compare& __comp = _Compare(),
  87. const allocator_type& __a = allocator_type())
  88. : _Base(__l, __comp, __a) { }
  89. explicit
  90. multiset(const allocator_type& __a)
  91. : _Base(__a) { }
  92. multiset(const multiset& __x, const allocator_type& __a)
  93. : _Base(__x, __a) { }
  94. multiset(multiset&& __x, const allocator_type& __a)
  95. noexcept( noexcept(_Base(std::move(__x), __a)) )
  96. : _Base(std::move(__x), __a) { }
  97. multiset(initializer_list<value_type> __l, const allocator_type& __a)
  98. : _Base(__l, __a) { }
  99. template<typename _InputIterator>
  100. multiset(_InputIterator __first, _InputIterator __last,
  101. const allocator_type& __a)
  102. : _Base(__first, __last, __a) { }
  103. #endif
  104. multiset(const _Base& __x)
  105. : _Base(__x) { }
  106. #if __cplusplus < 201103L
  107. multiset&
  108. operator=(const multiset& __x)
  109. {
  110. this->_M_profile_destruct();
  111. _M_base() = __x;
  112. this->_M_profile_construct();
  113. return *this;
  114. }
  115. #else
  116. multiset&
  117. operator=(const multiset&) = default;
  118. multiset&
  119. operator=(multiset&&) = default;
  120. multiset&
  121. operator=(initializer_list<value_type> __l)
  122. {
  123. this->_M_profile_destruct();
  124. _M_base() = __l;
  125. this->_M_profile_construct();
  126. return *this;
  127. }
  128. #endif
  129. // iterators
  130. iterator
  131. begin() _GLIBCXX_NOEXCEPT
  132. { return iterator(_Base::begin(), this); }
  133. const_iterator
  134. begin() const _GLIBCXX_NOEXCEPT
  135. { return const_iterator(_Base::begin(), this); }
  136. iterator
  137. end() _GLIBCXX_NOEXCEPT
  138. { return iterator(_Base::end(), this); }
  139. const_iterator
  140. end() const _GLIBCXX_NOEXCEPT
  141. { return const_iterator(_Base::end(), this); }
  142. #if __cplusplus >= 201103L
  143. const_iterator
  144. cbegin() const noexcept
  145. { return const_iterator(_Base::cbegin(), this); }
  146. const_iterator
  147. cend() const noexcept
  148. { return const_iterator(_Base::cend(), this); }
  149. #endif
  150. reverse_iterator
  151. rbegin() _GLIBCXX_NOEXCEPT
  152. {
  153. __profcxx_map2umap_invalidate(this->_M_map2umap_info);
  154. return reverse_iterator(end());
  155. }
  156. const_reverse_iterator
  157. rbegin() const _GLIBCXX_NOEXCEPT
  158. {
  159. __profcxx_map2umap_invalidate(this->_M_map2umap_info);
  160. return const_reverse_iterator(end());
  161. }
  162. reverse_iterator
  163. rend() _GLIBCXX_NOEXCEPT
  164. {
  165. __profcxx_map2umap_invalidate(this->_M_map2umap_info);
  166. return reverse_iterator(begin());
  167. }
  168. const_reverse_iterator
  169. rend() const _GLIBCXX_NOEXCEPT
  170. {
  171. __profcxx_map2umap_invalidate(this->_M_map2umap_info);
  172. return const_reverse_iterator(begin());
  173. }
  174. #if __cplusplus >= 201103L
  175. const_reverse_iterator
  176. crbegin() const noexcept
  177. {
  178. __profcxx_map2umap_invalidate(this->_M_map2umap_info);
  179. return const_reverse_iterator(cend());
  180. }
  181. const_reverse_iterator
  182. crend() const noexcept
  183. {
  184. __profcxx_map2umap_invalidate(this->_M_map2umap_info);
  185. return const_reverse_iterator(cbegin());
  186. }
  187. #endif
  188. void
  189. swap(multiset& __x)
  190. #if __cplusplus >= 201103L
  191. noexcept( noexcept(declval<_Base>().swap(__x)) )
  192. #endif
  193. {
  194. _Base::swap(__x);
  195. this->_M_swap(__x);
  196. }
  197. // modifiers:
  198. #if __cplusplus >= 201103L
  199. template<typename... _Args>
  200. iterator
  201. emplace(_Args&&... __args)
  202. {
  203. // The cost is the same whether or not the element is inserted so we
  204. // always report insertion of 1 element.
  205. __profcxx_map2umap_insert(this->_M_map2umap_info, this->size(), 1);
  206. return iterator(_Base::emplace(std::forward<_Args>(__args)...), this);
  207. }
  208. template<typename... _Args>
  209. iterator
  210. emplace_hint(const_iterator __pos, _Args&&... __args)
  211. {
  212. auto size_before = this->size();
  213. auto __res
  214. = _Base::emplace_hint(__pos.base(), std::forward<_Args>(__args)...);
  215. __profcxx_map2umap_insert(this->_M_map2umap_info,
  216. size_before, _M_hint_used(__pos.base(), __res) ? 0 : 1);
  217. return iterator(__res, this);
  218. }
  219. #endif
  220. iterator
  221. insert(const value_type& __x)
  222. {
  223. __profcxx_map2umap_insert(this->_M_map2umap_info, this->size(), 1);
  224. return iterator(_Base::insert(__x), this);
  225. }
  226. #if __cplusplus >= 201103L
  227. iterator
  228. insert(value_type&& __x)
  229. {
  230. __profcxx_map2umap_insert(this->_M_map2umap_info, this->size(), 1);
  231. return iterator(_Base::insert(std::move(__x)), this);
  232. }
  233. #endif
  234. iterator
  235. insert(const_iterator __pos, const value_type& __x)
  236. {
  237. size_type size_before = this->size();
  238. _Base_iterator __res = _Base::insert(__pos.base(), __x);
  239. __profcxx_map2umap_insert(this->_M_map2umap_info,
  240. size_before, _M_hint_used(__pos.base(), __res) ? 0 : 1);
  241. return iterator(__res, this);
  242. }
  243. #if __cplusplus >= 201103L
  244. iterator
  245. insert(const_iterator __pos, value_type&& __x)
  246. {
  247. auto size_before = this->size();
  248. auto __res = _Base::insert(__pos.base(), std::move(__x));
  249. __profcxx_map2umap_insert(this->_M_map2umap_info,
  250. size_before, _M_hint_used(__pos.base(), __res) ? 0 : 1);
  251. return iterator(__res, this);
  252. }
  253. #endif
  254. #if __cplusplus >= 201103L
  255. template<typename _InputIterator,
  256. typename = std::_RequireInputIter<_InputIterator>>
  257. #else
  258. template<typename _InputIterator>
  259. #endif
  260. void
  261. insert(_InputIterator __first, _InputIterator __last)
  262. {
  263. for (; __first != __last; ++__first)
  264. insert(*__first);
  265. }
  266. #if __cplusplus >= 201103L
  267. void
  268. insert(initializer_list<value_type> __l)
  269. { insert(__l.begin(), __l.end()); }
  270. #endif
  271. #if __cplusplus >= 201103L
  272. iterator
  273. erase(const_iterator __pos)
  274. {
  275. __profcxx_map2umap_erase(this->_M_map2umap_info, this->size(), 1);
  276. return iterator(_Base::erase(__pos.base()), this);
  277. }
  278. #else
  279. void
  280. erase(iterator __pos)
  281. {
  282. __profcxx_map2umap_erase(this->_M_map2umap_info, this->size(), 1);
  283. _Base::erase(__pos.base());
  284. }
  285. #endif
  286. size_type
  287. erase(const key_type& __x)
  288. {
  289. __profcxx_map2umap_find(this->_M_map2umap_info, this->size());
  290. __profcxx_map2umap_erase(this->_M_map2umap_info, this->size(), 1);
  291. return _Base::erase(__x);
  292. }
  293. #if __cplusplus >= 201103L
  294. iterator
  295. erase(const_iterator __first, const_iterator __last)
  296. {
  297. if (__first != __last)
  298. {
  299. iterator __ret;
  300. for (; __first != __last;)
  301. __ret = erase(__first++);
  302. return __ret;
  303. }
  304. else
  305. return iterator(_Base::erase(__first.base(), __last.base()), this);
  306. }
  307. #else
  308. void
  309. erase(iterator __first, iterator __last)
  310. {
  311. for (; __first != __last;)
  312. erase(__first++);
  313. }
  314. #endif
  315. void
  316. clear() _GLIBCXX_NOEXCEPT
  317. {
  318. this->_M_profile_destruct();
  319. _Base::clear();
  320. this->_M_profile_construct();
  321. }
  322. size_type
  323. count(const key_type& __x) const
  324. {
  325. __profcxx_map2umap_find(this->_M_map2umap_info, this->size());
  326. return _Base::count(__x);
  327. }
  328. // multiset operations:
  329. iterator
  330. find(const key_type& __x)
  331. {
  332. __profcxx_map2umap_find(this->_M_map2umap_info, this->size());
  333. return iterator(_Base::find(__x), this);
  334. }
  335. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  336. // 214. set::find() missing const overload
  337. const_iterator
  338. find(const key_type& __x) const
  339. {
  340. __profcxx_map2umap_find(this->_M_map2umap_info, this->size());
  341. return const_iterator(_Base::find(__x), this);
  342. }
  343. iterator
  344. lower_bound(const key_type& __x)
  345. {
  346. __profcxx_map2umap_find(this->_M_map2umap_info, this->size());
  347. return iterator(_Base::lower_bound(__x), this);
  348. }
  349. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  350. // 214. set::find() missing const overload
  351. const_iterator
  352. lower_bound(const key_type& __x) const
  353. {
  354. __profcxx_map2umap_find(this->_M_map2umap_info, this->size());
  355. __profcxx_map2umap_invalidate(this->_M_map2umap_info);
  356. return const_iterator(_Base::lower_bound(__x), this);
  357. }
  358. iterator
  359. upper_bound(const key_type& __x)
  360. {
  361. __profcxx_map2umap_find(this->_M_map2umap_info, this->size());
  362. __profcxx_map2umap_invalidate(this->_M_map2umap_info);
  363. return iterator(_Base::upper_bound(__x), this);
  364. }
  365. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  366. // 214. set::find() missing const overload
  367. const_iterator
  368. upper_bound(const key_type& __x) const
  369. {
  370. __profcxx_map2umap_find(this->_M_map2umap_info, this->size());
  371. __profcxx_map2umap_invalidate(this->_M_map2umap_info);
  372. return const_iterator(_Base::upper_bound(__x), this);
  373. }
  374. std::pair<iterator,iterator>
  375. equal_range(const key_type& __x)
  376. {
  377. __profcxx_map2umap_find(this->_M_map2umap_info, this->size());
  378. std::pair<_Base_iterator, _Base_iterator> __base_ret
  379. = _Base::equal_range(__x);
  380. return std::make_pair(iterator(__base_ret.first, this),
  381. iterator(__base_ret.second, this));
  382. }
  383. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  384. // 214. set::find() missing const overload
  385. std::pair<const_iterator,const_iterator>
  386. equal_range(const key_type& __x) const
  387. {
  388. __profcxx_map2umap_find(this->_M_map2umap_info, this->size());
  389. std::pair<_Base_const_iterator, _Base_const_iterator> __base_ret
  390. = _Base::equal_range(__x);
  391. return std::make_pair(const_iterator(__base_ret.first, this),
  392. const_iterator(__base_ret.second, this));
  393. }
  394. _Base&
  395. _M_base() _GLIBCXX_NOEXCEPT { return *this; }
  396. const _Base&
  397. _M_base() const _GLIBCXX_NOEXCEPT { return *this; }
  398. private:
  399. /** If hint is used we consider that the map and unordered_map
  400. * operations have equivalent insertion cost so we do not update metrics
  401. * about it.
  402. * Note that to find out if hint has been used is libstdc++
  403. * implementation dependent.
  404. */
  405. bool
  406. _M_hint_used(_Base_const_iterator __hint, _Base_iterator __res)
  407. {
  408. return (__hint == __res
  409. || (__hint == _M_base().end() && ++__res == _M_base().end())
  410. || (__hint != _M_base().end() && (++__hint == __res
  411. || ++__res == --__hint)));
  412. }
  413. template<typename _K1, typename _C1, typename _A1>
  414. friend bool
  415. operator==(const multiset<_K1, _C1, _A1>&,
  416. const multiset<_K1, _C1, _A1>&);
  417. template<typename _K1, typename _C1, typename _A1>
  418. friend bool
  419. operator< (const multiset<_K1, _C1, _A1>&,
  420. const multiset<_K1, _C1, _A1>&);
  421. };
  422. template<typename _Key, typename _Compare, typename _Allocator>
  423. inline bool
  424. operator==(const multiset<_Key, _Compare, _Allocator>& __lhs,
  425. const multiset<_Key, _Compare, _Allocator>& __rhs)
  426. {
  427. __profcxx_map2umap_invalidate(__lhs._M_map2umap_info);
  428. __profcxx_map2umap_invalidate(__rhs._M_map2umap_info);
  429. return __lhs._M_base() == __rhs._M_base();
  430. }
  431. template<typename _Key, typename _Compare, typename _Allocator>
  432. inline bool
  433. operator<(const multiset<_Key, _Compare, _Allocator>& __lhs,
  434. const multiset<_Key, _Compare, _Allocator>& __rhs)
  435. {
  436. __profcxx_map2umap_invalidate(__lhs._M_map2umap_info);
  437. __profcxx_map2umap_invalidate(__rhs._M_map2umap_info);
  438. return __lhs._M_base() < __rhs._M_base();
  439. }
  440. template<typename _Key, typename _Compare, typename _Allocator>
  441. inline bool
  442. operator!=(const multiset<_Key, _Compare, _Allocator>& __lhs,
  443. const multiset<_Key, _Compare, _Allocator>& __rhs)
  444. { return !(__lhs == __rhs); }
  445. template<typename _Key, typename _Compare, typename _Allocator>
  446. inline bool
  447. operator<=(const multiset<_Key, _Compare, _Allocator>& __lhs,
  448. const multiset<_Key, _Compare, _Allocator>& __rhs)
  449. { return !(__rhs < __lhs); }
  450. template<typename _Key, typename _Compare, typename _Allocator>
  451. inline bool
  452. operator>=(const multiset<_Key, _Compare, _Allocator>& __lhs,
  453. const multiset<_Key, _Compare, _Allocator>& __rhs)
  454. { return !(__lhs < __rhs); }
  455. template<typename _Key, typename _Compare, typename _Allocator>
  456. inline bool
  457. operator>(const multiset<_Key, _Compare, _Allocator>& __lhs,
  458. const multiset<_Key, _Compare, _Allocator>& __rhs)
  459. { return __rhs < __lhs; }
  460. template<typename _Key, typename _Compare, typename _Allocator>
  461. void
  462. swap(multiset<_Key, _Compare, _Allocator>& __x,
  463. multiset<_Key, _Compare, _Allocator>& __y)
  464. { return __x.swap(__y); }
  465. } // namespace __profile
  466. } // namespace std
  467. #endif