@@ -10,6 +10,7 @@ SYNOPSIS
1010
1111 use Algorithm::MinMaxHeap;
1212
13+ # item is a Int
1314 my $heap = Algorithm::MinMaxHeap.new();
1415 $heap.insert(0);
1516 $heap.insert(1);
@@ -30,6 +31,56 @@ SYNOPSIS
3031 }
3132 @array.say # [8, 7, 6, 5, 4, 3, 2, 1, 0]
3233
34+ # item is a class
35+
36+ # sets compare-to method using Algorithm::MinMaxHeap::Comparable role
37+ my class State {
38+ also does Algorithm::MinMaxHeap::Comparable[State];
39+ has Int $.value;
40+ has $.payload;
41+ submethod BUILD(:$!value) { }
42+ method compare-to(State $s) {
43+ if (self.value == $s.value) {
44+
45+ return Order::Same;
46+
47+ }
48+ if (self.value > $s.value) {
49+ return Order::More;
50+ }
51+
52+ if (self.value < $s.value) {
53+
54+ return Order::Less;
55+
56+ }
57+
58+ }
59+
60+ }
61+
62+ # specify Algorithm::MinMaxHeap::Comparable role as a item type
63+ my $class-heap = Algorithm::MinMaxHeap.new(type => Algorithm::MinMaxHeap::Comparable);
64+ $class-heap.insert(State.new(value => 0));
65+ $class-heap.insert(State.new(value => 1));
66+ $class-heap.insert(State.new(value => 2));
67+ $class-heap.insert(State.new(value => 3));
68+ $class-heap.insert(State.new(value => 4));
69+ $class-heap.insert(State.new(value => 5));
70+ $class-heap.insert(State.new(value => 6));
71+ $class-heap.insert(State.new(value => 7));
72+ $class-heap.insert(State.new(value => 8));
73+
74+ $heap.find-max.value.say # 8;
75+ $heap.find-min.value.say # 0;
76+
77+ my @array;
78+ while (not $heap.is-empty()) {
79+ my $state = $heap.pop-max;
80+ @array.push($state.value);
81+ }
82+ @array.say # [8, 7, 6, 5, 4, 3, 2, 1, 0]
83+
3384DESCRIPTION
3485===========
3586
@@ -38,40 +89,47 @@ Algorithm::MinMaxHeap is a simple implementation of double ended priority queue.
3889CONSTRUCTOR
3990-----------
4091
41- my $heap = MinMaxHeap.new();
92+ my $heap = Algorithm::MinMaxHeap.new(); # when no options are specified, it sets type => Int implicitly
93+ my $heap = Algorithm::MinMaxHeap.new(%options);
94+
95+ ### OPTIONS
96+
97+ * ` type => Algorithm::MinMaxHeap::Comparable|Cool|Str|Rat|Int|Num `
98+
99+ Sets either one of the type objects which you use to insert items to the heap.
42100
43101METHODS
44102-------
45103
46- ### insert(Int : D $value )
104+ ### insert($item )
47105
48- $heap.insert($value );
106+ $heap.insert($item );
49107
50- Inserts a value to the queue.
108+ Inserts a item to the queue.
51109
52110### pop-max()
53111
54- my $max-value = $heap.pop-max();
112+ my $max-value-item = $heap.pop-max();
55113
56- Returns a maximum value in the queue and deletes this value in the queue.
114+ Returns a maximum value item in the queue and deletes this item in the queue.
57115
58116### pop-min()
59117
60- my $min-value = $heap.pop-min();
118+ my $min-value-item = $heap.pop-min();
61119
62- Returns a minimum value in the queue and deletes this value in the queue.
120+ Returns a minimum value item in the queue and deletes this item in the queue.
63121
64122### find-max()
65123
66- my $max-value = $heap.find-max();
124+ my $max-value-item = $heap.find-max();
67125
68- Returns a maximum value in the queue.
126+ Returns a maximum value item in the queue.
69127
70128### find-min()
71129
72- my $min-value = $heap.find-min();
130+ my $min-value-item = $heap.find-min();
73131
74- Returns a minimum value in the queue.
132+ Returns a minimum value item in the queue.
75133
76134### is-empty() returns Bool: D
77135
0 commit comments