Skip to content

Commit 717a5db

Browse files
committed
Update description
1 parent f4384a0 commit 717a5db

File tree

2 files changed

+134
-25
lines changed

2 files changed

+134
-25
lines changed

README.md

Lines changed: 70 additions & 12 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,56 @@ 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+
45+
return Order::Same;
46+
47+
}
48+
if (self.value > $s.value) {
49+
return Order::More;
50+
}
51+
52+
if (self.value < $s.value) {
53+
54+
return Order::Less;
55+
56+
}
57+
58+
}
59+
60+
}
61+
62+
# specify Algorithm::MinMaxHeap::Comparable role as a item type
63+
my $class-heap = Algorithm::MinMaxHeap.new(type => Algorithm::MinMaxHeap::Comparable);
64+
$class-heap.insert(State.new(value => 0));
65+
$class-heap.insert(State.new(value => 1));
66+
$class-heap.insert(State.new(value => 2));
67+
$class-heap.insert(State.new(value => 3));
68+
$class-heap.insert(State.new(value => 4));
69+
$class-heap.insert(State.new(value => 5));
70+
$class-heap.insert(State.new(value => 6));
71+
$class-heap.insert(State.new(value => 7));
72+
$class-heap.insert(State.new(value => 8));
73+
74+
$heap.find-max.value.say # 8;
75+
$heap.find-min.value.say # 0;
76+
77+
my @array;
78+
while (not $heap.is-empty()) {
79+
my $state = $heap.pop-max;
80+
@array.push($state.value);
81+
}
82+
@array.say # [8, 7, 6, 5, 4, 3, 2, 1, 0]
83+
3384
DESCRIPTION
3485
===========
3586

@@ -38,40 +89,47 @@ Algorithm::MinMaxHeap is a simple implementation of double ended priority queue.
3889
CONSTRUCTOR
3990
-----------
4091

41-
my $heap = MinMaxHeap.new();
92+
my $heap = Algorithm::MinMaxHeap.new(); # when no options are specified, it sets type => Int implicitly
93+
my $heap = Algorithm::MinMaxHeap.new(%options);
94+
95+
### OPTIONS
96+
97+
* `type => Algorithm::MinMaxHeap::Comparable|Cool|Str|Rat|Int|Num`
98+
99+
Sets either one of the type objects which you use to insert items to the heap.
42100

43101
METHODS
44102
-------
45103

46-
### insert(Int:D $value)
104+
### insert($item)
47105

48-
$heap.insert($value);
106+
$heap.insert($item);
49107

50-
Inserts a value to the queue.
108+
Inserts a item to the queue.
51109

52110
### pop-max()
53111

54-
my $max-value = $heap.pop-max();
112+
my $max-value-item = $heap.pop-max();
55113

56-
Returns a maximum value in the queue and deletes this value in the queue.
114+
Returns a maximum value item in the queue and deletes this item in the queue.
57115

58116
### pop-min()
59117

60-
my $min-value = $heap.pop-min();
118+
my $min-value-item = $heap.pop-min();
61119

62-
Returns a minimum value in the queue and deletes this value in the queue.
120+
Returns a minimum value item in the queue and deletes this item in the queue.
63121

64122
### find-max()
65123

66-
my $max-value = $heap.find-max();
124+
my $max-value-item = $heap.find-max();
67125

68-
Returns a maximum value in the queue.
126+
Returns a maximum value item in the queue.
69127

70128
### find-min()
71129

72-
my $min-value = $heap.find-min();
130+
my $min-value-item = $heap.find-min();
73131

74-
Returns a minimum value in the queue.
132+
Returns a minimum value item in the queue.
75133

76134
### is-empty() returns Bool:D
77135

lib/Algorithm/MinMaxHeap.pm6

Lines changed: 64 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,8 @@ Algorithm::MinMaxHeap - double ended priority queue
361361
=head1 SYNOPSIS
362362
363363
use Algorithm::MinMaxHeap;
364-
364+
365+
# item is a Int
365366
my $heap = Algorithm::MinMaxHeap.new();
366367
$heap.insert(0);
367368
$heap.insert(1);
@@ -382,45 +383,95 @@ Algorithm::MinMaxHeap - double ended priority queue
382383
}
383384
@array.say # [8, 7, 6, 5, 4, 3, 2, 1, 0]
384385
386+
# item is a class
387+
388+
# sets compare-to method using Algorithm::MinMaxHeap::Comparable role
389+
my class State {
390+
also does Algorithm::MinMaxHeap::Comparable[State];
391+
has Int $.value;
392+
has $.payload;
393+
submethod BUILD(:$!value) { }
394+
method compare-to(State $s) {
395+
if (self.value == $s.value) {
396+
return Order::Same;
397+
}
398+
if (self.value > $s.value) {
399+
return Order::More;
400+
}
401+
if (self.value < $s.value) {
402+
return Order::Less;
403+
}
404+
}
405+
}
406+
407+
# specify Algorithm::MinMaxHeap::Comparable role as a item type
408+
my $class-heap = Algorithm::MinMaxHeap.new(type => Algorithm::MinMaxHeap::Comparable);
409+
$class-heap.insert(State.new(value => 0));
410+
$class-heap.insert(State.new(value => 1));
411+
$class-heap.insert(State.new(value => 2));
412+
$class-heap.insert(State.new(value => 3));
413+
$class-heap.insert(State.new(value => 4));
414+
$class-heap.insert(State.new(value => 5));
415+
$class-heap.insert(State.new(value => 6));
416+
$class-heap.insert(State.new(value => 7));
417+
$class-heap.insert(State.new(value => 8));
418+
419+
$heap.find-max.value.say # 8;
420+
$heap.find-min.value.say # 0;
421+
422+
my @array;
423+
while (not $heap.is-empty()) {
424+
my $state = $heap.pop-max;
425+
@array.push($state.value);
426+
}
427+
@array.say # [8, 7, 6, 5, 4, 3, 2, 1, 0]
428+
385429
=head1 DESCRIPTION
386430
387431
Algorithm::MinMaxHeap is a simple implementation of double ended priority queue.
388432
389433
=head2 CONSTRUCTOR
390434
391-
my $heap = MinMaxHeap.new();
435+
my $heap = Algorithm::MinMaxHeap.new(); # when no options are specified, it sets type => Int implicitly
436+
my $heap = Algorithm::MinMaxHeap.new(%options);
437+
438+
=head3 OPTIONS
439+
440+
=item C<<type => Algorithm::MinMaxHeap::Comparable|Cool|Str|Rat|Int|Num>>
441+
442+
Sets either one of the type objects which you use to insert items to the heap.
392443
393444
=head2 METHODS
394445
395-
=head3 insert(Int:D $value)
446+
=head3 insert($item)
396447
397-
$heap.insert($value);
448+
$heap.insert($item);
398449
399-
Inserts a value to the queue.
450+
Inserts a item to the queue.
400451
401452
=head3 pop-max()
402453
403-
my $max-value = $heap.pop-max();
454+
my $max-value-item = $heap.pop-max();
404455
405-
Returns a maximum value in the queue and deletes this value in the queue.
456+
Returns a maximum value item in the queue and deletes this item in the queue.
406457
407458
=head3 pop-min()
408459
409-
my $min-value = $heap.pop-min();
460+
my $min-value-item = $heap.pop-min();
410461
411-
Returns a minimum value in the queue and deletes this value in the queue.
462+
Returns a minimum value item in the queue and deletes this item in the queue.
412463
413464
=head3 find-max()
414465
415-
my $max-value = $heap.find-max();
466+
my $max-value-item = $heap.find-max();
416467
417-
Returns a maximum value in the queue.
468+
Returns a maximum value item in the queue.
418469
419470
=head3 find-min()
420471
421-
my $min-value = $heap.find-min();
472+
my $min-value-item = $heap.find-min();
422473
423-
Returns a minimum value in the queue.
474+
Returns a minimum value item in the queue.
424475
425476
=head3 is-empty() returns Bool:D
426477

0 commit comments

Comments
 (0)