Recent

Author Topic: Priority queue  (Read 697 times)

LemonParty

  • Sr. Member
  • ****
  • Posts: 375
Priority queue
« on: October 07, 2025, 07:13:18 pm »
Hello.

Is there a good implementation of a priority queue?
Lazarus v. 4.99. FPC v. 3.3.1. Windows 11

simone

  • Hero Member
  • *****
  • Posts: 679
Re: Priority queue
« Reply #1 on: October 07, 2025, 11:01:25 pm »
The following two libraries implement priority queue data structure:

- LGenerics: https://wiki.freepascal.org/LGenerics
- FCL-STL: https://ioinformatics.org/journal/INFOL101.pdf

See also: https://wiki.freepascal.org/Data_Structures,_Containers,_Collections
Microsoft Windows 10/11 64 bit - Lazarus 3.8/4.0 FPC 3.2.2 x86_64-win64-win32/win64

LemonParty

  • Sr. Member
  • ****
  • Posts: 375
Re: Priority queue
« Reply #2 on: October 08, 2025, 02:59:44 pm »
That is good. Thank you.
Lazarus v. 4.99. FPC v. 3.3.1. Windows 11

LemonParty

  • Sr. Member
  • ****
  • Posts: 375
Re: Priority queue
« Reply #3 on: October 08, 2025, 09:17:23 pm »
I study the code from LGenerics, lgPriorityQueue.pas.
I am not very familiar with generics. Could anybody show an example of usage of priority queue?
Lazarus v. 4.99. FPC v. 3.3.1. Windows 11

avk

  • Hero Member
  • *****
  • Posts: 824
Re: Priority queue
« Reply #4 on: October 09, 2025, 12:09:30 am »
With a priority queue, everything is roughly the same as with any other generic container.
First, you need to specify the type of element that the container will be specialized for, given that the priority queue also needs a way to compare the priorities of the elements in order to work properly.
Let, for example, this be the very first way: the TCmpRel functor(see line 59):
Code: Pascal  [Select][+][-]
  1. program pq_test;
  2. {$mode delphi}
  3. uses
  4.   SysUtils, LgUtils, LgPriorityQueue, LgMiscUtils;
  5.  
  6. type
  7.   TPqEl = record
  8.     Data,
  9.     Prio: Integer;
  10.     constructor Make(aData, aPrio: Integer);
  11.     class function Less(const L, R: TPqEl): Boolean; static; inline; /////
  12.   end;
  13.  
  14. constructor TPqEl.Make(aData, aPrio: Integer);
  15. begin
  16.   Data := aData;
  17.   Prio := aPrio;
  18. end;
  19.  
  20. class function TPqEl.Less(const L, R: TPqEl): Boolean;
  21. begin
  22.   Result := L.Prio < R.Prio;
  23. end;
  24.  
  25. var
  26.   pq: TGBinHeap<TPqEl>;
  27.   e: TPqEl;
  28. begin
  29. //you can create an empty queue
  30. //pq := TGBinHeap<TPqEl>.Create;
  31. //or not empty
  32.   pq := TGBinHeap<TPqEl>.Create(
  33.     [TPqEl.Make(70, 1), TPqEl.Make(60, 2), TPqEl.Make(50, 3), TPqEl.Make(40, 4)]
  34.   );
  35. //check that the queue is not empty and look at the top element
  36.   if pq.NonEmpty then
  37.     WriteLn('Top element: ', Stringify<TPqEl>(pq.Peek));
  38. //you can add elements one by one
  39.   pq.Enqueue(TPqEl.Make(30, 5));
  40.   pq.Enqueue(TPqEl.Make(20, 0));
  41. //or several at once
  42.   pq.EnqueueAll([TPqEl.Make(10, 6), TPqEl.Make(0, -1)]);
  43. //dequeue element
  44.   e := pq.Dequeue;
  45.   WriteLn('Element: ', Stringify<TPqEl>(e));
  46. //dequeue all elements
  47.   while pq.TryDequeue(e) do
  48.     WriteLn('Dequeued: ', Stringify<TPqEl>(e));
  49.   WriteLn('Remaining in queue: ', pq.Count);
  50.   pq.Free;
  51. end.
  52.  

LemonParty

  • Sr. Member
  • ****
  • Posts: 375
Re: Priority queue
« Reply #5 on: October 09, 2025, 04:22:33 pm »
Good explanation. Thank you.
Lazarus v. 4.99. FPC v. 3.3.1. Windows 11

 

TinyPortal © 2005-2018