_vector.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  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_DBG_VECTOR_H
  29. #define _STLP_INTERNAL_DBG_VECTOR_H
  30. #ifndef _STLP_DBG_ITERATOR_H
  31. # include <stl/debug/_iterator.h>
  32. #endif
  33. #define _STLP_NON_DBG_VECTOR _STLP_PRIV _STLP_NON_DBG_NAME(vector) <_Tp, _Alloc>
  34. _STLP_BEGIN_NAMESPACE
  35. #if defined (_STLP_DEBUG_USE_DISTINCT_VALUE_TYPE_HELPERS)
  36. template <class _Tp, class _Alloc>
  37. inline _Tp*
  38. value_type(const _STLP_PRIV _DBG_iter_base< _STLP_NON_DBG_VECTOR >&)
  39. { return (_Tp*)0; }
  40. template <class _Tp, class _Alloc>
  41. inline random_access_iterator_tag
  42. iterator_category(const _STLP_PRIV _DBG_iter_base< _STLP_NON_DBG_VECTOR >&)
  43. { return random_access_iterator_tag(); }
  44. #endif
  45. _STLP_MOVE_TO_PRIV_NAMESPACE
  46. template <class _Tp, class _NcIt>
  47. struct _Vector_const_traits;
  48. template <class _Tp, class _NcIt>
  49. struct _Vector_nonconst_traits {
  50. typedef _Nonconst_traits<_Tp> _BaseT;
  51. typedef _Tp value_type;
  52. typedef _Tp& reference;
  53. typedef _Tp* pointer;
  54. typedef _Vector_const_traits<_Tp, _NcIt> _ConstTraits;
  55. typedef _Vector_nonconst_traits<_Tp, _NcIt> _NonConstTraits;
  56. };
  57. template <class _Tp, class _NcIt>
  58. struct _Vector_const_traits {
  59. typedef _Const_traits<_Tp> _BaseT;
  60. typedef _Tp value_type;
  61. typedef const _Tp& reference;
  62. typedef const _Tp* pointer;
  63. typedef _Vector_const_traits<_Tp, _NcIt> _ConstTraits;
  64. typedef _Vector_nonconst_traits<_Tp, _NcIt> _NonConstTraits;
  65. };
  66. _STLP_TEMPLATE_NULL
  67. struct _Vector_nonconst_traits<bool, _Bit_iterator> {
  68. typedef _Bit_iterator::value_type value_type;
  69. typedef _Bit_iterator::reference reference;
  70. typedef _Bit_iterator::pointer pointer;
  71. typedef _Vector_const_traits<bool, _Bit_iterator> _ConstTraits;
  72. typedef _Vector_nonconst_traits<bool, _Bit_iterator> _NonConstTraits;
  73. };
  74. _STLP_TEMPLATE_NULL
  75. struct _Vector_const_traits<bool, _Bit_iterator> {
  76. typedef _Bit_const_iterator::value_type value_type;
  77. typedef _Bit_const_iterator::reference reference;
  78. typedef _Bit_const_iterator::pointer pointer;
  79. typedef _Vector_const_traits<bool, _Bit_iterator> _ConstTraits;
  80. typedef _Vector_nonconst_traits<bool, _Bit_iterator> _NonConstTraits;
  81. };
  82. _STLP_MOVE_TO_STD_NAMESPACE
  83. template <class _Tp, _STLP_DFL_TMPL_PARAM(_Alloc, allocator<_Tp>) >
  84. class vector :
  85. #if !defined (__DMC__)
  86. private
  87. #endif
  88. _STLP_PRIV __construct_checker< _STLP_NON_DBG_VECTOR >
  89. #if defined (_STLP_USE_PARTIAL_SPEC_WORKAROUND)
  90. , public __stlport_class<vector<_Tp, _Alloc> >
  91. #endif
  92. {
  93. private:
  94. typedef _STLP_NON_DBG_VECTOR _Base;
  95. typedef vector<_Tp, _Alloc> _Self;
  96. typedef _STLP_PRIV __construct_checker<_STLP_NON_DBG_VECTOR > _ConstructCheck;
  97. _Base _M_non_dbg_impl;
  98. _STLP_PRIV __owned_list _M_iter_list;
  99. public:
  100. __IMPORT_CONTAINER_TYPEDEFS(_Base)
  101. typedef _STLP_PRIV _DBG_iter<_Base,
  102. _STLP_PRIV _DbgTraits<_STLP_PRIV _Vector_nonconst_traits<value_type, typename _Base::iterator> > > iterator;
  103. typedef _STLP_PRIV _DBG_iter<_Base,
  104. _STLP_PRIV _DbgTraits<_STLP_PRIV _Vector_const_traits<value_type, typename _Base::iterator> > > const_iterator;
  105. private:
  106. void _Invalidate_all()
  107. { _M_iter_list._Invalidate_all(); }
  108. void _Invalidate_iterator(const iterator& __it)
  109. { _STLP_PRIV __invalidate_iterator(&_M_iter_list, __it); }
  110. void _Invalidate_iterators(const iterator& __first, const iterator& __last)
  111. { _STLP_PRIV __invalidate_range(&_M_iter_list, __first, __last); }
  112. void _Check_Overflow(size_type __nb) {
  113. if (size() + __nb > capacity())
  114. _Invalidate_all();
  115. }
  116. void _Compare_Capacity (size_type __old_capacity) {
  117. if (capacity() > __old_capacity) {
  118. _Invalidate_all();
  119. }
  120. }
  121. public:
  122. _STLP_DECLARE_RANDOM_ACCESS_REVERSE_ITERATORS;
  123. allocator_type get_allocator() const { return _M_non_dbg_impl.get_allocator(); }
  124. iterator begin() { return iterator(&_M_iter_list, _M_non_dbg_impl.begin()); }
  125. const_iterator begin() const { return const_iterator(&_M_iter_list, _M_non_dbg_impl.begin()); }
  126. iterator end() { return iterator(&_M_iter_list, _M_non_dbg_impl.end()); }
  127. const_iterator end() const { return const_iterator(&_M_iter_list, _M_non_dbg_impl.end()); }
  128. reverse_iterator rbegin() { return reverse_iterator(end()); }
  129. const_reverse_iterator rbegin() const { return const_reverse_iterator(end()); }
  130. reverse_iterator rend() { return reverse_iterator(begin()); }
  131. const_reverse_iterator rend() const { return const_reverse_iterator(begin()); }
  132. size_type size() const { return _M_non_dbg_impl.size(); }
  133. size_type max_size() const { return _M_non_dbg_impl.max_size(); }
  134. size_type capacity() const { return _M_non_dbg_impl.capacity(); }
  135. bool empty() const { return _M_non_dbg_impl.empty(); }
  136. reference operator[](size_type __n) {
  137. _STLP_VERBOSE_ASSERT(__n < size(), _StlMsg_OUT_OF_BOUNDS)
  138. return _M_non_dbg_impl[__n];
  139. }
  140. const_reference operator[](size_type __n) const {
  141. _STLP_VERBOSE_ASSERT(__n < size(), _StlMsg_OUT_OF_BOUNDS)
  142. return _M_non_dbg_impl[__n];
  143. }
  144. reference at(size_type __n) { return _M_non_dbg_impl.at(__n); }
  145. const_reference at(size_type __n) const { return _M_non_dbg_impl.at(__n); }
  146. explicit vector(const allocator_type& __a = allocator_type())
  147. : _M_non_dbg_impl(__a), _M_iter_list(&_M_non_dbg_impl) {}
  148. #if !defined(_STLP_DONT_SUP_DFLT_PARAM)
  149. explicit vector(size_type __n, const _Tp& __x = _Tp(),
  150. #else
  151. vector(size_type __n, const _Tp& __x,
  152. #endif
  153. const allocator_type& __a = allocator_type())
  154. : _M_non_dbg_impl(__n, __x, __a), _M_iter_list(&_M_non_dbg_impl) {}
  155. #if defined(_STLP_DONT_SUP_DFLT_PARAM)
  156. explicit vector(size_type __n)
  157. : _M_non_dbg_impl(__n), _M_iter_list(&_M_non_dbg_impl) {}
  158. #endif
  159. vector(const _Self& __x)
  160. : _ConstructCheck(__x), _M_non_dbg_impl(__x._M_non_dbg_impl), _M_iter_list(&_M_non_dbg_impl) {}
  161. #if !defined (_STLP_NO_MOVE_SEMANTIC)
  162. vector(__move_source<_Self> src)
  163. : _M_non_dbg_impl(__move_source<_Base>(src.get()._M_non_dbg_impl)),
  164. _M_iter_list(&_M_non_dbg_impl) {
  165. # if defined (_STLP_NO_EXTENSIONS) || (_STLP_DEBUG_LEVEL == _STLP_STANDARD_DBG_LEVEL)
  166. src.get()._M_iter_list._Invalidate_all();
  167. # else
  168. src.get()._M_iter_list._Set_owner(_M_iter_list);
  169. # endif
  170. }
  171. #endif
  172. #if defined (_STLP_MEMBER_TEMPLATES)
  173. template <class _InputIterator>
  174. vector(_InputIterator __first, _InputIterator __last,
  175. const allocator_type& __a _STLP_ALLOCATOR_TYPE_DFL)
  176. : _ConstructCheck(__first, __last),
  177. _M_non_dbg_impl(_STLP_PRIV _Non_Dbg_iter(__first), _STLP_PRIV _Non_Dbg_iter(__last), __a),
  178. _M_iter_list(&_M_non_dbg_impl) {}
  179. # if defined (_STLP_NEEDS_EXTRA_TEMPLATE_CONSTRUCTORS)
  180. template <class _InputIterator>
  181. vector(_InputIterator __first, _InputIterator __last)
  182. : _ConstructCheck(__first, __last),
  183. _M_non_dbg_impl(_STLP_PRIV _Non_Dbg_iter(__first), _STLP_PRIV _Non_Dbg_iter(__last)),
  184. _M_iter_list(&_M_non_dbg_impl) {}
  185. # endif
  186. #else
  187. vector(const _Tp* __first, const _Tp* __last,
  188. const allocator_type& __a = allocator_type())
  189. : _ConstructCheck(__first, __last), _M_non_dbg_impl(__first, __last, __a),
  190. _M_iter_list(&_M_non_dbg_impl) {}
  191. // mysterious VC++ bug ?
  192. vector(const_iterator __first, const_iterator __last ,
  193. const allocator_type& __a = allocator_type())
  194. : _ConstructCheck(__first, __last),
  195. _M_non_dbg_impl(__first._M_iterator, __last._M_iterator, __a),
  196. _M_iter_list(&_M_non_dbg_impl) {}
  197. #endif /* _STLP_MEMBER_TEMPLATES */
  198. _Self& operator=(const _Self& __x) {
  199. if (this != &__x) {
  200. _Invalidate_all();
  201. _M_non_dbg_impl = __x._M_non_dbg_impl;
  202. }
  203. return *this;
  204. }
  205. void reserve(size_type __n) {
  206. if (capacity() < __n)
  207. _Invalidate_all();
  208. _M_non_dbg_impl.reserve(__n);
  209. }
  210. reference front() {
  211. _STLP_VERBOSE_ASSERT(!empty(), _StlMsg_EMPTY_CONTAINER)
  212. return *begin();
  213. }
  214. const_reference front() const {
  215. _STLP_VERBOSE_ASSERT(!empty(), _StlMsg_EMPTY_CONTAINER)
  216. return *begin();
  217. }
  218. reference back() {
  219. _STLP_VERBOSE_ASSERT(!empty(), _StlMsg_EMPTY_CONTAINER)
  220. return *(--end());
  221. }
  222. const_reference back() const {
  223. _STLP_VERBOSE_ASSERT(!empty(), _StlMsg_EMPTY_CONTAINER)
  224. return *(--end());
  225. }
  226. void swap(_Self& __x) {
  227. _M_iter_list._Swap_owners(__x._M_iter_list);
  228. _M_non_dbg_impl.swap(__x._M_non_dbg_impl);
  229. }
  230. #if defined (_STLP_USE_PARTIAL_SPEC_WORKAROUND) && !defined (_STLP_FUNCTION_TMPL_PARTIAL_ORDER)
  231. void _M_swap_workaround(_Self& __x) { swap(__x); }
  232. #endif
  233. #if !defined(_STLP_DONT_SUP_DFLT_PARAM)
  234. iterator insert(iterator __pos, const _Tp& __x = _Tp()) {
  235. #else
  236. iterator insert(iterator __pos, const _Tp& __x) {
  237. #endif /*_STLP_DONT_SUP_DFLT_PARAM*/
  238. _STLP_DEBUG_CHECK(_STLP_PRIV __check_if_owner(&_M_iter_list, __pos))
  239. _Check_Overflow(1);
  240. return iterator(&_M_iter_list, _M_non_dbg_impl.insert(__pos._M_iterator, __x));
  241. }
  242. #if defined(_STLP_DONT_SUP_DFLT_PARAM)
  243. iterator insert(iterator __pos)
  244. { return insert(__pos, _STLP_DEFAULT_CONSTRUCTED(_Tp)); }
  245. #endif /*_STLP_DONT_SUP_DFLT_PARAM*/
  246. #if defined (_STLP_MEMBER_TEMPLATES)
  247. // Check whether it's an integral type. If so, it's not an iterator.
  248. template <class _InputIterator>
  249. void insert(iterator __pos,
  250. _InputIterator __first, _InputIterator __last) {
  251. _STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__first, __last))
  252. _STLP_DEBUG_CHECK(_STLP_PRIV __check_if_owner(&_M_iter_list, __pos))
  253. size_type __old_capacity = capacity();
  254. _M_non_dbg_impl.insert(__pos._M_iterator,
  255. _STLP_PRIV _Non_Dbg_iter(__first), _STLP_PRIV _Non_Dbg_iter(__last));
  256. _Compare_Capacity(__old_capacity);
  257. }
  258. #endif
  259. #if !defined (_STLP_MEMBER_TEMPLATES)
  260. void insert (iterator __pos,
  261. const value_type *__first, const value_type *__last) {
  262. _STLP_DEBUG_CHECK(_STLP_PRIV __check_ptr_range(__first,__last))
  263. _STLP_DEBUG_CHECK(_STLP_PRIV __check_if_owner(&_M_iter_list, __pos))
  264. size_type __old_capacity = capacity();
  265. _M_non_dbg_impl.insert(__pos._M_iterator, __first, __last);
  266. _Compare_Capacity(__old_capacity);
  267. }
  268. #endif
  269. #if !defined (_STLP_MEMBER_TEMPLATES) || !defined (_STLP_NO_METHOD_SPECIALIZATION)
  270. void insert(iterator __pos,
  271. const_iterator __first, const_iterator __last) {
  272. _STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__first,__last))
  273. _STLP_DEBUG_CHECK(_STLP_PRIV __check_if_owner(&_M_iter_list, __pos))
  274. //Sequence requirements 23.1.1 Table 67:
  275. _STLP_DEBUG_CHECK(_STLP_PRIV __check_if_not_owner(&_M_iter_list, __first));
  276. size_type __old_capacity = capacity();
  277. _M_non_dbg_impl.insert(__pos._M_iterator, __first._M_iterator, __last._M_iterator);
  278. _Compare_Capacity(__old_capacity);
  279. }
  280. void insert(iterator __pos,
  281. iterator __first, iterator __last) {
  282. _STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__first,__last))
  283. _STLP_DEBUG_CHECK(_STLP_PRIV __check_if_owner(&_M_iter_list, __pos))
  284. //Sequence requirements 23.1.1 Table 67:
  285. _STLP_DEBUG_CHECK(_STLP_PRIV __check_if_not_owner(&_M_iter_list, __first));
  286. size_type __old_capacity = capacity();
  287. _M_non_dbg_impl.insert(__pos._M_iterator, __first._M_iterator, __last._M_iterator);
  288. _Compare_Capacity(__old_capacity);
  289. }
  290. #endif
  291. void insert (iterator __pos, size_type __n, const _Tp& __x){
  292. _STLP_DEBUG_CHECK(_STLP_PRIV __check_if_owner(&_M_iter_list, __pos))
  293. _Check_Overflow(__n);
  294. _M_non_dbg_impl.insert(__pos._M_iterator, __n, __x);
  295. }
  296. void pop_back() {
  297. _STLP_VERBOSE_ASSERT(!empty(), _StlMsg_EMPTY_CONTAINER)
  298. _Invalidate_iterator(end());
  299. _M_non_dbg_impl.pop_back();
  300. }
  301. iterator erase(iterator __pos) {
  302. _STLP_DEBUG_CHECK(_STLP_PRIV _Dereferenceable(__pos))
  303. _STLP_DEBUG_CHECK(_STLP_PRIV __check_if_owner(&_M_iter_list, __pos))
  304. _Invalidate_iterators(__pos, end());
  305. return iterator(&_M_iter_list, _M_non_dbg_impl.erase(__pos._M_iterator));
  306. }
  307. iterator erase(iterator __first, iterator __last) {
  308. _STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__first, __last, begin(), end()))
  309. _Invalidate_iterators(__first, end());
  310. return iterator(&_M_iter_list, _M_non_dbg_impl.erase(__first._M_iterator, __last._M_iterator));
  311. }
  312. #if !defined (_STLP_DONT_SUP_DFLT_PARAM)
  313. void resize(size_type __new_size, const _Tp& __x = _STLP_DEFAULT_CONSTRUCTED(_Tp)) {
  314. #else
  315. void resize(size_type __new_size, const _Tp& __x) {
  316. #endif /*_STLP_DONT_SUP_DFLT_PARAM*/
  317. if (__new_size > capacity()) {
  318. _Invalidate_all();
  319. }
  320. else if (__new_size < size()) {
  321. _Invalidate_iterators(begin() + __new_size, end());
  322. }
  323. _M_non_dbg_impl.resize(__new_size, __x);
  324. }
  325. #if defined (_STLP_DONT_SUP_DFLT_PARAM)
  326. void resize(size_type __new_size) { resize(__new_size, _STLP_DEFAULT_CONSTRUCTED(_Tp)); }
  327. #endif
  328. #if defined (_STLP_MEMBER_TEMPLATES)
  329. template <class _InputIterator>
  330. void assign(_InputIterator __first, _InputIterator __last) {
  331. _STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__first,__last))
  332. _Invalidate_all();
  333. _M_non_dbg_impl.assign(_STLP_PRIV _Non_Dbg_iter(__first), _STLP_PRIV _Non_Dbg_iter(__last));
  334. }
  335. #else
  336. private:
  337. void _M_assign(const value_type *__first, const value_type *__last) {
  338. _Invalidate_all();
  339. _M_non_dbg_impl.assign(__first, __last);
  340. }
  341. public:
  342. void assign(const value_type *__first, const value_type *__last) {
  343. _STLP_DEBUG_CHECK(_STLP_PRIV __check_ptr_range(__first,__last))
  344. _M_assign(__first, __last);
  345. }
  346. void assign(const_iterator __first, const_iterator __last) {
  347. _STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__first,__last))
  348. _M_assign(__first._M_iterator, __last._M_iterator);
  349. }
  350. #endif
  351. public:
  352. void assign(size_type __n, const _Tp& __val) {
  353. _Invalidate_all();
  354. _M_non_dbg_impl.assign(__n, __val);
  355. }
  356. void clear() {
  357. _Invalidate_all();
  358. _M_non_dbg_impl.clear();
  359. }
  360. void push_back(const _Tp& __x) {
  361. _Check_Overflow(1);
  362. _M_non_dbg_impl.push_back(__x);
  363. }
  364. };
  365. _STLP_END_NAMESPACE
  366. #undef _STLP_NON_DBG_VECTOR
  367. #endif /* _STLP_DBG_VECTOR_H */
  368. // Local Variables:
  369. // mode:C++
  370. // End: