_vector.h 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /*
  2. * Copyright (c) 2003
  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_VECTOR_H
  19. #define _STLP_SPECIALIZED_VECTOR_H
  20. #ifndef _STLP_POINTERS_SPEC_TOOLS_H
  21. # include <stl/pointers/_tools.h>
  22. #endif
  23. _STLP_BEGIN_NAMESPACE
  24. #define VECTOR_IMPL _STLP_PTR_IMPL_NAME(vector)
  25. #if defined (_STLP_USE_TEMPLATE_EXPORT) && !defined (_STLP_USE_MSVC6_MEM_T_BUG_WORKAROUND)
  26. _STLP_EXPORT_TEMPLATE_CLASS _STLP_PRIV _Vector_base<void*,allocator<void*> >;
  27. _STLP_EXPORT_TEMPLATE_CLASS _STLP_PRIV VECTOR_IMPL<void*, allocator<void*> >;
  28. #endif
  29. #if defined (_STLP_DEBUG)
  30. # define vector _STLP_NON_DBG_NAME(vector)
  31. _STLP_MOVE_TO_PRIV_NAMESPACE
  32. #endif
  33. template <class _Tp, _STLP_DFL_TMPL_PARAM(_Alloc, allocator<_Tp>) >
  34. class vector
  35. #if defined (_STLP_USE_PARTIAL_SPEC_WORKAROUND) && !defined (vector)
  36. : public __stlport_class<vector<_Tp, _Alloc> >
  37. #endif
  38. {
  39. /* In the vector implementation iterators are pointer which give a number
  40. * of opportunities for optimization. To not break those optimizations
  41. * iterators passed to template should not be wrapped for casting purpose.
  42. * So vector implementation will always use a qualified void pointer type and
  43. * won't use iterator wrapping.
  44. */
  45. typedef _STLP_TYPENAME _STLP_PRIV _StorageType<_Tp>::_QualifiedType _StorageType;
  46. typedef typename _Alloc_traits<_StorageType, _Alloc>::allocator_type _StorageTypeAlloc;
  47. typedef _STLP_PRIV VECTOR_IMPL<_StorageType, _StorageTypeAlloc> _Base;
  48. typedef vector<_Tp, _Alloc> _Self;
  49. typedef _STLP_PRIV _CastTraits<_StorageType, _Tp> cast_traits;
  50. public:
  51. typedef _Tp value_type;
  52. typedef value_type* pointer;
  53. typedef const value_type* const_pointer;
  54. typedef value_type* iterator;
  55. typedef const value_type* const_iterator;
  56. typedef value_type& reference;
  57. typedef const value_type& const_reference;
  58. typedef size_t size_type;
  59. typedef ptrdiff_t difference_type;
  60. typedef random_access_iterator_tag _Iterator_category;
  61. _STLP_DECLARE_RANDOM_ACCESS_REVERSE_ITERATORS;
  62. _STLP_FORCE_ALLOCATORS(value_type, _Alloc)
  63. typedef typename _Alloc_traits<value_type, _Alloc>::allocator_type allocator_type;
  64. allocator_type get_allocator() const
  65. { return _STLP_CONVERT_ALLOCATOR(_M_impl.get_allocator(), value_type); }
  66. iterator begin() { return cast_traits::to_value_type_ptr(_M_impl.begin()); }
  67. const_iterator begin() const { return cast_traits::to_value_type_cptr(_M_impl.begin()); }
  68. iterator end() { return cast_traits::to_value_type_ptr(_M_impl.end()); }
  69. const_iterator end() const { return cast_traits::to_value_type_cptr(_M_impl.end()); }
  70. reverse_iterator rbegin() { return reverse_iterator(end()); }
  71. const_reverse_iterator rbegin() const { return const_reverse_iterator(end()); }
  72. reverse_iterator rend() { return reverse_iterator(begin()); }
  73. const_reverse_iterator rend() const { return const_reverse_iterator(begin()); }
  74. size_type size() const { return _M_impl.size(); }
  75. size_type max_size() const { return _M_impl.max_size(); }
  76. size_type capacity() const { return _M_impl.capacity(); }
  77. bool empty() const { return _M_impl.empty(); }
  78. reference operator[](size_type __n) { return cast_traits::to_value_type_ref(_M_impl[__n]); }
  79. const_reference operator[](size_type __n) const { return cast_traits::to_value_type_cref(_M_impl[__n]); }
  80. reference front() { return cast_traits::to_value_type_ref(_M_impl.front()); }
  81. const_reference front() const { return cast_traits::to_value_type_cref(_M_impl.front()); }
  82. reference back() { return cast_traits::to_value_type_ref(_M_impl.back()); }
  83. const_reference back() const { return cast_traits::to_value_type_cref(_M_impl.back()); }
  84. reference at(size_type __n) { return cast_traits::to_value_type_ref(_M_impl.at(__n)); }
  85. const_reference at(size_type __n) const { return cast_traits::to_value_type_cref(_M_impl.at(__n)); }
  86. explicit vector(const allocator_type& __a = allocator_type())
  87. : _M_impl(_STLP_CONVERT_ALLOCATOR(__a, _StorageType)) {}
  88. #if !defined(_STLP_DONT_SUP_DFLT_PARAM)
  89. explicit vector(size_type __n, const value_type& __val = _STLP_DEFAULT_CONSTRUCTED(value_type),
  90. #else
  91. vector(size_type __n, const value_type& __val,
  92. #endif /*_STLP_DONT_SUP_DFLT_PARAM*/
  93. const allocator_type& __a = allocator_type())
  94. : _M_impl(__n, cast_traits::to_storage_type_cref(__val),
  95. _STLP_CONVERT_ALLOCATOR(__a, _StorageType)) {}
  96. #if defined(_STLP_DONT_SUP_DFLT_PARAM)
  97. explicit vector(size_type __n)
  98. : _M_impl(__n, allocator_type() ) {}
  99. #endif
  100. vector(const _Self& __x)
  101. : _M_impl(__x._M_impl) {}
  102. #if !defined (_STLP_NO_MOVE_SEMANTIC)
  103. explicit vector(__move_source<_Self> src)
  104. : _M_impl(__move_source<_Base>(src.get()._M_impl)) {}
  105. #endif
  106. #if defined (_STLP_MEMBER_TEMPLATES)
  107. template <class _InputIterator>
  108. vector(_InputIterator __first, _InputIterator __last,
  109. const allocator_type& __a _STLP_ALLOCATOR_TYPE_DFL )
  110. : _M_impl(__first, __last,
  111. _STLP_CONVERT_ALLOCATOR(__a, _StorageType)) {}
  112. # if defined (_STLP_NEEDS_EXTRA_TEMPLATE_CONSTRUCTORS)
  113. template <class _InputIterator>
  114. vector(_InputIterator __first, _InputIterator __last)
  115. : _M_impl(__first, __last) {}
  116. # endif
  117. #else
  118. vector(const_iterator __first, const_iterator __last,
  119. const allocator_type& __a = allocator_type())
  120. : _M_impl(cast_traits::to_storage_type_cptr(__first), cast_traits::to_storage_type_cptr(__last),
  121. _STLP_CONVERT_ALLOCATOR(__a, _StorageType)) {}
  122. #endif /* _STLP_MEMBER_TEMPLATES */
  123. _Self& operator=(const _Self& __x) { _M_impl = __x._M_impl; return *this; }
  124. void reserve(size_type __n) {_M_impl.reserve(__n);}
  125. void assign(size_type __n, const value_type& __val)
  126. { _M_impl.assign(__n, cast_traits::to_storage_type_cref(__val)); }
  127. #if defined (_STLP_MEMBER_TEMPLATES)
  128. template <class _InputIterator>
  129. void assign(_InputIterator __first, _InputIterator __last)
  130. { _M_impl.assign(__first, __last); }
  131. #else
  132. void assign(const_iterator __first, const_iterator __last) {
  133. _M_impl.assign(cast_traits::to_storage_type_cptr(__first),
  134. cast_traits::to_storage_type_cptr(__last));
  135. }
  136. #endif /* _STLP_MEMBER_TEMPLATES */
  137. #if !defined(_STLP_DONT_SUP_DFLT_PARAM) && !defined(_STLP_NO_ANACHRONISMS)
  138. void push_back(const value_type& __x = _STLP_DEFAULT_CONSTRUCTED(value_type))
  139. #else
  140. void push_back(const value_type& __x)
  141. #endif /*!_STLP_DONT_SUP_DFLT_PARAM && !_STLP_NO_ANACHRONISMS*/
  142. { _M_impl.push_back(cast_traits::to_storage_type_cref(__x)); }
  143. #if !defined(_STLP_DONT_SUP_DFLT_PARAM) && !defined(_STLP_NO_ANACHRONISMS)
  144. iterator insert(iterator __pos, const value_type& __x = _STLP_DEFAULT_CONSTRUCTED(value_type))
  145. #else
  146. iterator insert(iterator __pos, const value_type& __x)
  147. #endif /*!_STLP_DONT_SUP_DFLT_PARAM && !_STLP_NO_ANACHRONISMS*/
  148. { return cast_traits::to_value_type_ptr(_M_impl.insert(cast_traits::to_storage_type_ptr(__pos),
  149. cast_traits::to_storage_type_cref(__x))); }
  150. #if defined(_STLP_DONT_SUP_DFLT_PARAM) && !defined(_STLP_NO_ANACHRONISMS)
  151. void push_back() { _M_impl.push_back(); }
  152. iterator insert(iterator __pos)
  153. { return _M_impl.insert(cast_traits::to_storage_type_ptr(__pos)); }
  154. #endif /*_STLP_DONT_SUP_DFLT_PARAM && !_STLP_NO_ANACHRONISMS*/
  155. void swap(_Self& __x) { _M_impl.swap(__x._M_impl); }
  156. #if defined (_STLP_USE_PARTIAL_SPEC_WORKAROUND) && !defined (_STLP_FUNCTION_TMPL_PARTIAL_ORDER)
  157. void _M_swap_workaround(_Self& __x) { swap(__x); }
  158. #endif
  159. #if defined (_STLP_MEMBER_TEMPLATES)
  160. template <class _InputIterator>
  161. void insert(iterator __pos, _InputIterator __first, _InputIterator __last)
  162. { _M_impl.insert(cast_traits::to_storage_type_ptr(__pos), __first, __last); }
  163. #else
  164. void insert(iterator __pos, const_iterator __first, const_iterator __last) {
  165. _M_impl.insert(cast_traits::to_storage_type_ptr(__pos), cast_traits::to_storage_type_cptr(__first),
  166. cast_traits::to_storage_type_cptr(__last));
  167. }
  168. #endif
  169. void insert (iterator __pos, size_type __n, const value_type& __x) {
  170. _M_impl.insert(cast_traits::to_storage_type_ptr(__pos), __n, cast_traits::to_storage_type_cref(__x));
  171. }
  172. void pop_back() {_M_impl.pop_back();}
  173. iterator erase(iterator __pos)
  174. {return cast_traits::to_value_type_ptr(_M_impl.erase(cast_traits::to_storage_type_ptr(__pos)));}
  175. iterator erase(iterator __first, iterator __last) {
  176. return cast_traits::to_value_type_ptr(_M_impl.erase(cast_traits::to_storage_type_ptr(__first),
  177. cast_traits::to_storage_type_ptr(__last)));
  178. }
  179. #if !defined(_STLP_DONT_SUP_DFLT_PARAM)
  180. void resize(size_type __new_size, const value_type& __x = _STLP_DEFAULT_CONSTRUCTED(value_type))
  181. #else
  182. void resize(size_type __new_size, const value_type& __x)
  183. #endif /*_STLP_DONT_SUP_DFLT_PARAM*/
  184. { _M_impl.resize(__new_size, cast_traits::to_storage_type_cref(__x)); }
  185. #if defined(_STLP_DONT_SUP_DFLT_PARAM)
  186. void resize(size_type __new_size) { _M_impl.resize(__new_size); }
  187. #endif /*_STLP_DONT_SUP_DFLT_PARAM*/
  188. void clear() { _M_impl.clear(); }
  189. private:
  190. _Base _M_impl;
  191. };
  192. #if defined (vector)
  193. # undef vector
  194. _STLP_MOVE_TO_STD_NAMESPACE
  195. #endif
  196. #undef VECTOR_IMPL
  197. _STLP_END_NAMESPACE
  198. #endif /* _STLP_SPECIALIZED_VECTOR_H */