Home | All Classes | Main Classes | Annotated | Grouped Classes | Functions |
The QMemArray class is a template class that provides arrays of simple types. More...
#include <qmemarray.h>
Inherited by QByteArray and QPointArray.
QMemArray is implemented as a template class. Define a template instance QMemArray<X> to create an array that contains X items.
QMemArray stores the array elements directly in the array. It can deal only 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.
The QPtrVector collection class is also a kind of array. Like most collection classes, it has pointers to the contained items.
QMemArray uses explicit sharing with a reference count. If more than one array share common data and one array is modified, all arrays will be modified.
The benefit of sharing is that a program does not need to duplicate data when it is not required, which results in less memory usage and less copying of data.
Example:
#include <qmemarray.h> #include <stdio.h> QMemArray<int> fib( int num ) // returns fibonacci array { Q_ASSERT( num > 2 ); QMemArray<int> f( num ); // array of ints f[0] = f[1] = 1; for ( int i = 2; i < num; i++ ) f[i] = f[i-1] + f[i-2]; return f; } int main() { QMemArray<int> a = fib( 6 ); // get 6 first fibonaccis for ( int i = 0; i < a.size(); i++ ) qDebug( "%d: %d", i, a[i] ); qDebug( "1 is found %d times", a.contains(1) ); qDebug( "5 is found at index %d", a.find(5) ); return 0; }
Program output:
0: 1 1: 1 2: 2 3: 3 4: 5 5: 8 1 is found 2 times 5 is found at index 4
Note about using 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:
// MyStruct may be padded to 4 or 8 bytes struct MyStruct { short i; // 2 bytes char c; // 1 byte }; QMemArray<MyStruct> a(1); a[0].i = 5; a[0].c = 't'; MyStruct x; x.i = '5'; x.c = 't'; int i = a.find( x ); // may return -1 if the pad bytes differ
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.
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().
You can make a shallow copy of the array with assign() (or operator=()) and a deep copy with duplicate().
Search for values in the array with find() and contains(). For sorted arrays (see sort()) you can search using bsearch().
You can set the data directly using setRawData() and resetRawData(), although this requires care.
See also Shared Classes and Non-GUI Classes.
See also isNull().
The elements are left uninitialized.
See also resize() and isNull().
See also assign().
See also operator=().
Shallow copy. Dereferences the current array and references the array data data, which contains size elements. Returns a reference to this array.
Do not delete data later; QMemArray will take care of it.
This can be used to both read and set an element.
See also operator[]().
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. In fact, not only does it behave like a usual pointer, it is a pointer.
Returns the position of v, or -1 if v could not be found.
See also find().
See also detach() and duplicate().
See also size().
Example: scribble/scribble.cpp.
The array is a null array if data() == 0 (null pointer).
See also isNull().
Examples: fileiconview/qfileiconview.cpp and network/networkprotocol/nntp.cpp.
Copying will be performed only if the reference count is greater than one.
See also copy().
Reimplemented in QBitArray.
See also copy().
Deep copy. Dereferences the current array and obtains a copy of the array data data instead. Returns a reference to this array. The size of the array is given by size.
See also copy().
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. In fact, not only does it behave like a usual pointer, it is a pointer.
Returns TRUE if successful, or FALSE if the memory cannot be allocated (only when size != -1).
See also resize().
Returns the position of v, or -1 if v could not be found.
See also contains().
isEmpty() is equivalent to isNull() for QMemArray (unlike QString).
Returns TRUE if the array is null; otherwise returns FALSE.
A null array has size() == 0 and data() == 0.
See also data().
The two arrays are compared bitwise.
See also operator==().
Equivalent to assign( a ).
The two arrays are compared bitwise.
See also operator!=().
This can be used to both read and set an element. Equivalent to at().
See also at().
The arguments must be the data and length, size, that were passed to setRawData(). This is for consistency checking.
See also setRawData().
Resizes (expands or shrinks) the array to size elements. The array becomes a null array if size == 0.
Returns TRUE if successful, or FALSE if the memory cannot be allocated.
New elements will not be initialized.
See also size().
Example: fileiconview/qfileiconview.cpp.
Sets raw data and returns a reference to the array.
Dereferences the current array and sets the new array data to data and the new array size to size. Do not attempt to resize or re-assign the array data when raw data has been set. Call resetRawData(data, size) to reset the array.
Setting raw data is useful because it sets QMemArray data without allocating memory or copying data.
Example I (intended use):
static char bindata[] = { 231, 1, 44, ... }; QByteArray a; a.setRawData( bindata, sizeof(bindata) ); // a points to bindata QDataStream s( a, IO_ReadOnly ); // open on a's data s >> <something>; // read raw bindata a.resetRawData( bindata, sizeof(bindata) ); // finished
Example II (you don't want to do this):
static char bindata[] = { 231, 1, 44, ... }; QByteArray a, b; a.setRawData( bindata, sizeof(bindata) ); // a points to bindata a.resize( 8 ); // will crash b = a; // will crash a[2] = 123; // might crash // forget to resetRawData: will crash
Warning: If you do not call resetRawData(), QMemArray will attempt to deallocate or reallocate the raw data, which might not be too good. Be careful.
See also resetRawData().
The array is a null array if size() == 0.
See also isNull() and resize().
See also bsearch().
Truncates the array at position pos.
Returns TRUE if successful, or FALSE if the memory cannot be allocated.
Equivalent to resize(pos).
See also resize().
See also Format of the QDataStream operators.
See also Format of the QDataStream operators.
The checksum is independent of the byte order (endianness).
This file is part of the Qt toolkit. Copyright © 1995-2002 Trolltech. All Rights Reserved.
Copyright © 2002 Trolltech | Trademarks | Qt version 3.0.4
|