Stl source code analysis-priority queue

Source: Internet
Author: User

Stl source code analysis-priority queue
The previous two articles introduced the source code implementation of vector and list in gcc4.8. This is the two most commonly used sequence containers in stl. In addition to containers, stl also provides a component called an adapter that uses containers for special operations, such as stack, queue, and priority queue, this article introduces the source code implementation of the priority queue of gcc4.8. As the name suggests, priority queue is a queue with a priority. Therefore, elements must provide the <operator. Unlike vector and list, priority queue allows elements to be added, but only elements with the highest priority can be retrieved. 1. priority queue defines that priority queue does not have a base class to copy the Code template <typename _ Tp, typename _ Sequence = vector <_ Tp>, typename _ Compare = less <typename _ Sequence :: value_type> class Sequence {public: typedef typename _ Sequence: value_type; typedef typename _ Sequence: reference; typedef typename _ Sequence: const_reference; typedef typename _ Sequence:: size_type; t Ypedef _ Sequence container_type; protected: _ Sequence c; _ Compare comp ;...... The replication code priority queue uses vector by default at the underlying layer, which contains two members. vector c stores data, and comp is a function to compare the data size. Ii. priority queue Construction Method: You can use vector to directly initialize priority queue, or you can initialize any iterator or array pointer. Copy the code explicit priority_queue (const _ Compare & _ x, const _ Sequence & _ s): c (_ s), comp (_ x) {std :: make_heap (c. begin (), c. end (), comp);} explicit priority_queue (const _ Compare & _ x = _ Compare (), _ Sequence & _ s = _ Sequence ()): c (std: move (_ s), comp (_ x) {std: make_heap (c. begin (), c. end (), comp);} template <typename _ InputIterator> priority_queue (_ InputIterator _ first, _ InputIterator _ last, c Onst _ Compare & _ x, const _ Sequence & _ s): c (_ s), comp (_ x) {_ glibcxx_requires_valid_range (_ first, _ last); c. insert (c. end (), _ first, _ last); std: make_heap (c. begin (), c. end (), comp);} template <typename _ InputIterator> priority_queue (_ InputIterator _ first, _ InputIterator _ last, const _ Compare & _ x = _ Compare (), _ Sequence & _ s = _ Sequence (): c (std: move (_ s )), comp (_ x) {_ glibcxx_req Uires_valid_range (_ first, _ last); c. insert (c. end (), _ first, _ last); std: make_heap (c. begin (), c. end (), comp);} After copying the code to insert all elements into the priority queue, use make_heap to build the largest heap, copy the Code template <typename _ iterator> void make_heap (_ RandomAccessIterator _ first, _ random _ last) {typedef typename iterator_traits <_ RandomAccessIterator>: value_type _ ValueType; typedef typename iterator_traits <_ RandomAccessIterator >:difference_type _ DistanceType; if (_ last-_ first <2) return; const _ DistanceType _ len = _ last-_ first; _ DistanceType _ parent = (_ len-2)/2; while (true) {_ ValueType _ value = _ GLIBCXX_MOVE (* (_ first + _ parent); std: :__ adjust_heap (_ first, _ parent, _ len, _ GLIBCXX_MOVE (_ value); if (_ parent = 0) return; _ parent -- ;}} copying code _ adjust_heap is a next trace process, first from the last non-leaf node The sub-tree with the root node is the largest heap. Copy the Code template <typename _ RandomAccessIterator, typename _ Distance, typename _ Tp, typename _ Compare> void _ adjust_heap (_ RandomAccessIterator _ first, _ Distance _ holeIndex, _ Distance _ len, _ Tp _ value, _ Compare _ comp) {const _ Distance _ topIndex = _ holeIndex; _ Distance _ secondChild = _ holeIndex; while (_ secondChild <(_ len-1)/2) {_ secondChild = 2 * (_ secondChild + 1 ); if (_ comp (* (_ f Irst + _ secondChild), * (_ first + (_ secondChild-1) _ secondChild --; * (_ first + _ holeIndex) = _ GLIBCXX_MOVE (* (_ first + _ secondChild); _ holeIndex = _ secondChild;} if (_ len & 1) = 0 & _ secondChild = (_ len-2)/2) {_ secondChild = 2 * (_ secondChild + 1 ); * (_ first + _ holeIndex) = _ GLIBCXX_MOVE (* (_ first + (_ secondChild-1 ))); _ holeIndex = _ secondChild-1;} std: :__ push _ Heap (_ first, _ holeIndex, _ topIndex, _ GLIBCXX_MOVE (_ value), _ comp );} copy Code 3, priority queue's element operation priority queue only has two main operations: push and pop. Add new elements to push, void push (const value_type & _ x) {c. push_back (_ x); std: push_heap (c. begin (), c. end (), comp);} is placed in the last position, and then push_heap is used to perform an upstream operation. The inserted element is moved to the proper position to ensure that the entire queue is still the largest heap. Copy the template <typename _ RandomAccessIterator, typename _ Distance, typename _ Tp> void _ push_heap (_ RandomAccessIterator _ first, _ Distance _ holeIndex, _ Distance _ topIndex, _ Tp _ value) {_ Distance _ parent = (_ holeIndex-1)/2; while (_ holeIndex> _ topIndex & * (_ first + _ parent) <_ value) {* (_ first + _ holeIndex) = _ GLIBCXX_MOVE (* (_ first + _ parent); _ holeIndex = _ parent; _ parent = (_ HoleIndex-1)/2;} * (_ first + _ holeIndex) = _ GLIBCXX_MOVE (_ value);} copy the code pop operation to remove the heap top element, void pop () {std: pop_heap (c. begin (), c. end (), comp); c. pop_back ();} Because vector is used, it will be very costly to remove the first element and then make_heap. Here, we will first swap the first element with the last element, delete the last element, and then perform a tracing process from the first element to create a new largest heap.

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.