📄 vector
字号:
// -*- C++ -*-
/***************************************************************************
*
* vector - declarations for the Standard Library vector class
*
* $Id: vector,v 1.3 2002/08/14 10:30:22 achapman Exp $
*
***************************************************************************
*
* 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) 1994-2001 Rogue Wave Software, Inc. All Rights Reserved.
*
* This computer software is owned by Rogue Wave Software, Inc. and is
* protected by U.S. copyright laws and other laws and by international
* treaties. This computer software is furnished by Rogue Wave Software,
* Inc. pursuant to a written license agreement and may be used, copied,
* transmitted, and stored only in accordance with the terms of such
* license and with the inclusion of the above copyright notice. This
* computer software or any other copies thereof may not be provided or
* otherwise made available to any other person.
*
* U.S. Government Restricted Rights. This computer software is provided
* with Restricted Rights. Use, duplication, or disclosure by the
* Government is subject to restrictions as set forth in subparagraph (c)
* (1) (ii) of The Rights in Technical Data and Computer Software clause
* at DFARS 252.227-7013 or subparagraphs (c) (1) and (2) of the
* Commercial Computer Software--Restricted Rights at 48 CFR 52.227-19,
* as applicable. Manufacturer is Rogue Wave Software, Inc., 5500
* Flatiron Parkway, Boulder, Colorado 80301 USA.
*
**************************************************************************/
#ifndef _RWSTD_VECTOR_INCLUDED
#define _RWSTD_VECTOR_INCLUDED
#include <limits>
#include <memory>
#include <rw/_algobase.h>
#include <rw/_iterator.h>
#include <rw/_defs.h>
#include <rw/_dispatch.h>
#include <rw/_error.h>
_RWSTD_NAMESPACE_BEGIN (std)
template <class _TypeT,
class _Allocator _RWSTD_COMPLEX_DEFAULT(allocator<_TypeT>) >
class vector : private _Allocator
{
public:
typedef _TypeT value_type;
typedef _Allocator allocator_type;
typedef _TYPENAME allocator_type::size_type size_type;
typedef _TYPENAME allocator_type::difference_type difference_type;
typedef _TYPENAME allocator_type::reference reference;
typedef _TYPENAME allocator_type::const_reference const_reference;
typedef _TYPENAME allocator_type::pointer pointer;
typedef _TYPENAME allocator_type::const_pointer const_pointer;
#ifndef _RWSTD_NO_DEBUG_ITER
typedef _RW::__rw_debug_iter <vector, pointer, pointer> iterator;
typedef _RW::__rw_debug_iter <vector, const_pointer, pointer>
const_iterator;
iterator _C_make_iter (pointer __ptr) {
return iterator (*this, __ptr);
}
const_iterator _C_make_iter (pointer __ptr) const {
return const_iterator (*this, __ptr);
}
#else // if defined (_RWSTD_NO_DEBUG_ITER)
typedef pointer iterator;
typedef const_pointer const_iterator;
iterator _C_make_iter (pointer __ptr) {
return __ptr;
}
const_iterator _C_make_iter (const_pointer __ptr) const {
return __ptr;
}
#endif // _RWSTD_NO_DEBUG_ITER
#ifndef _RWSTD_NO_CLASS_PARTIAL_SPEC
typedef _STD::reverse_iterator<const_iterator> const_reverse_iterator;
typedef _STD::reverse_iterator<iterator> reverse_iterator;
#else
typedef _STD::reverse_iterator<const_iterator,
random_access_iterator_tag, value_type,
const_reference, const_pointer, difference_type>
const_reverse_iterator;
typedef _STD::reverse_iterator<iterator,
random_access_iterator_tag, value_type,
reference, pointer, difference_type>
reverse_iterator;
#endif
protected:
typedef _RWSTD_ALLOC_TYPE (_Allocator, value_type) _C_value_alloc_type;
pointer _C_start;
pointer _C_finish;
pointer _C_end_of_storage;
void _C_insert_aux (iterator, const_reference);
void _C_insert_aux (iterator, size_type, const_reference);
#ifndef _RWSTD_NO_MEMBER_TEMPLATES
template<class _InputIter>
void _C_insert_aux (iterator __position, _InputIter __first,
_InputIter __last, _RW_is_not_integer) {
_C_insert_aux2 (__position, __first, __last);
}
template<class _InputIter>
void _C_insert_aux (iterator __position, _InputIter __first,
_InputIter __last, _RW_is_integer) {
_C_insert_aux (__position, (size_type)__first, __last);
}
template<class _InputIter>
void _C_insert_aux2 (iterator __position, _InputIter __first,
_InputIter __last);
template <class _InputIter>
void _C_insert_interval_dispatch (iterator __position,
_InputIter __first,
_InputIter __last,
forward_iterator_tag) {
typedef _TYPENAME _RWdispatch<_InputIter>::_RWtype _RWtype;
_C_insert_aux(__position, __first, __last, _RWtype());
}
template <class _InputIter>
void _C_insert_interval_dispatch (iterator __position,
_InputIter __first,
_InputIter __last,
input_iterator_tag) {
while(__first != __last) {
__position = insert (__position,*__first);
++__position;
++__first;
}
}
#else
void _C_insert_aux2 (iterator, const_iterator, const_iterator);
#endif
void _C_destroy (iterator __start, iterator __finish) {
for ( ; __start != __finish; ++__start)
_RWSTD_VALUE_ALLOC (_C_value_alloc_type, destroy (&*__start));
}
//
// Allocate buffers and fill with __n values
//
void _C_initn(size_type __n, const_reference __value) {
size_t __initial_capacity =
max (__n, (size_t)_RW::__rw_new_capacity(0,this));
_C_start = _RWSTD_VALUE_ALLOC(_C_value_alloc_type,
allocate(__initial_capacity,0));
_TRY {
uninitialized_fill_n(begin(), __n, __value,
_RWSTD_VALUE_ALLOC_CAST (*this));
}
_CATCH (...) {
_RWSTD_VALUE_ALLOC(_C_value_alloc_type, deallocate(_C_start,__n));
_RETHROW;
}
_C_finish = _C_start + __n;
_C_end_of_storage = _C_start + __initial_capacity;
}
public:
_EXPLICIT vector (const _Allocator& __alloc = allocator_type ())
: allocator_type (__alloc), _C_start (0), _C_finish (0),
_C_end_of_storage (0){ }
_EXPLICIT
vector (size_type __n, const_reference __value = value_type (),
const _Allocator& __alloc = allocator_type ())
: allocator_type(__alloc), _C_start(0), _C_finish(0),
_C_end_of_storage (0) {
_C_initn (size_type (__n), __value);
}
#ifndef _RWSTD_NO_MEMBER_TEMPLATES
template<class _InputIter>
void _C_init_aux (_InputIter __first, _InputIter __last,
_RW_is_not_integer) {
if (__is_input_iterator (_RWSTD_ITERATOR_CATEGORY (_InputIter,
__first))) {
copy(__first, __last, back_inserter(*this));
}
else {
size_type __n = _DISTANCE (__first, __last, size_type);
size_t __initial_capacity =
max (__n, (size_t)_RW::__rw_new_capacity(0,this));
_C_start = _RWSTD_VALUE_ALLOC(_C_value_alloc_type,
allocate(__initial_capacity,0));
_TRY {
uninitialized_copy(__first, __last, _C_start,
_RWSTD_VALUE_ALLOC_CAST (*this));
}
_CATCH (...) {
_RWSTD_VALUE_ALLOC(_C_value_alloc_type,
deallocate(_C_start,__n));
_RETHROW;
}
_C_finish = _C_start + __n;
_C_end_of_storage = _C_start + __initial_capacity;
}
}
template<class _InputIter>
void _C_init_aux (_InputIter __first, _InputIter __last,
_RW_is_integer) {
_C_initn((size_type)__first,__last);
}
template<class _InputIter>
vector (_InputIter __first, _InputIter __last,
const _Allocator& __alloc = allocator_type ())
: allocator_type(__alloc), _C_start(0), _C_finish(0),
_C_end_of_storage(0) {
typedef _TYPENAME _RWdispatch<_InputIter>::_RWtype _RWtype;
_C_init_aux(__first, __last, _RWtype());
}
#else // defined _RWSTD_NO_MEMBER_TEMPLATES
vector (const_iterator __first, const_iterator __last,
const _Allocator& __alloc = allocator_type ())
: allocator_type(__alloc), _C_start(0), _C_finish(0),
_C_end_of_storage(0) {
size_type __n = _DISTANCE (__first, __last, size_type);
size_t __initial_capacity =
max (__n, (size_t)_RW::__rw_new_capacity(0,this));
_C_start = _RWSTD_VALUE_ALLOC(_C_value_alloc_type,
allocate(__initial_capacity,0));
_TRY {
uninitialized_copy(__first, __last, _C_start,
_RWSTD_VALUE_ALLOC_CAST (*this));
}
_CATCH (...) {
_RWSTD_VALUE_ALLOC(_C_value_alloc_type, deallocate(_C_start,__n));
_RETHROW;
}
_C_finish = _C_start + __n;
_C_end_of_storage = _C_start + __initial_capacity;
}
#endif // _RWSTD_NO_MEMBER_TEMPLATES
vector (const vector &__x)
: allocator_type (__x.get_allocator ()), _C_start(0), _C_finish(0),
_C_end_of_storage(0) {
size_t __initial_capacity =
max (__x.size(), (size_t)_RW::__rw_new_capacity(0,this));
_C_start = _RWSTD_VALUE_ALLOC(_C_value_alloc_type,
allocate(__initial_capacity,0));
_TRY {
uninitialized_copy(__x.begin(), __x.end(), begin(),
_RWSTD_VALUE_ALLOC_CAST (*this));
}
_CATCH (...) {
_RWSTD_VALUE_ALLOC(_C_value_alloc_type,
deallocate(_C_start,__x.size()));
_RETHROW;
}
_C_finish = _C_start + __x.size();
_C_end_of_storage = _C_start + __initial_capacity;
}
~vector () {
_C_destroy(begin (), end ());
_RWSTD_VALUE_ALLOC(_C_value_alloc_type, deallocate(_C_start,
_C_end_of_storage - _C_start));
}
vector& operator= (const vector &__x);
#ifndef _RWSTD_NO_MEMBER_TEMPLATES
template<class _InputIter>
void assign (_InputIter __first, _InputIter __last) {
erase(begin(), end());
typedef _TYPENAME _RWdispatch<_InputIter>::_RWtype _RWtype;
_C_insert_aux(begin(), __first, __last, _RWtype());
}
void assign (size_type __n, const_reference __t) {
erase(begin(), end()); insert(begin(), __n, __t);
}
#else
void assign (const_iterator __first, const_iterator __last) {
erase(begin(), end()); insert(begin(), __first, __last);
}
//
// Assign n copies of t to this vector.
//
void assign (size_type __n, const_reference __t) {
erase(begin(), end()); insert(begin(), __n, __t);
}
#endif // _RWSTD_NO_MEMBER_TEMPLATES
allocator_type get_allocator() const {
return *this;
}
//
// Iterators.
//
iterator begin () {
return _C_make_iter (_C_start);
}
const_iterator begin () const {
return _C_make_iter (_C_start);
}
iterator end () {
return _C_make_iter (_C_finish);
}
const_iterator end () const {
return _C_make_iter (_C_finish);
}
reverse_iterator rbegin () {
reverse_iterator __tmp(end());
return __tmp;
}
const_reverse_iterator rbegin () const {
const_reverse_iterator __tmp(end());
return __tmp;
}
reverse_iterator rend () {
reverse_iterator __tmp(begin());
return __tmp;
}
const_reverse_iterator rend () const {
const_reverse_iterator __tmp(begin());
return __tmp;
}
size_type size () const {
return size_type(end() - begin());
}
size_type max_size () const {
return _RWSTD_VALUE_ALLOC(_C_value_alloc_type, max_size());
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -