_deque.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. /*
  2. * Copyright (c) 2004
  3. * Francois Dumont
  4. *
  5. * This material is provided "as is", with absolutely no warranty expressed
  6. * or implied. Any use is at your own risk.
  7. *
  8. * Permission to use or copy this software for any purpose is hereby granted
  9. * without fee, provided the above notices are retained on all copies.
  10. * Permission to modify the code and to distribute modified code is granted,
  11. * provided the above notices are retained, and a notice that the code was
  12. * modified is included with the above copyright notice.
  13. *
  14. */
  15. /* NOTE: This is an internal header file, included by other STL headers.
  16. * You should not attempt to use it directly.
  17. */
  18. #ifndef _STLP_SPECIALIZED_DEQUE_H
  19. #define _STLP_SPECIALIZED_DEQUE_H
  20. #ifndef _STLP_POINTERS_SPEC_TOOLS_H
  21. # include <stl/pointers/_tools.h>
  22. #endif
  23. _STLP_BEGIN_NAMESPACE
  24. _STLP_MOVE_TO_PRIV_NAMESPACE
  25. /*
  26. * struct helper to cast deque iterators:
  27. */
  28. template <class _StorageT, class _ValueT>
  29. struct _DequeIteCast {
  30. typedef _Deque_iterator<_ValueT, _Nonconst_traits<_ValueT> > iterator;
  31. typedef _Deque_iterator<_ValueT, _Const_traits<_ValueT> > const_iterator;
  32. typedef _Deque_iterator<_StorageT, _Nonconst_traits<_StorageT> > storage_iterator;
  33. typedef _Deque_iterator<_StorageT, _Const_traits<_StorageT> > const_storage_iterator;
  34. typedef _CastTraits<_StorageT, _ValueT> cast_traits;
  35. static iterator to_value_type_ite (storage_iterator const& __ite) {
  36. iterator tmp;
  37. tmp._M_cur = cast_traits::to_value_type_ptr(__ite._M_cur);
  38. tmp._M_first = cast_traits::to_value_type_ptr(__ite._M_first);
  39. tmp._M_last = cast_traits::to_value_type_ptr(__ite._M_last);
  40. tmp._M_node = cast_traits::to_value_type_pptr(__ite._M_node);
  41. return tmp;
  42. }
  43. static storage_iterator to_storage_type_ite (iterator const& __ite) {
  44. storage_iterator tmp;
  45. tmp._M_cur = cast_traits::to_storage_type_ptr(__ite._M_cur);
  46. tmp._M_first = cast_traits::to_storage_type_ptr(__ite._M_first);
  47. tmp._M_last = cast_traits::to_storage_type_ptr(__ite._M_last);
  48. tmp._M_node = cast_traits::to_storage_type_pptr(__ite._M_node);
  49. return tmp;
  50. }
  51. static const_iterator to_value_type_cite (const_storage_iterator const& __ite) {
  52. const_iterator tmp;
  53. tmp._M_cur = cast_traits::to_value_type_ptr(__ite._M_cur);
  54. tmp._M_first = cast_traits::to_value_type_ptr(__ite._M_first);
  55. tmp._M_last = cast_traits::to_value_type_ptr(__ite._M_last);
  56. tmp._M_node = cast_traits::to_value_type_pptr(__ite._M_node);
  57. return tmp;
  58. }
  59. static const_storage_iterator to_storage_type_cite (const_iterator const& __ite) {
  60. const_storage_iterator tmp;
  61. tmp._M_cur = cast_traits::to_storage_type_ptr(__ite._M_cur);
  62. tmp._M_first = cast_traits::to_storage_type_ptr(__ite._M_first);
  63. tmp._M_last = cast_traits::to_storage_type_ptr(__ite._M_last);
  64. tmp._M_node = cast_traits::to_storage_type_pptr(__ite._M_node);
  65. return tmp;
  66. }
  67. };
  68. #define DEQUE_IMPL _STLP_PTR_IMPL_NAME(deque)
  69. #if defined (__BORLANDC__) || defined (__DMC__)
  70. # define typename
  71. #endif
  72. #if defined (_STLP_USE_TEMPLATE_EXPORT) && !defined (_STLP_USE_MSVC6_MEM_T_BUG_WORKAROUND)
  73. _STLP_EXPORT_TEMPLATE_CLASS _STLP_alloc_proxy<size_t, void*, allocator<void*> >;
  74. _STLP_EXPORT_TEMPLATE_CLASS _STLP_alloc_proxy<void***, void**, allocator<void**> >;
  75. _STLP_EXPORT template struct _STLP_CLASS_DECLSPEC _Deque_iterator<void*, _Nonconst_traits<void*> >;
  76. _STLP_EXPORT_TEMPLATE_CLASS _Deque_base<void*,allocator<void*> >;
  77. _STLP_EXPORT_TEMPLATE_CLASS DEQUE_IMPL<void*,allocator<void*> >;
  78. #endif
  79. #if defined (_STLP_DEBUG)
  80. # define deque _STLP_NON_DBG_NAME(deque)
  81. #else
  82. _STLP_MOVE_TO_STD_NAMESPACE
  83. #endif
  84. template <class _Tp, _STLP_DFL_TMPL_PARAM(_Alloc, allocator<_Tp>) >
  85. class deque
  86. #if defined (_STLP_USE_PARTIAL_SPEC_WORKAROUND) && !defined (deque)
  87. : public __stlport_class<deque<_Tp, _Alloc> >
  88. #endif
  89. {
  90. typedef _STLP_TYPENAME _STLP_PRIV _StorageType<_Tp>::_Type _StorageType;
  91. typedef typename _Alloc_traits<_StorageType, _Alloc>::allocator_type _StorageTypeAlloc;
  92. typedef _STLP_PRIV DEQUE_IMPL<_StorageType, _StorageTypeAlloc> _Base;
  93. typedef deque<_Tp, _Alloc> _Self;
  94. typedef _STLP_PRIV _CastTraits<_StorageType, _Tp> cast_traits;
  95. typedef _STLP_PRIV _DequeIteCast<_StorageType, _Tp> ite_cast_traits;
  96. public:
  97. typedef _Tp value_type;
  98. typedef value_type* pointer;
  99. typedef const value_type* const_pointer;
  100. typedef value_type& reference;
  101. typedef const value_type& const_reference;
  102. typedef size_t size_type;
  103. typedef ptrdiff_t difference_type;
  104. typedef random_access_iterator_tag _Iterator_category;
  105. _STLP_FORCE_ALLOCATORS(value_type, _Alloc)
  106. typedef typename _Alloc_traits<value_type, _Alloc>::allocator_type allocator_type;
  107. typedef _STLP_PRIV _Deque_iterator<value_type, _Nonconst_traits<value_type> > iterator;
  108. typedef _STLP_PRIV _Deque_iterator<value_type, _Const_traits<value_type> > const_iterator;
  109. _STLP_DECLARE_RANDOM_ACCESS_REVERSE_ITERATORS;
  110. public: // Basic accessors
  111. iterator begin() { return ite_cast_traits::to_value_type_ite(_M_impl.begin()); }
  112. iterator end() { return ite_cast_traits::to_value_type_ite(_M_impl.end()); }
  113. const_iterator begin() const { return ite_cast_traits::to_value_type_cite(_M_impl.begin()); }
  114. const_iterator end() const { return ite_cast_traits::to_value_type_cite(_M_impl.end()); }
  115. reverse_iterator rbegin() { return reverse_iterator(end()); }
  116. reverse_iterator rend() { return reverse_iterator(begin()); }
  117. const_reverse_iterator rbegin() const
  118. { return const_reverse_iterator(end()); }
  119. const_reverse_iterator rend() const
  120. { return const_reverse_iterator(begin()); }
  121. reference operator[](size_type __n)
  122. { return cast_traits::to_value_type_ref(_M_impl[__n]); }
  123. const_reference operator[](size_type __n) const
  124. { return cast_traits::to_value_type_cref(_M_impl[__n]); }
  125. reference at(size_type __n)
  126. { return cast_traits::to_value_type_ref(_M_impl.at(__n)); }
  127. const_reference at(size_type __n) const
  128. { return cast_traits::to_value_type_cref(_M_impl.at(__n)); }
  129. reference front() { return cast_traits::to_value_type_ref(_M_impl.front()); }
  130. reference back() { return cast_traits::to_value_type_ref(_M_impl.back()); }
  131. const_reference front() const { return cast_traits::to_value_type_cref(_M_impl.front()); }
  132. const_reference back() const { return cast_traits::to_value_type_cref(_M_impl.back()); }
  133. size_type size() const { return _M_impl.size(); }
  134. size_type max_size() const { return _M_impl.max_size(); }
  135. bool empty() const { return _M_impl.empty(); }
  136. allocator_type get_allocator() const { return _STLP_CONVERT_ALLOCATOR(_M_impl.get_allocator(), value_type); }
  137. explicit deque(const allocator_type& __a = allocator_type())
  138. : _M_impl(_STLP_CONVERT_ALLOCATOR(__a, _StorageType)) {}
  139. deque(const _Self& __x) : _M_impl(__x._M_impl) {}
  140. #if !defined (_STLP_DONT_SUP_DFLT_PARAM)
  141. explicit deque(size_type __n, const value_type& __val = _STLP_DEFAULT_CONSTRUCTED(value_type),
  142. #else
  143. deque(size_type __n, const value_type& __val,
  144. #endif /*_STLP_DONT_SUP_DFLT_PARAM*/
  145. const allocator_type& __a = allocator_type())
  146. : _M_impl(__n, cast_traits::to_storage_type_cref(__val), _STLP_CONVERT_ALLOCATOR(__a, _StorageType)) {}
  147. // int,long variants may be needed
  148. #if defined (_STLP_DONT_SUP_DFLT_PARAM)
  149. explicit deque(size_type __n) : _M_impl(__n) {}
  150. #endif /*_STLP_DONT_SUP_DFLT_PARAM*/
  151. #if defined (_STLP_MEMBER_TEMPLATES)
  152. template <class _InputIterator>
  153. deque(_InputIterator __first, _InputIterator __last,
  154. const allocator_type& __a _STLP_ALLOCATOR_TYPE_DFL)
  155. #if !defined (_STLP_USE_ITERATOR_WRAPPER)
  156. : _M_impl(__first, __last,
  157. _STLP_CONVERT_ALLOCATOR(__a, _StorageType)) {
  158. #else
  159. : _M_impl(_STLP_CONVERT_ALLOCATOR(__a, _StorageType)) {
  160. #endif
  161. #if defined (_STLP_USE_ITERATOR_WRAPPER)
  162. insert(end(), __first, __last);
  163. #endif
  164. }
  165. # if defined (_STLP_NEEDS_EXTRA_TEMPLATE_CONSTRUCTORS)
  166. template <class _InputIterator>
  167. deque(_InputIterator __first, _InputIterator __last)
  168. # if !defined (_STLP_USE_ITERATOR_WRAPPER)
  169. : _M_impl(__first, __last) {}
  170. # else
  171. { insert(end(), __first, __last); }
  172. # endif
  173. # endif
  174. #else
  175. deque(const_pointer __first, const_pointer __last,
  176. const allocator_type& __a = allocator_type() )
  177. : _M_impl(cast_traits::to_storage_type_cptr(__first),
  178. cast_traits::to_storage_type_cptr(__last),
  179. _STLP_CONVERT_ALLOCATOR(__a, _StorageType)) {}
  180. deque(const_iterator __first, const_iterator __last,
  181. const allocator_type& __a = allocator_type() )
  182. : _M_impl(ite_cast_traits::to_storage_type_cite(__first),
  183. ite_cast_traits::to_storage_type_cite(__last),
  184. _STLP_CONVERT_ALLOCATOR(__a, _StorageType)) {}
  185. #endif /* _STLP_MEMBER_TEMPLATES */
  186. #if !defined (_STLP_NO_MOVE_SEMANTIC)
  187. deque(__move_source<_Self> src)
  188. : _M_impl(__move_source<_Base>(src.get()._M_impl)) {}
  189. #endif
  190. _Self& operator= (const _Self& __x) { _M_impl = __x._M_impl; return *this; }
  191. void swap(_Self& __x) { _M_impl.swap(__x._M_impl); }
  192. #if defined (_STLP_USE_PARTIAL_SPEC_WORKAROUND) && !defined (_STLP_FUNCTION_TMPL_PARTIAL_ORDER)
  193. void _M_swap_workaround(_Self& __x) { swap(__x); }
  194. #endif
  195. void assign(size_type __n, const value_type& __val) {
  196. _M_impl.assign(__n, cast_traits::to_storage_type_cref(__val));
  197. }
  198. #if defined (_STLP_MEMBER_TEMPLATES)
  199. # if defined (_STLP_USE_ITERATOR_WRAPPER)
  200. private:
  201. template <class _Integer>
  202. void _M_assign_dispatch(_Integer __n, _Integer __val,
  203. const __true_type&)
  204. { _M_impl.assign(__n, __val); }
  205. template <class _InputIterator>
  206. void _M_assign_dispatch(_InputIterator __first, _InputIterator __last,
  207. const __false_type&) {
  208. _M_impl.assign(_STLP_TYPENAME _STLP_PRIV _IteWrapper<_StorageType, _Tp, _InputIterator>::_Ite(__first),
  209. _STLP_TYPENAME _STLP_PRIV _IteWrapper<_StorageType, _Tp, _InputIterator>::_Ite(__last));
  210. }
  211. public:
  212. # endif
  213. template <class _InputIterator>
  214. void assign(_InputIterator __first, _InputIterator __last) {
  215. # if defined (_STLP_USE_ITERATOR_WRAPPER)
  216. typedef typename _IsIntegral<_InputIterator>::_Ret _Integral;
  217. _M_assign_dispatch(__first, __last, _Integral());
  218. # else
  219. _M_impl.assign(__first, __last);
  220. # endif
  221. }
  222. #else
  223. void assign(const_pointer __first, const_pointer __last)
  224. { _M_impl.assign(cast_traits::to_storage_type_cptr(__first),
  225. cast_traits::to_storage_type_cptr(__last)); }
  226. void assign(const_iterator __first, const_iterator __last)
  227. { _M_impl.assign(ite_cast_traits::to_storage_type_cite(__first),
  228. ite_cast_traits::to_storage_type_cite(__last)); }
  229. #endif /* _STLP_MEMBER_TEMPLATES */
  230. #if !defined (_STLP_DONT_SUP_DFLT_PARAM) && !defined (_STLP_NO_ANACHRONISMS)
  231. void push_back(const value_type& __t = _STLP_DEFAULT_CONSTRUCTED(value_type))
  232. #else
  233. void push_back(const value_type& __t)
  234. #endif /*!_STLP_DONT_SUP_DFLT_PARAM && !_STLP_NO_ANACHRONISMS*/
  235. { _M_impl.push_back(cast_traits::to_storage_type_cref(__t)); }
  236. #if !defined (_STLP_DONT_SUP_DFLT_PARAM) && !defined (_STLP_NO_ANACHRONISMS)
  237. void push_front(const value_type& __t = _STLP_DEFAULT_CONSTRUCTED(value_type))
  238. #else
  239. void push_front(const value_type& __t)
  240. #endif /*!_STLP_DONT_SUP_DFLT_PARAM && !_STLP_NO_ANACHRONISMS*/
  241. { _M_impl.push_front(cast_traits::to_storage_type_cref(__t)); }
  242. # if defined (_STLP_DONT_SUP_DFLT_PARAM) && !defined (_STLP_NO_ANACHRONISMS)
  243. void push_back() { _M_impl.push_back(); }
  244. void push_front() { _M_impl.push_front(); }
  245. # endif /*_STLP_DONT_SUP_DFLT_PARAM && !_STLP_NO_ANACHRONISMS*/
  246. void pop_back() { _M_impl.pop_back(); }
  247. void pop_front() { _M_impl.pop_front(); }
  248. #if !defined (_STLP_DONT_SUP_DFLT_PARAM) && !defined (_STLP_NO_ANACHRONISMS)
  249. iterator insert(iterator __pos, const value_type& __x = _STLP_DEFAULT_CONSTRUCTED(value_type))
  250. #else
  251. iterator insert(iterator __pos, const value_type& __x)
  252. #endif /*!_STLP_DONT_SUP_DFLT_PARAM && !_STLP_NO_ANACHRONISMS*/
  253. { return ite_cast_traits::to_value_type_ite(_M_impl.insert(ite_cast_traits::to_storage_type_ite(__pos),
  254. cast_traits::to_storage_type_cref(__x))); }
  255. #if defined (_STLP_DONT_SUP_DFLT_PARAM) && !defined (_STLP_NO_ANACHRONISMS)
  256. iterator insert(iterator __pos) { return insert(__pos, _STLP_DEFAULT_CONSTRUCTED(value_type)); }
  257. #endif /*_STLP_DONT_SUP_DFLT_PARAM && !_STLP_NO_ANACHRONISMS*/
  258. void insert(iterator __pos, size_type __n, const value_type& __x)
  259. { _M_impl.insert(ite_cast_traits::to_storage_type_ite(__pos), __n, cast_traits::to_storage_type_cref(__x)); }
  260. #if defined (_STLP_MEMBER_TEMPLATES)
  261. # if defined (_STLP_USE_ITERATOR_WRAPPER)
  262. private:
  263. template <class _Integer>
  264. void _M_insert_dispatch(iterator __pos, _Integer __n, _Integer __val,
  265. const __true_type&) {
  266. _M_impl.insert(ite_cast_traits::to_storage_type_ite(__pos), __n, __val);
  267. }
  268. template <class _InputIterator>
  269. void _M_insert_dispatch(iterator __pos,
  270. _InputIterator __first, _InputIterator __last,
  271. const __false_type&) {
  272. _M_impl.insert(ite_cast_traits::to_storage_type_ite(__pos),
  273. _STLP_TYPENAME _STLP_PRIV _IteWrapper<_StorageType, _Tp, _InputIterator>::_Ite(__first),
  274. _STLP_TYPENAME _STLP_PRIV _IteWrapper<_StorageType, _Tp, _InputIterator>::_Ite(__last));
  275. }
  276. public:
  277. # endif
  278. template <class _InputIterator>
  279. void insert(iterator __pos, _InputIterator __first, _InputIterator __last) {
  280. # if defined (_STLP_USE_ITERATOR_WRAPPER)
  281. // Check whether it's an integral type. If so, it's not an iterator.
  282. typedef typename _IsIntegral<_InputIterator>::_Ret _Integral;
  283. _M_insert_dispatch(__pos, __first, __last, _Integral());
  284. # else
  285. _M_impl.insert(ite_cast_traits::to_storage_type_ite(__pos), __first, __last);
  286. # endif
  287. }
  288. #else /* _STLP_MEMBER_TEMPLATES */
  289. void insert(iterator __pos,
  290. const_pointer __first, const_pointer __last) {
  291. _M_impl.insert(ite_cast_traits::to_storage_type_ite(__pos),
  292. cast_traits::to_storage_type_cptr(__first),
  293. cast_traits::to_storage_type_cptr(__last));
  294. }
  295. void insert(iterator __pos,
  296. const_iterator __first, const_iterator __last) {
  297. _M_impl.insert(ite_cast_traits::to_storage_type_ite(__pos),
  298. ite_cast_traits::to_storage_type_cite(__first),
  299. ite_cast_traits::to_storage_type_cite(__last));
  300. }
  301. #endif /* _STLP_MEMBER_TEMPLATES */
  302. #if !defined (_STLP_DONT_SUP_DFLT_PARAM)
  303. void resize(size_type __new_size, const value_type& __x = _STLP_DEFAULT_CONSTRUCTED(value_type))
  304. #else
  305. void resize(size_type __new_size, const value_type& __x)
  306. #endif /*_STLP_DONT_SUP_DFLT_PARAM*/
  307. { _M_impl.resize(__new_size, cast_traits::to_storage_type_cref(__x)); }
  308. #if defined (_STLP_DONT_SUP_DFLT_PARAM)
  309. void resize(size_type __new_size) { _M_impl.resize(__new_size); }
  310. #endif /*_STLP_DONT_SUP_DFLT_PARAM*/
  311. iterator erase(iterator __pos)
  312. { return ite_cast_traits::to_value_type_ite(_M_impl.erase(ite_cast_traits::to_storage_type_ite(__pos))); }
  313. iterator erase(iterator __first, iterator __last)
  314. { return ite_cast_traits::to_value_type_ite(_M_impl.erase(ite_cast_traits::to_storage_type_ite(__first),
  315. ite_cast_traits::to_storage_type_ite(__last))); }
  316. void clear() { _M_impl.clear(); }
  317. private:
  318. _Base _M_impl;
  319. };
  320. #if defined (deque)
  321. # undef deque
  322. _STLP_MOVE_TO_STD_NAMESPACE
  323. #endif
  324. #undef DEQUE_IMPL
  325. #if defined (__BORLANDC__) || defined (__DMC__)
  326. # undef typename
  327. #endif
  328. _STLP_END_NAMESPACE
  329. #endif /* _STLP_SPECIALIZED_DEQUE_H */
  330. // Local Variables:
  331. // mode:C++
  332. // End: