LLVM API Documentation

PriorityQueue.h
Go to the documentation of this file.
00001 //===- llvm/ADT/PriorityQueue.h - Priority queues ---------------*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file defines the PriorityQueue class.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #ifndef LLVM_ADT_PRIORITYQUEUE_H
00015 #define LLVM_ADT_PRIORITYQUEUE_H
00016 
00017 #include <algorithm>
00018 #include <queue>
00019 
00020 namespace llvm {
00021 
00022 /// PriorityQueue - This class behaves like std::priority_queue and
00023 /// provides a few additional convenience functions.
00024 ///
00025 template<class T,
00026          class Sequence = std::vector<T>,
00027          class Compare = std::less<typename Sequence::value_type> >
00028 class PriorityQueue : public std::priority_queue<T, Sequence, Compare> {
00029 public:
00030   explicit PriorityQueue(const Compare &compare = Compare(),
00031                          const Sequence &sequence = Sequence())
00032     : std::priority_queue<T, Sequence, Compare>(compare, sequence)
00033   {}
00034 
00035   template<class Iterator>
00036   PriorityQueue(Iterator begin, Iterator end,
00037                 const Compare &compare = Compare(),
00038                 const Sequence &sequence = Sequence())
00039     : std::priority_queue<T, Sequence, Compare>(begin, end, compare, sequence)
00040   {}
00041 
00042   /// erase_one - Erase one element from the queue, regardless of its
00043   /// position. This operation performs a linear search to find an element
00044   /// equal to t, but then uses all logarithmic-time algorithms to do
00045   /// the erase operation.
00046   ///
00047   void erase_one(const T &t) {
00048     // Linear-search to find the element.
00049     typename Sequence::size_type i =
00050       std::find(this->c.begin(), this->c.end(), t) - this->c.begin();
00051 
00052     // Logarithmic-time heap bubble-up.
00053     while (i != 0) {
00054       typename Sequence::size_type parent = (i - 1) / 2;
00055       this->c[i] = this->c[parent];
00056       i = parent;
00057     }
00058 
00059     // The element we want to remove is now at the root, so we can use
00060     // priority_queue's plain pop to remove it.
00061     this->pop();
00062   }
00063 
00064   /// reheapify - If an element in the queue has changed in a way that
00065   /// affects its standing in the comparison function, the queue's
00066   /// internal state becomes invalid. Calling reheapify() resets the
00067   /// queue's state, making it valid again. This operation has time
00068   /// complexity proportional to the number of elements in the queue,
00069   /// so don't plan to use it a lot.
00070   ///
00071   void reheapify() {
00072     std::make_heap(this->c.begin(), this->c.end(), this->comp);
00073   }
00074 
00075   /// clear - Erase all elements from the queue.
00076   ///
00077   void clear() {
00078     this->c.clear();
00079   }
00080 };
00081 
00082 } // End llvm namespace
00083 
00084 #endif