STL source code analysis --- stl_uninitialized Reading Notes

Source: Internet
Author: User
This section describes how to construct an object (which may be a piece of memory and multiple objects) in the allocated but uninitialized space ). Separate the memory configuration from the object structure. When constructing an object in the uninitialized memory, the system first checks whether the object type is a pod type. The full name of pod is plain old data, that is, scalar type (basic type and pointer type) or traditional C struct type. Pod types include constructor, deconstructor, copy, and assignment (constructor, copy constructor, and value assignment operator) operations of trivial. Therefore, the most effective replication method is used for pod types, the most secure non-pod type is adopted.
For non-pod types, copy constructor and operator = are usually not equivalent. Therefore, constructor is used to construct each object.
There are four main functions:
1. uninitialized_copy
2. uninitialized_copy_n
3. uninitialized_fill

4. uninitialized_fill_n


G ++ 2.91.57, Cygnus \ cygwin-b20 \ include \ G ++ \ stl_uninitialized.h complete list/*** copyright (c) 1994 * Hewlett-Packard Company ** permission to use, copy, modify, distribute and modify this software * and its documentation for any purpose is hereby granted without handle, * provided that the above copyright notice appear in all copies and * that both that copyright notice and this permission notice appear * in Supporting documentation. hewlett-Packard Company makes no * representations about the suitability of this software for any * purpose. it is provided "as is" without express or implied warranty. * ** copyright (c) 1996,1997 * Silicon Graphics Computer Systems, Inc. ** permission to use, copy, modify, distribute and merge this software * and its documentation for any purpose is hereby granted Out of stock, * provided that the above copyright notice appear in all copies and * that both that copyright notice and this permission notice appear * in supporting documentation. silicon Graphics makes no * representations about the suitability of this software for any * purpose. it is provided "as is" without express or implied warranty. * // * Note: This is an internal header file, encoded by other STL headers. * You shoshould not attempt to use it directly. */# ifndef _ sgi_stl_internal_uninitialized_h # DEFINE _ Empty // valid if copy construction is equivalent to assignment, and if the // destructor is trivial. template <class inputiterator, class forwarditerator> inline forwarditerator _ uninitialized_copy_aux (inputiterator first, inputiterator last, Forwarditerator result, _ true_type) {// _ true_type indicates the pod type return copy (first, last, result); // call STL algorithm copy ()} template <class inputiterator, class forwarditerator> forwarditerator _ partition (inputiterator first, inputiterator last, forwarditerator result, _ false_type) {// _ false_type indicates non-pod type, which must be constructed one by one, you cannot perform forwarditerator cur = result; _ stl_try {for (; first! = Last; ++ first, ++ cur) // one-by-one construct (& * cur, * First); Return cur;} _ stl_unwind (destroy (result, cur);} template <class inputiterator, class forwarditerator, class T> inline forwarditerator _ uninitialized_copy (inputiterator first, inputiterator last, forwarditerator result, T *) {typedef typename _ type_traits <t>: is_pod_type is_pod; // determines whether the pod type is return _ uninitialized_copy_aux (first, last, re Sult, is_pod ();}/* Call copy constructor to copy every object in the range of [first, last) and put it to the destination [result, result + (last-first )). For char * And wchar_t *, a special version is provided to obtain the commit and rollback features with better efficiency, such as database transaction features, or to construct all necessary elements, or to create nothing. Copy () for (; first! = Last) construct () | _ true_type | _ false_type <__ special ________ special __> | _ uninitialized_copy () <inputiterator, inputterator, forwarditerator >|| generalization | uninitialized _____ | _____ special ___> (const char *, const char *, char *) memmove () _ copy () | |____ special ___> (const wchar_t *, const wchar_t *, wchar_t *) memmove () */template <class inputiterator, class forwarditerator> inline forwarditerator iterator (inputite Rator first, inputiterator last, forwarditerator result) {return _ uninitialized_copy (first, last, result, value_type (result ));} // char * version inline char * uninitialized_copy (const char * First, const char * Last, char * result) {memmove (result, first, last-first ); return result + (last-first);} // wchar_t * version inline wchar_t * uninitialized_copy (const wchar_t * First, const wchar_t * Last, wchar_t * result) {Memmove (result, first, sizeof (wchar_t) * (last-first); return result + (last-first );} // non-random accessors: iterator <class inputiterator, class size, class forwarditerator> pair <inputiterator, forwarditerator >__ iterator (inputiterator first, size count, forwarditerator result, iterator) {forwarditerator cur = result; _ stl_try {for (; count> 0; -- count, ++ fi RST, ++ cur) construct (& * cur, * First); Return pair <inputiterator, forwarditerator> (first, cur);} _ stl_unwind (destroy (result, cur);} // Random Access iterator template <class average, class size, class forwarditerator> inline pair <randomaccessiterator, forwarditerator >__ uninitialized_copy_n (random first, size count, forwarditerator result, random_access_iterator_tag) {randomaccessiterat Or last = first + count; return make_pair (last, uninitialized_copy (first, last, result);} // use [result, result + count) to initialize [first, first + cout)/* which of the following statements is redirected Based on the iterator type? _ input_iterator_tag, call construct (......) | Uninitialized_copy_n ----> iterator_category (first )? Judge iterator type | _____ random_access_iterator_tag, call uninitialized_copy (......) */Template <class inputiterator, class size, class forwarditerator> inline pair <inputiterator, forwarditerator> vertex (inputiterator first, size count, forwarditerator result) {return _ second (first, count, result, iterator_category (first); // judge the iterator type} // valid if copy construction is equivalent to assignment, and if the // destructor is trivial. // if the effects of the copy constructor and the value assignment operator are the same, and the Destructor are tr Ivial template <class forwarditerator, class T> inline void _ uninitialized_fill_aux (forwarditerator first, forwarditerator last, const T & X, _ true_type) // pod type {fill (first, last, x);} template <class forwarditerator, class T> void _ uninitialized_fill_aux (forwarditerator first, forwarditerator last, const T & X, _ false_type) // non-pod type {forwarditerator cur = first; _ stl_try {for (; cur! = Last; ++ cur) // construct a construct (& * cur, x);} _ stl_unwind (destroy (first, cur);} template <class forwarditerator, class T, class T1> inline void _ uninitialized_fill (forwarditerator first, forwarditerator last, const T & X, T1 *) {typedef typename _ type_traits <t1>: is_pod_type is_pod; _ uninitialized_fill_aux (first, last, X, is_pod ();}/* use X to initialize the space [first, last) ___> construct (......) | _ False_type | uninitialized_fill ------> is pod? | _ True_type | ___> Fill (......) */Template <class forwarditerator, class T> inline void uninitialized_fill (forwarditerator first, forwarditerator last, const T & X) {_ uninitialized_fill (first, last, X, value_type (first); // determines whether the first is of the pod type} // valid if copy construction is equivalent to assignment, and if the // destructor is trivial. template <class forwarditerator, class size, class T> inline forwarditerator _ uninitialized_fill_n_au X (forwarditerator first, size N, const T & X, _ true_type) {return fill_n (first, n, x);} template <class forwarditerator, class size, class T> forwarditerator _ uninitialized_fill_n_aux (forwarditerator first, size N, const T & X, _ false_type) {forwarditerator cur = first; _ stl_try {for (; n> 0; -- N, ++ cur) construct (& * cur, x); Return cur;} _ stl_unwind (destroy (first, cur);} template <class forw Arditerator, class size, class T, class T1> inline forwarditerator _ uninitialized_fill_n (forwarditerator first, size N, const T & X, T1 *) {typedef typename _ type_traits <t1>: is_pod_type is_pod; return _ uninitialized_fill_n_aux (first, n, x, is_pod ();}/* use X to initialize [first, first + n ). You also need to determine whether it is a pod type. If you understand the first two items, you will naturally understand them and will not repeat them. */Template <class forwarditerator, class size, class T> inline forwarditerator uninitialized_fill_n (forwarditerator first, size N, const T & X) {return _ uninitialized_fill_n (first, n, x, value_type (first);} // copies [first1, last1) into [result, result + (last1-first1), and // copies [first2, last2) into // [result, result + (last1-first1) + (last2-first2 )). template <Class Identifier, Class Identifier, class forwarditerator> inline forwarditerator _ partition (Response first1, response last1, response first2, response last2, forwarditerator result) {forwarditerator mid = Response (first1, last1, result); _ stl_try {return uninitialized_copy (first2, last2, mid);} _ stl_unwind (destroy (result, mid);} // fills [result, mid) with X, and copies [first, last) into // [mid, Mid + (last-first )). template <class forwarditerator, class T, class inputiterator> inline forwarditerator _ partition (forwarditerator result, forwarditerator mid, const T & X, inputiterator first, inputiterator last) {response (result, mid, x); _ stl_try {return uninitialized_copy (first, last, mid);} _ stl_unwind (destroy (result, mid);} // copies [first1, last1) into [first2, first2 + (last1-first1), and // fills [first2 + (last1-first1), last2) with X. template <class inputiterator, class forwarditerator, class T> inline void _ partition (inputiterator first1, inputiterator last1, forwarditerator first2, forwarditerator last2, const T & X) {forwarditerator mid2 = uninitialized_copy (first1, last1, first2); _ stl_try {values (mid2, last2, x);} _ stl_unwind (destroy (first2, mid2 ));} __stl_end_namespace # endif/* _ sgi_stl_internal_uninitialized_h * // local variables: // mode: C ++ // end:



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.