分享
 
 
 

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

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

文件位置:young/y_deque.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_DEQUE_HEADER_FILE__

#define __MACRO_CPLUSPLUS_YOUNG_LIBRARY_DEQUE_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__

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

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

static const def_size_t init_map_size = 32;

inline def_size_t deque_buffer_size( def_size_t buf_size,

def_size_t type_bytes )

{

if( buf_size != 0 )

return buf_size;

else

return ( type_bytes < 512 ? (512 / type_bytes) : 1 );

}

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

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

template< typename T, typename Ref, typename Ptr, typename Alloc, def_size_t >

class deque_iterator;

template< typename T, typename Allocator, def_size_t >

class deque;

template< typename T, typename Ref, typename Ptr, typename Alloc,

def_size_t buf_size >

class deque_iterator

{

public:

typedef random_access_iterator_tag iterator_category;

typedef def_size_t size_type;

typedef def_ptrdiff_t difference_type;

typedef T value_type;

typedef Ref reference;

typedef Ptr pointer;

typedef T** map_pointer;

typedef deque_iterator<T, Ref, Ptr, Alloc, buf_size>

self;

typedef deque_iterator<T, T&, T*, Alloc, buf_size>

iterator;

typedef deque_iterator<T, const T&, const T*, Alloc, buf_size>

const_iterator;

private:

typedef typename primal_type<Ref>::contrary_const_ref Ref_t;

typedef typename primal_type<Ptr>::contrary_const_ptr Ptr_t;

friend class deque<T, Alloc, buf_size>;

friend class deque_iterator<T, Ref_t, Ptr_t, Alloc, buf_size>;

friend iterator const_iter_cast <> ( const const_iterator& );

value_type* current;

value_type* first;

value_type* last;

value_type** node;

void set_node( map_pointer new_node )

{

node = new_node;

first = *node;

last = first + buffer_size();

}

static size_type buffer_size()

{

return deque_buffer_size( buf_size, sizeof(T) );

}

public:

deque_iterator() : current(NULL_POINTER), first(NULL_POINTER),

last(NULL_POINTER), node(NULL_POINTER) {}

deque_iterator( T* curr, map_pointer mp )

: current(curr), first(*mp), last(*mp + buffer_size()), node(mp) {}

deque_iterator( const iterator& x )

: current(x.current), first(x.first), last(x.last), node(x.node) {}

self& operator=( def_nullptr_t n )

{

if( n == NULL_POINTER )

{

current = NULL_POINTER;

first = NULL_POINTER;

last = NULL_POINTER;

node = NULL_POINTER;

}

return *this;

}

pointer operator->() const { return current; }

reference operator*() const { return *current; }

bool operator!() const { return ( !current && !first && !last && !node ); }

bool operator==( const self& rhs ) const { return current == rhs.current; }

bool operator!=( const self& rhs ) const { return current != rhs.current; }

bool operator<( const self& rhs ) const

{

if( node == rhs.node )

return ( current < rhs.current );

else

return ( node < rhs.node );

}

self& operator++()

{

++current;

if( current == last )

{

set_node( node + 1 );

current = first;

}

return *this;

}

self operator++(int)

{

self old = *this;

++( *this );

return old;

}

self& operator--()

{

if( current == first )

{

set_node( node - 1 );

current = last;

}

--current;

return *this;

}

self operator--(int)

{

self old = *this;

--( *this );

return old;

}

self& operator+=( difference_type n );

self& operator-=( difference_type n ) { return ( *this += -n ); }

def_ptrdiff_t operator-( const self& rhs ) const

{

return ( def_ptrdiff_t( buffer_size() ) * ( node - rhs.node - 1 )

+ ( current - first ) + ( rhs.last - rhs.current ) );

}

}; //end iterator

template< typename T, typename Ref, typename Ptr, typename Alloc,

def_size_t buf_size >

deque_iterator<T, Ref, Ptr, Alloc, buf_size>&

deque_iterator<T, Ref, Ptr, Alloc, buf_size>::operator+=( difference_type n )

{

difference_type buf = difference_type( buffer_size() );

difference_type offset = n + ( current - first ); //计算指针的偏移量

if( offset >= 0 && offset < buf )

current += n;

else //指针可能需要后退或者是跳到其他的内存块

{

difference_type node_offset;

if( offset > 0 )

node_offset = offset / buf;

else

node_offset = -( (-offset - 1) / buf ) - 1;

set_node( node + node_offset );

current = first + ( offset - node_offset * buf );

}

return *this;

}

template< typename T, typename Ref, typename Ptr, typename Alloc,

def_size_t buf_size >

inline deque_iterator<T, Ref, Ptr, Alloc, buf_size>

operator-( const deque_iterator<T, Ref, Ptr, Alloc, buf_size>& lhs, def_ptrdiff_t n )

{

deque_iterator<T, Ref, Ptr, Alloc, buf_size> temp( lhs );

return ( temp -= n );

}

template< typename T, typename Ref, typename Ptr, typename Alloc,

def_size_t buf_size >

inline deque_iterator<T, Ref, Ptr, Alloc, buf_size>

operator+( const deque_iterator<T, Ref, Ptr, Alloc, buf_size>& lhs, def_ptrdiff_t n )

{

deque_iterator<T, Ref, Ptr, Alloc, buf_size> temp( lhs );

return ( temp += n );

}

template< typename T, typename Ref, typename Ptr, typename Alloc,

def_size_t buf_size >

inline deque_iterator<T, Ref, Ptr, Alloc, buf_size>

operator+( def_ptrdiff_t n, const deque_iterator<T, Ref, Ptr, Alloc, buf_size>& rhs )

{

deque_iterator<T, Ref, Ptr, Alloc, buf_size> temp( rhs );

return ( temp += n );

}

template< typename T, typename Ref, typename Ptr, typename Alloc,

def_size_t buf_size >

inline bool operator>( const deque_iterator<T, Ref, Ptr, Alloc, buf_size>& lhs,

const deque_iterator<T, Ref, Ptr, Alloc, buf_size>& rhs )

{

return ( rhs < lhs );

}

template< typename T, typename Ref, typename Ptr, typename Alloc,

def_size_t buf_size >

inline bool operator<=( const deque_iterator<T, Ref, Ptr, Alloc, buf_size>& lhs,

const deque_iterator<T, Ref, Ptr, Alloc, buf_size>& rhs )

{

return !( rhs < lhs );

}

template< typename T, typename Ref, typename Ptr, typename Alloc,

def_size_t buf_size >

inline bool operator>=( const deque_iterator<T, Ref, Ptr, Alloc, buf_size>& lhs,

const deque_iterator<T, Ref, Ptr, Alloc, buf_size>& rhs )

{

return !( lhs < rhs );

}

template< typename T, typename Alloc, def_size_t buf_size >

inline deque_iterator<T, T&, T*, Alloc, buf_size>

const_iter_cast( const deque_iterator<T, const T&, const T*,

Alloc, buf_size>& citer )

{

return deque_iterator<T, T&, T*, Alloc,

buf_size>( citer.current, citer.node );

}

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

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

template< typename T, typename Allocator = allocator<T>,

def_size_t buf_size = 0 >

class deque

{

public:

typedef deque<T, Allocator, buf_size> 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 deque_iterator<T, T&, T*, Allocator, buf_size>

iterator;

typedef deque_iterator<T, const T&, const T*, Allocator, buf_size>

const_iterator;

typedef Reverse_Iterator<iterator> reverse_iterator;

typedef Reverse_Iterator<const_iterator> const_reverse_iterator;

protected:

typedef pointer* map_pointer;

typedef allocator_type data_allocator;

typedef typename Allocator::rebind<pointer>::other map_allocator;

// typedef allocator<pointer> map_allocator;

static size_type buffer_size()

{

return deque_buffer_size( buf_size, sizeof(value_type) );

}

map_pointer m_map;

size_type m_map_size;

iterator m_start, m_finish;

map_allocator m_map_alloc;

data_allocator m_data_alloc;

public:

deque() : m_map(NULL_POINTER), m_map_size(0), m_start(), m_finish()

{

alloc_map_and_nodes( 0 );

}

explicit deque( size_type size )

: m_map(NULL_POINTER), m_map_size(0), m_start(), m_finish()

{

fill_init( size, value_type() );

}

deque( size_type size, const_reference value )

: m_map(NULL_POINTER), m_map_size(0), m_start(), m_finish()

{

fill_init( size, value );

}

deque( int size, const_reference value )

: m_map(NULL_POINTER), m_map_size(0), m_start(), m_finish()

{

fill_init( static_cast<size_type>( size ), value );

}

deque( long size, const_reference value )

: m_map(NULL_POINTER), m_map_size(0), m_start(), m_finish()

{

fill_init( static_cast<size_type>( size ), value );

}

template< typename InputIterator >

deque( InputIterator first, InputIterator last, size_type size = 0 )

: m_map(NULL_POINTER), m_map_size(0), m_start(), m_finish()

{

typedef typename iterator_traits<InputIterator>::iterator_category

cate;

range_init( first, last, size, cate() );

}

deque( const self& rhs )

: m_map(NULL_POINTER), m_map_size(0), m_start(), m_finish()

{

alloc_map_and_nodes( rhs.size() );

try

{

init_copy( rhs.begin(), rhs.end(), begin() );

}

catch(...)

{

free_map_and_nodes();

throw;

}

}

deque& operator=( const self& rhs );

~deque();

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 m_finish; }

reverse_iterator rend() { return m_start; }

const_reverse_iterator rbegin() const { return m_finish; }

const_reverse_iterator rend() const { return m_start; }

reference front() { return *m_start; }

reference back() { return *(--end()); }

const_reference front() const { return *m_start; }

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 max_size() const { return size_t_max; }

reference operator[]( size_type index )

{

iterator result = m_start;

return *( result += index );

}

const_reference operator[]( size_type index ) const

{

const_iterator result = m_start;

return *( result += index );

}

reference at( size_type index )

{

if( index >= size() )

throw_out_of_range( "deque::at()" );

iterator result = m_start;

return *( result += index );

}

const_reference at( size_type index ) const

{

if( index >= size() )

throw_out_of_range( "deque::at()" );

const_iterator result = m_start;

return *( result += index );

}

void swap( self& rhs )

{

if( this != &rhs )

{

data_swap( m_map, rhs.m_map );

data_swap( m_map_size, rhs.m_map_size );

data_swap( m_start, rhs.m_start );

data_swap( m_finish, rhs.m_finish );

data_swap( m_map_alloc, rhs.m_map_alloc );

data_swap( m_data_alloc, rhs.m_data_alloc );

}

}

void push_back( const_reference value )

{

if( m_finish.current != (m_finish.last - 1) )

{

construct( m_finish.current, value );

++m_finish.current;

}

else

push_back_aux( value );

}

void push_front( const_reference value )

{

if( m_start.current != m_start.first )

{

construct( m_start.current - 1, value );

--m_start.current;

}

else

push_front_aux( value );

}

void pop_back()

{

if( m_finish.current != m_finish.first )

{

destroy( m_finish.current );

--m_finish.current;

}

else

pop_back_aux();

}

void pop_front()

{

if( m_start.current != (m_start.last - 1) )

{

destroy( m_start.current );

++m_start.current;

}

else

pop_front_aux();

}

void resize( size_type new_size, const_reference value = value_type() )

{

const size_type len = size();

if( new_size > len )

insert( m_finish, new_size - len, value );

else if( new_size < len )

erase( m_start + new_size, m_finish );

}

void clear();

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 )

{

typedef typename iterator_traits<InputIterator>::iterator_category cate;

if( first == last )

clear();

else

assign_aux( first, last, 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() )

{

if( position == m_start )

{

push_front( value );

return m_start;

}

else if( position == m_finish )

{

push_back( value );

return --end();

}

else

return insert_aux( position, value );

}

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:

//进行初始化的辅助函数

void fill_init( size_type n, const value_type& value );

template< typename InputIterator >

void range_init( InputIterator first, InputIterator last,

size_type size, input_iterator_tag )

{

alloc_map_and_nodes( 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_map_and_nodes( max(n, size) );

try

{

init_copy( first, last, m_start );

}

catch(...)

{

free_map_and_nodes();

throw;

}

}

//assign、insert、push、pop的辅助函数

void insert_n( iterator position, size_type extra_size,

const_reference value );

iterator insert_aux( iterator position, const_reference value );

template< typename InputIterator >

void range_insert( iterator position,

InputIterator first, InputIterator last,

size_type extra_size );

template< typename InputIterator >

void assign_aux( InputIterator first, InputIterator last,

input_iterator_tag );

template< typename InputIterator >

void assign_aux( InputIterator first, InputIterator last,

random_access_iterator_tag );

void reserve_map_at_back( size_type add_nodes = 1 )

{

if( add_nodes + 1 > (m_map_size - (m_finish.node - m_map)) )

reallocate_map( add_nodes, false );

}

void reserve_map_at_front( size_type add_nodes = 1 )

{

if( add_nodes > (m_start.node - m_map) )

reallocate_map( add_nodes, true );

}

iterator reserve_elements_at_front( size_type n )

{

size_type space = m_start.current - m_start.first;

if( n > space )

new_elements_at_front( n - space );

return ( m_start - n );

}

iterator reserve_elements_at_back( size_type n )

{

size_type space = ( m_finish.last - m_finish.current ) - 1;

if( n > space )

new_elements_at_back( n - space );

return ( m_finish + n );

}

void new_elements_at_front( size_type n );

void new_elements_at_back( size_type n );

void destroy_nodes_at_front( iterator before_start );

void destroy_nodes_at_back( iterator after_finish );

void push_back_aux( const_reference value );

void push_front_aux( const_reference value );

void pop_back_aux();

void pop_front_aux();

//负责分配和回收map与缓冲区空间的函数

void alloc_map_and_nodes( size_type n );

void free_map_and_nodes();

void reallocate_map( size_type add_nodes, bool add_at_front );

map_pointer alloc_map( size_type n )

{

return m_map_alloc.allocate(n);

}

void dealloc_map( map_pointer map_ptr, size_type n )

{

if( map_ptr )

m_map_alloc.deallocate( map_ptr, n );

}

pointer alloc_node()

{

return m_data_alloc.allocate( buffer_size() );

}

void dealloc_node( pointer ptr )

{

if( ptr )

m_data_alloc.deallocate( ptr, buffer_size() );

}

}; //end class

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

template< typename T, typename Allocator, def_size_t buf_size >

deque<T, Allocator, buf_size>::~deque()

{

size_type buf = buffer_size();

for( map_pointer curr = m_start.node + 1; curr < m_finish.node; ++curr )

{

destroy( *curr, *curr + buf );

dealloc_node( *curr );

}

if( m_start.node != m_finish.node ) //首尾不在同一个map内

{

destroy( m_start.current, m_start.last );

destroy( m_finish.first, m_finish.current );

dealloc_node( *(m_start.node) );

dealloc_node( *(m_finish.node) );

}

else //首尾在同一个map内

{

destroy( m_start.current, m_finish.current );

dealloc_node( *(m_start.node) );

}

dealloc_map( m_map, m_map_size );

}

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

template< typename T, typename Allocator, def_size_t buf_size >

deque<T, Allocator, buf_size>&

deque<T, Allocator, buf_size>::operator=( const self& rhs )

{

if( this != &rhs )

{

const size_type len = size();

if( len >= rhs.size() )

erase( copy(rhs.begin(), rhs.end(), m_start), m_finish );

else

{

const_iterator itr = rhs.begin() + len;

copy( rhs.begin(), itr, m_start );

insert( m_finish, itr, rhs.end() );

}

}

return *this;

}

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

template< typename T, typename Allocator, def_size_t buf_size >

void deque<T, Allocator, buf_size>::alloc_map_and_nodes( size_type data_size )

{

size_type nodes_count = data_size / buffer_size() + 1; //可能还有余数,所以要加1

m_map_size = max( init_map_size, nodes_count + 2 );

m_map = alloc_map( m_map_size );

map_pointer node_start = m_map + ( m_map_size - nodes_count ) / 2;

map_pointer node_finish = node_start + nodes_count - 1;

map_pointer curr;

try

{

for( curr = node_start; curr <= node_finish; ++curr )

*curr = alloc_node();

}

catch(...)

{

for( ; node_start < curr; ++node_start )

dealloc_node( *node_start );

dealloc_map( m_map, m_map_size );

throw;

}

m_start.set_node( node_start );

m_finish.set_node( node_finish );

m_start.current = m_start.first;

m_finish.current = m_finish.first + data_size % buffer_size();

}

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

template< typename T, typename Allocator, def_size_t buf_size >

void deque<T, Allocator, buf_size>::reallocate_map( size_type add_nodes,

bool add_at_front )

{

size_type old_nodes = m_finish.node - m_start.node + 1;

size_type new_nodes = old_nodes + add_nodes;

map_pointer new_start;

if( m_map_size > 2 * new_nodes ) //map空间足够大

{

//在起始位置增加节点则需将当前节点数据后移

new_start = m_map + (m_map_size - new_nodes) / 2

+ ( add_at_front ? add_nodes : 0 );

if( new_start < m_start.node ) //在起始处增加节点的操作

copy( m_start.node, m_finish.node + 1, new_start );

else //在末尾增加节点的操作

copy_backward( m_start.node, m_finish.node + 1, new_start + old_nodes );

}

else

{

size_type new_map_size = m_map_size + max( m_map_size, add_nodes ) + 2;

map_pointer new_map = alloc_map( new_map_size );

new_start = new_map + (new_map_size - new_nodes) / 2

+ ( add_at_front ? add_nodes : 0 );

copy( m_start.node, m_finish.node + 1, new_start );

dealloc_map( m_map, m_map_size );

m_map = new_map;

m_map_size = new_map_size;

}

m_start.set_node( new_start );

m_finish.set_node( new_start + old_nodes - 1 );

}

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

template< typename T, typename Allocator, def_size_t buf_size >

void deque<T, Allocator, buf_size>::free_map_and_nodes()

{

for( map_pointer curr = m_start.node; curr <= m_finish.node; ++curr )

dealloc_node( *curr );

dealloc_map( m_map, m_map_size );

}

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

template< typename T, typename Allocator, def_size_t buf_size >

void deque<T, Allocator, buf_size>::fill_init( size_type n, const T& value )

{

alloc_map_and_nodes( n );

map_pointer curr;

size_type buf = buffer_size();

try

{

for( curr = m_start.node; curr < m_finish.node; ++curr )

init_fill_n( *curr, buf, value );

init_fill_n( m_finish.first, m_finish.current - m_finish.first, value );

}

catch(...)

{

for( map_pointer i = m_start.node; i < curr; ++i )

destroy( *i, *i + buf );

free_map_and_nodes();

throw;

}

}

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

template< typename T, typename Allocator, def_size_t buf_size >

void deque<T, Allocator, buf_size>::clear()

{

size_type buf = buffer_size();

//释放start与finish之间的节点所分配的空间

for( map_pointer curr = m_start.node + 1; curr < m_finish.node; ++curr )

{

destroy( *curr, *curr + buf );

dealloc_node( *curr );

}

if( m_start.node != m_finish.node )

{

destroy( m_start.current, m_start.last );

destroy( m_finish.first, m_finish.current );

dealloc_node( *(m_finish.node) );

}

else

destroy( m_start.current, m_finish.current );

m_finish = m_start;

}

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

template< typename T, typename Allocator, def_size_t buf_size >

void deque<T, Allocator, buf_size>::push_back_aux( const_reference value )

{

reserve_map_at_back(); //检查是否需要为map重新分配内存空间

*( m_finish.node + 1 ) = alloc_node();

try

{

construct( m_finish.current, value );

}

catch(...)

{

dealloc_node( *(m_finish.node + 1) );

throw;

}

m_finish.set_node( m_finish.node + 1 );

m_finish.current = m_finish.first;

}

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

template< typename T, typename Allocator, def_size_t buf_size >

void deque<T, Allocator, buf_size>::push_front_aux( const_reference value )

{

reserve_map_at_front();

*( m_start.node - 1 ) = alloc_node();

try

{

m_start.set_node( m_start.node - 1 );

m_start.current = m_start.last - 1;

construct( m_start.current, value );

}

catch(...)

{

m_start.set_node( m_start.node - 1 );

m_start.current = m_start.first;

dealloc_node( *(m_start.node - 1) );

throw;

}

}

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

template< typename T, typename Allocator, def_size_t buf_size >

void deque<T, Allocator, buf_size>::pop_back_aux()

{

dealloc_node( m_finish.first );

m_finish.set_node( m_finish.node - 1 );

m_finish.current = m_finish.last - 1;

destroy( m_finish.current );

}

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

template< typename T, typename Allocator, def_size_t buf_size >

void deque<T, Allocator, buf_size>::pop_front_aux()

{

destroy( m_start.current );

dealloc_node( m_start.first );

m_start.set_node( m_start.node + 1 );

m_start.current = m_start.first;

}

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

template< typename T, typename Allocator, def_size_t buf_size >

void deque<T, Allocator, buf_size>::assign( size_type new_size,

const_reference value )

{

if( new_size < 1 )

{

clear();

return;

}

const size_type len = size();

if( len < new_size )

{

fill_n( m_start, len, value );

insert( m_finish, new_size - len, value );

}

else if( len > new_size )

{

fill_n( m_start, new_size, value );

erase( m_start + new_size, m_finish );

}

else

fill_n( m_start, len, value );

}

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

template< typename T, typename Allocator, def_size_t buf_size >

template< typename InputIterator >

void deque<T, Allocator, buf_size>::assign_aux( InputIterator first,

InputIterator last,

input_iterator_tag )

{

const size_type len = size();

iterator itr = m_start;

for( ; (len > 0) && (first != last); --len,++first,++itr )

*itr = *first;

if( len > 0 ) //原数据较长

erase( itr, m_finish );

else if( (len == 0) && (first != last) )

insert( m_finish, first, last );

}

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

template< typename T, typename Allocator, def_size_t buf_size >

template< typename InputIterator >

void deque<T, Allocator, buf_size>::assign_aux( InputIterator first,

InputIterator last,

random_access_iterator_tag )

{

const size_type len = size();

const size_type itr_len = last - first;

if( itr_len > len )

{

copy_n( first, len, m_start );

insert( m_finish, first + len, last );

}

else if( itr_len < len )

{

copy_n( first, itr_len, m_start );

erase( m_start + itr_len, m_finish );

}

else

copy( first, last, m_start );

}

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

template< typename T, typename Allocator, def_size_t buf_size >

typename deque<T, Allocator, buf_size>::iterator

deque<T, Allocator, buf_size>::insert_aux( iterator position,

const_reference value )

{

difference_type elements_before = position - m_start;

if( elements_before < (size() / 2) ) //向前面移动

{

push_front( front() );

position = m_start + elements_before; //迭代器有可能失效,需重新定位

iterator front1 = m_start; ++front1; //指向原来的第一个数据的位置

iterator front2 = front1; ++front2; //指向原来的第二个数据的位置

copy( front2, posiion, front1 ); //从第二个数据开始全部前移一位

--position;

}

else

{

push_back( back() );

position = m_start + elements_before;

iterator back1 = m_finish; --back1; //原来的end()

iterator back2 = back1; --back2; //原来的back()

copy_backward( position, back2, back1 );

}

*position = value;

return position;

}

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

template< typename T, typename Allocator, def_size_t buf_size >

void deque<T, Allocator, buf_size>::insert_n( iterator position,

size_type extra_size,

const_reference value )

{

if( position == m_start )

{

iterator new_start = reserve_elements_at_front( extra_size );

try

{

init_fill_n( new_start, extra_size, value );

}

catch(...)

{

destroy_nodes_at_front( new_start );

throw;

}

m_start = new_start;

}

else if( position == m_finish )

{

iterator new_finish = reserve_elements_at_back( extra_size );

try

{

init_fill_n( m_finish, extra_size, value );

}

catch(...)

{

destroy_nodes_at_back( new_finish );

throw;

}

m_finish = new_finish;

}

else //处理一般情况下的插入操作

{

difference_type elements_before = position - m_start;

size_type len = size();

if( elements_before < (len / 2) ) //向前面移动

{

iterator new_start = reserve_elements_at_front( extra_size );

iterator old_start = m_start;

position = m_start + elements_before;

try

{

//position前面的已构造空间可以放进所有的新数据

if( elements_before >= difference_type(extra_size) )

{

iterator start_count = m_start + difference_type(extra_size);

init_copy( m_start, start_count, new_start );

m_start = new_start;

copy_n( start_count, position - start_count, old_start );

fill( position - extra_size, position, value );

}

else //position前面的已构造空间不能放进所有的新数据

{

//先将[m_start, position)复制进[new_start, m_start),

//然后再将value填满[new_start, m_start)的剩余空间

init_copy_fill( m_start, position, new_start, m_start, value );

m_start = new_start;

fill( old_start, position, value );

}

}

catch(...)

{

destroy_nodes_at_front( new_start );

throw;

}

}

else //向后面移动

{

iterator new_finish = reserve_elements_at_back( extra_size );

iterator old_finish = m_finish;

difference_type elements_after = difference_type(len) - elements_before;

position = m_finish - elements_after;

try

{

//position后面的已构造空间可以放进所有的新数据

if( elements_after >= difference_type(extra_size) )

{

iterator finish_count = m_finish - difference_type(extra_size);

init_copy( finish_count, m_finish, m_finish );

m_finish = new_finish;

copy_backward( position, finish_count, old_finish );

fill_n( position, extra_size, value );

}

else //position后面的已构造空间不能放进所有的新数据

{

//先将value添满[m_finish, position + extra_size),

//再将[position, m_finish)复制进[position + extra_size, enough)

init_fill_copy( m_finish, position + difference_type(extra_size),

value, position, m_finish );

m_finish = new_finish;

fill( position, old_finish, value );

}

}

catch(...)

{

destroy_nodes_at_back( new_finish );

throw;

}

} //end else ( elements_before >= (len / 2) )

} //end else

}

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

template< typename T, typename Allocator, def_size_t buf_size >

template< typename InputIterator >

void deque<T, Allocator, buf_size>::range_insert( iterator position,

InputIterator first,

InputIterator last,

size_type extra_size )

{

difference_type elements_before = position - m_start;

size_type len = size();

if( elements_before < (len / 2) ) //向前面移动

{

iterator new_start = reserve_elements_at_front( extra_size );

iterator old_start = m_start;

position = m_start + elements_before;

try

{

//position前面的已构造空间可以放进所有的新数据

if( elements_before >= difference_type(extra_size) )

{

iterator start_count = m_start + difference_type(extra_size);

init_copy( m_start, start_count, new_start );

m_start = new_start;

copy_n( start_count, position - start_count, old_start );

copy_n( first, extra_size, position - difference_type(extra_size) );

}

else //position前面的已构造空间不能放进所有的新数据

{

InputIterator mid = first;

difference_type i = difference_type(extra_size) - elements_before;

advance( mid, i );

//将[m_start, position)和[first, mid)依次复制到[new_start, enough)

init_copy_copy( m_start, position, first, mid, new_start );

m_start = new_start;

copy_n( mid, extra_size - i, old_start );

}

}

catch(...)

{

destroy_nodes_at_front( new_start );

throw;

}

}

else //向后面移动

{

iterator new_finish = reserve_elements_at_back( extra_size );

iterator old_finish = m_finish;

difference_type elements_after = difference_type(len) - elements_before;

position = m_finish - elements_after;

try

{

//position后面的已构造空间可以放进所有的新数据

if( elements_after >= difference_type(extra_size) )

{

iterator finish_count = m_finish - difference_type(extra_size);

init_copy( finish_count, m_finish, m_finish );

m_finish = new_finish;

copy_backward( position, finish_count, old_finish );

copy_n( first, extra_size, position );

}

else //position后面的已构造空间不能放进所有的新数据

{

InputIterator mid = first;

advance( mid, elements_after );

//将[mid, last)和[position, m_finish)依次复制进[m_finish, enough)

init_copy_copy( mid, last, position, m_finish, m_finish );

m_finish = new_finish;

copy_n( first, extra_size - elements_after, position );

}

}

catch(...)

{

destroy_nodes_at_back( new_finish );

throw;

}

} //end else

}

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

template< typename T, typename Allocator, def_size_t buf_size >

typename deque<T, Allocator, buf_size>::iterator

deque<T, Allocator, buf_size>::erase( iterator position )

{

if( position == m_finish )

return position;

iterator next = position;

++next;

difference_type i = position - m_start;

if( i < (size() / 2) ) //移动前面的数据

{

copy_backward( m_start, position, next );

pop_front();

}

else //移动后面的数据

{

copy( next, m_finish, position );

pop_back();

}

return ( m_start + i );

}

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

template< typename T, typename Allocator, def_size_t buf_size >

typename deque<T, Allocator, buf_size>::iterator

deque<T, Allocator, buf_size>::erase( iterator first, iterator last )

{

if( first == m_start && last == m_finish )

{

clear();

return m_finish;

}

difference_type n = last - first;

difference_type elements_before = first - m_start;

if( elements_before < ((size() - n) / 2) ) //移动前面的数据

{

copy_backward( m_start, first, last );

iterator new_start = m_start + n;

destroy( m_start, new_start );

for( map_pointer curr = m_start.node; curr < new_start.node; ++curr )

dealloc_node( *curr );

m_start = new_start;

}

else //移动后面的数据

{

copy( last, m_finish, first );

iterator new_finish = m_finish - n;

destroy( new_finish, m_finish );

for( map_pointer curr = m_finish.node; curr > new_finish.node; --curr )

dealloc_node( *curr );

m_finish = new_finish;

}

return ( m_start + elements_before );

}

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

template< typename T, typename Allocator, def_size_t buf_size >

void deque<T, Allocator, buf_size>::new_elements_at_front( size_type n )

{

size_type buf = buffer_size();

size_type add_nodes = ( n + buf - 1 ) / buf;

reserve_map_at_front( add_nodes );

size_type i;

try

{

for( i = 1; i <= add_nodes; ++i )

*( m_start.node - i ) = alloc_node();

}

catch(...)

{

for( size_type j = 1; j < i; ++j )

dealloc_node( *(m_start.node - j) );

throw;

}

}

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

template< typename T, typename Allocator, def_size_t buf_size >

void deque<T, Allocator, buf_size>::new_elements_at_back( size_type n )

{

size_type buf = buffer_size();

size_type add_nodes = ( n + buf - 1 ) / buf;

reserve_map_at_back( add_nodes );

size_type i;

try

{

for( i = 1; i <= add_nodes; ++i )

*( m_finish.node + i ) = alloc_node();

}

catch(...)

{

for( size_type j = 1; j < i; ++j )

dealloc_node( *(m_finish.node + j) );

throw;

}

}

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

template< typename T, typename Allocator, def_size_t buf_size >

void deque<T, Allocator, buf_size>::

destroy_nodes_at_front( iterator before_start )

{

for( map_pointer curr = before_start.node; curr < m_start.node; ++curr )

dealloc_node( *curr );

}

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

template< typename T, typename Allocator, def_size_t buf_size >

void deque<T, Allocator, buf_size>::

destroy_nodes_at_back( iterator after_finish )

{

for( map_pointer curr = after_finish.node; curr > m_finish.node; --curr )

dealloc_node( *curr );

}

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

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

template< typename T, typename Allocator, def_size_t buf_size >

inline void swap( deque<T, Allocator, buf_size>& lhs,

deque<T, Allocator, buf_size>& rhs )

{

lhs.swap( rhs );

}

template< typename T, typename Allocator, def_size_t buf_size >

inline bool operator==( const deque<T, Allocator, buf_size>& lhs,

const deque<T, Allocator, buf_size>& rhs )

{

return ( lhs.size() == rhs.size()

&& equal( lhs.begin(), lhs.end(), rhs.begin() ) );

}

template< typename T, typename Allocator, def_size_t buf_size >

inline bool operator!=( const deque<T, Allocator, buf_size>& lhs,

const deque<T, Allocator, buf_size>& rhs )

{

return !( lhs == rhs );

}

template< typename T, typename Allocator, def_size_t buf_size >

inline bool operator<( const deque<T, Allocator, buf_size>& lhs,

const deque<T, Allocator, buf_size>& 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, def_size_t buf_size >

inline bool operator>( const deque<T, Allocator, buf_size>& lhs,

const deque<T, Allocator, buf_size>& rhs )

{

return ( rhs < lhs );

}

template< typename T, typename Allocator, def_size_t buf_size >

inline bool operator<=( const deque<T, Allocator, buf_size>& lhs,

const deque<T, Allocator, buf_size>& rhs )

{

return !( rhs < lhs );

}

template< typename T, typename Allocator, def_size_t buf_size >

inline bool operator>=( const deque<T, Allocator, buf_size>& lhs,

const deque<T, Allocator, buf_size>& 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- 王朝網路 版權所有