1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
|
with Unchecked_Deallocation ;
package body Pqueue is
procedure Init( Q : out T_Priority_Queue ;
Mode : in T_Priority_Mode ) is
begin
Q.First := null ;
Q.Mode := Mode ;
end;
procedure Destroy( Q : in out T_Priority_Queue ) is
procedure Free is new Unchecked_Deallocation( T_Priority_Queue_Node , T_Priority_Queue_Node_Ptr );
procedure Destroy( Q : in out T_Priority_Queue_Node_Ptr ) is
begin
if Q /= null then
Destroy( Q.Brother );
Destroy( Q.Child );
Free( Q );
Q := null ;
end if;
end ;
begin
Destroy( Q.First );
end;
procedure Copy( Q : out T_Priority_Queue ; Src : in T_Priority_Queue ) is
procedure Copy( Node : out T_Priority_Queue_Node_ptr ;
Src : in T_Priority_Queue_Node_Ptr ;
Father : in T_Priority_Queue_Node_Ptr ) is
begin
if Src = null then
Node := null ;
else
Node := new T_Priority_Queue_Node'( Prio => Src.Prio ,
Data => Src.Data ,
Degree => Src.Degree ,
Father => Father ,
Brother => null ,
Child => null );
Copy( Node.Brother , Src.Brother , Father );
Copy( Node.Child , Src.Child , Node ) ;
end if;
end;
begin
Copy( Q.First , Src.First , null );
Q.Mode := Src.Mode ;
end;
procedure Add( Q : in out T_Priority_Queue ; X : in T_Data ; K : in T_Priority ) is
Node : T_Priority_Queue_Node_Ptr := null ;
Tmp : T_Priority_Queue_Node_Ptr := null ;
begin
Node := new T_Priority_Queue_Node'( Data => X ,
Prio => K ,
Degree => 0 ,
Brother => null ,
Child => null ,
Father => null );
if Q.Mode = Min_Priority then
Fusion_Min( Tmp , Q.First , Node );
else
Fusion_Max( Tmp , Q.First , Node );
end if;
Q.First := Tmp ;
end;
procedure Extract( Q : in out T_Priority_Queue ; X : out T_Data ; K : out T_Priority ) is
procedure Free is new Unchecked_Deallocation( T_Priority_Queue_Node , T_Priority_Queue_Node_Ptr );
procedure Invert( New_Head : out T_Priority_Queue_Node_Ptr ;
List : in out T_Priority_Queue_Node_Ptr ;
Brother : in T_Priority_Queue_Node_Ptr ) is
begin
if List = null then
New_Head := null ;
elsif List.Brother = null then
New_Head := List ;
List.Brother := Brother ;
List.Father := null ;
else
Invert( New_Head , List.Brother , List );
List.Brother := Brother ;
List.Father := null ;
end if;
end;
Cur : T_Priority_Queue_Node_Ptr := Q.First ;
Good : T_Priority_Queue_Node_Ptr := Q.First ;
Before : T_Priority_Queue_Node_Ptr := null ;
Before_Good : T_Priority_Queue_Node_Ptr := null ;
Tmp : T_Priority_Queue_Node_Ptr ;
Tmp2 : T_Priority_Queue_Node_Ptr ;
begin
-- Recupere le mini
if Cur = null then
raise Priority_Queue_Underflow ;
else
K := Cur.Prio ;
Before := Cur ;
Cur := Cur.Brother ;
if Q.Mode = Min_Priority then
loop
exit when Cur = null ;
if Cur.Prio < K then
K := Cur.Prio ;
Good := Cur ;
Before_Good := Before ;
end if;
Before := Cur ;
Cur := Cur.Brother ;
end loop;
else
loop
exit when Cur = null ;
if K < Cur.Prio then
K := Cur.Prio ;
Good := Cur ;
Before_Good := Before ;
end if ;
Before := Cur ;
Cur := Cur.Brother ;
end loop;
end if;
X := Good.Data ;
end if;
-- supprime good de la liste des racines
if Before_Good = null then
-- suppression en tete
Q.First := Good.Brother ;
else
Before_Good.Brother := Good.Brother ;
end if;
-- inverse la liste des fils de good
Invert( Tmp , Good.Child , null );
if Q.Mode = Min_Priority then
Fusion_Min( Tmp2 , Q.First , Tmp );
else
Fusion_Max( Tmp2 , Q.First , Tmp );
end if;
Q.First := Tmp2 ;
Free( Good );
end;
procedure Top( Q : in out T_Priority_Queue ; X : out T_Data ; K : out T_Priority ) is
Cur : T_Priority_Queue_Node_Ptr := Q.First ;
Good : T_Priority_Queue_Node_Ptr := Q.First ;
begin
if Cur = null then
raise Priority_Queue_Underflow ;
else
K := Cur.Prio ;
Cur := Cur.Brother ;
if Q.Mode = Min_Priority then
loop
exit when Cur = null ;
if Cur.Prio < K then
K := Cur.Prio ;
Good := Cur ;
end if;
Cur := Cur.Brother ;
end loop;
else
loop
exit when Cur = null ;
if K < Cur.Prio then
K := Cur.Prio ;
Good := Cur ;
end if ;
Cur := Cur.Brother ;
end loop;
end if;
X := Good.Data ;
end if;
end;
function Empty( Q : in T_Priority_Queue ) return Boolean is
begin
return Q.First = null ;
end;
procedure Fusion_List( Res : out T_Priority_Queue_Node_Ptr ;
Q1 : in out T_Priority_Queue_Node_Ptr ;
Q2 : in out T_Priority_Queue_Node_Ptr ) is
Cur_Q1 : T_Priority_Queue_Node_Ptr := Q1 ;
Cur_Q2 : T_Priority_Queue_Node_Ptr := Q2 ;
Cur_Res : T_Priority_Queue_Node_Ptr := null ;
Old : T_Priority_Queue_Node_Ptr := null ;
begin
-- gere le premier echelon
if Cur_Q1 /= null and then Cur_Q2 /= null then
if Cur_Q1.Degree < Cur_Q2.Degree then
Res := Cur_Q1 ;
Cur_Res := Cur_Q1 ;
Cur_Q1 := Cur_Q1.Brother ;
else
Res := Cur_Q2 ;
Cur_Res := Cur_Q2 ;
Cur_Q2 := Cur_Q2.Brother ;
end if;
elsif Cur_Q1 = null and then Cur_Q2 /= null then
Res := Cur_Q2 ;
Cur_Res := Cur_Q2 ;
Cur_Q2 := Cur_Q2.Brother ;
elsif Cur_Q2 = null and then Cur_Q1 /= null then
Res := Cur_Q1 ;
Cur_Res := Cur_Q1 ;
Cur_Q1 := Cur_Q1.Brother ;
else -- les deux vides
Res := null ;
return ;
end if;
-- Les deux listes non vides
loop
exit when Cur_Q1 = null ;
exit when Cur_Q2 = null ;
if Cur_Q1.Degree < Cur_Q2.Degree then
Cur_Res.Brother := Cur_Q1 ;
Cur_Q1 := Cur_Q1.Brother ;
Cur_Res := Cur_Res.Brother ;
else
Cur_Res.Brother := Cur_Q2 ;
Cur_Q2 := Cur_Q2.Brother ;
Cur_Res := Cur_Res.Brother ;
end if;
end loop;
if Cur_Q2 = null Then
Cur_Res.Brother := Cur_Q1 ;
else
Cur_Res.Brother := Cur_Q2 ;
end if;
end;
procedure Fusion_Min( Res : out T_Priority_Queue_Node_Ptr ;
Q1 : in out T_Priority_Queue_Node_Ptr ;
Q2 : in out T_Priority_Queue_Node_Ptr ) is
-- link node under new_father
procedure Link( Node : in out T_Priority_Queue_Node_Ptr ;
New_Father : in out T_Priority_Queue_Node_Ptr ) is
begin
Node.Father := New_Father ;
Node.Brother := New_Father.Child ;
New_Father.Child := Node ;
New_Father.Degree := New_Father.Degree + 1 ;
end;
Prev , Cur , Next : T_Priority_Queue_Node_Ptr ;
begin
Fusion_List( Res , Q1 , Q2 );
if Res = null then
return ;
end if;
Prev := null ;
Cur := Res ;
Next := Cur.Brother ;
loop
exit when Next = null ;
if Cur.Degree /= Next.Degree then
-- Cas 1 : degree differents, on passe
Prev := Cur ;
Cur := Next ;
Next := Cur.Brother ;
elsif Next.Brother /= null and then
Cur.Degree = Next.Degree and then
Next.Degree = Next.Brother.Degree then
-- Cas 2 : trois racines de mm degre
Prev := Cur ;
Cur := Next ;
Next := Cur.Brother ;
else
-- Cas 3 : deux racines de mm degre
if Cur.Prio < Next.Prio then
-- On accroche next dessous cur
Cur.Brother := Next.Brother ;
Link( Next , Cur );
else
-- On accroche cur dessous next
if Prev = null then
Res := Next ;
else
Prev.Brother := Next ;
end if;
Link( Cur , Next );
Cur := Next ;
end if;
Next := Cur.Brother ;
end if;
end loop;
end ;
procedure Fusion_Max( Res : out T_Priority_Queue_Node_Ptr ;
Q1 : in out T_Priority_Queue_Node_Ptr ;
Q2 : in out T_Priority_Queue_Node_Ptr ) is
-- link node under new_father
procedure Link( Node : in out T_Priority_Queue_Node_Ptr ;
New_Father : in out T_Priority_Queue_Node_Ptr ) is
begin
Node.Father := New_Father ;
Node.Brother := New_Father.Child ;
New_Father.Child := Node ;
New_Father.Degree := New_Father.Degree + 1 ;
end;
Prev , Cur , Next : T_Priority_Queue_Node_Ptr ;
begin
Fusion_List( Res , Q1 , Q2 );
if Res = null then
return ;
end if;
Prev := null ;
Cur := Res ;
Next := Cur.Brother ;
loop
exit when Next = null ;
if Cur.Degree /= Next.Degree then
-- Cas 1 : degree differents, on passe
Prev := Cur ;
Cur := Next ;
Next := Cur.Brother ;
elsif Next.Brother /= null and then
Cur.Degree = Next.Degree and then
Next.Degree = Next.Brother.Degree then
-- Cas 2 : trois racines de mm degre
Prev := Cur ;
Cur := Next ;
Next := Cur.Brother ;
else
-- Cas 3 : deux racines de mm degre
if Next.Prio < Cur.Prio then
-- On accroche next dessous cur
Cur.Brother := Next.Brother ;
Link( Next , Cur );
else
-- On accroche cur dessous next
if Prev = null then
Res := Next ;
else
Prev.Brother := Next ;
end if;
Link( Cur , Next );
Cur := Next ;
end if;
Next := Cur.Brother ;
end if;
end loop;
end ;
end Pqueue ; |
Partager