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
  
#pike __REAL_VERSION__ 
 
inherit .Heap; 
 
//! This class implements a priority queue. Each element in the priority 
//! queue is assigned a priority value, and the priority queue always 
//! remains sorted in increasing order of the priority values. The top of 
//! the priority queue always holds the element with the smallest priority. 
//! The priority queue is realized as a (min-)heap. 
 
class elem { 
  inherit Element; 
 
  int|float pri; 
 
  void create(int|float a, mixed b) { pri=a; ::create(b); } 
 
  void set_pri(int|float p) 
    { 
      pri=p; 
      adjust(this); 
    } 
 
  int|float get_pri() { return pri; } 
 
  int `<(object o) { return pri<o->pri; } 
  int `>(object o) { return pri>o->pri; } 
}; 
 
//! Push an element @[val] into the priority queue and assign a priority value 
//! @[pri] to it. The priority queue will automatically sort itself so that 
//! the element with the smallest priority will be at the top. 
elem push(int|float pri, mixed val) 
{ 
  elem handle; 
 
  handle=elem(pri, val); 
  ::push(handle); 
  return handle; 
} 
 
//! Adjust the priority value @[new_pri] of an element @[handle] in the 
//! priority queue. The priority queue will automatically sort itself so 
//! that the element with the smallest priority value will be at the top. 
void adjust_pri(mixed handle, int|float new_pri) 
{ 
  handle->set_pri(new_pri); 
} 
 
//! Removes and returns the item on top of the heap, 
//! which also is the smallest value in the heap. 
mixed pop() { return ::pop(); } 
 
//! Returns the item on top of the priority queue (which is also the element 
//! with the smallest priority value) without removing it. 
mixed peek() { return ::peek(); }