OSDN Git Service

2004-07-04 Paolo Carlini <pcarlini@suse.de>
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / include / bits / vector.tcc
1 // Vector implementation (out of line) -*- C++ -*-
2
3 // Copyright (C) 2001, 2002, 2003, 2004 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) 1996
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 vector.tcc
57  *  This is an internal header file, included by other library headers.
58  *  You should not attempt to use it directly.
59  */
60
61 #ifndef _VECTOR_TCC
62 #define _VECTOR_TCC 1
63
64 namespace _GLIBCXX_STD
65 {
66   template<typename _Tp, typename _Alloc>
67     void
68     vector<_Tp, _Alloc>::
69     reserve(size_type __n)
70     {
71       if (__n > this->max_size())
72         __throw_length_error(__N("vector::reserve"));
73       if (this->capacity() < __n)
74         {
75           const size_type __old_size = size();
76           pointer __tmp = _M_allocate_and_copy(__n,
77                                                this->_M_impl._M_start,
78                                                this->_M_impl._M_finish);
79           std::_Destroy(this->_M_impl._M_start, this->_M_impl._M_finish);
80           _M_deallocate(this->_M_impl._M_start,
81                         this->_M_impl._M_end_of_storage
82                         - this->_M_impl._M_start);
83           this->_M_impl._M_start = __tmp;
84           this->_M_impl._M_finish = __tmp + __old_size;
85           this->_M_impl._M_end_of_storage = this->_M_impl._M_start + __n;
86         }
87     }
88
89   template<typename _Tp, typename _Alloc>
90     typename vector<_Tp, _Alloc>::iterator
91     vector<_Tp, _Alloc>::
92     insert(iterator __position, const value_type& __x)
93     {
94       const size_type __n = __position - begin();
95       if (this->_M_impl._M_finish != this->_M_impl._M_end_of_storage
96           && __position == end())
97         {
98           std::_Construct(this->_M_impl._M_finish, __x);
99           ++this->_M_impl._M_finish;
100         }
101       else
102         _M_insert_aux(__position, __x);
103       return begin() + __n;
104     }
105
106   template<typename _Tp, typename _Alloc>
107     typename vector<_Tp, _Alloc>::iterator
108     vector<_Tp, _Alloc>::
109     erase(iterator __position)
110     {
111       if (__position + 1 != end())
112         std::copy(__position + 1, end(), __position);
113       --this->_M_impl._M_finish;
114       std::_Destroy(this->_M_impl._M_finish);
115       return __position;
116     }
117
118   template<typename _Tp, typename _Alloc>
119     typename vector<_Tp, _Alloc>::iterator
120     vector<_Tp, _Alloc>::
121     erase(iterator __first, iterator __last)
122     {
123       iterator __i(copy(__last, end(), __first));
124       std::_Destroy(__i, end());
125       this->_M_impl._M_finish = this->_M_impl._M_finish - (__last - __first);
126       return __first;
127     }
128
129   template<typename _Tp, typename _Alloc>
130     vector<_Tp, _Alloc>&
131     vector<_Tp, _Alloc>::
132     operator=(const vector<_Tp, _Alloc>& __x)
133     {
134       if (&__x != this)
135         {
136           const size_type __xlen = __x.size();
137           if (__xlen > capacity())
138             {
139               pointer __tmp = _M_allocate_and_copy(__xlen, __x.begin(),
140                                                    __x.end());
141               std::_Destroy(this->_M_impl._M_start,
142                             this->_M_impl._M_finish);
143               _M_deallocate(this->_M_impl._M_start,
144                             this->_M_impl._M_end_of_storage
145                             - this->_M_impl._M_start);
146               this->_M_impl._M_start = __tmp;
147               this->_M_impl._M_end_of_storage = this->_M_impl._M_start + __xlen;
148             }
149           else if (size() >= __xlen)
150             {
151               iterator __i(copy(__x.begin(), __x.end(), begin()));
152               std::_Destroy(__i, end());
153             }
154           else
155             {
156               std::copy(__x.begin(), __x.begin() + size(),
157                         this->_M_impl._M_start);
158               std::uninitialized_copy(__x.begin() + size(),
159                                       __x.end(), this->_M_impl._M_finish);
160             }
161           this->_M_impl._M_finish = this->_M_impl._M_start + __xlen;
162         }
163       return *this;
164     }
165
166   template<typename _Tp, typename _Alloc>
167     void
168     vector<_Tp, _Alloc>::
169     _M_fill_assign(size_t __n, const value_type& __val)
170     {
171       if (__n > capacity())
172         {
173           vector __tmp(__n, __val, get_allocator());
174           __tmp.swap(*this);
175         }
176       else if (__n > size())
177         {
178           std::fill(begin(), end(), __val);
179           this->_M_impl._M_finish = std::uninitialized_fill_n(this->
180                                                               _M_impl._M_finish,
181                                                               __n - size(),
182                                                               __val);
183         }
184       else
185         erase(fill_n(begin(), __n, __val), end());
186     }
187
188   template<typename _Tp, typename _Alloc>
189     template<typename _InputIterator>
190       void
191       vector<_Tp, _Alloc>::
192       _M_assign_aux(_InputIterator __first, _InputIterator __last,
193                     input_iterator_tag)
194       {
195         iterator __cur(begin());
196         for (; __first != __last && __cur != end(); ++__cur, ++__first)
197           *__cur = *__first;
198         if (__first == __last)
199           erase(__cur, end());
200         else
201           insert(end(), __first, __last);
202       }
203
204   template<typename _Tp, typename _Alloc>
205     template<typename _ForwardIterator>
206       void
207       vector<_Tp, _Alloc>::
208       _M_assign_aux(_ForwardIterator __first, _ForwardIterator __last,
209                     forward_iterator_tag)
210       {
211         const size_type __len = std::distance(__first, __last);
212
213         if (__len > capacity())
214           {
215             pointer __tmp(_M_allocate_and_copy(__len, __first, __last));
216             std::_Destroy(this->_M_impl._M_start, this->_M_impl._M_finish);
217             _M_deallocate(this->_M_impl._M_start,
218                           this->_M_impl._M_end_of_storage
219                           - this->_M_impl._M_start);
220             this->_M_impl._M_start = __tmp;
221             this->_M_impl._M_finish = this->_M_impl._M_start + __len;
222             this->_M_impl._M_end_of_storage = this->_M_impl._M_finish;
223           }
224         else if (size() >= __len)
225           {
226             iterator __new_finish(copy(__first, __last,
227                                        this->_M_impl._M_start));
228             std::_Destroy(__new_finish, end());
229             this->_M_impl._M_finish = __new_finish.base();
230           }
231         else
232           {
233             _ForwardIterator __mid = __first;
234             std::advance(__mid, size());
235             std::copy(__first, __mid, this->_M_impl._M_start);
236             this->_M_impl._M_finish = std::uninitialized_copy(__mid,
237                                                               __last,
238                                                               this->_M_impl.
239                                                               _M_finish);
240           }
241       }
242
243   template<typename _Tp, typename _Alloc>
244     void
245     vector<_Tp, _Alloc>::
246     _M_insert_aux(iterator __position, const _Tp& __x)
247     {
248       if (this->_M_impl._M_finish != this->_M_impl._M_end_of_storage)
249         {
250           std::_Construct(this->_M_impl._M_finish,
251                           *(this->_M_impl._M_finish - 1));
252           ++this->_M_impl._M_finish;
253           _Tp __x_copy = __x;
254           std::copy_backward(__position,
255                              iterator(this->_M_impl._M_finish-2),
256                              iterator(this->_M_impl._M_finish-1));
257           *__position = __x_copy;
258         }
259       else
260         {
261           const size_type __old_size = size();
262           const size_type __len = __old_size != 0 ? 2 * __old_size : 1;
263           iterator __new_start(this->_M_allocate(__len));
264           iterator __new_finish(__new_start);
265           try
266             {
267               __new_finish = std::uninitialized_copy(iterator(this->
268                                                               _M_impl._M_start),
269                                                      __position,
270                                                      __new_start);
271               std::_Construct(__new_finish.base(), __x);
272               ++__new_finish;
273               __new_finish = std::uninitialized_copy(__position,
274                                                      iterator(this->_M_impl.
275                                                               _M_finish),
276                                                      __new_finish);
277           }
278           catch(...)
279             {
280               std::_Destroy(__new_start,__new_finish);
281               _M_deallocate(__new_start.base(),__len);
282               __throw_exception_again;
283             }
284           std::_Destroy(begin(), end());
285           _M_deallocate(this->_M_impl._M_start,
286                         this->_M_impl._M_end_of_storage
287                         - this->_M_impl._M_start);
288           this->_M_impl._M_start = __new_start.base();
289           this->_M_impl._M_finish = __new_finish.base();
290           this->_M_impl._M_end_of_storage = __new_start.base() + __len;
291         }
292     }
293
294   template<typename _Tp, typename _Alloc>
295     void
296     vector<_Tp, _Alloc>::
297     _M_fill_insert(iterator __position, size_type __n, const value_type& __x)
298     {
299       if (__n != 0)
300         {
301           if (size_type(this->_M_impl._M_end_of_storage
302                         - this->_M_impl._M_finish) >= __n)
303             {
304               value_type __x_copy = __x;
305               const size_type __elems_after = end() - __position;
306               iterator __old_finish(this->_M_impl._M_finish);
307               if (__elems_after > __n)
308                 {
309                   std::uninitialized_copy(this->_M_impl._M_finish - __n,
310                                           this->_M_impl._M_finish,
311                                           this->_M_impl._M_finish);
312                   this->_M_impl._M_finish += __n;
313                   std::copy_backward(__position, __old_finish - __n,
314                                      __old_finish);
315                   std::fill(__position, __position + __n, __x_copy);
316                 }
317               else
318                 {
319                   std::uninitialized_fill_n(this->_M_impl._M_finish,
320                                             __n - __elems_after,
321                                             __x_copy);
322                   this->_M_impl._M_finish += __n - __elems_after;
323                   std::uninitialized_copy(__position, __old_finish,
324                                           this->_M_impl._M_finish);
325                   this->_M_impl._M_finish += __elems_after;
326                   std::fill(__position, __old_finish, __x_copy);
327                 }
328             }
329           else
330             {
331               const size_type __old_size = size();
332               const size_type __len = __old_size + std::max(__old_size, __n);
333               iterator __new_start(this->_M_allocate(__len));
334               iterator __new_finish(__new_start);
335               try
336                 {
337                   __new_finish = std::uninitialized_copy(begin(), __position,
338                                                          __new_start);
339                   __new_finish = std::uninitialized_fill_n(__new_finish, __n,
340                                                            __x);
341                   __new_finish = std::uninitialized_copy(__position, end(),
342                                                          __new_finish);
343                 }
344               catch(...)
345                 {
346                   std::_Destroy(__new_start,__new_finish);
347                   _M_deallocate(__new_start.base(),__len);
348                   __throw_exception_again;
349                 }
350               std::_Destroy(this->_M_impl._M_start, this->_M_impl._M_finish);
351               _M_deallocate(this->_M_impl._M_start,
352                             this->_M_impl._M_end_of_storage
353                             - this->_M_impl._M_start);
354               this->_M_impl._M_start = __new_start.base();
355               this->_M_impl._M_finish = __new_finish.base();
356               this->_M_impl._M_end_of_storage = __new_start.base() + __len;
357             }
358         }
359     }
360
361   template<typename _Tp, typename _Alloc> template<typename _InputIterator>
362     void
363     vector<_Tp, _Alloc>::
364     _M_range_insert(iterator __pos, _InputIterator __first,
365                     _InputIterator __last, input_iterator_tag)
366     {
367       for (; __first != __last; ++__first)
368         {
369           __pos = insert(__pos, *__first);
370           ++__pos;
371         }
372     }
373
374   template<typename _Tp, typename _Alloc>
375     template<typename _ForwardIterator>
376       void
377       vector<_Tp, _Alloc>::
378       _M_range_insert(iterator __position,_ForwardIterator __first,
379                       _ForwardIterator __last, forward_iterator_tag)
380       {
381         if (__first != __last)
382           {
383             const size_type __n = std::distance(__first, __last);
384             if (size_type(this->_M_impl._M_end_of_storage
385                           - this->_M_impl._M_finish) >= __n)
386               {
387                 const size_type __elems_after = end() - __position;
388                 iterator __old_finish(this->_M_impl._M_finish);
389                 if (__elems_after > __n)
390                   {
391                     std::uninitialized_copy(this->_M_impl._M_finish - __n,
392                                             this->_M_impl._M_finish,
393                                             this->_M_impl._M_finish);
394                     this->_M_impl._M_finish += __n;
395                     std::copy_backward(__position, __old_finish - __n,
396                                        __old_finish);
397                     std::copy(__first, __last, __position);
398                   }
399                 else
400                   {
401                     _ForwardIterator __mid = __first;
402                     std::advance(__mid, __elems_after);
403                     std::uninitialized_copy(__mid, __last,
404                                             this->_M_impl._M_finish);
405                     this->_M_impl._M_finish += __n - __elems_after;
406                     std::uninitialized_copy(__position, __old_finish,
407                                             this->_M_impl._M_finish);
408                     this->_M_impl._M_finish += __elems_after;
409                     std::copy(__first, __mid, __position);
410                   }
411               }
412             else
413               {
414                 const size_type __old_size = size();
415                 const size_type __len = __old_size + std::max(__old_size, __n);
416                 iterator __new_start(this->_M_allocate(__len));
417                 iterator __new_finish(__new_start);
418                 try
419                   {
420                     __new_finish = std::uninitialized_copy(iterator(this->
421                                                                     _M_impl.
422                                                                     _M_start),
423                                                            __position,
424                                                            __new_start);
425                     __new_finish = std::uninitialized_copy(__first, __last,
426                                                            __new_finish);
427                     __new_finish = std::uninitialized_copy(__position,
428                                                            iterator(this->
429                                                                     _M_impl.
430                                                                     _M_finish),
431                                                            __new_finish);
432                   }
433                 catch(...)
434                   {
435                     std::_Destroy(__new_start,__new_finish);
436                     _M_deallocate(__new_start.base(), __len);
437                     __throw_exception_again;
438                   }
439                 std::_Destroy(this->_M_impl._M_start, this->_M_impl._M_finish);
440                 _M_deallocate(this->_M_impl._M_start,
441                               this->_M_impl._M_end_of_storage
442                               - this->_M_impl._M_start);
443                 this->_M_impl._M_start = __new_start.base();
444                 this->_M_impl._M_finish = __new_finish.base();
445                 this->_M_impl._M_end_of_storage = __new_start.base() + __len;
446               }
447           }
448       }
449 } // namespace std
450
451 #endif /* _VECTOR_TCC */