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.
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.
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.
An alternative to QMemArray is TQValueVector. The TQValueVector class also provides an array of objects, but can deal with objects that have constructors (specifically a copy constructor and a default constructor). TQValueVector provides an STL-compatible syntax and is implicitly shared.
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().
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.
Returns an iterator pointing at the beginning of this array. This iterator can be used in the same way as the iterators of TQValueList and TQMap, for example.
Returns a const iterator pointing at the beginning of this array. This iterator can be used in the same way as the iterators of TQValueList and TQMap, 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.
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.
Returns an iterator pointing behind the last element of this array. This iterator can be used in the same way as the iterators of TQValueList and TQMap, for example.
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 TQValueList and TQMap, for example.
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.
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
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.