summaryrefslogtreecommitdiffstats
path: root/qtinterface/qt4/Qt/q3glist.h
blob: d33e0a07452becb20ed76e9229397fae28352e4b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/****************************************************************************
**
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation ([email protected])
**
** This file is part of the Qt3Support module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial Usage
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Nokia.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights.  These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU General Public License version 3.0 requirements will be
** met: http://www.gnu.org/copyleft/gpl.html.
**
** If you have questions regarding the use of this file, please contact
** Nokia at [email protected].
** $QT_END_LICENSE$
**
****************************************************************************/

#ifndef Q3GLIST_H
#define Q3GLIST_H

#include <Qt3Support/q3ptrcollection.h>

QT_BEGIN_HEADER

QT_BEGIN_NAMESPACE

QT_MODULE(Qt3SupportLight)

class Q_COMPAT_EXPORT Q3LNode
{
friend class Q3GList;
friend class Q3GListIterator;
friend class Q3GListStdIterator;
public:
    Q3PtrCollection::Item getData()	{ return data; }
private:
    Q3PtrCollection::Item data;
    Q3LNode *prev;
    Q3LNode *next;
    Q3LNode( Q3PtrCollection::Item d ) { data = d; }
};

class Q3GListIteratorList; // internal helper class

class Q_COMPAT_EXPORT Q3GList : public Q3PtrCollection	// doubly linked generic list
{
friend class Q3GListIterator;
friend class Q3GListIteratorList;
friend class Q3GVector;				// needed by Q3GVector::toList
public:
    uint  count() const;			// return number of nodes

#ifndef QT_NO_DATASTREAM
    QDataStream &read( QDataStream & );		// read list from stream
    QDataStream &write( QDataStream & ) const;	// write list to stream
#endif
protected:
    Q3GList();					// create empty list
    Q3GList( const Q3GList & );			// make copy of other list
    virtual ~Q3GList();

    Q3GList &operator=( const Q3GList & );	// assign from other list
    bool operator==( const Q3GList& ) const;

    void inSort( Q3PtrCollection::Item );		// add item sorted in list
    void append( Q3PtrCollection::Item );		// add item at end of list
    bool insertAt( uint index, Q3PtrCollection::Item ); // add item at i'th position
    void relinkNode( Q3LNode * );		// relink as first item
    bool removeNode( Q3LNode * );		// remove node
    bool remove( Q3PtrCollection::Item = 0 );	// remove item (0=current)
    bool removeRef( Q3PtrCollection::Item = 0 );	// remove item (0=current)
    bool removeFirst();				// remove first item
    bool removeLast();				// remove last item
    bool removeAt( uint );			// remove item at i'th position
    bool replaceAt( uint, Q3PtrCollection::Item ); // replace item at position i with item
    Q3PtrCollection::Item takeNode( Q3LNode * );	// take out node
    Q3PtrCollection::Item take();		// take out current item
    Q3PtrCollection::Item takeAt( uint index );	// take out item at i'th pos
    Q3PtrCollection::Item takeFirst();		// take out first item
    Q3PtrCollection::Item takeLast();		// take out last item

    void sort();                        	// sort all items;
    void clear();				// remove all items

    int	 findRef( Q3PtrCollection::Item, bool = true ); // find exact item in list
    int	 find( Q3PtrCollection::Item, bool = true ); // find equal item in list

    uint containsRef( Q3PtrCollection::Item ) const;	// get number of exact matches
    uint contains( Q3PtrCollection::Item ) const;	// get number of equal matches

    Q3PtrCollection::Item at( uint index );	// access item at i'th pos
    int	  at() const;				// get current index
    Q3LNode *currentNode() const;		// get current node

    Q3PtrCollection::Item get() const;		// get current item

    Q3PtrCollection::Item cfirst() const;	// get ptr to first list item
    Q3PtrCollection::Item clast()  const;	// get ptr to last list item
    Q3PtrCollection::Item first();		// set first item in list curr
    Q3PtrCollection::Item last();		// set last item in list curr
    Q3PtrCollection::Item next();		// set next item in list curr
    Q3PtrCollection::Item prev();		// set prev item in list curr

    void  toVector( Q3GVector * ) const;		// put items in vector

    virtual int compareItems( Q3PtrCollection::Item, Q3PtrCollection::Item );

#ifndef QT_NO_DATASTREAM
    virtual QDataStream &read( QDataStream &, Q3PtrCollection::Item & );
    virtual QDataStream &write( QDataStream &, Q3PtrCollection::Item ) const;
#endif

    Q3LNode* begin() const { return firstNode; }
    Q3LNode* end() const { return 0; }
    Q3LNode* erase( Q3LNode* it );

private:
    void  prepend( Q3PtrCollection::Item );	// add item at start of list

    void heapSortPushDown( Q3PtrCollection::Item* heap, int first, int last );

    Q3LNode *firstNode;				// first node
    Q3LNode *lastNode;				// last node
    Q3LNode *curNode;				// current node
    int curIndex;				// current index
    uint numNodes;				// number of nodes
    Q3GListIteratorList *iterators; 		// list of iterators

    Q3LNode *locate( uint );			// get node at i'th pos
    Q3LNode *unlink();				// unlink node
};


inline uint Q3GList::count() const
{
    return numNodes;
}

inline bool Q3GList::removeFirst()
{
    first();
    return remove();
}

inline bool Q3GList::removeLast()
{
    last();
    return remove();
}

inline int Q3GList::at() const
{
    return curIndex;
}

inline Q3PtrCollection::Item Q3GList::at( uint index )
{
    Q3LNode *n = locate( index );
    return n ? n->data : 0;
}

inline Q3LNode *Q3GList::currentNode() const
{
    return curNode;
}

inline Q3PtrCollection::Item Q3GList::get() const
{
    return curNode ? curNode->data : 0;
}

inline Q3PtrCollection::Item Q3GList::cfirst() const
{
    return firstNode ? firstNode->data : 0;
}

inline Q3PtrCollection::Item Q3GList::clast() const
{
    return lastNode ? lastNode->data : 0;
}


/*****************************************************************************
  Q3GList stream functions
 *****************************************************************************/

#ifndef QT_NO_DATASTREAM
Q_COMPAT_EXPORT QDataStream &operator>>( QDataStream &, Q3GList & );
Q_COMPAT_EXPORT QDataStream &operator<<( QDataStream &, const Q3GList & );
#endif

/*****************************************************************************
  Q3GListIterator class
 *****************************************************************************/

class Q_COMPAT_EXPORT Q3GListIterator			// Q3GList iterator
{
friend class Q3GList;
friend class Q3GListIteratorList;
protected:
    Q3GListIterator( const Q3GList & );
    Q3GListIterator( const Q3GListIterator & );
    Q3GListIterator &operator=( const Q3GListIterator & );
   ~Q3GListIterator();

    bool  atFirst() const;			// test if at first item
    bool  atLast()  const;			// test if at last item
    Q3PtrCollection::Item	  toFirst();				// move to first item
    Q3PtrCollection::Item	  toLast();				// move to last item

    Q3PtrCollection::Item	  get() const;				// get current item
    Q3PtrCollection::Item	  operator()();				// get current and move to next
    Q3PtrCollection::Item	  operator++();				// move to next item (prefix)
    Q3PtrCollection::Item	  operator+=(uint);			// move n positions forward
    Q3PtrCollection::Item	  operator--();				// move to prev item (prefix)
    Q3PtrCollection::Item	  operator-=(uint);			// move n positions backward

protected:
    Q3GList *list;				// reference to list

private:
    Q3LNode  *curNode;				// current node in list
};


inline bool Q3GListIterator::atFirst() const
{
    return curNode == list->firstNode;
}

inline bool Q3GListIterator::atLast() const
{
    return curNode == list->lastNode;
}

inline Q3PtrCollection::Item Q3GListIterator::get() const
{
    return curNode ? curNode->data : 0;
}

class Q_COMPAT_EXPORT Q3GListStdIterator
{
public:
    inline Q3GListStdIterator( Q3LNode* n ) : node( n ){}
    inline operator Q3LNode* () { return node; }
protected:
    inline Q3LNode *next() { return node->next; }
    Q3LNode *node;
};

QT_END_NAMESPACE

QT_END_HEADER

#endif	// Q3GLIST_H