Skip to content

Commit e3f6ab8

Browse files
committed
Merge pull request #6 from titsuki/feature-clear
Add a clear method
2 parents 5f04cd5 + 54c1158 commit e3f6ab8

File tree

3 files changed

+42
-4
lines changed

3 files changed

+42
-4
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,12 @@ Returns a minimum value item in the queue.
132132

133133
Returns whether the queue is empty or not.
134134

135+
### clear()
136+
137+
$heap.clear();
138+
139+
Deletes all items in the queue.
140+
135141
CAUTION
136142
=======
137143

lib/Algorithm/MinMaxHeap.pm6

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,14 @@ method pop-max() {
101101
}
102102
}
103103

104+
method is-empty() returns Bool:D {
105+
return @!nodes.elems == 0 ?? True !! False;
106+
}
107+
108+
method clear() {
109+
@!nodes = ();
110+
}
111+
104112
method !compare($lhs, $rhs) returns Order:D {
105113
if ($!type ~~ Cool) {
106114
return $lhs minmaxheap-cmp $rhs;
@@ -109,10 +117,6 @@ method !compare($lhs, $rhs) returns Order:D {
109117
}
110118
}
111119

112-
method is-empty() returns Bool:D {
113-
return @!nodes.elems == 0 ?? True !! False;
114-
}
115-
116120
method !bubble-up($index) {
117121
if (self!is-minlevel($index)) {
118122
if (self!has-parent($index) and (@!nodes[$index] minmaxheap-cmp @!nodes[self!find-parent($index)] == Order::More)) {
@@ -481,6 +485,12 @@ Returns a minimum value item in the queue.
481485
482486
Returns whether the queue is empty or not.
483487
488+
=head3 clear()
489+
490+
$heap.clear();
491+
492+
Deletes all items in the queue.
493+
484494
=head1 CAUTION
485495
486496
Don't insert both numerical items and stringified items into the same queue.

t/07-clear.t

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
use v6;
2+
use Test;
3+
use Algorithm::MinMaxHeap;
4+
5+
{
6+
my $heap = Algorithm::MinMaxHeap.new();
7+
$heap.insert(0);
8+
$heap.insert(1);
9+
$heap.insert(2);
10+
$heap.insert(3);
11+
$heap.insert(4);
12+
$heap.insert(5);
13+
$heap.insert(6);
14+
$heap.insert(7);
15+
$heap.insert(8);
16+
17+
is $heap.nodes.elems, 9;
18+
$heap.clear();
19+
is $heap.nodes.elems, 0;
20+
}
21+
22+
done-testing;

0 commit comments

Comments
 (0)