1+ /**
2+ * Created by Stefano on 31/03/14.
3+ */
4+
5+ PriorityQueue . prototype = new Aggregate ( ) ;
6+ PriorityQueue . prototype . constructor = PriorityQueue ;
7+
8+ /**
9+ * Class for managing a priority queue.
10+ * @constructor
11+ */
12+ function PriorityQueue ( ) {
13+ /**
14+ * The list of the items in the queue.
15+ * @type {RBTreeList }
16+ */
17+ this . items = new RBTreeList ( ) ;
18+ /**
19+ * The length of the queue.
20+ * @type {number }
21+ */
22+ this . length = 0 ;
23+ }
24+
25+ /**
26+ * @inheritDoc
27+ */
28+ PriorityQueue . prototype . getIterator = function ( ) {
29+ return new PriorityQueueIterator ( this ) ;
30+ } ;
31+
32+ /**
33+ * Add the item at the tail of the queue.
34+ * @param priority {number} The priority of the item.
35+ * @param item {*} The item to add.
36+ * @return {void }
37+ */
38+ PriorityQueue . prototype . enqueue = function ( priority , item ) {
39+ var queue = this . items . search ( priority ) ;
40+ if ( ! queue ) {
41+ queue = new Queue ( ) ;
42+ this . items . insert ( priority , queue ) ;
43+ }
44+ queue . enqueue ( item ) ;
45+ this . length ++ ;
46+ } ;
47+
48+ /**
49+ * Adds the items with the same priority at the tail of the queue.
50+ * @param priority {number} The priority of the items.
51+ * @param items {Array<*>} The items to add.
52+ * @return {void }
53+ */
54+ PriorityQueue . prototype . multiEnqueue = function ( priority , items ) {
55+ for ( var i = 0 ; i < items . length ; i ++ )
56+ this . enqueue ( priority , items [ i ] ) ;
57+ } ;
58+
59+ /**
60+ * Remove the item at the head of the queue.
61+ * @return {* } The item at the head of the queue. It's undefined if the queue is empty.
62+ */
63+ PriorityQueue . prototype . dequeue = function ( ) {
64+ var node = this . items . maximum ( ) ;
65+ var item = undefined ;
66+ if ( node ) {
67+ var queue = node . item ;
68+ item = queue . dequeue ( ) ;
69+ if ( queue . isEmpty ( ) )
70+ this . items . deleteNode ( node ) ;
71+ this . length -- ;
72+ }
73+ return item ;
74+ } ;
75+
76+ /**
77+ * Removes the items at the head of the queue.
78+ * @param times {number} The number of times to repeat the dequeue method.
79+ * @return {Array<*> } The items at the head of the queue.
80+ */
81+ PriorityQueue . prototype . multiDequeue = function ( times ) {
82+ var items = [ ] ;
83+ for ( var i = 0 ; i < times && this . length ; i ++ )
84+ items . push ( this . dequeue ( ) ) ;
85+ return items ;
86+ } ;
87+
88+ /**
89+ * Removes the first length items from the position index.
90+ * @param index {number} The position where to start to remove the items.
91+ * @param [length = 1] {number} The number of items to remove.
92+ * @return {void }
93+ */
94+ PriorityQueue . prototype . remove = function ( index , length ) {
95+ length = length || 1 ;
96+ var it = this . items . getIterator ( ) ;
97+ for ( it . last ( ) ; ! it . isDone ( ) && length > 0 ; it . previous ( ) ) {
98+ var queue = it . getItem ( ) ;
99+ if ( index > - 1 && index < queue . getLength ( ) ) {
100+ var oldLength = queue . getLength ( ) ;
101+ queue . remove ( index , length ) ;
102+ length -= oldLength - index ;
103+ index = 0 ;
104+ if ( ! queue . getLength ( ) )
105+ this . items . deleteNode ( it . getNode ( ) ) ;
106+ } else
107+ index = index - queue . getLength ( ) ;
108+ }
109+ } ;
110+
111+ /**
112+ * Return the item at the position index.
113+ * @param index {number} The index of the item.
114+ * @return {* } The item found. It's undefined if the position index is out of bounds.
115+ */
116+ PriorityQueue . prototype . getItem = function ( index ) {
117+ var it = this . items . getIterator ( ) ;
118+ for ( it . last ( ) ; ! it . isDone ( ) ; it . previous ( ) ) {
119+ var queue = it . getItem ( ) ;
120+ if ( index > - 1 && index < queue . getLength ( ) )
121+ return queue . getItem ( index ) ;
122+ index = index - queue . getLength ( ) ;
123+ }
124+ return undefined ;
125+ } ;
126+
127+ /**
128+ * Return the items relatives to the priority.
129+ * @param priority {number} The priority of the items.
130+ * @return {Array<*> } The items found.
131+ */
132+ PriorityQueue . prototype . getItems = function ( priority ) {
133+ var items = this . items . search ( priority ) ;
134+ if ( items )
135+ return items . items ;
136+ return [ ] ;
137+ } ;
138+
139+ /**
140+ * Return the first item in the queue. The item is not removed.
141+ * @return {* } The first item. It's undefined if the queue is empty.
142+ */
143+ PriorityQueue . prototype . peek = function ( ) {
144+ return this . items . maximum ( ) . item . peek ( ) ;
145+ } ;
146+
147+ /**
148+ * Return the length of the queue.
149+ * @return {number } The length of the queue.
150+ */
151+ PriorityQueue . prototype . getLength = function ( ) {
152+ return this . length ;
153+ } ;
154+
155+ /**
156+ * Checks if the queue is empty.
157+ * @return {boolean } True if the queue is empty, false otherwise.
158+ */
159+ PriorityQueue . prototype . isEmpty = function ( ) {
160+ return ! this . length ;
161+ } ;
162+
163+ /**
164+ * Removes all the items stored in the queue.
165+ * @return {void }
166+ */
167+ PriorityQueue . prototype . clear = function ( ) {
168+ this . items = new RBTreeList ( ) ;
169+ this . length = 0 ;
170+ } ;
171+
172+ /**
173+ * Checks if the queue contains a priority that satisfy the condition represented by the callback function.
174+ * @param priority {number} The priority to find.
175+ * @param [callback = function(p){return(p===priority);}] The condition to satisfy. The callback must accept the current priority to check.
176+ * @return {boolean } True if the queue contains the priority that satisfy the condition, false otherwise.
177+ */
178+ PriorityQueue . prototype . containsPriority = function ( priority , callback ) {
179+ if ( callback )
180+ return this . items . fullContains ( callback ) ;
181+ else
182+ return this . items . contains ( priority ) ;
183+ } ;
184+
185+ /**
186+ * Return the queue created by the priority queue with the items in the same order but without the priority.
187+ * @return {Queue } The queue created.
188+ */
189+ PriorityQueue . prototype . toQueue = function ( ) {
190+ var queue = new Queue ( ) ;
191+ var it = this . items . getIterator ( ) ;
192+ for ( it . last ( ) ; ! it . isDone ( ) ; it . previous ( ) ) {
193+ var item = it . getItem ( ) ;
194+ var itQ = item . getIterator ( ) ;
195+ for ( itQ . first ( ) ; ! itQ . isDone ( ) ; itQ . next ( ) )
196+ queue . enqueue ( itQ . getItem ( ) ) ;
197+ }
198+ return queue ;
199+ } ;
200+
201+ /**
202+ * Executes the callback function for each item of the queue.
203+ * This method modifies the queue so if you don't need to modify it you must return the same item of the array.
204+ * @param callback {function} The function to execute for each item. The function must accept the current item on which execute the function.
205+ * @return {void }
206+ */
207+ PriorityQueue . prototype . execute = function ( callback ) {
208+ var it = this . items . getIterator ( ) ;
209+ for ( it . last ( ) ; ! it . isDone ( ) ; it . previous ( ) )
210+ it . getItem ( ) . execute ( callback ) ;
211+ } ;
212+
213+ /**
214+ * Change the priority of the item at the position index.
215+ * @param index {number} The position of the item of which increase the priority.
216+ * @param newPriority {number} The new priority.
217+ * @return {void }
218+ */
219+ PriorityQueue . prototype . changePriority = function ( index , newPriority ) {
220+ var item = this . getItem ( index ) ;
221+ this . remove ( index ) ;
222+ this . enqueue ( newPriority , item ) ;
223+ } ;
224+
225+ /**
226+ * Returns the items that satisfy the condition determined by the callback.
227+ * @param callback {function} The function that implements the condition.
228+ * @return {Array<*> } The array that contains the items that satisfy the condition.
229+ */
230+ PriorityQueue . prototype . filter = function ( callback ) {
231+ var result = [ ] ;
232+ var it = this . items . getIterator ( ) ;
233+ for ( it . last ( ) ; ! it . isDone ( ) ; it . previous ( ) ) {
234+ var itQ = it . getItem ( ) . getIterator ( ) ;
235+ for ( itQ . first ( ) ; ! itQ . isDone ( ) ; itQ . next ( ) ) {
236+ if ( callback ( itQ . getItem ( ) ) )
237+ result . push ( itQ . getItem ( ) ) ;
238+ }
239+ }
240+ return result ;
241+ } ;
242+
243+ /**
244+ * Clones the queue into a new queue.
245+ * @return {PriorityQueue } The queue cloned from this queue.
246+ */
247+ PriorityQueue . prototype . clone = function ( ) {
248+ var queue = new PriorityQueue ( ) ;
249+ queue . items = this . items . clone ( ) ;
250+ queue . length = this . length ;
251+ return queue ;
252+ } ;
253+
254+ /**
255+ * Clones the queue into a new queue without cloning duplicated items.
256+ * @return {PriorityQueue } The queue cloned from this queue.
257+ */
258+ PriorityQueue . prototype . cloneDistinct = function ( ) {
259+ var queue = new PriorityQueue ( ) ;
260+ queue . items = this . items . cloneDistinct ( ) ;
261+ queue . length = queue . items . getSize ( ) ;
262+ return queue ;
263+ } ;
0 commit comments