📄 wy_array.h
字号:
}; _b_end+=src_size; } else { // allocate another array twice as large if(src_size>this->max_capacity()) { WY_THROW( Reply(Wym_EFBIG) ); } const SizeType cap( (src_size+this->size())<<1 ); if(cap>this->max_capacity()) { WY_THROW( Reply(Wym_EFBIG) ); } ElemType *ptmp=_alloc_raw(cap*sizeof(ElemType)); if(ptmp==NULL) { WY_THROW( Reply(Wym_ENOMEM) ); } ElemType *p1(ptmp+idx); // insert point (in ptmp) ElemType *p2(p1); try { for(SizeType i=0; i<src_size; ++i,++p2) { new(p2) ElemType(src[i]); } } catch(...) { _destruct(p1,p2); _free_raw(ptmp); throw; }; ElemType *des_ptr(ptmp); p2=_b_bgn; // move all elements in _b_bgn to ptmp for(; des_ptr!=p1; ++p2,++des_ptr) { new(des_ptr) ElemType(*p2,Wy::ByMove); } des_ptr+=src_size; for(; p2!=_b_end; ++p2,++des_ptr) { new(des_ptr) ElemType(*p2,Wy::ByMove); } // update _b_bgn,_b_end,_b_eob to the new array(ptmp) p2=_b_bgn; _b_bgn=ptmp; _b_end=des_ptr; _b_eob=ptmp+cap; _free_raw(p2); } WY__CHK_VALIDOBJ(*this); }; void insert(SizeType idx, const WySeg<ElemType>& src) { WY__CHK_VALIDOBJ(*this); if(idx>this->size()) { WY_THROW( Reply(Wym_EINVAL) ); } if(src.is_overlap( WySeg<ElemType>(_b_bgn,(ElemType*)_b_eob) )) { WY_THROW( Reply(Wym_ELOOP) ); } const SizeType src_size( src.size() ); if(this->size()+src_size<=this->_capacity()) { if(src_size==0) { return; } ElemType * const p0(_b_bgn+idx); // insert point ElemType *p1(_b_end); // point to the last+1 ElemType *p2(p1+src_size); // move dest.+1 for(; p1>p0; ) { --p1; --p2; new(p2) ElemType(*(p1),Wy::ByMove); } try { p2=p0; for(SizeType i=0; i<src_size; ++i,++p2) { new(p2) ElemType(src[i]); } } catch(...) { p1=p0; _destruct(p1,p2); for(p2=p1+src_size; p1<_b_end; ++p1) { new(p1) ElemType(*(p2),Wy::ByMove); } throw; }; _b_end+=src_size; } else { // allocate another array twice as large if(src_size>this->max_capacity()) { WY_THROW( Reply(Wym_EFBIG) ); } const SizeType cap( (src_size+this->size())<<1 ); if(cap>this->max_capacity()) { WY_THROW( Reply(Wym_EFBIG) ); } ElemType *ptmp=_alloc_raw(cap*sizeof(ElemType)); if(ptmp==NULL) { WY_THROW( Reply(Wym_ENOMEM) ); } ElemType *p1(ptmp+idx); // insert point (in ptmp) ElemType *p2(p1); try { for(SizeType i=0; i<src_size; ++i,++p2) { new(p2) ElemType(src[i]); } } catch(...) { _destruct(p1,p2); _free_raw(ptmp); throw; }; ElemType *des_ptr(ptmp); p2=_b_bgn; // move all elements in _b_bgn to ptmp for(; des_ptr!=p1; ++p2,++des_ptr) { new(des_ptr) ElemType(*p2,Wy::ByMove); } des_ptr+=src_size; for(; p2!=_b_end; ++p2,++des_ptr) { new(des_ptr) ElemType(*p2,Wy::ByMove); } // update _b_bgn,_b_end,_b_eob to the new array(ptmp) p2=_b_bgn; _b_bgn=ptmp; _b_end=des_ptr; _b_eob=ptmp+cap; _free_raw(p2); } WY__CHK_VALIDOBJ(*this); }; void insert(SizeType idx, const Wy_Array& src) { this->insert(idx, src.subseg()); }; void replace(SizeType sidx, SizeType slen, SizeType n_elem, const ElemType& elem) { WY__CHK_VALIDOBJ(*this); const SizeType OrgSize( this->size() ); if(sidx>OrgSize) { WY_THROW( Reply(Wym_EINVAL) ); } // Clip slen to at most the existing number of elements [sidx,_b_end) SizeType rs= OrgSize-sidx; if(slen>rs) { slen=rs; } rs=(OrgSize-slen)+n_elem; // resultant size of this array if(rs>this->max_capacity()) { WY_THROW( Reply(Wym_EFBIG) ); } if(rs<=this->_capacity()) { const ElemType InsElem(elem); // in case elem in *this // Destruct elements indicated by (sidx:slen) ElemType *des_ptr=_b_bgn+sidx; // insert point ElemType *src_ptr(des_ptr+slen); try { _destruct(des_ptr,src_ptr); } catch(...) { // Make *this default and rethrow _destruct(_b_bgn,des_ptr); _destruct(src_ptr,_b_end); _b_end=_b_bgn; throw; }; #ifdef WY_DEBUG if(((des_ptr-_b_bgn)+n_elem+(_b_end-src_ptr))!=rs) { WY_TERMINATE(""); } #endif _objmove(des_ptr+n_elem,src_ptr,_b_end-src_ptr); for(SizeType i=0 ; i<n_elem; ++des_ptr,++i) try { new(des_ptr) ElemType(InsElem); } catch(...) { for( ; i; --i) { --des_ptr; _destruct(des_ptr); } // Make *this default and rethrow _destruct(_b_bgn,des_ptr); _destruct(src_ptr,_b_end); _b_end=_b_bgn; throw; }; _b_end=_b_bgn+rs; } else { // allocate memory for the new array const SizeType cap( rs ); ElemType *ptmp=_alloc_raw(cap*sizeof(ElemType)); if(ptmp==NULL) { WY_THROW( Reply(Wym_ENOMEM) ); } ElemType *p1(ptmp+sidx); // insert point for(SizeType i=0; i<n_elem; ++p1,++i) try { new(p1) ElemType(elem); } catch(...) { _destruct(ptmp+sidx,p1); _free_raw(ptmp); throw; }; // Destruct elements indicated by (sidx:slen) ElemType *des_ptr=_b_bgn+sidx; ElemType *p2(des_ptr+slen); try { _destruct(des_ptr,p2); } catch(...) { // Make *this default and rethrow _destruct(_b_bgn,des_ptr); _destruct(p2,_b_end); _b_end=_b_bgn; p1=ptmp+sidx; _destruct(p1,p1+n_elem); _free_raw(ptmp); throw; }; des_ptr=ptmp; p1=ptmp+sidx; p2=_b_bgn; // move all elements in _b_bgn to ptmp for(; des_ptr!=p1; ++p2,++des_ptr) { new(des_ptr) T(*p2,Wy::ByMove); } des_ptr+=n_elem; p2+=slen; for(; p2!=_b_end; ++p2,++des_ptr) { new(des_ptr) T(*p2,Wy::ByMove); } // update _b_bgn,_b_end,_b_eob to the new array(ptmp) p2=_b_bgn; _b_bgn=ptmp; _b_end=des_ptr; _b_eob=ptmp+cap; _free_raw(p2); }; WY__CHK_VALIDOBJ(*this); }; void resize(SizeType nsize) { WY__CHK_VALIDOBJ(*this); if(nsize<this->size()) { ElemType *p1(_b_bgn+nsize); ElemType *p2(_b_end); _b_end=p1; _destruct(p1,p2); } else if(nsize>this->size()) { // reserve enough capacity const WyRet r( this->_reserve(nsize) ); if(r!=Ok) { WY_THROW( Reply(r) ); } ElemType *p1(_b_end); ElemType *p2(_b_bgn+nsize); try { for(; p1<p2; ++p1) { new(p1) ElemType(); } } catch(...) { _destruct(_b_end,p1); throw; }; _b_end=p2; } else { }; WY__CHK_VALIDOBJ(*this); }; void resize(SizeType nsize, const ElemType& elem) { WY__CHK_VALIDOBJ(*this); if(nsize<this->size()) { ElemType *p1(_b_bgn+nsize); ElemType *p2(_b_end); _b_end=p1; _destruct(p1,p2); } else if(nsize>this->size()) { // reserve enough capacity const WyRet r( this->_reserve(nsize) ); if(r!=Ok) { WY_THROW( Reply(r) ); } ElemType *p1(_b_end); ElemType *p2(_b_bgn+nsize); try { for(; p1<p2; ++p1) { new(p1) ElemType(elem); } } catch(...) { _destruct(_b_end,p1); throw; }; _b_end=p2; } else { }; WY__CHK_VALIDOBJ(*this); }; bool operator ==(const Wy_Array& rhs) const { WY__CHK_VALIDOBJ(*this); if(this->size()!=rhs.size()) { return(false); } for(const ElemType *p1(_b_bgn), *p2(rhs._b_bgn); p1!=_b_end; ++p1,++p2) { if((p1->operator ==(*p2))==false) { return(false); } } return(true); }; bool operator !=(const Wy_Array& rhs) const { WY__CHK_VALIDOBJ(*this); if(this->size()!=rhs.size()) { return(true); } for(const ElemType *p1(_b_bgn), *p2(rhs._b_bgn); p1!=_b_end; ++p1,++p2) { if((p1->operator !=(*p2))==true) { return(true); } } return(false); }; SizeType _capacity(void) const WY__TSPC() { WY__CHK_VALIDOBJ(*this); return _b_eob-_b_bgn; }; WyRet _reserve(SizeType rcap) WY__TSPC() { WY__CHK_VALIDOBJ(*this); if(rcap<=this->_capacity()) { return(Ok); } if(rcap>this->max_capacity()) { WY_RETURN( Wym_EFBIG ); } // allocate another array of rcap capacity ElemType *ptmp=_alloc_raw(rcap*sizeof(ElemType)); if(ptmp==NULL) { WY_RETURN( Wym_ENOMEM ); } ElemType *des_ptr(ptmp+this->size()); // move all elements of *this to ptmp ElemType *src_ptr(_b_end); while(des_ptr!=ptmp) { --des_ptr; --src_ptr; new(des_ptr) T(*src_ptr,Wy::ByMove); } #ifdef WY_DEBUG if((des_ptr!=ptmp)||(src_ptr!=_b_bgn)) { WY_TERMINATE(""); } #endif // update _b_bgn,_b_end,_b_eob to the new array(ptmp) des_ptr=_b_end; _b_end=ptmp+(this->size()); _b_bgn=ptmp; _b_eob=ptmp+rcap; // free the original array _free_raw(src_ptr); WY__CHK_VALIDOBJ(*this); return(Ok); };};template<typename T>const size_t Wy_Array<T>::MaxSizeRawMemory =Wy_Array_MaxSizeRawMemory;template<typename T>const size_t Wy_Array<T>::MinimumCapacity =Wy_Array_MinimumCapacity;template<typename T>const size_t Wy_Array<T>::DefaultSize =Wy_Array_DefaultSize;#include "wy__arrayvalue.h"#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -