The requirements for writing applications and libraries are totally different-STL insert_n

Source: Internet
Author: User

On cross-platform big data, you cannot find STL on Symbian, and there is no rarray on Windows Mobile or brew. Obviously, you need to re-write a similar container to meet similar requirements. Here, we strongly feel that the requirements for writing applications are totally different from those of libraries.

Some common insert, resize, and other operations in STD: vector usually have a common internal implementation function. What we see in ms stl is _ insert_n, what we see in SGI-STL is _ m_insert_aux. We can understand the subtleties through code. See _ insert_n.

Void _ insert_n (iterator _ Where, <br/> size_type _ count, const _ ty & _ Val) <br/> {// insert _ count * _ Val at _ Where <br/> # If _ has_iterator_debugging <br/> If (_ Where. _ mycont! = This <br/> | _ Where. _ myptr <_ myfirst | _ mylast <_ Where. _ myptr) <br/> _ debug_error ("vector insert iterator outside range "); <br/> # endif/* _ has_iterator_debugging */<br/> _ ty _ TMP = _ Val; // In case _ Val is in sequence <br/> size_type _ capacity = capacity (); <br/> If (_ COUNT = 0) <br/>; <br/> else if (max_size ()-size () <_ count) <br/> _ xlen (); // result too long <br/> else if (_ capacity <size () + _ Count) <br/> {// not enough room, reallocate <br/> _ capacity = max_size ()-_ capacity/2 <_ capacity <br/>? 0: _ capacity + _ capacity/2; // try to grow by 50% <br/> If (_ capacity <size () + _ count) <br/> _ capacity = size () + _ count; <br/> pointer _ newvec = This-> _ alval. allocate (_ capacity); <br/> pointer _ PTR = _ newvec; <br/> _ try_begin <br/> _ PTR = _ umove (_ myfirst, _ vec_iter_base (_ Where), <br/> _ newvec); // copy prefix <br/> _ PTR = _ ufill (_ PTR, _ count, _ TMP ); // Add new stuff <br/> _ umove (_ vec_iter_base (_ Where ), _ Mylast, _ PTR); // copy suffix <br/> _ catch_all <br/> _ destroy (_ newvec, _ PTR ); <br/> This-> _ alval. deallocate (_ newvec, _ capacity); <br/> _ reraise; <br/> _ catch_end <br/> _ count + = size (); <br/> If (_ myfirst! = 0) <br/> {// destroy and deallocate old array <br/> _ destroy (_ myfirst, _ mylast); <br/> This-> _ alval. deallocate (_ myfirst, _ myend-_ myfirst); <br/>}< br/> # If _ has_iterator_debugging <br/> This-> _ orphan_all (); <br/> # endif/* _ has_iterator_debugging */<br/> _ myend = _ newvec + _ capacity; <br/> _ mylast = _ newvec + _ count; <br/> _ myfirst = _ newvec; <br/>}< br/> else if (size_type) (_ mylast-_ vec_iter_base (_ Where) <_ count) <br/> {// new stuff spills off end <br/> _ umove (_ vec_iter_base (_ Where), _ mylast, <br/> _ vec_iter_base (_ Where) + _ count); // copy suffix <br/> _ try_begin <br/> _ ufill (_ mylast, _ count-(_ mylast-_ vec_iter_base (_ Where )), <br/> _ TMP); // Insert new stuff off end <br/> _ catch_all <br/> _ destroy (_ vec_iter_base (_ Where) + _ count, _ mylast + _ count); <br/> _ reraise; <br/> _ catch_end <br/> _ mylast + = _ count; <br/> # If _ has_iterator_debugging <br/> _ orphan_range (_ Where. _ myptr, _ mylast); <br/> # endif/* _ has_iterator_debugging */<br/> Fill (_ vec_iter_base (_ Where), _ mylast-_ count, <br/> _ TMP ); // insert up to old end <br/>}< br/> else <br/> {// new stuff can all be assigned <br/> pointer _ oldend = _ mylast; <br/> _ mylast = _ umove (_ oldend-_ count, _ oldend, <br/> _ mylast ); // copy suffix <br/> # If _ has_iterator_debugging <br/> _ orphan_range (_ Where. _ myptr, _ mylast); <br/> # endif/* _ has_iterator_debugging */<br/> _ stdext _ unchecked_move_backward (_ vec_iter_base (_ Where ), _ oldend-_ count, <br/> _ oldend); // copy hole <br/> Fill (_ vec_iter_base (_ Where), _ vec_iter_base (_ Where) + _ count, <br/> _ TMP); // insert into hole <br/>}< br/>}

We can focus on _ ty _ TMP = _ Val; // In case _ Val is in sequence
In general, the process of inserting a new element at a specific position is: if capacity is sufficient, copy the content after the position one by one, then copy the content at the corresponding location with _ Val. This process works in many libraries, but the problem arises. If _ Val is originally in this vector, the previous copy operation may damage the _ Val content!

So here we need to copy the _ Val content first before we can move it and then fill in the content:

_ PTR = _ umove (_ myfirst, _ vec_iter_base (_ Where), <br/> _ newvec ); // copy prefix <br/> _ PTR = _ ufill (_ PTR, _ count, _ TMP); // Add new stuff

It may be because of the discovery and modification of bugs, but its robustness makes us feel better and we can't help ourselves. Similarly, we see the _ m_insert_aux function implementation in the SGI-STL:

Template <class _ TP, class _ alloc> <br/> void <br/> vector <_ TP, _ alloc >:: _ m_insert_aux (iterator _ position, const _ TP & _ x) <br/>{< br/> If (_ m_finish! = _ M_end_of_storage) {<br/> construct (_ m_finish, * (_ m_finish-1); <br/> ++ _ m_finish; <br/> _ TP _ x_copy = _ x; <br/> copy_backward (_ Position, _ m_finish-2, _ m_finish-1 ); <br/> * _ Position = _ x_copy; <br/>}< br/> else {<br/> const size_type _ old_size = size (); <br/> const size_type _ Len = _ old_size! = 0? 2 * _ old_size: 1; <br/> iterator _ new_start = _ m_allocate (_ Len); <br/> iterator _ new_finish = _ new_start; <br/> _ stl_try {<br/> _ new_finish = uninitialized_copy (_ m_start, _ Position, _ new_start); <br/> construct (_ new_finish, _ x); <br/> ++ _ new_finish; <br/> _ new_finish = uninitialized_copy (_ Position, _ m_finish, _ new_finish ); <br/>}< br/> _ stl_unwind (destroy (_ new_start ,__ new_finish), <br/> _ m_deallocate (_ new_start, __len); <br/> destroy (begin (), end (); <br/> _ m_deallocate (_ m_start, _ m_end_of_storage-_ m_start ); <br/> _ m_start = _ new_start; <br/> _ m_finish = _ new_finish; <br/> _ m_end_of_storage = _ new_start + _ Len; <br/>}< br/>}

We also saw

_ TP _ x_copy = _ x; <br/> copy_backward (_ Position, _ m_finish-2, _ m_finish-1 ); <br/> * _ Position = _ x_copy;

Similar implementation. We can see that the writing method is exactly the same. I thought the code I wrote was enough to run. However, the gap between excellent codes is still so huge that I am deeply touched. Of course, similar sighs also occur on the famous memmove. We know that there was a memcpy before this function appeared. However, when source and DEST have overlap, this memcpy won't work normally. Why? Similar to the implementation of strycpy, we have seen many of them are * P ++ = * q ++, but we didn't take the moving of the intersection location into account, so we made a correction in memmove, here is a similar implementation:

Void * roken_lib_function <br/> memmove (void * S1, const void * S2, size_t N) <br/>{< br/> char * s = (char *) S2, * D = (char *) S1; <br/> If (D> S) {<br/> S + = n-1; <br/> D + = n-1; <br/> while (n) {<br/> * d -- = * s --; <br/> n --; <br/>}< br/>} else if (d <s) <br/> while (n) {<br/> * D ++ = * s ++; <br/> n --; <br/>}< br/> return S1; <br/>}

Here we will compare S1 and S2, that is, the first address of source and destination, and select the copy Order Based on the different addresses, because we know that there is always a safe way to copy by bit! I hope to record these excellent writing methods to bring my code to a higher level.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.