a580e12000-09-27Fredrik Hübinette (Hubbe) #pike __REAL_VERSION__
a20af62000-09-26Fredrik Hübinette (Hubbe) 
b78e3a1999-12-30Fredrik Hübinette (Hubbe) inherit .Heap;
38cbfe2006-04-24Robert Hinn //! 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.
b78e3a1999-12-30Fredrik Hübinette (Hubbe) class elem {
dd3ae62015-09-10Henrik Grubbström (Grubba)  inherit Element;
dfd4872015-08-16Henrik Grubbström (Grubba)  int|float pri;
2401832004-06-21Martin Nilsson 
09707e2019-04-11Henrik Grubbström (Grubba)  protected void create(int|float a, mixed b) { pri=a; ::create(b); }
eb56082000-01-09Fredrik Hübinette (Hubbe) 
dfd4872015-08-16Henrik Grubbström (Grubba)  void set_pri(int|float p)
eb56082000-01-09Fredrik Hübinette (Hubbe)  { pri=p;
563bd72004-01-11Martin Nilsson  adjust(this);
eb56082000-01-09Fredrik Hübinette (Hubbe)  }
dfd4872015-08-16Henrik Grubbström (Grubba)  int|float get_pri() { return pri; }
2401832004-06-21Martin Nilsson 
09707e2019-04-11Henrik Grubbström (Grubba)  protected int `<(object o) { return pri<o->pri; } protected int `>(object o) { return pri>o->pri; }
b78e3a1999-12-30Fredrik Hübinette (Hubbe) };
38cbfe2006-04-24Robert Hinn //! 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.
dd3ae62015-09-10Henrik Grubbström (Grubba) elem push(int|float pri, mixed val)
b78e3a1999-12-30Fredrik Hübinette (Hubbe) {
dd3ae62015-09-10Henrik Grubbström (Grubba)  elem handle;
2401832004-06-21Martin Nilsson 
b78e3a1999-12-30Fredrik Hübinette (Hubbe)  handle=elem(pri, val); ::push(handle); return handle; }
38cbfe2006-04-24Robert Hinn //! 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.
dfd4872015-08-16Henrik Grubbström (Grubba) void adjust_pri(mixed handle, int|float new_pri)
b78e3a1999-12-30Fredrik Hübinette (Hubbe) {
4bea8a2015-08-09Henrik Grubbström (Grubba)  handle->set_pri(new_pri);
b78e3a1999-12-30Fredrik Hübinette (Hubbe) }
38cbfe2006-04-24Robert Hinn //! Removes and returns the item on top of the heap, //! which also is the smallest value in the heap.
dd3ae62015-09-10Henrik Grubbström (Grubba) mixed pop() { return ::pop(); }
38cbfe2006-04-24Robert Hinn  //! Returns the item on top of the priority queue (which is also the element //! with the smallest priority value) without removing it.
dd3ae62015-09-10Henrik Grubbström (Grubba) mixed peek() { return ::peek(); }