Movatterモバイル変換


[0]ホーム

URL:


We bake cookies in your browser for a better experience. Using this site means that you consent.Read More

Menu

Qt Documentation

  • Qt 4.8
  • Qt3SupportLight
  • Q3MemArray

Q3MemArray Class

TheQ3MemArray class is a template class that provides arrays of simple types.More...

Header:#include <Q3MemArray>

Public Types

typedefConstIterator
typedefIterator

Public Functions

Q3MemArray()
Q3MemArray(int size)
Q3MemArray(const Q3MemArray<type> & a)
Q3MemArray(const QVector<type> & vector)
~Q3MemArray()
Q3MemArray<type> &assign(const Q3MemArray<type> & a)
Q3MemArray<type> &assign(const type * data, uint size)
type &at(uint index) const
Iteratorbegin()
ConstIteratorbegin() const
intbsearch(const type & v) const
intcontains(const type & v) const
Q3MemArray<type>copy() const
uintcount() const
type *data() const
Q3MemArray<type> &duplicate(const Q3MemArray<type> & a)
Q3MemArray<type> &duplicate(const type * data, uint size)
Iteratorend()
ConstIteratorend() const
boolfill(const type & v, int size = -1)
intfind(const type & v, uint index = 0) const
boolisEmpty() const
boolisNull() const
uintnrefs() const
voidresetRawData(const type * data, uint size)
boolresize(uint size, Optimization optim)
boolresize(uint size)
Q3MemArray<type> &setRawData(const type * data, uint size)
uintsize() const
voidsort()
booltruncate(uint pos)
operator QVector<type>() const
operator const type *() const
booloperator!=(const Q3MemArray<type> & a) const
Q3MemArray<type> &operator=(const Q3MemArray<type> & a)
booloperator==(const Q3MemArray<type> & a) const
type &operator[](int index) const

Reimplemented Public Functions

virtual voiddetach()

Protected Functions

Q3MemArray(int arg1, int arg2)

Detailed Description

TheQ3MemArray class is a template class that provides arrays of simple types.

Q3MemArray is implemented as a template class. Define a template instanceQ3MemArray<X> to create an array that contains X items.

Q3MemArray 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).Q3MemArray uses bitwise operations to copy and compare array elements.

TheQ3PtrVector collection class is also a kind of array. Like most old Qt collection classes, it uses pointers to the contained items.

Q3MemArray 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.

Example:

#include <q3memarray.h>#include <stdio.h>Q3MemArray<int> fib(int num )// returns fibonacci array{    Q_ASSERT( num>2 );Q3MemArray<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(){Q3MemArray<int> a= fib(6 );// get first 6 fibonaccisfor (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) );return0;}

Program output:

0:11:12:23:34:55:81 is found2 times5 is found at index4

Note concerning the use ofQ3MemArray 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 sizeQ3MemArray will use for its bitwise element comparisons. Because the remaining bytes will typically be uninitialized, this can causefind() etc. to fail to find the element. Example:

// MyStruct may be padded to 4 or 8 bytesstruct MyStruct{short i;// 2 byteschar c;// 1 byte};Q3MemArray<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.

Q3MemArray data can be traversed by iterators (seebegin() andend()). The number of items is returned bycount(). The array can be resized withresize() and filled usingfill().

You can make a shallow copy of the array withassign() (or operator=()) and a deep copy withduplicate().

Search for values in the array withfind() andcontains(). For sorted arrays (seesort()) you can search usingbsearch().

You can set the data directly usingsetRawData() andresetRawData(), although this requires care.

Member Type Documentation

typedef Q3MemArray::ConstIterator

A constQ3MemArray iterator.

See alsobegin() andend().

typedef Q3MemArray::Iterator

AQ3MemArray iterator.

See alsobegin() andend().

Member Function Documentation

[protected]Q3MemArray::Q3MemArray(int arg1,int arg2)

Constructs an arraywithout allocating array space. The argumentsarg1 andarg2 should be zero. Use at your own risk.

Q3MemArray::Q3MemArray()

Constructs a null array.

See alsoisNull().

Q3MemArray::Q3MemArray(int size)

Constructs an array with room forsize elements. Makes a null array ifsize == 0.

The elements are left uninitialized.

See alsoresize() andisNull().

Q3MemArray::Q3MemArray(constQ3MemArray<type> & a)

Constructs a shallow copy ofa.

See alsoassign().

Q3MemArray::Q3MemArray(constQVector<type> & vector)

Constructs a copy ofvector.

Q3MemArray::~Q3MemArray()

Dereferences the array data and deletes it if this was the last reference.

Q3MemArray<type> & Q3MemArray::assign(constQ3MemArray<type> & a)

Shallow copy. Dereferences the current array and references the data contained ina instead. Returns a reference to this array.

See alsooperator=().

Q3MemArray<type> & Q3MemArray::assign(consttype * data,uint size)

This is an overloaded function.

Shallow copy. Dereferences the current array and references the array datadata, which containssize elements. Returns a reference to this array.

Do not deletedata later;Q3MemArray will call free() on it at the right time.

type & Q3MemArray::at(uint index) const

Returns a reference to the element at positionindex in the array.

This can be used to both read and set an element.

See alsooperator[]().

Iterator Q3MemArray::begin()

Returns an iterator pointing at the beginning of this array. This iterator can be used in the same way as the iterators ofQ3ValueList andQMap, for example.

ConstIterator Q3MemArray::begin() const

This is an overloaded function.

Returns a const iterator pointing at the beginning of this array. This iterator can be used in the same way as the iterators ofQ3ValueList andQMap, for example.

int Q3MemArray::bsearch(consttype & v) const

In a sorted array (as sorted bysort()), finds the first occurrence ofv by using a binary search. For a sorted array this is generally much faster thanfind(), which does a linear search.

Returns the position ofv, or -1 ifv could not be found.

See alsosort() andfind().

int Q3MemArray::contains(consttype & v) const

Returns the number of timesv occurs in the array.

See alsofind().

Q3MemArray<type> Q3MemArray::copy() const

Returns a deep copy of this array.

See alsodetach() andduplicate().

uint Q3MemArray::count() const

Returns the same assize().

See alsosize().

type * Q3MemArray::data() const

Returns a pointer to the actual array data.

The array is a null array if data() == 0 (null pointer).

See alsoisNull().

[virtual]void Q3MemArray::detach()

Detaches this array from shared array data; i.e. it makes a private, deep copy of the data.

Copying will be performed only if thereference count is greater than one.

See alsocopy().

Q3MemArray<type> & Q3MemArray::duplicate(constQ3MemArray<type> & a)

Deep copy. Dereferences the current array and obtains a copy of the data contained ina instead. Returns a reference to this array.

See alsocopy().

Q3MemArray<type> & Q3MemArray::duplicate(consttype * data,uint size)

This is an overloaded function.

Deep copy. Dereferences the current array and obtains a copy of the array datadata instead. Returns a reference to this array. The size of the array is given bysize.

See alsocopy().

Iterator Q3MemArray::end()

Returns an iterator pointing behind the last element of this array. This iterator can be used in the same way as the iterators ofQ3ValueList andQMap, for example.

ConstIterator Q3MemArray::end() const

This is an overloaded function.

Returns a const iterator pointing behind the last element of this array. This iterator can be used in the same way as the iterators ofQ3ValueList andQMap, for example.

bool Q3MemArray::fill(consttype & v,int size = -1)

Fills the array with the valuev. Ifsize is specified as different from -1, then the array will be resized before being filled.

Returns TRUE if successful, i.e. ifsize is -1, orsize is != -1 and the memory can be allocated; otherwise returns FALSE.

See alsoresize().

int Q3MemArray::find(consttype & v,uint index = 0) const

Finds the first occurrence ofv, starting at positionindex.

Returns the position ofv, or -1 ifv could not be found.

See alsocontains().

bool Q3MemArray::isEmpty() const

Returns TRUE if the array is empty; otherwise returns FALSE.

isEmpty() is equivalent toisNull() forQ3MemArray (unlikeQString).

bool Q3MemArray::isNull() const

Returns TRUE if the array is null; otherwise returns FALSE.

A null array hassize() == 0 anddata() == 0.

uint Q3MemArray::nrefs() const

Returns the reference count for the shared array data. This reference count is always greater than zero.

void Q3MemArray::resetRawData(consttype * data,uint size)

Removes internal references to the raw data that was set usingsetRawData(). This means thatQ3MemArray no longer has access to thedata, so you are free to manipulatedata as you wish. You can now use theQ3MemArray without affecting the originaldata, for example by callingsetRawData() with a pointer to some other data.

The arguments must be thedata and length,size, that were passed tosetRawData(). This is for consistency checking.

See alsosetRawData().

bool Q3MemArray::resize(uint size,Optimization optim)

Resizes (expands or shrinks) the array tosize elements. The array becomes a null array ifsize == 0.

Returns TRUE if successful, or FALSE if the memory cannot be allocated.

New elements are not initialized.

optim is eitherMemOptim (the default) orSpeedOptim. When optimizing for speed rather than memory consumption, the array uses a smart grow and shrink algorithm that might allocate more memory than is actually needed forsize elements. This speeds up subsequent resize operations, for example when appending many elements to an array, since the space has already been allocated.

See alsosize().

bool Q3MemArray::resize(uint size)

This is an overloaded function.

Resizes (expands or shrinks) the array tosize elements. The array becomes a null array ifsize == 0.

Returns TRUE if successful, i.e. if the memory can be allocated; otherwise returns FALSE.

New elements are not initialized.

See alsosize().

Q3MemArray<type> & Q3MemArray::setRawData(consttype * data,uint size)

Sets raw data and returns a reference to the array.

Dereferences the current array and sets the new array data todata and the new array size tosize. Do not attempt to resize or re-assign the array data when raw data has been set. CallresetRawData(data,size) to reset the array.

Setting raw data is useful because it setsQ3MemArray data without allocating memory or copying data.

Example I (intended use):

staticchar bindata[]= {231,1,44,... };QByteArray      a;a.setRawData( bindata,sizeof(bindata) );// a points to bindataQDataStream s( a, IO_ReadOnly );// open on a's datas>><something>;// read raw bindataa.resetRawData( bindata,sizeof(bindata) );// finished

Example II (you don't want to do this):

staticchar bindata[]= {231,1,44,... };QByteArray      a, b;a.setRawData( bindata,sizeof(bindata) );// a points to bindataa.resize(8 );// will crashb= a;// will crasha[2]=123;// might crash// forget to resetRawData: will crash

Warning: If you do not callresetRawData(),Q3MemArray will attempt to deallocate or reallocate the raw data, which might not be too good. Be careful.

See alsoresetRawData().

uint Q3MemArray::size() const

Returns the size of the array (maximum number of elements).

The array is a null array if size() == 0.

See alsoisNull() andresize().

void Q3MemArray::sort()

Sorts the array elements in ascending order, using bitwise comparison (memcmp()).

See alsobsearch().

bool Q3MemArray::truncate(uint pos)

Truncates the array at positionpos.

Returns TRUE if successful, i.e. if the memory can be allocated; otherwise returns FALSE.

Equivalent to resize(pos).

See alsoresize().

Q3MemArray::operator QVector<type>() const

Automatically converts theQ3MemArray<type> into aQVector<type>.

Q3MemArray::operator const type *() const

Cast operator. Returns a pointer to the array.

See alsodata().

bool Q3MemArray::operator!=(constQ3MemArray<type> & a) const

Returns TRUE if this array is different froma; otherwise returns FALSE.

The two arrays are compared bitwise.

See alsooperator==().

Q3MemArray<type> & Q3MemArray::operator=(constQ3MemArray<type> & a)

Assigns a shallow copy ofa to this array and returns a reference to this array.

Equivalent to assign( a ).

bool Q3MemArray::operator==(constQ3MemArray<type> & a) const

Returns TRUE if this array is equal toa; otherwise returns FALSE.

The two arrays are compared bitwise.

See alsooperator!=().

type & Q3MemArray::operator[](int index) const

Returns a reference to the element at positionindex in the array.

This can be used to both read and set an element. Equivalent toat().

See alsoat().

© 2016 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of theGNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.


[8]ページ先頭

©2009-2025 Movatter.jp