2 pqueue.hh -- declare PQueue_ent and PQueue
4 source file of the Flower Library
6 (c) 1997 Han-Wen Nienhuys <hanwen@stack.nl>
15 template<class K, class T>
22 template<class K, class T>
23 int compare (PQueue_ent<K,T> const &e1 , PQueue_ent<K,T> const &e2) {
24 return compare(e1.key , e2.key);
28 Priority queue using a (variable size) in-situ heap.
37 return heap_arr_[i-1];
39 T const&elt(int i) const {
40 return heap_arr_[i-1];
43 /** acces an heap element. Careful with this, as changing the
44 priority might fuck up the invariants
46 @param 1 <= i < size() */
47 T& operator[](int i) { return heap_arr_[i]; }
48 T operator[](int i) const { return heap_arr_[i]; }
52 for (int i =2; i <= size(); i++)
53 assert(compare (elt(i/2), elt(i)) <= 0);
56 T front () const { return elt(1); }
57 int size() const { return heap_arr_.size(); }
60 int i = heap_arr_.size();
63 if (compare(elt(j), v) > 0) {
75 int first_leaf_i = size();
81 T last = heap_arr_.top();
86 while ( mini < size() ) {
87 if (compare(elt(mini + 1), elt(mini)) <0)
89 if (compare(last,elt(mini) ) < 0)
91 elt(lasti) = elt(mini);