set.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  1. // Profiling set 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/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. #if __cplusplus >= 201103L
  189. noexcept( noexcept(declval<_Base>().swap(__x)) )
  190. #endif
  191. {
  192. _Base::swap(__x);
  193. this->_M_swap(__x);
  194. }
  195. // modifiers:
  196. #if __cplusplus >= 201103L
  197. template<typename... _Args>
  198. std::pair<iterator, bool>
  199. emplace(_Args&&... __args)
  200. {
  201. __profcxx_map2umap_insert(this->_M_map2umap_info, this->size(), 1);
  202. auto __base_ret = _Base::emplace(std::forward<_Args>(__args)...);
  203. return std::make_pair(iterator(__base_ret.first, this),
  204. __base_ret.second);
  205. }
  206. template<typename... _Args>
  207. iterator
  208. emplace_hint(const_iterator __pos, _Args&&... __args)
  209. {
  210. auto size_before = this->size();
  211. auto __res
  212. = _Base::emplace_hint(__pos.base(), std::forward<_Args>(__args)...);
  213. __profcxx_map2umap_insert(this->_M_map2umap_info,
  214. size_before, _M_hint_used(__pos.base(), __res) ? 0 : 1);
  215. return iterator(__res, this);
  216. }
  217. #endif
  218. std::pair<iterator, bool>
  219. insert(const value_type& __x)
  220. {
  221. __profcxx_map2umap_insert(this->_M_map2umap_info, this->size(), 1);
  222. std::pair<_Base_iterator, bool> __base_ret = _Base::insert(__x);
  223. return std::make_pair(iterator(__base_ret.first, this),
  224. __base_ret.second);
  225. }
  226. #if __cplusplus >= 201103L
  227. std::pair<iterator, bool>
  228. insert(value_type&& __x)
  229. {
  230. __profcxx_map2umap_insert(this->_M_map2umap_info, this->size(), 1);
  231. std::pair<_Base_iterator, bool> __base_ret
  232. = _Base::insert(std::move(__x));
  233. return std::make_pair(iterator(__base_ret.first, this),
  234. __base_ret.second);
  235. }
  236. #endif
  237. iterator
  238. insert(const_iterator __pos, const value_type& __x)
  239. {
  240. size_type size_before = this->size();
  241. _Base_iterator __res = _Base::insert(__pos.base(), __x);
  242. __profcxx_map2umap_insert(this->_M_map2umap_info,
  243. size_before, _M_hint_used(__pos.base(), __res) ? 0 : 1);
  244. return iterator(__res, this);
  245. }
  246. #if __cplusplus >= 201103L
  247. iterator
  248. insert(const_iterator __pos, value_type&& __x)
  249. { return iterator(_Base::insert(__pos.base(), std::move(__x)), this); }
  250. #endif
  251. template<typename _InputIterator>
  252. void
  253. insert(_InputIterator __first, _InputIterator __last)
  254. {
  255. for (; __first != __last; ++__first)
  256. insert(*__first);
  257. }
  258. #if __cplusplus >= 201103L
  259. void
  260. insert(initializer_list<value_type> __l)
  261. { insert(__l.begin(), __l.end()); }
  262. #endif
  263. #if __cplusplus >= 201103L
  264. iterator
  265. erase(const_iterator __pos)
  266. {
  267. __profcxx_map2umap_erase(this->_M_map2umap_info, this->size(), 1);
  268. return iterator(_Base::erase(__pos.base()), this);
  269. }
  270. #else
  271. void
  272. erase(iterator __pos)
  273. {
  274. __profcxx_map2umap_erase(this->_M_map2umap_info, this->size(), 1);
  275. _Base::erase(__pos.base());
  276. }
  277. #endif
  278. size_type
  279. erase(const key_type& __x)
  280. {
  281. __profcxx_map2umap_find(this->_M_map2umap_info, this->size());
  282. __profcxx_map2umap_erase(this->_M_map2umap_info, this->size(), 1);
  283. return _Base::erase(__x);
  284. }
  285. #if __cplusplus >= 201103L
  286. iterator
  287. erase(const_iterator __first, const_iterator __last)
  288. {
  289. if (__first != __last)
  290. {
  291. iterator __ret;
  292. for (; __first != __last;)
  293. __ret = erase(__first++);
  294. return __ret;
  295. }
  296. return iterator(_Base::erase(__first.base(), __last.base()), this);
  297. }
  298. #else
  299. void
  300. erase(iterator __first, iterator __last)
  301. {
  302. for (; __first != __last;)
  303. erase(__first++);
  304. }
  305. #endif
  306. void
  307. clear() _GLIBCXX_NOEXCEPT
  308. {
  309. this->_M_profile_destruct();
  310. _Base::clear();
  311. this->_M_profile_construct();
  312. }
  313. size_type
  314. count(const key_type& __x) const
  315. {
  316. __profcxx_map2umap_find(this->_M_map2umap_info, this->size());
  317. return _Base::count(__x);
  318. }
  319. // set operations:
  320. iterator
  321. find(const key_type& __x)
  322. {
  323. __profcxx_map2umap_find(this->_M_map2umap_info, this->size());
  324. return iterator(_Base::find(__x), this);
  325. }
  326. const_iterator
  327. find(const key_type& __x) const
  328. {
  329. __profcxx_map2umap_find(this->_M_map2umap_info, this->size());
  330. return const_iterator(_Base::find(__x), this);
  331. }
  332. iterator
  333. lower_bound(const key_type& __x)
  334. {
  335. __profcxx_map2umap_find(this->_M_map2umap_info, this->size());
  336. __profcxx_map2umap_invalidate(this->_M_map2umap_info);
  337. return iterator(_Base::lower_bound(__x), this);
  338. }
  339. const_iterator
  340. lower_bound(const key_type& __x) const
  341. {
  342. __profcxx_map2umap_find(this->_M_map2umap_info, this->size());
  343. __profcxx_map2umap_invalidate(this->_M_map2umap_info);
  344. return const_iterator(_Base::lower_bound(__x), this);
  345. }
  346. iterator
  347. upper_bound(const key_type& __x)
  348. {
  349. __profcxx_map2umap_find(this->_M_map2umap_info, this->size());
  350. __profcxx_map2umap_invalidate(this->_M_map2umap_info);
  351. return iterator(_Base::upper_bound(__x), this);
  352. }
  353. const_iterator
  354. upper_bound(const key_type& __x) const
  355. {
  356. __profcxx_map2umap_find(this->_M_map2umap_info, this->size());
  357. __profcxx_map2umap_invalidate(this->_M_map2umap_info);
  358. return const_iterator(_Base::upper_bound(__x), this);
  359. }
  360. std::pair<iterator, iterator>
  361. equal_range(const key_type& __x)
  362. {
  363. __profcxx_map2umap_find(this->_M_map2umap_info, this->size());
  364. std::pair<_Base_iterator, _Base_iterator> __base_ret
  365. = _Base::equal_range(__x);
  366. return std::make_pair(iterator(__base_ret.first, this),
  367. iterator(__base_ret.second, this));
  368. }
  369. std::pair<const_iterator, const_iterator>
  370. equal_range(const key_type& __x) const
  371. {
  372. __profcxx_map2umap_find(this->_M_map2umap_info, this->size());
  373. std::pair<_Base_const_iterator, _Base_const_iterator> __base_ret
  374. = _Base::equal_range(__x);
  375. return std::make_pair(const_iterator(__base_ret.first, this),
  376. const_iterator(__base_ret.second, this));
  377. }
  378. _Base&
  379. _M_base() _GLIBCXX_NOEXCEPT { return *this; }
  380. const _Base&
  381. _M_base() const _GLIBCXX_NOEXCEPT { return *this; }
  382. private:
  383. /** If hint is used we consider that the map and unordered_map
  384. * operations have equivalent insertion cost so we do not update metrics
  385. * about it.
  386. * Note that to find out if hint has been used is libstdc++
  387. * implementation dependent.
  388. */
  389. bool
  390. _M_hint_used(_Base_const_iterator __hint, _Base_iterator __res)
  391. {
  392. return (__hint == __res
  393. || (__hint == _M_base().end() && ++__res == _M_base().end())
  394. || (__hint != _M_base().end() && (++__hint == __res
  395. || ++__res == --__hint)));
  396. }
  397. template<typename _K1, typename _C1, typename _A1>
  398. friend bool
  399. operator==(const set<_K1, _C1, _A1>&, const set<_K1, _C1, _A1>&);
  400. template<typename _K1, typename _C1, typename _A1>
  401. friend bool
  402. operator<(const set<_K1, _C1, _A1>&, const set<_K1, _C1, _A1>&);
  403. };
  404. template<typename _Key, typename _Compare, typename _Allocator>
  405. inline bool
  406. operator==(const set<_Key, _Compare, _Allocator>& __lhs,
  407. const set<_Key, _Compare, _Allocator>& __rhs)
  408. {
  409. __profcxx_map2umap_invalidate(__lhs._M_map2umap_info);
  410. __profcxx_map2umap_invalidate(__rhs._M_map2umap_info);
  411. return __lhs._M_base() == __rhs._M_base();
  412. }
  413. template<typename _Key, typename _Compare, typename _Allocator>
  414. inline bool
  415. operator<(const set<_Key, _Compare, _Allocator>& __lhs,
  416. const set<_Key, _Compare, _Allocator>& __rhs)
  417. {
  418. __profcxx_map2umap_invalidate(__lhs._M_map2umap_info);
  419. __profcxx_map2umap_invalidate(__rhs._M_map2umap_info);
  420. return __lhs._M_base() < __rhs._M_base();
  421. }
  422. template<typename _Key, typename _Compare, typename _Allocator>
  423. inline bool
  424. operator!=(const set<_Key, _Compare, _Allocator>& __lhs,
  425. const set<_Key, _Compare, _Allocator>& __rhs)
  426. { return !(__lhs == __rhs); }
  427. template<typename _Key, typename _Compare, typename _Allocator>
  428. inline bool
  429. operator<=(const set<_Key, _Compare, _Allocator>& __lhs,
  430. const set<_Key, _Compare, _Allocator>& __rhs)
  431. { return !(__rhs < __lhs); }
  432. template<typename _Key, typename _Compare, typename _Allocator>
  433. inline bool
  434. operator>=(const set<_Key, _Compare, _Allocator>& __lhs,
  435. const set<_Key, _Compare, _Allocator>& __rhs)
  436. { return !(__lhs < __rhs); }
  437. template<typename _Key, typename _Compare, typename _Allocator>
  438. inline bool
  439. operator>(const set<_Key, _Compare, _Allocator>& __lhs,
  440. const set<_Key, _Compare, _Allocator>& __rhs)
  441. { return __rhs < __lhs; }
  442. template<typename _Key, typename _Compare, typename _Allocator>
  443. void
  444. swap(set<_Key, _Compare, _Allocator>& __x,
  445. set<_Key, _Compare, _Allocator>& __y)
  446. { return __x.swap(__y); }
  447. } // namespace __profile
  448. } // namespace std
  449. #endif