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.
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 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().
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.
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().
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.