_bvector.h 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841
  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_BVECTOR_H
  29. #define _STLP_INTERNAL_BVECTOR_H
  30. #ifndef _STLP_INTERNAL_VECTOR_H
  31. # include <stl/_vector.h>
  32. #endif
  33. #define _STLP_WORD_BIT (int(CHAR_BIT * sizeof(unsigned int)))
  34. _STLP_BEGIN_NAMESPACE
  35. _STLP_MOVE_TO_PRIV_NAMESPACE
  36. struct _Bit_reference {
  37. unsigned int* _M_p;
  38. unsigned int _M_mask;
  39. _Bit_reference(unsigned int* __x, unsigned int __y)
  40. : _M_p(__x), _M_mask(__y) {}
  41. public:
  42. _Bit_reference() : _M_p(0), _M_mask(0) {}
  43. operator bool() const {
  44. return !(!(*_M_p & _M_mask));
  45. }
  46. _Bit_reference& operator = (bool __x) {
  47. if (__x) *_M_p |= _M_mask;
  48. else *_M_p &= ~_M_mask;
  49. return *this;
  50. }
  51. _Bit_reference& operator = (const _Bit_reference& __x) {
  52. return *this = bool(__x);
  53. }
  54. bool operator == (const _Bit_reference& __x) const {
  55. return bool(*this) == bool(__x);
  56. }
  57. bool operator < (const _Bit_reference& __x) const {
  58. return !bool(*this) && bool(__x);
  59. }
  60. _Bit_reference& operator |= (bool __x) {
  61. if (__x)
  62. *_M_p |= _M_mask;
  63. return *this;
  64. }
  65. _Bit_reference& operator &= (bool __x) {
  66. if (!__x)
  67. *_M_p &= ~_M_mask;
  68. return *this;
  69. }
  70. void flip() { *_M_p ^= _M_mask; }
  71. };
  72. _STLP_MOVE_TO_STD_NAMESPACE
  73. inline void swap(_STLP_PRIV _Bit_reference& __x, _STLP_PRIV _Bit_reference& __y) {
  74. bool __tmp = (bool)__x;
  75. __x = __y;
  76. __y = __tmp;
  77. }
  78. // Might not be very useful but costs nothing!
  79. _STLP_TEMPLATE_NULL
  80. struct __type_traits<_STLP_PRIV _Bit_reference> {
  81. typedef __false_type has_trivial_default_constructor;
  82. typedef __true_type has_trivial_copy_constructor;
  83. typedef __false_type has_trivial_assignment_operator;
  84. typedef __true_type has_trivial_destructor;
  85. typedef __false_type is_POD_type;
  86. };
  87. _STLP_MOVE_TO_PRIV_NAMESPACE
  88. struct _Bit_iterator_base {
  89. typedef ptrdiff_t difference_type;
  90. unsigned int* _M_p;
  91. unsigned int _M_offset;
  92. void _M_bump_up() {
  93. if (_M_offset++ == _STLP_WORD_BIT - 1) {
  94. _M_offset = 0;
  95. ++_M_p;
  96. }
  97. }
  98. void _M_bump_down() {
  99. if (_M_offset-- == 0) {
  100. _M_offset = _STLP_WORD_BIT - 1;
  101. --_M_p;
  102. }
  103. }
  104. _Bit_iterator_base() : _M_p(0), _M_offset(0) {}
  105. _Bit_iterator_base(unsigned int* __x, unsigned int __y) : _M_p(__x), _M_offset(__y) {}
  106. // see comment in doc/README.evc4 and doc/README.evc8
  107. #if defined(_MSC_VER) && _MSC_VER<=1401 && defined(MIPS) && defined(NDEBUG)
  108. _Bit_iterator_base( const _Bit_iterator_base& __x) : _M_p(__x._M_p), _M_offset(__x._M_offset) {}
  109. #endif
  110. // _Bit_iterator_base& operator = ( const _Bit_iterator_base& __x) { _M_p = __x._M_p ; _M_offset = __x._M_offset ; return *this; }
  111. void _M_advance (difference_type __i) {
  112. difference_type __n = __i + _M_offset;
  113. _M_p += __n / _STLP_WORD_BIT;
  114. __n = __n % _STLP_WORD_BIT;
  115. if (__n < 0) {
  116. _M_offset = (unsigned int) __n + _STLP_WORD_BIT;
  117. --_M_p;
  118. } else
  119. _M_offset = (unsigned int) __n;
  120. }
  121. difference_type _M_subtract(const _Bit_iterator_base& __x) const {
  122. return _STLP_WORD_BIT * (_M_p - __x._M_p) + _M_offset - __x._M_offset;
  123. }
  124. };
  125. inline bool _STLP_CALL operator==(const _Bit_iterator_base& __x, const _Bit_iterator_base& __y) {
  126. return __y._M_p == __x._M_p && __y._M_offset == __x._M_offset;
  127. }
  128. inline bool _STLP_CALL operator!=(const _Bit_iterator_base& __x, const _Bit_iterator_base& __y) {
  129. return __y._M_p != __x._M_p || __y._M_offset != __x._M_offset;
  130. }
  131. inline bool _STLP_CALL operator<(const _Bit_iterator_base& __x, const _Bit_iterator_base& __y) {
  132. return __x._M_p < __y._M_p || (__x._M_p == __y._M_p && __x._M_offset < __y._M_offset);
  133. }
  134. inline bool _STLP_CALL operator>(const _Bit_iterator_base& __x, const _Bit_iterator_base& __y) {
  135. return operator <(__y , __x);
  136. }
  137. inline bool _STLP_CALL operator<=(const _Bit_iterator_base& __x, const _Bit_iterator_base& __y) {
  138. return !(__y < __x);
  139. }
  140. inline bool _STLP_CALL operator>=(const _Bit_iterator_base& __x, const _Bit_iterator_base& __y) {
  141. return !(__x < __y);
  142. }
  143. template <class _Ref, class _Ptr>
  144. struct _Bit_iter : public _Bit_iterator_base {
  145. typedef _Ref reference;
  146. typedef _Ptr pointer;
  147. typedef _Bit_iter<_Ref, _Ptr> _Self;
  148. typedef random_access_iterator_tag iterator_category;
  149. typedef bool value_type;
  150. typedef ptrdiff_t difference_type;
  151. typedef size_t size_type;
  152. _Bit_iter(unsigned int* __x, unsigned int __y) : _Bit_iterator_base(__x, __y) {}
  153. _Bit_iter() {}
  154. _Bit_iter(const _Bit_iter<_Bit_reference, _Bit_reference*>& __x):
  155. _Bit_iterator_base((const _Bit_iterator_base&)__x) {}
  156. // _Self& operator = (const _Bit_iter<_Bit_reference, _Bit_reference*>& __x)
  157. // { (_Bit_iterator_base&)*this = (const _Bit_iterator_base&)__x; return *this; }
  158. reference operator*() const {
  159. return _Bit_reference(_M_p, 1UL << _M_offset);
  160. }
  161. _Self& operator++() {
  162. _M_bump_up();
  163. return *this;
  164. }
  165. _Self operator++(int) {
  166. _Self __tmp = *this;
  167. _M_bump_up();
  168. return __tmp;
  169. }
  170. _Self& operator--() {
  171. _M_bump_down();
  172. return *this;
  173. }
  174. _Self operator--(int) {
  175. _Self __tmp = *this;
  176. _M_bump_down();
  177. return __tmp;
  178. }
  179. _Self& operator+=(difference_type __i) {
  180. _M_advance(__i);
  181. return *this;
  182. }
  183. _Self& operator-=(difference_type __i) {
  184. *this += -__i;
  185. return *this;
  186. }
  187. _Self operator+(difference_type __i) const {
  188. _Self __tmp = *this;
  189. return __tmp += __i;
  190. }
  191. _Self operator-(difference_type __i) const {
  192. _Self __tmp = *this;
  193. return __tmp -= __i;
  194. }
  195. difference_type operator-(const _Self& __x) const {
  196. return _M_subtract(__x);
  197. }
  198. reference operator[](difference_type __i) { return *(*this + __i); }
  199. };
  200. template <class _Ref, class _Ptr>
  201. inline _Bit_iter<_Ref,_Ptr> _STLP_CALL
  202. operator+(ptrdiff_t __n, const _Bit_iter<_Ref, _Ptr>& __x) {
  203. return __x + __n;
  204. }
  205. _STLP_MOVE_TO_STD_NAMESPACE
  206. #if defined (_STLP_CLASS_PARTIAL_SPECIALIZATION)
  207. template <class _Ref, class _Ptr>
  208. struct __type_traits< _STLP_PRIV _Bit_iter<_Ref, _Ptr> > {
  209. typedef __false_type has_trivial_default_constructor;
  210. typedef __true_type has_trivial_copy_constructor;
  211. typedef __true_type has_trivial_assignment_operator;
  212. typedef __true_type has_trivial_destructor;
  213. typedef __false_type is_POD_type;
  214. };
  215. #endif /* _STLP_CLASS_PARTIAL_SPECIALIZATION */
  216. #if defined (_STLP_USE_OLD_HP_ITERATOR_QUERIES)
  217. inline random_access_iterator_tag iterator_category(const _STLP_PRIV _Bit_iterator_base&)
  218. { return random_access_iterator_tag(); }
  219. inline ptrdiff_t* distance_type(const _STLP_PRIV _Bit_iterator_base&)
  220. { return (ptrdiff_t*)0; }
  221. inline bool* value_type(const _STLP_PRIV _Bit_iter<_STLP_PRIV _Bit_reference, _STLP_PRIV _Bit_reference*>&)
  222. { return (bool*)0; }
  223. inline bool* value_type(const _STLP_PRIV _Bit_iter<bool, const bool*>&)
  224. { return (bool*)0; }
  225. #endif
  226. _STLP_MOVE_TO_PRIV_NAMESPACE
  227. typedef _Bit_iter<bool, const bool*> _Bit_const_iterator;
  228. typedef _Bit_iter<_Bit_reference, _Bit_reference*> _Bit_iterator;
  229. // Bit-vector base class, which encapsulates the difference between
  230. // old SGI-style allocators and standard-conforming allocators.
  231. template <class _Alloc>
  232. class _Bvector_base {
  233. typedef _Bvector_base<_Alloc> _Self;
  234. public:
  235. _STLP_FORCE_ALLOCATORS(bool, _Alloc)
  236. typedef _Alloc allocator_type;
  237. typedef unsigned int __chunk_type;
  238. typedef typename _Alloc_traits<__chunk_type, _Alloc>::allocator_type __chunk_allocator_type;
  239. allocator_type get_allocator() const
  240. { return _STLP_CONVERT_ALLOCATOR(__STATIC_CAST(const __chunk_allocator_type&, _M_end_of_storage), bool); }
  241. _Bvector_base(const allocator_type& __a)
  242. : _M_start(), _M_finish(), _M_end_of_storage(_STLP_CONVERT_ALLOCATOR(__a, __chunk_type),
  243. (__chunk_type*)0)
  244. {}
  245. #if !defined (_STLP_NO_MOVE_SEMANTIC)
  246. _Bvector_base(__move_source<_Self> src)
  247. : _M_start(src.get()._M_start), _M_finish(src.get()._M_finish),
  248. _M_end_of_storage(src.get()._M_end_of_storage) {
  249. //Make the source destroyable
  250. src.get()._M_start._M_p = 0;
  251. }
  252. #endif
  253. ~_Bvector_base() {
  254. _M_deallocate();
  255. }
  256. protected:
  257. static size_t _M_bits_to_chunks(size_t __n_bits)
  258. { return (__n_bits + _STLP_WORD_BIT - 1) / _STLP_WORD_BIT; }
  259. __chunk_type* _M_bit_alloc(size_t __n)
  260. { return _M_end_of_storage.allocate(_M_bits_to_chunks(__n)); }
  261. void _M_deallocate() {
  262. if (_M_start._M_p)
  263. _M_end_of_storage.deallocate(_M_start._M_p,
  264. _M_end_of_storage._M_data - _M_start._M_p);
  265. }
  266. _Bit_iterator _M_start;
  267. _Bit_iterator _M_finish;
  268. _STLP_alloc_proxy<__chunk_type*, __chunk_type, __chunk_allocator_type> _M_end_of_storage;
  269. };
  270. // The next few lines are confusing. What we're doing is declaring a
  271. // partial specialization of vector<T, Alloc> if we have the necessary
  272. // compiler support. Otherwise, we define a class bit_vector which uses
  273. // the default allocator.
  274. #if defined (_STLP_CLASS_PARTIAL_SPECIALIZATION) && !defined (_STLP_NO_BOOL) && !defined (__SUNPRO_CC)
  275. # define _STLP_VECBOOL_TEMPLATE
  276. # define __BVEC_TMPL_HEADER template <class _Alloc>
  277. #else
  278. # undef _STLP_VECBOOL_TEMPLATE
  279. # ifdef _STLP_NO_BOOL
  280. # define __BVEC_TMPL_HEADER
  281. # else
  282. # define __BVEC_TMPL_HEADER _STLP_TEMPLATE_NULL
  283. # endif
  284. # define _Alloc allocator<bool>
  285. #endif
  286. #if defined (_STLP_DEBUG)
  287. # define vector _STLP_NON_DBG_NAME(vector)
  288. #endif
  289. #ifdef _STLP_NO_BOOL
  290. # define __BVECTOR_QUALIFIED bit_vector
  291. # define __BVECTOR bit_vector
  292. #else
  293. # ifdef _STLP_VECBOOL_TEMPLATE
  294. # define __BVECTOR_QUALIFIED vector<bool, _Alloc>
  295. # else
  296. # define __BVECTOR_QUALIFIED vector<bool, allocator<bool> >
  297. # endif
  298. # if defined (_STLP_PARTIAL_SPEC_NEEDS_TEMPLATE_ARGS)
  299. # define __BVECTOR __BVECTOR_QUALIFIED
  300. # else
  301. # define __BVECTOR vector
  302. # endif
  303. #endif
  304. #if !defined (_STLP_DEBUG) || defined (_STLP_NO_BOOL)
  305. _STLP_MOVE_TO_STD_NAMESPACE
  306. #endif
  307. __BVEC_TMPL_HEADER
  308. class __BVECTOR_QUALIFIED : public _STLP_PRIV _Bvector_base<_Alloc >
  309. #if defined (_STLP_USE_PARTIAL_SPEC_WORKAROUND) && !defined (_STLP_DEBUG)
  310. , public __stlport_class< __BVECTOR_QUALIFIED >
  311. #endif
  312. {
  313. typedef _STLP_PRIV _Bvector_base<_Alloc > _Base;
  314. typedef __BVECTOR_QUALIFIED _Self;
  315. public:
  316. typedef bool value_type;
  317. typedef size_t size_type;
  318. typedef ptrdiff_t difference_type;
  319. typedef _STLP_PRIV _Bit_reference reference;
  320. typedef bool const_reference;
  321. typedef _STLP_PRIV _Bit_reference* pointer;
  322. typedef const bool* const_pointer;
  323. typedef random_access_iterator_tag _Iterator_category;
  324. typedef _STLP_PRIV _Bit_iterator iterator;
  325. typedef _STLP_PRIV _Bit_const_iterator const_iterator;
  326. _STLP_DECLARE_RANDOM_ACCESS_REVERSE_ITERATORS;
  327. #ifdef _STLP_VECBOOL_TEMPLATE
  328. typedef _STLP_TYPENAME _STLP_PRIV _Bvector_base<_Alloc >::allocator_type allocator_type;
  329. typedef _STLP_TYPENAME _STLP_PRIV _Bvector_base<_Alloc >::__chunk_type __chunk_type;
  330. #else
  331. typedef _STLP_PRIV _Bvector_base<_Alloc >::allocator_type allocator_type;
  332. typedef _STLP_PRIV _Bvector_base<_Alloc >::__chunk_type __chunk_type;
  333. #endif
  334. protected:
  335. void _M_initialize(size_type __n) {
  336. __chunk_type* __q = this->_M_bit_alloc(__n);
  337. this->_M_end_of_storage._M_data = __q + _Base::_M_bits_to_chunks(__n);
  338. this->_M_start = iterator(__q, 0);
  339. this->_M_finish = this->_M_start + difference_type(__n);
  340. }
  341. void _M_insert_aux(iterator __position, bool __x) {
  342. if (this->_M_finish._M_p != this->_M_end_of_storage._M_data) {
  343. _STLP_PRIV __copy_backward(__position, this->_M_finish, this->_M_finish + 1,
  344. random_access_iterator_tag(), (difference_type*)0 );
  345. *__position = __x;
  346. ++this->_M_finish;
  347. }
  348. else {
  349. size_type __len = size() ? 2 * size() : _STLP_WORD_BIT;
  350. __chunk_type* __q = this->_M_bit_alloc(__len);
  351. iterator __i = _STLP_STD::copy(begin(), __position, iterator(__q, 0));
  352. *__i++ = __x;
  353. this->_M_finish = _STLP_STD::copy(__position, end(), __i);
  354. this->_M_deallocate();
  355. this->_M_end_of_storage._M_data = __q + _Base::_M_bits_to_chunks(__len);
  356. this->_M_start = iterator(__q, 0);
  357. }
  358. }
  359. #if defined (_STLP_MEMBER_TEMPLATES)
  360. template <class _InputIterator>
  361. void _M_initialize_range(_InputIterator __first, _InputIterator __last,
  362. const input_iterator_tag &) {
  363. this->_M_start = iterator();
  364. this->_M_finish = iterator();
  365. this->_M_end_of_storage._M_data = 0;
  366. for ( ; __first != __last; ++__first)
  367. push_back(*__first);
  368. }
  369. template <class _ForwardIterator>
  370. void _M_initialize_range(_ForwardIterator __first, _ForwardIterator __last,
  371. const forward_iterator_tag &) {
  372. size_type __n = _STLP_STD::distance(__first, __last);
  373. _M_initialize(__n);
  374. _STLP_STD::copy(__first, __last, this->_M_start);
  375. }
  376. template <class _InputIterator>
  377. void _M_insert_range(iterator __pos,
  378. _InputIterator __first, _InputIterator __last,
  379. const input_iterator_tag &) {
  380. for ( ; __first != __last; ++__first) {
  381. __pos = insert(__pos, *__first);
  382. ++__pos;
  383. }
  384. }
  385. template <class _ForwardIterator>
  386. void _M_insert_range(iterator __position,
  387. _ForwardIterator __first, _ForwardIterator __last,
  388. const forward_iterator_tag &) {
  389. if (__first != __last) {
  390. size_type __n = _STLP_STD::distance(__first, __last);
  391. if (capacity() - size() >= __n) {
  392. _STLP_PRIV __copy_backward(__position, end(), this->_M_finish + difference_type(__n),
  393. random_access_iterator_tag(), (difference_type*)0 );
  394. _STLP_STD::copy(__first, __last, __position);
  395. this->_M_finish += difference_type(__n);
  396. }
  397. else {
  398. size_type __len = size() + (max)(size(), __n);
  399. __chunk_type* __q = this->_M_bit_alloc(__len);
  400. iterator __i = _STLP_STD::copy(begin(), __position, iterator(__q, 0));
  401. __i = _STLP_STD::copy(__first, __last, __i);
  402. this->_M_finish = _STLP_STD::copy(__position, end(), __i);
  403. this->_M_deallocate();
  404. this->_M_end_of_storage._M_data = __q + _Base::_M_bits_to_chunks(__len);
  405. this->_M_start = iterator(__q, 0);
  406. }
  407. }
  408. }
  409. #endif /* _STLP_MEMBER_TEMPLATES */
  410. public:
  411. iterator begin() { return this->_M_start; }
  412. const_iterator begin() const { return this->_M_start; }
  413. iterator end() { return this->_M_finish; }
  414. const_iterator end() const { return this->_M_finish; }
  415. reverse_iterator rbegin() { return reverse_iterator(end()); }
  416. const_reverse_iterator rbegin() const {
  417. return const_reverse_iterator(end());
  418. }
  419. reverse_iterator rend() { return reverse_iterator(begin()); }
  420. const_reverse_iterator rend() const {
  421. return const_reverse_iterator(begin());
  422. }
  423. size_type size() const { return size_type(end() - begin()); }
  424. size_type max_size() const { return size_type(-1); }
  425. size_type capacity() const {
  426. return size_type(const_iterator(this->_M_end_of_storage._M_data, 0) - begin());
  427. }
  428. bool empty() const { return begin() == end(); }
  429. reference operator[](size_type __n)
  430. { return *(begin() + difference_type(__n)); }
  431. const_reference operator[](size_type __n) const
  432. { return *(begin() + difference_type(__n)); }
  433. void _M_range_check(size_type __n) const {
  434. if (__n >= this->size())
  435. __stl_throw_range_error("vector<bool>");
  436. }
  437. reference at(size_type __n)
  438. { _M_range_check(__n); return (*this)[__n]; }
  439. const_reference at(size_type __n) const
  440. { _M_range_check(__n); return (*this)[__n]; }
  441. explicit __BVECTOR(const allocator_type& __a = allocator_type())
  442. : _STLP_PRIV _Bvector_base<_Alloc >(__a) {}
  443. __BVECTOR(size_type __n, bool __val,
  444. const allocator_type& __a = allocator_type())
  445. : _STLP_PRIV _Bvector_base<_Alloc >(__a) {
  446. _M_initialize(__n);
  447. fill(this->_M_start._M_p, (__chunk_type*)(this->_M_end_of_storage._M_data), __val ? ~0 : 0);
  448. }
  449. explicit __BVECTOR(size_type __n)
  450. : _STLP_PRIV _Bvector_base<_Alloc >(allocator_type()) {
  451. _M_initialize(__n);
  452. fill(this->_M_start._M_p, (__chunk_type*)(this->_M_end_of_storage._M_data), 0);
  453. }
  454. __BVECTOR(const _Self& __x)
  455. : _STLP_PRIV _Bvector_base<_Alloc >(__x.get_allocator()) {
  456. _M_initialize(__x.size());
  457. _STLP_STD::copy(__x.begin(), __x.end(), this->_M_start);
  458. }
  459. #if defined (_STLP_MEMBER_TEMPLATES)
  460. template <class _Integer>
  461. void _M_initialize_dispatch(_Integer __n, _Integer __x, const __true_type&) {
  462. _M_initialize(__n);
  463. fill(this->_M_start._M_p, this->_M_end_of_storage._M_data, __x ? ~0 : 0);
  464. }
  465. template <class _InputIterator>
  466. void _M_initialize_dispatch(_InputIterator __first, _InputIterator __last,
  467. const __false_type&) {
  468. _M_initialize_range(__first, __last, _STLP_ITERATOR_CATEGORY(__first, _InputIterator));
  469. }
  470. # if defined (_STLP_NEEDS_EXTRA_TEMPLATE_CONSTRUCTORS)
  471. // Check whether it's an integral type. If so, it's not an iterator.
  472. template <class _InputIterator>
  473. __BVECTOR(_InputIterator __first, _InputIterator __last)
  474. : _STLP_PRIV _Bvector_base<_Alloc >(allocator_type()) {
  475. typedef typename _IsIntegral<_InputIterator>::_Ret _Integral;
  476. _M_initialize_dispatch(__first, __last, _Integral());
  477. }
  478. # endif
  479. template <class _InputIterator>
  480. __BVECTOR(_InputIterator __first, _InputIterator __last,
  481. const allocator_type& __a _STLP_ALLOCATOR_TYPE_DFL)
  482. : _STLP_PRIV _Bvector_base<_Alloc >(__a) {
  483. typedef typename _IsIntegral<_InputIterator>::_Ret _Integral;
  484. _M_initialize_dispatch(__first, __last, _Integral());
  485. }
  486. #else /* _STLP_MEMBER_TEMPLATES */
  487. __BVECTOR(const_iterator __first, const_iterator __last,
  488. const allocator_type& __a = allocator_type())
  489. : _STLP_PRIV _Bvector_base<_Alloc >(__a) {
  490. size_type __n = _STLP_STD::distance(__first, __last);
  491. _M_initialize(__n);
  492. _STLP_STD::copy(__first, __last, this->_M_start);
  493. }
  494. __BVECTOR(const bool* __first, const bool* __last,
  495. const allocator_type& __a = allocator_type())
  496. : _STLP_PRIV _Bvector_base<_Alloc >(__a) {
  497. size_type __n = _STLP_STD::distance(__first, __last);
  498. _M_initialize(__n);
  499. _STLP_STD::copy(__first, __last, this->_M_start);
  500. }
  501. #endif /* _STLP_MEMBER_TEMPLATES */
  502. #if !defined (_STLP_NO_MOVE_SEMANTIC)
  503. __BVECTOR(__move_source<_Self> src)
  504. : _STLP_PRIV _Bvector_base<_Alloc >(__move_source<_Base>(src.get())) {}
  505. #endif
  506. ~__BVECTOR() {}
  507. __BVECTOR_QUALIFIED& operator=(const __BVECTOR_QUALIFIED& __x) {
  508. if (&__x == this) return *this;
  509. if (__x.size() > capacity()) {
  510. this->_M_deallocate();
  511. _M_initialize(__x.size());
  512. }
  513. _STLP_STD::copy(__x.begin(), __x.end(), begin());
  514. this->_M_finish = begin() + difference_type(__x.size());
  515. return *this;
  516. }
  517. // assign(), a generalized assignment member function. Two
  518. // versions: one that takes a count, and one that takes a range.
  519. // The range version is a member template, so we dispatch on whether
  520. // or not the type is an integer.
  521. void _M_fill_assign(size_t __n, bool __x) {
  522. if (__n > size()) {
  523. fill(this->_M_start._M_p, (__chunk_type*)(this->_M_end_of_storage._M_data), __x ? ~0 : 0);
  524. insert(end(), __n - size(), __x);
  525. }
  526. else {
  527. erase(begin() + __n, end());
  528. fill(this->_M_start._M_p, (__chunk_type*)(this->_M_end_of_storage._M_data), __x ? ~0 : 0);
  529. }
  530. }
  531. void assign(size_t __n, bool __x) { _M_fill_assign(__n, __x); }
  532. #if defined (_STLP_MEMBER_TEMPLATES)
  533. template <class _InputIterator>
  534. void assign(_InputIterator __first, _InputIterator __last) {
  535. typedef typename _IsIntegral<_InputIterator>::_Ret _Integral;
  536. _M_assign_dispatch(__first, __last, _Integral());
  537. }
  538. template <class _Integer>
  539. void _M_assign_dispatch(_Integer __n, _Integer __val, const __true_type&)
  540. { _M_fill_assign((size_t) __n, (bool) __val); }
  541. template <class _InputIter>
  542. void _M_assign_dispatch(_InputIter __first, _InputIter __last, const __false_type&)
  543. { _M_assign_aux(__first, __last, _STLP_ITERATOR_CATEGORY(__first, _InputIter)); }
  544. template <class _InputIterator>
  545. void _M_assign_aux(_InputIterator __first, _InputIterator __last,
  546. const input_iterator_tag &) {
  547. iterator __cur = begin();
  548. for ( ; __first != __last && __cur != end(); ++__cur, ++__first)
  549. *__cur = *__first;
  550. if (__first == __last)
  551. erase(__cur, end());
  552. else
  553. insert(end(), __first, __last);
  554. }
  555. template <class _ForwardIterator>
  556. void _M_assign_aux(_ForwardIterator __first, _ForwardIterator __last,
  557. const forward_iterator_tag &) {
  558. size_type __len = _STLP_STD::distance(__first, __last);
  559. if (__len < size())
  560. erase(_STLP_STD::copy(__first, __last, begin()), end());
  561. else {
  562. _ForwardIterator __mid = __first;
  563. _STLP_STD::advance(__mid, size());
  564. _STLP_STD::copy(__first, __mid, begin());
  565. insert(end(), __mid, __last);
  566. }
  567. }
  568. #endif /* _STLP_MEMBER_TEMPLATES */
  569. void reserve(size_type __n) {
  570. if (capacity() < __n) {
  571. if (max_size() < __n)
  572. __stl_throw_length_error("vector<bool>");
  573. __chunk_type* __q = this->_M_bit_alloc(__n);
  574. _STLP_PRIV _Bit_iterator __z(__q, 0);
  575. this->_M_finish = _STLP_STD::copy(begin(), end(), __z);
  576. this->_M_deallocate();
  577. this->_M_start = iterator(__q, 0);
  578. this->_M_end_of_storage._M_data = __q + _Base::_M_bits_to_chunks(__n);
  579. }
  580. }
  581. reference front() { return *begin(); }
  582. const_reference front() const { return *begin(); }
  583. reference back() { return *(end() - 1); }
  584. const_reference back() const { return *(end() - 1); }
  585. void push_back(bool __x) {
  586. if (this->_M_finish._M_p != this->_M_end_of_storage._M_data) {
  587. *(this->_M_finish) = __x;
  588. ++this->_M_finish;
  589. }
  590. else
  591. _M_insert_aux(end(), __x);
  592. }
  593. void swap(__BVECTOR_QUALIFIED& __x) {
  594. _STLP_STD::swap(this->_M_start, __x._M_start);
  595. _STLP_STD::swap(this->_M_finish, __x._M_finish);
  596. this->_M_end_of_storage.swap(__x._M_end_of_storage);
  597. }
  598. #if defined (_STLP_USE_PARTIAL_SPEC_WORKAROUND) && !defined (_STLP_FUNCTION_TMPL_PARTIAL_ORDER)
  599. void _M_swap_workaround(__BVECTOR_QUALIFIED& __x) { swap(__x); }
  600. #endif
  601. iterator insert(iterator __position, bool __x = bool()) {
  602. difference_type __n = __position - begin();
  603. if (this->_M_finish._M_p != this->_M_end_of_storage._M_data && __position == end()) {
  604. *(this->_M_finish) = __x;
  605. ++this->_M_finish;
  606. }
  607. else
  608. _M_insert_aux(__position, __x);
  609. return begin() + __n;
  610. }
  611. #if defined (_STLP_MEMBER_TEMPLATES)
  612. template <class _Integer>
  613. void _M_insert_dispatch(iterator __pos, _Integer __n, _Integer __x,
  614. const __true_type&) {
  615. _M_fill_insert(__pos, (size_type) __n, (bool) __x);
  616. }
  617. template <class _InputIterator>
  618. void _M_insert_dispatch(iterator __pos,
  619. _InputIterator __first, _InputIterator __last,
  620. const __false_type&) {
  621. _M_insert_range(__pos, __first, __last, _STLP_ITERATOR_CATEGORY(__first, _InputIterator));
  622. }
  623. // Check whether it's an integral type. If so, it's not an iterator.
  624. template <class _InputIterator>
  625. void insert(iterator __position,
  626. _InputIterator __first, _InputIterator __last) {
  627. typedef typename _IsIntegral<_InputIterator>::_Ret _Integral;
  628. _M_insert_dispatch(__position, __first, __last, _Integral());
  629. }
  630. #else /* _STLP_MEMBER_TEMPLATES */
  631. void insert(iterator __position,
  632. const_iterator __first, const_iterator __last) {
  633. if (__first == __last) return;
  634. size_type __n = _STLP_STD::distance(__first, __last);
  635. if (capacity() - size() >= __n) {
  636. _STLP_PRIV __copy_backward(__position, end(), this->_M_finish + __n,
  637. random_access_iterator_tag(), (difference_type*)0 );
  638. _STLP_STD::copy(__first, __last, __position);
  639. this->_M_finish += __n;
  640. }
  641. else {
  642. size_type __len = size() + (max)(size(), __n);
  643. __chunk_type* __q = this->_M_bit_alloc(__len);
  644. iterator __i = _STLP_STD::copy(begin(), __position, iterator(__q, 0));
  645. __i = _STLP_STD::copy(__first, __last, __i);
  646. this->_M_finish = _STLP_STD::copy(__position, end(), __i);
  647. this->_M_deallocate();
  648. this->_M_end_of_storage._M_data = __q + _Base::_M_bits_to_chunks(__len);
  649. this->_M_start = iterator(__q, 0);
  650. }
  651. }
  652. void insert(iterator __position, const bool* __first, const bool* __last) {
  653. if (__first == __last) return;
  654. size_type __n = _STLP_STD::distance(__first, __last);
  655. if (capacity() - size() >= __n) {
  656. _STLP_PRIV __copy_backward(__position, end(), this->_M_finish + __n,
  657. random_access_iterator_tag(), (difference_type*)0 );
  658. _STLP_STD::copy(__first, __last, __position);
  659. this->_M_finish += __n;
  660. }
  661. else {
  662. size_type __len = size() + (max)(size(), __n);
  663. __chunk_type* __q = this->_M_bit_alloc(__len);
  664. iterator __i = _STLP_STD::copy(begin(), __position, iterator(__q, 0));
  665. __i = _STLP_STD::copy(__first, __last, __i);
  666. this->_M_finish = _STLP_STD::copy(__position, end(), __i);
  667. this->_M_deallocate();
  668. this->_M_end_of_storage._M_data = __q + _Base::_M_bits_to_chunks(__len);
  669. this->_M_start = iterator(__q, 0);
  670. }
  671. }
  672. #endif /* _STLP_MEMBER_TEMPLATES */
  673. void _M_fill_insert(iterator __position, size_type __n, bool __x) {
  674. if (__n == 0) return;
  675. if (capacity() - size() >= __n) {
  676. _STLP_PRIV __copy_backward(__position, end(), this->_M_finish + difference_type(__n),
  677. random_access_iterator_tag(), (difference_type*)0 );
  678. fill(__position, __position + difference_type(__n), __x);
  679. this->_M_finish += difference_type(__n);
  680. }
  681. else {
  682. size_type __len = size() + (max)(size(), __n);
  683. __chunk_type* __q = this->_M_bit_alloc(__len);
  684. iterator __i = _STLP_STD::copy(begin(), __position, iterator(__q, 0));
  685. fill_n(__i, __n, __x);
  686. this->_M_finish = _STLP_STD::copy(__position, end(), __i + difference_type(__n));
  687. this->_M_deallocate();
  688. this->_M_end_of_storage._M_data = __q + _Base::_M_bits_to_chunks(__len);
  689. this->_M_start = iterator(__q, 0);
  690. }
  691. }
  692. void insert(iterator __position, size_type __n, bool __x) {
  693. _M_fill_insert(__position, __n, __x);
  694. }
  695. void pop_back() {
  696. --this->_M_finish;
  697. }
  698. iterator erase(iterator __position) {
  699. if (__position + 1 != end())
  700. _STLP_STD::copy(__position + 1, end(), __position);
  701. --this->_M_finish;
  702. return __position;
  703. }
  704. iterator erase(iterator __first, iterator __last) {
  705. this->_M_finish = _STLP_STD::copy(__last, end(), __first);
  706. return __first;
  707. }
  708. void resize(size_type __new_size, bool __x = bool()) {
  709. if (__new_size < size())
  710. erase(begin() + difference_type(__new_size), end());
  711. else
  712. insert(end(), __new_size - size(), __x);
  713. }
  714. void flip() {
  715. for (__chunk_type* __p = this->_M_start._M_p; __p != this->_M_end_of_storage._M_data; ++__p)
  716. *__p = ~*__p;
  717. }
  718. void clear() { erase(begin(), end()); }
  719. };
  720. #if defined (_STLP_NO_BOOL) || defined (__HP_aCC) // fixed soon (03/17/2000)
  721. # define _STLP_TEMPLATE_HEADER __BVEC_TMPL_HEADER
  722. # define _STLP_TEMPLATE_CONTAINER __BVECTOR_QUALIFIED
  723. # include <stl/_relops_cont.h>
  724. # undef _STLP_TEMPLATE_CONTAINER
  725. # undef _STLP_TEMPLATE_HEADER
  726. #endif /* NO_BOOL */
  727. #if defined (_STLP_DEBUG) && !defined (_STLP_NO_BOOL)
  728. _STLP_MOVE_TO_STD_NAMESPACE
  729. #endif
  730. _STLP_END_NAMESPACE
  731. #undef vector
  732. #undef _Alloc
  733. #undef _STLP_VECBOOL_TEMPLATE
  734. #undef __BVECTOR
  735. #undef __BVECTOR_QUALIFIED
  736. #undef __BVEC_TMPL_HEADER
  737. #undef _STLP_WORD_BIT
  738. #endif /* _STLP_INTERNAL_BVECTOR_H */
  739. // Local Variables:
  740. // mode:C++
  741. // End: