F:/KPlato/koffice/libs/kofficecore/tests/priorityqueue_test.cpp

Aller à la documentation de ce fichier.
00001 /* This file is part of the KDE project
00002    Copyright (C) 2001 Werner Trobin <trobin@kde.org>
00003 
00004    This library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Library General Public
00006    License as published by the Free Software Foundation; either
00007    version 2 of the License, or (at your option) any later version.
00008 
00009    This library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Library General Public License for more details.
00013 
00014    You should have received a copy of the GNU Library General Public License
00015    along with this library; see the file COPYING.LIB.  If not, write to
00016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00017  * Boston, MA 02110-1301, USA.
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 /*argc*/, char **/*argv*/ )
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         // Check whether the AsciiDict CTOR is okay
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 }

Généré le Wed Nov 22 23:41:04 2006 pour KPlato par  doxygen 1.5.1-p1