版本比较
C++杂交容器 vector+deque(veque)
正文
增加 36 行删除 36 行
59 行未改动6061626364646566676819 行未改动88899091929293949595969798997 行未改动10710810911011111111211311411411511611711711811912012112212212312412512622 行未改动14915015115215315315415515615721 行未改动1791801811821831831841851861871881891901901911921931948 行未改动2032042052062072072082092102102112122132142152162172172182192202202212222232242252262272272282292302312 行未改动2342352362372382382392402412423 行未改动24624724824925025025125225225325425525625725725825925926026126226326426526526626726826916 行未改动2862872882892902902912922932943 行未改动29829930030130230230330430530630730830931031031131231331414 行未改动32933033133233333333433533633733833934034134234234334434534634734834834935035135235335435435535635735735835936036111 行未改动37337437537637737737837938038144 行未改动42642742842943043043143243343443543543643743843925 行未改动46546646746846946947047147247326 行未改动50050150250350450450550650750826 行未改动53553653753853953954054154254326 行未改动57057157257357457457557657757848 行未改动59 行未改动 using allocation_before_front=std::ratio<0>; using allocation_after_back=std::ratio<0>; static constexpr auto resize_from_closest_side=false; };
template<typename T,typename RT=fast_resize_traits,typename Alc=std::allocator<T>> class veque{ public: using allocator_type=Alc;19 行未改动 veque(std::initializer_list<T>lst,const Alc&a=Alc()):veque(_aut{},lst.size(),a){_ccr(lst.begin(),lst.end(),begin());} veque(const veque&o):veque(_aut{},o.size(),alloc_traits::select_on_container_copy_construction(o._al())){_ccr(o.begin(),o.end(),begin());} template<typename ORT>veque(const veque<T,ORT,Alc>&o):veque(_aut{},o.size(),alloc_traits::select_on_container_copy_construction(o._al())){_ccr(o.begin(),o.end(),begin());} template<typename ORT>veque(const veque<T,ORT,Alc>&o,const Alc&a):veque(_aut{},o.size(),a){_ccr(o.begin(),o.end(),begin());}
veque(veque&&o)noexcept{_swa(std::move(o));} template<typename ORT>veque(veque<T,ORT,Alc>&&o)noexcept{_swa(std::move(o));}
template<typename ORT>veque(veque<T,ORT,Alc>&&o,const Alc&a)noexcept:veque(a){ if constexpr(!alloc_traits::is_always_equal::value)if(a!=o._al()){ auto _=veque(_aut{},o.size(),a); _nmcr(o.begin(),o.end(),_.begin());7 行未改动 template<typename ORT>veque&operator=(veque<T,ORT,Alc>&&o)noexcept(noexcept(alloc_traits::propagate_on_container_move_assignment::value||alloc_traits::is_always_equal::value)){return _ma(std::move(o));} veque&operator=(std::initializer_list<T>lst){_a(lst.begin(),lst.end());return*this;} void assign(size_type cnt,const T&value){if(cnt>capacity_full())_swoa(veque(cnt,value,_al()));else _res(cnt,value);} template<typename It,typename ItCat=typename std::iterator_traits<It>::iterator_category>void assign(It b,It e){_a(b,e,ItCat{});}
void assign(std::initializer_list<T>lst){_a(lst.begin(),lst.end());} allocator_type get_allocator()const{return _al();}
reference at(size_type idx){if(idx>=size()){throw std::out_of_range("veque<T,ResizeTraits,Alloc>::at("+std::to_string(idx)+") out of range");}return(*this)[idx];} const_reference at(size_type idx)const{if(idx>=size()){throw std::out_of_range("veque<T,ResizeTraits,Alloc>::at("+std::to_string(idx)+") out of range");}return(*this)[idx];}
reference operator[](size_type idx){return*(begin()+idx);} const_reference operator[](size_type idx)const{return*(begin()+idx);} reference front(){return(*this)[0];} const_reference front()const{return(*this)[0];}
reference back(){return(*this)[size()-1];} const_reference back()const{return(*this)[size()-1];} T*data()noexcept{return begin();} const T*data()const noexcept{return begin();}22 行未改动 if(front>capacity_front() || back>capacity_back()){ auto allocated_before_begin=std::max(capacity_front(),front)-size(); auto allocated_after_begin=std::max(capacity_back(),back); auto new_full_capacity=allocated_before_begin+allocated_after_begin;
if(new_full_capacity>max_size()) throw std::length_error("veque<T,ResizeTraits,Alloc>::reserve("+std::to_string(front)+","+std::to_string(back)+") exceeds max_size()"); _re(new_full_capacity,allocated_before_begin); }21 行未改动 iterator insert(const_iterator it,T&&value){return emplace(it,std::move(value));} iterator insert(const_iterator it,size_type cnt,const T&value){auto res=_is(it,cnt);_vcr(res,res+cnt,value);return res;} template<typename It,typename ItCat=typename std::iterator_traits<It>::iterator_category>iterator insert(const_iterator it,It b,It e){return _i(it,b,e,ItCat{});} iterator insert(const_iterator it,std::initializer_list<T>lst){return insert(it,lst.begin(),lst.end());}
template<typename...Args> iterator emplace(const_iterator it,Args&&...args){ auto res=_is(it,1); alloc_traits::construct(_al(),res,std::forward<Args>(args)...); return res; }
iterator erase(const_iterator it){return erase(it,std::next(it));} iterator erase(const_iterator b,const_iterator e){ auto cnt=std::distance(b,e); if constexpr(_rfcs){8 行未改动 _shf(e,end(),cnt); _me(-cnt); return _mi(b); }
void push_back(const T&value){emplace_back(value);} void push_back(T&&value){emplace_back(std::move(value));}
template<typename...Args>reference emplace_back(Args&&...args){ if(size()==capacity_back())_resab(size()+1); alloc_traits::construct(_al(),end(),std::forward<Args>(args)...); _me(1); return back(); }
void push_front(const T&value){emplace_front(value);} void push_front(T&&value){emplace_front(std::move(value));}
template<typename...Args>reference emplace_front(Args&&...args){ if(size()==capacity_front())_resaf(size()+1); alloc_traits::construct(_al(),begin()-1,std::forward<Args>(args)...); _mb(-1); return front(); }
void pop_back(){alloc_traits::destroy(_al(),&back());_me(-1);} T pop_back_element(){auto res(_ncm(back()));pop_back();return res;} void pop_front(){alloc_traits::destroy(_al(),&front());_mb(1);} T pop_front_element(){auto res(_ncm(front()));pop_front();return res;}2 行未改动 void resize_back(size_type cnt){_rb(cnt);} void resize_back(size_type cnt,const T&value){_rb(cnt,value);} void resize(size_type cnt){_rb(cnt);} void resize(size_type cnt,const T&value){_rb(cnt,value);}
template<typename ORT>void swap(veque<T,ORT,Alc>&o)noexcept(noexcept(alloc_traits::propagate_on_container_swap::value||alloc_traits::is_always_equal::value)){ if constexpr(alloc_traits::propagate_on_container_swap::value)_swa(std::move(o)); else{ if(_al()==o._al())_swoa(std::move(o));3 行未改动 _swoa(std::move(new_this));o._swoa(std::move(new_o)); } } }
private:
using _front_realloc=typename RT::allocation_before_front::type; using _back_realloc=typename RT::allocation_after_back::type; using _unused_realloc=std::ratio_add<_front_realloc,_back_realloc>; using _full_realloc=std::ratio_add<std::ratio<1>,_unused_realloc>;
static constexpr auto _rfcs=RT::resize_from_closest_side;
static_assert(_front_realloc::den>0 ); static_assert(_back_realloc::den>0 ); static_assert(std::ratio_greater_equal_v<_front_realloc,std::ratio<0>>,"Reserving negative space is not well-defined"); static_assert(std::ratio_greater_equal_v<_back_realloc,std::ratio<0>>,"Reserving negative space is not well-defined"); static_assert(std::ratio_greater_equal_v<_unused_realloc,std::ratio<0>>,"Reserving negative space is not well-defined");
static constexpr auto _cdcd=std::is_same_v<allocator_type,std::allocator<T>>; static constexpr auto _cccd=std::is_same_v<allocator_type,std::allocator<T>>; static constexpr auto _cdd=std::is_same_v<allocator_type,std::allocator<T>>; size_type _size=0;16 行未改动 } Alc&allocator(){return*this;} const Alc&allocator()const{return*this;} } _data;
template<typename It>veque(It b,It e,const Alc&a,std::input_iterator_tag):veque{a}{for(;b!=e;++b){push_back(*b);}} template<typename It>veque(It b,It e,const Alc&a,std::forward_iterator_tag):veque(_aut{},std::distance(b,e),a){_ccr(b,e,begin());} struct _aut{}; struct _rut{};3 行未改动 static constexpr size_type _cr(size_type size){return size*_full_realloc::num/_full_realloc::den;} static constexpr size_type _co(size_type size){return size*_front_realloc::num/_front_realloc::den;} Alc&_al()noexcept{return _data.allocator();} const Alc&_al()const noexcept{return _data.allocator();}
void _d(const_iterator b,const_iterator e){ if constexpr(std::is_trivially_destructible_v<T>&&_cdd){(void)b;(void)e;} else{ auto start=_mi(b); for(auto i=start;i!=e;++i)alloc_traits::destroy(_al(),i); } }
template<typename ORT>veque&_ca(const veque<T,ORT,Alc>&o){ if constexpr(alloc_traits::propagate_on_container_copy_assignment::value) if constexpr(!alloc_traits::is_always_equal::value) if(o._al()!=_al() || o.size()>capacity_full()){_swa(veque(o,o._al()));return *this;}14 行未改动 } _swoa(std::move(o)); return *this; }
template<typename...Args>void _vcr(const_iterator b,const_iterator e,const Args&...args){ static_assert(sizeof...(args)<=1,"This is for default-or copy-constructing"); if constexpr(std::is_trivially_copy_constructible_v<T>&&_cdcd){ if constexpr(sizeof...(args)==0)std::memset(_mi(b),0,std::distance(b,e)*sizeof(T)); else std::fill(_mi(b),_mi(e),args...); } else for(auto dest=_mi(b);dest!=e;++dest) alloc_traits::construct(_al(),dest,args...); }
template<typename It>void _ccr(It b,It e,const_iterator dest){ static_assert(std::is_convertible_v<typename std::iterator_traits<It>::iterator_category,std::forward_iterator_tag>); if constexpr(std::is_trivially_copy_constructible_v<T>&&_cccd)std::memcpy(_mi(dest),b,std::distance(b,e)*sizeof(T)); else for(;b!=e;++dest,++b)alloc_traits::construct(_al(),dest,*b); }
template<typename It>void _a(It b,It e){ static_assert(std::is_convertible_v<typename std::iterator_traits<It>::iterator_category,std::forward_iterator_tag>); if(std::distance(b,e)>static_cast<difference_type>(capacity_full()))_swoa(veque(b,e,_al())); else _res(b,e); }
template<typename It>void _a(It b,It e,std::forward_iterator_tag){_a(b,e);} template<typename It>void _a(It b,It e,std::input_iterator_tag){clear();for(;b!=e;++b)push_back(*b);}
template<typename It>iterator _i(const_iterator it,It b,It e){ static_assert(std::is_convertible_v<typename std::iterator_traits<It>::iterator_category,std::forward_iterator_tag>); auto res=_is(it,std::distance(b,e)); _ccr(b,e,res);11 行未改动 std::swap(_offset,o._offset); std::swap(_data._allocated,o._data._allocated); std::swap(_data._storage,o._data._storage); }
template<typename...Args>void _rf(size_type cnt,const Args&...args){ difference_type delta=cnt-size(); if(delta>0){ if(cnt>capacity_front())_resaf(cnt);44 行未改动 auto required_size=size()+cnt; auto can_shift_back=capacity_back()>=required_size; if constexpr(std::ratio_greater_v<_front_realloc,std::ratio<0>>) if(can_shift_back&&it==begin())can_shift_back=false;
if constexpr(_rfcs){ auto can_shift_front=capacity_front()>=required_size; if constexpr(std::ratio_greater_v<_back_realloc,std::ratio<0>>) if(can_shift_front&&it==end())can_shift_front=false;
if(can_shift_back&&can_shift_front){ auto index=std::distance(cbegin(),it); if(index<=ssize()/2)can_shift_back=false; else can_shift_front=false;25 行未改动 } _d(std::max(cbegin(),e-cnt),e); } void _shb(const_iterator b,const_iterator e,size_type cnt){ auto start=_mi(b); auto start=_mi(b); if(b==end())return; auto element_cnt=std::distance(b,e); if(element_cnt>0){ if constexpr(std::is_trivially_copyable_v<T>&&std::is_trivially_copy_constructible_v<T>&&_cccd)std::memmove(start+cnt,start,element_cnt*sizeof(T));26 行未改动 } _mb(std::distance(begin(),ib)); _me(std::distance(end(),ib+cnt)); }
void _res(size_type cnt,const T&value){ auto size_delta=static_cast<difference_type>(cnt-size()); auto ib=_sb(); if constexpr(std::ratio_greater_v<_unused_realloc,std::ratio<0>>){26 行未改动 if constexpr(std::is_trivially_copy_constructible_v<T>&&_cccd)std::memcpy(dest,b,size*sizeof(T)); else for(;b!=e;++dest,++b)_nmc(dest,b); } }
static void _nma(iterator dest,iterator src){if constexpr(std::is_nothrow_move_assignable_v<T>)*dest=std::move(*src);else*dest=*src;} static void _nmar(iterator b,iterator e,iterator src){for(auto dest=b;dest!=e;++dest,++src)_nma(dest,src);} void _mb(difference_type cnt)noexcept{_size-=cnt;_offset+=cnt;} void _me(difference_type cnt)noexcept{_size+=cnt;}26 行未改动}
#endifint main(){
}
/*被更改的变量名48 行未改动