文件位置:young/y_vector.hpp
/*
The young Library
Copyright (c) 2005 by 杨桓
Permission to use, copy, modify, distribute and sell this software 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.
The author make no representations about the suitability of this software
for any purpose. It is provided "as is" without express or implied warranty.
*/
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
#ifndef __MACRO_CPLUSPLUS_YOUNG_LIBRARY_VECTOR_HEADER_FILE__
#define __MACRO_CPLUSPLUS_YOUNG_LIBRARY_VECTOR_HEADER_FILE__
//-----------------------------------------------------------------------------
#include "y_allocator.hpp"
#include "y_initialization.hpp"
#include "y_exception.hpp"
#include "algorithm/y_algorithm_base.hpp"
#include "algorithm/y_algorithm_copy.hpp"
#include "algorithm/y_algorithm_compare.hpp"
#include "algorithm/y_algorithm_fill.hpp"
//-----------------------------------------------------------------------------
__MACRO_CPLUSPLUS_YOUNG_LIBRARY_BEGIN_NAMESPACE__
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
template< typename T, typename Allocator = allocator<T> >
class vector
{
public:
typedef vector<T, Allocator> self;
typedef Allocator allocator_type;
typedef T value_type;
typedef value_type& reference;
typedef const value_type& const_reference;
typedef value_type* pointer;
typedef const value_type* const_pointer;
typedef def_size_t size_type;
typedef def_ptrdiff_t difference_type;
typedef value_type* iterator;
typedef const value_type* const_iterator;
typedef Reverse_Iterator<iterator> reverse_iterator;
typedef Reverse_Iterator<const_iterator> const_reverse_iterator;
protected:
pointer m_start, m_finish, m_storage;
allocator_type m_alloc;
public:
vector() : m_start(NULL_POINTER),
m_finish(NULL_POINTER),
m_storage(NULL_POINTER) {}
explicit vector( size_type size )
{ fill_init( size, value_type() ); }
vector( size_type size, const_reference value )
{ fill_init( size, value ); }
vector( int size, const_reference value )
{ fill_init( static_cast<size_type>( size ), value ); }
vector( long size, const_reference value )
{ fill_init( static_cast<size_type>( size ), value ); }
template< typename InputIterator >
vector( InputIterator first, InputIterator last, size_type size = 0 )
{
typedef typename iterator_traits<InputIterator>::iterator_category
cate;
range_init( first, last, size, cate() );
}
vector( const self& rhs )
{
alloc_data( rhs.size() );
try
{
init_copy( rhs.begin(), rhs.end(), m_start );
}
catch(...)
{
dealloc_data();
throw;
}
m_finish = m_start + rhs.size();
}
self& operator=( const self& rhs )
{
if( this != &rhs )
assign( rhs.begin(), rhs.end(), rhs.size() );
return *this;
}
~vector()
{
destroy( begin(), end() );
dealloc_data();
}
iterator begin() { return m_start; }
iterator end() { return m_finish; }
const_iterator begin() const { return m_start; }
const_iterator end() const { return m_finish; }
reverse_iterator rbegin() { return end(); }
reverse_iterator rend() { return begin(); }
const_reverse_iterator rbegin() const { return end(); }
const_reverse_iterator rend() const { return begin(); }
reference front() { return *begin(); }
reference back() { return *(--end()); }
const_reference front() const { return *begin(); }
const_reference back() const { return *(--end()); }
bool empty() const { return ( m_start == m_finish ); }
size_type size() const { return ( m_finish - m_start ); }
size_type space() const { return ( m_storage - m_finish ); }
size_type capacity() const { return ( m_storage - m_start ); }
size_type max_size() const { return size_t_max / sizeof(value_type); }
reference operator[]( size_type index )
{ return m_start[index]; }
const_reference operator[]( size_type index ) const
{ return m_start[index]; }
reference at( size_type index )
{
if( index >= size() )
throw_out_of_range( "vector::at()" );
return m_start[index];
}
const_reference at( size_type index ) const
{
if( index >= size() )
throw_out_of_range( "vector::at()" );
return m_start[index];
}
void push_back( const_reference value )
{
if( m_finish != m_storage )
{
construct( m_finish, value );
++m_finish;
}
else
insert_aux( end(), value );
}
void pop_back()
{
--m_finish;
destroy( m_finish );
}
void clear()
{
destroy( begin(), end() );
dealloc_data();
m_start = NULL_POINTER;
m_finish = NULL_POINTER;
m_storage = NULL_POINTER;
}
void reserve( size_type new_size )
{
if( capacity() < new_size )
{
self temp( begin(), end(), new_size );
swap( temp );
}
}
void swap( self& rhs )
{
if( this != &rhs )
{
data_swap( m_start, rhs.m_start );
data_swap( m_finish, rhs.m_finish );
data_swap( m_storage, rhs.m_storage );
data_swap( m_alloc, rhs.m_alloc );
}
}
void resize( size_type new_size, const_reference value = value_type() )
{
const size_type len = size();
if( new_size < len )
erase( begin() + new_size, end() );
else if( new_size > len )
insert( end(), new_size - len, value );
}
void reverse();
iterator erase( iterator position );
iterator erase( iterator first, iterator last );
void assign( size_type new_size, const_reference value = value_type() );
void assign( int new_size, const_reference value = value_type() )
{ assign( static_cast<size_type>( new_size ), value ); }
void assign( long new_size, const_reference value = value_type() )
{ assign( static_cast<size_type>( new_size ), value ); }
template< typename InputIterator >
void assign( InputIterator first, InputIterator last,
size_type new_size = 0 )
{
typedef typename iterator_traits<InputIterator>::iterator_category cate;
if( first == last )
clear();
else
assign_aux( first, last, new_size, cate() );
}
void insert( iterator position, size_type count, const_reference value )
{
if( count > 1 )
insert_n( position, count, value );
}
void insert( iterator position, int count, const_reference value )
{ insert( position, static_cast<size_type>( count ), value ); }
void insert( iterator position, long count, const_reference value )
{ insert( position, static_cast<size_type>( count ), value ); }
iterator insert( iterator position, const_reference value = value_type() )
{
const size_type n = position - m_start;
if( position == m_finish && m_finish != m_storage )
{
construct( m_finish, value );
++m_finish;
}
else
insert_aux( position, value );
return ( m_start + n );
}
template< typename InputIterator >
void insert( iterator position, InputIterator first, InputIterator last,
size_type extra_size = 0 )
{
if( first != last )
range_insert( position, first, last,
range_length(first, last, extra_size) );
}
protected:
//insert辅助函数
void insert_n( iterator position, size_type extra_size,
const_reference value );
void insert_aux( iterator position, const_reference value );
template< typename InputIterator >
void range_insert( iterator position,
InputIterator first, InputIterator last,
size_type extra_size );
//assign辅助函数
template< typename InputIterator >
void range_assign( InputIterator first, InputIterator last,
size_type new_size );
template< typename InputIterator >
void assign_aux( InputIterator first, InputIterator last,
size_type new_size, input_iterator_tag )
{
range_assign( first, last, new_size );
}
template< typename InputIterator >
void assign_aux( InputIterator first, InputIterator last,
size_type new_size, random_access_iterator_tag )
{
new_size = max( static_cast<size_type>(last - first), new_size );
range_assign( first, last, new_size );
}
//初始化的辅助函数
template< typename InputIterator >
void range_init( InputIterator first, InputIterator last,
size_type size, input_iterator_tag )
{
alloc_data( size );
for( ; first != last; ++first )
push_back( *first );
}
template< typename InputIterator >
void range_init( InputIterator first, InputIterator last,
size_type size, random_access_iterator_tag )
{
size_type n = last - first;
alloc_data( max(n, size) );
try
{
init_copy( first, last, m_start );
}
catch(...)
{
dealloc_data();
throw;
}
m_finish = m_start + n;
}
void fill_init( size_type n, const_reference value )
{
alloc_data( n );
try
{
init_fill_n( m_start, n, value );
}
catch(...)
{
dealloc_data();
throw;
}
m_finish = m_start + n;
}
//负责分配和回收空间的辅助函数
void alloc_data( size_type n )
{
typedef typename type_traits<value_type>::is_POD_type is_pod;
if( n > 0 )
{
m_start = alloc_aux( n, is_pod() );
m_finish = m_start;
m_storage = m_start + n;
}
else
{
m_start = NULL_POINTER;
m_finish = NULL_POINTER;
m_storage = NULL_POINTER;
}
}
void dealloc_data()
{
if( m_start )
m_alloc.deallocate( m_start, capacity() );
}
pointer alloc_aux( size_type& n, true_type );
pointer alloc_aux( size_type& n, false_type )
{
return m_alloc.allocate( n );
}
}; //end class
//-----------------------------------------------------------------------------
template< typename T, typename Allocator >
typename vector<T, Allocator>::pointer
vector<T, Allocator>::alloc_aux( size_type& n, true_type )
{
size_type n_bytes = n * sizeof( value_type );
size_type i_bytes = alignment_bytes;
while( i_bytes < n_bytes )
i_bytes *= 2;
n = i_bytes / sizeof( value_type );
return m_alloc.allocate( n );
}
//-----------------------------------------------------------------------------
template< typename T, typename Allocator >
void vector<T, Allocator>::reverse()
{
if( size() < 2 )
return;
iterator first = begin(), last = end();
--last;
for( ; first < last; ++first,--last )
data_swap( *first, *last );
}
//-----------------------------------------------------------------------------
template< typename T, typename Allocator >
void vector<T, Allocator>::assign( size_type new_size, const T& value )
{
if( capacity() < new_size )
{
self temp( new_size, value );
swap( temp );
}
else
{
const size_type len = size();
if( len > new_size )
{
fill_n( begin(), new_size, value );
destroy( begin() + new_size, end() );
m_finish = m_start + new_size;
}
else if( len < new_size )
{
fill( begin(), end(), value );
init_fill_n( end(), new_size - len, value );
m_finish = m_start + new_size;
}
else
fill( begin(), end(), value );
}
}
//-----------------------------------------------------------------------------
template< typename T, typename Allocator >
template< typename InputIterator >
void vector<T, Allocator>::range_assign( InputIterator first,
InputIterator last,
size_type new_size )
{
if( capacity() < new_size )
{
self temp( first, last, new_size );
swap( temp );
}
else
{
iterator itr = m_start;
for( ; (itr != m_finish) && (first != last); ++itr,++first )
*itr = *first;
if( (itr == m_finish) && (first != last) )
insert( end(), first, last );
else if( itr != m_finish )
erase( itr, end() );
}
}
//-----------------------------------------------------------------------------
template< typename T, typename Allocator >
typename vector<T, Allocator>::iterator
vector<T, Allocator>::erase( iterator position )
{
if( position != m_finish )
{
if( position + 1 != m_finish )
copy( position + 1, m_finish, position );
--m_finish;
destroy( m_finish );
}
return position;
}
//-----------------------------------------------------------------------------
template< typename T, typename Allocator >
typename vector<T, Allocator>::iterator
vector<T, Allocator>::erase( iterator first, iterator last )
{
if( first != last )
{
size_type n = m_finish - last;
iterator itr;
if( n > 0 )
itr = copy_n( last, n, first );
destroy( itr, m_finish );
m_finish -= ( last - first );
}
return first;
}
//-----------------------------------------------------------------------------
template< typename T, typename Allocator >
void vector<T, Allocator>::insert_aux( iterator position, const T& value )
{
if( m_finish != m_storage ) //剩余空间足够
{
construct( m_finish, *( m_finish - 1 ) );
++m_finish;
copy_backward( position, m_finish - 2, m_finish - 1 );
*position = value;
}
else //剩余空间不够
{
size_type old_size = size();
size_type new_capa = max( 2 * old_size, (size_type)1 );
typedef typename type_traits<value_type>::is_POD_type is_pod;
pointer new_start = alloc_aux( new_capa, is_pod() );
pointer new_finish = new_start;
try
{
new_finish = init_copy( begin(), position, new_start );
construct( new_finish, value );
++new_finish;
new_finish = init_copy( position, end(), new_finish );
}
catch(...)
{
destroy( new_start, new_finish );
m_alloc.deallocate( new_start, new_capa );
throw;
}
destroy( begin(), end() );
dealloc_data();
m_start = new_start;
m_finish = new_finish;
m_storage = m_start + new_capa;
}
}
//-----------------------------------------------------------------------------
template< typename T, typename Allocator >
void vector<T, Allocator>::insert_n( iterator position,
size_type extra_size,
const T& value )
{
if( space() >= extra_size ) //剩余空间足够
{
size_type elements_after = m_finish - position;
iterator old_finish = m_finish;
//position后面的已构造空间可以放进所有的新数据
if( elements_after >= extra_size )
{
m_finish = init_copy( m_finish - extra_size, m_finish, m_finish );
copy_backward( position, old_finish - extra_size, old_finish );
fill_n( position, extra_size, value );
}
else //position后面的已构造空间不能放进所有的新数据
{
//先将value添满[m_finish, position + extra_size),
//再将[position, m_finish)复制进[position + extra_size, enough)
m_finish = init_fill_copy( m_finish, position + extra_size, value,
position, m_finish );
fill( position, old_finish, value );
}
}
else //剩余空间不够
{
size_type old_capa = capacity();
size_type new_capa = max( 2 * old_capa, old_capa + extra_size );
typedef typename type_traits<value_type>::is_POD_type is_pod;
pointer new_start = alloc_aux( new_capa, is_pod() );
pointer new_finish = new_start;
try
{
new_finish = init_copy( begin(), position, new_start );
init_fill_n( new_finish, extra_size, value );
new_finish += extra_size;
new_finish = init_copy( position, end(), new_finish );
}
catch(...)
{
destroy( new_start, new_finish );
m_alloc.deallocate( new_start, new_capa );
throw;
}
destroy( begin(), end() );
dealloc_data();
m_start = new_start;
m_finish = new_finish;
m_storage = m_start + new_capa;
}
}
//-----------------------------------------------------------------------------
template< typename T, typename Allocator >
template< typename InputIterator >
void vector<T, Allocator>::range_insert( iterator position,
InputIterator first,
InputIterator last,
size_type extra_size )
{
size_type old_capa = capacity();
size_type new_size = size() + extra_size;
if( old_capa >= new_size ) //剩余空间足够
{
size_type elements_after = m_finish - position;
iterator old_finish = m_finish;
//position后面的已构造空间可以放进所有的新数据
if( elements_after >= extra_size )
{
m_finish = init_copy( m_finish - extra_size, m_finish, m_finish );
copy_backward( position, old_finish - extra_size, old_finish );
copy( first, last, position );
}
else //position后面的已构造空间不能放进所有的新数据
{
InputIterator mid = first;
advance( mid, elements_after );
//将[mid, last)和[position, m_finish)依次复制到[m_finish, enough)
m_finish = init_copy_copy( mid, last, position, m_finish, m_finish );
copy( first, mid, position );
}
}
else //剩余空间不够
{
size_type new_capa = max( 2 * old_capa, new_size );
typedef typename type_traits<value_type>::is_POD_type is_pod;
pointer new_start = alloc_aux( new_capa, is_pod() );
pointer new_finish = new_start;
try
{
new_finish = init_copy( begin(), position, new_start );
new_finish = init_copy( first, last, new_finish );
new_finish = init_copy( position, end(), new_finish );
}
catch(...)
{
destroy( new_start, new_finish );
m_alloc.deallocate( new_start, new_capa );
throw;
}
destroy( begin(), end() );
dealloc_data();
m_start = new_start;
m_finish = new_finish;
m_storage = m_start + new_capa;
}
}
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
template< typename T, typename Allocator >
inline void swap( vector<T, Allocator>& lhs,
vector<T, Allocator>& rhs )
{
lhs.swap( rhs );
}
template< typename T, typename Allocator >
inline bool operator==( const vector<T, Allocator>& lhs,
const vector<T, Allocator>& rhs )
{
return ( lhs.size() == rhs.size()
&& equal( lhs.begin(), lhs.end(), rhs.begin() ) );
}
template< typename T, typename Allocator >
inline bool operator!=( const vector<T, Allocator>& lhs,
const vector<T, Allocator>& rhs )
{
return !( lhs == rhs );
}
template< typename T, typename Allocator >
inline bool operator<( const vector<T, Allocator>& lhs,
const vector<T, Allocator>& rhs )
{
if( lhs.begin() == rhs.begin() || lhs.size() > rhs.size() )
return false;
return lexicographical_compare( lhs.begin(), lhs.end(),
rhs.begin(), rhs.end() );
}
template< typename T, typename Allocator >
inline bool operator>( const vector<T, Allocator>& lhs,
const vector<T, Allocator>& rhs )
{
return ( rhs < lhs );
}
template< typename T, typename Allocator >
inline bool operator<=( const vector<T, Allocator>& lhs,
const vector<T, Allocator>& rhs )
{
return !( rhs < lhs );
}
template< typename T, typename Allocator >
inline bool operator>=( const vector<T, Allocator>& lhs,
const vector<T, Allocator>& rhs )
{
return !( lhs < rhs );
}
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
__MACRO_CPLUSPLUS_YOUNG_LIBRARY_END_NAMESPACE__
#endif
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------