Skip to content

Commit b30273b

Browse files
committed
Merge pull request #9 from titsuki/fix-clone
Fix a clone method
2 parents b708a1c + c653555 commit b30273b

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

lib/Algorithm/MinMaxHeap.pm6

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,10 @@ method clear() {
104104
@!nodes = ();
105105
}
106106

107+
method clone {
108+
nextwith(:type($.type.clone), :nodes(@.nodes.clone))
109+
}
110+
107111
method !bubble-up($index) {
108112
if (self!is-minlevel($index)) {
109113
if (self!has-parent($index) and (@!nodes[$index] minmaxheap-cmp @!nodes[self!find-parent($index)] == Order::More)) {

t/08-clone.t

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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+
19+
my $clone-heap = $heap.clone;
20+
is $clone-heap.nodes, $heap.nodes;
21+
$heap.clear();
22+
is $clone-heap.nodes.elems, 9;
23+
is $clone-heap.type === Int, True;
24+
}
25+
26+
done-testing;

0 commit comments

Comments
 (0)