Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitb56d35f

Browse files
committed
Merge pull request#5 from Turbo87/clear
Add clear() method
2 parentsc4b1034 +c2b622c commitb56d35f

File tree

6 files changed

+26
-0
lines changed

6 files changed

+26
-0
lines changed

‎README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ A priority queue is a data structure with these operations:
1010
| Length|`var length = queue.length;`| Returns the number of elements in the queue|
1111
| Peek|`var firstItem = queue.peek();`| Returns the smallest item in the queue and leaves the queue unchanged|
1212
| Dequeue|`var firstItem = queue.dequeue();`| Returns the smallest item in the queue and removes it from the queue|
13+
| Clear|`queue.clear();`| Removes all values from the queue|
1314

1415
You cannot access the data in any other way: you must dequeue or peek.
1516

‎src/PriorityQueue/AbstractPriorityQueue.coffee‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,7 @@ module.exports = class AbstractPriorityQueue
1818
peek: (value)->
1919
throw'Empty queue'if!@length
2020
@priv.peek()
21+
22+
clear:->
23+
@length=0
24+
@priv.clear()

‎src/PriorityQueue/ArrayStrategy.coffee‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,7 @@ module.exports = class ArrayStrategy
3030

3131
peek:->
3232
@data[@data.length-1]
33+
34+
clear:->
35+
@data.length=0
36+
undefined

‎src/PriorityQueue/BHeapStrategy.coffee‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ module.exports = class BHeapStrategy
4545
peek:->
4646
@_read(1)
4747

48+
clear:->
49+
@length=0
50+
@_memory.length=0
51+
undefined
52+
4853
_write: (index,value)->
4954
page= index>>@_shift
5055
while page>=@_memory.length# we want page < @_memory.length

‎src/PriorityQueue/BinaryHeapStrategy.coffee‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ module.exports = class BinaryHeapStrategy
2727
peek:->
2828
@data[0]
2929

30+
clear:->
31+
@length=0
32+
@data.length=0
33+
undefined
34+
3035
_bubbleUp: (pos)->
3136
while pos>0
3237
parent= (pos-1)>>>1

‎test/helpers/StrategyHelper.coffee‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,10 @@ module.exports = StrategyHelper =
6969
it'should queue a duplicate at the end',->
7070
priv.queue(3)
7171
expect(queueToArray(priv,4)).to.deep.equal([1,2,3,3])
72+
73+
describe'clear',->
74+
it'should remove all elements',->
75+
expect(priv.peek()).to.equal(1)
76+
priv.clear()
77+
priv.queue(10)
78+
expect(priv.peek()).to.equal(10)

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp