分享
 
 
 

C++ young 程序库——y_vector.hpp

王朝c/c++·作者佚名  2006-01-10
窄屏简体版  字體: |||超大  

文件位置: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

//-----------------------------------------------------------------------------

//-----------------------------------------------------------------------------

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有