multimap.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  1. // Profiling multimap 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/multimap.h
  21. * This file is a GNU profile extension to the Standard C++ Library.
  22. */
  23. #ifndef _GLIBCXX_PROFILE_MULTIMAP_H
  24. #define _GLIBCXX_PROFILE_MULTIMAP_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::multimap wrapper with performance instrumentation.
  32. template<typename _Key, typename _Tp, typename _Compare = std::less<_Key>,
  33. typename _Allocator = std::allocator<std::pair<const _Key, _Tp> > >
  34. class multimap
  35. : public _GLIBCXX_STD_C::multimap<_Key, _Tp, _Compare, _Allocator>,
  36. public _Ordered_profile<map<_Key, _Tp, _Compare, _Allocator> >
  37. {
  38. typedef _GLIBCXX_STD_C::multimap<_Key, _Tp, _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 _Tp mapped_type;
  45. typedef std::pair<const _Key, _Tp> value_type;
  46. typedef _Compare key_compare;
  47. typedef typename _Base::reference reference;
  48. typedef typename _Base::const_reference const_reference;
  49. typedef __iterator_tracker<_Base_iterator,
  50. multimap> iterator;
  51. typedef __iterator_tracker<_Base_const_iterator,
  52. multimap> const_iterator;
  53. typedef std::reverse_iterator<iterator> reverse_iterator;
  54. typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
  55. typedef typename _Base::size_type size_type;
  56. typedef typename _Base::difference_type difference_type;
  57. // 23.3.1.1 construct/copy/destroy:
  58. #if __cplusplus < 201103L
  59. multimap()
  60. : _Base() { }
  61. multimap(const multimap& __x)
  62. : _Base(__x) { }
  63. ~multimap() { }
  64. #else
  65. multimap() = default;
  66. multimap(const multimap&) = default;
  67. multimap(multimap&&) = default;
  68. ~multimap() = default;
  69. #endif
  70. explicit multimap(const _Compare& __comp,
  71. const _Allocator& __a = _Allocator())
  72. : _Base(__comp, __a) { }
  73. #if __cplusplus >= 201103L
  74. template<typename _InputIterator,
  75. typename = std::_RequireInputIter<_InputIterator>>
  76. #else
  77. template<typename _InputIterator>
  78. #endif
  79. multimap(_InputIterator __first, _InputIterator __last,
  80. const _Compare& __comp = _Compare(),
  81. const _Allocator& __a = _Allocator())
  82. : _Base(__first, __last, __comp, __a) { }
  83. #if __cplusplus >= 201103L
  84. multimap(initializer_list<value_type> __l,
  85. const _Compare& __c = _Compare(),
  86. const _Allocator& __a = _Allocator())
  87. : _Base(__l, __c, __a) { }
  88. explicit
  89. multimap(const _Allocator& __a)
  90. : _Base(__a) { }
  91. multimap(const multimap& __x, const _Allocator& __a)
  92. : _Base(__x, __a) { }
  93. multimap(multimap&& __x, const _Allocator& __a)
  94. noexcept( noexcept(_Base(std::move(__x), __a)) )
  95. : _Base(std::move(__x), __a) { }
  96. multimap(initializer_list<value_type> __l, const _Allocator& __a)
  97. : _Base(__l, __a) { }
  98. template<typename _InputIterator>
  99. multimap(_InputIterator __first, _InputIterator __last,
  100. const _Allocator& __a)
  101. : _Base(__first, __last, __a) { }
  102. #endif
  103. multimap(const _Base& __x)
  104. : _Base(__x) { }
  105. #if __cplusplus < 201103L
  106. multimap&
  107. operator=(const multimap& __x)
  108. {
  109. this->_M_profile_destruct();
  110. _M_base() = __x;
  111. this->_M_profile_construct();
  112. return *this;
  113. }
  114. #else
  115. multimap&
  116. operator=(const multimap&) = default;
  117. multimap&
  118. operator=(multimap&&) = default;
  119. multimap&
  120. operator=(initializer_list<value_type> __l)
  121. {
  122. this->_M_profile_destruct();
  123. _M_base() = __l;
  124. this->_M_profile_construct();
  125. return *this;
  126. }
  127. #endif
  128. // iterators
  129. iterator
  130. begin() _GLIBCXX_NOEXCEPT
  131. { return iterator(_Base::begin(), this); }
  132. const_iterator
  133. begin() const _GLIBCXX_NOEXCEPT
  134. { return const_iterator(_Base::begin(), this); }
  135. iterator
  136. end() _GLIBCXX_NOEXCEPT
  137. { return iterator(_Base::end(), this); }
  138. const_iterator
  139. end() const _GLIBCXX_NOEXCEPT
  140. { return const_iterator(_Base::end(), this); }
  141. #if __cplusplus >= 201103L
  142. const_iterator
  143. cbegin() const noexcept
  144. { return const_iterator(_Base::cbegin(), this); }
  145. const_iterator
  146. cend() const noexcept
  147. { return const_iterator(_Base::cend(), this); }
  148. #endif
  149. reverse_iterator
  150. rbegin() _GLIBCXX_NOEXCEPT
  151. {
  152. __profcxx_map2umap_invalidate(this->_M_map2umap_info);
  153. return reverse_iterator(end());
  154. }
  155. const_reverse_iterator
  156. rbegin() const _GLIBCXX_NOEXCEPT
  157. {
  158. __profcxx_map2umap_invalidate(this->_M_map2umap_info);
  159. return const_reverse_iterator(end());
  160. }
  161. reverse_iterator
  162. rend() _GLIBCXX_NOEXCEPT
  163. {
  164. __profcxx_map2umap_invalidate(this->_M_map2umap_info);
  165. return reverse_iterator(begin());
  166. }
  167. const_reverse_iterator
  168. rend() const _GLIBCXX_NOEXCEPT
  169. {
  170. __profcxx_map2umap_invalidate(this->_M_map2umap_info);
  171. return const_reverse_iterator(begin());
  172. }
  173. #if __cplusplus >= 201103L
  174. const_reverse_iterator
  175. crbegin() const noexcept
  176. {
  177. __profcxx_map2umap_invalidate(this->_M_map2umap_info);
  178. return const_reverse_iterator(cend());
  179. }
  180. const_reverse_iterator
  181. crend() const noexcept
  182. {
  183. __profcxx_map2umap_invalidate(this->_M_map2umap_info);
  184. return const_reverse_iterator(cbegin());
  185. }
  186. #endif
  187. // modifiers:
  188. #if __cplusplus >= 201103L
  189. template<typename... _Args>
  190. iterator
  191. emplace(_Args&&... __args)
  192. {
  193. __profcxx_map2umap_insert(this->_M_map2umap_info, this->size(), 1);
  194. return iterator(_Base::emplace(std::forward<_Args>(__args)...), this);
  195. }
  196. template<typename... _Args>
  197. iterator
  198. emplace_hint(const_iterator __pos, _Args&&... __args)
  199. {
  200. auto size_before = this->size();
  201. auto __res
  202. = _Base::emplace_hint(__pos.base(), std::forward<_Args>(__args)...);
  203. __profcxx_map2umap_insert(this->_M_map2umap_info,
  204. size_before, _M_hint_used(__pos.base(), __res) ? 0 : 1);
  205. return iterator(__res, this);
  206. }
  207. #endif
  208. iterator
  209. insert(const value_type& __x)
  210. {
  211. __profcxx_map2umap_insert(this->_M_map2umap_info, this->size(), 1);
  212. return iterator(_Base::insert(__x), this);
  213. }
  214. #if __cplusplus >= 201103L
  215. template<typename _Pair, typename = typename
  216. std::enable_if<std::is_constructible<value_type,
  217. _Pair&&>::value>::type>
  218. iterator
  219. insert(_Pair&& __x)
  220. {
  221. __profcxx_map2umap_insert(this->_M_map2umap_info, this->size(), 1);
  222. return iterator(_Base::insert(std::forward<_Pair>(__x)), this);
  223. }
  224. #endif
  225. #if __cplusplus >= 201103L
  226. void
  227. insert(std::initializer_list<value_type> __list)
  228. { insert(__list.begin(), __list.end()); }
  229. #endif
  230. iterator
  231. #if __cplusplus >= 201103L
  232. insert(const_iterator __pos, const value_type& __x)
  233. #else
  234. insert(iterator __pos, const value_type& __x)
  235. #endif
  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. template<typename _Pair, typename = typename
  245. std::enable_if<std::is_constructible<value_type,
  246. _Pair&&>::value>::type>
  247. iterator
  248. insert(const_iterator __pos, _Pair&& __x)
  249. {
  250. size_type size_before = this->size();
  251. auto __res = _Base::insert(__pos.base(), std::forward<_Pair>(__x));
  252. __profcxx_map2umap_insert(this->_M_map2umap_info,
  253. size_before, _M_hint_used(__pos.base(), __res) ? 0 : 1);
  254. return iterator(__res, this);
  255. }
  256. #endif
  257. template<typename _InputIterator>
  258. void
  259. insert(_InputIterator __first, _InputIterator __last)
  260. {
  261. for (; __first != __last; ++__first)
  262. insert(*__first);
  263. }
  264. #if __cplusplus >= 201103L
  265. iterator
  266. erase(const_iterator __pos)
  267. {
  268. __profcxx_map2umap_erase(this->_M_map2umap_info, this->size(), 1);
  269. return iterator(_Base::erase(__pos.base()), this);
  270. }
  271. iterator
  272. erase(iterator __pos)
  273. {
  274. __profcxx_map2umap_erase(this->_M_map2umap_info, this->size(), 1);
  275. return iterator(_Base::erase(__pos.base()), this);
  276. }
  277. #else
  278. void
  279. erase(iterator __pos)
  280. {
  281. __profcxx_map2umap_erase(this->_M_map2umap_info, this->size(), 1);
  282. _Base::erase(__pos.base());
  283. }
  284. #endif
  285. size_type
  286. erase(const key_type& __x)
  287. {
  288. __profcxx_map2umap_find(this->_M_map2umap_info, this->size());
  289. __profcxx_map2umap_erase(this->_M_map2umap_info, this->size(), 1);
  290. return _Base::erase(__x);
  291. }
  292. #if __cplusplus >= 201103L
  293. iterator
  294. erase(const_iterator __first, const_iterator __last)
  295. {
  296. if (__first != __last)
  297. {
  298. iterator __ret;
  299. for (; __first != __last;)
  300. __ret = erase(__first++);
  301. return __ret;
  302. }
  303. else
  304. return iterator(_Base::erase(__first.base(), __last.base()), this);
  305. }
  306. #else
  307. void
  308. erase(iterator __first, iterator __last)
  309. {
  310. for (; __first != __last;)
  311. erase(__first++);
  312. }
  313. #endif
  314. void
  315. swap(multimap& __x)
  316. #if __cplusplus >= 201103L
  317. noexcept( noexcept(declval<_Base>().swap(__x)) )
  318. #endif
  319. {
  320. std::swap(this->_M_map2umap_info, __x._M_map2umap_info);
  321. _Base::swap(__x);
  322. }
  323. void
  324. clear() _GLIBCXX_NOEXCEPT
  325. {
  326. this->_M_profile_destruct();
  327. _Base::clear();
  328. this->_M_profile_construct();
  329. }
  330. // 23.3.1.3 multimap operations:
  331. iterator
  332. find(const key_type& __x)
  333. {
  334. __profcxx_map2umap_find(this->_M_map2umap_info, this->size());
  335. return iterator(_Base::find(__x), this);
  336. }
  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. size_type
  344. count(const key_type& __x) const
  345. {
  346. __profcxx_map2umap_find(this->_M_map2umap_info, this->size());
  347. return _Base::count(__x);
  348. }
  349. iterator
  350. lower_bound(const key_type& __x)
  351. {
  352. __profcxx_map2umap_find(this->_M_map2umap_info, this->size());
  353. __profcxx_map2umap_invalidate(this->_M_map2umap_info);
  354. return iterator(_Base::lower_bound(__x), this);
  355. }
  356. const_iterator
  357. lower_bound(const key_type& __x) const
  358. {
  359. __profcxx_map2umap_find(this->_M_map2umap_info, this->size());
  360. __profcxx_map2umap_invalidate(this->_M_map2umap_info);
  361. return const_iterator(_Base::lower_bound(__x), this);
  362. }
  363. iterator
  364. upper_bound(const key_type& __x)
  365. {
  366. __profcxx_map2umap_find(this->_M_map2umap_info, this->size());
  367. __profcxx_map2umap_invalidate(this->_M_map2umap_info);
  368. return iterator(_Base::upper_bound(__x), this);
  369. }
  370. const_iterator
  371. upper_bound(const key_type& __x) const
  372. {
  373. __profcxx_map2umap_find(this->_M_map2umap_info, this->size());
  374. __profcxx_map2umap_invalidate(this->_M_map2umap_info);
  375. return const_iterator(_Base::upper_bound(__x), this);
  376. }
  377. std::pair<iterator,iterator>
  378. equal_range(const key_type& __x)
  379. {
  380. __profcxx_map2umap_find(this->_M_map2umap_info, this->size());
  381. std::pair<_Base_iterator, _Base_iterator> __base_ret
  382. = _Base::equal_range(__x);
  383. return std::make_pair(iterator(__base_ret.first, this),
  384. iterator(__base_ret.second, this));
  385. }
  386. std::pair<const_iterator,const_iterator>
  387. equal_range(const key_type& __x) const
  388. {
  389. __profcxx_map2umap_find(this->_M_map2umap_info, this->size());
  390. std::pair<_Base_const_iterator, _Base_const_iterator> __base_ret
  391. = _Base::equal_range(__x);
  392. return std::make_pair(const_iterator(__base_ret.first, this),
  393. const_iterator(__base_ret.second, this));
  394. }
  395. _Base&
  396. _M_base() _GLIBCXX_NOEXCEPT { return *this; }
  397. const _Base&
  398. _M_base() const _GLIBCXX_NOEXCEPT { return *this; }
  399. private:
  400. /** If hint is used we consider that the map and unordered_map
  401. * operations have equivalent insertion cost so we do not update metrics
  402. * about it.
  403. * Note that to find out if hint has been used is libstdc++
  404. * implementation dependent.
  405. */
  406. bool
  407. _M_hint_used(_Base_const_iterator __hint, _Base_iterator __res)
  408. {
  409. return (__hint == __res
  410. || (__hint == _M_base().end() && ++__res == _M_base().end())
  411. || (__hint != _M_base().end() && (++__hint == __res
  412. || ++__res == --__hint)));
  413. }
  414. template<typename _K1, typename _T1, typename _C1, typename _A1>
  415. friend bool
  416. operator==(const multimap<_K1, _T1, _C1, _A1>&,
  417. const multimap<_K1, _T1, _C1, _A1>&);
  418. template<typename _K1, typename _T1, typename _C1, typename _A1>
  419. friend bool
  420. operator<(const multimap<_K1, _T1, _C1, _A1>&,
  421. const multimap<_K1, _T1, _C1, _A1>&);
  422. };
  423. template<typename _Key, typename _Tp,
  424. typename _Compare, typename _Allocator>
  425. inline bool
  426. operator==(const multimap<_Key, _Tp, _Compare, _Allocator>& __lhs,
  427. const multimap<_Key, _Tp, _Compare, _Allocator>& __rhs)
  428. {
  429. __profcxx_map2umap_invalidate(__lhs._M_map2umap_info);
  430. __profcxx_map2umap_invalidate(__rhs._M_map2umap_info);
  431. return __lhs._M_base() == __rhs._M_base();
  432. }
  433. template<typename _Key, typename _Tp,
  434. typename _Compare, typename _Allocator>
  435. inline bool
  436. operator<(const multimap<_Key, _Tp, _Compare, _Allocator>& __lhs,
  437. const multimap<_Key, _Tp, _Compare, _Allocator>& __rhs)
  438. {
  439. __profcxx_map2umap_invalidate(__lhs._M_map2umap_info);
  440. __profcxx_map2umap_invalidate(__rhs._M_map2umap_info);
  441. return __lhs._M_base() < __rhs._M_base();
  442. }
  443. template<typename _Key, typename _Tp,
  444. typename _Compare, typename _Allocator>
  445. inline bool
  446. operator!=(const multimap<_Key, _Tp, _Compare, _Allocator>& __lhs,
  447. const multimap<_Key, _Tp, _Compare, _Allocator>& __rhs)
  448. { return !(__lhs == __rhs); }
  449. template<typename _Key, typename _Tp,
  450. typename _Compare, typename _Allocator>
  451. inline bool
  452. operator<=(const multimap<_Key, _Tp, _Compare, _Allocator>& __lhs,
  453. const multimap<_Key, _Tp, _Compare, _Allocator>& __rhs)
  454. { return !(__rhs < __lhs); }
  455. template<typename _Key, typename _Tp,
  456. typename _Compare, typename _Allocator>
  457. inline bool
  458. operator>=(const multimap<_Key, _Tp, _Compare, _Allocator>& __lhs,
  459. const multimap<_Key, _Tp, _Compare, _Allocator>& __rhs)
  460. { return !(__lhs < __rhs); }
  461. template<typename _Key, typename _Tp,
  462. typename _Compare, typename _Allocator>
  463. inline bool
  464. operator>(const multimap<_Key, _Tp, _Compare, _Allocator>& __lhs,
  465. const multimap<_Key, _Tp, _Compare, _Allocator>& __rhs)
  466. { return __rhs < __lhs; }
  467. template<typename _Key, typename _Tp,
  468. typename _Compare, typename _Allocator>
  469. inline void
  470. swap(multimap<_Key, _Tp, _Compare, _Allocator>& __lhs,
  471. multimap<_Key, _Tp, _Compare, _Allocator>& __rhs)
  472. { __lhs.swap(__rhs); }
  473. } // namespace __profile
  474. } // namespace std
  475. #endif