1 // deque implementation -*- C++ -*- 2 3 // Copyright (C) 2001, 2002 Free Software Foundation, Inc. 4 // 5 // This file is part of the GNU ISO C++ Library. This library is free 6 // software; you can redistribute it and/or modify it under the 7 // terms of the GNU General Public License as published by the 8 // Free Software Foundation; either version 2, or (at your option) 9 // any later version. 10 11 // This library is distributed in the hope that it will be useful, 12 // but WITHOUT ANY WARRANTY; without even the implied warranty of 13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 // GNU General Public License for more details. 15 16 // You should have received a copy of the GNU General Public License along 17 // with this library; see the file COPYING. If not, write to the Free 18 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, 19 // USA. 20 21 // As a special exception, you may use this file as part of a free software 22 // library without restriction. Specifically, if other files instantiate 23 // templates or use macros or inline functions from this file, or you compile 24 // this file and link it with other files to produce an executable, this 25 // file does not by itself cause the resulting executable to be covered by 26 // the GNU General Public License. This exception does not however 27 // invalidate any other reasons why the executable file might be covered by 28 // the GNU General Public License. 29 30 /* 31 * 32 * Copyright (c) 1994 33 * Hewlett-Packard Company 34 * 35 * Permission to use, copy, modify, distribute and sell this software 36 * and its documentation for any purpose is hereby granted without fee, 37 * provided that the above copyright notice appear in all copies and 38 * that both that copyright notice and this permission notice appear 39 * in supporting documentation. Hewlett-Packard Company makes no 40 * representations about the suitability of this software for any 41 * purpose. It is provided "as is" without express or implied warranty. 42 * 43 * 44 * Copyright (c) 1997 45 * Silicon Graphics Computer Systems, Inc. 46 * 47 * Permission to use, copy, modify, distribute and sell this software 48 * and its documentation for any purpose is hereby granted without fee, 49 * provided that the above copyright notice appear in all copies and 50 * that both that copyright notice and this permission notice appear 51 * in supporting documentation. Silicon Graphics makes no 52 * representations about the suitability of this software for any 53 * purpose. It is provided "as is" without express or implied warranty. 54 */ 55 56 /** @file stl_deque.h 57 * This is an internal header file, included by other library headers. 58 * You should not attempt to use it directly. 59 */ 60 61 #include <bits/concept_check.h> 62 #include <bits/stl_iterator_base_types.h> 63 #include <bits/stl_iterator_base_funcs.h> 64 65 #ifndef __GLIBCPP_INTERNAL_DEQUE_H 66 #define __GLIBCPP_INTERNAL_DEQUE_H 67 68 69 // Since this entire file is within namespace std, there's no reason to 70 // waste two spaces along the left column. Thus the leading indentation is 71 // slightly violated from here on. 72 namespace std 73 { 74 75 /** 76 * @if maint 77 * @brief This function controls the size of memory nodes. 78 * @param size The size of an element. 79 * @return The number (not bytesize) of elements per node. 80 * 81 * This function started off as a compiler kludge from SGI, but seems to 82 * be a useful wrapper around a repeated constant expression. 83 * @endif 84 */ 85 inline size_t 86 __deque_buf_size(size_t __size) 87 { return __size < 512 ? size_t(512 / __size) : size_t(1); } 88 89 90 /// A deque::iterator. 91 /** 92 * Quite a bit of intelligence here. Much of the functionality of deque is 93 * actually passed off to this class. A deque holds two of these internally, 94 * marking its valid range. Access to elements is done as offsets of either 95 * of those two, relying on operator overloading in this class. 96 * 97 * @if maint 98 * All the functions are op overloads except for _M_set_node. 99 * @endif 100 */ 101 template <class _Tp, class _Ref, class _Ptr> 102 struct _Deque_iterator 103 { 104 typedef _Deque_iterator<_Tp, _Tp&, _Tp*> iterator; 105 typedef _Deque_iterator<_Tp, const _Tp&, const _Tp*> const_iterator; 106 static size_t _S_buffer_size() { return __deque_buf_size(sizeof(_Tp)); } 107 108 typedef random_access_iterator_tag iterator_category; 109 typedef _Tp value_type; 110 typedef _Ptr pointer; 111 typedef _Ref reference; 112 typedef size_t size_type; 113 typedef ptrdiff_t difference_type; 114 typedef _Tp** _Map_pointer; 115 typedef _Deque_iterator _Self; 116 117 _Tp* _M_cur; 118 _Tp* _M_first; 119 _Tp* _M_last; 120 _Map_pointer _M_node; 121 122 _Deque_iterator(_Tp* __x, _Map_pointer __y) 123 : _M_cur(__x), _M_first(*__y), 124 _M_last(*__y + _S_buffer_size()), _M_node(__y) {} 125 _Deque_iterator() : _M_cur(0), _M_first(0), _M_last(0), _M_node(0) {} 126 _Deque_iterator(const iterator& __x) 127 : _M_cur(__x._M_cur), _M_first(__x._M_first), 128 _M_last(__x._M_last), _M_node(__x._M_node) {} 129 130 reference operator*() const { return *_M_cur; } 131 pointer operator->() const { return _M_cur; } 132 133 difference_type operator-(const _Self& __x) const { 134 return difference_type(_S_buffer_size()) * (_M_node - __x._M_node - 1) + 135 (_M_cur - _M_first) + (__x._M_last - __x._M_cur); 136 } 137 138 _Self& operator++() { 139 ++_M_cur; 140 if (_M_cur == _M_last) { 141 _M_set_node(_M_node + 1); 142 _M_cur = _M_first; 143 } 144 return *this; 145 } 146 _Self operator++(int) { 147 _Self __tmp = *this; 148 ++*this; 149 return __tmp; 150 } 151 152 _Self& operator--() { 153 if (_M_cur == _M_first) { 154 _M_set_node(_M_node - 1); 155 _M_cur = _M_last; 156 } 157 --_M_cur; 158 return *this; 159 } 160 _Self operator--(int) { 161 _Self __tmp = *this; 162 --*this; 163 return __tmp; 164 } 165 166 _Self& operator+=(difference_type __n) 167 { 168 difference_type __offset = __n + (_M_cur - _M_first); 169 if (__offset >= 0 && __offset < difference_type(_S_buffer_size())) 170 _M_cur += __n; 171 else { 172 difference_type __node_offset = 173 __offset > 0 ? __offset / difference_type(_S_buffer_size()) 174 : -difference_type((-__offset - 1) / _S_buffer_size()) - 1; 175 _M_set_node(_M_node + __node_offset); 176 _M_cur = _M_first + 177 (__offset - __node_offset * difference_type(_S_buffer_size())); 178 } 179 return *this; 180 } 181 182 _Self operator+(difference_type __n) const 183 { 184 _Self __tmp = *this; 185 return __tmp += __n; 186 } 187 188 _Self& operator-=(difference_type __n) { return *this += -__n; } 189 190 _Self operator-(difference_type __n) const { 191 _Self __tmp = *this; 192 return __tmp -= __n; 193 } 194 195 reference operator[](difference_type __n) const { return *(*this + __n); } 196 197 bool operator==(const _Self& __x) const { return _M_cur == __x._M_cur; } 198 bool operator!=(const _Self& __x) const { return !(*this == __x); } 199 bool operator<(const _Self& __x) const { 200 return (_M_node == __x._M_node) ? 201 (_M_cur < __x._M_cur) : (_M_node < __x._M_node); 202 } 203 bool operator>(const _Self& __x) const { return __x < *this; } 204 bool operator<=(const _Self& __x) const { return !(__x < *this); } 205 bool operator>=(const _Self& __x) const { return !(*this < __x); } 206 207 /** @if maint 208 * Prepares to traverse new_node. Sets everything except _M_cur, which 209 * should therefore be set by the caller immediately afterwards, based on 210 * _M_first and _M_last. 211 * @endif 212 */ 213 void _M_set_node(_Map_pointer __new_node) { 214 _M_node = __new_node; 215 _M_first = *__new_node; 216 _M_last = _M_first + difference_type(_S_buffer_size()); 217 } 218 }; 219 220 template <class _Tp, class _Ref, class _Ptr> 221 inline _Deque_iterator<_Tp, _Ref, _Ptr> 222 operator+(ptrdiff_t __n, const _Deque_iterator<_Tp, _Ref, _Ptr>& __x) 223 { 224 return __x + __n; 225 } 226 227 228 /// @if maint Primary default version. @endif 229 /** 230 * @if maint 231 * Deque base class. It has two purposes. First, its constructor 232 * and destructor allocate (but don't initialize) storage. This makes 233 * exception safety easier. Second, the base class encapsulates all of 234 * the differences between SGI-style allocators and standard-conforming 235 * allocators. There are two versions: this ordinary one, and the 236 * space-saving specialization for instanceless allocators. 237 * @endif 238 */ 239 template <class _Tp, class _Alloc, bool __is_static> 240 class _Deque_alloc_base 241 { 242 public: 243 typedef typename _Alloc_traits<_Tp,_Alloc>::allocator_type allocator_type; 244 allocator_type get_allocator() const { return _M_node_allocator; } 245 246 _Deque_alloc_base(const allocator_type& __a) 247 : _M_node_allocator(__a), _M_map_allocator(__a), 248 _M_map(0), _M_map_size(0) 249 {} 250 251 protected: 252 typedef typename _Alloc_traits<_Tp*, _Alloc>::allocator_type 253 _Map_allocator_type; 254 255 allocator_type _M_node_allocator; 256 _Map_allocator_type _M_map_allocator; 257 258 _Tp* _M_allocate_node() { 259 return _M_node_allocator.allocate(__deque_buf_size(sizeof(_Tp))); 260 } 261 void _M_deallocate_node(_Tp* __p) { 262 _M_node_allocator.deallocate(__p, __deque_buf_size(sizeof(_Tp))); 263 } 264 _Tp** _M_allocate_map(size_t __n) 265 { return _M_map_allocator.allocate(__n); } 266 void _M_deallocate_map(_Tp** __p, size_t __n) 267 { _M_map_allocator.deallocate(__p, __n); } 268 269 _Tp** _M_map; 270 size_t _M_map_size; 271 }; 272 273 /// @if maint Specialization for instanceless allocators. @endif 274 template <class _Tp, class _Alloc> 275 class _Deque_alloc_base<_Tp, _Alloc, true> 276 { 277 public: 278 typedef typename _Alloc_traits<_Tp,_Alloc>::allocator_type allocator_type; 279 allocator_type get_allocator() const { return allocator_type(); } 280 281 _Deque_alloc_base(const allocator_type&) : _M_map(0), _M_map_size(0) {} 282 283 protected: 284 typedef typename _Alloc_traits<_Tp, _Alloc>::_Alloc_type _Node_alloc_type; 285 typedef typename _Alloc_traits<_Tp*, _Alloc>::_Alloc_type _Map_alloc_type; 286 287 _Tp* _M_allocate_node() { 288 return _Node_alloc_type::allocate(__deque_buf_size(sizeof(_Tp))); 289 } 290 void _M_deallocate_node(_Tp* __p) { 291 _Node_alloc_type::deallocate(__p, __deque_buf_size(sizeof(_Tp))); 292 } 293 _Tp** _M_allocate_map(size_t __n) 294 { return _Map_alloc_type::allocate(__n); } 295 void _M_deallocate_map(_Tp** __p, size_t __n) 296 { _Map_alloc_type::deallocate(__p, __n); } 297 298 _Tp** _M_map; 299 size_t _M_map_size; 300 }; 301 302 303 /** 304 * @if maint 305 * Deque base class. Using _Alloc_traits in the instantiation of the parent 306 * class provides the compile-time dispatching mentioned in the parent's docs. 307 * This class provides the unified face for deque's allocation. 308 * 309 * Nothing in this class ever constructs or destroys an actual Tp element. 310 * (Deque handles that itself.) Only/All memory management is performed here. 311 * @endif 312 */ 313 template <class _Tp, class _Alloc> 314 class _Deque_base 315 : public _Deque_alloc_base<_Tp,_Alloc, 316 _Alloc_traits<_Tp, _Alloc>::_S_instanceless> 317 { 318 public: 319 typedef _Deque_alloc_base<_Tp,_Alloc, 320 _Alloc_traits<_Tp, _Alloc>::_S_instanceless> 321 _Base; 322 typedef typename _Base::allocator_type allocator_type; 323 typedef _Deque_iterator<_Tp,_Tp&,_Tp*> iterator; 324 typedef _Deque_iterator<_Tp,const _Tp&,const _Tp*> const_iterator; 325 326 _Deque_base(const allocator_type& __a, size_t __num_elements) 327 : _Base(__a), _M_start(), _M_finish() 328 { _M_initialize_map(__num_elements); } 329 _Deque_base(const allocator_type& __a) 330 : _Base(__a), _M_start(), _M_finish() {} 331 ~_Deque_base(); 332 333 protected: 334 void _M_initialize_map(size_t); 335 void _M_create_nodes(_Tp** __nstart, _Tp** __nfinish); 336 void _M_destroy_nodes(_Tp** __nstart, _Tp** __nfinish); 337 enum { _S_initial_map_size = 8 }; 338 339 protected: 340 iterator _M_start; 341 iterator _M_finish; 342 }; 343 344 345 template <class _Tp, class _Alloc> 346 _Deque_base<_Tp,_Alloc>::~_Deque_base() 347 { 348 if (_M_map) { 349 _M_destroy_nodes(_M_start._M_node, _M_finish._M_node + 1); 350 _M_deallocate_map(_M_map, _M_map_size); 351 } 352 } 353 354 /** 355 * @if maint 356 * @brief Layout storage. 357 * @param num_elements The count of T's for which to allocate space at first. 358 * @return Nothing. 359 * 360 * The initial underlying memory layout is a bit complicated... 361 * @endif 362 */ 363 template <class _Tp, class _Alloc> 364 void 365 _Deque_base<_Tp,_Alloc>::_M_initialize_map(size_t __num_elements) 366 { 367 size_t __num_nodes = 368 __num_elements / __deque_buf_size(sizeof(_Tp)) + 1; 369 370 _M_map_size = max((size_t) _S_initial_map_size, __num_nodes + 2); 371 _M_map = _M_allocate_map(_M_map_size); 372 373 _Tp** __nstart = _M_map + (_M_map_size - __num_nodes) / 2; 374 _Tp** __nfinish = __nstart + __num_nodes; 375 376 try 377 { _M_create_nodes(__nstart, __nfinish); } 378 catch(...) 379 { 380 _M_deallocate_map(_M_map, _M_map_size); 381 _M_map = 0; 382 _M_map_size = 0; 383 __throw_exception_again; 384 } 385 386 _M_start._M_set_node(__nstart); 387 _M_finish._M_set_node(__nfinish - 1); 388 _M_start._M_cur = _M_start._M_first; 389 _M_finish._M_cur = _M_finish._M_first + 390 __num_elements % __deque_buf_size(sizeof(_Tp)); 391 } 392 393 template <class _Tp, class _Alloc> 394 void _Deque_base<_Tp,_Alloc>::_M_create_nodes(_Tp** __nstart, _Tp** __nfinish) 395 { 396 _Tp** __cur; 397 try { 398 for (__cur = __nstart; __cur < __nfinish; ++__cur) 399 *__cur = _M_allocate_node(); 400 } 401 catch(...) 402 { 403 _M_destroy_nodes(__nstart, __cur); 404 __throw_exception_again; 405 } 406 } 407 408 template <class _Tp, class _Alloc> 409 void 410 _Deque_base<_Tp,_Alloc>::_M_destroy_nodes(_Tp** __nstart, _Tp** __nfinish) 411 { 412 for (_Tp** __n = __nstart; __n < __nfinish; ++__n) 413 _M_deallocate_node(*__n); 414 } 415 416 417 /** 418 * @ingroup Containers 419 * @ingroup Sequences 420 * 421 * Meets the requirements of a <a href="tables.html#65">container</a>, a 422 * <a href="tables.html#66">reversible container</a>, and a 423 * <a href="tables.html#67">sequence</a>, including the 424 * <a href="tables.html#68">optional sequence requirements</a>. 425 * 426 * Placeholder: see http://www.sgi.com/tech/stl/Deque.html for now. 427 * 428 * In previous HP/SGI versions of deque, there was an extra template parameter 429 * so users could control the node size. This extension turned out to violate 430 * the C++ standard (it can be detected using template template parameters), 431 * and it was removed. 432 * 433 * @if maint 434 * Here's how a deque<Tp> manages memory. Each deque has 4 members: 435 * 436 * - Tp** _M_map 437 * - size_t _M_map_size 438 * - iterator _M_start, _M_finish 439 * 440 * map_size is at least 8. %map is an array of map_size pointers-to-"nodes". 441 * (The name has nothing to do with the std::map class.) 442 * 443 * A "node" has no specific type name as such, but it is referred to as 444 * "node" in this file. It is a simple array-of-Tp. If Tp is very large, 445 * there will be one Tp element per node (i.e., an "array" of one). 446 * For non-huge Tp's, node size is inversely related to Tp size: the 447 * larger the Tp, the fewer Tp's will fit in a node. The goal here is to 448 * keep the total size of a node relatively small and constant over different 449 * Tp's, to improve allocator efficiency. 450 * 451 * **** As I write this, the nodes are /not/ allocated using the high-speed 452 * memory pool. There are 20 hours left in the year; perhaps I can fix 453 * this before 2002. 454 * 455 * Not every pointer in the %map array will point to a node. If the initial 456 * number of elements in the deque is small, the /middle/ %map pointers will 457 * be valid, and the ones at the edges will be unused. This same situation 458 * will arise as the %map grows: available %map pointers, if any, will be on 459 * the ends. As new nodes are created, only a subset of the %map's pointers 460 * need to be copied "outward". 461 * 462 * Class invariants: 463 * - For any nonsingular iterator i: 464 * - i.node points to a member of the %map array. (Yes, you read that 465 * correctly: i.node does not actually point to a node.) The member of 466 * the %map array is what actually points to the node. 467 * - i.first == *(i.node) (This points to the node (first Tp element).) 468 * - i.last == i.first + node_size 469 * - i.cur is a pointer in the range [i.first, i.last). NOTE: 470 * the implication of this is that i.cur is always a dereferenceable 471 * pointer, even if i is a past-the-end iterator. 472 * - Start and Finish are always nonsingular iterators. NOTE: this means that 473 * an empty deque must have one node, a deque with <N elements (where N is 474 * the node buffer size) must have one node, a deque with N through (2N-1) 475 * elements must have two nodes, etc. 476 * - For every node other than start.node and finish.node, every element in the 477 * node is an initialized object. If start.node == finish.node, then 478 * [start.cur, finish.cur) are initialized objects, and the elements outside 479 * that range are uninitialized storage. Otherwise, [start.cur, start.last) 480 * and [finish.first, finish.cur) are initialized objects, and [start.first, 481 * start.cur) and [finish.cur, finish.last) are uninitialized storage. 482 * - [%map, %map + map_size) is a valid, non-empty range. 483 * - [start.node, finish.node] is a valid range contained within 484 * [%map, %map + map_size). 485 * - A pointer in the range [%map, %map + map_size) points to an allocated node 486 * if and only if the pointer is in the range [start.node, finish.node]. 487 * 488 * Here's the magic: nothing in deque is "aware" of the discontiguous storage! 489 * 490 * The memory setup and layout occurs in the parent, _Base, and the iterator 491 * class is entirely responsible for "leaping" from one node to the next. All 492 * the implementation routines for deque itself work only through the start 493 * and finish iterators. This keeps the routines simple and sane, and we can 494 * use other standard algorithms as well. 495 * @endif 496 */ 497 template <class _Tp, class _Alloc = allocator<_Tp> > 498 class deque : protected _Deque_base<_Tp, _Alloc> 499 { 500 // concept requirements 501 __glibcpp_class_requires(_Tp, _SGIAssignableConcept) 502 503 typedef _Deque_base<_Tp, _Alloc> _Base; 504 505 public: 506 typedef _Tp value_type; 507 typedef value_type* pointer; 508 typedef const value_type* const_pointer; 509 typedef value_type& reference; 510 typedef const value_type& const_reference; 511 typedef size_t size_type; 512 typedef ptrdiff_t difference_type; 513 514 typedef typename _Base::allocator_type allocator_type; 515 allocator_type get_allocator() const { return _Base::get_allocator(); } 516 517 typedef typename _Base::iterator iterator; 518 typedef typename _Base::const_iterator const_iterator; 519 typedef reverse_iterator<const_iterator> const_reverse_iterator; 520 typedef reverse_iterator<iterator> reverse_iterator; 521 522 protected: 523 typedef pointer* _Map_pointer; 524 static size_t _S_buffer_size() { return __deque_buf_size(sizeof(_Tp)); } 525 526 // Functions controlling memory layout, and nothing else. 527 using _Base::_M_initialize_map; 528 using _Base::_M_create_nodes; 529 using _Base::_M_destroy_nodes; 530 using _Base::_M_allocate_node; 531 using _Base::_M_deallocate_node; 532 using _Base::_M_allocate_map; 533 using _Base::_M_deallocate_map; 534 535 /** @if maint 536 * A total of four data members accumulated down the heirarchy. If the 537 * _Alloc type requires separate instances, then two of them will also be 538 * included in each deque. 539 * @endif 540 */ 541 using _Base::_M_map; 542 using _Base::_M_map_size; 543 using _Base::_M_start; 544 using _Base::_M_finish; 545 546 public: // Basic accessors 547 iterator begin() { return _M_start; } 548 iterator end() { return _M_finish; } 549 const_iterator begin() const { return _M_start; } 550 const_iterator end() const { return _M_finish; } 551 552 reverse_iterator rbegin() { return reverse_iterator(_M_finish); } 553 reverse_iterator rend() { return reverse_iterator(_M_start); } 554 const_reverse_iterator rbegin() const 555 { return const_reverse_iterator(_M_finish); } 556 const_reverse_iterator rend() const 557 { return const_reverse_iterator(_M_start); } 558 559 reference operator[](size_type __n) 560 { return _M_start[difference_type(__n)]; } 561 const_reference operator[](size_type __n) const 562 { return _M_start[difference_type(__n)]; } 563 564 void _M_range_check(size_type __n) const { 565 if (__n >= this->size()) 566 __throw_range_error("deque"); 567 } 568 569 reference at(size_type __n) 570 { _M_range_check(__n); return (*this)[__n]; } 571 const_reference at(size_type __n) const 572 { _M_range_check(__n); return (*this)[__n]; } 573 574 reference front() { return *_M_start; } 575 reference back() { 576 iterator __tmp = _M_finish; 577 --__tmp; 578 return *__tmp; 579 } 580 const_reference front() const { return *_M_start; } 581 const_reference back() const { 582 const_iterator __tmp = _M_finish; 583 --__tmp; 584 return *__tmp; 585 } 586 587 size_type size() const { return _M_finish - _M_start; } 588 size_type max_size() const { return size_type(-1); } 589 bool empty() const { return _M_finish == _M_start; } 590 591 public: // Constructor, destructor. 592 explicit deque(const allocator_type& __a = allocator_type()) 593 : _Base(__a, 0) {} 594 deque(const deque& __x) : _Base(__x.get_allocator(), __x.size()) 595 { uninitialized_copy(__x.begin(), __x.end(), _M_start); } 596 deque(size_type __n, const value_type& __value, 597 const allocator_type& __a = allocator_type()) : _Base(__a, __n) 598 { _M_fill_initialize(__value); } 599 600 explicit 601 deque(size_type __n) 602 : _Base(allocator_type(), __n) 603 { _M_fill_initialize(value_type()); } 604 605 // Check whether it's an integral type. If so, it's not an iterator. 606 template<class _InputIterator> 607 deque(_InputIterator __first, _InputIterator __last, 608 const allocator_type& __a = allocator_type()) 609 : _Base(__a) 610 { 611 typedef typename _Is_integer<_InputIterator>::_Integral _Integral; 612 _M_initialize_dispatch(__first, __last, _Integral()); 613 } 614 615 template<class _Integer> 616 void 617 _M_initialize_dispatch(_Integer __n, _Integer __x, __true_type) 618 { 619 _M_initialize_map(__n); 620 _M_fill_initialize(__x); 621 } 622 623 template<class _InputIter> 624 void 625 _M_initialize_dispatch(_InputIter __first, _InputIter __last, __false_type) 626 { 627 typedef typename iterator_traits<_InputIter>::iterator_category _IterCategory; 628 _M_range_initialize(__first, __last, _IterCategory()); 629 } 630 631 ~deque() 632 { _Destroy(_M_start, _M_finish); } 633 634 deque& operator= (const deque& __x) { 635 const size_type __len = size(); 636 if (&__x != this) { 637 if (__len >= __x.size()) 638 erase(copy(__x.begin(), __x.end(), _M_start), _M_finish); 639 else { 640 const_iterator __mid = __x.begin() + difference_type(__len); 641 copy(__x.begin(), __mid, _M_start); 642 insert(_M_finish, __mid, __x.end()); 643 } 644 } 645 return *this; 646 } 647 648 void swap(deque& __x) { 649 std::swap(_M_start, __x._M_start); 650 std::swap(_M_finish, __x._M_finish); 651 std::swap(_M_map, __x._M_map); 652 std::swap(_M_map_size, __x._M_map_size); 653 } 654 655 public: 656 // assign(), a generalized assignment member function. Two 657 // versions: one that takes a count, and one that takes a range. 658 // The range version is a member template, so we dispatch on whether 659 // or not the type is an integer. 660 661 void _M_fill_assign(size_type __n, const _Tp& __val) { 662 if (__n > size()) { 663 fill(begin(), end(), __val); 664 insert(end(), __n - size(), __val); 665 } 666 else { 667 erase(begin() + __n, end()); 668 fill(begin(), end(), __val); 669 } 670 } 671 672 void 673 assign(size_type __n, const _Tp& __val) 674 { _M_fill_assign(__n, __val); } 675 676 template<class _InputIterator> 677 void 678 assign(_InputIterator __first, _InputIterator __last) 679 { 680 typedef typename _Is_integer<_InputIterator>::_Integral _Integral; 681 _M_assign_dispatch(__first, __last, _Integral()); 682 } 683 684 private: // helper functions for assign() 685 686 template<class _Integer> 687 void 688 _M_assign_dispatch(_Integer __n, _Integer __val, __true_type) 689 { _M_fill_assign(static_cast<size_type>(__n), static_cast<_Tp>(__val)); } 690 691 template<class _InputIterator> 692 void 693 _M_assign_dispatch(_InputIterator __first, _InputIterator __last, __false_type) 694 { 695 typedef typename iterator_traits<_InputIterator>::iterator_category _IterCategory; 696 _M_assign_aux(__first, __last, _IterCategory()); 697 } 698 699 template <class _InputIterator> 700 void _M_assign_aux(_InputIterator __first, _InputIterator __last, 701 input_iterator_tag); 702 703 template <class _ForwardIterator> 704 void _M_assign_aux(_ForwardIterator __first, _ForwardIterator __last, 705 forward_iterator_tag) { 706 size_type __len = distance(__first, __last); 707 if (__len > size()) { 708 _ForwardIterator __mid = __first; 709 advance(__mid, size()); 710 copy(__first, __mid, begin()); 711 insert(end(), __mid, __last); 712 } 713 else 714 erase(copy(__first, __last, begin()), end()); 715 } 716 717 public: // push_* and pop_* 718 719 void 720 push_back(const value_type& __t) 721 { 722 if (_M_finish._M_cur != _M_finish._M_last - 1) { 723 _Construct(_M_finish._M_cur, __t); 724 ++_M_finish._M_cur; 725 } 726 else 727 _M_push_back_aux(__t); 728 } 729 730 void 731 push_back() 732 { 733 if (_M_finish._M_cur != _M_finish._M_last - 1) { 734 _Construct(_M_finish._M_cur); 735 ++_M_finish._M_cur; 736 } 737 else 738 _M_push_back_aux(); 739 } 740 741 void 742 push_front(const value_type& __t) 743 { 744 if (_M_start._M_cur != _M_start._M_first) { 745 _Construct(_M_start._M_cur - 1, __t); 746 --_M_start._M_cur; 747 } 748 else 749 _M_push_front_aux(__t); 750 } 751 752 void 753 push_front() 754 { 755 if (_M_start._M_cur != _M_start._M_first) { 756 _Construct(_M_start._M_cur - 1); 757 --_M_start._M_cur; 758 } 759 else 760 _M_push_front_aux(); 761 } 762 763 764 void 765 pop_back() 766 { 767 if (_M_finish._M_cur != _M_finish._M_first) { 768 --_M_finish._M_cur; 769 _Destroy(_M_finish._M_cur); 770 } 771 else 772 _M_pop_back_aux(); 773 } 774 775 void 776 pop_front() 777 { 778 if (_M_start._M_cur != _M_start._M_last - 1) { 779 _Destroy(_M_start._M_cur); 780 ++_M_start._M_cur; 781 } 782 else 783 _M_pop_front_aux(); 784 } 785 786 public: // Insert 787 788 iterator 789 insert(iterator position, const value_type& __x) 790 { 791 if (position._M_cur == _M_start._M_cur) { 792 push_front(__x); 793 return _M_start; 794 } 795 else if (position._M_cur == _M_finish._M_cur) { 796 push_back(__x); 797 iterator __tmp = _M_finish; 798 --__tmp; 799 return __tmp; 800 } 801 else { 802 return _M_insert_aux(position, __x); 803 } 804 } 805 806 iterator 807 insert(iterator __position) 808 { return insert(__position, value_type()); } 809 810 void 811 insert(iterator __pos, size_type __n, const value_type& __x) 812 { _M_fill_insert(__pos, __n, __x); } 813 814 void 815 _M_fill_insert(iterator __pos, size_type __n, const value_type& __x); 816 817 // Check whether it's an integral type. If so, it's not an iterator. 818 template<class _InputIterator> 819 void 820 insert(iterator __pos, _InputIterator __first, _InputIterator __last) 821 { 822 typedef typename _Is_integer<_InputIterator>::_Integral _Integral; 823 _M_insert_dispatch(__pos, __first, __last, _Integral()); 824 } 825 826 template<class _Integer> 827 void 828 _M_insert_dispatch(iterator __pos, _Integer __n, _Integer __x, __true_type) 829 { _M_fill_insert(__pos, static_cast<size_type>(__n), static_cast<value_type>(__x)); } 830 831 template<class _InputIterator> 832 void 833 _M_insert_dispatch(iterator __pos, 834 _InputIterator __first, _InputIterator __last, 835 __false_type) 836 { 837 typedef typename iterator_traits<_InputIterator>::iterator_category _IterCategory; 838 insert(__pos, __first, __last, _IterCategory()); 839 } 840 841 void resize(size_type __new_size, const value_type& __x) { 842 const size_type __len = size(); 843 if (__new_size < __len) 844 erase(_M_start + __new_size, _M_finish); 845 else 846 insert(_M_finish, __new_size - __len, __x); 847 } 848 849 void resize(size_type new_size) { resize(new_size, value_type()); } 850 851 public: // Erase 852 iterator erase(iterator __pos) { 853 iterator __next = __pos; 854 ++__next; 855 size_type __index = __pos - _M_start; 856 if (__index < (size() >> 1)) { 857 copy_backward(_M_start, __pos, __next); 858 pop_front(); 859 } 860 else { 861 copy(__next, _M_finish, __pos); 862 pop_back(); 863 } 864 return _M_start + __index; 865 } 866 867 iterator erase(iterator __first, iterator __last); 868 void clear(); 869 870 protected: // Internal construction/destruction 871 872 void _M_fill_initialize(const value_type& __value); 873 874 template <class _InputIterator> 875 void _M_range_initialize(_InputIterator __first, _InputIterator __last, 876 input_iterator_tag); 877 878 template <class _ForwardIterator> 879 void _M_range_initialize(_ForwardIterator __first, _ForwardIterator __last, 880 forward_iterator_tag); 881 882 protected: // Internal push_* and pop_* 883 884 void _M_push_back_aux(const value_type&); 885 void _M_push_back_aux(); 886 void _M_push_front_aux(const value_type&); 887 void _M_push_front_aux(); 888 void _M_pop_back_aux(); 889 void _M_pop_front_aux(); 890 891 protected: // Internal insert functions 892 893 template <class _InputIterator> 894 void insert(iterator __pos, _InputIterator __first, _InputIterator __last, 895 input_iterator_tag); 896 897 template <class _ForwardIterator> 898 void insert(iterator __pos, 899 _ForwardIterator __first, _ForwardIterator __last, 900 forward_iterator_tag); 901 902 iterator _M_insert_aux(iterator __pos, const value_type& __x); 903 iterator _M_insert_aux(iterator __pos); 904 void _M_insert_aux(iterator __pos, size_type __n, const value_type& __x); 905 906 template <class _ForwardIterator> 907 void _M_insert_aux(iterator __pos, 908 _ForwardIterator __first, _ForwardIterator __last, 909 size_type __n); 910 911 iterator _M_reserve_elements_at_front(size_type __n) { 912 size_type __vacancies = _M_start._M_cur - _M_start._M_first; 913 if (__n > __vacancies) 914 _M_new_elements_at_front(__n - __vacancies); 915 return _M_start - difference_type(__n); 916 } 917 918 iterator _M_reserve_elements_at_back(size_type __n) { 919 size_type __vacancies = (_M_finish._M_last - _M_finish._M_cur) - 1; 920 if (__n > __vacancies) 921 _M_new_elements_at_back(__n - __vacancies); 922 return _M_finish + difference_type(__n); 923 } 924 925 void _M_new_elements_at_front(size_type __new_elements); 926 void _M_new_elements_at_back(size_type __new_elements); 927 928 protected: // Allocation of _M_map and nodes 929 930 // Makes sure the _M_map has space for new nodes. Does not actually 931 // add the nodes. Can invalidate _M_map pointers. (And consequently, 932 // deque iterators.) 933 934 void _M_reserve_map_at_back (size_type __nodes_to_add = 1) { 935 if (__nodes_to_add + 1 > _M_map_size - (_M_finish._M_node - _M_map)) 936 _M_reallocate_map(__nodes_to_add, false); 937 } 938 939 void _M_reserve_map_at_front (size_type __nodes_to_add = 1) { 940 if (__nodes_to_add > size_type(_M_start._M_node - _M_map)) 941 _M_reallocate_map(__nodes_to_add, true); 942 } 943 944 void _M_reallocate_map(size_type __nodes_to_add, bool __add_at_front); 945 }; 946 947 // Non-inline member functions 948 949 template <class _Tp, class _Alloc> 950 template <class _InputIter> 951 void deque<_Tp, _Alloc> 952 ::_M_assign_aux(_InputIter __first, _InputIter __last, input_iterator_tag) 953 { 954 iterator __cur = begin(); 955 for ( ; __first != __last && __cur != end(); ++__cur, ++__first) 956 *__cur = *__first; 957 if (__first == __last) 958 erase(__cur, end()); 959 else 960 insert(end(), __first, __last); 961 } 962 963 template <class _Tp, class _Alloc> 964 void deque<_Tp, _Alloc>::_M_fill_insert(iterator __pos, 965 size_type __n, const value_type& __x) 966 { 967 if (__pos._M_cur == _M_start._M_cur) { 968 iterator __new_start = _M_reserve_elements_at_front(__n); 969 try { 970 uninitialized_fill(__new_start, _M_start, __x); 971 _M_start = __new_start; 972 } 973 catch(...) 974 { 975 _M_destroy_nodes(__new_start._M_node, _M_start._M_node); 976 __throw_exception_again; 977 } 978 } 979 else if (__pos._M_cur == _M_finish._M_cur) { 980 iterator __new_finish = _M_reserve_elements_at_back(__n); 981 try { 982 uninitialized_fill(_M_finish, __new_finish, __x); 983 _M_finish = __new_finish; 984 } 985 catch(...) 986 { 987 _M_destroy_nodes(_M_finish._M_node + 1, __new_finish._M_node + 1); 988 __throw_exception_again; 989 } 990 } 991 else 992 _M_insert_aux(__pos, __n, __x); 993 } 994 995 template <class _Tp, class _Alloc> 996 typename deque<_Tp,_Alloc>::iterator 997 deque<_Tp,_Alloc>::erase(iterator __first, iterator __last) 998 { 999 if (__first == _M_start && __last == _M_finish) { 1000 clear(); 1001 return _M_finish; 1002 } 1003 else { 1004 difference_type __n = __last - __first; 1005 difference_type __elems_before = __first - _M_start; 1006 if (static_cast<size_type>(__elems_before) < (size() - __n) / 2) { 1007 copy_backward(_M_start, __first, __last); 1008 iterator __new_start = _M_start + __n; 1009 _Destroy(_M_start, __new_start); 1010 _M_destroy_nodes(_M_start._M_node, __new_start._M_node); 1011 _M_start = __new_start; 1012 } 1013 else { 1014 copy(__last, _M_finish, __first); 1015 iterator __new_finish = _M_finish - __n; 1016 _Destroy(__new_finish, _M_finish); 1017 _M_destroy_nodes(__new_finish._M_node + 1, _M_finish._M_node + 1); 1018 _M_finish = __new_finish; 1019 } 1020 return _M_start + __elems_before; 1021 } 1022 } 1023 1024 template <class _Tp, class _Alloc> 1025 void deque<_Tp,_Alloc>::clear() 1026 { 1027 for (_Map_pointer __node = _M_start._M_node + 1; 1028 __node < _M_finish._M_node; 1029 ++__node) { 1030 _Destroy(*__node, *__node + _S_buffer_size()); 1031 _M_deallocate_node(*__node); 1032 } 1033 1034 if (_M_start._M_node != _M_finish._M_node) { 1035 _Destroy(_M_start._M_cur, _M_start._M_last); 1036 _Destroy(_M_finish._M_first, _M_finish._M_cur); 1037 _M_deallocate_node(_M_finish._M_first); 1038 } 1039 else 1040 _Destroy(_M_start._M_cur, _M_finish._M_cur); 1041 1042 _M_finish = _M_start; 1043 } 1044 1045 /** 1046 * @if maint 1047 * @brief Fills the deque with copies of value. 1048 * @param value Initial value. 1049 * @return Nothing. 1050 * @pre _M_start and _M_finish have already been initialized, but none of the 1051 * deque's elements have yet been constructed. 1052 * 1053 * This function is called only when the user provides an explicit size (with 1054 * or without an explicit exemplar value). 1055 * @endif 1056 */ 1057 template <class _Tp, class _Alloc> 1058 void deque<_Tp,_Alloc>::_M_fill_initialize(const value_type& __value) 1059 { 1060 _Map_pointer __cur; 1061 try { 1062 for (__cur = _M_start._M_node; __cur < _M_finish._M_node; ++__cur) 1063 uninitialized_fill(*__cur, *__cur + _S_buffer_size(), __value); 1064 uninitialized_fill(_M_finish._M_first, _M_finish._M_cur, __value); 1065 } 1066 catch(...) 1067 { 1068 _Destroy(_M_start, iterator(*__cur, __cur)); 1069 __throw_exception_again; 1070 } 1071 } 1072 1073 /** @{ 1074 * @if maint 1075 * @brief Fills the deque with whatever is in [first,last). 1076 * @param first An input iterator. 1077 * @param last An input iterator. 1078 * @return Nothing. 1079 * 1080 * If the iterators are actually forward iterators (or better), then the 1081 * memory layout can be done all at once. Else we move forward using 1082 * push_back on each value from the iterator. 1083 * @endif 1084 */ 1085 template <class _Tp, class _Alloc> template <class _InputIterator> 1086 void deque<_Tp,_Alloc>::_M_range_initialize(_InputIterator __first, 1087 _InputIterator __last, 1088 input_iterator_tag) 1089 { 1090 _M_initialize_map(0); 1091 try { 1092 for ( ; __first != __last; ++__first) 1093 push_back(*__first); 1094 } 1095 catch(...) 1096 { 1097 clear(); 1098 __throw_exception_again; 1099 } 1100 } 1101 1102 template <class _Tp, class _Alloc> template <class _ForwardIterator> 1103 void deque<_Tp,_Alloc>::_M_range_initialize(_ForwardIterator __first, 1104 _ForwardIterator __last, 1105 forward_iterator_tag) 1106 { 1107 size_type __n = distance(__first, __last); 1108 _M_initialize_map(__n); 1109 1110 _Map_pointer __cur_node; 1111 try { 1112 for (__cur_node = _M_start._M_node; 1113 __cur_node < _M_finish._M_node; 1114 ++__cur_node) { 1115 _ForwardIterator __mid = __first; 1116 advance(__mid, _S_buffer_size()); 1117 uninitialized_copy(__first, __mid, *__cur_node); 1118 __first = __mid; 1119 } 1120 uninitialized_copy(__first, __last, _M_finish._M_first); 1121 } 1122 catch(...) 1123 { 1124 _Destroy(_M_start, iterator(*__cur_node, __cur_node)); 1125 __throw_exception_again; 1126 } 1127 } 1128 /** @} */ 1129 1130 // Called only if _M_finish._M_cur == _M_finish._M_last - 1. 1131 template <class _Tp, class _Alloc> 1132 void 1133 deque<_Tp,_Alloc>::_M_push_back_aux(const value_type& __t) 1134 { 1135 value_type __t_copy = __t; 1136 _M_reserve_map_at_back(); 1137 *(_M_finish._M_node + 1) = _M_allocate_node(); 1138 try { 1139 _Construct(_M_finish._M_cur, __t_copy); 1140 _M_finish._M_set_node(_M_finish._M_node + 1); 1141 _M_finish._M_cur = _M_finish._M_first; 1142 } 1143 catch(...) 1144 { 1145 _M_deallocate_node(*(_M_finish._M_node + 1)); 1146 __throw_exception_again; 1147 } 1148 } 1149 1150 // Called only if _M_finish._M_cur == _M_finish._M_last - 1. 1151 template <class _Tp, class _Alloc> 1152 void 1153 deque<_Tp,_Alloc>::_M_push_back_aux() 1154 { 1155 _M_reserve_map_at_back(); 1156 *(_M_finish._M_node + 1) = _M_allocate_node(); 1157 try { 1158 _Construct(_M_finish._M_cur); 1159 _M_finish._M_set_node(_M_finish._M_node + 1); 1160 _M_finish._M_cur = _M_finish._M_first; 1161 } 1162 catch(...) 1163 { 1164 _M_deallocate_node(*(_M_finish._M_node + 1)); 1165 __throw_exception_again; 1166 } 1167 } 1168 1169 // Called only if _M_start._M_cur == _M_start._M_first. 1170 template <class _Tp, class _Alloc> 1171 void 1172 deque<_Tp,_Alloc>::_M_push_front_aux(const value_type& __t) 1173 { 1174 value_type __t_copy = __t; 1175 _M_reserve_map_at_front(); 1176 *(_M_start._M_node - 1) = _M_allocate_node(); 1177 try { 1178 _M_start._M_set_node(_M_start._M_node - 1); 1179 _M_start._M_cur = _M_start._M_last - 1; 1180 _Construct(_M_start._M_cur, __t_copy); 1181 } 1182 catch(...) 1183 { 1184 ++_M_start; 1185 _M_deallocate_node(*(_M_start._M_node - 1)); 1186 __throw_exception_again; 1187 } 1188 } 1189 1190 // Called only if _M_start._M_cur == _M_start._M_first. 1191 template <class _Tp, class _Alloc> 1192 void 1193 deque<_Tp,_Alloc>::_M_push_front_aux() 1194 { 1195 _M_reserve_map_at_front(); 1196 *(_M_start._M_node - 1) = _M_allocate_node(); 1197 try { 1198 _M_start._M_set_node(_M_start._M_node - 1); 1199 _M_start._M_cur = _M_start._M_last - 1; 1200 _Construct(_M_start._M_cur); 1201 } 1202 catch(...) 1203 { 1204 ++_M_start; 1205 _M_deallocate_node(*(_M_start._M_node - 1)); 1206 __throw_exception_again; 1207 } 1208 } 1209 1210 // Called only if _M_finish._M_cur == _M_finish._M_first. 1211 template <class _Tp, class _Alloc> 1212 void deque<_Tp,_Alloc>::_M_pop_back_aux() 1213 { 1214 _M_deallocate_node(_M_finish._M_first); 1215 _M_finish._M_set_node(_M_finish._M_node - 1); 1216 _M_finish._M_cur = _M_finish._M_last - 1; 1217 _Destroy(_M_finish._M_cur); 1218 } 1219 1220 // Called only if _M_start._M_cur == _M_start._M_last - 1. Note that 1221 // if the deque has at least one element (a precondition for this member 1222 // function), and if _M_start._M_cur == _M_start._M_last, then the deque 1223 // must have at least two nodes. 1224 template <class _Tp, class _Alloc> 1225 void deque<_Tp,_Alloc>::_M_pop_front_aux() 1226 { 1227 _Destroy(_M_start._M_cur); 1228 _M_deallocate_node(_M_start._M_first); 1229 _M_start._M_set_node(_M_start._M_node + 1); 1230 _M_start._M_cur = _M_start._M_first; 1231 } 1232 1233 template <class _Tp, class _Alloc> template <class _InputIterator> 1234 void deque<_Tp,_Alloc>::insert(iterator __pos, 1235 _InputIterator __first, _InputIterator __last, 1236 input_iterator_tag) 1237 { 1238 copy(__first, __last, inserter(*this, __pos)); 1239 } 1240 1241 template <class _Tp, class _Alloc> template <class _ForwardIterator> 1242 void 1243 deque<_Tp,_Alloc>::insert(iterator __pos, 1244 _ForwardIterator __first, _ForwardIterator __last, 1245 forward_iterator_tag) { 1246 size_type __n = distance(__first, __last); 1247 if (__pos._M_cur == _M_start._M_cur) { 1248 iterator __new_start = _M_reserve_elements_at_front(__n); 1249 try { 1250 uninitialized_copy(__first, __last, __new_start); 1251 _M_start = __new_start; 1252 } 1253 catch(...) 1254 { 1255 _M_destroy_nodes(__new_start._M_node, _M_start._M_node); 1256 __throw_exception_again; 1257 } 1258 } 1259 else if (__pos._M_cur == _M_finish._M_cur) { 1260 iterator __new_finish = _M_reserve_elements_at_back(__n); 1261 try { 1262 uninitialized_copy(__first, __last, _M_finish); 1263 _M_finish = __new_finish; 1264 } 1265 catch(...) 1266 { 1267 _M_destroy_nodes(_M_finish._M_node + 1, __new_finish._M_node + 1); 1268 __throw_exception_again; 1269 } 1270 } 1271 else 1272 _M_insert_aux(__pos, __first, __last, __n); 1273 } 1274 1275 template <class _Tp, class _Alloc> 1276 typename deque<_Tp, _Alloc>::iterator 1277 deque<_Tp,_Alloc>::_M_insert_aux(iterator __pos, const value_type& __x) 1278 { 1279 difference_type __index = __pos - _M_start; 1280 value_type __x_copy = __x; 1281 if (static_cast<size_type>(__index) < size() / 2) { 1282 push_front(front()); 1283 iterator __front1 = _M_start; 1284 ++__front1; 1285 iterator __front2 = __front1; 1286 ++__front2; 1287 __pos = _M_start + __index; 1288 iterator __pos1 = __pos; 1289 ++__pos1; 1290 copy(__front2, __pos1, __front1); 1291 } 1292 else { 1293 push_back(back()); 1294 iterator __back1 = _M_finish; 1295 --__back1; 1296 iterator __back2 = __back1; 1297 --__back2; 1298 __pos = _M_start + __index; 1299 copy_backward(__pos, __back2, __back1); 1300 } 1301 *__pos = __x_copy; 1302 return __pos; 1303 } 1304 1305 template <class _Tp, class _Alloc> 1306 typename deque<_Tp,_Alloc>::iterator 1307 deque<_Tp,_Alloc>::_M_insert_aux(iterator __pos) 1308 { 1309 difference_type __index = __pos - _M_start; 1310 if (static_cast<size_type>(__index) < size() / 2) { 1311 push_front(front()); 1312 iterator __front1 = _M_start; 1313 ++__front1; 1314 iterator __front2 = __front1; 1315 ++__front2; 1316 __pos = _M_start + __index; 1317 iterator __pos1 = __pos; 1318 ++__pos1; 1319 copy(__front2, __pos1, __front1); 1320 } 1321 else { 1322 push_back(back()); 1323 iterator __back1 = _M_finish; 1324 --__back1; 1325 iterator __back2 = __back1; 1326 --__back2; 1327 __pos = _M_start + __index; 1328 copy_backward(__pos, __back2, __back1); 1329 } 1330 *__pos = value_type(); 1331 return __pos; 1332 } 1333 1334 template <class _Tp, class _Alloc> 1335 void deque<_Tp,_Alloc>::_M_insert_aux(iterator __pos, 1336 size_type __n, 1337 const value_type& __x) 1338 { 1339 const difference_type __elems_before = __pos - _M_start; 1340 size_type __length = this->size(); 1341 value_type __x_copy = __x; 1342 if (__elems_before < difference_type(__length / 2)) { 1343 iterator __new_start = _M_reserve_elements_at_front(__n); 1344 iterator __old_start = _M_start; 1345 __pos = _M_start + __elems_before; 1346 try { 1347 if (__elems_before >= difference_type(__n)) { 1348 iterator __start_n = _M_start + difference_type(__n); 1349 uninitialized_copy(_M_start, __start_n, __new_start); 1350 _M_start = __new_start; 1351 copy(__start_n, __pos, __old_start); 1352 fill(__pos - difference_type(__n), __pos, __x_copy); 1353 } 1354 else { 1355 __uninitialized_copy_fill(_M_start, __pos, __new_start, 1356 _M_start, __x_copy); 1357 _M_start = __new_start; 1358 fill(__old_start, __pos, __x_copy); 1359 } 1360 } 1361 catch(...) 1362 { 1363 _M_destroy_nodes(__new_start._M_node, _M_start._M_node); 1364 __throw_exception_again; 1365 } 1366 } 1367 else { 1368 iterator __new_finish = _M_reserve_elements_at_back(__n); 1369 iterator __old_finish = _M_finish; 1370 const difference_type __elems_after = 1371 difference_type(__length) - __elems_before; 1372 __pos = _M_finish - __elems_after; 1373 try { 1374 if (__elems_after > difference_type(__n)) { 1375 iterator __finish_n = _M_finish - difference_type(__n); 1376 uninitialized_copy(__finish_n, _M_finish, _M_finish); 1377 _M_finish = __new_finish; 1378 copy_backward(__pos, __finish_n, __old_finish); 1379 fill(__pos, __pos + difference_type(__n), __x_copy); 1380 } 1381 else { 1382 __uninitialized_fill_copy(_M_finish, __pos + difference_type(__n), 1383 __x_copy, __pos, _M_finish); 1384 _M_finish = __new_finish; 1385 fill(__pos, __old_finish, __x_copy); 1386 } 1387 } 1388 catch(...) 1389 { 1390 _M_destroy_nodes(_M_finish._M_node + 1, __new_finish._M_node + 1); 1391 __throw_exception_again; 1392 } 1393 } 1394 } 1395 1396 template <class _Tp, class _Alloc> template <class _ForwardIterator> 1397 void deque<_Tp,_Alloc>::_M_insert_aux(iterator __pos, 1398 _ForwardIterator __first, 1399 _ForwardIterator __last, 1400 size_type __n) 1401 { 1402 const difference_type __elemsbefore = __pos - _M_start; 1403 size_type __length = size(); 1404 if (static_cast<size_type>(__elemsbefore) < __length / 2) { 1405 iterator __new_start = _M_reserve_elements_at_front(__n); 1406 iterator __old_start = _M_start; 1407 __pos = _M_start + __elemsbefore; 1408 try { 1409 if (__elemsbefore >= difference_type(__n)) { 1410 iterator __start_n = _M_start + difference_type(__n); 1411 uninitialized_copy(_M_start, __start_n, __new_start); 1412 _M_start = __new_start; 1413 copy(__start_n, __pos, __old_start); 1414 copy(__first, __last, __pos - difference_type(__n)); 1415 } 1416 else { 1417 _ForwardIterator __mid = __first; 1418 advance(__mid, difference_type(__n) - __elemsbefore); 1419 __uninitialized_copy_copy(_M_start, __pos, __first, __mid, 1420 __new_start); 1421 _M_start = __new_start; 1422 copy(__mid, __last, __old_start); 1423 } 1424 } 1425 catch(...) 1426 { 1427 _M_destroy_nodes(__new_start._M_node, _M_start._M_node); 1428 __throw_exception_again; 1429 } 1430 } 1431 else { 1432 iterator __new_finish = _M_reserve_elements_at_back(__n); 1433 iterator __old_finish = _M_finish; 1434 const difference_type __elemsafter = 1435 difference_type(__length) - __elemsbefore; 1436 __pos = _M_finish - __elemsafter; 1437 try { 1438 if (__elemsafter > difference_type(__n)) { 1439 iterator __finish_n = _M_finish - difference_type(__n); 1440 uninitialized_copy(__finish_n, _M_finish, _M_finish); 1441 _M_finish = __new_finish; 1442 copy_backward(__pos, __finish_n, __old_finish); 1443 copy(__first, __last, __pos); 1444 } 1445 else { 1446 _ForwardIterator __mid = __first; 1447 advance(__mid, __elemsafter); 1448 __uninitialized_copy_copy(__mid, __last, __pos, _M_finish, _M_finish); 1449 _M_finish = __new_finish; 1450 copy(__first, __mid, __pos); 1451 } 1452 } 1453 catch(...) 1454 { 1455 _M_destroy_nodes(_M_finish._M_node + 1, __new_finish._M_node + 1); 1456 __throw_exception_again; 1457 } 1458 } 1459 } 1460 1461 template <class _Tp, class _Alloc> 1462 void deque<_Tp,_Alloc>::_M_new_elements_at_front(size_type __new_elems) 1463 { 1464 size_type __new_nodes 1465 = (__new_elems + _S_buffer_size() - 1) / _S_buffer_size(); 1466 _M_reserve_map_at_front(__new_nodes); 1467 size_type __i; 1468 try { 1469 for (__i = 1; __i <= __new_nodes; ++__i) 1470 *(_M_start._M_node - __i) = _M_allocate_node(); 1471 } 1472 catch(...) { 1473 for (size_type __j = 1; __j < __i; ++__j) 1474 _M_deallocate_node(*(_M_start._M_node - __j)); 1475 __throw_exception_again; 1476 } 1477 } 1478 1479 template <class _Tp, class _Alloc> 1480 void deque<_Tp,_Alloc>::_M_new_elements_at_back(size_type __new_elems) 1481 { 1482 size_type __new_nodes 1483 = (__new_elems + _S_buffer_size() - 1) / _S_buffer_size(); 1484 _M_reserve_map_at_back(__new_nodes); 1485 size_type __i; 1486 try { 1487 for (__i = 1; __i <= __new_nodes; ++__i) 1488 *(_M_finish._M_node + __i) = _M_allocate_node(); 1489 } 1490 catch(...) { 1491 for (size_type __j = 1; __j < __i; ++__j) 1492 _M_deallocate_node(*(_M_finish._M_node + __j)); 1493 __throw_exception_again; 1494 } 1495 } 1496 1497 template <class _Tp, class _Alloc> 1498 void deque<_Tp,_Alloc>::_M_reallocate_map(size_type __nodes_to_add, 1499 bool __add_at_front) 1500 { 1501 size_type __old_num_nodes = _M_finish._M_node - _M_start._M_node + 1; 1502 size_type __new_num_nodes = __old_num_nodes + __nodes_to_add; 1503 1504 _Map_pointer __new_nstart; 1505 if (_M_map_size > 2 * __new_num_nodes) { 1506 __new_nstart = _M_map + (_M_map_size - __new_num_nodes) / 2 1507 + (__add_at_front ? __nodes_to_add : 0); 1508 if (__new_nstart < _M_start._M_node) 1509 copy(_M_start._M_node, _M_finish._M_node + 1, __new_nstart); 1510 else 1511 copy_backward(_M_start._M_node, _M_finish._M_node + 1, 1512 __new_nstart + __old_num_nodes); 1513 } 1514 else { 1515 size_type __new_map_size = 1516 _M_map_size + max(_M_map_size, __nodes_to_add) + 2; 1517 1518 _Map_pointer __new_map = _M_allocate_map(__new_map_size); 1519 __new_nstart = __new_map + (__new_map_size - __new_num_nodes) / 2 1520 + (__add_at_front ? __nodes_to_add : 0); 1521 copy(_M_start._M_node, _M_finish._M_node + 1, __new_nstart); 1522 _M_deallocate_map(_M_map, _M_map_size); 1523 1524 _M_map = __new_map; 1525 _M_map_size = __new_map_size; 1526 } 1527 1528 _M_start._M_set_node(__new_nstart); 1529 _M_finish._M_set_node(__new_nstart + __old_num_nodes - 1); 1530 } 1531 1532 1533 // Nonmember functions. 1534 1535 template <class _Tp, class _Alloc> 1536 inline bool operator==(const deque<_Tp, _Alloc>& __x, 1537 const deque<_Tp, _Alloc>& __y) { 1538 return __x.size() == __y.size() && 1539 equal(__x.begin(), __x.end(), __y.begin()); 1540 } 1541 1542 template <class _Tp, class _Alloc> 1543 inline bool operator<(const deque<_Tp, _Alloc>& __x, 1544 const deque<_Tp, _Alloc>& __y) { 1545 return lexicographical_compare(__x.begin(), __x.end(), 1546 __y.begin(), __y.end()); 1547 } 1548 1549 template <class _Tp, class _Alloc> 1550 inline bool operator!=(const deque<_Tp, _Alloc>& __x, 1551 const deque<_Tp, _Alloc>& __y) { 1552 return !(__x == __y); 1553 } 1554 1555 template <class _Tp, class _Alloc> 1556 inline bool operator>(const deque<_Tp, _Alloc>& __x, 1557 const deque<_Tp, _Alloc>& __y) { 1558 return __y < __x; 1559 } 1560 1561 template <class _Tp, class _Alloc> 1562 inline bool operator<=(const deque<_Tp, _Alloc>& __x, 1563 const deque<_Tp, _Alloc>& __y) { 1564 return !(__y < __x); 1565 } 1566 template <class _Tp, class _Alloc> 1567 inline bool operator>=(const deque<_Tp, _Alloc>& __x, 1568 const deque<_Tp, _Alloc>& __y) { 1569 return !(__x < __y); 1570 } 1571 1572 template <class _Tp, class _Alloc> 1573 inline void swap(deque<_Tp,_Alloc>& __x, deque<_Tp,_Alloc>& __y) { 1574 __x.swap(__y); 1575 } 1576 1577 } // namespace std 1578 1579 #endif /* __GLIBCPP_INTERNAL_DEQUE_H */ 1580 1581