00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include <priorityqueue.h>
00021 #include <kdebug.h>
00022 #include <q3ptrlist.h>
00023 #include <q3asciidict.h>
00024 #include <stdlib.h>
00025 #include <time.h>
00026
00027 struct Node {
00028 Node( unsigned int key ) : m_key( key ), m_index( 0 ) {}
00029
00030 unsigned int key() const { return m_key; }
00031 void setKey( unsigned int key ) { m_key = key; }
00032
00033 int index() const { return m_index; }
00034 void setIndex( int i ) { m_index = i; }
00035 private:
00036 unsigned int m_key;
00037 int m_index;
00038 };
00039
00040 static const char* const keys[] = { "one", "two", "three", "four", "five",
00041 "six", "seven", "eight", "nine", "ten",
00042 "eleven", "twelve", 0 };
00043
00044 int main( int , char ** )
00045 {
00046 Q3PtrList<Node> list;
00047 list.setAutoDelete( true );
00048 Q3AsciiDict<Node> dict;
00049
00050 KOffice::PriorityQueue<Node> queue;
00051
00052 srand( time( 0 ) );
00053 for ( int i = 0; i < 12; ++i ) {
00054 Node *n = new Node( rand() % 20 );
00055 list.append( n );
00056 queue.insert( n );
00057
00058 Node *n2 = new Node( *n );
00059 dict.insert( keys[ i ], n2 );
00060 }
00061
00062 kDebug() << "##### Queue 1: " << endl;
00063 queue.dump();
00064
00065 kDebug() << "##### Queue 2: " << endl;
00066 KOffice::PriorityQueue<Node> queue2( dict );
00067 queue2.dump();
00068
00069 Node *n = list.at( 6 );
00070 kDebug() << "##### Decreasing node: " << n->key() << " at " << n->index() << endl;
00071 n->setKey( 2 );
00072 queue.keyDecreased( n );
00073 queue.dump();
00074
00075 n = list.at( 2 );
00076 kDebug() << "##### Decreasing node: " << n->key() << " at " << n->index() << endl;
00077 n->setKey( 0 );
00078 queue.keyDecreased( n );
00079 queue.dump();
00080
00081 n = queue.extractMinimum();
00082 while ( n ) {
00083 queue.dump();
00084 n = queue.extractMinimum();
00085 }
00086 return 0;
00087 }