18 #ifndef _DECAF_UTIL_PRIORITYQUEUE_H_ 19 #define _DECAF_UTIL_PRIORITYQUEUE_H_ 77 template<
typename E >
88 class PriorityQueueIterator :
public Iterator<E> {
91 PriorityQueueIterator(
const PriorityQueueIterator& );
92 PriorityQueueIterator& operator= (
const PriorityQueueIterator& );
102 PriorityQueueIterator(
PriorityQueue* queue ) : position( 0 ), allowRemove(
false ), queue( queue ) {}
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-- );
133 class ConstPriorityQueueIterator :
public PriorityQueueIterator {
136 ConstPriorityQueueIterator(
const ConstPriorityQueueIterator& );
137 ConstPriorityQueueIterator& operator= (
const ConstPriorityQueueIterator& );
142 PriorityQueueIterator( const_cast<PriorityQueue*>( queue ) ) {}
144 virtual void remove() {
147 "PriorityQueue::Iterator::remove - Not Valid on a Const Iterator" );
151 friend class PriorityQueueIterator;
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 );
251 return new PriorityQueueIterator(
this );
255 return new ConstPriorityQueueIterator(
this );
268 delete [] this->elements;
270 this->elements =
new E[DEFAULT_CAPACITY];
271 this->capacity = DEFAULT_CAPACITY;
275 virtual bool offer(
const E& value ) {
279 increaseCapacity( _size + 1 );
280 elements[_size++] = value;
285 virtual bool poll( E& result ) {
287 if( this->isEmpty() ) {
291 result = elements[0];
296 virtual bool peek( E& result )
const {
298 if( this->isEmpty() ) {
302 result = elements[0];
308 if( !this->isEmpty() ) {
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;
360 void initQueue(
int initialSize,
Comparator<E>* 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;
416 for(
int ix = 0; ix < c.
size(); ++ix ) {
417 this->elements[ix] = c.elements[ix];
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();
445 elements =
new E[capacity];
448 elements =
new E[capacity];
452 void increaseCapacity(
int size ) {
454 if( size > capacity ) {
455 E* newElements =
new E[ size * DEFAULT_CAPACITY_RATIO ];
457 for(
int ix = 0; ix < capacity; ix++ ) {
458 newElements[ix] = elements[ix];
463 elements = newElements;
464 capacity = size * DEFAULT_CAPACITY_RATIO;
void reset(T *value=NULL)
Resets the Pointer to hold the new value.
Definition: Pointer.h:161
virtual bool offer(const E &value)
Inserts the specified element into the queue provided that the condition allows such an operation...
Definition: PriorityQueue.h:275
The root interface in the collection hierarchy.
Definition: Collection.h:68
#define DECAF_CATCH_RETHROW(type)
Macro for catching and rethrowing an exception of a given type.
Definition: ExceptionDefines.h:27
virtual bool poll(E &result)
Gets and removes the element in the head of the queue.
Definition: PriorityQueue.h:285
An unbounded priority queue based on a binary heap algorithm.
Definition: PriorityQueue.h:78
virtual decaf::util::Iterator< E > * iterator()
Definition: PriorityQueue.h:250
#define NULL
Definition: Config.h:33
virtual bool isEmpty() const =0
decaf::lang::Pointer< Comparator< E > > comparator() const
obtains a Copy of the Pointer instance that this PriorityQueue is using to compare the elements in th...
Definition: PriorityQueue.h:354
PriorityQueue()
Creates a Priority Queue with the default initial capacity.
Definition: PriorityQueue.h:158
virtual int size() const =0
Returns the number of elements in this collection.
virtual void clear()
Removes all of the elements from this collection (optional operation).The collection will be empty af...
Definition: PriorityQueue.h:262
virtual ~PriorityQueueBase()
Definition: PriorityQueue.h:44
Defines an object that can be used to iterate over the elements of a collection.
Definition: Iterator.h:34
Definition: UnsupportedOperationException.h:32
PriorityQueue(const PriorityQueue< E > &source)
Creates a PriorityQueue containing the elements in the specified priority queue.
Definition: PriorityQueue.h:216
static const int DEFAULT_CAPACITY_RATIO
Definition: PriorityQueue.h:42
virtual decaf::util::Iterator< E > * iterator()=0
virtual bool add(const E &value)
Returns true if this collection changed as a result of the call.(Returns false if this collection doe...
Definition: PriorityQueue.h:335
PriorityQueue(const Collection< E > &source)
Creates a PriorityQueue containing the elements in the specified Collection.
Definition: PriorityQueue.h:203
Simple Less Comparator that compares to elements to determine if the first is less than the second...
Definition: Less.h:37
Definition: IllegalArgumentException.h:31
Definition: IllegalStateException.h:32
static const int DEFAULT_CAPACITY
Definition: PriorityQueue.h:41
PriorityQueue(int initialCapacity)
Creates a Priority Queue with the capacity value supplied.
Definition: PriorityQueue.h:168
PriorityQueue(int initialCapacity, Comparator< E > *comparator)
Creates a Priority Queue with the default initial capacity.
Definition: PriorityQueue.h:186
virtual decaf::util::Iterator< E > * iterator() const
Definition: PriorityQueue.h:254
#define DECAF_CATCHALL_THROW(type)
A catch-all that throws a known exception.
Definition: ExceptionDefines.h:50
Definition: NullPointerException.h:32
#define DECAF_API
Definition: Config.h:29
Definition: NoSuchElementException.h:31
virtual bool peek(E &result) const
Gets but not removes the element in the head of the queue.
Definition: PriorityQueue.h:296
Decaf's implementation of a Smart Pointer that is a template on a Type and is Thread Safe if the defa...
Definition: Pointer.h:53
Definition: PriorityQueue.h:38
virtual ~PriorityQueue()
Definition: PriorityQueue.h:222
static double ceil(double value)
Returns the natural logarithm (base e) of a double value.
virtual int size() const
Returns the number of elements in this collection.
Definition: PriorityQueue.h:258
This class provides skeletal implementations of some Queue operations.
Definition: AbstractQueue.h:47
Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements...
Definition: AprPool.h:25
#define DECAF_CATCH_EXCEPTION_CONVERT(sourceType, targetType)
Macro for catching an exception of one type and then rethrowing as another type.
Definition: ExceptionDefines.h:39