📄 wy_array.h
字号:
{ WY__CHK_VALIDOBJ(*this); if(n>=SizeType(_b_end-_b_bgn)) { WY_THROW( Reply(Wym_EINVAL) ); } return _b_bgn[n]; }; // Note: capacity is intact // void reset(void) { WY__CHK_VALIDOBJ(*this); try { _destruct(_b_bgn,_b_end); } catch(...) { _b_end=_b_bgn; throw; }; _b_end=_b_bgn; WY__CHK_VALIDOBJ(*this); }; void reset(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( Reply(Wym_ELOOP) ); } // This algorithm reset *this first and reuse current array, // i.e. whatever copy constructor of ElemType throws will // be rethrown, *this is default. // try { _destruct(_b_bgn,_b_end); } catch(...) { // postcondion, for stack unwind from ~T() _b_end=_b_bgn; // is default for this member throw; }; _b_end=_b_bgn; // copy construct elements try { const ElemType *src_ptr(src.begin()); for( ; src_ptr!=src.end(); ++src_ptr,++_b_end) { new(_b_end) ElemType(*src_ptr); }; } catch(...) { _destruct(_b_bgn,_b_end); _b_end=_b_bgn; throw; }; } else { // This algorithm duplicate src first // const SizeType cap( src_size>=Wy_Array::min_capacity()? src_size : Wy_Array::min_capacity() ); ElemType *ptmp=_dup_array(src.begin(),src_size,cap); if(ptmp==NULL) { WY_THROW( Reply(Wym_ENOMEM) ); } // reset() *this try { _destruct(_b_bgn,_b_end); } catch(...) { // postcondion, for stack unwind from ~T() _b_end=_b_bgn; // is default for this member _destruct(ptmp,ptmp+src_size); _free_raw(ptmp); throw; }; _free_raw(_b_bgn); _b_bgn=ptmp; _b_end=ptmp+src_size; _b_eob=ptmp+cap; } WY__CHK_VALIDOBJ(*this); }; void reset(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( Reply(Wym_ELOOP) ); } // This algorithm reset *this first and reuse current array, // i.e. whatever copy constructor of ElemType throws will // be rethrown, *this is default. // try { _destruct(_b_bgn,_b_end); } catch(...) { // postcondion, for stack unwind from ~T() _b_end=_b_bgn; // is default for this member throw; }; _b_end=_b_bgn; // copy construct elements try { const ElemType *src_ptr(src.begin()); for( ; src_ptr!=src.end(); ++src_ptr,++_b_end) { new(_b_end) ElemType(*src_ptr); }; } catch(...) { _destruct(_b_bgn,_b_end); _b_end=_b_bgn; throw; }; } else { // This algorithm duplicate src first // const SizeType cap( src_size>=Wy_Array::min_capacity()? src_size : Wy_Array::min_capacity() ); ElemType *ptmp=_dup_array(src.begin(),src_size,cap); if(ptmp==NULL) { WY_THROW( Reply(Wym_ENOMEM) ); } // reset() *this try { _destruct(_b_bgn,_b_end); } catch(...) { // postcondion, for stack unwind from ~T() _b_end=_b_bgn; // is default for this member _destruct(ptmp,ptmp+src_size); _free_raw(ptmp); throw; }; _free_raw(_b_bgn); _b_bgn=ptmp; _b_end=ptmp+src_size; _b_eob=ptmp+cap; } WY__CHK_VALIDOBJ(*this); }; // Note: capacity is set to needed minimum if reallocation occurred, // otherwise capacity is intact // void reset(const Wy_Array& src) { WY__CHK_VALIDOBJ(*this); const SizeType src_size(src.size()); if(src_size<=this->_capacity()) { if(_b_bgn==src._b_bgn) { return; // v.reset(v) } // This algorithm reset *this first and reuse current array, // i.e. whatever copy constructor of ElemType throws will // be rethrown, *this is default. // try { _destruct(_b_bgn,_b_end); } catch(...) { // postcondion, for stack unwind from ~T() _b_end=_b_bgn; // is default for this member throw; }; _b_end=_b_bgn; // copy construct elements try { ElemType *src_ptr(src._b_bgn); for( ; src_ptr!=src._b_end; ++src_ptr,++_b_end) { new(_b_end) ElemType(*src_ptr); }; } catch(...) { _destruct(_b_bgn,_b_end); _b_end=_b_bgn; throw; }; } else { // This algorithm duplicate src first // const SizeType cap( src_size>=Wy_Array::min_capacity()? src_size : Wy_Array::min_capacity() ); ElemType *ptmp=_dup_array(src._b_bgn,src_size,cap); if(ptmp==NULL) { WY_THROW( Reply(Wym_ENOMEM) ); } // reset() *this try { _destruct(_b_bgn,_b_end); } catch(...) { // postcondion, for stack unwind from ~T() _b_end=_b_bgn; // is default for this member _destruct(ptmp,ptmp+src_size); _free_raw(ptmp); throw; }; _free_raw(_b_bgn); _b_bgn=ptmp; _b_end=ptmp+src_size; _b_eob=ptmp+cap; } WY__CHK_VALIDOBJ(*this); }; void reset(size_t n_elem, const ElemType& elem) { WY__CHK_VALIDOBJ(*this); const SizeType cap( n_elem<MinimumCapacity? MinimumCapacity : n_elem ); if(cap>this->max_capacity()) { WY_THROW( Reply(Wym_EFBIG) ); } if(cap<=this->_capacity()) { this->reset(); try { WY_ASSERT(_b_bgn==_b_end); for(SizeType i=0; i<n_elem; ++i,++_b_end) { new(_b_end) ElemType(elem); } } catch(...) { _destruct(_b_bgn,_b_end); _b_end=_b_bgn; throw; }; } else { ElemType *ptmp=_alloc_raw(cap*sizeof(ElemType)); if(ptmp==NULL) { WY_THROW( Reply(Wym_ENOMEM) ); } ElemType *p1(ptmp); ElemType *p2(ptmp+n_elem); try { for(; p1<p2; ++p1) { new(p1) ElemType(elem); } } catch(...) { _destruct(ptmp,p1); _free_raw(ptmp); throw; }; p1= _b_bgn; p2= _b_end; _b_bgn=ptmp; _b_end=ptmp+n_elem; _b_eob=ptmp+cap; try { _destruct(p1,p2); } catch(...) { _free_raw(ptmp); throw; }; _free_raw(p1); } WY__CHK_VALIDOBJ(*this); }; // note: property relies on ElemType::operator=, otherwise quite // identical the definition of reset(const Wy_Array&) // const Wy_Array& operator =(const Wy_Array& rhs) { WY__CHK_VALIDOBJ(*this); const SizeType src_size(rhs.size()); if(src_size<=this->_capacity()) { if(_b_bgn==rhs._b_bgn) { return(*this); // v.reset(v) } // This algorithm deliberately uses ElemType::operator= to require // it being defined, for object equivalence consideration. // // note: Whatever object thrown from ElemType is rethrown, // *this is default. (for convenience) // const ElemType *src_ptr(rhs._b_bgn); ElemType *des_ptr(this->_b_bgn); try { if(this->size()<=src_size) { for( ; des_ptr!=_b_end; ++des_ptr,++src_ptr) { *des_ptr=*src_ptr; } for( ; src_ptr!=rhs._b_end; ++src_ptr,++_b_end) { new(_b_end) ElemType(*src_ptr); }; } else { for( ; src_ptr!=rhs._b_end; ++des_ptr,++src_ptr) { *des_ptr=*src_ptr; } ElemType *ptmp(_b_end); _b_end=des_ptr; _destruct(_b_end,ptmp); } } catch(...) { _destruct(_b_bgn,_b_end); _b_end=_b_bgn; throw; }; } else { // This algorithm duplicate src on different memory block // const SizeType cap( src_size>=Wy_Array::min_capacity()? src_size : Wy_Array::min_capacity() ); ElemType *ptmp=_dup_array(rhs._b_bgn,src_size,cap); if(ptmp==NULL) { WY_THROW( Reply(Wym_ENOMEM) ); } // reset() *this try { _destruct(_b_bgn,_b_end); } catch(...) { // postcondion, for stack unwind from ~T() _b_end=_b_bgn; // is default for this member _destruct(ptmp,ptmp+src_size); _free_raw(ptmp); throw; }; _free_raw(_b_bgn); _b_bgn=ptmp; _b_end=ptmp+src_size; _b_eob=ptmp+cap; } WY__CHK_VALIDOBJ(*this); return(*this); }; const Wy_Array& 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( Reply(Wym_ELOOP) ); } // This algorithm reset *this first and reuse current array, // i.e. whatever copy constructor of ElemType throws will // be rethrown, *this is default. // try { _destruct(_b_bgn,_b_end); } catch(...) { // postcondion, for stack unwind from ~T() _b_end=_b_bgn; // is default for this member throw; }; _b_end=_b_bgn; // copy construct elements try { const ElemType *src_ptr(src.begin()); for( ; src_ptr!=src.end(); ++src_ptr,++_b_end) { new(_b_end) ElemType(*src_ptr); }; } catch(...) { _destruct(_b_bgn,_b_end); _b_end=_b_bgn; throw; }; } else { // This algorithm duplicate src first // const SizeType cap( src_size>=Wy_Array::min_capacity()? src_size : Wy_Array::min_capacity() ); ElemType *ptmp=_dup_array(src.begin(),src_size,cap); if(ptmp==NULL) { WY_THROW( Reply(Wym_ENOMEM) ); } // reset() *this try { _destruct(_b_bgn,_b_end); } catch(...) { // postcondion, for stack unwind from ~T() _b_end=_b_bgn; // is default for this member _destruct(ptmp,ptmp+src_size); _free_raw(ptmp); throw; }; _free_raw(_b_bgn); _b_bgn=ptmp; _b_end=ptmp+src_size; _b_eob=ptmp+cap; } WY__CHK_VALIDOBJ(*this); return(*this); }; const Wy_Array& 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( Reply(Wym_ELOOP) );
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -