'\" 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.

.PP \fC#include \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 & a )" .br .ti -1c .BI "\fB~QMemArray\fR ()" .br .ti -1c .BI "QMemArray & \fBoperator=\fR ( const QMemArray & 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 \fBcopy\fR () const" .br .ti -1c .BI "QMemArray & \fBassign\fR ( const QMemArray & a )" .br .ti -1c .BI "QMemArray & \fBassign\fR ( const type * data, uint size )" .br .ti -1c .BI "QMemArray & \fBduplicate\fR ( const QMemArray & a )" .br .ti -1c .BI "QMemArray & \fBduplicate\fR ( const type * data, uint size )" .br .ti -1c .BI "QMemArray & \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 & a ) const" .br .ti -1c .BI "bool \fBoperator!=\fR ( const QMemArray & 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 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 retquired, 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 .br #include .br .br QMemArray fib( int num ) // returns fibonacci array .br { .br Q_ASSERT( num > 2 ); .br QMemArray 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 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 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 retquires 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 & 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 & QMemArray::assign ( const QMemArray & 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 & 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 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 & QMemArray::duplicate ( const QMemArray & 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 & 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 & 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 & QMemArray::operator= ( const QMemArray & 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 & 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 & 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 >> ; // 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).