📄 wy__arrayvalue.h
字号:
} else { ElemType *ptmp=_alloc_raw(cap*sizeof(ElemType)); if(ptmp==NULL) { WY_THROW( typename Wy_Array<ElemType>::Reply(Wym_ENOMEM) ); } for(_b_end=ptmp, _b_eob=ptmp+n_elem; _b_end<_b_eob; ++_b_end) { *_b_end= elem; } _free_raw(_b_bgn); _b_bgn=ptmp; _b_eob=ptmp+cap; } WY__CHK_VALIDOBJ(*this); }; const Wy__ArrayValue& operator =(const Wy__ArrayValue& rhs) { this->reset(rhs); return *this; }; const Wy__ArrayValue& operator=(const WySeg<const ElemType>& src) { WY__CHK_VALIDOBJ(*this); const SizeType src_size(src.size()); if(src_size<=this->_capacity()) { if(src.is_overlap( WySeg<const ElemType>(_b_bgn,SizeType(_b_eob-_b_bgn)) )) { WY_THROW( typename Wy_Array<ElemType>::Reply(Wym_ELOOP) ); } ::memcpy(_b_bgn,src.begin(),src_size*sizeof(ElemType)); _b_end=_b_bgn+src_size; } else { // This algorithm duplicate src first // const SizeType cap( src_size>=Wy_Array<ElemType>::min_capacity()? src_size : Wy_Array<ElemType>::min_capacity() ); ElemType *ptmp=_dup_array(src.begin(),src_size,cap); if(ptmp==NULL) { WY_THROW( typename Wy_Array<ElemType>::Reply(Wym_ENOMEM) ); } _free_raw(_b_bgn); _b_bgn=ptmp; _b_end=ptmp+src_size; _b_eob=ptmp+cap; } WY__CHK_VALIDOBJ(*this); return(*this); }; const Wy__ArrayValue& operator=(const WySeg<ElemType>& src) { WY__CHK_VALIDOBJ(*this); const SizeType src_size(src.size()); if(src_size<=this->_capacity()) { if(src.is_overlap( WySeg<ElemType>(_b_bgn,SizeType(_b_eob-_b_bgn)) )) { WY_THROW( typename Wy_Array<ElemType>::Reply(Wym_ELOOP) ); } ::memcpy(_b_bgn,src.begin(),src_size*sizeof(ElemType)); _b_end=_b_bgn+src_size; } else { // This algorithm duplicate src first // const SizeType cap( src_size>=Wy_Array<ElemType>::min_capacity()? src_size : Wy_Array<ElemType>::min_capacity() ); ElemType *ptmp=_dup_array(src.begin(),src_size,cap); if(ptmp==NULL) { WY_THROW( typename Wy_Array<ElemType>::Reply(Wym_ENOMEM) ); } _free_raw(_b_bgn); _b_bgn=ptmp; _b_end=ptmp+src_size; _b_eob=ptmp+cap; } WY__CHK_VALIDOBJ(*this); return(*this); }; void swap(Wy__ArrayValue& v2) WY__TSPC() { WY__CHK_VALIDOBJ(*this); // v.swap(v) ok { ElemType* tmp(_b_bgn); _b_bgn=v2._b_bgn; v2._b_bgn=tmp; tmp=_b_end; _b_end=v2._b_end; v2._b_end=tmp; } { const ElemType* tmp(_b_eob); _b_eob=v2._b_eob; v2._b_eob=tmp; } WY__CHK_VALIDOBJ(*this); /* This definition may be better char buf[sizeof(Wy__ArrayValue)]; // alignment problem! new(buf) Wy__ArrayValue(v2,ByMove); new(v2) Wy__ArrayValue(*this,ByMove); new(this) Wy__ArrayValue(*((Wy__ArrayValue*)buf),ByMove); */ }; // note: argument elem is a real object (not reference type) // void push_back(ElemType elem) { // elem in *this ok WY__CHK_VALIDOBJ(*this); if(_b_end<_b_eob) { *_b_end=elem; ++_b_end; } else { const SizeType OrgSize( this->size() ); #ifdef WY_DEBUG if(_b_eob!=_b_end) { WY_TERMINATE(""); } #endif // allocate another array twice as large const SizeType cap( OrgSize<<1 ); if(cap>this->max_capacity()) { WY_THROW( typename Wy_Array<ElemType>::Reply(Wym_EFBIG) ); } ElemType *ptmp=_alloc_raw(cap*sizeof(ElemType)); if(ptmp==NULL) { WY_THROW( typename Wy_Array<ElemType>::Reply(Wym_ENOMEM) ); } std::memcpy(ptmp,_b_bgn,sizeof(ElemType)*OrgSize); ptmp[OrgSize]=elem; // update _b_bgn,_b_end,_b_eob to the new array(ptmp) _free_raw(_b_bgn); _b_bgn=ptmp; _b_end=ptmp+(OrgSize+1); _b_eob=ptmp+cap; } WY__CHK_VALIDOBJ(*this); }; void pop_back(void) { WY__CHK_VALIDOBJ(*this); if(_b_end==_b_bgn) { return; } --_b_end; WY__CHK_VALIDOBJ(*this); }; void erase(SizeType idx, SizeType n) { WY__CHK_VALIDOBJ(*this); ElemType *p1(_b_bgn+idx); // p1 -> start of erase if(p1>=_b_end) { if(p1==_b_end) { WY__CHK_VALIDOBJ(*this); return; } WY_THROW( typename Wy_Array<ElemType>::Reply(Wym_EINVAL) ); } // assert( p1<_b_end ); ElemType *p2=_b_end; if(SizeType(p2-p1)>=n) { if(n<=0) { WY__CHK_VALIDOBJ(*this); return; } p2=p1+n; } const SizeType NumRem(_b_end-p2); std::memmove(p1,p2,NumRem*sizeof(ElemType)); _b_end=p1+NumRem; WY__CHK_VALIDOBJ(*this); };/* This member may be redundent from resize(len) void erase(SizeType idx) { WY__CHK_VALIDOBJ(*this); ElemType *p1(_b_bgn+idx); // p1 -> start of erase if(p1>=_b_end) { if(p1==_b_end) { WY__CHK_VALIDOBJ(*this); return; } WY_THROW( typename Wy_Array<ElemType>::Reply(Wym_EINVAL) ); } // assert( p1<_b_end ); _b_end=p1; WY__CHK_VALIDOBJ(*this); };*/ // note: argument elem is a real object (not reference type) // void insert(SizeType idx, const ElemType elem) { // elem in *this ok WY__CHK_VALIDOBJ(*this); const SizeType OrgSize( this->size() ); if(idx>OrgSize) { WY_THROW( typename Wy_Array<ElemType>::Reply(Wym_EINVAL) ); } if(OrgSize<this->_capacity()) { ElemType *p1(_b_bgn+idx); // insert point std::memmove(p1+1,p1,(_b_end-p1)*sizeof(ElemType)); *p1=elem; ++_b_end; } else { #ifdef WY_DEBUG if(OrgSize!=this->_capacity()) { WY_TERMINATE(""); } #endif // allocate another array twice as large const SizeType cap( OrgSize<<1 ); if(cap>this->max_capacity()) { WY_THROW( typename Wy_Array<ElemType>::Reply(Wym_EFBIG) ); } ElemType *ptmp=_alloc_raw(cap*sizeof(ElemType)); if(ptmp==NULL) { WY_THROW( typename Wy_Array<ElemType>::Reply(Wym_ENOMEM) ); } std::memcpy(ptmp,_b_bgn,idx*sizeof(ElemType)); ptmp[idx]=elem; std::memcpy(ptmp+idx+1,_b_bgn+idx,(OrgSize-idx)*sizeof(ElemType)); // update _b_bgn,_b_end,_b_eob to the new array(ptmp) ElemType *p2(_b_bgn); _b_bgn=ptmp; _b_end=ptmp+OrgSize+1; _b_eob=ptmp+cap; _free_raw(p2); } WY__CHK_VALIDOBJ(*this); }; void insert(SizeType idx, SizeType n_elem, const ElemType& elem) { WY__CHK_VALIDOBJ(*this); const SizeType OrgSize( this->size() ); if(idx>OrgSize) { WY_THROW( typename Wy_Array<ElemType>::Reply(Wym_EINVAL) ); } if( (OrgSize+n_elem)<=this->_capacity()) { ElemType *p1(_b_bgn+idx); // insert point std::memmove(p1+n_elem,p1,(_b_end-p1)*sizeof(ElemType)); for(SizeType i=0; i<n_elem; ++i,++p1) { *p1=elem; } _b_end+=n_elem; } else { // allocate another array twice as large if(n_elem>this->max_capacity()) { WY_THROW( typename Wy_Array<ElemType>::Reply(Wym_EFBIG) ); } const SizeType cap( (n_elem+OrgSize)<<1 ); if(cap>this->max_capacity()) { WY_THROW( typename Wy_Array<ElemType>::Reply(Wym_EFBIG) ); } ElemType *ptmp=_alloc_raw(cap*sizeof(ElemType)); if(ptmp==NULL) { WY_THROW( typename Wy_Array<ElemType>::Reply(Wym_ENOMEM) ); } std::memcpy(ptmp,_b_bgn,idx*sizeof(ElemType)); ElemType *p1(ptmp+idx); for(SizeType i=0; i<n_elem; ++i,++p1) { *p1=elem; } std::memcpy(ptmp+(idx+n_elem),_b_bgn+idx,(OrgSize-idx)*sizeof(ElemType)); // update _b_bgn,_b_end,_b_eob to the new array(ptmp) p1=_b_bgn; _b_bgn=ptmp; _b_end=ptmp+OrgSize+n_elem; _b_eob=ptmp+cap; _free_raw(p1); } WY__CHK_VALIDOBJ(*this); }; void insert(SizeType idx, const WySeg<const ElemType>& src) { WY__CHK_VALIDOBJ(*this); const SizeType OrgSize( this->size() ); if(idx>OrgSize) { WY_THROW( typename Wy_Array<ElemType>::Reply(Wym_EINVAL) ); } if(src.is_overlap( WySeg<const ElemType>(_b_bgn,(ElemType*)_b_eob) )) { WY_THROW( typename Wy_Array<ElemType>::Reply(Wym_ELOOP) ); } const SizeType src_size( src.size() ); if( (OrgSize+src_size)<=this->_capacity()) { ElemType *p1(_b_bgn+idx); // insert point std::memmove(p1+src_size,p1,(_b_end-p1)*sizeof(ElemType)); std::memcpy(p1,src.begin(),src_size*sizeof(ElemType)); _b_end+=src_size; } else { // allocate another array twice as large if(src_size>this->max_capacity()) { WY_THROW( typename Wy_Array<ElemType>::Reply(Wym_EFBIG) ); } const SizeType cap( (src_size+OrgSize)<<1 ); if(cap>this->max_capacity()) { WY_THROW( typename Wy_Array<ElemType>::Reply(Wym_EFBIG) ); } ElemType *ptmp=_alloc_raw(cap*sizeof(ElemType)); if(ptmp==NULL) { WY_THROW( typename Wy_Array<ElemType>::Reply(Wym_ENOMEM) ); } std::memcpy(ptmp,_b_bgn,idx*sizeof(ElemType)); std::memcpy(ptmp+idx,src.begin(),src_size*sizeof(ElemType)); std::memcpy(ptmp+(idx+src_size),_b_bgn+idx,(OrgSize-idx)*sizeof(ElemType)); // update _b_bgn,_b_end,_b_eob to the new array(ptmp) ElemType *p2(_b_bgn); _b_bgn=ptmp; _b_end=ptmp+OrgSize+src_size; _b_eob=ptmp+cap; _free_raw(p2); } WY__CHK_VALIDOBJ(*this); }; void insert(SizeType idx, const WySeg<ElemType>& src) { WY__CHK_VALIDOBJ(*this); const SizeType OrgSize( this->size() ); if(idx>OrgSize) { WY_THROW( typename Wy_Array<ElemType>::Reply(Wym_EINVAL) ); } if(src.is_overlap( WySeg<ElemType>(_b_bgn,(ElemType*)_b_eob) )) { WY_THROW( typename Wy_Array<ElemType>::Reply(Wym_ELOOP) ); } const SizeType src_size( src.size() ); if( (OrgSize+src_size)<=this->_capacity()) { ElemType *p1(_b_bgn+idx); // insert point std::memmove(p1+src_size,p1,(_b_end-p1)*sizeof(ElemType)); std::memcpy(p1,src.begin(),src_size*sizeof(ElemType)); _b_end+=src_size; } else { // allocate another array twice as large if(src_size>this->max_capacity()) { WY_THROW( typename Wy_Array<ElemType>::Reply(Wym_EFBIG) ); } const SizeType cap( (src_size+OrgSize)<<1 ); if(cap>this->max_capacity()) { WY_THROW( typename Wy_Array<ElemType>::Reply(Wym_EFBIG) ); } ElemType *ptmp=_alloc_raw(cap*sizeof(ElemType)); if(ptmp==NULL) { WY_THROW( typename Wy_Array<ElemType>::Reply(Wym_ENOMEM) ); } std::memcpy(ptmp,_b_bgn,idx*sizeof(ElemType)); std::memcpy(ptmp+idx,src.begin(),src_size*sizeof(ElemType)); std::memcpy(ptmp+(idx+src_size),_b_bgn+idx,(OrgSize-idx)*sizeof(ElemType)); // update _b_bgn,_b_end,_b_eob to the new array(ptmp) ElemType *p2(_b_bgn); _b_bgn=ptmp; _b_end=ptmp+OrgSize+src_size; _b_eob=ptmp+cap; _free_raw(p2); } WY__CHK_VALIDOBJ(*this); }; void insert(SizeType idx, const Wy_Array<ElemType>& 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( typename Wy_Array<ElemType>::Reply(Wym_EINVAL) ); } // Clamp 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( typename Wy_Array<ElemType>::Reply(Wym_EFBIG) ); } if(rs<=this->_capacity()) { const ElemType InsElem(elem); // in case elem in *this ElemType *des_ptr=_b_bgn+sidx; // insert point ElemType *src_ptr(des_ptr+slen);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -