| 1 | /**************************************************************************** | 
|---|
| 2 | ** $Id: qglist.h 2 2005-11-16 15:49:26Z dmik $ | 
|---|
| 3 | ** | 
|---|
| 4 | ** Definition of QGList and QGListIterator classes | 
|---|
| 5 | ** | 
|---|
| 6 | ** Created : 920624 | 
|---|
| 7 | ** | 
|---|
| 8 | ** Copyright (C) 1992-2000 Trolltech AS.  All rights reserved. | 
|---|
| 9 | ** | 
|---|
| 10 | ** This file is part of the tools module of the Qt GUI Toolkit. | 
|---|
| 11 | ** | 
|---|
| 12 | ** This file may be distributed under the terms of the Q Public License | 
|---|
| 13 | ** as defined by Trolltech AS of Norway and appearing in the file | 
|---|
| 14 | ** LICENSE.QPL included in the packaging of this file. | 
|---|
| 15 | ** | 
|---|
| 16 | ** This file may be distributed and/or modified under the terms of the | 
|---|
| 17 | ** GNU General Public License version 2 as published by the Free Software | 
|---|
| 18 | ** Foundation and appearing in the file LICENSE.GPL included in the | 
|---|
| 19 | ** packaging of this file. | 
|---|
| 20 | ** | 
|---|
| 21 | ** Licensees holding valid Qt Enterprise Edition or Qt Professional Edition | 
|---|
| 22 | ** licenses may use this file in accordance with the Qt Commercial License | 
|---|
| 23 | ** Agreement provided with the Software. | 
|---|
| 24 | ** | 
|---|
| 25 | ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE | 
|---|
| 26 | ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. | 
|---|
| 27 | ** | 
|---|
| 28 | ** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for | 
|---|
| 29 | **   information about Qt Commercial License Agreements. | 
|---|
| 30 | ** See http://www.trolltech.com/qpl/ for QPL licensing information. | 
|---|
| 31 | ** See http://www.trolltech.com/gpl/ for GPL licensing information. | 
|---|
| 32 | ** | 
|---|
| 33 | ** Contact info@trolltech.com if any conditions of this licensing are | 
|---|
| 34 | ** not clear to you. | 
|---|
| 35 | ** | 
|---|
| 36 | **********************************************************************/ | 
|---|
| 37 |  | 
|---|
| 38 | #ifndef QGLIST_H | 
|---|
| 39 | #define QGLIST_H | 
|---|
| 40 |  | 
|---|
| 41 | #ifndef QT_H | 
|---|
| 42 | #include "qptrcollection.h" | 
|---|
| 43 | #endif // QT_H | 
|---|
| 44 |  | 
|---|
| 45 | class Q_EXPORT QLNode | 
|---|
| 46 | { | 
|---|
| 47 | friend class QGList; | 
|---|
| 48 | friend class QGListIterator; | 
|---|
| 49 | friend class QGListStdIterator; | 
|---|
| 50 | public: | 
|---|
| 51 | QPtrCollection::Item getData()      { return data; } | 
|---|
| 52 | private: | 
|---|
| 53 | QPtrCollection::Item data; | 
|---|
| 54 | QLNode *prev; | 
|---|
| 55 | QLNode *next; | 
|---|
| 56 | QLNode( QPtrCollection::Item d ) { data = d; } | 
|---|
| 57 | }; | 
|---|
| 58 |  | 
|---|
| 59 | class QGListIteratorList; // internal helper class | 
|---|
| 60 |  | 
|---|
| 61 | class Q_EXPORT QGList : public QPtrCollection   // doubly linked generic list | 
|---|
| 62 | { | 
|---|
| 63 | friend class QGListIterator; | 
|---|
| 64 | friend class QGListIteratorList; | 
|---|
| 65 | friend class QGVector;                          // needed by QGVector::toList | 
|---|
| 66 | public: | 
|---|
| 67 | uint  count() const;                        // return number of nodes | 
|---|
| 68 |  | 
|---|
| 69 | #ifndef QT_NO_DATASTREAM | 
|---|
| 70 | QDataStream &read( QDataStream & );         // read list from stream | 
|---|
| 71 | QDataStream &write( QDataStream & ) const;  // write list to stream | 
|---|
| 72 | #endif | 
|---|
| 73 | protected: | 
|---|
| 74 | QGList();                                   // create empty list | 
|---|
| 75 | QGList( const QGList & );                   // make copy of other list | 
|---|
| 76 | virtual ~QGList(); | 
|---|
| 77 |  | 
|---|
| 78 | QGList &operator=( const QGList & );        // assign from other list | 
|---|
| 79 | bool operator==( const QGList& ) const; | 
|---|
| 80 |  | 
|---|
| 81 | void inSort( QPtrCollection::Item );                // add item sorted in list | 
|---|
| 82 | void append( QPtrCollection::Item );                // add item at end of list | 
|---|
| 83 | bool insertAt( uint index, QPtrCollection::Item ); // add item at i'th position | 
|---|
| 84 | void relinkNode( QLNode * );                // relink as first item | 
|---|
| 85 | bool removeNode( QLNode * );                // remove node | 
|---|
| 86 | bool remove( QPtrCollection::Item = 0 );    // remove item (0=current) | 
|---|
| 87 | bool removeRef( QPtrCollection::Item = 0 ); // remove item (0=current) | 
|---|
| 88 | bool removeFirst();                         // remove first item | 
|---|
| 89 | bool removeLast();                          // remove last item | 
|---|
| 90 | bool removeAt( uint );                      // remove item at i'th position | 
|---|
| 91 | bool replaceAt( uint, QPtrCollection::Item ); // replace item at position i with item | 
|---|
| 92 | QPtrCollection::Item takeNode( QLNode * );  // take out node | 
|---|
| 93 | QPtrCollection::Item take();                // take out current item | 
|---|
| 94 | QPtrCollection::Item takeAt( uint index );  // take out item at i'th pos | 
|---|
| 95 | QPtrCollection::Item takeFirst();           // take out first item | 
|---|
| 96 | QPtrCollection::Item takeLast();            // take out last item | 
|---|
| 97 |  | 
|---|
| 98 | void sort();                                // sort all items; | 
|---|
| 99 | void clear();                               // remove all items | 
|---|
| 100 |  | 
|---|
| 101 | int  findRef( QPtrCollection::Item, bool = TRUE ); // find exact item in list | 
|---|
| 102 | int  find( QPtrCollection::Item, bool = TRUE ); // find equal item in list | 
|---|
| 103 |  | 
|---|
| 104 | uint containsRef( QPtrCollection::Item ) const;     // get number of exact matches | 
|---|
| 105 | uint contains( QPtrCollection::Item ) const;        // get number of equal matches | 
|---|
| 106 |  | 
|---|
| 107 | QPtrCollection::Item at( uint index );      // access item at i'th pos | 
|---|
| 108 | int   at() const;                           // get current index | 
|---|
| 109 | QLNode *currentNode() const;                // get current node | 
|---|
| 110 |  | 
|---|
| 111 | QPtrCollection::Item get() const;           // get current item | 
|---|
| 112 |  | 
|---|
| 113 | QPtrCollection::Item cfirst() const;        // get ptr to first list item | 
|---|
| 114 | QPtrCollection::Item clast()  const;        // get ptr to last list item | 
|---|
| 115 | QPtrCollection::Item first();               // set first item in list curr | 
|---|
| 116 | QPtrCollection::Item last();                // set last item in list curr | 
|---|
| 117 | QPtrCollection::Item next();                // set next item in list curr | 
|---|
| 118 | QPtrCollection::Item prev();                // set prev item in list curr | 
|---|
| 119 |  | 
|---|
| 120 | void  toVector( QGVector * ) const;         // put items in vector | 
|---|
| 121 |  | 
|---|
| 122 | virtual int compareItems( QPtrCollection::Item, QPtrCollection::Item ); | 
|---|
| 123 |  | 
|---|
| 124 | #ifndef QT_NO_DATASTREAM | 
|---|
| 125 | virtual QDataStream &read( QDataStream &, QPtrCollection::Item & ); | 
|---|
| 126 | virtual QDataStream &write( QDataStream &, QPtrCollection::Item ) const; | 
|---|
| 127 | #endif | 
|---|
| 128 |  | 
|---|
| 129 | QLNode* begin() const { return firstNode; } | 
|---|
| 130 | QLNode* end() const { return 0; } | 
|---|
| 131 | QLNode* erase( QLNode* it ); | 
|---|
| 132 |  | 
|---|
| 133 | private: | 
|---|
| 134 | void  prepend( QPtrCollection::Item );      // add item at start of list | 
|---|
| 135 |  | 
|---|
| 136 | void heapSortPushDown( QPtrCollection::Item* heap, int first, int last ); | 
|---|
| 137 |  | 
|---|
| 138 | QLNode *firstNode;                          // first node | 
|---|
| 139 | QLNode *lastNode;                           // last node | 
|---|
| 140 | QLNode *curNode;                            // current node | 
|---|
| 141 | int curIndex;                               // current index | 
|---|
| 142 | uint numNodes;                              // number of nodes | 
|---|
| 143 | QGListIteratorList *iterators;              // list of iterators | 
|---|
| 144 |  | 
|---|
| 145 | QLNode *locate( uint );                     // get node at i'th pos | 
|---|
| 146 | QLNode *unlink();                           // unlink node | 
|---|
| 147 | }; | 
|---|
| 148 |  | 
|---|
| 149 |  | 
|---|
| 150 | inline uint QGList::count() const | 
|---|
| 151 | { | 
|---|
| 152 | return numNodes; | 
|---|
| 153 | } | 
|---|
| 154 |  | 
|---|
| 155 | inline bool QGList::removeFirst() | 
|---|
| 156 | { | 
|---|
| 157 | first(); | 
|---|
| 158 | return remove(); | 
|---|
| 159 | } | 
|---|
| 160 |  | 
|---|
| 161 | inline bool QGList::removeLast() | 
|---|
| 162 | { | 
|---|
| 163 | last(); | 
|---|
| 164 | return remove(); | 
|---|
| 165 | } | 
|---|
| 166 |  | 
|---|
| 167 | inline int QGList::at() const | 
|---|
| 168 | { | 
|---|
| 169 | return curIndex; | 
|---|
| 170 | } | 
|---|
| 171 |  | 
|---|
| 172 | inline QPtrCollection::Item QGList::at( uint index ) | 
|---|
| 173 | { | 
|---|
| 174 | QLNode *n = locate( index ); | 
|---|
| 175 | return n ? n->data : 0; | 
|---|
| 176 | } | 
|---|
| 177 |  | 
|---|
| 178 | inline QLNode *QGList::currentNode() const | 
|---|
| 179 | { | 
|---|
| 180 | return curNode; | 
|---|
| 181 | } | 
|---|
| 182 |  | 
|---|
| 183 | inline QPtrCollection::Item QGList::get() const | 
|---|
| 184 | { | 
|---|
| 185 | return curNode ? curNode->data : 0; | 
|---|
| 186 | } | 
|---|
| 187 |  | 
|---|
| 188 | inline QPtrCollection::Item QGList::cfirst() const | 
|---|
| 189 | { | 
|---|
| 190 | return firstNode ? firstNode->data : 0; | 
|---|
| 191 | } | 
|---|
| 192 |  | 
|---|
| 193 | inline QPtrCollection::Item QGList::clast() const | 
|---|
| 194 | { | 
|---|
| 195 | return lastNode ? lastNode->data : 0; | 
|---|
| 196 | } | 
|---|
| 197 |  | 
|---|
| 198 |  | 
|---|
| 199 | /***************************************************************************** | 
|---|
| 200 | QGList stream functions | 
|---|
| 201 | *****************************************************************************/ | 
|---|
| 202 |  | 
|---|
| 203 | #ifndef QT_NO_DATASTREAM | 
|---|
| 204 | Q_EXPORT QDataStream &operator>>( QDataStream &, QGList & ); | 
|---|
| 205 | Q_EXPORT QDataStream &operator<<( QDataStream &, const QGList & ); | 
|---|
| 206 | #endif | 
|---|
| 207 |  | 
|---|
| 208 | /***************************************************************************** | 
|---|
| 209 | QGListIterator class | 
|---|
| 210 | *****************************************************************************/ | 
|---|
| 211 |  | 
|---|
| 212 | class Q_EXPORT QGListIterator                   // QGList iterator | 
|---|
| 213 | { | 
|---|
| 214 | friend class QGList; | 
|---|
| 215 | friend class QGListIteratorList; | 
|---|
| 216 | protected: | 
|---|
| 217 | QGListIterator( const QGList & ); | 
|---|
| 218 | QGListIterator( const QGListIterator & ); | 
|---|
| 219 | QGListIterator &operator=( const QGListIterator & ); | 
|---|
| 220 | ~QGListIterator(); | 
|---|
| 221 |  | 
|---|
| 222 | bool  atFirst() const;                      // test if at first item | 
|---|
| 223 | bool  atLast()  const;                      // test if at last item | 
|---|
| 224 | QPtrCollection::Item          toFirst();                            // move to first item | 
|---|
| 225 | QPtrCollection::Item          toLast();                             // move to last item | 
|---|
| 226 |  | 
|---|
| 227 | QPtrCollection::Item          get() const;                          // get current item | 
|---|
| 228 | QPtrCollection::Item          operator()();                         // get current and move to next | 
|---|
| 229 | QPtrCollection::Item          operator++();                         // move to next item (prefix) | 
|---|
| 230 | QPtrCollection::Item          operator+=(uint);                     // move n positions forward | 
|---|
| 231 | QPtrCollection::Item          operator--();                         // move to prev item (prefix) | 
|---|
| 232 | QPtrCollection::Item          operator-=(uint);                     // move n positions backward | 
|---|
| 233 |  | 
|---|
| 234 | protected: | 
|---|
| 235 | QGList *list;                               // reference to list | 
|---|
| 236 |  | 
|---|
| 237 | private: | 
|---|
| 238 | QLNode  *curNode;                           // current node in list | 
|---|
| 239 | }; | 
|---|
| 240 |  | 
|---|
| 241 |  | 
|---|
| 242 | inline bool QGListIterator::atFirst() const | 
|---|
| 243 | { | 
|---|
| 244 | return curNode == list->firstNode; | 
|---|
| 245 | } | 
|---|
| 246 |  | 
|---|
| 247 | inline bool QGListIterator::atLast() const | 
|---|
| 248 | { | 
|---|
| 249 | return curNode == list->lastNode; | 
|---|
| 250 | } | 
|---|
| 251 |  | 
|---|
| 252 | inline QPtrCollection::Item QGListIterator::get() const | 
|---|
| 253 | { | 
|---|
| 254 | return curNode ? curNode->data : 0; | 
|---|
| 255 | } | 
|---|
| 256 |  | 
|---|
| 257 | class Q_EXPORT QGListStdIterator | 
|---|
| 258 | { | 
|---|
| 259 | public: | 
|---|
| 260 | inline QGListStdIterator( QLNode* n ) : node( n ){} | 
|---|
| 261 | inline operator QLNode* () { return node; } | 
|---|
| 262 | protected: | 
|---|
| 263 | inline QLNode *next() { return node->next; } | 
|---|
| 264 | QLNode *node; | 
|---|
| 265 | }; | 
|---|
| 266 |  | 
|---|
| 267 |  | 
|---|
| 268 | #endif  // QGLIST_H | 
|---|