map.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592
  1. // Profiling map 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. //
  10. // This library is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. // GNU General Public License for more details.
  14. // Under Section 7 of GPL version 3, you are granted additional
  15. // permissions described in the GCC Runtime Library Exception, version
  16. // 3.1, as published by the Free Software Foundation.
  17. // You should have received a copy of the GNU General Public License along
  18. // with this library; see the file COPYING3. If not see
  19. // <http://www.gnu.org/licenses/>.
  20. /** @file profile/map.h
  21. * This file is a GNU profile extension to the Standard C++ Library.
  22. */
  23. #ifndef _GLIBCXX_PROFILE_MAP_H
  24. #define _GLIBCXX_PROFILE_MAP_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::map 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 map
  35. : public _GLIBCXX_STD_C::map<_Key, _Tp, _Compare, _Allocator>,
  36. public _Ordered_profile<map<_Key, _Tp, _Compare, _Allocator> >
  37. {
  38. typedef _GLIBCXX_STD_C::map<_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 typename _Base::value_type 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, map> iterator;
  50. typedef __iterator_tracker<_Base_const_iterator,
  51. map> const_iterator;
  52. typedef std::reverse_iterator<iterator> reverse_iterator;
  53. typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
  54. typedef typename _Base::size_type size_type;
  55. typedef typename _Base::difference_type difference_type;
  56. // 23.3.1.1 construct/copy/destroy:
  57. #if __cplusplus < 201103L
  58. map()
  59. : _Base() { }
  60. map(const map& __x)
  61. : _Base(__x) { }
  62. ~map()
  63. { }
  64. #else
  65. map() = default;
  66. map(const map&) = default;
  67. map(map&&) = default;
  68. ~map() = default;
  69. #endif
  70. explicit
  71. map(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. map(_InputIterator __first, _InputIterator __last,
  81. const _Compare& __comp = _Compare(),
  82. const _Allocator& __a = _Allocator())
  83. : _Base(__first, __last, __comp, __a) { }
  84. map(const _Base& __x)
  85. : _Base(__x) { }
  86. #if __cplusplus >= 201103L
  87. map(initializer_list<value_type> __l,
  88. const _Compare& __c = _Compare(),
  89. const _Allocator& __a = _Allocator())
  90. : _Base(__l, __c, __a) { }
  91. explicit
  92. map(const _Allocator& __a)
  93. : _Base(__a) { }
  94. map(const map& __x, const _Allocator& __a)
  95. : _Base(__x, __a) { }
  96. map(map&& __x, const _Allocator& __a)
  97. noexcept( noexcept(_Base(std::move(__x), __a)) )
  98. : _Base(std::move(__x), __a) { }
  99. map(initializer_list<value_type> __l, const _Allocator& __a)
  100. : _Base(__l, __a) { }
  101. template<typename _InputIterator>
  102. map(_InputIterator __first, _InputIterator __last,
  103. const _Allocator& __a)
  104. : _Base(__first, __last, __a) { }
  105. #endif
  106. #if __cplusplus < 201103L
  107. map&
  108. operator=(const map& __x)
  109. {
  110. this->_M_profile_destruct();
  111. _M_base() = __x;
  112. this->_M_profile_construct();
  113. return *this;
  114. }
  115. #else
  116. map&
  117. operator=(const map&) = default;
  118. map&
  119. operator=(map&&) = default;
  120. map&
  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. // 23.3.1.2 element access:
  189. mapped_type&
  190. operator[](const key_type& __k)
  191. {
  192. __profcxx_map2umap_find(this->_M_map2umap_info, this->size());
  193. return _Base::operator[](__k);
  194. }
  195. #if __cplusplus >= 201103L
  196. mapped_type&
  197. operator[](key_type&& __k)
  198. {
  199. __profcxx_map2umap_find(this->_M_map2umap_info, this->size());
  200. return _Base::operator[](std::move(__k));
  201. }
  202. #endif
  203. mapped_type&
  204. at(const key_type& __k)
  205. {
  206. __profcxx_map2umap_find(this->_M_map2umap_info, this->size());
  207. return _Base::at(__k);
  208. }
  209. const mapped_type&
  210. at(const key_type& __k) const
  211. {
  212. __profcxx_map2umap_find(this->_M_map2umap_info, this->size());
  213. return _Base::at(__k);
  214. }
  215. // modifiers:
  216. #if __cplusplus >= 201103L
  217. template<typename... _Args>
  218. std::pair<iterator, bool>
  219. emplace(_Args&&... __args)
  220. {
  221. // The cost is the same whether or not the element is inserted so we
  222. // always report insertion of 1 element.
  223. __profcxx_map2umap_insert(this->_M_map2umap_info, this->size(), 1);
  224. auto __base_ret = _Base::emplace(std::forward<_Args>(__args)...);
  225. return std::make_pair(iterator(__base_ret.first, this),
  226. __base_ret.second);
  227. }
  228. template<typename... _Args>
  229. iterator
  230. emplace_hint(const_iterator __pos, _Args&&... __args)
  231. {
  232. auto size_before = this->size();
  233. auto __res
  234. = _Base::emplace_hint(__pos.base(), std::forward<_Args>(__args)...);
  235. __profcxx_map2umap_insert(this->_M_map2umap_info,
  236. size_before, _M_hint_used(__pos.base(), __res) ? 0 : 1);
  237. return iterator(__res, this);
  238. }
  239. #endif
  240. std::pair<iterator, bool>
  241. insert(const value_type& __x)
  242. {
  243. __profcxx_map2umap_insert(this->_M_map2umap_info, this->size(), 1);
  244. std::pair<_Base_iterator, bool> __base_ret = _Base::insert(__x);
  245. return std::make_pair(iterator(__base_ret.first, this),
  246. __base_ret.second);
  247. }
  248. #if __cplusplus >= 201103L
  249. template<typename _Pair, typename = typename
  250. std::enable_if<std::is_constructible<value_type,
  251. _Pair&&>::value>::type>
  252. std::pair<iterator, bool>
  253. insert(_Pair&& __x)
  254. {
  255. __profcxx_map2umap_insert(this->_M_map2umap_info, this->size(), 1);
  256. auto __base_ret= _Base::insert(std::forward<_Pair>(__x));
  257. return std::make_pair(iterator(__base_ret.first, this),
  258. __base_ret.second);
  259. }
  260. #endif
  261. #if __cplusplus >= 201103L
  262. void
  263. insert(std::initializer_list<value_type> __list)
  264. { insert(__list.begin(), __list.end()); }
  265. #endif
  266. iterator
  267. #if __cplusplus >= 201103L
  268. insert(const_iterator __pos, const value_type& __x)
  269. #else
  270. insert(iterator __pos, const value_type& __x)
  271. #endif
  272. {
  273. size_type size_before = this->size();
  274. _Base_iterator __res = _Base::insert(__pos.base(), __x);
  275. __profcxx_map2umap_insert(this->_M_map2umap_info,
  276. size_before, _M_hint_used(__pos.base(), __res) ? 0 : 1);
  277. return iterator(__res, this);
  278. }
  279. #if __cplusplus >= 201103L
  280. template<typename _Pair, typename = typename
  281. std::enable_if<std::is_constructible<value_type,
  282. _Pair&&>::value>::type>
  283. iterator
  284. insert(const_iterator __pos, _Pair&& __x)
  285. {
  286. size_type size_before = this->size();
  287. auto __res = _Base::insert(__pos.base(), std::forward<_Pair>(__x));
  288. __profcxx_map2umap_insert(this->_M_map2umap_info,
  289. size_before, _M_hint_used(__pos.base(), __res) ? 0 : 1);
  290. return iterator(__res, this);
  291. }
  292. #endif
  293. template<typename _InputIterator>
  294. void
  295. insert(_InputIterator __first, _InputIterator __last)
  296. {
  297. for (; __first != __last; ++__first)
  298. insert(*__first);
  299. }
  300. #if __cplusplus >= 201103L
  301. iterator
  302. erase(const_iterator __pos)
  303. {
  304. __profcxx_map2umap_erase(this->_M_map2umap_info, this->size(), 1);
  305. return iterator(_Base::erase(__pos.base()), this);
  306. }
  307. iterator
  308. erase(iterator __pos)
  309. {
  310. __profcxx_map2umap_erase(this->_M_map2umap_info, this->size(), 1);
  311. return iterator(_Base::erase(__pos.base()), this);
  312. }
  313. #else
  314. void
  315. erase(iterator __pos)
  316. {
  317. __profcxx_map2umap_erase(this->_M_map2umap_info, this->size(), 1);
  318. _Base::erase(__pos.base());
  319. }
  320. #endif
  321. size_type
  322. erase(const key_type& __x)
  323. {
  324. __profcxx_map2umap_find(this->_M_map2umap_info, this->size());
  325. __profcxx_map2umap_erase(this->_M_map2umap_info, this->size(), 1);
  326. return _Base::erase(__x);
  327. }
  328. #if __cplusplus >= 201103L
  329. iterator
  330. erase(const_iterator __first, const_iterator __last)
  331. {
  332. if (__first != __last)
  333. {
  334. iterator __ret;
  335. for (; __first != __last;)
  336. __ret = erase(__first++);
  337. return __ret;
  338. }
  339. else
  340. return iterator(_Base::erase(__first.base(), __last.base()), this);
  341. }
  342. #else
  343. void
  344. erase(iterator __first, iterator __last)
  345. {
  346. for (; __first != __last;)
  347. erase(__first++);
  348. }
  349. #endif
  350. void
  351. swap(map& __x)
  352. #if __cplusplus >= 201103L
  353. noexcept( noexcept(declval<_Base>().swap(__x)) )
  354. #endif
  355. {
  356. _Base::swap(__x);
  357. this->_M_swap(__x);
  358. }
  359. void
  360. clear() _GLIBCXX_NOEXCEPT
  361. {
  362. this->_M_profile_destruct();
  363. _Base::clear();
  364. this->_M_profile_construct();
  365. }
  366. // 23.3.1.3 map operations:
  367. iterator
  368. find(const key_type& __x)
  369. {
  370. __profcxx_map2umap_find(this->_M_map2umap_info, this->size());
  371. return iterator(_Base::find(__x), this);
  372. }
  373. const_iterator
  374. find(const key_type& __x) const
  375. {
  376. __profcxx_map2umap_find(this->_M_map2umap_info, this->size());
  377. return const_iterator(_Base::find(__x), this);
  378. }
  379. size_type
  380. count(const key_type& __x) const
  381. {
  382. __profcxx_map2umap_find(this->_M_map2umap_info, this->size());
  383. return _Base::count(__x);
  384. }
  385. iterator
  386. lower_bound(const key_type& __x)
  387. {
  388. __profcxx_map2umap_find(this->_M_map2umap_info, this->size());
  389. __profcxx_map2umap_invalidate(this->_M_map2umap_info);
  390. return iterator(_Base::lower_bound(__x), this);
  391. }
  392. const_iterator
  393. lower_bound(const key_type& __x) const
  394. {
  395. __profcxx_map2umap_find(this->_M_map2umap_info, this->size());
  396. __profcxx_map2umap_invalidate(this->_M_map2umap_info);
  397. return const_iterator(_Base::lower_bound(__x), this);
  398. }
  399. iterator
  400. upper_bound(const key_type& __x)
  401. {
  402. __profcxx_map2umap_find(this->_M_map2umap_info, this->size());
  403. __profcxx_map2umap_invalidate(this->_M_map2umap_info);
  404. return iterator(_Base::upper_bound(__x), this);
  405. }
  406. const_iterator
  407. upper_bound(const key_type& __x) const
  408. {
  409. __profcxx_map2umap_find(this->_M_map2umap_info, this->size());
  410. __profcxx_map2umap_invalidate(this->_M_map2umap_info);
  411. return const_iterator(_Base::upper_bound(__x), this);
  412. }
  413. std::pair<iterator,iterator>
  414. equal_range(const key_type& __x)
  415. {
  416. __profcxx_map2umap_find(this->_M_map2umap_info, this->size());
  417. std::pair<_Base_iterator, _Base_iterator> __base_ret
  418. = _Base::equal_range(__x);
  419. return std::make_pair(iterator(__base_ret.first, this),
  420. iterator(__base_ret.second, this));
  421. }
  422. std::pair<const_iterator,const_iterator>
  423. equal_range(const key_type& __x) const
  424. {
  425. __profcxx_map2umap_find(this->_M_map2umap_info, this->size());
  426. std::pair<_Base_const_iterator, _Base_const_iterator> __base_ret
  427. = _Base::equal_range(__x);
  428. return std::make_pair(const_iterator(__base_ret.first, this),
  429. const_iterator(__base_ret.second, this));
  430. }
  431. _Base&
  432. _M_base() _GLIBCXX_NOEXCEPT { return *this; }
  433. const _Base&
  434. _M_base() const _GLIBCXX_NOEXCEPT { return *this; }
  435. private:
  436. /** If hint is used we consider that the map and unordered_map
  437. * operations have equivalent insertion cost so we do not update metrics
  438. * about it.
  439. * Note that to find out if hint has been used is libstdc++
  440. * implementation dependent.
  441. */
  442. bool
  443. _M_hint_used(_Base_const_iterator __hint, _Base_iterator __res)
  444. {
  445. return (__hint == __res
  446. || (__hint == _M_base().end() && ++__res == _M_base().end())
  447. || (__hint != _M_base().end() && (++__hint == __res
  448. || ++__res == --__hint)));
  449. }
  450. template<typename _K1, typename _T1, typename _C1, typename _A1>
  451. friend bool
  452. operator==(const map<_K1, _T1, _C1, _A1>&,
  453. const map<_K1, _T1, _C1, _A1>&);
  454. template<typename _K1, typename _T1, typename _C1, typename _A1>
  455. friend bool
  456. operator<(const map<_K1, _T1, _C1, _A1>&,
  457. const map<_K1, _T1, _C1, _A1>&);
  458. };
  459. template<typename _Key, typename _Tp,
  460. typename _Compare, typename _Allocator>
  461. inline bool
  462. operator==(const map<_Key, _Tp, _Compare, _Allocator>& __lhs,
  463. const map<_Key, _Tp, _Compare, _Allocator>& __rhs)
  464. {
  465. __profcxx_map2umap_invalidate(__lhs._M_map2umap_info);
  466. __profcxx_map2umap_invalidate(__rhs._M_map2umap_info);
  467. return __lhs._M_base() == __rhs._M_base();
  468. }
  469. template<typename _Key, typename _Tp,
  470. typename _Compare, typename _Allocator>
  471. inline bool
  472. operator<(const map<_Key, _Tp, _Compare, _Allocator>& __lhs,
  473. const map<_Key, _Tp, _Compare, _Allocator>& __rhs)
  474. {
  475. __profcxx_map2umap_invalidate(__lhs._M_map2umap_info);
  476. __profcxx_map2umap_invalidate(__rhs._M_map2umap_info);
  477. return __lhs._M_base() < __rhs._M_base();
  478. }
  479. template<typename _Key, typename _Tp,
  480. typename _Compare, typename _Allocator>
  481. inline bool
  482. operator!=(const map<_Key, _Tp, _Compare, _Allocator>& __lhs,
  483. const map<_Key, _Tp, _Compare, _Allocator>& __rhs)
  484. { return !(__lhs == __rhs); }
  485. template<typename _Key, typename _Tp,
  486. typename _Compare, typename _Allocator>
  487. inline bool
  488. operator<=(const map<_Key, _Tp, _Compare, _Allocator>& __lhs,
  489. const map<_Key, _Tp, _Compare, _Allocator>& __rhs)
  490. { return !(__rhs < __lhs); }
  491. template<typename _Key, typename _Tp,
  492. typename _Compare, typename _Allocator>
  493. inline bool
  494. operator>=(const map<_Key, _Tp, _Compare, _Allocator>& __lhs,
  495. const map<_Key, _Tp, _Compare, _Allocator>& __rhs)
  496. { return !(__lhs < __rhs); }
  497. template<typename _Key, typename _Tp,
  498. typename _Compare, typename _Allocator>
  499. inline bool
  500. operator>(const map<_Key, _Tp, _Compare, _Allocator>& __lhs,
  501. const map<_Key, _Tp, _Compare, _Allocator>& __rhs)
  502. { return __rhs < __lhs; }
  503. template<typename _Key, typename _Tp,
  504. typename _Compare, typename _Allocator>
  505. inline void
  506. swap(map<_Key, _Tp, _Compare, _Allocator>& __lhs,
  507. map<_Key, _Tp, _Compare, _Allocator>& __rhs)
  508. { __lhs.swap(__rhs); }
  509. } // namespace __profile
  510. } // namespace std
  511. #endif