From b64516a94c5149f3d4af2073ff10321f2d5db6cb Mon Sep 17 00:00:00 2001 From: Martin Barth Date: Sat, 27 Jul 2019 14:46:26 +0200 Subject: Create priority-queue.pl6 --- challenge-018/martin-barth/priority-queue.pl6 | 41 +++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 challenge-018/martin-barth/priority-queue.pl6 diff --git a/challenge-018/martin-barth/priority-queue.pl6 b/challenge-018/martin-barth/priority-queue.pl6 new file mode 100644 index 0000000000..ca50cd29a2 --- /dev/null +++ b/challenge-018/martin-barth/priority-queue.pl6 @@ -0,0 +1,41 @@ +class PriorityQueue { + class Item { + has Real $.prio; + has Any $.element; + } + has @.elements; + + method insert-with-priority(Real:D $prio, Any:D $element) { + my $item = Item.new(:$prio, :$element); + my $new_pos = self!find-pos($prio, 0, @!elements.elems); + @!elements.splice($new_pos, 0, $item); + } + + method !find-pos($prio, Int $start, Int $end) { + my $pos = (($start + $end) / 2); + if $pos < 0 { + return 0; + } else { + if $start == $end { + return $start; + } else { + $pos = $pos.Int; + my $item = @!elements[$pos]; + if $item.prio < $prio { + return self!find-pos($prio, $start, $pos); + } else { + return self!find-pos($prio, $pos+1, $end); + } + } + } + } + + method is-empty(--> Bool) { + return @!elements.elems == 0; + } + + method pull-highest-priority-element(--> Any) { + fail if self.is-empty; + return @!elements.shift.element; + } +} -- cgit From 9595daace67435a9d22731ca0841e00379b00030 Mon Sep 17 00:00:00 2001 From: Martin Barth Date: Sat, 27 Jul 2019 14:50:05 +0200 Subject: fixed naming of the solution --- challenge-018/martin-barth/perl6/ch-2.p6 | 41 +++++++++++++++++++++++++++ challenge-018/martin-barth/priority-queue.pl6 | 41 --------------------------- 2 files changed, 41 insertions(+), 41 deletions(-) create mode 100644 challenge-018/martin-barth/perl6/ch-2.p6 delete mode 100644 challenge-018/martin-barth/priority-queue.pl6 diff --git a/challenge-018/martin-barth/perl6/ch-2.p6 b/challenge-018/martin-barth/perl6/ch-2.p6 new file mode 100644 index 0000000000..ca50cd29a2 --- /dev/null +++ b/challenge-018/martin-barth/perl6/ch-2.p6 @@ -0,0 +1,41 @@ +class PriorityQueue { + class Item { + has Real $.prio; + has Any $.element; + } + has @.elements; + + method insert-with-priority(Real:D $prio, Any:D $element) { + my $item = Item.new(:$prio, :$element); + my $new_pos = self!find-pos($prio, 0, @!elements.elems); + @!elements.splice($new_pos, 0, $item); + } + + method !find-pos($prio, Int $start, Int $end) { + my $pos = (($start + $end) / 2); + if $pos < 0 { + return 0; + } else { + if $start == $end { + return $start; + } else { + $pos = $pos.Int; + my $item = @!elements[$pos]; + if $item.prio < $prio { + return self!find-pos($prio, $start, $pos); + } else { + return self!find-pos($prio, $pos+1, $end); + } + } + } + } + + method is-empty(--> Bool) { + return @!elements.elems == 0; + } + + method pull-highest-priority-element(--> Any) { + fail if self.is-empty; + return @!elements.shift.element; + } +} diff --git a/challenge-018/martin-barth/priority-queue.pl6 b/challenge-018/martin-barth/priority-queue.pl6 deleted file mode 100644 index ca50cd29a2..0000000000 --- a/challenge-018/martin-barth/priority-queue.pl6 +++ /dev/null @@ -1,41 +0,0 @@ -class PriorityQueue { - class Item { - has Real $.prio; - has Any $.element; - } - has @.elements; - - method insert-with-priority(Real:D $prio, Any:D $element) { - my $item = Item.new(:$prio, :$element); - my $new_pos = self!find-pos($prio, 0, @!elements.elems); - @!elements.splice($new_pos, 0, $item); - } - - method !find-pos($prio, Int $start, Int $end) { - my $pos = (($start + $end) / 2); - if $pos < 0 { - return 0; - } else { - if $start == $end { - return $start; - } else { - $pos = $pos.Int; - my $item = @!elements[$pos]; - if $item.prio < $prio { - return self!find-pos($prio, $start, $pos); - } else { - return self!find-pos($prio, $pos+1, $end); - } - } - } - } - - method is-empty(--> Bool) { - return @!elements.elems == 0; - } - - method pull-highest-priority-element(--> Any) { - fail if self.is-empty; - return @!elements.shift.element; - } -} -- cgit