6 * $Date: 2012-07-06 15:04:28 +0200 (Fr, 06. Jul 2012) $
7 ***************************************************************/
10 * \brief Declaration of class PQueue (priority queue).
12 * \author Stefan Hachul
15 * This file is part of the Open Graph Drawing Framework (OGDF).
19 * See README.txt in the root directory of the OGDF installation for details.
22 * This program is free software; you can redistribute it and/or
23 * modify it under the terms of the GNU General Public License
24 * Version 2 or 3 as published by the Free Software Foundation;
25 * see the file LICENSE.txt included in the packaging of this file
29 * This program is distributed in the hope that it will be useful,
30 * but WITHOUT ANY WARRANTY; without even the implied warranty of
31 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
32 * GNU General Public License for more details.
35 * You should have received a copy of the GNU General Public
36 * License along with this program; if not, write to the Free
37 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
38 * Boston, MA 02110-1301, USA.
40 * \see http://www.gnu.org/copyleft/gpl.html
41 ***************************************************************/
51 #include <ogdf/basic/List.h>
55 //Needed for storing entries of the heap.
59 HelpRecord() { } //constructor
60 ~HelpRecord(){ } //destructor
62 void set_ListIterator(ListIterator
<PackingRowInfo
>& it
) { iterator
= it
; }
63 void set_value (double v
) { value
= v
; }
64 ListIterator
<PackingRowInfo
> get_ListIterator() const { return iterator
; }
65 double get_value() const { return value
; }
69 ListIterator
<PackingRowInfo
> iterator
;
74 //Helping data structure that is a priority queue (heap) and holds double values
75 //(as comparison values) and iterators of type ListIterator<PackingRowInfo>
76 //as contents. It is needed in class MAARPacking for the Best_Fit insert strategy.
80 PQueue() { P
.clear(); } //constructor
81 ~PQueue(){ } //destructor
83 //Inserts content with value value into the priority queue and restores the heap.
84 void insert(double value
, ListIterator
<PackingRowInfo
> iterator
)
88 h
.set_ListIterator(iterator
);
91 reheap_bottom_up(P
.size()-1);
94 //Deletes the element with the minimum value from the queue and restores
99 cout
<<"Error PQueue:: del_min() ; Heap is empty"<<endl
;
102 //last element becomes first element
106 P
.pushFront(P
.back());
114 //Returns the content with the minimum value.
115 ListIterator
<PackingRowInfo
> find_min()
117 OGDF_ASSERT(P
.size() >= 1);
119 // cout<<"Error PQueue:: find_min() ; Heap is empty"<<endl;
121 return P
.front().get_ListIterator();
125 List
<HelpRecord
> P
;//the priority queue;
127 //Restores the heap property in P starting from position i bottom up.
128 void reheap_bottom_up(int i
)
130 int parent
= (i
-1)/2;
132 if((i
!= 0) && ((*P
.get(parent
)).get_value() > (*P
.get(i
)).get_value()))
135 reheap_bottom_up(parent
);
139 //Restores the heap property in P starting from position i top down.
140 void reheap_top_down(int i
)
146 if((l
<= P
.size()-1) && ((*P
.get(l
)).get_value() < (*P
.get(i
)).get_value()))
150 if((r
<= P
.size()-1) && ((*P
.get(r
)).get_value() < (*P
.get(smallest
)).get_value()))
152 if(smallest
!= i
)//exchange and recursion
154 exchange(i
,smallest
);
155 reheap_top_down(smallest
);
159 //Exchanges heap entries at positions i and j.
160 void exchange(int i
, int j
)
162 HelpRecord h
= *P
.get(i
);
163 *P
.get(i
) = *P
.get(j
);