src/qt3support/tools/q3glist.h
changeset 0 1918ee327afb
child 4 3b1da2848fc7
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the Qt3Support module of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 #ifndef Q3GLIST_H
       
    43 #define Q3GLIST_H
       
    44 
       
    45 #include <Qt3Support/q3ptrcollection.h>
       
    46 
       
    47 QT_BEGIN_HEADER
       
    48 
       
    49 QT_BEGIN_NAMESPACE
       
    50 
       
    51 QT_MODULE(Qt3SupportLight)
       
    52 
       
    53 class Q_COMPAT_EXPORT Q3LNode
       
    54 {
       
    55 friend class Q3GList;
       
    56 friend class Q3GListIterator;
       
    57 friend class Q3GListStdIterator;
       
    58 public:
       
    59     Q3PtrCollection::Item getData()	{ return data; }
       
    60 private:
       
    61     Q3PtrCollection::Item data;
       
    62     Q3LNode *prev;
       
    63     Q3LNode *next;
       
    64     Q3LNode( Q3PtrCollection::Item d ) { data = d; }
       
    65 };
       
    66 
       
    67 class Q3GListIteratorList; // internal helper class
       
    68 
       
    69 class Q_COMPAT_EXPORT Q3GList : public Q3PtrCollection	// doubly linked generic list
       
    70 {
       
    71 friend class Q3GListIterator;
       
    72 friend class Q3GListIteratorList;
       
    73 friend class Q3GVector;				// needed by Q3GVector::toList
       
    74 public:
       
    75     uint  count() const;			// return number of nodes
       
    76 
       
    77 #ifndef QT_NO_DATASTREAM
       
    78     QDataStream &read( QDataStream & );		// read list from stream
       
    79     QDataStream &write( QDataStream & ) const;	// write list to stream
       
    80 #endif
       
    81 protected:
       
    82     Q3GList();					// create empty list
       
    83     Q3GList( const Q3GList & );			// make copy of other list
       
    84     virtual ~Q3GList();
       
    85 
       
    86     Q3GList &operator=( const Q3GList & );	// assign from other list
       
    87     bool operator==( const Q3GList& ) const;
       
    88 
       
    89     void inSort( Q3PtrCollection::Item );		// add item sorted in list
       
    90     void append( Q3PtrCollection::Item );		// add item at end of list
       
    91     bool insertAt( uint index, Q3PtrCollection::Item ); // add item at i'th position
       
    92     void relinkNode( Q3LNode * );		// relink as first item
       
    93     bool removeNode( Q3LNode * );		// remove node
       
    94     bool remove( Q3PtrCollection::Item = 0 );	// remove item (0=current)
       
    95     bool removeRef( Q3PtrCollection::Item = 0 );	// remove item (0=current)
       
    96     bool removeFirst();				// remove first item
       
    97     bool removeLast();				// remove last item
       
    98     bool removeAt( uint );			// remove item at i'th position
       
    99     bool replaceAt( uint, Q3PtrCollection::Item ); // replace item at position i with item
       
   100     Q3PtrCollection::Item takeNode( Q3LNode * );	// take out node
       
   101     Q3PtrCollection::Item take();		// take out current item
       
   102     Q3PtrCollection::Item takeAt( uint index );	// take out item at i'th pos
       
   103     Q3PtrCollection::Item takeFirst();		// take out first item
       
   104     Q3PtrCollection::Item takeLast();		// take out last item
       
   105 
       
   106     void sort();                        	// sort all items;
       
   107     void clear();				// remove all items
       
   108 
       
   109     int	 findRef( Q3PtrCollection::Item, bool = true ); // find exact item in list
       
   110     int	 find( Q3PtrCollection::Item, bool = true ); // find equal item in list
       
   111 
       
   112     uint containsRef( Q3PtrCollection::Item ) const;	// get number of exact matches
       
   113     uint contains( Q3PtrCollection::Item ) const;	// get number of equal matches
       
   114 
       
   115     Q3PtrCollection::Item at( uint index );	// access item at i'th pos
       
   116     int	  at() const;				// get current index
       
   117     Q3LNode *currentNode() const;		// get current node
       
   118 
       
   119     Q3PtrCollection::Item get() const;		// get current item
       
   120 
       
   121     Q3PtrCollection::Item cfirst() const;	// get ptr to first list item
       
   122     Q3PtrCollection::Item clast()  const;	// get ptr to last list item
       
   123     Q3PtrCollection::Item first();		// set first item in list curr
       
   124     Q3PtrCollection::Item last();		// set last item in list curr
       
   125     Q3PtrCollection::Item next();		// set next item in list curr
       
   126     Q3PtrCollection::Item prev();		// set prev item in list curr
       
   127 
       
   128     void  toVector( Q3GVector * ) const;		// put items in vector
       
   129 
       
   130     virtual int compareItems( Q3PtrCollection::Item, Q3PtrCollection::Item );
       
   131 
       
   132 #ifndef QT_NO_DATASTREAM
       
   133     virtual QDataStream &read( QDataStream &, Q3PtrCollection::Item & );
       
   134     virtual QDataStream &write( QDataStream &, Q3PtrCollection::Item ) const;
       
   135 #endif
       
   136 
       
   137     Q3LNode* begin() const { return firstNode; }
       
   138     Q3LNode* end() const { return 0; }
       
   139     Q3LNode* erase( Q3LNode* it );
       
   140 
       
   141 private:
       
   142     void  prepend( Q3PtrCollection::Item );	// add item at start of list
       
   143 
       
   144     void heapSortPushDown( Q3PtrCollection::Item* heap, int first, int last );
       
   145 
       
   146     Q3LNode *firstNode;				// first node
       
   147     Q3LNode *lastNode;				// last node
       
   148     Q3LNode *curNode;				// current node
       
   149     int curIndex;				// current index
       
   150     uint numNodes;				// number of nodes
       
   151     Q3GListIteratorList *iterators; 		// list of iterators
       
   152 
       
   153     Q3LNode *locate( uint );			// get node at i'th pos
       
   154     Q3LNode *unlink();				// unlink node
       
   155 };
       
   156 
       
   157 
       
   158 inline uint Q3GList::count() const
       
   159 {
       
   160     return numNodes;
       
   161 }
       
   162 
       
   163 inline bool Q3GList::removeFirst()
       
   164 {
       
   165     first();
       
   166     return remove();
       
   167 }
       
   168 
       
   169 inline bool Q3GList::removeLast()
       
   170 {
       
   171     last();
       
   172     return remove();
       
   173 }
       
   174 
       
   175 inline int Q3GList::at() const
       
   176 {
       
   177     return curIndex;
       
   178 }
       
   179 
       
   180 inline Q3PtrCollection::Item Q3GList::at( uint index )
       
   181 {
       
   182     Q3LNode *n = locate( index );
       
   183     return n ? n->data : 0;
       
   184 }
       
   185 
       
   186 inline Q3LNode *Q3GList::currentNode() const
       
   187 {
       
   188     return curNode;
       
   189 }
       
   190 
       
   191 inline Q3PtrCollection::Item Q3GList::get() const
       
   192 {
       
   193     return curNode ? curNode->data : 0;
       
   194 }
       
   195 
       
   196 inline Q3PtrCollection::Item Q3GList::cfirst() const
       
   197 {
       
   198     return firstNode ? firstNode->data : 0;
       
   199 }
       
   200 
       
   201 inline Q3PtrCollection::Item Q3GList::clast() const
       
   202 {
       
   203     return lastNode ? lastNode->data : 0;
       
   204 }
       
   205 
       
   206 
       
   207 /*****************************************************************************
       
   208   Q3GList stream functions
       
   209  *****************************************************************************/
       
   210 
       
   211 #ifndef QT_NO_DATASTREAM
       
   212 Q_COMPAT_EXPORT QDataStream &operator>>( QDataStream &, Q3GList & );
       
   213 Q_COMPAT_EXPORT QDataStream &operator<<( QDataStream &, const Q3GList & );
       
   214 #endif
       
   215 
       
   216 /*****************************************************************************
       
   217   Q3GListIterator class
       
   218  *****************************************************************************/
       
   219 
       
   220 class Q_COMPAT_EXPORT Q3GListIterator			// Q3GList iterator
       
   221 {
       
   222 friend class Q3GList;
       
   223 friend class Q3GListIteratorList;
       
   224 protected:
       
   225     Q3GListIterator( const Q3GList & );
       
   226     Q3GListIterator( const Q3GListIterator & );
       
   227     Q3GListIterator &operator=( const Q3GListIterator & );
       
   228    ~Q3GListIterator();
       
   229 
       
   230     bool  atFirst() const;			// test if at first item
       
   231     bool  atLast()  const;			// test if at last item
       
   232     Q3PtrCollection::Item	  toFirst();				// move to first item
       
   233     Q3PtrCollection::Item	  toLast();				// move to last item
       
   234 
       
   235     Q3PtrCollection::Item	  get() const;				// get current item
       
   236     Q3PtrCollection::Item	  operator()();				// get current and move to next
       
   237     Q3PtrCollection::Item	  operator++();				// move to next item (prefix)
       
   238     Q3PtrCollection::Item	  operator+=(uint);			// move n positions forward
       
   239     Q3PtrCollection::Item	  operator--();				// move to prev item (prefix)
       
   240     Q3PtrCollection::Item	  operator-=(uint);			// move n positions backward
       
   241 
       
   242 protected:
       
   243     Q3GList *list;				// reference to list
       
   244 
       
   245 private:
       
   246     Q3LNode  *curNode;				// current node in list
       
   247 };
       
   248 
       
   249 
       
   250 inline bool Q3GListIterator::atFirst() const
       
   251 {
       
   252     return curNode == list->firstNode;
       
   253 }
       
   254 
       
   255 inline bool Q3GListIterator::atLast() const
       
   256 {
       
   257     return curNode == list->lastNode;
       
   258 }
       
   259 
       
   260 inline Q3PtrCollection::Item Q3GListIterator::get() const
       
   261 {
       
   262     return curNode ? curNode->data : 0;
       
   263 }
       
   264 
       
   265 class Q_COMPAT_EXPORT Q3GListStdIterator
       
   266 {
       
   267 public:
       
   268     inline Q3GListStdIterator( Q3LNode* n ) : node( n ){}
       
   269     inline operator Q3LNode* () { return node; }
       
   270 protected:
       
   271     inline Q3LNode *next() { return node->next; }
       
   272     Q3LNode *node;
       
   273 };
       
   274 
       
   275 QT_END_NAMESPACE
       
   276 
       
   277 QT_END_HEADER
       
   278 
       
   279 #endif	// Q3GLIST_H