Google's Chromium browser source learning--base Public General Library (iii)

Source: Internet
Author: User

This section describes the containers in the base common common library, which contains stacks, lists, collections, and most recently used caches (most recently using cache templates).

Linked_list.h: A simple list type, through the template implementation, the internal use of double-linked list form, there is a difference from the C + + Standard Template Library Std::list<t*>, it is used in the way of:base::linkedlist<t>;

Relative Std::list<t*>, the advantages are:

1. Delete an element, the operation complexity is O (1), and Std::list<t*> is O (n), because of its internal need to get a t* element of the iterator;

2. Inserting an element does not require a heap allocator.

The template linked list has its built-in node:base::linknode<t>; linked list:base::linkedlist<t>

Node Example:

Class Mynodetype:public Base::linknode<mynodetype>

{

};

Create a list of examples:linkedlist<mynodetype> lists;

Specific Use examples:

1 classNode: PublicLinknode<node>2 {3   Public:4 5   ExplicitNode (intID): id_ (ID) {}6 7   intID ()Const{returnid_;}8 9  Private:Ten  One   intid_; A};

  

1Linkedlist<node>list;2Node N1 (1);3Node N2 (2);4Node N3 (3);5Node N4 (4);6Node N5 (5);7List. Append (&N1);8List. Append (&n2);9List. Append (&n3);TenList. Append (&N4); OneList. Append (&N5); A  - N3. RemoveFromList (); -  theList. Append (&n3); -N3. InsertBefore (&n2); -N4. InsertAfter (&N1); -  +  for(Constlinknode<node>* Node = List.head (); Node! = List.end (); node = node->next ()) -  { +printf"id =%d,", Node->value ()ID ()); A   } at  -  for(Constlinknode<node>* Node = List.Tail (); Node! = List.end (); node = node->Previous ()) -  { -printf"id =%d,", Node->value ()ID ()); -}

The above code contains the node custom type, List object definition, creation, node append, node removal, node append, node insertion, movement, forward traversal, and back traversal;
When the 12th line is executed, the contents of the list are: 1,2,3,4,5; After executing 13 lines: 1,2,4,5; After executing 15 lines: 1,2,4,5,3; after executing 16 lines: 1,3,2,4,5 (no execution of the line); After executing 17 lines: 1,4,3,2,5;

Forward traversal: id = 1,id = 4,id = 3,id = 2,id = 5; back traverse: id = 5,id = 2,id = 3,id = 4,id = 1.;

Seems to be in fact inconsistent with the expected results, please note: the 15th line, 16 lines will actually introduce a bug, causing the traversal to produce an infinite loop, when used to avoid the same element address is written to the list of cases;

Summary Linked list:

The node linknode<t> contains linknode<t>* previous_ and linknode<t>* next_ pointer members, respectively saving the previous and subsequent element addresses to the current element, base:: The linkedlist<t> contains member Linknode<t> Root_, which is the root node of the entire list, as well as the last node in the Traverse, maintains the linked list, and appends the elements to the linked list in the form of a back interpolation.

Basically the implementation is relatively simple, the relative std::list is faster and based on the element address, but may also introduce the above bug because of the cautious operation.

Stack_container.h: Allocator Stackallocator, which inherits from std::allocator<t>; its internal maintenance of an original application heap buffer object Stackallocator::source, Use the allocator to back up the storage content, reduce the need to request again in the heap, or other operations, the other use of the allocator can share the same storage space; You can see that Stackallocator::source uses base:: Alignedmemory the original stack buffer to which the alignment is allocated Stack_buffer_ and an identity used_stack_buffer_ that holds whether the current buffer is being used;

Allocator Stackallocator, containing rebind of the type that can be re-specified, the copy constructor of the shared storage buffer, the explicit constructor, the allocate of the application space, the buffer can be allocated directly when the source is not used otherwise, or directly through the std: : Allocator<t>::allocate is allocated for it, and the same deallocate, when Source is released as a buffer, is released directly (actually simply Used_stack_buffer_ = False), Otherwise, it is considered to be allocated by std::allocator<t>::allocate, and the buffer is freed by Std::allocator<t>::d eallocate.

  

              

Google's Chromium browser source learning--base Public General Library (iii)

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.