Skip to content

Commit 33683af

Browse files
committed
Merge pull request #2 from titsuki/feature-class-insertion
Feature class insertion
2 parents 508fde7 + 10182d9 commit 33683af

File tree

11 files changed

+578
-66
lines changed

11 files changed

+578
-66
lines changed

META6.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
"resources" : [ ],
66
"build-depends" : [ ],
77
"provides" : {
8-
"Algorithm::MinMaxHeap" : "lib/Algorithm/MinMaxHeap.pm6"
8+
"Algorithm::MinMaxHeap::Comparable" : "lib/Algorithm/MinMaxHeap/Comparable.pm6",
9+
"Algorithm::MinMaxHeap" : "lib/Algorithm/MinMaxHeap.pm6",
10+
"Algorithm::MinMaxHeap::CmpOperator" : "lib/Algorithm/MinMaxHeap/CmpOperator.pm6"
911
},
1012
"depends" : [ ],
1113
"description" : "double ended priority queue",

README.md

Lines changed: 64 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ SYNOPSIS
1010

1111
use Algorithm::MinMaxHeap;
1212

13+
# item is a Int
1314
my $heap = Algorithm::MinMaxHeap.new();
1415
$heap.insert(0);
1516
$heap.insert(1);
@@ -30,6 +31,49 @@ SYNOPSIS
3031
}
3132
@array.say # [8, 7, 6, 5, 4, 3, 2, 1, 0]
3233

34+
# item is a class
35+
36+
# sets compare-to method using Algorithm::MinMaxHeap::Comparable role
37+
my class State {
38+
also does Algorithm::MinMaxHeap::Comparable[State];
39+
has Int $.value;
40+
has $.payload;
41+
submethod BUILD(:$!value) { }
42+
method compare-to(State $s) {
43+
if (self.value == $s.value) {
44+
return Order::Same;
45+
}
46+
if (self.value > $s.value) {
47+
return Order::More;
48+
}
49+
if (self.value < $s.value) {
50+
return Order::Less;
51+
}
52+
}
53+
}
54+
55+
# specify Algorithm::MinMaxHeap::Comparable role as an item type
56+
my $class-heap = Algorithm::MinMaxHeap.new(type => Algorithm::MinMaxHeap::Comparable);
57+
$class-heap.insert(State.new(value => 0));
58+
$class-heap.insert(State.new(value => 1));
59+
$class-heap.insert(State.new(value => 2));
60+
$class-heap.insert(State.new(value => 3));
61+
$class-heap.insert(State.new(value => 4));
62+
$class-heap.insert(State.new(value => 5));
63+
$class-heap.insert(State.new(value => 6));
64+
$class-heap.insert(State.new(value => 7));
65+
$class-heap.insert(State.new(value => 8));
66+
67+
$class-heap.find-max.value.say # 8;
68+
$class-heap.find-min.value.say # 0;
69+
70+
my @array;
71+
while (not $class-heap.is-empty()) {
72+
my $state = $class-heap.pop-max;
73+
@array.push($state.value);
74+
}
75+
@array.say # [8, 7, 6, 5, 4, 3, 2, 1, 0]
76+
3377
DESCRIPTION
3478
===========
3579

@@ -38,44 +82,51 @@ Algorithm::MinMaxHeap is a simple implementation of double ended priority queue.
3882
CONSTRUCTOR
3983
-----------
4084

41-
my $heap = MinMaxHeap.new();
85+
my $heap = Algorithm::MinMaxHeap.new(); # when no options are specified, it sets type => Int implicitly
86+
my $heap = Algorithm::MinMaxHeap.new(%options);
87+
88+
### OPTIONS
89+
90+
* `type => Algorithm::MinMaxHeap::Comparable|Cool|Str|Rat|Int|Num`
91+
92+
Sets either one of the type objects which you use to insert items to the heap.
4293

4394
METHODS
4495
-------
4596

46-
### insert(Int:D $value)
97+
### insert($item)
4798

48-
$heap.insert($value);
99+
$heap.insert($item);
49100

50-
Inserts a value to the queue.
101+
Inserts an item to the queue.
51102

52103
### pop-max()
53104

54-
my $max-value = $heap.pop-max();
105+
my $max-value-item = $heap.pop-max();
55106

56-
Returns a maximum value in the queue and deletes this value in the queue.
107+
Returns a maximum value item in the queue and deletes this item in the queue.
57108

58109
### pop-min()
59110

60-
my $min-value = $heap.pop-min();
111+
my $min-value-item = $heap.pop-min();
61112

62-
Returns a minimum value in the queue and deletes this value in the queue.
113+
Returns a minimum value item in the queue and deletes this item in the queue.
63114

64115
### find-max()
65116

66-
my $max-value = $heap.find-max();
117+
my $max-value-item = $heap.find-max();
67118

68-
Returns a maximum value in the queue.
119+
Returns a maximum value item in the queue.
69120

70121
### find-min()
71122

72-
my $min-value = $heap.find-min();
123+
my $min-value-item = $heap.find-min();
73124

74-
Returns a minimum value in the queue.
125+
Returns a minimum value item in the queue.
75126

76127
### is-empty() returns Bool:D
77128

78-
while (not is-empty()) {
129+
while (not $heap.is-empty()) {
79130
// YOUR CODE
80131
}
81132

0 commit comments

Comments
 (0)