SGI-STL abbreviated (VI) - sequence containers (vector)

stl_vector.h :
    vector: the container may be a random access sequence elements, after completion of the insertion or deletion of the constant time to complete within the first portion or from the intermediate linear time is required;
    
    _Vector_alloc_base: vector distribution base class template; template parameter data type are T, dispenser type _Allocator, and a bool identification _IsStatic (standard for distinguishing whether the dispenser or dispenser SGI);
    Data members:
        _M_data_allocator: allocator object;
        _M_start: Save request first address buffer (equivalent to the first address of the container element);
        _M_finish: saving the contents of the container at the end of the address length;
        _M_end_of_storage: Save the application end of the buffer address;
    Member function:
        Constructor: allocator_type type dispenser to initialize the reference _M_data_allocator;
        get_allocator: Gets the allocator object _M_data_allocator;
        _M_allocate: _M_data_allocator allocation size objects through the distributor is n type size memory elements;
        _M_deallocate: releases the specified data element type pointer address data elements of size n type size of memory space;
    It also offers specialized version _Vector_alloc_base <_TP, _Allocator, to true > , the interior of the dispensing base class template objects no longer used dispenser, but directly using the static member functions are allocated simple_alloc management;
    
    _Vector_base: vector base class inherits _Vector_alloc_base, template parameter _IsStatic its base class is initialized by extraction _S_instanceless _Alloc_traits obtained;
        Constructors: overloaded version A dispenser initializing the object type; the other n parameters further increased, the pre-allocated internal call _M_allocate n bytes in the buffer and adjusts _M_start, _M_finish, _M_end_of_storage value;
        Destructor: call _M_deallocate release the current buffer;
    
    vector: vector template sequence container class inherits protection _Vector_base, the template parameters dispenser used as a default macro __STL_DEFAULT_ALLOCATOR dispenser; may be set by adjusting the macro, and therefore can use the allocator <T> or alloc
        (malloc_alloc (i.e. __malloc_alloc_template < 0 >) or __default_alloc_template <__ NODE_ALLOCATOR_THREADS, 0 > ) as the default memory allocator;
        Further statements of a conventional type and iteratively re-type and the constant iterator type (vector iterator type pointer of the same type, different from other containers (such as: list, deque, etc.), the respective other container requires a separate iterator);
        get_allocator: function overriding the base class, the base class acquire the object dispenser;
        Constructors: more overloaded version constructor, comprising providing distribution allocator_type parameters, an initialization element number n and a given value or initialization value used to initialize the default values, to provide end-address data element,
        (Internal call uninitialized_fill_n fill the buffer element) and __M_finish adjustment value;
        Copy constructors: ditto initialization process;
        Destructor: internal call destroy destroyed (not release) the specified range _M_start, _M_finish memory element;
        begin: get the first part iterator (return _M_start);
        end: Get iterator tail (return _M_finish);
        rbegin, rend: initialization means configured to begin and end and returns inverting iterator object;
        size: Get vector capacity, i.e. the iterator range (End () - the begin ());
        max_size: Get element capacity can accommodate the largest size (size_type ( - . 1 ) / the sizeof (T));
        capacity: Get currently receiving element capacity (size_type (_M_end_of_storage - the begin ())) (actually is the current container have been allocated memory space);
        empty: determining whether the container is empty (the begin () == End ());
         operator []: Returns a reference element at a specified index (* (the begin () + n-));
        at: function with operator [], it will be an indirect call operator [], but increased border checks, exceeding the capacity of the container is thrown range_error an exception;
        _M_allocate_and_copy: Application n bytes memory space and the size of the original copy of the data element iterator range to the space, the application returns the first address memory (iterator);
        reserve: Reserve at least greater than n bytes of memory space can accommodate the size of the current element volume (or less if the capacity does nothing, or the call _M_allocate_and_copy weight for memory space, after the destruction of the original container elements and memory space and readjusted
        Iterator container and the capacity of the memory space a pointer _M_end_of_storage position of the new application), so that the operation may result in substantial destruction and reapply the memory;
        assign: allocation function, two overloaded versions, one version for the parameter n size_type, const T & val val represents a distribution of n values of the container element (filled _M_fill_assign call distribution), another version of the input parameter to a range iterator (call _M_assign_dispatch
        distribution);
        _M_fill_assign: n elements is filled val dispensing operation (if n is greater than memory capacity is required to re-apply, but which is achieved by re-applying elements n vector of the object to val by direct exchange swap (in fact simply exchanged iterator pointer object only)
        ; If n is greater than and less than the capacity size, fill the call directly filled, the rear-end remaining iterators uninitialized_fill_n initialization value and to adjust _M_finish tail iterator; if n is less than the size, then the first call fill_n filled iterator n elements, call to erase destruction of surplus
        Memory space);
        _M_assign_dispatch: allocation assignment template, if the iterator corresponding element of type integer _Is_integer the _M_assign_dispatch template parameter calls for the release of __true_type (internal call _M_fill_assign filling distribution); otherwise the call _M_assign_aux allocation, the fact that the
        Overload function also has two versions, for the input iterator or subclass and forward iterator type, is so, because the latter iterator implementations may achieve a faster manner rather than input iterator (of course using the latter the former may be the way to achieve, but for traversing the length of the acquisition is relatively slow,
        Is lost (can be obtained directly when the subtracted random iteration obtains length) efficiency) is as iterated upon the like;
        _M_assign_aux: input iterator type version, by an iterative position of the container element iterator parameter passing directly overwrite the existing container element, when the input parameters have iterator element is less than the container, the removal of the primary container redundant elements; if the input parameters there are extra iterator is called insert insert
        Remaining elements into a vessel;
        _M_assign_aux: Forward iterator type version, before the first call len distance to get an iterator range, if len is greater than the capacity you need to re-apply for memory space, but its implementation is to re-apply through n elements vector objects to val and through swap direct exchange (in fact simply exchanged iterator pointer object only)
        ; If len is less than the capacity and size greater than, the iterator is called advance acquired when mid size before hopping to the first forward iterator then calls the first copy and the copy iterator mid range data to the original container, after which call initialization uninitialized_copy iterator after the remaining values ​​and the tail end of the adjustment _M_finish iterator;
        If the size is less than len, is called copy copy of the first data to the last iteration to the original container, and then calls the destroy excess destroy remaining data elements in the original container and adjust _M_finish;
        front, back: get the container first element and the last element ( * the begin (), * (End () - 1 ), if the container is empty will appear abnormal operation);
        push_back: end of the container is pushed into data elements, and sufficient buffer is called the tail of the directly before the iterator _M_finish if pushed construct and adjust _M_finish configured to initialize, or the call before the end of the data _M_insert_aux insert element iterator and adjust _M_finish;
        _M_insert_aux: Insert before the specified data element x in iterator; insert if there is enough buffer before the end of the adjustment iterator + 1 and after calls to copy data copy_backward element, after the insertion position can directly overwrite the original data element; if less We need to apply enough memory space, its strategy: if the original container capacity is 0,
        Application to a spatial element, otherwise the application 2 times the memory space of the original space; uninitialized_copy copy of the original container then calls the iterator iterator range led to the insertion point iterator data elements to a new memory space, after which the new memory space construct inserted element call to initialize the iterator at the end, and then the rest of the original container
        The remaining portion of the data elements are sequentially copied to the back uninitialized_copy call, last call destroy, _M_deallocate release of memory space and all the elements of the container and adjust the original container iterator for the new memory address range and capacity can;
        swap: exchange two objects vector container (internal fact simply exchanged iterator pointer only);
        insert: providing three overloaded versions;
            Inserted before specified data element x iterator and returns an iterator when the insertion position:
                If there is enough buffer prior to insertion and prior to the end of the iterative construct inserted directly calls the initialization value at the end of the iterator _M_finish x and adjust to _M_finish; otherwise the call _M_insert_aux achieve specific insertion operation;
            Before the specified element is inserted all the iterator iterator range:
                It calls _M_insert_dispatch and determine whether the integer parameter _Is_integer according to the data element types, if the integer then call _M_fill_insert insert padding, or call _M_range_insert insert, however _M_range_insert as a template function,
                The data element which is _M_range_insert iterator type, when the input iterator is input iterators to traverse the loop call insert data is inserted (in the traversal to be adjusted during insertion of the insertion point iterator position), when the former is to call the iterator specific when the strategy adopted
                : First call len distance before getting to the iterator range, this time the situation is less than the remaining container available memory may appear;
                    1 . If len is less than the remaining available memory is divided into two cases (the reason why there are two cases in order to improve the efficiency of insertion (in particular data element when the built-in type integer, etc.) as much as possible);
                         1 ) the insertion point. to iterator tail length greater than the insert iterator range:
                            Uninitialized_copy call when the front end of the copy constructor iterator n elements to the end of the range of the data and adjust the position of the container iterator iterator tail position;
                            After calling copy_backward after the insertion of the element range of the iterator to insert iterator is not copied to the copy of the original at the end iterator;
                            Calling the copy element copies the input iterator range at the insertion point;
                        2 .) The insertion point to the end than the insertion length iterator iterator large scale:
                            Iterator to obtain the end point of the insertion length of the scope of the iterator __elems_after;
                            Acquiring location mid call advance when the first input iterator iterator __elems_after rearward position;
                            Uninitialized_copy copy constructor to call mid input iterator iterator range data to the tail end of the container and adjust the position of the container iterator iterator tail position;
                            Uninitialized_copy copy constructor call iterator insertion point to the end of the original container to the range data iterator iterator position a new container and container end of the iterative adjustment position;
                            Calling the copy element copies the input iterator range at the insertion point;
                    2 . Insufficient remaining memory available:
                        Get the size of the original container and the scope of the iterator plus maximum length in the original container takes the input buffer size as the size of the new container application will request a new buffer;
                        Uninitialized_copy copy constructor call first container iterator iterator to the insertion point to the new range of the data buffer and adjust the first record at the iterator iterator tail;
                        Uninitialized_copy copy constructor call input iterator range data to the new end of the buffer and adjusts the iterator at the new end of the buffer iterator;
                        Copy constructor call uninitialized_copy original container insertion point iterator to the original end of the vessel to a new range data iterator iterator at the end of the buffer;
                        Call to destroy, _M_deallocate destroy the original container elements and memory space, call each container iterator to the corresponding iterator new buffer;
            X n data elements inserted before specified iterator:
                Call _M_fill_insert filling insertion strategy with the former _M_range_insert version of the iterator type;
        pop_back: pop end of the container element (forward movement and the end of the call to destroy iterator destroy trailing elements);
        erase: two overloaded versions, removes the specified location iterator element version (personally feel that there is BUG, ​​passed in iterator's position is not a phenomenon before pop_back otherwise there will be more than the end of the iterator) call to remove the copy directly to the point iterator after the end of the iterator to the iterator copied to a point at the front removed;
            Forward movement of the end of the iterator and calls are returned to remove the tail destroy destroy iterator points; similarly removed iterator range version;
        resize: reset the size of the container, providing two overloaded version; version with initialization parameter when the demand is less than the container size new_size size original container, the first container is removed after the call earse iterator new_size start to the end iterator iterator range data elements and adjusts the position of the end of the iteration;
                Otherwise call insert inserted data element value x is less than the size of the original container new_size iterator at the end of the container;
                Without initialization parameter version of the data element type to call the default constructor resize parameterized version parameter value;
        clear: remove the container calling erase data elements;
        Further overloaded plurality of different comparison operators to support the comparison operation;
        Overload assignment operator operator = : assignment to support, also used to accelerate the internal operation as efficient as possible;
    When the vector with the use of a version of the default memory allocator pool, if more than 128 bytes will direct the use of malloc, free memory management, the next vector memory space requirements in general relatively large memory pool significance will become less, especially the operation of certain vector member function is even more frequent application,
    Destruction of memory space, so the significance of the main thread pool to reduce memory fragmentation and frequent application features a small memory space; of course, if the user to provide custom memory allocator (memory pool) can also self-management strategies; because in addition to the actual allocation vector memory is continuous, it can be taken directly or iterator
    Fetching pointer or operator [] prefetch address is safe;

 

Guess you like

Origin www.cnblogs.com/haomiao/p/11647245.html