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