vector.hpp
来自「Boost provides free peer-reviewed portab」· HPP 代码 · 共 1,703 行 · 第 1/5 页
HPP
1,703 行
//! <b>Throws</b>: Nothing. //! //! <b>Complexity</b>: Constant. pointer data() { return this->members_.m_start; } //! <b>Returns</b>: A pointer such that [data(),data() + size()) is a valid range. //! For a non-empty vector, data() == &front(). //! //! <b>Throws</b>: Nothing. //! //! <b>Complexity</b>: Constant. const_pointer data() const { return this->members_.m_start; } //! <b>Effects</b>: Returns the number of the elements contained in the vector. //! //! <b>Throws</b>: Nothing. //! //! <b>Complexity</b>: Constant. size_type size() const { return this->members_.m_size; } //! <b>Effects</b>: Returns the largest possible size of the vector. //! //! <b>Throws</b>: Nothing. //! //! <b>Complexity</b>: Constant. size_type max_size() const { return this->alloc().max_size(); } //! <b>Effects</b>: Number of elements for which memory has been allocated. //! capacity() is always greater than or equal to size(). //! //! <b>Throws</b>: Nothing. //! //! <b>Complexity</b>: Constant. size_type capacity() const { return this->members_.m_capacity; } //! <b>Effects</b>: Returns true if the vector contains no elements. //! //! <b>Throws</b>: Nothing. //! //! <b>Complexity</b>: Constant. bool empty() const { return !this->members_.m_size; } //! <b>Requires</b>: size() < n. //! //! <b>Effects</b>: Returns a reference to the nth element //! from the beginning of the container. //! //! <b>Throws</b>: Nothing. //! //! <b>Complexity</b>: Constant. reference operator[](size_type n) { return this->members_.m_start[n]; } //! <b>Requires</b>: size() < n. //! //! <b>Effects</b>: Returns a const reference to the nth element //! from the beginning of the container. //! //! <b>Throws</b>: Nothing. //! //! <b>Complexity</b>: Constant. const_reference operator[](size_type n) const { return this->members_.m_start[n]; } //! <b>Requires</b>: size() < n. //! //! <b>Effects</b>: Returns a reference to the nth element //! from the beginning of the container. //! //! <b>Throws</b>: std::range_error if n >= size() //! //! <b>Complexity</b>: Constant. reference at(size_type n) { this->priv_check_range(n); return this->members_.m_start[n]; } //! <b>Requires</b>: size() < n. //! //! <b>Effects</b>: Returns a const reference to the nth element //! from the beginning of the container. //! //! <b>Throws</b>: std::range_error if n >= size() //! //! <b>Complexity</b>: Constant. const_reference at(size_type n) const { this->priv_check_range(n); return this->members_.m_start[n]; } //! <b>Effects</b>: Returns a copy of the internal allocator. //! //! <b>Throws</b>: If allocator's copy constructor throws. //! //! <b>Complexity</b>: Constant. allocator_type get_allocator() const { return this->alloc(); } const stored_allocator_type &get_stored_allocator() const { return this->alloc(); } stored_allocator_type &get_stored_allocator() { return this->alloc(); } //! <b>Effects</b>: If n is less than or equal to capacity(), this call has no //! effect. Otherwise, it is a request for allocation of additional memory. //! If the request is successful, then capacity() is greater than or equal to //! n; otherwise, capacity() is unchanged. In either case, size() is unchanged. //! //! <b>Throws</b>: If memory allocation allocation throws or T's copy constructor throws. void reserve(size_type new_cap) { if (this->capacity() < new_cap){ //There is not enough memory, allocate a new //buffer or expand the old one. bool same_buffer_start; size_type real_cap = 0; std::pair<pointer, bool> ret = this->allocation_command (allocate_new | expand_fwd | expand_bwd, new_cap, new_cap, real_cap, this->members_.m_start); //Check for forward expansion same_buffer_start = ret.second && this->members_.m_start == ret.first; if(same_buffer_start){ #ifdef BOOST_INTERPROCESS_VECTOR_ALLOC_STATS ++this->num_expand_fwd; #endif this->members_.m_capacity = real_cap; } //If there is no forward expansion, move objects else{ //We will reuse insert code, so create a dummy input iterator typename value_traits::copy_move_it dummy_it(detail::get_pointer(this->members_.m_start)); detail::advanced_insert_aux_proxy<T, typename value_traits::copy_move_it, T*> proxy(dummy_it, dummy_it); //Backwards (and possibly forward) expansion if(ret.second){ #ifdef BOOST_INTERPROCESS_VECTOR_ALLOC_STATS ++this->num_expand_bwd; #endif this->priv_range_insert_expand_backwards ( detail::get_pointer(ret.first) , real_cap , detail::get_pointer(this->members_.m_start) , 0 , proxy); } //New buffer else{ #ifdef BOOST_INTERPROCESS_VECTOR_ALLOC_STATS ++this->num_alloc; #endif this->priv_range_insert_new_allocation ( detail::get_pointer(ret.first) , real_cap , detail::get_pointer(this->members_.m_start) , 0 , proxy); } } } } //! <b>Effects</b>: Makes *this contain the same elements as x. //! //! <b>Postcondition</b>: this->size() == x.size(). *this contains a copy //! of each of x's elements. //! //! <b>Throws</b>: If memory allocation throws or T's copy constructor throws. //! //! <b>Complexity</b>: Linear to the number of elements in x. vector<T, A>& operator=(const vector<T, A>& x) { if (&x != this){ this->assign(x.members_.m_start, x.members_.m_start + x.members_.m_size); } return *this; } //! <b>Effects</b>: Move assignment. All mx's values are transferred to *this. //! //! <b>Postcondition</b>: x.empty(). *this contains a the elements x had //! before the function. //! //! <b>Throws</b>: If allocator_type's copy constructor throws. //! //! <b>Complexity</b>: Constant. #ifndef BOOST_INTERPROCESS_RVALUE_REFERENCE vector<T, A>& operator=(const detail::moved_object<vector<T, A> >& mx) { vector<T, A> &x = mx.get(); #else vector<T, A>& operator=(vector<T, A> && x) { #endif if (&x != this){ this->swap(x); x.clear(); } return *this; } //! <b>Effects</b>: Assigns the n copies of val to *this. //! //! <b>Throws</b>: If memory allocation throws or T's copy constructor throws. //! //! <b>Complexity</b>: Linear to n. void assign(size_type n, const value_type& val) { this->assign(cvalue_iterator(val, n), cvalue_iterator()); } //! <b>Effects</b>: Assigns the the range [first, last) to *this. //! //! <b>Throws</b>: If memory allocation throws or //! T's constructor from dereferencing InpIt throws. //! //! <b>Complexity</b>: Linear to n. template <class InIt> void assign(InIt first, InIt last) { //Dispatch depending on integer/iterator const bool aux_boolean = detail::is_convertible<InIt, std::size_t>::value; typedef detail::bool_<aux_boolean> Result; this->priv_assign_dispatch(first, last, Result()); } //! <b>Effects</b>: Inserts a copy of x at the end of the vector. //! //! <b>Throws</b>: If memory allocation throws or //! T's copy constructor throws. //! //! <b>Complexity</b>: Amortized constant time. void push_back(const T& x) { if (this->members_.m_size < this->members_.m_capacity){ //There is more memory, just construct a new object at the end new((void*)(detail::get_pointer(this->members_.m_start) + this->members_.m_size))value_type(x); ++this->members_.m_size; } else{ this->insert(this->cend(), x); } } //! <b>Effects</b>: Constructs a new element in the end of the vector //! and moves the resources of mx to this new element. //! //! <b>Throws</b>: If memory allocation throws. //! //! <b>Complexity</b>: Amortized constant time. #ifndef BOOST_INTERPROCESS_RVALUE_REFERENCE void push_back(const detail::moved_object<T> & mx) { value_type &x = mx.get(); #else void push_back(T && x) { #endif if (this->members_.m_size < this->members_.m_capacity){ //There is more memory, just construct a new object at the end new((void*)detail::get_pointer(this->members_.m_start + this->members_.m_size))value_type(detail::move_impl(x)); ++this->members_.m_size; } else{ this->insert(this->cend(), detail::move_impl(x)); } } #ifdef BOOST_INTERPROCESS_PERFECT_FORWARDING //! <b>Effects</b>: Inserts an object of type T constructed with //! std::forward<Args>(args)... in the end of the vector. //! //! <b>Throws</b>: If memory allocation throws or the in-place constructor throws. //! //! <b>Complexity</b>: Amortized constant time. template<class ...Args> void emplace_back(Args &&...args) { T* back_pos = detail::get_pointer(this->members_.m_start) + this->members_.m_size; if (this->members_.m_size < this->members_.m_capacity){ //There is more memory, just construct a new object at the end new((void*)(back_pos))value_type(detail::forward_impl<Args>(args)...); ++this->members_.m_size; } else{ detail::advanced_insert_aux_emplace<T, T*, Args...> proxy (detail::forward_impl<Args>(args)...); priv_range_insert(back_pos, 1, proxy); } } //! <b>Requires</b>: position must be a valid iterator of *this. //! //! <b>Effects</b>: Inserts an object of type T constructed with //! std::forward<Args>(args)... before position //! //! <b>Throws</b>: If memory allocation throws or the in-place constructor throws. //! //! <b>Complexity</b>: If position is end(), amortized constant time //! Linear time otherwise. template<class ...Args> iterator emplace(const_iterator position, Args && ...args) { //Just call more general insert(pos, size, value) and return iterator size_type pos_n = position - cbegin(); detail::advanced_insert_aux_emplace<T, T*, Args...> proxy (detail::forward_impl<Args>(args)...); priv_range_insert(position.get_ptr(), 1, proxy); return iterator(this->members_.m_start + pos_n); } #else void emplace_back() { T* back_pos = detail::get_pointer(this->members_.m_start) + this->members_.m_size; if (this->members_.m_size < this->members_.m_capacity){ //There is more memory, just construct a new object at the end new((void*)(back_pos))value_type(); ++this->members_.m_size; } else{ detail::advanced_insert_aux_emplace<value_type, T*> proxy; priv_range_insert(back_pos, 1, proxy); } } iterator emplace(const_iterator position) { size_type pos_n = position - cbegin(); detail::advanced_insert_aux_emplace<value_type, T*> proxy; priv_range_insert(detail::get_pointer(position.get_ptr()), 1, proxy); return iterator(this->members_.m_start + pos_n); } #define BOOST_PP_LOCAL_MACRO(n) \ template<BOOST_PP_ENUM_PARAMS(n, class P)> \ void emplace_back(BOOST_PP_ENUM(n, BOOST_INTERPROCESS_PP_PARAM_LIST, _)) \
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?