set.h 18 KB

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