source: trunk/src/qt3support/tools/q3glist.h@ 104

Last change on this file since 104 was 2, checked in by Dmitry A. Kuminov, 16 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 9.0 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** Contact: Qt Software Information ([email protected])
5**
6** This file is part of the Qt3Support module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial Usage
10** Licensees holding valid Qt Commercial licenses may use this file in
11** accordance with the Qt Commercial License Agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and Nokia.
14**
15** GNU Lesser General Public License Usage
16** Alternatively, this file may be used under the terms of the GNU Lesser
17** General Public License version 2.1 as published by the Free Software
18** Foundation and appearing in the file LICENSE.LGPL included in the
19** packaging of this file. Please review the following information to
20** ensure the GNU Lesser General Public License version 2.1 requirements
21** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
22**
23** In addition, as a special exception, Nokia gives you certain
24** additional rights. These rights are described in the Nokia Qt LGPL
25** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
26** package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you are unsure which license is appropriate for your use, please
37** contact the sales department at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#ifndef Q3GLIST_H
43#define Q3GLIST_H
44
45#include <Qt3Support/q3ptrcollection.h>
46
47QT_BEGIN_HEADER
48
49QT_BEGIN_NAMESPACE
50
51QT_MODULE(Qt3SupportLight)
52
53class Q_COMPAT_EXPORT Q3LNode
54{
55friend class Q3GList;
56friend class Q3GListIterator;
57friend class Q3GListStdIterator;
58public:
59 Q3PtrCollection::Item getData() { return data; }
60private:
61 Q3PtrCollection::Item data;
62 Q3LNode *prev;
63 Q3LNode *next;
64 Q3LNode( Q3PtrCollection::Item d ) { data = d; }
65};
66
67class Q3GListIteratorList; // internal helper class
68
69class Q_COMPAT_EXPORT Q3GList : public Q3PtrCollection // doubly linked generic list
70{
71friend class Q3GListIterator;
72friend class Q3GListIteratorList;
73friend class Q3GVector; // needed by Q3GVector::toList
74public:
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
81protected:
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
141private:
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
158inline uint Q3GList::count() const
159{
160 return numNodes;
161}
162
163inline bool Q3GList::removeFirst()
164{
165 first();
166 return remove();
167}
168
169inline bool Q3GList::removeLast()
170{
171 last();
172 return remove();
173}
174
175inline int Q3GList::at() const
176{
177 return curIndex;
178}
179
180inline Q3PtrCollection::Item Q3GList::at( uint index )
181{
182 Q3LNode *n = locate( index );
183 return n ? n->data : 0;
184}
185
186inline Q3LNode *Q3GList::currentNode() const
187{
188 return curNode;
189}
190
191inline Q3PtrCollection::Item Q3GList::get() const
192{
193 return curNode ? curNode->data : 0;
194}
195
196inline Q3PtrCollection::Item Q3GList::cfirst() const
197{
198 return firstNode ? firstNode->data : 0;
199}
200
201inline 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
212Q_COMPAT_EXPORT QDataStream &operator>>( QDataStream &, Q3GList & );
213Q_COMPAT_EXPORT QDataStream &operator<<( QDataStream &, const Q3GList & );
214#endif
215
216/*****************************************************************************
217 Q3GListIterator class
218 *****************************************************************************/
219
220class Q_COMPAT_EXPORT Q3GListIterator // Q3GList iterator
221{
222friend class Q3GList;
223friend class Q3GListIteratorList;
224protected:
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
242protected:
243 Q3GList *list; // reference to list
244
245private:
246 Q3LNode *curNode; // current node in list
247};
248
249
250inline bool Q3GListIterator::atFirst() const
251{
252 return curNode == list->firstNode;
253}
254
255inline bool Q3GListIterator::atLast() const
256{
257 return curNode == list->lastNode;
258}
259
260inline Q3PtrCollection::Item Q3GListIterator::get() const
261{
262 return curNode ? curNode->data : 0;
263}
264
265class Q_COMPAT_EXPORT Q3GListStdIterator
266{
267public:
268 inline Q3GListStdIterator( Q3LNode* n ) : node( n ){}
269 inline operator Q3LNode* () { return node; }
270protected:
271 inline Q3LNode *next() { return node->next; }
272 Q3LNode *node;
273};
274
275QT_END_NAMESPACE
276
277QT_END_HEADER
278
279#endif // Q3GLIST_H
Note: See TracBrowser for help on using the repository browser.