1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2011 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 documentation of the Qt Toolkit.
|
---|
8 | **
|
---|
9 | ** $QT_BEGIN_LICENSE:FDL$
|
---|
10 | ** Commercial Usage
|
---|
11 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
12 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
13 | ** Software or, alternatively, in accordance with the terms contained in a
|
---|
14 | ** written agreement between you and Nokia.
|
---|
15 | **
|
---|
16 | ** GNU Free Documentation License
|
---|
17 | ** Alternatively, this file may be used under the terms of the GNU Free
|
---|
18 | ** Documentation License version 1.3 as published by the Free Software
|
---|
19 | ** Foundation and appearing in the file included in the packaging of this
|
---|
20 | ** file.
|
---|
21 | **
|
---|
22 | ** If you have questions regarding the use of this file, please contact
|
---|
23 | ** Nokia at qt-info@nokia.com.
|
---|
24 | ** $QT_END_LICENSE$
|
---|
25 | **
|
---|
26 | ****************************************************************************/
|
---|
27 |
|
---|
28 | /*!
|
---|
29 | \class Q3PtrVector
|
---|
30 | \brief The Q3PtrVector class is a template collection class that
|
---|
31 | provides a vector (array).
|
---|
32 | \compat
|
---|
33 |
|
---|
34 | Q3ValueVector is an STL-compatible alternative to this class.
|
---|
35 |
|
---|
36 | Q3PtrVector is implemented as a template class. Defines a template
|
---|
37 | instance Q3PtrVector\<X\> to create a vector that contains pointers
|
---|
38 | to X (X*).
|
---|
39 |
|
---|
40 | A vector is the same as an array. The main difference between
|
---|
41 | Q3PtrVector and Q3MemArray is that Q3PtrVector stores pointers to the
|
---|
42 | elements, whereas Q3MemArray stores the elements themselves (i.e.
|
---|
43 | Q3MemArray is value-based and Q3PtrVector is pointer-based).
|
---|
44 |
|
---|
45 | Items are added to the vector using insert() or fill(). Items are
|
---|
46 | removed with remove(). You can get a pointer to an item at a
|
---|
47 | particular index position using at().
|
---|
48 |
|
---|
49 | Unless otherwise stated, all functions that remove items from the
|
---|
50 | vector will also delete the element pointed to if \link
|
---|
51 | setAutoDelete() auto-deletion\endlink is enabled. By default,
|
---|
52 | auto-deletion is disabled; see setAutoDelete(). This behavior can
|
---|
53 | be changed in a subclass by reimplementing the virtual function
|
---|
54 | deleteItem().
|
---|
55 |
|
---|
56 | Functions that compare items (find() and sort() for example) will
|
---|
57 | do so using the virtual function compareItems(). The default
|
---|
58 | implementation of this function only compares the pointer values.
|
---|
59 | Reimplement compareItems() in a subclass to get searching and
|
---|
60 | sorting based on the item contents. You can perform a linear
|
---|
61 | search for a pointer in the vector using findRef(), or a binary
|
---|
62 | search (of a sorted vector) using bsearch(). You can count the
|
---|
63 | number of times an item appears in the vector with contains() or
|
---|
64 | containsRef().
|
---|
65 |
|
---|
66 | \sa Q3MemArray
|
---|
67 | */
|
---|
68 |
|
---|
69 | /*!
|
---|
70 | \fn Q3PtrVector::Q3PtrVector()
|
---|
71 |
|
---|
72 | Constructs a null vector.
|
---|
73 |
|
---|
74 | \sa isNull()
|
---|
75 | */
|
---|
76 |
|
---|
77 | /*!
|
---|
78 | \fn Q3PtrVector::Q3PtrVector(uint size)
|
---|
79 |
|
---|
80 | Constructs an vector with room for \a size items. Makes a null
|
---|
81 | vector if \a size == 0.
|
---|
82 |
|
---|
83 | All \a size positions in the vector are initialized to 0.
|
---|
84 |
|
---|
85 | \sa size(), resize(), isNull()
|
---|
86 | */
|
---|
87 |
|
---|
88 | /*!
|
---|
89 | \fn Q3PtrVector::Q3PtrVector(const Q3PtrVector<type> &v)
|
---|
90 |
|
---|
91 | Constructs a copy of \a v. Only the pointers are copied (i.e.
|
---|
92 | shallow copy).
|
---|
93 | */
|
---|
94 |
|
---|
95 | /*!
|
---|
96 | \fn Q3PtrVector::~Q3PtrVector()
|
---|
97 |
|
---|
98 | Removes all items from the vector, and destroys the vector itself.
|
---|
99 |
|
---|
100 | \sa clear()
|
---|
101 | */
|
---|
102 |
|
---|
103 | /*!
|
---|
104 | \fn Q3PtrVector<type> &Q3PtrVector::operator=(const Q3PtrVector<type> &v)
|
---|
105 |
|
---|
106 | Assigns \a v to this vector and returns a reference to this
|
---|
107 | vector.
|
---|
108 |
|
---|
109 | This vector is first cleared and then all the items from \a v are
|
---|
110 | copied into the vector. Only the pointers are copied (i.e. shallow
|
---|
111 | copy).
|
---|
112 |
|
---|
113 | \sa clear()
|
---|
114 | */
|
---|
115 |
|
---|
116 | /*!
|
---|
117 | \fn type **Q3PtrVector::data() const
|
---|
118 |
|
---|
119 | Returns a pointer to the actual vector data, which is an array of
|
---|
120 | type*.
|
---|
121 |
|
---|
122 | The vector is a null vector if data() == 0 (null pointer).
|
---|
123 |
|
---|
124 | \sa isNull()
|
---|
125 | */
|
---|
126 |
|
---|
127 | /*!
|
---|
128 | \fn uint Q3PtrVector::size() const
|
---|
129 |
|
---|
130 | Returns the size of the vector, i.e. the number of vector
|
---|
131 | positions. This is also the maximum number of items the vector can
|
---|
132 | hold.
|
---|
133 |
|
---|
134 | The vector is a null vector if size() == 0.
|
---|
135 |
|
---|
136 | \sa isNull(), resize(), count()
|
---|
137 | */
|
---|
138 |
|
---|
139 | /*!
|
---|
140 | \fn uint Q3PtrVector::count() const
|
---|
141 |
|
---|
142 | Returns the number of items in the vector. The vector is empty if
|
---|
143 | count() == 0.
|
---|
144 |
|
---|
145 | \sa isEmpty(), size(), isNull()
|
---|
146 | */
|
---|
147 |
|
---|
148 | /*!
|
---|
149 | \fn bool Q3PtrVector::isEmpty() const
|
---|
150 |
|
---|
151 | Returns true if the vector is empty; otherwise returns false.
|
---|
152 |
|
---|
153 | \sa count()
|
---|
154 | */
|
---|
155 |
|
---|
156 | /*!
|
---|
157 | \fn bool Q3PtrVector::isNull() const
|
---|
158 |
|
---|
159 | Returns true if the vector is null; otherwise returns false.
|
---|
160 |
|
---|
161 | A null vector has size() == 0 and data() == 0.
|
---|
162 |
|
---|
163 | \sa size()
|
---|
164 | */
|
---|
165 |
|
---|
166 | /*!
|
---|
167 | \fn bool Q3PtrVector::resize(uint size)
|
---|
168 |
|
---|
169 | Resizes (expands or shrinks) the vector to \a size elements. The
|
---|
170 | vector becomes a null vector if \a size == 0.
|
---|
171 |
|
---|
172 | Any items at position \a size or beyond in the vector are removed.
|
---|
173 | New positions are initialized to 0.
|
---|
174 |
|
---|
175 | Returns true if successful, i.e. if the memory was successfully
|
---|
176 | allocated; otherwise returns false.
|
---|
177 |
|
---|
178 | \sa size(), isNull()
|
---|
179 | */
|
---|
180 |
|
---|
181 | /*!
|
---|
182 | \fn bool Q3PtrVector::insert(uint i, const type *d)
|
---|
183 |
|
---|
184 | Sets position \a i in the vector to contain the item \a d. \a i
|
---|
185 | must be less than size(). Any previous element in position \a i is
|
---|
186 | removed.
|
---|
187 |
|
---|
188 | Returns true if \a i is within range; otherwise returns false.
|
---|
189 |
|
---|
190 | \sa at()
|
---|
191 | */
|
---|
192 |
|
---|
193 | /*!
|
---|
194 | \fn bool Q3PtrVector::remove(uint i)
|
---|
195 |
|
---|
196 | Removes the item at position \a i in the vector, if there is one.
|
---|
197 | \a i must be less than size().
|
---|
198 |
|
---|
199 | Returns true if \a i is within range; otherwise returns false.
|
---|
200 |
|
---|
201 | \sa take(), at()
|
---|
202 | */
|
---|
203 |
|
---|
204 | /*!
|
---|
205 | \fn type* Q3PtrVector::take(uint i)
|
---|
206 |
|
---|
207 | Returns the item at position \a i in the vector, and removes that
|
---|
208 | item from the vector. \a i must be less than size(). If there is
|
---|
209 | no item at position \a i, 0 is returned.
|
---|
210 |
|
---|
211 | Unlike remove(), this function does \e not call deleteItem() for
|
---|
212 | the removed item.
|
---|
213 |
|
---|
214 | \sa remove(), at()
|
---|
215 | */
|
---|
216 |
|
---|
217 | /*!
|
---|
218 | \fn void Q3PtrVector::clear()
|
---|
219 |
|
---|
220 | Removes all items from the vector, and destroys the vector itself.
|
---|
221 |
|
---|
222 | The vector becomes a null vector.
|
---|
223 |
|
---|
224 | \sa isNull()
|
---|
225 | */
|
---|
226 |
|
---|
227 | /*!
|
---|
228 | \fn bool Q3PtrVector::fill(const type *d, int size)
|
---|
229 |
|
---|
230 | Inserts item \a d in all positions in the vector. Any existing
|
---|
231 | items are removed. If \a d is 0, the vector becomes empty.
|
---|
232 |
|
---|
233 | If \a size >= 0, the vector is first resized to \a size. By
|
---|
234 | default, \a size is -1.
|
---|
235 |
|
---|
236 | Returns true if successful, i.e. \a size is the same as the
|
---|
237 | current size, or \a size is larger and the memory has successfully
|
---|
238 | been allocated; otherwise returns false.
|
---|
239 |
|
---|
240 | \sa resize(), insert(), isEmpty()
|
---|
241 | */
|
---|
242 |
|
---|
243 | /*!
|
---|
244 | \fn void Q3PtrVector::sort()
|
---|
245 |
|
---|
246 | Sorts the items in ascending order. Any empty positions will be
|
---|
247 | put last.
|
---|
248 |
|
---|
249 | Compares items using the virtual function compareItems().
|
---|
250 |
|
---|
251 | \sa bsearch()
|
---|
252 | */
|
---|
253 |
|
---|
254 | /*!
|
---|
255 | \fn int Q3PtrVector::bsearch(const type* d) const
|
---|
256 |
|
---|
257 | In a sorted array, finds the first occurrence of \a d using a
|
---|
258 | binary search. For a sorted array, this is generally much faster
|
---|
259 | than find(), which performs a linear search.
|
---|
260 |
|
---|
261 | Returns the position of \a d, or -1 if \a d could not be found. \a
|
---|
262 | d must not be 0.
|
---|
263 |
|
---|
264 | Compares items using the virtual function compareItems().
|
---|
265 |
|
---|
266 | \sa sort(), find()
|
---|
267 | */
|
---|
268 |
|
---|
269 |
|
---|
270 | /*!
|
---|
271 | \fn int Q3PtrVector::findRef(const type *d, uint i) const
|
---|
272 |
|
---|
273 | Finds the first occurrence of the item pointer \a d in the vector
|
---|
274 | using a linear search. The search starts at position \a i, which
|
---|
275 | must be less than size(). \a i is by default 0; i.e. the search
|
---|
276 | starts at the start of the vector.
|
---|
277 |
|
---|
278 | Returns the position of \a d, or -1 if \a d could not be found.
|
---|
279 |
|
---|
280 | This function does \e not use compareItems() to compare items.
|
---|
281 |
|
---|
282 | Use the much faster bsearch() to search a sorted vector.
|
---|
283 |
|
---|
284 | \sa find(), bsearch()
|
---|
285 | */
|
---|
286 |
|
---|
287 | /*!
|
---|
288 | \fn int Q3PtrVector::find(const type *d, uint i) const
|
---|
289 |
|
---|
290 | Finds the first occurrence of item \a d in the vector using a
|
---|
291 | linear search. The search starts at position \a i, which must be
|
---|
292 | less than size(). \a i is by default 0; i.e. the search starts at
|
---|
293 | the start of the vector.
|
---|
294 |
|
---|
295 | Returns the position of \a d, or -1 if \a d could not be found.
|
---|
296 |
|
---|
297 | Compares items using the virtual function compareItems().
|
---|
298 |
|
---|
299 | Use the much faster bsearch() to search a sorted vector.
|
---|
300 |
|
---|
301 | \sa findRef(), bsearch()
|
---|
302 | */
|
---|
303 |
|
---|
304 |
|
---|
305 | /*!
|
---|
306 | \fn uint Q3PtrVector::containsRef(const type *d) const
|
---|
307 |
|
---|
308 | Returns the number of occurrences of the item pointer \a d in the
|
---|
309 | vector.
|
---|
310 |
|
---|
311 | This function does \e not use compareItems() to compare items.
|
---|
312 |
|
---|
313 | \sa findRef()
|
---|
314 | */
|
---|
315 |
|
---|
316 | /*!
|
---|
317 | \fn uint Q3PtrVector::contains(const type *d) const
|
---|
318 |
|
---|
319 | Returns the number of occurrences of item \a d in the vector.
|
---|
320 |
|
---|
321 | Compares items using the virtual function compareItems().
|
---|
322 |
|
---|
323 | \sa containsRef()
|
---|
324 | */
|
---|
325 |
|
---|
326 | /*!
|
---|
327 | \fn type *Q3PtrVector::operator[](int i) const
|
---|
328 |
|
---|
329 | Returns the item at position \a i, or 0 if there is no item at
|
---|
330 | that position. \a i must be less than size().
|
---|
331 |
|
---|
332 | Equivalent to at(\a i).
|
---|
333 |
|
---|
334 | \sa at()
|
---|
335 | */
|
---|
336 |
|
---|
337 | /*!
|
---|
338 | \fn type *Q3PtrVector::at(uint i) const
|
---|
339 |
|
---|
340 | Returns the item at position \a i, or 0 if there is no item at
|
---|
341 | that position. \a i must be less than size().
|
---|
342 | */
|
---|
343 |
|
---|
344 |
|
---|
345 | /*!
|
---|
346 | \fn void Q3PtrVector::toList(Q3GList *list) const
|
---|
347 |
|
---|
348 | \internal
|
---|
349 |
|
---|
350 | Copies all items in this vector to the list \a list. \a list is
|
---|
351 | first cleared and then all items are appended to \a list.
|
---|
352 |
|
---|
353 | \sa Q3PtrList, Q3PtrStack, Q3PtrQueue
|
---|
354 | */
|
---|
355 |
|
---|
356 | /*!
|
---|
357 | \fn int Q3PtrVector::compareItems(Q3PtrCollection::Item d1,
|
---|
358 | Q3PtrCollection::Item d2)
|
---|
359 |
|
---|
360 | This virtual function compares two list items.
|
---|
361 |
|
---|
362 | Returns:
|
---|
363 | \list
|
---|
364 | \i zero if \a d1 == \a d2
|
---|
365 | \i nonzero if \a d1 != \a d2
|
---|
366 | \endlist
|
---|
367 |
|
---|
368 | This function returns \e int rather than \e bool so that
|
---|
369 | reimplementations can return one of three values and use it to
|
---|
370 | sort by:
|
---|
371 | \list
|
---|
372 | \i 0 if \a d1 == \a d2
|
---|
373 | \i \> 0 (positive integer) if \a d1 \> \a d2
|
---|
374 | \i \< 0 (negative integer) if \a d1 \< \a d2
|
---|
375 | \endlist
|
---|
376 |
|
---|
377 | The sort() and bsearch() functions require compareItems() to be
|
---|
378 | implemented as described here.
|
---|
379 |
|
---|
380 | This function should not modify the vector because some const
|
---|
381 | functions call compareItems().
|
---|
382 | */
|
---|
383 |
|
---|
384 | /*!
|
---|
385 | \fn QDataStream& Q3PtrVector::read(QDataStream &s,
|
---|
386 | Q3PtrCollection::Item &item)
|
---|
387 |
|
---|
388 | Reads a vector item, \a item, from the stream \a s and returns a
|
---|
389 | reference to the stream.
|
---|
390 |
|
---|
391 | The default implementation sets \a item to 0.
|
---|
392 |
|
---|
393 | \sa write()
|
---|
394 | */
|
---|
395 |
|
---|
396 | /*!
|
---|
397 | \fn QDataStream& Q3PtrVector::write(QDataStream &s,
|
---|
398 | Q3PtrCollection::Item item) const
|
---|
399 |
|
---|
400 | Writes a vector item, \a item, to the stream \a s and returns a
|
---|
401 | reference to the stream.
|
---|
402 |
|
---|
403 | The default implementation does nothing.
|
---|
404 |
|
---|
405 | \sa read()
|
---|
406 | */
|
---|
407 |
|
---|
408 | /*!
|
---|
409 | \fn bool Q3PtrVector::operator==(const Q3PtrVector<type> &v) const
|
---|
410 |
|
---|
411 | Returns true if this vector and \a v are equal; otherwise returns
|
---|
412 | false.
|
---|
413 | */
|
---|