File tree Expand file tree Collapse file tree 3 files changed +42
-4
lines changed Expand file tree Collapse file tree 3 files changed +42
-4
lines changed Original file line number Diff line number Diff line change @@ -132,6 +132,12 @@ Returns a minimum value item in the queue.
132132
133133Returns whether the queue is empty or not.
134134
135+ ### clear()
136+
137+ $heap.clear();
138+
139+ Deletes all items in the queue.
140+
135141CAUTION
136142=======
137143
Original file line number Diff line number Diff line change @@ -101,6 +101,14 @@ method pop-max() {
101101 }
102102}
103103
104+ method is-empty () returns Bool : D {
105+ return @ ! nodes . elems == 0 ?? True !! False ;
106+ }
107+
108+ method clear () {
109+ @ ! nodes = ();
110+ }
111+
104112method ! compare ($ lhs , $ rhs ) returns Order : D {
105113 if ($ ! type ~~ Cool ) {
106114 return $ lhs minmaxheap-cmp $ rhs ;
@@ -109,10 +117,6 @@ method !compare($lhs, $rhs) returns Order:D {
109117 }
110118}
111119
112- method is-empty () returns Bool : D {
113- return @ ! nodes . elems == 0 ?? True !! False ;
114- }
115-
116120method ! bubble-up ($ index ) {
117121 if (self ! is-minlevel($ index )) {
118122 if (self ! has-parent($ index ) and (@ ! nodes [$ index ] minmaxheap-cmp @ ! nodes [self ! find-parent($ index )] == Order ::More)) {
@@ -481,6 +485,12 @@ Returns a minimum value item in the queue.
481485
482486Returns whether the queue is empty or not.
483487
488+ = head3 clear()
489+
490+ $heap.clear();
491+
492+ Deletes all items in the queue.
493+
484494= head1 CAUTION
485495
486496Don't insert both numerical items and stringified items into the same queue.
Original file line number Diff line number Diff line change 1+ use v6 ;
2+ use Test ;
3+ use Algorithm::MinMaxHeap;
4+
5+ {
6+ my $ heap = Algorithm::MinMaxHeap. new ();
7+ $ heap . insert(0 );
8+ $ heap . insert(1 );
9+ $ heap . insert(2 );
10+ $ heap . insert(3 );
11+ $ heap . insert(4 );
12+ $ heap . insert(5 );
13+ $ heap . insert(6 );
14+ $ heap . insert(7 );
15+ $ heap . insert(8 );
16+
17+ is $ heap . nodes. elems , 9 ;
18+ $ heap . clear();
19+ is $ heap . nodes. elems , 0 ;
20+ }
21+
22+ done-testing ;
You can’t perform that action at this time.
0 commit comments