blob: abc3f80ed0e296d1b241f3ec43ef7a28b9eeaae6 (
plain)
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
|
################################################################################
=begin comment
Perl Weekly Challenge 018
=========================
Task #2
-------
Write a script to implement *Priority Queue*. It is like regular *queue* except
each element has a *priority* associated with it. In a priority queue, an
element with high priority is served before an element with low priority. Please
check this [ https://en.wikipedia.org/wiki/Priority_queue |wiki page] for more
informations. It should serve the following operations:
1) *is_empty*: check whether the queue has no elements.
2) *insert_with_priority*: add an element to the queue with an associated
priority.
3) *pull_highest_priority_element*: remove the element from the queue that has
the highest priority, and return it. If two elements have the same
priority, then return element added first.
=end comment
################################################################################
#--------------------------------------#
# Copyright © 2019 PerlMonk Athanasius #
#--------------------------------------#
class MyPriorityQueue
{
use Heap;
has Heap $!heap;
submethod BUILD(Bool :$reverse = False)
{
$!heap = $reverse ?? Heap[-*<order>].new
!! Heap[ *<order>].new;
}
method is_empty(--> Bool)
{
return !?$!heap;
}
method insert_with_priority(Int:D $priority, Any:D $element)
{
$!heap.push: { order => $priority,
datum => $element, };
}
method pull_highest_priority_element(--> Any)
{
my Any $element;
unless self.is_empty()
{
my %top = $!heap.pop;
$element = %top< datum >;
}
return $element;
}
}
################################################################################
|