File: qarray.html

package info (click to toggle)
qt-x11 3%3A2.3.1-22
  • links: PTS
  • area: main
  • in suites: woody
  • size: 48,524 kB
  • ctags: 46,337
  • sloc: cpp: 260,077; ansic: 32,457; makefile: 31,131; yacc: 2,444; sh: 1,513; lex: 480; perl: 422; xml: 68; lisp: 15
file content (348 lines) | stat: -rw-r--r-- 20,385 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Qt Toolkit - QArray Class</title><style type="text/css"><!--
h3.fn,span.fn { margin-left: 1cm; text-indent: -1cm; }
a:link { color: #004faf; text-decoration: none }
a:visited { color: #672967; text-decoration: none }body { background: white; color: black; }
--></style>
</head><body bgcolor="#ffffff">

<table width="100%">
<tr><td><a href="index.html">
<img width="100" height="100" src="qtlogo.png"
alt="Home" border="0"><img width="100"
height="100" src="face.png" alt="Home" border="0">
</a><td valign=top><div align=right><img src="dochead.png" width="472" height="27"><br>
<a href="classes.html"><b>Classes</b></a>
-<a href="annotated.html">Annotated</a>
- <a href="hierarchy.html">Tree</a>
-<a href="functions.html">Functions</a>
-<a href="index.html">Home</a>
-<a href="topicals.html"><b>Structure</b></a>
</div>
</table>

<h1 align=center>QArray Class Reference</h1><br clear="all">
<p>
The QArray class is a template class that provides arrays of simple types.
<a href="#details">More...</a>
<p>
<code>#include &lt;<a href="qarray-h.html">qarray.h</a>&gt;</code>
<p>
Inherits <a href="qgarray.html">QGArray</a>.
<p>Inherited by <a href="qbytearray.html">QByteArray</a> and <a href="qpointarray.html">QPointArray</a>.
<p><a href="qarray-members.html">List of all member functions.</a>
<h2>Public Members</h2>
<ul>
<li><div class="fn"><a href="#1ada39"><b>QArray</b></a>()</div>
<li><div class="fn"><a href="#b4b325"><b>QArray</b></a>(intsize)</div>
<li><div class="fn"><a href="#5600cd"><b>QArray</b></a>(constQArray&lt;type&gt;&amp;a)</div>
<li><div class="fn"><a href="#6ebb0e"><b>~QArray</b></a>()</div>
<li><div class="fn">QArray&lt;type&gt;&amp;<a href="#fd241d"><b>operator=</b></a>(constQArray&lt;type&gt;&amp;a)</div>
<li><div class="fn">type*<a href="#48a062"><b>data</b></a>()const</div>
<li><div class="fn">uint<a href="#985e32"><b>nrefs</b></a>()const</div>
<li><div class="fn">uint<a href="#169918"><b>size</b></a>()const</div>
<li><div class="fn">uint<a href="#b7e5f4"><b>count</b></a>()const</div>
<li><div class="fn">bool<a href="#a6ff66"><b>isEmpty</b></a>()const</div>
<li><div class="fn">bool<a href="#513bb5"><b>isNull</b></a>()const</div>
<li><div class="fn">bool<a href="#a3ecbd"><b>resize</b></a>(uintsize)</div>
<li><div class="fn">bool<a href="#d24a82"><b>truncate</b></a>(uintpos)</div>
<li><div class="fn">bool<a href="#636db1"><b>fill</b></a>(consttype&amp;v, intsize=-1)</div>
<li><div class="fn">virtualvoid<a href="#1d6bcc"><b>detach</b></a>()</div>
<li><div class="fn">QArray&lt;type&gt;<a href="#f7f6c1"><b>copy</b></a>()const</div>
<li><div class="fn">QArray&lt;type&gt;&amp;<a href="#fafab6"><b>assign</b></a>(constQArray&lt;type&gt;&amp;a)</div>
<li><div class="fn">QArray&lt;type&gt;&amp;<a href="#30ba42"><b>assign</b></a>(consttype*data, uintsize)</div>
<li><div class="fn">QArray&lt;type&gt;&amp;<a href="#47e366"><b>duplicate</b></a>(constQArray&lt;type&gt;&amp;a)</div>
<li><div class="fn">QArray&lt;type&gt;&amp;<a href="#6d83ae"><b>duplicate</b></a>(consttype*data, uintsize)</div>
<li><div class="fn">QArray&lt;type&gt;&amp;<a href="#3ea380"><b>setRawData</b></a>(consttype*data, uintsize)</div>
<li><div class="fn">void<a href="#27e2b3"><b>resetRawData</b></a>(consttype*data, uintsize)</div>
<li><div class="fn">int<a href="#d9014f"><b>find</b></a>(consttype&amp;v, uintindex=0)const</div>
<li><div class="fn">int<a href="#4768be"><b>contains</b></a>(consttype&amp;v)const</div>
<li><div class="fn">void<a href="#a0dcc3"><b>sort</b></a>()</div>
<li><div class="fn">int<a href="#6ac1f4"><b>bsearch</b></a>(consttype&amp;v)const</div>
<li><div class="fn">type&amp;<a href="#d51ed9"><b>operator[]</b></a>(intindex)const</div>
<li><div class="fn">type&amp;<a href="#4cc327"><b>at</b></a>(uintindex)const</div>
<li><div class="fn">operator<a href=qarray.html#4b2061><b>consttype*</b></a>()const</div>
<li><div class="fn">bool<a href="#623b8b"><b>operator==</b></a>(constQArray&lt;type&gt;&amp;a)const</div>
<li><div class="fn">bool<a href="#dfc5c8"><b>operator!=</b></a>(constQArray&lt;type&gt;&amp;a)const</div>
<li><div class="fn">Iterator<a href="#0419f0"><b>begin</b></a>()</div>
<li><div class="fn">Iterator<a href="#a3714e"><b>end</b></a>()</div>
<li><div class="fn">ConstIterator<a href="#4ce9de"><b>begin</b></a>()const</div>
<li><div class="fn">ConstIterator<a href="#7182f1"><b>end</b></a>()const</div>
</ul>
<h2>Protected Members</h2>
<ul>
<li><div class="fn"><a href="#fea902"><b>QArray</b></a>(int, int)</div>
</ul>
<hr><h2><a name="details"></a>Detailed Description</h2>
The QArray class is a template class that provides arrays of simple types.
<p>
QArray is implemented as a template class. Define a template
instance QArray&lt;X&gt; to create an array that contains X items.
<p>QArray 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.  QArray uses
bitwise operations to copy and compare array elements.
<p>The <a href="qvector.html">QVector</a> collection class is also a kind of array.  Like most
<a href="collection.html">collection classes</a>, it has pointers to the
contained items.
<p>QArray uses explicit <a href="shclass.html">sharing</a> with a reference
count.  If more than one array share common data, and one array is
modified, all arrays will be modified.
<p>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.
<p>Example:
<pre>    #include &lt;qarray.h&gt;
    #include &lt;stdio.h&gt;

    <a href="qarray.html">QArray</a>&lt;int&gt; fib( int num )                  // returns fibonacci array
    {
        ASSERT( num &gt; 2 );
        <a href="qarray.html">QArray</a>&lt;int&gt; f( num );                   // array of ints

        f[0] = f[1] = 1;                        // initialize first two numbers
        for ( int i=2; i&lt;num; i++ )
            f[i] = f[i-1] + f[i-2];     

        return f;
    }

    void main()
    {
        <a href="qarray.html">QArray</a>&lt;int&gt; a = fib( 6 );               // get 6 first fibonaccis
        int i;

        for ( i=0; i&lt;a.<a href="#169918">size</a>(); i++ )            // print them
            prinf( "%d: %d\n", i, a[i] );

        printf( "1 is found %d time(s)\n", a.<a href="#4768be">contains</a>(1) );
        printf( "5 is found at index %d\n", a.<a href="#d9014f">find</a>(5) );
    }
</pre>
<p>Program output:
<pre>        0: 1
        1: 1
        2: 2
        3: 3
        4: 5
        5: 8
        1 is found 2 times
        5 is found at index 4
</pre>
<p>Note about using QArray 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 QArray will use
for its bitwise element comparisons. Since the remaining bytes will
typically be uninitialized, this can cause <a href="#d9014f">find</a>() etc. to fail to
find the element. Example:
<p><pre>    struct MyStruct
    {
      short i;                    // 2 bytes
      char c;                     // 1 byte
    };                            // sizeof(MyStruct) may be padded to 4 bytes

    <a href="qarray.html">QArray</a>&lt;MyStruct&gt; a(1);
    a[0].i = 5;
    a[0].c = 't';

    MyStruct x;
    x.i = '5';
    x.c = 't';
    int i = a.<a href="#d9014f">find</a>( x );          // May return -1 if the pad bytes differ
</pre>
<p>To workaround 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.
<p>See also  <a href="shclass.html">Shared Classes</a>

<hr><h2>Member Function Documentation</h2>
<h3 class="fn"><a name="1ada39"></a>QArray::QArray()</h3>
<p>Constructs a null array.
<p>See also  <a href="#513bb5">isNull</a>().
<h3 class="fn"><a name="5600cd"></a>QArray::QArray(constQArray&lt;type&gt;&amp;a)</h3>
<p>Constructs a shallow copy of <em>a.</em>
<p>See also  <a href="#30ba42">assign</a>().
<h3 class="fn"><a name="b4b325"></a>QArray::QArray(intsize)</h3>
<p>Constructs an array with room for <em>size</em> elements.
Makes a null array if <em>size</em> == 0.
<p>Note that the elements are not initialized.
<p>See also  <a href="#a3ecbd">resize</a>() and <a href="#513bb5">isNull</a>().
<h3 class="fn"><a name="fea902"></a>QArray::QArray(int, int) <code>[protected]</code></h3>
<p>Constructs an array <em>without allocating</em> array space.
The arguments should be (0, 0). Use at own risk.
<h3 class="fn"><a name="6ebb0e"></a>QArray::~QArray()</h3>
<p>Dereferences the array data and deletes it if this was the last
reference.
<h3 class="fn"><a name="4b2061"></a>QArray::operatorconsttype*()const</h3>
<p>Cast operator.  Returns a pointer to the array.
<p>See also  <a href="#48a062">data</a>().
<h3 class="fn">QArray&lt;type&gt;&amp;<a name="fafab6"></a>QArray::assign(constQArray&lt;type&gt;&amp;a)</h3>
<p>Shallow copy. Dereferences the current array and references the data
contained in <em>a</em> instead. Returns a reference to this array.
<p>See also  <a href="#fd241d">operator=</a>().
<h3 class="fn">QArray&lt;type&gt;&amp;<a name="30ba42"></a>QArray::assign(consttype*data, uintsize)</h3>
<p>Shallow copy. Dereferences the current array and references the
array data <em>data,</em> which contains <em>size</em> elements.
Returns a reference to this array.
<p>Do not delete <em>data</em> later, QArray takes care of that.
<h3 class="fn">type&amp;<a name="4cc327"></a>QArray::at(uintindex)const</h3>
<p>Returns a reference to the element at position <em>index</em> in the array.
<p>This can be used to both read and set an element.
<p>See also  <a href="#d51ed9">operator[]</a>().
<h3 class="fn">ConstIterator<a name="4ce9de"></a>QArray::begin()const</h3>
<p>Returns a const iterator pointing at the beginning of this array.
This iterator can be used as the iterators of <a href="qvaluelist.html">QValueList</a> and <a href="qmap.html">QMap</a>
for example. In fact it does not only behave like a usual pointer:
It is a pointer.
<h3 class="fn">Iterator<a name="0419f0"></a>QArray::begin()</h3>
<p>Returns an iterator pointing at the beginning of this array.
This iterator can be used as the iterators of <a href="qvaluelist.html">QValueList</a> and <a href="qmap.html">QMap</a>
for example. In fact it does not only behave like a usual pointer:
It is a pointer.
<h3 class="fn">int<a name="6ac1f4"></a>QArray::bsearch(consttype&amp;v)const</h3>
<p>In a sorted array, finds the first occurrence of <em>v</em> using binary
search. For a sorted array, this is generally much faster than
<a href="#d9014f">find</a>(), which does a linear search.
<p>Returns the position of <em>v,</em> or -1 if <em>v</em> could not be found.
<p>See also  <a href="#a0dcc3">sort</a>() and <a href="#d9014f">find</a>().
<h3 class="fn">int<a name="4768be"></a>QArray::contains(consttype&amp;v)const</h3>
<p>Returns the number of times <em>v</em> occurs in the array.
<p>See also  <a href="#d9014f">find</a>().
<h3 class="fn">QArray&lt;type&gt;<a name="f7f6c1"></a>QArray::copy()const</h3>
<p>Returns a deep copy of this array.
<p>See also  <a href="#1d6bcc">detach</a>() and <a href="#47e366">duplicate</a>().
<h3 class="fn">uint<a name="b7e5f4"></a>QArray::count()const</h3>
<p>Returns the same as <a href="#169918">size</a>().
<p>See also  <a href="#169918">size</a>().
<h3 class="fn">type*<a name="48a062"></a>QArray::data()const</h3>
<p>Returns a pointer to the actual array data.
<p>The array is a null array if data() == 0 (null pointer).
<p>See also  <a href="#513bb5">isNull</a>().
<h3 class="fn">void<a name="1d6bcc"></a>QArray::detach() <code>[virtual]</code></h3>
<p>Detaches this array from shared array data, i.e. makes a private, deep
copy of the data.
<p>Copying will only be performed if the
<a href="#985e32">reference count</a> is greater than one.
<p>See also  <a href="#f7f6c1">copy</a>().
<p>Reimplemented from <a href="qgarray.html#9b33f3">QGArray.</a>
<h3 class="fn">QArray&lt;type&gt;&amp;<a name="47e366"></a>QArray::duplicate(constQArray&lt;type&gt;&amp;a)</h3>
<p>Deep copy. Dereferences the current array and obtains a copy of the data
contained in <em>a</em> instead. Returns a reference to this array.
<p>See also  <a href="#f7f6c1">copy</a>().
<h3 class="fn">QArray&lt;type&gt;&amp;<a name="6d83ae"></a>QArray::duplicate(consttype*data, uintsize)</h3>
<p>Deep copy. Dereferences the current array and obtains a copy of the
array data <em>data</em> instead.  Returns a reference to this array.
<p>See also  <a href="#f7f6c1">copy</a>().
<h3 class="fn">ConstIterator<a name="7182f1"></a>QArray::end()const</h3>
<p>Returns a const iterator pointing behind the last element of this array.
This iterator can be used as the iterators of <a href="qvaluelist.html">QValueList</a> and <a href="qmap.html">QMap</a>
for example. In fact it does not only behave like a usual pointer:
It is a pointer.
<h3 class="fn">Iterator<a name="a3714e"></a>QArray::end()</h3>
<p>Returns an iterator pointing behind the last element of this array.
This iterator can be used as the iterators of <a href="qvaluelist.html">QValueList</a> and <a href="qmap.html">QMap</a>
for example. In fact it does not only behave like a usual pointer:
It is a pointer.
<h3 class="fn">bool<a name="636db1"></a>QArray::fill(consttype&amp;v, intsize=-1)</h3>
<p>Fills the array with the value <em>v.</em> If <em>size</em> is specified as different
from -1, then the array will be resized before filled.
<p>Returns TRUE if successful, or FALSE if the memory cannot be allocated
(only when <em>size</em> != -1).
<p>See also  <a href="#a3ecbd">resize</a>().
<h3 class="fn">int<a name="d9014f"></a>QArray::find(consttype&amp;v, uintindex=0)const</h3>
<p>Finds the first occurrence of <em>v,</em> starting at position <em>index.</em>
<p>Returns the position of <em>v,</em> or -1 if <em>v</em> could not be found.
<p>See also  <a href="#4768be">contains</a>().
<h3 class="fn">bool<a name="a6ff66"></a>QArray::isEmpty()const</h3>
<p>Returns TRUE if the array is empty, i.e. <a href="#169918">size</a>() == 0, otherwise FALSE.
<p>isEmpty() is equivalent with <a href="#513bb5">isNull</a>() for QArray.  Note that this is not
the case for <a href="qcstring.html#180a48">QCString::isEmpty</a>().
<h3 class="fn">bool<a name="513bb5"></a>QArray::isNull()const</h3>
<p>Returns TRUE if the array is null, otherwise FALSE.
<p>A null array has <a href="#169918">size</a>() == 0 and <a href="#48a062">data</a>() == 0.
<h3 class="fn">uint<a name="985e32"></a>QArray::nrefs()const</h3>
<p>Returns the reference count for the shared array data. This reference count
is always greater than zero.
<h3 class="fn">bool<a name="dfc5c8"></a>QArray::operator!=(constQArray&lt;type&gt;&amp;a)const</h3>
<p>Returns TRUE if this array is different from <em>a,</em> otherwise FALSE.
<p>The two arrays are bitwise compared.
<p>See also  <a href="#623b8b">operator==</a>().
<h3 class="fn">QArray&lt;type&gt;&amp;<a name="fd241d"></a>QArray::operator=(constQArray&lt;type&gt;&amp;a)</h3>
<p>Assigns a shallow copy of <em>a</em> to this array and returns a reference
to this array.
<p>Equivalent to <a href="#30ba42">assign</a>( a ).
<h3 class="fn">bool<a name="623b8b"></a>QArray::operator==(constQArray&lt;type&gt;&amp;a)const</h3>
<p>Returns TRUE if this array is equal to <em>a,</em> otherwise FALSE.
<p>The two arrays are bitwise compared.
<p>See also  <a href="#dfc5c8">operator!=</a>().
<h3 class="fn">type&amp;<a name="d51ed9"></a>QArray::operator[](intindex)const</h3>
<p>Returns a reference to the element at position <em>index</em> in the array.
<p>This can be used to both read and set an element.  Equivalent to <a href="#4cc327">at</a>().
<p>See also  <a href="#4cc327">at</a>().
<h3 class="fn">void<a name="27e2b3"></a>QArray::resetRawData(consttype*data, uintsize)</h3>
<p>Resets raw data that was set using <a href="#3ea380">setRawData</a>().
<p>The arguments must be the data and length that were passed to
setRawData().  This is for consistency checking.
<p>See also  <a href="#3ea380">setRawData</a>().
<h3 class="fn">bool<a name="a3ecbd"></a>QArray::resize(uintsize)</h3>
<p>Resizes (expands or shrinks) the array to <em>size</em> elements. The array
becomes a null array if <em>size</em> == 0.
<p>Returns TRUE if successful, or FALSE if the memory cannot be allocated.
<p>New elements will not be initialized.
<p>See also  <a href="#169918">size</a>().
<h3 class="fn">QArray&lt;type&gt;&amp;<a name="3ea380"></a>QArray::setRawData(consttype*data, uintsize)</h3>
<p>Sets raw data and returns a reference to the array.
<p>Dereferences the current array and sets the new array data to <em>data</em> and
the new array size to <em>size.</em>  Do not attempt to resize or re-assign the
array data when raw data has been set.
Call <a href="#27e2b3">resetRawData</a>(d,len) to reset the array.
<p>Setting raw data is useful because it sets QArray data without allocating
memory or copying data.
<p>Example I (intended use):
<pre>    static char bindata[] = { 231, 1, 44, ... };
    <a href="qbytearray.html">QByteArray</a>  a;
    a.<a href="qbytearray.html#886dfd">setRawData</a>( bindata, sizeof(bindata) );   // a points to bindata
    <a href="qdatastream.html">QDataStream</a> s( a, IO_ReadOnly );            // open on a's data
    s &gt;&gt; &lt;something&gt;;                           // read raw bindata
    a.<a href="qbytearray.html#6a0f7e">resetRawData</a>( bindata, sizeof(bindata) ); // finished
</pre>
<p>Example II (you don't want to do this):
<pre>    static char bindata[] = { 231, 1, 44, ... };
    <a href="qbytearray.html">QByteArray</a>  a, b;
    a.<a href="qbytearray.html#886dfd">setRawData</a>( bindata, sizeof(bindata) );   // a points to bindata
    a.<a href="qbytearray.html#ba23bf">resize</a>( 8 );                              // will crash
    b = a;                                      // will crash
    a[2] = 123;                                 // might crash
      // forget to resetRawData - will crash
</pre>
<p><b>Warning:</b> If you do not call resetRawData(), QArray will attempt to
deallocate or reallocate the raw data, which might not be too good.
Be careful.
<p>See also  <a href="#27e2b3">resetRawData</a>().
<h3 class="fn">uint<a name="169918"></a>QArray::size()const</h3>
<p>Returns the size of the array (max number of elements).
<p>The array is a null array if size() == 0.
<p>See also  <a href="#513bb5">isNull</a>() and <a href="#a3ecbd">resize</a>().
<h3 class="fn">void<a name="a0dcc3"></a>QArray::sort()</h3>
<p>Sorts the array elements in ascending order, using bitwise
comparison (memcmp()).
<p>See also  <a href="#6ac1f4">bsearch</a>().
<h3 class="fn">bool<a name="d24a82"></a>QArray::truncate(uintpos)</h3>
<p>Truncates the array at position <em>pos.</em>
<p>Returns TRUE if successful, or FALSE if the memory cannot be allocated.
<p>Equivalent to <a href="#a3ecbd">resize</a>(<em>pos).</em>
<p>See also  <a href="#a3ecbd">resize</a>().
<hr><p>
Search the documentation, FAQ, qt-interest archive and more (uses
<a href="http://www.trolltech.com">www.trolltech.com</a>):<br>
<form method=post action="http://www.trolltech.com/search.cgi">
<input type=hidden name="version" value="2.3.1"><nobr>
<input size="50" name="search"><input type=submit value="Search">
</nobr></form><hr><p>
This file is part of the <a href="index.html">Qt toolkit</a>,
copyright &copy; 1995-2000
<a href="http://www.trolltech.com">Trolltech</a>, all rights reserved.<p><address><hr><div align="center">
<table width="100%" cellspacing="0" border="0"><tr>
<td>Copyright  2000 Trolltech<td><a href="http://www.trolltech.com/trademarks.html">Trademarks</a>
<td align="right"><div align="right">Qt version 2.3.1</div>
</table></div></address></body></html>