Skip to content

Commit a3924bc

Browse files
committed
Merge pull request #11 from titsuki/fix-clone
Fix a clone method to handle Comparable role
2 parents 8208ea2 + e3c4dcc commit a3924bc

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

lib/Algorithm/MinMaxHeap.pm6

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ method clear() {
105105
}
106106

107107
method clone {
108-
nextwith(:type($.type.clone), :nodes(@.nodes.clone))
108+
nextwith(:type($!type.WHAT), :nodes(@.nodes.clone))
109109
}
110110

111111
method !bubble-up($index) {

t/08-clone.t

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,45 @@ use Algorithm::MinMaxHeap;
2323
is $clone-heap.type === Int, True;
2424
}
2525

26+
{
27+
my class State {
28+
also does Algorithm::MinMaxHeap::Comparable[State];
29+
has Int $.value;
30+
has $.payload;
31+
submethod BUILD(:$!value) { }
32+
method compare-to(State $s) {
33+
if (self.value == $s.value) {
34+
return Order::Same;
35+
}
36+
if (self.value > $s.value) {
37+
return Order::More;
38+
}
39+
if (self.value < $s.value) {
40+
return Order::Less;
41+
}
42+
}
43+
}
44+
45+
my $heap = Algorithm::MinMaxHeap.new(type => Algorithm::MinMaxHeap::Comparable);
46+
47+
$heap.insert(State.new(value => 0));
48+
$heap.insert(State.new(value => 1));
49+
$heap.insert(State.new(value => 2));
50+
$heap.insert(State.new(value => 3));
51+
$heap.insert(State.new(value => 4));
52+
$heap.insert(State.new(value => 5));
53+
$heap.insert(State.new(value => 6));
54+
$heap.insert(State.new(value => 7));
55+
$heap.insert(State.new(value => 8));
56+
57+
is $heap.nodes.elems, 9;
58+
59+
my $clone-heap = $heap.clone;
60+
is $clone-heap.nodes, $heap.nodes;
61+
$heap.clear();
62+
is $clone-heap.nodes.elems, 9;
63+
is $clone-heap.type === Algorithm::MinMaxHeap::Comparable, True;
64+
}
65+
66+
2667
done-testing;

0 commit comments

Comments
 (0)