Efficient stl--non-standard hash container

Source: Internet
Author: User

STL is built on generalization. The array is generalized to a container, and the type of the contained object is parameterized. function generalization is an algorithm that parameterize the type of iterator used. Pointer generalization is an iterator that parameterize the type of object being pointed to. Six components in the STL: containers, Algorithms, iterators, Configurator, adapters, functor.

These six components in the container is divided into sequential containers and related container two categories, just as the STL source code Analysis of the contents of the book. Iterators are the glue between the container and the algorithm, and from an implementation point of view, an iterator is a class that overloads the operator*, Operator->, operator++, operator-, and other pointer-related operations. Template. All STL containers come with their own unique iterators, because only the container designer knows how to traverse their own elements. A functor is a class or class template that overloads the operator (), and a generic function pointer can be considered a narrowly defined functor. An adapter is something that modifies the container or the interface of an imitation function or iterator, such as the queue and stack provided by the STL, although seemingly a container is only a container adapter, because their bottom is fully deque, and all operations are supplied by the underlying deque. Change functor interface, become Funciton adapter, change container interface, become container adapter, change iterator interface person, become iterator adapter.

Several types of iterators are defined within the STL, but when used, iterators can be divided into input, output iterators, forward, bidirectional iterators, random access iterators, depending on how iterators are used.

What STL implements is a conceptual structure built on the basis of generic thinking. This structure, which takes the abstract concept as the main body rather than the actual class as the main body, forms a rigorous interface standard. Under this interface, any component has maximum independence and is glued together with so-called iterators, or mating with a so-called adapter, or a strategy can be dynamically selected with the so-called functor.

For the Space Configurator, the allocator Configurator is used in STD, but the standard Configurator used in the STL is instead used by the SGI Custom Configurator Alloc. Because the allocator configurator in the standard is not very efficient. Because the allocator Configurator simply wraps new and delete in a single layer.

Spatial configuration in STL, Std::alloc

For a normal operation

Classfoo {};

FOO*FP = new Foo;

DELETEFP;

The above process involves a single phase, calling new to allocate memory, and calling Foo () to construct the object. Delete also includes two stages, call ~foo () destructor, call Delete to free memory. For a clear division of labor, the STL will operate the two phases separately. The memory configuration operation is handled by Alloc::allocate (), which is responsible for the memory release operation by Alloc::d eallocate (); The object construction operation is performed by:: Construct (), and the object destructor is handled by::d Estroy (). Configurator defined in <memory>, SGI <memory> contains stl_alloc.h stl_construct.h The former is responsible for the allocation and release of memory space, the latter is responsible for the construction and destruction of objects.

Given the potential for memory fragmentation caused by cell blocks, SGI designed a two-tier Configurator, the first level directly using malloc () and free (), and the second level, depending on the situation to determine what strategy to use, when the configuration chunk exceeds 128KB, directly using the first level configurator; When the configuration block rains 128BK, Use a secondary memory pool collation instead of resorting to the first level configurator. Regardless of whether the first or second level configurator is used, it is encapsulated as a alloc that does not receive parameters, and is then packaged as a simple Simple_alloc class. This class allows the space configurator to have standard interfaces externally. There are only four static functions in this class, which correspond to a pair of functions in Alloc

Template<class T,class alloc>

Class Simple_alloc

{

Public:

statict* Allocate (size_t N)

{return 0 = = n? 0: (t*) Alloc::allocate (n*sizeof (T));}

statict* Allocate (void)

{return (t*) alloc::allocate (sizeof (T));}

Staticvoid deallocate (t* p,size_t N)

{if (0!+ N) Alloc::d eallocate (p,n*sizeof (T));}

Staticvoid deallocate (t* p)

{Alloc::d eallocate (p,sizeof (T));}

};

interior of the four Static The member function corresponds to the function of the implementation in the space Configurator, then has the global construction and the destruction of the number, the combination of these two resources management together .

We just need to know that in the SGI STL there is a dedicated space Configurator, the Space Configurator manages this memory allocation and release, that is, the memory configurator initialized two functions to manage the release and allocation of memory, memory allocation is optimized, first look at the application of memory is large enough, if large, Then directly invoke the first level of the Configurator, that is, using malloc, if not more than 128KB, then use the second level of the Configurator, the second level of the Configurator saved a series of linked lists, from the saved list to find a suitable memory block to meet the requirements, this configurator is not to receive template parameters Alloc , and also made a simple package-simple_alloc, this class is also encapsulated that pair of functions. There is also a pair of global functions that are responsible for the construction and destruction of objects, namely destroy () and construct ().

With the Space Configurator Alloc and the upper package simple_alloc, how do I use it?

Template<class T,CALSS alloc= alloc>

Class Vector

{

Public:

Typedeft Value_type;

Protected:

Typedefsimple_alloc<value_type,alloc> Data_allocator;

};

So far, we know that the space Configurator is alloc, in the container as the default space Configurator, in order to have a better external interface, encapsulated into the Simple_alloc class, which encapsulates the alloc of two important functions, respectively, user and memory allocation and release, At the same time, there are two global functions for object construction and destruction-construct () Destroy ().

In fact, there are three global functions, Uninitialized_copy () Uninitialized_fill () Uninitialized_fill_n (), corresponding to the high-level function copy () Fill () Fill_n (), respectively--- These are the STL algorithms. These three global functions can also be judged by their names.

The above-mentioned uninit* three functions have all the techniques of generalization and specialization, in order to deal with them more efficiently. The three functions in the STL are used in the Generalization branch.

Summary: In the space configuration, to understand that SLT uses its own defined Alloc configurator, this configurator is divided into two levels, provides the memory of the release and allocation of work, then define five global functions, two global is used for object construction and destruction, three global is more efficient initialization memory exists. In order to better use the Alloc. The Simple_alloc class is encapsulated externally, and this class is used in each container.

Efficient stl--non-standard hash container

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.