summaryrefslogtreecommitdiffstats
path: root/doc/man/man3/qmemarray.3qt
blob: 40f2c20744b30bbf227329e5888244941e7dce62 (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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
'\" t
.TH QMemArray 3qt "2 February 2007" "Trolltech AS" \" -*- nroff -*-
.\" Copyright 1992-2007 Trolltech ASA.  All rights reserved.  See the
.\" license file included in the distribution for a complete license
.\" statement.
.\"
.ad l
.nh
.SH NAME
QMemArray \- Template class that provides arrays of simple types
.SH SYNOPSIS
All the functions in this class are reentrant when Qt is built with thread support.</p>
.PP
\fC#include <qmemarray.h>\fR
.PP
Inherited by QByteArray and QPointArray.
.PP
.SS "Public Members"
.in +1c
.ti -1c
.BI "typedef type * \fBIterator\fR"
.br
.ti -1c
.BI "typedef const type * \fBConstIterator\fR"
.br
.ti -1c
.BI "\fBQMemArray\fR ()"
.br
.ti -1c
.BI "\fBQMemArray\fR ( int size )"
.br
.ti -1c
.BI "\fBQMemArray\fR ( const QMemArray<type> & a )"
.br
.ti -1c
.BI "\fB~QMemArray\fR ()"
.br
.ti -1c
.BI "QMemArray<type> & \fBoperator=\fR ( const QMemArray<type> & a )"
.br
.ti -1c
.BI "type * \fBdata\fR () const"
.br
.ti -1c
.BI "uint \fBnrefs\fR () const"
.br
.ti -1c
.BI "uint \fBsize\fR () const"
.br
.ti -1c
.BI "uint \fBcount\fR () const"
.br
.ti -1c
.BI "bool \fBisEmpty\fR () const"
.br
.ti -1c
.BI "bool \fBisNull\fR () const"
.br
.ti -1c
.BI "bool \fBresize\fR ( uint size )"
.br
.ti -1c
.BI "bool \fBresize\fR ( uint size, Optimization optim )"
.br
.ti -1c
.BI "bool \fBtruncate\fR ( uint pos )"
.br
.ti -1c
.BI "bool \fBfill\fR ( const type & v, int size = -1 )"
.br
.ti -1c
.BI "virtual void \fBdetach\fR ()"
.br
.ti -1c
.BI "QMemArray<type> \fBcopy\fR () const"
.br
.ti -1c
.BI "QMemArray<type> & \fBassign\fR ( const QMemArray<type> & a )"
.br
.ti -1c
.BI "QMemArray<type> & \fBassign\fR ( const type * data, uint size )"
.br
.ti -1c
.BI "QMemArray<type> & \fBduplicate\fR ( const QMemArray<type> & a )"
.br
.ti -1c
.BI "QMemArray<type> & \fBduplicate\fR ( const type * data, uint size )"
.br
.ti -1c
.BI "QMemArray<type> & \fBsetRawData\fR ( const type * data, uint size )"
.br
.ti -1c
.BI "void \fBresetRawData\fR ( const type * data, uint size )"
.br
.ti -1c
.BI "int \fBfind\fR ( const type & v, uint index = 0 ) const"
.br
.ti -1c
.BI "int \fBcontains\fR ( const type & v ) const"
.br
.ti -1c
.BI "void \fBsort\fR ()"
.br
.ti -1c
.BI "int \fBbsearch\fR ( const type & v ) const"
.br
.ti -1c
.BI "type & \fBoperator[]\fR ( int index ) const"
.br
.ti -1c
.BI "type & \fBat\fR ( uint index ) const"
.br
.ti -1c
.BI "\fBoperator const type *\fR () const"
.br
.ti -1c
.BI "bool \fBoperator==\fR ( const QMemArray<type> & a ) const"
.br
.ti -1c
.BI "bool \fBoperator!=\fR ( const QMemArray<type> & a ) const"
.br
.ti -1c
.BI "Iterator \fBbegin\fR ()"
.br
.ti -1c
.BI "Iterator \fBend\fR ()"
.br
.ti -1c
.BI "ConstIterator \fBbegin\fR () const"
.br
.ti -1c
.BI "ConstIterator \fBend\fR () const"
.br
.in -1c
.SS "Protected Members"
.in +1c
.ti -1c
.BI "\fBQMemArray\fR ( int, int )"
.br
.in -1c
.SH RELATED FUNCTION DOCUMENTATION
.in +1c
.ti -1c
.BI "Q_UINT16 \fBqChecksum\fR ( const char * data, uint len )"
.br
.ti -1c
.BI "QDataStream & \fBoperator<<\fR ( QDataStream & s, const QByteArray & a )"
.br
.ti -1c
.BI "QDataStream & \fBoperator>>\fR ( QDataStream & s, QByteArray & a )"
.br
.in -1c
.SH DESCRIPTION
The QMemArray class is a template class that provides arrays of simple types.
.PP
QMemArray is implemented as a template class. Define a template instance QMemArray<X> to create an array that contains X items.
.PP
QMemArray stores the array elements directly in the array. It can only deal with simple types (i.e. C++ types, structs, and classes that have no constructors, destructors, or virtual functions). QMemArray uses bitwise operations to copy and compare array elements.
.PP
The QPtrVector collection class is also a kind of array. Like most collection classes, it uses pointers to the contained items.
.PP
QMemArray uses explicit sharing with a reference count. If more than one array shares common data and one of the arrays is modified, all the arrays are modified.
.PP
The benefit of sharing is that a program does not need to duplicate data when it is not required, which results in lower memory use and less copying of data.
.PP
An alternative to QMemArray is QValueVector. The QValueVector class also provides an array of objects, but can deal with objects that have constructors (specifically a copy constructor and a default constructor). QValueVector provides an STL-compatible syntax and is implicitly shared.
.PP
Example:
.PP
.nf
.br
    #include <qmemarray.h>
.br
    #include <stdio.h>
.br
.br
    QMemArray<int> fib( int num ) // returns fibonacci array
.br
    {
.br
        Q_ASSERT( num > 2 );
.br
        QMemArray<int> f( num ); // array of ints
.br
.br
        f[0] = f[1] = 1;
.br
        for ( int i = 2; i < num; i++ )
.br
            f[i] = f[i-1] + f[i-2];
.br
.br
        return f;
.br
    }
.br
.br
    int main()
.br
    {
.br
        QMemArray<int> a = fib( 6 ); // get first 6 fibonaccis
.br
        for ( int i = 0; i < a.size(); i++ )
.br
            qDebug( "%d: %d", i, a[i] );
.br
.br
        qDebug( "1 is found %d times", a.contains(1) );
.br
        qDebug( "5 is found at index %d", a.find(5) );
.br
.br
        return 0;
.br
    }
.br
.fi
.PP
Program output:
.PP
.nf
.br
    0: 1
.br
    1: 1
.br
    2: 2
.br
    3: 3
.br
    4: 5
.br
    5: 8
.br
    1 is found 2 times
.br
    5 is found at index 4
.br
.fi
.PP
Note concerning the use of QMemArray for manipulating structs or classes: Compilers will often pad the size of structs of odd sizes up to the nearest word boundary. This will then be the size QMemArray will use for its bitwise element comparisons. Because the remaining bytes will typically be uninitialized, this can cause find() etc. to fail to find the element. Example:
.PP
.nf
.br
    // MyStruct may be padded to 4 or 8 bytes
.br
    struct MyStruct
.br
    {
.br
        short i; // 2 bytes
.br
        char c;  // 1 byte
.br
    };
.br
.br
    QMemArray<MyStruct> a(1);
.br
    a[0].i = 5;
.br
    a[0].c = 't';
.br
.br
    MyStruct x;
.br
    x.i = '5';
.br
    x.c = 't';
.br
    int i = a.find( x ); // may return -1 if the pad bytes differ
.br
.fi
.PP
To work around this, make sure that you use a struct where sizeof() returns the same as the sum of the sizes of the members either by changing the types of the struct members or by adding dummy members.
.PP
QMemArray data can be traversed by iterators (see begin() and end()). The number of items is returned by count(). The array can be resized with resize() and filled using fill().
.PP
You can make a shallow copy of the array with assign() (or operator=()) and a deep copy with duplicate().
.PP
Search for values in the array with find() and contains(). For sorted arrays (see sort()) you can search using bsearch().
.PP
You can set the data directly using setRawData() and resetRawData(), although this requires care.
.PP
See also Shared Classes and Non-GUI Classes.
.SS "Member Type Documentation"
.SH "QMemArray::ConstIterator"
A const QMemArray iterator.
.PP
See also begin() and end().
.SH "QMemArray::Iterator"
A QMemArray iterator.
.PP
See also begin() and end().
.SH MEMBER FUNCTION DOCUMENTATION
.SH "QMemArray::QMemArray ( int, int )\fC [protected]\fR"
Constructs an array \fIwithout allocating\fR array space. The arguments should be (0, 0). Use at your own risk.
.SH "QMemArray::QMemArray ()"
Constructs a null array.
.PP
See also isNull().
.SH "QMemArray::QMemArray ( int size )"
Constructs an array with room for \fIsize\fR elements. Makes a null array if \fIsize\fR == 0.
.PP
The elements are left uninitialized.
.PP
See also resize() and isNull().
.SH "QMemArray::QMemArray ( const QMemArray<type> & a )"
Constructs a shallow copy of \fIa\fR.
.PP
See also assign().
.SH "QMemArray::~QMemArray ()"
Dereferences the array data and deletes it if this was the last reference.
.SH "QMemArray<type> & QMemArray::assign ( const QMemArray<type> & a )"
Shallow copy. Dereferences the current array and references the data contained in \fIa\fR instead. Returns a reference to this array.
.PP
See also operator=().
.SH "QMemArray<type> & QMemArray::assign ( const type * data, uint size )"
This is an overloaded member function, provided for convenience. It behaves essentially like the above function.
.PP
Shallow copy. Dereferences the current array and references the array data \fIdata\fR, which contains \fIsize\fR elements. Returns a reference to this array.
.PP
Do not delete \fIdata\fR later; QMemArray will call free() on it at the right time.
.SH "type & QMemArray::at ( uint index ) const"
Returns a reference to the element at position \fIindex\fR in the array.
.PP
This can be used to both read and set an element.
.PP
See also operator[]().
.SH "Iterator QMemArray::begin ()"
Returns an iterator pointing at the beginning of this array. This iterator can be used in the same way as the iterators of QValueList and QMap, for example.
.SH "ConstIterator QMemArray::begin () const"
This is an overloaded member function, provided for convenience. It behaves essentially like the above function.
.PP
Returns a const iterator pointing at the beginning of this array. This iterator can be used in the same way as the iterators of QValueList and QMap, for example.
.SH "int QMemArray::bsearch ( const type & v ) const"
In a sorted array (as sorted by sort()), finds the first occurrence of \fIv\fR by using a binary search. For a sorted array this is generally much faster than find(), which does a linear search.
.PP
Returns the position of \fIv\fR, or -1 if \fIv\fR could not be found.
.PP
See also sort() and find().
.SH "int QMemArray::contains ( const type & v ) const"
Returns the number of times \fIv\fR occurs in the array.
.PP
See also find().
.SH "QMemArray<type> QMemArray::copy () const"
Returns a deep copy of this array.
.PP
See also detach() and duplicate().
.SH "uint QMemArray::count () const"
Returns the same as size().
.PP
See also size().
.PP
Example: scribble/scribble.cpp.
.SH "type * QMemArray::data () const"
Returns a pointer to the actual array data.
.PP
The array is a null array if data() == 0 (null pointer).
.PP
See also isNull().
.PP
Examples:
.)l fileiconview/qfileiconview.cpp and network/networkprotocol/nntp.cpp.
.SH "void QMemArray::detach ()\fC [virtual]\fR"
Detaches this array from shared array data; i.e. it makes a private, deep copy of the data.
.PP
Copying will be performed only if the reference count is greater than one.
.PP
See also copy().
.PP
Reimplemented in QBitArray.
.SH "QMemArray<type> & QMemArray::duplicate ( const QMemArray<type> & a )"
Deep copy. Dereferences the current array and obtains a copy of the data contained in \fIa\fR instead. Returns a reference to this array.
.PP
See also copy().
.SH "QMemArray<type> & QMemArray::duplicate ( const type * data, uint size )"
This is an overloaded member function, provided for convenience. It behaves essentially like the above function.
.PP
Deep copy. Dereferences the current array and obtains a copy of the array data \fIdata\fR instead. Returns a reference to this array. The size of the array is given by \fIsize\fR.
.PP
See also copy().
.SH "Iterator QMemArray::end ()"
Returns an iterator pointing behind the last element of this array. This iterator can be used in the same way as the iterators of QValueList and QMap, for example.
.SH "ConstIterator QMemArray::end () const"
This is an overloaded member function, provided for convenience. It behaves essentially like the above function.
.PP
Returns a const iterator pointing behind the last element of this array. This iterator can be used in the same way as the iterators of QValueList and QMap, for example.
.SH "bool QMemArray::fill ( const type & v, int size = -1 )"
Fills the array with the value \fIv\fR. If \fIsize\fR is specified as different from -1, then the array will be resized before being filled.
.PP
Returns TRUE if successful, i.e. if \fIsize\fR is -1, or \fIsize\fR is != -1 and the memory can be allocated; otherwise returns FALSE.
.PP
See also resize().
.SH "int QMemArray::find ( const type & v, uint index = 0 ) const"
Finds the first occurrence of \fIv\fR, starting at position \fIindex\fR.
.PP
Returns the position of \fIv\fR, or -1 if \fIv\fR could not be found.
.PP
See also contains().
.SH "bool QMemArray::isEmpty () const"
Returns TRUE if the array is empty; otherwise returns FALSE.
.PP
isEmpty() is equivalent to isNull() for QMemArray (unlike QString).
.SH "bool QMemArray::isNull () const"
Returns TRUE if the array is null; otherwise returns FALSE.
.PP
A null array has size() == 0 and data() == 0.
.SH "uint QMemArray::nrefs () const"
Returns the reference count for the shared array data. This reference count is always greater than zero.
.SH "QMemArray::operator const type * () const"
Cast operator. Returns a pointer to the array.
.PP
See also data().
.SH "bool QMemArray::operator!= ( const QMemArray<type> & a ) const"
Returns TRUE if this array is different from \fIa\fR; otherwise returns FALSE.
.PP
The two arrays are compared bitwise.
.PP
See also operator==().
.SH "QMemArray<type> & QMemArray::operator= ( const QMemArray<type> & a )"
Assigns a shallow copy of \fIa\fR to this array and returns a reference to this array.
.PP
Equivalent to assign( a ).
.SH "bool QMemArray::operator== ( const QMemArray<type> & a ) const"
Returns TRUE if this array is equal to \fIa\fR; otherwise returns FALSE.
.PP
The two arrays are compared bitwise.
.PP
See also operator!=().
.SH "type & QMemArray::operator[] ( int index ) const"
Returns a reference to the element at position \fIindex\fR in the array.
.PP
This can be used to both read and set an element. Equivalent to at().
.PP
See also at().
.SH "void QMemArray::resetRawData ( const type * data, uint size )"
Removes internal references to the raw data that was set using setRawData(). This means that QMemArray no longer has access to the \fIdata\fR, so you are free to manipulate \fIdata\fR as you wish. You can now use the QMemArray without affecting the original \fIdata\fR, for example by calling setRawData() with a pointer to some other data.
.PP
The arguments must be the \fIdata\fR and length, \fIsize\fR, that were passed to setRawData(). This is for consistency checking.
.PP
See also setRawData().
.SH "bool QMemArray::resize ( uint size, Optimization optim )"
Resizes (expands or shrinks) the array to \fIsize\fR elements. The array becomes a null array if \fIsize\fR == 0.
.PP
Returns TRUE if successful, or FALSE if the memory cannot be allocated.
.PP
New elements are not initialized.
.PP
\fIoptim\fR is either \fCQGArray::MemOptim\fR (the default) or \fCQGArray::SpeedOptim\fR.
.PP
\fBNote:\fR By default, \fCSpeedOptim\fR is not available for general use since it is only available if Qt is built in a particular configuration.
.PP
See also size().
.PP
Example: fileiconview/qfileiconview.cpp.
.SH "bool QMemArray::resize ( uint size )"
This is an overloaded member function, provided for convenience. It behaves essentially like the above function.
.PP
Resizes (expands or shrinks) the array to \fIsize\fR elements. The array becomes a null array if \fIsize\fR == 0.
.PP
Returns TRUE if successful, i.e. if the memory can be allocated; otherwise returns FALSE.
.PP
New elements are not initialized.
.PP
See also size().
.SH "QMemArray<type> & QMemArray::setRawData ( const type * data, uint size )"
Sets raw data and returns a reference to the array.
.PP
Dereferences the current array and sets the new array data to \fIdata\fR and the new array size to \fIsize\fR. Do not attempt to resize or re-assign the array data when raw data has been set. Call resetRawData(\fIdata\fR, \fIsize\fR) to reset the array.
.PP
Setting raw data is useful because it sets QMemArray data without allocating memory or copying data.
.PP
Example I (intended use):
.PP
.nf
.br
    static char bindata[] = { 231, 1, 44, ... };
.br
    QByteArray  a;
.br
    a.setRawData( bindata, sizeof(bindata) );   // a points to bindata
.br
    QDataStream s( a, IO_ReadOnly );            // open on a's data
.br
    s >> <something>;                           // read raw bindata
.br
    a.resetRawData( bindata, sizeof(bindata) ); // finished
.br
.fi
.PP
Example II (you don't want to do this):
.PP
.nf
.br
    static char bindata[] = { 231, 1, 44, ... };
.br
    QByteArray  a, b;
.br
    a.setRawData( bindata, sizeof(bindata) );   // a points to bindata
.br
    a.resize( 8 );                              // will crash
.br
    b = a;                                      // will crash
.br
    a[2] = 123;                                 // might crash
.br
    // forget to resetRawData: will crash
.br
.fi
.PP
\fBWarning:\fR If you do not call resetRawData(), QMemArray will attempt to deallocate or reallocate the raw data, which might not be too good. Be careful.
.PP
See also resetRawData().
.SH "uint QMemArray::size () const"
Returns the size of the array (maximum number of elements).
.PP
The array is a null array if size() == 0.
.PP
See also isNull() and resize().
.SH "void QMemArray::sort ()"
Sorts the array elements in ascending order, using bitwise comparison (memcmp()).
.PP
See also bsearch().
.SH "bool QMemArray::truncate ( uint pos )"
Truncates the array at position \fIpos\fR.
.PP
Returns TRUE if successful, i.e. if the memory can be allocated; otherwise returns FALSE.
.PP
Equivalent to resize(\fIpos\fR).
.PP
See also resize().
.SH RELATED FUNCTION DOCUMENTATION
.SH "QDataStream & operator<< ( QDataStream & s, const QByteArray & a )"
Writes byte array \fIa\fR to the stream \fIs\fR and returns a reference to the stream.
.PP
See also Format of the QDataStream operators.
.SH "QDataStream & operator>> ( QDataStream & s, QByteArray & a )"
Reads a byte array into \fIa\fR from the stream \fIs\fR and returns a reference to the stream.
.PP
See also Format of the QDataStream operators.
.SH "Q_UINT16 qChecksum ( const char * data, uint len )"
Returns the CRC-16 checksum of \fIlen\fR bytes starting at \fIdata\fR.
.PP
The checksum is independent of the byte order (endianness).

.SH "SEE ALSO"
.BR http://doc.trolltech.com/qmemarray.html
.BR http://www.trolltech.com/faq/tech.html
.SH COPYRIGHT
Copyright 1992-2007 Trolltech ASA, http://www.trolltech.com.  See the
license file included in the distribution for a complete license
statement.
.SH AUTHOR
Generated automatically from the source code.
.SH BUGS
If you find a bug in Qt, please report it as described in
.BR http://doc.trolltech.com/bughowto.html .
Good bug reports help us to help you. Thank you.
.P
The definitive Qt documentation is provided in HTML format; it is
located at $QTDIR/doc/html and can be read using Qt Assistant or with
a web browser. This man page is provided as a convenience for those
users who prefer man pages, although this format is not officially
supported by Trolltech. 
.P
If you find errors in this manual page, please report them to
.BR qt-bugs@trolltech.com .
Please include the name of the manual page (qmemarray.3qt) and the Qt
version (3.3.8).