source: trunk/src/qt3support/tools/q3memarray.qdoc@ 1054

Last change on this file since 1054 was 846, checked in by Dmitry A. Kuminov, 14 years ago

trunk: Merged in qt 4.7.2 sources from branches/vendor/nokia/qt.

  • Property svn:eol-style set to native
File size: 13.2 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4** All rights reserved.
5** Contact: Nokia Corporation ([email protected])
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 [email protected].
24** $QT_END_LICENSE$
25**
26****************************************************************************/
27
28/*!
29 \class Q3MemArray
30 \brief The Q3MemArray class is a template class that provides arrays of simple types.
31 \compat
32
33 Q3MemArray is implemented as a template class. Define a template
34 instance Q3MemArray\<X\> to create an array that contains X items.
35
36 Q3MemArray stores the array elements directly in the array. It can
37 only deal with simple types (i.e. C++ types, structs, and classes
38 that have no constructors, destructors, or virtual functions).
39 Q3MemArray uses bitwise operations to copy and compare array
40 elements.
41
42 The Q3PtrVector collection class is also a kind of array. Like most
43 old Qt collection classes, it uses pointers to the contained items.
44
45 Q3MemArray uses explicit sharing with a
46 reference count. If more than one array shares common data and one
47 of the arrays is modified, all the arrays are modified.
48
49 The benefit of sharing is that a program does not need to duplicate
50 data when it is not required, which results in lower memory use
51 and less copying of data.
52
53 Example:
54 \snippet doc/src/snippets/code/doc_src_q3memarray.qdoc 0
55
56 Program output:
57 \snippet doc/src/snippets/code/doc_src_q3memarray.qdoc 1
58
59 Note concerning the use of Q3MemArray for manipulating structs or
60 classes: Compilers will often pad the size of structs of odd sizes
61 up to the nearest word boundary. This will then be the size
62 Q3MemArray will use for its bitwise element comparisons. Because
63 the remaining bytes will typically be uninitialized, this can
64 cause find() etc. to fail to find the element. Example:
65
66 \snippet doc/src/snippets/code/doc_src_q3memarray.qdoc 2
67
68 To work around this, make sure that you use a struct where
69 sizeof() returns the same as the sum of the sizes of the members
70 either by changing the types of the struct members or by adding
71 dummy members.
72
73 Q3MemArray data can be traversed by iterators (see begin() and
74 end()). The number of items is returned by count(). The array can
75 be resized with resize() and filled using fill().
76
77 You can make a shallow copy of the array with assign() (or
78 operator=()) and a deep copy with duplicate().
79
80 Search for values in the array with find() and contains(). For
81 sorted arrays (see sort()) you can search using bsearch().
82
83 You can set the data directly using setRawData() and
84 resetRawData(), although this requires care.
85*/
86
87/*! \fn Q3MemArray::operator QVector<type>() const
88
89 Automatically converts the Q3MemArray<type> into a QVector<type>.
90*/
91
92/*! \typedef Q3MemArray::Iterator
93 A Q3MemArray iterator.
94 \sa begin() end()
95*/
96/*! \typedef Q3MemArray::ConstIterator
97 A const Q3MemArray iterator.
98 \sa begin() end()
99*/
100/*! \typedef Q3MemArray::ValueType
101 \internal
102*/
103
104/*!
105 \fn Q3MemArray::Q3MemArray()
106
107 Constructs a null array.
108
109 \sa isNull()
110*/
111
112/*!
113 \fn Q3MemArray::Q3MemArray( int size )
114
115 Constructs an array with room for \a size elements. Makes a null
116 array if \a size == 0.
117
118 The elements are left uninitialized.
119
120 \sa resize(), isNull()
121*/
122
123/*!
124 \fn Q3MemArray::Q3MemArray( const Q3MemArray<type> &a )
125
126 Constructs a shallow copy of \a a.
127
128 \sa assign()
129*/
130
131/*!
132 \fn Q3MemArray::Q3MemArray(const QVector<type> &vector)
133
134 Constructs a copy of \a vector.
135*/
136
137/*!
138 \fn Q3MemArray::Q3MemArray(int arg1, int arg2)
139
140 Constructs an array \e{without allocating} array space. The
141 arguments \a arg1 and \a arg2 should be zero. Use at your own
142 risk.
143*/
144
145/*!
146 \fn Q3MemArray::~Q3MemArray()
147
148 Dereferences the array data and deletes it if this was the last
149 reference.
150*/
151
152/*!
153 \fn Q3MemArray<type> &Q3MemArray::operator=( const Q3MemArray<type> &a )
154
155 Assigns a shallow copy of \a a to this array and returns a
156 reference to this array.
157
158 Equivalent to assign( a ).
159*/
160
161/*!
162 \fn type *Q3MemArray::data() const
163
164 Returns a pointer to the actual array data.
165
166 The array is a null array if data() == 0 (null pointer).
167
168 \sa isNull()
169*/
170
171/*!
172 \fn uint Q3MemArray::nrefs() const
173
174 Returns the reference count for the shared array data. This
175 reference count is always greater than zero.
176*/
177
178/*!
179 \fn uint Q3MemArray::size() const
180
181 Returns the size of the array (maximum number of elements).
182
183 The array is a null array if size() == 0.
184
185 \sa isNull(), resize()
186*/
187
188/*!
189 \fn uint Q3MemArray::count() const
190
191 Returns the same as size().
192
193 \sa size()
194*/
195
196/*!
197 \fn bool Q3MemArray::isEmpty() const
198
199 Returns TRUE if the array is empty; otherwise returns FALSE.
200
201 isEmpty() is equivalent to isNull() for Q3MemArray (unlike
202 QString).
203*/
204
205/*!
206 \fn bool Q3MemArray::isNull() const
207
208 Returns TRUE if the array is null; otherwise returns FALSE.
209
210 A null array has size() == 0 and data() == 0.
211*/
212
213/*!
214 \fn bool Q3MemArray::resize( uint size, Optimization optim )
215
216 Resizes (expands or shrinks) the array to \a size elements. The
217 array becomes a null array if \a size == 0.
218
219 Returns TRUE if successful, or FALSE if the memory cannot be
220 allocated.
221
222 New elements are not initialized.
223
224 \a optim is either Q3GArray::MemOptim (the default) or
225 Q3GArray::SpeedOptim. When optimizing for speed rather than memory
226 consumption, the array uses a smart grow and shrink algorithm that
227 might allocate more memory than is actually needed for \a size
228 elements. This speeds up subsequent resize operations, for example
229 when appending many elements to an array, since the space has
230 already been allocated.
231
232 \sa size()
233*/
234
235/*!
236 \fn bool Q3MemArray::resize( uint size )
237
238 \overload
239
240 Resizes (expands or shrinks) the array to \a size elements. The
241 array becomes a null array if \a size == 0.
242
243 Returns TRUE if successful, i.e. if the memory can be allocated;
244 otherwise returns FALSE.
245
246 New elements are not initialized.
247
248 \sa size()
249*/
250
251/*!
252 \fn bool Q3MemArray::truncate( uint pos )
253
254 Truncates the array at position \a pos.
255
256 Returns TRUE if successful, i.e. if the memory can be allocated;
257 otherwise returns FALSE.
258
259 Equivalent to resize(\a pos).
260
261 \sa resize()
262*/
263
264/*!
265 \fn bool Q3MemArray::fill( const type &v, int size )
266
267 Fills the array with the value \a v. If \a size is specified as
268 different from -1, then the array will be resized before being
269 filled.
270
271 Returns TRUE if successful, i.e. if \a size is -1, or \a size is
272 != -1 and the memory can be allocated; otherwise returns FALSE.
273
274 \sa resize()
275*/
276
277/*!
278 \fn void Q3MemArray::detach()
279
280 Detaches this array from shared array data; i.e. it makes a
281 private, deep copy of the data.
282
283 Copying will be performed only if the \link nrefs() reference
284 count\endlink is greater than one.
285
286 \sa copy()
287*/
288
289/*!
290 \fn Q3MemArray<type> Q3MemArray::copy() const
291
292 Returns a deep copy of this array.
293
294 \sa detach(), duplicate()
295*/
296
297/*!
298 \fn Q3MemArray<type> &Q3MemArray::assign( const Q3MemArray<type> &a )
299
300 Shallow copy. Dereferences the current array and references the
301 data contained in \a a instead. Returns a reference to this array.
302
303 \sa operator=()
304*/
305
306/*!
307 \fn Q3MemArray<type> &Q3MemArray::assign( const type *data, uint size )
308
309 \overload
310
311 Shallow copy. Dereferences the current array and references the
312 array data \a data, which contains \a size elements. Returns a
313 reference to this array.
314
315 Do not delete \a data later; Q3MemArray will call free() on it
316 at the right time.
317*/
318
319/*!
320 \fn Q3MemArray<type> &Q3MemArray::duplicate( const Q3MemArray<type> &a )
321
322 Deep copy. Dereferences the current array and obtains a copy of
323 the data contained in \a a instead. Returns a reference to this
324 array.
325
326 \sa copy()
327*/
328
329/*!
330 \fn Q3MemArray<type> &Q3MemArray::duplicate( const type *data, uint size )
331
332 \overload
333
334 Deep copy. Dereferences the current array and obtains a copy of
335 the array data \a data instead. Returns a reference to this array.
336 The size of the array is given by \a size.
337
338 \sa copy()
339*/
340
341/*!
342 \fn Q3MemArray<type> &Q3MemArray::setRawData( const type *data, uint size )
343
344 Sets raw data and returns a reference to the array.
345
346 Dereferences the current array and sets the new array data to \a
347 data and the new array size to \a size. Do not attempt to resize
348 or re-assign the array data when raw data has been set. Call
349 resetRawData(\a data, \a size) to reset the array.
350
351 Setting raw data is useful because it sets Q3MemArray data without
352 allocating memory or copying data.
353
354 Example I (intended use):
355 \snippet doc/src/snippets/code/doc_src_q3memarray.qdoc 3
356
357 Example II (you don't want to do this):
358 \snippet doc/src/snippets/code/doc_src_q3memarray.qdoc 4
359
360 \warning If you do not call resetRawData(), Q3MemArray will attempt
361 to deallocate or reallocate the raw data, which might not be too
362 good. Be careful.
363
364 \sa resetRawData()
365*/
366
367/*!
368 \fn void Q3MemArray::resetRawData( const type *data, uint size )
369
370 Removes internal references to the raw data that was set using
371 setRawData(). This means that Q3MemArray no longer has access to
372 the \a data, so you are free to manipulate \a data as you wish.
373 You can now use the Q3MemArray without affecting the original \a
374 data, for example by calling setRawData() with a pointer to some
375 other data.
376
377 The arguments must be the \a data and length, \a size, that were
378 passed to setRawData(). This is for consistency checking.
379
380 \sa setRawData()
381*/
382
383/*!
384 \fn int Q3MemArray::find( const type &v, uint index ) const
385
386 Finds the first occurrence of \a v, starting at position \a index.
387
388 Returns the position of \a v, or -1 if \a v could not be found.
389
390 \sa contains()
391*/
392
393/*!
394 \fn int Q3MemArray::contains( const type &v ) const
395
396 Returns the number of times \a v occurs in the array.
397
398 \sa find()
399*/
400
401/*!
402 \fn void Q3MemArray::sort()
403
404 Sorts the array elements in ascending order, using bitwise
405 comparison (memcmp()).
406
407 \sa bsearch()
408*/
409
410/*!
411 \fn int Q3MemArray::bsearch( const type &v ) const
412
413 In a sorted array (as sorted by sort()), finds the first
414 occurrence of \a v by using a binary search. For a sorted
415 array this is generally much faster than find(), which does
416 a linear search.
417
418 Returns the position of \a v, or -1 if \a v could not be found.
419
420 \sa sort(), find()
421*/
422
423/*!
424 \fn type &Q3MemArray::operator[]( int index ) const
425
426 Returns a reference to the element at position \a index in the
427 array.
428
429 This can be used to both read and set an element. Equivalent to
430 at().
431
432 \sa at()
433*/
434
435/*!
436 \fn type &Q3MemArray::at( uint index ) const
437
438 Returns a reference to the element at position \a index in the array.
439
440 This can be used to both read and set an element.
441
442 \sa operator[]()
443*/
444
445/*!
446 \fn Q3MemArray::operator const type *() const
447
448 Cast operator. Returns a pointer to the array.
449
450 \sa data()
451*/
452
453/*!
454 \fn bool Q3MemArray::operator==( const Q3MemArray<type> &a ) const
455
456 Returns TRUE if this array is equal to \a a; otherwise returns
457 FALSE.
458
459 The two arrays are compared bitwise.
460
461 \sa operator!=()
462*/
463
464/*!
465 \fn bool Q3MemArray::operator!=( const Q3MemArray<type> &a ) const
466
467 Returns TRUE if this array is different from \a a; otherwise
468 returns FALSE.
469
470 The two arrays are compared bitwise.
471
472 \sa operator==()
473*/
474
475/*!
476 \fn Iterator Q3MemArray::begin()
477
478 Returns an iterator pointing at the beginning of this array. This
479 iterator can be used in the same way as the iterators of
480 Q3ValueList and QMap, for example.
481*/
482
483/*!
484 \fn Iterator Q3MemArray::end()
485
486 Returns an iterator pointing behind the last element of this
487 array. This iterator can be used in the same way as the iterators
488 of Q3ValueList and QMap, for example.
489*/
490
491/*!
492 \fn ConstIterator Q3MemArray::begin() const
493
494 \overload
495
496 Returns a const iterator pointing at the beginning of this array.
497 This iterator can be used in the same way as the iterators of
498 Q3ValueList and QMap, for example.
499*/
500
501/*!
502 \fn ConstIterator Q3MemArray::end() const
503
504 \overload
505
506 Returns a const iterator pointing behind the last element of this
507 array. This iterator can be used in the same way as the iterators
508 of Q3ValueList and QMap, for example.
509*/
Note: See TracBrowser for help on using the repository browser.