📄 ycpp_vector.hpp
字号:
reverse_iterator rbegin()
{ return reverse_iterator( end() ); }
reverse_iterator rend()
{ return reverse_iterator( begin() ); }
const_reverse_iterator rbegin() const
{ return const_reverse_iterator( end() ); }
const_reverse_iterator rend() const
{ return const_reverse_iterator( begin() ); }
reference front()
{ return *((pointer)dyrscarr_front(&m_arr)); }
reference back()
{ return *((pointer)dyrscarr_back(&m_arr)); }
const_reference front() const
{ return *((const_pointer)dyrscarr_front(&m_arr)); }
const_reference back() const
{ return *((const_pointer)dyrscarr_back(&m_arr)); }
void clear() { dyrscarr_erase_range( &m_arr, 0, size() ); }
bool empty() const { return dyrscarr_empty(&m_arr); }
size_type max_size() const { return dyrscarr_max_size(&m_arr); }
#ifdef __GNUC__
size_type size() const { return dyrscarr_size(&m_arr); }
size_type space() const { return dyrscarr_space(&m_arr); }
size_type capacity() const { return dyrscarr_capacity(&m_arr); }
#else
size_type size() const { return DYRSCARR_SIZE(m_arr, T); }
size_type space() const { return DYRSCARR_SPACE(m_arr, T); }
size_type capacity() const { return DYRSCARR_CAPACITY(m_arr, T); }
#endif
reference operator[]( size_type index )
{ return DYRSCARR_INDEX(m_arr, index, T); }
const_reference operator[]( size_type index ) const
{ return DYRSCARR_INDEX(m_arr, index, T); }
reference at( size_type index )
{
if( index >= size() )
throw std::out_of_range( "young::rscvector::at out of range!" );
return DYRSCARR_INDEX(m_arr, index, T);
}
const_reference at( size_type index ) const
{
if( index >= size() )
throw std::out_of_range( "young::rscvector::at out of range!" );
return DYRSCARR_INDEX(m_arr, index, T);
}
void swap( self& rhs )
{
if( &rhs != this )
std::swap( m_arr, rhs.m_arr );
}
void pop_back()
{
if( m_arr.m_finish > m_arr.m_start )
{
m_arr.m_finish = static_cast<T*>(m_arr.m_finish) - 1;
ylib_destroy( (T*)(m_arr.m_finish) );
}
}
void push_back( const T& value )
{
if( space() > 0 )
{
ylib_construct( end(), value );
m_arr.m_finish = static_cast<T*>(m_arr.m_finish) + 1;
}
else if( !dyrscarr_insert_value(&m_arr, size(), &value, 1) )
throw std::bad_alloc();
}
void reserve( size_type new_capa )
{
if( !dyrscarr_reserve(&m_arr, new_capa) )
throw std::bad_alloc();
}
void resize( size_type new_size, const T& value = T() )
{
if( !dyrscarr_resize(&m_arr, new_size, &value) )
throw std::bad_alloc();
}
iterator erase( iterator pos )
{
size_t i = pos - begin();
dyrscarr_erase_pos( &m_arr, i );
return begin() + i;
}
iterator erase( iterator first, iterator last )
{
size_t f = first - begin();
dyrscarr_erase_range( &m_arr, f, last - begin() );
return begin() + f;
}
void insert( iterator pos, int count, const T& value )
{ insert( pos, static_cast<size_type>( count ), value ); }
void insert( iterator pos, long count, const T& value )
{ insert( pos, static_cast<size_type>( count ), value ); }
void insert( iterator pos, size_type count, const T& value )
{
if( !dyrscarr_insert_value(&m_arr, pos - begin(), &value, count) )
throw std::bad_alloc();
}
iterator insert( iterator pos, const T& value = T() )
{
size_t i = pos - begin();
if( !dyrscarr_insert_value(&m_arr, i, &value, 1) )
throw std::bad_alloc();
return begin() + i;
}
void insert( iterator pos, T* first, T* last )
{ insert( pos, (const T*)first, (const T*)last ); }
void insert( iterator pos, const T* first, const T* last )
{
size_t i = pos - begin();
if( !dyrscarr_insert_array(&m_arr, i, first, last - first) )
throw std::bad_alloc();
}
template< typename InputIterator >
void insert( iterator pos, InputIterator first, InputIterator last )
{
size_t i = pos - begin();
if( !dyrscarr_insert_array(&m_arr, i, NULL, std::distance(first, last)) )
throw std::bad_alloc();
std::copy( first, last, begin() + i );
}
void assign( int new_size, const T& value = T() )
{ assign( static_cast<size_type>(new_size), value ); }
void assign( long new_size, const T& value = T() )
{ assign( static_cast<size_type>(new_size), value ); }
void assign( size_type new_size, const T& value = T() )
{
if( !dyrscarr_replace_fill(&m_arr, 0, SIZE_MAX, &value, new_size) )
throw std::bad_alloc();
}
void assign( T* first, T* last )
{ assign( (const T*)first, (const T*)last ); }
void assign( const T* first, const T* last )
{
if( !dyrscarr_replace_array(&m_arr, 0, SIZE_MAX, first, last - first) )
throw std::bad_alloc();
}
template< typename InputIterator >
void assign( InputIterator first, InputIterator last )
{
if( !dyrscarr_replace_array( &m_arr, 0, SIZE_MAX, NULL,
std::distance(first, last) ) )
throw std::bad_alloc();
std::copy( first, last, begin() );
}
private:
mutable youngc::dyrscarray m_arr;
void init()
{
typedef typename move_traits<T>::move_ability mv;
init_obj( mv() );
}
void init_obj( true_type )
{
youngc::ylib_fp_oper_t init = init_adapter<T>;
youngc::ylib_fp_copy_t cp = init_copy_adapter<T>;
youngc::ylib_fp_copy_t ass = assign_copy_adapter<T>;
youngc::ylib_fp_oper_t dsty = destroy_adapter<T>;
dyrscarr_init( &m_arr, sizeof(T),
property_traits<T>::trivial_constructor ? NULL : init,
property_traits<T>::trivial_copy ? NULL : cp,
property_traits<T>::trivial_assign ? NULL : ass,
T::init_move, T::assign_move,
property_traits<T>::trivial_destructor ? NULL : dsty,
alloc_adapter<Allocator>, dealloc_adapter<Allocator> );
}
void init_obj( false_type )
{
youngc::ylib_fp_oper_t init = init_adapter<T>;
youngc::ylib_fp_copy_t cp = init_copy_adapter<T>;
youngc::ylib_fp_copy_t ass = assign_copy_adapter<T>;
youngc::ylib_fp_oper_t dsty = destroy_adapter<T>;
dyrscarr_init( &m_arr, sizeof(T),
property_traits<T>::trivial_constructor ? NULL : init,
property_traits<T>::trivial_copy ? NULL : cp,
property_traits<T>::trivial_assign ? NULL : ass,
NULL, NULL,
property_traits<T>::trivial_destructor ? NULL : dsty,
alloc_adapter<Allocator>, dealloc_adapter<Allocator> );
}
void init_fill( size_type n, const T& value )
{
init();
if( !dyrscarr_resize(&m_arr, static_cast<size_t>(n), &value) )
throw std::bad_alloc();
}
}; //end class rscvector
template< typename T, typename Allocator, typename U >
struct vector_traits
{
typedef rscvector<T, Allocator> vector_base;
};
template< typename T, typename Allocator >
struct vector_traits<T, Allocator, false_type>
{
typedef rscvector<T, Allocator> vector_base;
};
template< typename T, typename Allocator >
struct vector_traits<T, Allocator, true_type>
{
typedef memvector<T, Allocator> vector_base;
};
template< typename T, typename Allocator = std::allocator<T>,
typename Base = typename vector_traits<T, Allocator,
typename property_traits<T>::is_POD>::vector_base >
class vector : public Base
{
public:
typedef typename Base::allocator_type allocator_type;
typedef typename Base::size_type size_type;
typedef typename Base::difference_type difference_type;
typedef typename Base::value_type value_type;
typedef typename Base::reference reference;
typedef typename Base::const_reference const_reference;
typedef typename Base::pointer pointer;
typedef typename Base::const_pointer const_pointer;
typedef typename Base::iterator iterator;
typedef typename Base::const_iterator const_iterator;
typedef typename Base::reverse_iterator reverse_iterator;
typedef typename Base::const_reverse_iterator const_reverse_iterator;
};
template< typename T, typename A >
struct move_traits< vector<T, A> >
{
typedef true_type move_ability;
};
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
template< typename T, typename Allocator, typename Base >
inline void swap( vector<T, Allocator, Base>& lhs,
vector<T, Allocator, Base>& rhs )
{
lhs.swap( rhs );
}
template< typename T, typename Allocator, typename Base >
inline bool operator==( const vector<T, Allocator, Base>& lhs,
const vector<T, Allocator, Base>& rhs )
{
return lhs.size() == rhs.size()
&& std::equal( lhs.begin(), lhs.end(), rhs.begin() );
}
template< typename T, typename Allocator, typename Base >
inline bool operator!=( const vector<T, Allocator, Base>& lhs,
const vector<T, Allocator, Base>& rhs )
{
return !( lhs == rhs );
}
template< typename T, typename Allocator, typename Base >
inline bool operator<( const vector<T, Allocator, Base>& lhs,
const vector<T, Allocator, Base>& rhs )
{
return std::lexicographical_compare( lhs.begin(), lhs.end(),
rhs.begin(), rhs.end() );
}
template< typename T, typename Allocator, typename Base >
inline bool operator>( const vector<T, Allocator, Base>& lhs,
const vector<T, Allocator, Base>& rhs )
{
return rhs < lhs;
}
template< typename T, typename Allocator, typename Base >
inline bool operator<=( const vector<T, Allocator, Base>& lhs,
const vector<T, Allocator, Base>& rhs )
{
return !( rhs < lhs );
}
template< typename T, typename Allocator, typename Base >
inline bool operator>=( const vector<T, Allocator, Base>& lhs,
const vector<T, Allocator, Base>& rhs )
{
return !( lhs < rhs );
}
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
} //end namespace
#endif
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -