_set.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. /*
  2. *
  3. * Copyright (c) 1994
  4. * Hewlett-Packard Company
  5. *
  6. * Copyright (c) 1996,1997
  7. * Silicon Graphics Computer Systems, Inc.
  8. *
  9. * Copyright (c) 1997
  10. * Moscow Center for SPARC Technology
  11. *
  12. * Copyright (c) 1999
  13. * Boris Fomitchev
  14. *
  15. * This material is provided "as is", with absolutely no warranty expressed
  16. * or implied. Any use is at your own risk.
  17. *
  18. * Permission to use or copy this software for any purpose is hereby granted
  19. * without fee, provided the above notices are retained on all copies.
  20. * Permission to modify the code and to distribute modified code is granted,
  21. * provided the above notices are retained, and a notice that the code was
  22. * modified is included with the above copyright notice.
  23. *
  24. */
  25. /* NOTE: This is an internal header file, included by other STL headers.
  26. * You should not attempt to use it directly.
  27. */
  28. #ifndef _STLP_INTERNAL_SET_H
  29. #define _STLP_INTERNAL_SET_H
  30. #ifndef _STLP_INTERNAL_TREE_H
  31. # include <stl/_tree.h>
  32. #endif
  33. #if !defined (_STLP_USE_PTR_SPECIALIZATIONS)
  34. _STLP_BEGIN_NAMESPACE
  35. //Specific iterator traits creation
  36. _STLP_CREATE_ITERATOR_TRAITS(SetTraitsT, Const_traits)
  37. template <class _Key, _STLP_DFL_TMPL_PARAM(_Compare, less<_Key>),
  38. _STLP_DFL_TMPL_PARAM(_Alloc, allocator<_Key>) >
  39. class set
  40. #if defined (_STLP_USE_PARTIAL_SPEC_WORKAROUND)
  41. : public __stlport_class<set<_Key, _Compare, _Alloc> >
  42. #endif
  43. {
  44. typedef set<_Key, _Compare, _Alloc> _Self;
  45. public:
  46. // typedefs:
  47. typedef _Key key_type;
  48. typedef _Key value_type;
  49. typedef _Compare key_compare;
  50. typedef _Compare value_compare;
  51. private:
  52. //Specific iterator traits creation
  53. typedef _STLP_PRIV _SetTraitsT<value_type> _SetTraits;
  54. public:
  55. //Following typedef have to be public for __move_traits specialization.
  56. typedef _STLP_PRIV _Rb_tree<key_type, key_compare,
  57. value_type, _STLP_PRIV _Identity<value_type>,
  58. _SetTraits, _Alloc> _Rep_type;
  59. typedef typename _Rep_type::pointer pointer;
  60. typedef typename _Rep_type::const_pointer const_pointer;
  61. typedef typename _Rep_type::reference reference;
  62. typedef typename _Rep_type::const_reference const_reference;
  63. typedef typename _Rep_type::iterator iterator;
  64. typedef typename _Rep_type::const_iterator const_iterator;
  65. typedef typename _Rep_type::reverse_iterator reverse_iterator;
  66. typedef typename _Rep_type::const_reverse_iterator const_reverse_iterator;
  67. typedef typename _Rep_type::size_type size_type;
  68. typedef typename _Rep_type::difference_type difference_type;
  69. typedef typename _Rep_type::allocator_type allocator_type;
  70. private:
  71. _Rep_type _M_t; // red-black tree representing set
  72. _STLP_KEY_TYPE_FOR_CONT_EXT(key_type)
  73. public:
  74. // allocation/deallocation
  75. #if !defined (_STLP_DONT_SUP_DFLT_PARAM)
  76. explicit set(const _Compare& __comp = _Compare(),
  77. const allocator_type& __a = allocator_type())
  78. #else
  79. set()
  80. : _M_t(_Compare(), allocator_type()) {}
  81. explicit set(const _Compare& __comp)
  82. : _M_t(__comp, allocator_type()) {}
  83. set(const _Compare& __comp, const allocator_type& __a)
  84. #endif
  85. : _M_t(__comp, __a) {}
  86. #if defined (_STLP_MEMBER_TEMPLATES)
  87. template <class _InputIterator>
  88. set(_InputIterator __first, _InputIterator __last)
  89. : _M_t(_Compare(), allocator_type())
  90. { _M_t.insert_unique(__first, __last); }
  91. # if defined (_STLP_NEEDS_EXTRA_TEMPLATE_CONSTRUCTORS)
  92. template <class _InputIterator>
  93. set(_InputIterator __first, _InputIterator __last, const _Compare& __comp)
  94. : _M_t(__comp, allocator_type()) { _M_t.insert_unique(__first, __last); }
  95. # endif
  96. template <class _InputIterator>
  97. set(_InputIterator __first, _InputIterator __last, const _Compare& __comp,
  98. const allocator_type& __a _STLP_ALLOCATOR_TYPE_DFL)
  99. : _M_t(__comp, __a) { _M_t.insert_unique(__first, __last); }
  100. #else
  101. set(const value_type* __first, const value_type* __last)
  102. : _M_t(_Compare(), allocator_type())
  103. { _M_t.insert_unique(__first, __last); }
  104. set(const value_type* __first,
  105. const value_type* __last, const _Compare& __comp,
  106. const allocator_type& __a = allocator_type())
  107. : _M_t(__comp, __a) { _M_t.insert_unique(__first, __last); }
  108. set(const_iterator __first, const_iterator __last)
  109. : _M_t(_Compare(), allocator_type())
  110. { _M_t.insert_unique(__first, __last); }
  111. set(const_iterator __first, const_iterator __last, const _Compare& __comp,
  112. const allocator_type& __a = allocator_type())
  113. : _M_t(__comp, __a) { _M_t.insert_unique(__first, __last); }
  114. #endif /* _STLP_MEMBER_TEMPLATES */
  115. set(const _Self& __x) : _M_t(__x._M_t) {}
  116. #if !defined (_STLP_NO_MOVE_SEMANTIC)
  117. set(__move_source<_Self> src)
  118. : _M_t(__move_source<_Rep_type>(src.get()._M_t)) {}
  119. #endif
  120. _Self& operator=(const _Self& __x) {
  121. _M_t = __x._M_t;
  122. return *this;
  123. }
  124. // accessors:
  125. key_compare key_comp() const { return _M_t.key_comp(); }
  126. value_compare value_comp() const { return _M_t.key_comp(); }
  127. allocator_type get_allocator() const { return _M_t.get_allocator(); }
  128. iterator begin() { return _M_t.begin(); }
  129. iterator end() { return _M_t.end(); }
  130. const_iterator begin() const { return _M_t.begin(); }
  131. const_iterator end() const { return _M_t.end(); }
  132. reverse_iterator rbegin() { return _M_t.rbegin(); }
  133. reverse_iterator rend() { return _M_t.rend(); }
  134. const_reverse_iterator rbegin() const { return _M_t.rbegin(); }
  135. const_reverse_iterator rend() const { return _M_t.rend(); }
  136. bool empty() const { return _M_t.empty(); }
  137. size_type size() const { return _M_t.size(); }
  138. size_type max_size() const { return _M_t.max_size(); }
  139. void swap(_Self& __x) { _M_t.swap(__x._M_t); }
  140. #if defined (_STLP_USE_PARTIAL_SPEC_WORKAROUND) && !defined (_STLP_FUNCTION_TMPL_PARTIAL_ORDER)
  141. void _M_swap_workaround(_Self& __x) { swap(__x); }
  142. #endif
  143. // insert/erase
  144. pair<iterator,bool> insert(const value_type& __x)
  145. { return _M_t.insert_unique(__x); }
  146. iterator insert(iterator __pos, const value_type& __x)
  147. { return _M_t.insert_unique( __pos , __x); }
  148. #if defined (_STLP_MEMBER_TEMPLATES)
  149. template <class _InputIterator>
  150. void insert(_InputIterator __first, _InputIterator __last)
  151. { _M_t.insert_unique(__first, __last); }
  152. #else
  153. void insert(const_iterator __first, const_iterator __last)
  154. { _M_t.insert_unique(__first, __last); }
  155. void insert(const value_type* __first, const value_type* __last)
  156. { _M_t.insert_unique(__first, __last); }
  157. #endif /* _STLP_MEMBER_TEMPLATES */
  158. void erase(iterator __pos) { _M_t.erase( __pos ); }
  159. size_type erase(const key_type& __x) { return _M_t.erase_unique(__x); }
  160. void erase(iterator __first, iterator __last) { _M_t.erase(__first, __last ); }
  161. void clear() { _M_t.clear(); }
  162. // set operations:
  163. _STLP_TEMPLATE_FOR_CONT_EXT
  164. const_iterator find(const _KT& __x) const { return _M_t.find(__x); }
  165. _STLP_TEMPLATE_FOR_CONT_EXT
  166. iterator find(const _KT& __x) { return _M_t.find(__x); }
  167. _STLP_TEMPLATE_FOR_CONT_EXT
  168. size_type count(const _KT& __x) const
  169. { return _M_t.find(__x) == _M_t.end() ? 0 : 1 ; }
  170. _STLP_TEMPLATE_FOR_CONT_EXT
  171. iterator lower_bound(const _KT& __x) { return _M_t.lower_bound(__x); }
  172. _STLP_TEMPLATE_FOR_CONT_EXT
  173. const_iterator lower_bound(const _KT& __x) const { return _M_t.lower_bound(__x); }
  174. _STLP_TEMPLATE_FOR_CONT_EXT
  175. iterator upper_bound(const _KT& __x) { return _M_t.upper_bound(__x); }
  176. _STLP_TEMPLATE_FOR_CONT_EXT
  177. const_iterator upper_bound(const _KT& __x) const { return _M_t.upper_bound(__x); }
  178. _STLP_TEMPLATE_FOR_CONT_EXT
  179. pair<iterator, iterator> equal_range(const _KT& __x)
  180. { return _M_t.equal_range_unique(__x); }
  181. _STLP_TEMPLATE_FOR_CONT_EXT
  182. pair<const_iterator, const_iterator> equal_range(const _KT& __x) const
  183. { return _M_t.equal_range_unique(__x); }
  184. };
  185. //Specific iterator traits creation
  186. _STLP_CREATE_ITERATOR_TRAITS(MultisetTraitsT, Const_traits)
  187. template <class _Key, _STLP_DFL_TMPL_PARAM(_Compare, less<_Key>),
  188. _STLP_DFL_TMPL_PARAM(_Alloc, allocator<_Key>) >
  189. class multiset
  190. #if defined (_STLP_USE_PARTIAL_SPEC_WORKAROUND)
  191. : public __stlport_class<multiset<_Key, _Compare, _Alloc> >
  192. #endif
  193. {
  194. typedef multiset<_Key, _Compare, _Alloc> _Self;
  195. public:
  196. // typedefs:
  197. typedef _Key key_type;
  198. typedef _Key value_type;
  199. typedef _Compare key_compare;
  200. typedef _Compare value_compare;
  201. private:
  202. //Specific iterator traits creation
  203. typedef _STLP_PRIV _MultisetTraitsT<value_type> _MultisetTraits;
  204. public:
  205. //Following typedef have to be public for __move_traits specialization.
  206. typedef _STLP_PRIV _Rb_tree<key_type, key_compare,
  207. value_type, _STLP_PRIV _Identity<value_type>,
  208. _MultisetTraits, _Alloc> _Rep_type;
  209. typedef typename _Rep_type::pointer pointer;
  210. typedef typename _Rep_type::const_pointer const_pointer;
  211. typedef typename _Rep_type::reference reference;
  212. typedef typename _Rep_type::const_reference const_reference;
  213. typedef typename _Rep_type::iterator iterator;
  214. typedef typename _Rep_type::const_iterator const_iterator;
  215. typedef typename _Rep_type::reverse_iterator reverse_iterator;
  216. typedef typename _Rep_type::const_reverse_iterator const_reverse_iterator;
  217. typedef typename _Rep_type::size_type size_type;
  218. typedef typename _Rep_type::difference_type difference_type;
  219. typedef typename _Rep_type::allocator_type allocator_type;
  220. private:
  221. _Rep_type _M_t; // red-black tree representing multiset
  222. _STLP_KEY_TYPE_FOR_CONT_EXT(key_type)
  223. public:
  224. #if !defined (_STLP_DONT_SUP_DFLT_PARAM)
  225. explicit multiset(const _Compare& __comp = _Compare(),
  226. const allocator_type& __a = allocator_type())
  227. #else
  228. multiset()
  229. : _M_t(_Compare(), allocator_type()) {}
  230. explicit multiset(const _Compare& __comp)
  231. : _M_t(__comp, allocator_type()) {}
  232. multiset(const _Compare& __comp, const allocator_type& __a)
  233. #endif
  234. : _M_t(__comp, __a) {}
  235. #if defined (_STLP_MEMBER_TEMPLATES)
  236. template <class _InputIterator>
  237. multiset(_InputIterator __first, _InputIterator __last)
  238. : _M_t(_Compare(), allocator_type())
  239. { _M_t.insert_equal(__first, __last); }
  240. template <class _InputIterator>
  241. multiset(_InputIterator __first, _InputIterator __last,
  242. const _Compare& __comp,
  243. const allocator_type& __a _STLP_ALLOCATOR_TYPE_DFL)
  244. : _M_t(__comp, __a) { _M_t.insert_equal(__first, __last); }
  245. # if defined (_STLP_NEEDS_EXTRA_TEMPLATE_CONSTRUCTORS)
  246. template <class _InputIterator>
  247. multiset(_InputIterator __first, _InputIterator __last,
  248. const _Compare& __comp)
  249. : _M_t(__comp, allocator_type()) { _M_t.insert_equal(__first, __last); }
  250. # endif
  251. #else
  252. multiset(const value_type* __first, const value_type* __last)
  253. : _M_t(_Compare(), allocator_type())
  254. { _M_t.insert_equal(__first, __last); }
  255. multiset(const value_type* __first, const value_type* __last,
  256. const _Compare& __comp,
  257. const allocator_type& __a = allocator_type())
  258. : _M_t(__comp, __a) { _M_t.insert_equal(__first, __last); }
  259. multiset(const_iterator __first, const_iterator __last)
  260. : _M_t(_Compare(), allocator_type())
  261. { _M_t.insert_equal(__first, __last); }
  262. multiset(const_iterator __first, const_iterator __last,
  263. const _Compare& __comp,
  264. const allocator_type& __a = allocator_type())
  265. : _M_t(__comp, __a) { _M_t.insert_equal(__first, __last); }
  266. #endif /* _STLP_MEMBER_TEMPLATES */
  267. multiset(const _Self& __x) : _M_t(__x._M_t) {}
  268. _Self& operator=(const _Self& __x) {
  269. _M_t = __x._M_t;
  270. return *this;
  271. }
  272. #if !defined (_STLP_NO_MOVE_SEMANTIC)
  273. multiset(__move_source<_Self> src)
  274. : _M_t(__move_source<_Rep_type>(src.get()._M_t)) {}
  275. #endif
  276. // accessors:
  277. key_compare key_comp() const { return _M_t.key_comp(); }
  278. value_compare value_comp() const { return _M_t.key_comp(); }
  279. allocator_type get_allocator() const { return _M_t.get_allocator(); }
  280. iterator begin() { return _M_t.begin(); }
  281. iterator end() { return _M_t.end(); }
  282. const_iterator begin() const { return _M_t.begin(); }
  283. const_iterator end() const { return _M_t.end(); }
  284. reverse_iterator rbegin() { return _M_t.rbegin(); }
  285. reverse_iterator rend() { return _M_t.rend(); }
  286. const_reverse_iterator rbegin() const { return _M_t.rbegin(); }
  287. const_reverse_iterator rend() const { return _M_t.rend(); }
  288. bool empty() const { return _M_t.empty(); }
  289. size_type size() const { return _M_t.size(); }
  290. size_type max_size() const { return _M_t.max_size(); }
  291. void swap(_Self& __x) { _M_t.swap(__x._M_t); }
  292. #if defined (_STLP_USE_PARTIAL_SPEC_WORKAROUND) && !defined (_STLP_FUNCTION_TMPL_PARTIAL_ORDER)
  293. void _M_swap_workaround(_Self& __x) { swap(__x); }
  294. #endif
  295. // insert/erase
  296. iterator insert(const value_type& __x)
  297. { return _M_t.insert_equal(__x); }
  298. iterator insert(iterator __pos, const value_type& __x)
  299. { return _M_t.insert_equal(__pos, __x); }
  300. #if defined (_STLP_MEMBER_TEMPLATES)
  301. template <class _InputIterator>
  302. void insert(_InputIterator __first, _InputIterator __last)
  303. { _M_t.insert_equal(__first, __last); }
  304. #else
  305. void insert(const value_type* __first, const value_type* __last)
  306. { _M_t.insert_equal(__first, __last); }
  307. void insert(const_iterator __first, const_iterator __last)
  308. { _M_t.insert_equal(__first, __last); }
  309. #endif /* _STLP_MEMBER_TEMPLATES */
  310. void erase(iterator __pos) { _M_t.erase( __pos ); }
  311. size_type erase(const key_type& __x) { return _M_t.erase(__x); }
  312. void erase(iterator __first, iterator __last) { _M_t.erase( __first, __last ); }
  313. void clear() { _M_t.clear(); }
  314. // multiset operations:
  315. _STLP_TEMPLATE_FOR_CONT_EXT
  316. iterator find(const _KT& __x) { return _M_t.find(__x); }
  317. _STLP_TEMPLATE_FOR_CONT_EXT
  318. const_iterator find(const _KT& __x) const { return _M_t.find(__x); }
  319. _STLP_TEMPLATE_FOR_CONT_EXT
  320. size_type count(const _KT& __x) const { return _M_t.count(__x); }
  321. _STLP_TEMPLATE_FOR_CONT_EXT
  322. iterator lower_bound(const _KT& __x) { return _M_t.lower_bound(__x); }
  323. _STLP_TEMPLATE_FOR_CONT_EXT
  324. const_iterator lower_bound(const _KT& __x) const { return _M_t.lower_bound(__x); }
  325. _STLP_TEMPLATE_FOR_CONT_EXT
  326. iterator upper_bound(const _KT& __x) { return _M_t.upper_bound(__x); }
  327. _STLP_TEMPLATE_FOR_CONT_EXT
  328. const_iterator upper_bound(const _KT& __x) const { return _M_t.upper_bound(__x); }
  329. _STLP_TEMPLATE_FOR_CONT_EXT
  330. pair<iterator, iterator> equal_range(const _KT& __x) { return _M_t.equal_range(__x); }
  331. _STLP_TEMPLATE_FOR_CONT_EXT
  332. pair<const_iterator, const_iterator> equal_range(const _KT& __x) const { return _M_t.equal_range(__x); }
  333. };
  334. #else
  335. # include <stl/pointers/_set.h>
  336. _STLP_BEGIN_NAMESPACE
  337. #endif /* _STLP_USE_PTR_SPECIALIZATIONS */
  338. #define _STLP_TEMPLATE_HEADER template <class _Key, class _Compare, class _Alloc>
  339. #define _STLP_TEMPLATE_CONTAINER set<_Key,_Compare,_Alloc>
  340. #include <stl/_relops_cont.h>
  341. #undef _STLP_TEMPLATE_CONTAINER
  342. #define _STLP_TEMPLATE_CONTAINER multiset<_Key,_Compare,_Alloc>
  343. #include <stl/_relops_cont.h>
  344. #undef _STLP_TEMPLATE_CONTAINER
  345. #undef _STLP_TEMPLATE_HEADER
  346. #if defined (_STLP_CLASS_PARTIAL_SPECIALIZATION) && !defined (_STLP_NO_MOVE_SEMANTIC)
  347. template <class _Key, class _Compare, class _Alloc>
  348. struct __move_traits<set<_Key,_Compare,_Alloc> > :
  349. _STLP_PRIV __move_traits_aux<typename set<_Key,_Compare,_Alloc>::_Rep_type>
  350. {};
  351. template <class _Key, class _Compare, class _Alloc>
  352. struct __move_traits<multiset<_Key,_Compare,_Alloc> > :
  353. _STLP_PRIV __move_traits_aux<typename multiset<_Key,_Compare,_Alloc>::_Rep_type>
  354. {};
  355. #endif
  356. _STLP_END_NAMESPACE
  357. #endif /* _STLP_INTERNAL_SET_H */
  358. // Local Variables:
  359. // mode:C++
  360. // End: