_map.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  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_MAP_H
  29. #define _STLP_INTERNAL_MAP_H
  30. #ifndef _STLP_INTERNAL_TREE_H
  31. # include <stl/_tree.h>
  32. #endif
  33. _STLP_BEGIN_NAMESPACE
  34. //Specific iterator traits creation
  35. _STLP_CREATE_ITERATOR_TRAITS(MapTraitsT, traits)
  36. template <class _Key, class _Tp, _STLP_DFL_TMPL_PARAM(_Compare, less<_Key> ),
  37. _STLP_DEFAULT_PAIR_ALLOCATOR_SELECT(_STLP_CONST _Key, _Tp) >
  38. class map
  39. #if defined (_STLP_USE_PARTIAL_SPEC_WORKAROUND)
  40. : public __stlport_class<map<_Key, _Tp, _Compare, _Alloc> >
  41. #endif
  42. {
  43. typedef map<_Key, _Tp, _Compare, _Alloc> _Self;
  44. public:
  45. // typedefs:
  46. typedef _Key key_type;
  47. typedef _Tp data_type;
  48. typedef _Tp mapped_type;
  49. typedef pair<_STLP_CONST _Key, _Tp> value_type;
  50. typedef _Compare key_compare;
  51. class value_compare
  52. : public binary_function<value_type, value_type, bool> {
  53. friend class map<_Key,_Tp,_Compare,_Alloc>;
  54. protected :
  55. //c is a Standard name (23.3.1), do no make it STLport naming convention compliant.
  56. _Compare comp;
  57. value_compare(_Compare __c) : comp(__c) {}
  58. public:
  59. bool operator()(const value_type& __x, const value_type& __y) const
  60. { return comp(__x.first, __y.first); }
  61. };
  62. protected:
  63. typedef _STLP_PRIV _MapTraitsT<value_type> _MapTraits;
  64. public:
  65. //Following typedef have to be public for __move_traits specialization.
  66. typedef _STLP_PRIV _Rb_tree<key_type, key_compare,
  67. value_type, _STLP_SELECT1ST(value_type, _Key),
  68. _MapTraits, _Alloc> _Rep_type;
  69. typedef typename _Rep_type::pointer pointer;
  70. typedef typename _Rep_type::const_pointer const_pointer;
  71. typedef typename _Rep_type::reference reference;
  72. typedef typename _Rep_type::const_reference const_reference;
  73. typedef typename _Rep_type::iterator iterator;
  74. typedef typename _Rep_type::const_iterator const_iterator;
  75. typedef typename _Rep_type::reverse_iterator reverse_iterator;
  76. typedef typename _Rep_type::const_reverse_iterator const_reverse_iterator;
  77. typedef typename _Rep_type::size_type size_type;
  78. typedef typename _Rep_type::difference_type difference_type;
  79. typedef typename _Rep_type::allocator_type allocator_type;
  80. private:
  81. _Rep_type _M_t; // red-black tree representing map
  82. _STLP_KEY_TYPE_FOR_CONT_EXT(key_type)
  83. public:
  84. // allocation/deallocation
  85. map() : _M_t(_Compare(), allocator_type()) {}
  86. #if !defined (_STLP_DONT_SUP_DFLT_PARAM)
  87. explicit map(const _Compare& __comp,
  88. const allocator_type& __a = allocator_type())
  89. #else
  90. explicit map(const _Compare& __comp)
  91. : _M_t(__comp, allocator_type()) {}
  92. explicit map(const _Compare& __comp, const allocator_type& __a)
  93. #endif
  94. : _M_t(__comp, __a) {}
  95. #if defined (_STLP_MEMBER_TEMPLATES)
  96. template <class _InputIterator>
  97. map(_InputIterator __first, _InputIterator __last)
  98. : _M_t(_Compare(), allocator_type())
  99. { _M_t.insert_unique(__first, __last); }
  100. template <class _InputIterator>
  101. map(_InputIterator __first, _InputIterator __last, const _Compare& __comp,
  102. const allocator_type& __a _STLP_ALLOCATOR_TYPE_DFL)
  103. : _M_t(__comp, __a) { _M_t.insert_unique(__first, __last); }
  104. # if defined (_STLP_NEEDS_EXTRA_TEMPLATE_CONSTRUCTORS)
  105. template <class _InputIterator>
  106. map(_InputIterator __first, _InputIterator __last, const _Compare& __comp)
  107. : _M_t(__comp, allocator_type()) { _M_t.insert_unique(__first, __last); }
  108. # endif
  109. #else
  110. map(const value_type* __first, const value_type* __last)
  111. : _M_t(_Compare(), allocator_type())
  112. { _M_t.insert_unique(__first, __last); }
  113. map(const value_type* __first,
  114. const value_type* __last, const _Compare& __comp,
  115. const allocator_type& __a = allocator_type())
  116. : _M_t(__comp, __a) { _M_t.insert_unique(__first, __last); }
  117. map(const_iterator __first, const_iterator __last)
  118. : _M_t(_Compare(), allocator_type())
  119. { _M_t.insert_unique(__first, __last); }
  120. map(const_iterator __first, const_iterator __last, const _Compare& __comp,
  121. const allocator_type& __a = allocator_type())
  122. : _M_t(__comp, __a) { _M_t.insert_unique(__first, __last); }
  123. #endif /* _STLP_MEMBER_TEMPLATES */
  124. map(const _Self& __x) : _M_t(__x._M_t) {}
  125. #if !defined (_STLP_NO_MOVE_SEMANTIC)
  126. map(__move_source<_Self> src)
  127. : _M_t(__move_source<_Rep_type>(src.get()._M_t)) {}
  128. #endif
  129. _Self& operator=(const _Self& __x) {
  130. _M_t = __x._M_t;
  131. return *this;
  132. }
  133. // accessors:
  134. key_compare key_comp() const { return _M_t.key_comp(); }
  135. value_compare value_comp() const { return value_compare(_M_t.key_comp()); }
  136. allocator_type get_allocator() const { return _M_t.get_allocator(); }
  137. iterator begin() { return _M_t.begin(); }
  138. const_iterator begin() const { return _M_t.begin(); }
  139. iterator end() { return _M_t.end(); }
  140. const_iterator end() const { return _M_t.end(); }
  141. reverse_iterator rbegin() { return _M_t.rbegin(); }
  142. const_reverse_iterator rbegin() const { return _M_t.rbegin(); }
  143. reverse_iterator rend() { return _M_t.rend(); }
  144. const_reverse_iterator rend() const { return _M_t.rend(); }
  145. bool empty() const { return _M_t.empty(); }
  146. size_type size() const { return _M_t.size(); }
  147. size_type max_size() const { return _M_t.max_size(); }
  148. _STLP_TEMPLATE_FOR_CONT_EXT
  149. _Tp& operator[](const _KT& __k) {
  150. iterator __i = lower_bound(__k);
  151. // __i->first is greater than or equivalent to __k.
  152. if (__i == end() || key_comp()(__k, (*__i).first))
  153. __i = insert(__i, value_type(__k, _STLP_DEFAULT_CONSTRUCTED(_Tp)));
  154. return (*__i).second;
  155. }
  156. void swap(_Self& __x) { _M_t.swap(__x._M_t); }
  157. #if defined (_STLP_USE_PARTIAL_SPEC_WORKAROUND) && !defined (_STLP_FUNCTION_TMPL_PARTIAL_ORDER)
  158. void _M_swap_workaround(_Self& __x) { swap(__x); }
  159. #endif
  160. // insert/erase
  161. pair<iterator,bool> insert(const value_type& __x)
  162. { return _M_t.insert_unique(__x); }
  163. iterator insert(iterator __pos, const value_type& __x)
  164. { return _M_t.insert_unique(__pos, __x); }
  165. #ifdef _STLP_MEMBER_TEMPLATES
  166. template <class _InputIterator>
  167. void insert(_InputIterator __first, _InputIterator __last)
  168. { _M_t.insert_unique(__first, __last); }
  169. #else
  170. void insert(const value_type* __first, const value_type* __last)
  171. { _M_t.insert_unique(__first, __last); }
  172. void insert(const_iterator __first, const_iterator __last)
  173. { _M_t.insert_unique(__first, __last); }
  174. #endif /* _STLP_MEMBER_TEMPLATES */
  175. void erase(iterator __pos) { _M_t.erase(__pos); }
  176. size_type erase(const key_type& __x) { return _M_t.erase_unique(__x); }
  177. void erase(iterator __first, iterator __last) { _M_t.erase(__first, __last); }
  178. void clear() { _M_t.clear(); }
  179. // map operations:
  180. _STLP_TEMPLATE_FOR_CONT_EXT
  181. iterator find(const _KT& __x) { return _M_t.find(__x); }
  182. _STLP_TEMPLATE_FOR_CONT_EXT
  183. const_iterator find(const _KT& __x) const { return _M_t.find(__x); }
  184. _STLP_TEMPLATE_FOR_CONT_EXT
  185. size_type count(const _KT& __x) const { return _M_t.find(__x) == _M_t.end() ? 0 : 1; }
  186. _STLP_TEMPLATE_FOR_CONT_EXT
  187. iterator lower_bound(const _KT& __x) { return _M_t.lower_bound(__x); }
  188. _STLP_TEMPLATE_FOR_CONT_EXT
  189. const_iterator lower_bound(const _KT& __x) const { return _M_t.lower_bound(__x); }
  190. _STLP_TEMPLATE_FOR_CONT_EXT
  191. iterator upper_bound(const _KT& __x) { return _M_t.upper_bound(__x); }
  192. _STLP_TEMPLATE_FOR_CONT_EXT
  193. const_iterator upper_bound(const _KT& __x) const { return _M_t.upper_bound(__x); }
  194. _STLP_TEMPLATE_FOR_CONT_EXT
  195. pair<iterator,iterator> equal_range(const _KT& __x)
  196. { return _M_t.equal_range_unique(__x); }
  197. _STLP_TEMPLATE_FOR_CONT_EXT
  198. pair<const_iterator,const_iterator> equal_range(const _KT& __x) const
  199. { return _M_t.equal_range_unique(__x); }
  200. };
  201. //Specific iterator traits creation
  202. _STLP_CREATE_ITERATOR_TRAITS(MultimapTraitsT, traits)
  203. template <class _Key, class _Tp, _STLP_DFL_TMPL_PARAM(_Compare, less<_Key> ),
  204. _STLP_DEFAULT_PAIR_ALLOCATOR_SELECT(_STLP_CONST _Key, _Tp) >
  205. class multimap
  206. #if defined (_STLP_USE_PARTIAL_SPEC_WORKAROUND)
  207. : public __stlport_class<multimap<_Key, _Tp, _Compare, _Alloc> >
  208. #endif
  209. {
  210. typedef multimap<_Key, _Tp, _Compare, _Alloc> _Self;
  211. public:
  212. // typedefs:
  213. typedef _Key key_type;
  214. typedef _Tp data_type;
  215. typedef _Tp mapped_type;
  216. typedef pair<_STLP_CONST _Key, _Tp> value_type;
  217. typedef _Compare key_compare;
  218. class value_compare : public binary_function<value_type, value_type, bool> {
  219. friend class multimap<_Key,_Tp,_Compare,_Alloc>;
  220. protected:
  221. //comp is a Standard name (23.3.2), do no make it STLport naming convention compliant.
  222. _Compare comp;
  223. value_compare(_Compare __c) : comp(__c) {}
  224. public:
  225. bool operator()(const value_type& __x, const value_type& __y) const
  226. { return comp(__x.first, __y.first); }
  227. };
  228. protected:
  229. //Specific iterator traits creation
  230. typedef _STLP_PRIV _MultimapTraitsT<value_type> _MultimapTraits;
  231. public:
  232. //Following typedef have to be public for __move_traits specialization.
  233. typedef _STLP_PRIV _Rb_tree<key_type, key_compare,
  234. value_type, _STLP_SELECT1ST(value_type, _Key),
  235. _MultimapTraits, _Alloc> _Rep_type;
  236. typedef typename _Rep_type::pointer pointer;
  237. typedef typename _Rep_type::const_pointer const_pointer;
  238. typedef typename _Rep_type::reference reference;
  239. typedef typename _Rep_type::const_reference const_reference;
  240. typedef typename _Rep_type::iterator iterator;
  241. typedef typename _Rep_type::const_iterator const_iterator;
  242. typedef typename _Rep_type::reverse_iterator reverse_iterator;
  243. typedef typename _Rep_type::const_reverse_iterator const_reverse_iterator;
  244. typedef typename _Rep_type::size_type size_type;
  245. typedef typename _Rep_type::difference_type difference_type;
  246. typedef typename _Rep_type::allocator_type allocator_type;
  247. private:
  248. _Rep_type _M_t; // red-black tree representing multimap
  249. _STLP_KEY_TYPE_FOR_CONT_EXT(key_type)
  250. public:
  251. // allocation/deallocation
  252. multimap() : _M_t(_Compare(), allocator_type()) { }
  253. explicit multimap(const _Compare& __comp,
  254. const allocator_type& __a = allocator_type())
  255. : _M_t(__comp, __a) { }
  256. #ifdef _STLP_MEMBER_TEMPLATES
  257. template <class _InputIterator>
  258. multimap(_InputIterator __first, _InputIterator __last)
  259. : _M_t(_Compare(), allocator_type())
  260. { _M_t.insert_equal(__first, __last); }
  261. # ifdef _STLP_NEEDS_EXTRA_TEMPLATE_CONSTRUCTORS
  262. template <class _InputIterator>
  263. multimap(_InputIterator __first, _InputIterator __last,
  264. const _Compare& __comp)
  265. : _M_t(__comp, allocator_type()) { _M_t.insert_equal(__first, __last); }
  266. # endif
  267. template <class _InputIterator>
  268. multimap(_InputIterator __first, _InputIterator __last,
  269. const _Compare& __comp,
  270. const allocator_type& __a _STLP_ALLOCATOR_TYPE_DFL)
  271. : _M_t(__comp, __a) { _M_t.insert_equal(__first, __last); }
  272. #else
  273. multimap(const value_type* __first, const value_type* __last)
  274. : _M_t(_Compare(), allocator_type())
  275. { _M_t.insert_equal(__first, __last); }
  276. multimap(const value_type* __first, const value_type* __last,
  277. const _Compare& __comp,
  278. const allocator_type& __a = allocator_type())
  279. : _M_t(__comp, __a) { _M_t.insert_equal(__first, __last); }
  280. multimap(const_iterator __first, const_iterator __last)
  281. : _M_t(_Compare(), allocator_type())
  282. { _M_t.insert_equal(__first, __last); }
  283. multimap(const_iterator __first, const_iterator __last,
  284. const _Compare& __comp,
  285. const allocator_type& __a = allocator_type())
  286. : _M_t(__comp, __a) { _M_t.insert_equal(__first, __last); }
  287. #endif /* _STLP_MEMBER_TEMPLATES */
  288. multimap(const _Self& __x) : _M_t(__x._M_t) {}
  289. #if !defined (_STLP_NO_MOVE_SEMANTIC)
  290. multimap(__move_source<_Self> src)
  291. : _M_t(__move_source<_Rep_type>(src.get()._M_t)) {}
  292. #endif
  293. _Self& operator=(const _Self& __x) {
  294. _M_t = __x._M_t;
  295. return *this;
  296. }
  297. // accessors:
  298. key_compare key_comp() const { return _M_t.key_comp(); }
  299. value_compare value_comp() const { return value_compare(_M_t.key_comp()); }
  300. allocator_type get_allocator() const { return _M_t.get_allocator(); }
  301. iterator begin() { return _M_t.begin(); }
  302. const_iterator begin() const { return _M_t.begin(); }
  303. iterator end() { return _M_t.end(); }
  304. const_iterator end() const { return _M_t.end(); }
  305. reverse_iterator rbegin() { return _M_t.rbegin(); }
  306. const_reverse_iterator rbegin() const { return _M_t.rbegin(); }
  307. reverse_iterator rend() { return _M_t.rend(); }
  308. const_reverse_iterator rend() const { return _M_t.rend(); }
  309. bool empty() const { return _M_t.empty(); }
  310. size_type size() const { return _M_t.size(); }
  311. size_type max_size() const { return _M_t.max_size(); }
  312. void swap(_Self& __x) { _M_t.swap(__x._M_t); }
  313. #if defined (_STLP_USE_PARTIAL_SPEC_WORKAROUND) && !defined (_STLP_FUNCTION_TMPL_PARTIAL_ORDER)
  314. void _M_swap_workaround(_Self& __x) { swap(__x); }
  315. #endif
  316. // insert/erase
  317. iterator insert(const value_type& __x) { return _M_t.insert_equal(__x); }
  318. iterator insert(iterator __pos, const value_type& __x) { return _M_t.insert_equal(__pos, __x); }
  319. #if defined (_STLP_MEMBER_TEMPLATES)
  320. template <class _InputIterator>
  321. void insert(_InputIterator __first, _InputIterator __last)
  322. { _M_t.insert_equal(__first, __last); }
  323. #else
  324. void insert(const value_type* __first, const value_type* __last)
  325. { _M_t.insert_equal(__first, __last); }
  326. void insert(const_iterator __first, const_iterator __last)
  327. { _M_t.insert_equal(__first, __last); }
  328. #endif /* _STLP_MEMBER_TEMPLATES */
  329. void erase(iterator __pos) { _M_t.erase(__pos); }
  330. size_type erase(const key_type& __x) { return _M_t.erase(__x); }
  331. void erase(iterator __first, iterator __last) { _M_t.erase(__first, __last); }
  332. void clear() { _M_t.clear(); }
  333. // multimap operations:
  334. _STLP_TEMPLATE_FOR_CONT_EXT
  335. iterator find(const _KT& __x) { return _M_t.find(__x); }
  336. _STLP_TEMPLATE_FOR_CONT_EXT
  337. const_iterator find(const _KT& __x) const { return _M_t.find(__x); }
  338. _STLP_TEMPLATE_FOR_CONT_EXT
  339. size_type count(const _KT& __x) const { return _M_t.count(__x); }
  340. _STLP_TEMPLATE_FOR_CONT_EXT
  341. iterator lower_bound(const _KT& __x) { return _M_t.lower_bound(__x); }
  342. _STLP_TEMPLATE_FOR_CONT_EXT
  343. const_iterator lower_bound(const _KT& __x) const { return _M_t.lower_bound(__x); }
  344. _STLP_TEMPLATE_FOR_CONT_EXT
  345. iterator upper_bound(const _KT& __x) { return _M_t.upper_bound(__x); }
  346. _STLP_TEMPLATE_FOR_CONT_EXT
  347. const_iterator upper_bound(const _KT& __x) const { return _M_t.upper_bound(__x); }
  348. _STLP_TEMPLATE_FOR_CONT_EXT
  349. pair<iterator,iterator> equal_range(const _KT& __x)
  350. { return _M_t.equal_range(__x); }
  351. _STLP_TEMPLATE_FOR_CONT_EXT
  352. pair<const_iterator,const_iterator> equal_range(const _KT& __x) const
  353. { return _M_t.equal_range(__x); }
  354. };
  355. #define _STLP_TEMPLATE_HEADER template <class _Key, class _Tp, class _Compare, class _Alloc>
  356. #define _STLP_TEMPLATE_CONTAINER map<_Key,_Tp,_Compare,_Alloc>
  357. #include <stl/_relops_cont.h>
  358. #undef _STLP_TEMPLATE_CONTAINER
  359. #define _STLP_TEMPLATE_CONTAINER multimap<_Key,_Tp,_Compare,_Alloc>
  360. #include <stl/_relops_cont.h>
  361. #undef _STLP_TEMPLATE_CONTAINER
  362. #undef _STLP_TEMPLATE_HEADER
  363. #if defined (_STLP_CLASS_PARTIAL_SPECIALIZATION) && !defined (_STLP_NO_MOVE_SEMANTIC)
  364. template <class _Key, class _Tp, class _Compare, class _Alloc>
  365. struct __move_traits<map<_Key,_Tp,_Compare,_Alloc> > :
  366. _STLP_PRIV __move_traits_aux<typename map<_Key,_Tp,_Compare,_Alloc>::_Rep_type>
  367. {};
  368. template <class _Key, class _Tp, class _Compare, class _Alloc>
  369. struct __move_traits<multimap<_Key,_Tp,_Compare,_Alloc> > :
  370. _STLP_PRIV __move_traits_aux<typename multimap<_Key,_Tp,_Compare,_Alloc>::_Rep_type>
  371. {};
  372. #endif
  373. _STLP_END_NAMESPACE
  374. #endif /* _STLP_INTERNAL_MAP_H */
  375. // Local Variables:
  376. // mode:C++
  377. // End: