Skip to content

Commit

Permalink
Make it more perl6ish
Browse files Browse the repository at this point in the history
  • Loading branch information
titsuki committed Nov 8, 2018
1 parent dba5caf commit 48bd715
Showing 1 changed file with 24 additions and 24 deletions.
48 changes: 24 additions & 24 deletions lib/Algorithm/Kruskal.pm6
Original file line number Diff line number Diff line change
Expand Up @@ -14,37 +14,37 @@ submethod BUILD(:$!vertex-size) {

method add-edge(Int $from, Int $to, Num $weight) {
my class State {
also does Algorithm::MinMaxHeap::Comparable[State];
has ($.from, $.to);
has $.weight;
submethod BUILD (:$!from, :$!to, :$!weight) {}
method compare-to(State $s) {
if (self.weight == $s.weight) {
return Order::Same;
}
if (self.weight > $s.weight) {
return Order::More;
}
if (self.weight < $s.weight) {
return Order::Less;
}
}
also does Algorithm::MinMaxHeap::Comparable[State];

has ($.from, $.to);
has $.weight;
submethod BUILD (:$!from, :$!to, :$!weight) {}
method compare-to(State $s) {
if self.weight == $s.weight {
return Order::Same;
}
if self.weight > $s.weight {
return Order::More;
}
if self.weight < $s.weight {
return Order::Less;
}
}
}
$!heap.insert(State.new(from => $from, to => $to, weight => $weight));
$!heap.insert: State.new(from => $from, to => $to, weight => $weight);
}

method compute-minimal-spanning-tree(--> List) {
my $weight = 0;
my @edges;
my $prev-heap = $!heap.clone;
my Algorithm::SetUnion $set-union = Algorithm::SetUnion.new(size => $!vertex-size);
while (not $!heap.is-empty()) {
my $state = $!heap.pop-min;
if ($set-union.union($state.from, $state.to)) {
$weight += $state.weight;
@edges.push([$state.from, $state.to]);
}
my Algorithm::SetUnion $set-union .= new(size => $!vertex-size);
until $!heap.is-empty {
my $state = $!heap.pop-min;
if $set-union.union($state.from, $state.to) {
$weight += $state.weight;
@edges.push: [$state.from, $state.to];
}
}
$!heap = $prev-heap;
return :@edges, :$weight;
Expand Down

0 comments on commit 48bd715

Please sign in to comment.