18 #ifndef _DECAF_UTIL_PRIORITYQUEUE_H_
19 #define _DECAF_UTIL_PRIORITYQUEUE_H_
77 template<
typename E >
109 "No more elements to Iterate over." );
113 return queue->elements[position++];
116 virtual bool hasNext()
const {
117 return position < queue->_size;
120 virtual void remove() {
125 "No more elements to Iterate over." );
129 queue->removeAt( position-- );
136 ConstPriorityQueueIterator(
const ConstPriorityQueueIterator& );
137 ConstPriorityQueueIterator&
operator= (
const ConstPriorityQueueIterator& );
144 virtual void remove() {
147 "PriorityQueue::Iterator::remove - Not Valid on a Const Iterator" );
189 if( comparator ==
NULL ) {
191 __FILE__, __LINE__,
"Passed Comparator Cannot be NULL." );
194 this->initQueue( initialCapacity, comparator );
206 this->getFromCollection( source );
219 this->getFromPriorityQueue( source );
233 this->getFromCollection( source );
244 this->getFromPriorityQueue( source );
255 return new ConstPriorityQueueIterator(
this );
268 delete [] this->elements;
275 virtual bool offer(
const E& value ) {
279 increaseCapacity( _size + 1 );
280 elements[_size++] = value;
285 virtual bool poll( E& result ) {
291 result = elements[0];
296 virtual bool peek( E& result )
const {
302 result = elements[0];
309 E result = elements[0];
315 __FILE__, __LINE__,
"Unable to remove specified element from the Queue." );
318 virtual bool remove(
const E& value ) {
321 for( targetIndex = 0; targetIndex < _size; targetIndex++ ) {
322 if( 0 == _comparator->compare( value, elements[targetIndex] ) ) {
327 if( _size == 0 || _size == targetIndex ) {
331 removeAt( targetIndex );
335 virtual bool add(
const E& value ) {
338 return offer( value );
355 return this->_comparator;
361 this->elements =
new E[initialSize];
362 this->capacity = initialSize;
364 this->_comparator.
reset( comparator );
369 int current = _size - 1;
370 int parent = ( current - 1 ) / 2;
372 while( current != 0 && _comparator->compare( elements[current], elements[parent] ) < 0 ) {
375 E tmp = elements[current];
376 elements[current] = elements[parent];
377 elements[parent] = tmp;
381 parent = ( current - 1 ) / 2;
385 void downHeap(
int pos ) {
388 int child = 2 * current + 1;
390 while( child < _size && !this->
isEmpty() ) {
393 if( child + 1 < _size && _comparator->compare( elements[child + 1], elements[child] ) < 0 ) {
398 if( _comparator->compare( elements[current], elements[child] ) < 0 ) {
403 E tmp = elements[current];
404 elements[current] = elements[child];
405 elements[child] = tmp;
409 child = 2 * current + 1;
413 void getFromPriorityQueue(
const PriorityQueue<E>& c ) {
415 _comparator = c.comparator();
416 for(
int ix = 0; ix < c.size(); ++ix ) {
417 this->elements[ix] = c.elements[ix];
422 void getFromCollection(
const Collection<E>& c ) {
424 _comparator.
reset(
new comparators::Less<E>() );
425 std::auto_ptr< Iterator<E> > iter( c.iterator() );
426 while( iter->hasNext() ) {
427 this->
offer( iter->next() );
431 void removeAt(
int index ) {
433 elements[index] = elements[_size];
435 elements[_size] = E();
438 void initCapacity(
const Collection<E>& c ) {
445 elements =
new E[capacity];
448 elements =
new E[capacity];
452 void increaseCapacity(
int size ) {
454 if( size > capacity ) {
457 for(
int ix = 0; ix < capacity; ix++ ) {
458 newElements[ix] = elements[ix];
463 elements = newElements;