📄 stl_vector.h
字号:
// Vector implementation -*- C++ -*-// Copyright (C) 2001, 2002 Free Software Foundation, Inc.//// This file is part of the GNU ISO C++ Library. This library is free// software; you can redistribute it and/or modify it under the// terms of the GNU General Public License as published by the// Free Software Foundation; either version 2, or (at your option)// any later version.// This library is distributed in the hope that it will be useful,// but WITHOUT ANY WARRANTY; without even the implied warranty of// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the// GNU General Public License for more details.// You should have received a copy of the GNU General Public License along// with this library; see the file COPYING. If not, write to the Free// Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,// USA.// As a special exception, you may use this file as part of a free software// library without restriction. Specifically, if other files instantiate// templates or use macros or inline functions from this file, or you compile// this file and link it with other files to produce an executable, this// file does not by itself cause the resulting executable to be covered by// the GNU General Public License. This exception does not however// invalidate any other reasons why the executable file might be covered by// the GNU General Public License./* * * Copyright (c) 1994 * Hewlett-Packard Company * * Permission to use, copy, modify, distribute and sell this software * and its documentation for any purpose is hereby granted without fee, * provided that the above copyright notice appear in all copies and * that both that copyright notice and this permission notice appear * in supporting documentation. Hewlett-Packard Company makes no * representations about the suitability of this software for any * purpose. It is provided "as is" without express or implied warranty. * * * Copyright (c) 1996 * Silicon Graphics Computer Systems, Inc. * * Permission to use, copy, modify, distribute and sell this software * and its documentation for any purpose is hereby granted without fee, * provided that the above copyright notice appear in all copies and * that both that copyright notice and this permission notice appear * in supporting documentation. Silicon Graphics makes no * representations about the suitability of this software for any * purpose. It is provided "as is" without express or implied warranty. *//** @file stl_vector.h * This is an internal header file, included by other library headers. * You should not attempt to use it directly. */#ifndef __GLIBCPP_INTERNAL_VECTOR_H#define __GLIBCPP_INTERNAL_VECTOR_H#include <bits/stl_iterator_base_funcs.h>#include <bits/functexcept.h>#include <bits/concept_check.h>namespace std{ /// @if maint Primary default version. @endif /** * @if maint * See bits/stl_deque.h's _Deque_alloc_base for an explanation. * @endif */ template<typename _Tp, typename _Allocator, bool _IsStatic> class _Vector_alloc_base { public: typedef typename _Alloc_traits<_Tp, _Allocator>::allocator_type allocator_type; allocator_type get_allocator() const { return _M_data_allocator; } _Vector_alloc_base(const allocator_type& __a) : _M_data_allocator(__a), _M_start(0), _M_finish(0), _M_end_of_storage(0) { } protected: allocator_type _M_data_allocator; _Tp* _M_start; _Tp* _M_finish; _Tp* _M_end_of_storage; _Tp* _M_allocate(size_t __n) { return _M_data_allocator.allocate(__n); } void _M_deallocate(_Tp* __p, size_t __n) { if (__p) _M_data_allocator.deallocate(__p, __n); } }; /// @if maint Specialization for instanceless allocators. @endif template<typename _Tp, typename _Allocator> class _Vector_alloc_base<_Tp, _Allocator, true> { public: typedef typename _Alloc_traits<_Tp, _Allocator>::allocator_type allocator_type; allocator_type get_allocator() const { return allocator_type(); } _Vector_alloc_base(const allocator_type&) : _M_start(0), _M_finish(0), _M_end_of_storage(0) { } protected: _Tp* _M_start; _Tp* _M_finish; _Tp* _M_end_of_storage; typedef typename _Alloc_traits<_Tp, _Allocator>::_Alloc_type _Alloc_type; _Tp* _M_allocate(size_t __n) { return _Alloc_type::allocate(__n); } void _M_deallocate(_Tp* __p, size_t __n) { _Alloc_type::deallocate(__p, __n);} }; /** * @if maint * See bits/stl_deque.h's _Deque_base for an explanation. * @endif */ template<typename _Tp, typename _Alloc> struct _Vector_base : public _Vector_alloc_base<_Tp, _Alloc, _Alloc_traits<_Tp, _Alloc>::_S_instanceless> { public: typedef _Vector_alloc_base<_Tp, _Alloc, _Alloc_traits<_Tp, _Alloc>::_S_instanceless> _Base; typedef typename _Base::allocator_type allocator_type; _Vector_base(const allocator_type& __a) : _Base(__a) { } _Vector_base(size_t __n, const allocator_type& __a) : _Base(__a) { _M_start = _M_allocate(__n); _M_finish = _M_start; _M_end_of_storage = _M_start + __n; } ~_Vector_base() { _M_deallocate(_M_start, _M_end_of_storage - _M_start); } }; /** * @brief A standard container which offers fixed time access to individual * elements in any order. * * @ingroup Containers * @ingroup Sequences * * Meets the requirements of a <a href="tables.html#65">container</a>, a * <a href="tables.html#66">reversible container</a>, and a * <a href="tables.html#67">sequence</a>, including the * <a href="tables.html#68">optional sequence requirements</a> with the * %exception of @c push_front and @c pop_front. * * In some terminology a %vector can be described as a dynamic C-style array, * it offers fast and efficient access to individual elements in any order * and saves the user from worrying about memory and size allocation. * Subscripting ( @c [] ) access is also provided as with C-style arrays. */ template<typename _Tp, typename _Alloc = allocator<_Tp> > class vector : protected _Vector_base<_Tp, _Alloc> { // Concept requirements. __glibcpp_class_requires(_Tp, _SGIAssignableConcept) typedef _Vector_base<_Tp, _Alloc> _Base; typedef vector<_Tp, _Alloc> vector_type; public: typedef _Tp value_type; typedef value_type* pointer; typedef const value_type* const_pointer; typedef __gnu_cxx::__normal_iterator<pointer, vector_type> iterator; typedef __gnu_cxx::__normal_iterator<const_pointer, vector_type> const_iterator; typedef std::reverse_iterator<const_iterator> const_reverse_iterator; typedef std::reverse_iterator<iterator> reverse_iterator; typedef value_type& reference; typedef const value_type& const_reference; typedef size_t size_type; typedef ptrdiff_t difference_type; typedef typename _Base::allocator_type allocator_type; protected: /** @if maint * These two functions and three data members are all from the * top-most base class, which varies depending on the type of * %allocator. They should be pretty self-explanatory, as * %vector uses a simple contiguous allocation scheme. @endif */ using _Base::_M_allocate; using _Base::_M_deallocate; using _Base::_M_start; using _Base::_M_finish; using _Base::_M_end_of_storage; public: // [23.2.4.1] construct/copy/destroy // (assign() and get_allocator() are also listed in this section) /** * @brief Default constructor creates no elements. */ explicit vector(const allocator_type& __a = allocator_type()) : _Base(__a) { } /** * @brief Create a %vector with copies of an exemplar element. * @param n The number of elements to initially create. * @param value An element to copy. * * This constructor fills the %vector with @a n copies of @a value. */ vector(size_type __n, const value_type& __value, const allocator_type& __a = allocator_type()) : _Base(__n, __a) { _M_finish = uninitialized_fill_n(_M_start, __n, __value); } /** * @brief Create a %vector with default elements. * @param n The number of elements to initially create. * * This constructor fills the %vector with @a n copies of a * default-constructed element. */ explicit vector(size_type __n) : _Base(__n, allocator_type()) { _M_finish = uninitialized_fill_n(_M_start, __n, value_type()); } /** * @brief %Vector copy constructor. * @param x A %vector of identical element and allocator types. * * The newly-created %vector uses a copy of the allocation * object used by @a x. All the elements of @a x are copied, * but any extra memory in * @a x (for fast expansion) will not be copied. */ vector(const vector& __x) : _Base(__x.size(), __x.get_allocator()) { _M_finish = uninitialized_copy(__x.begin(), __x.end(), _M_start); } /** * @brief Builds a %vector from a range. * @param first An input iterator. * @param last An input iterator. * * Create a %vector consisting of copies of the elements from * [first,last). * * If the iterators are forward, bidirectional, or random-access, then * this will call the elements' copy constructor N times (where N is * distance(first,last)) and do no memory reallocation. But if only * input iterators are used, then this will do at most 2N calls to the * copy constructor, and logN memory reallocations. */ template<typename _InputIterator> vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a = allocator_type()) : _Base(__a) { // Check whether it's an integral type. If so, it's not an iterator. typedef typename _Is_integer<_InputIterator>::_Integral _Integral; _M_initialize_dispatch(__first, __last, _Integral()); } /** * The dtor only erases the elements, and note that if the elements * themselves are pointers, the pointed-to memory is not touched in any * way. Managing the pointer is the user's responsibilty. */ ~vector() { _Destroy(_M_start, _M_finish); } /** * @brief %Vector assignment operator. * @param x A %vector of identical element and allocator types. * * All the elements of @a x are copied, but any extra memory in * @a x (for fast expansion) will not be copied. Unlike the * copy constructor, the allocator object is not copied. */ vector& operator=(const vector& __x); /** * @brief Assigns a given value to a %vector. * @param n Number of elements to be assigned. * @param val Value to be assigned. * * This function fills a %vector with @a n copies of the given * value. Note that the assignment completely changes the * %vector and that the resulting %vector's size is the same as * the number of elements assigned. Old data may be lost. */ void assign(size_type __n, const value_type& __val) { _M_fill_assign(__n, __val); } /** * @brief Assigns a range to a %vector. * @param first An input iterator. * @param last An input iterator. * * This function fills a %vector with copies of the elements in the * range [first,last). *
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -