Priority queue: Difference between revisions
Content added Content deleted
Line 4,364: | Line 4,364: | ||
=={{header|Logtalk}}== |
=={{header|Logtalk}}== |
||
Logtalk comes with a [https://github.com/LogtalkDotOrg/logtalk3/tree/master/library/heaps heap implementation] out of the box. As such it by definition also has a priority queue. It can be used at the toplevel like this (with some formatting changes for clarity, and |
Logtalk comes with a [https://github.com/LogtalkDotOrg/logtalk3/tree/master/library/heaps heap implementation] out of the box. As such it by definition also has a priority queue. It can be used at the toplevel like this (with some formatting changes for clarity, and <code>%</code> marking comments that would not be in the output): |
||
<syntaxhighlight lang="logtalk">?- logtalk_load(heaps(loader)). % also `{heaps(loader)}.` on most back-ends |
<syntaxhighlight lang="logtalk">?- logtalk_load(heaps(loader)). % also `{heaps(loader)}.` on most back-ends |
||
Line 4,389: | Line 4,389: | ||
heap(<)::top(H4, K4, V4). % K4=2, V4='Tax return'</syntaxhighlight> |
heap(<)::top(H4, K4, V4). % K4=2, V4='Tax return'</syntaxhighlight> |
||
Since |
Since <code>heap(Ordering)</code> is a parametrized object in Logtalk, with the parameter being the ordering predicate, we actually use <code>heap(<)</code> object to get min ordering. There are two objects provided in Logtalk that eliminate the unnecessary replication of the two most common orderings: |
||
<syntaxhighlight lang="logtalk">:- object(minheap, |
<syntaxhighlight lang="logtalk">:- object(minheap, |
||
Line 4,416: | Line 4,416: | ||
:- end_object.</syntaxhighlight> |
:- end_object.</syntaxhighlight> |
||
Given the presence of these two objects, all of the example code above could have |
Given the presence of these two objects, all of the example code above could have <code>heap(<)</code> replaced with <code>minheap</code> for identical results (including identical performance). It also illustrates how quickly and easily other orderings could be provided at need. |
||
=={{header|Lua}}== |
=={{header|Lua}}== |