source: trunk/doc/html/qvaluevector-h.html@ 208

Last change on this file since 208 was 190, checked in by rudi, 14 years ago

reference documentation added

File size: 15.0 KB
Line 
1<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
2<!-- /home/espenr/tmp/qt-3.3.8-espenr-2499/qt-x11-free-3.3.8/include/qvaluevector.h:1 -->
3<html>
4<head>
5<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
6<title>qvaluevector.h Include File</title>
7<style type="text/css"><!--
8fn { margin-left: 1cm; text-indent: -1cm; }
9a:link { color: #004faf; text-decoration: none }
10a:visited { color: #672967; text-decoration: none }
11body { background: #ffffff; color: black; }
12--></style>
13</head>
14<body>
15
16<table border="0" cellpadding="0" cellspacing="0" width="100%">
17<tr bgcolor="#E5E5E5">
18<td valign=center>
19 <a href="index.html">
20<font color="#004faf">Home</font></a>
21 | <a href="classes.html">
22<font color="#004faf">All&nbsp;Classes</font></a>
23 | <a href="mainclasses.html">
24<font color="#004faf">Main&nbsp;Classes</font></a>
25 | <a href="annotated.html">
26<font color="#004faf">Annotated</font></a>
27 | <a href="groups.html">
28<font color="#004faf">Grouped&nbsp;Classes</font></a>
29 | <a href="functions.html">
30<font color="#004faf">Functions</font></a>
31</td>
32<td align="right" valign="center"><img src="logo32.png" align="right" width="64" height="32" border="0"></td></tr></table><h1 align=center>qvaluevector.h</h1>
33
34<p>This is the verbatim text of the qvaluevector.h include file. It is provided only for illustration; the copyright remains with Trolltech.
35<hr>
36<pre>
37/****************************************************************************
38** $Id: qvaluevector-h.html 2051 2007-02-21 10:04:20Z chehrlic $
39**
40** Definition of QValueVector class
41**
42** Copyright (C) 1992-2007 Trolltech ASA. All rights reserved.
43**
44** This file is part of the tools module of the Qt GUI Toolkit.
45**
46** This file may be distributed under the terms of the Q Public License
47** as defined by Trolltech ASA of Norway and appearing in the file
48** LICENSE.QPL included in the packaging of this file.
49**
50** This file may be distributed and/or modified under the terms of the
51** GNU General Public License version 2 as published by the Free Software
52** Foundation and appearing in the file LICENSE.GPL included in the
53** packaging of this file.
54**
55** Licensees holding valid Qt Enterprise Edition or Qt Professional Edition
56** licenses may use this file in accordance with the Qt Commercial License
57** Agreement provided with the Software.
58**
59** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
60** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
61**
62** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
63** information about Qt Commercial License Agreements.
64** See http://www.trolltech.com/qpl/ for QPL licensing information.
65** See http://www.trolltech.com/gpl/ for GPL licensing information.
66**
67** Contact info@trolltech.com if any conditions of this licensing are
68** not clear to you.
69**
70**********************************************************************/
71
72#ifndef QVALUEVECTOR_H
73#define QVALUEVECTOR_H
74
75#ifndef QT_H
76#include "qtl.h"
77#include "qshared.h"
78#include "qdatastream.h"
79#endif // QT_H
80
81#ifndef QT_NO_STL
82#include &lt;vector&gt;
83#endif
84
85template &lt;class T&gt;
86class QValueVectorPrivate : public QShared
87{
88public:
89 typedef T value_type;
90 typedef T* pointer;
91
92 QValueVectorPrivate()
93 : start( 0 ), finish( 0 ), end( 0 )
94 {
95 }
96
97 QValueVectorPrivate( const QValueVectorPrivate&lt;T&gt;&amp; x );
98 QValueVectorPrivate( size_t size );
99
100 void derefAndDelete() // work-around for hp-cc
101 {
102 if ( deref() )
103 delete this;
104 }
105
106#if defined(Q_TEMPLATEDLL)
107 // Workaround MS bug in memory de/allocation in DLL vs. EXE
108 virtual
109#endif
110 ~QValueVectorPrivate()
111 {
112 delete[] start;
113 }
114
115 size_t size() const
116 {
117 return finish - start;
118 }
119
120 bool empty() const
121 {
122 return start == finish;
123 }
124
125 size_t capacity() const
126 {
127 return end - start;
128 }
129
130 void insert( pointer pos, const T&amp; x );
131 void insert( pointer pos, size_t n, const T&amp; x );
132 void reserve( size_t n );
133
134 void clear()
135 {
136 delete[] start;
137 start = 0;
138 finish = 0;
139 end = 0;
140 }
141
142
143 pointer start;
144 pointer finish;
145 pointer end;
146
147private:
148 pointer growAndCopy( size_t n, pointer s, pointer f );
149
150 QValueVectorPrivate&lt;T&gt;&amp; operator=( const QValueVectorPrivate&lt;T&gt;&amp; x );
151
152};
153
154template &lt;class T&gt;
155Q_INLINE_TEMPLATES QValueVectorPrivate&lt;T&gt;::QValueVectorPrivate( const QValueVectorPrivate&lt;T&gt;&amp; x )
156 : QShared()
157{
158 size_t i = x.size();
159 if ( i &gt; 0 ) {
160 start = new T[ i ];
161 finish = start + i;
162 end = start + i;
163#if defined(__xlC__) &amp;&amp; __xlC__ &lt; 0x400 // xlC 3.6 confused by const
164 qCopy( (pointer)x.start, (pointer)x.finish, start );
165#else
166 qCopy( x.start, x.finish, start );
167#endif
168 } else {
169 start = 0;
170 finish = 0;
171 end = 0;
172 }
173}
174
175template &lt;class T&gt;
176Q_INLINE_TEMPLATES QValueVectorPrivate&lt;T&gt;::QValueVectorPrivate( size_t size )
177{
178 if ( size &gt; 0 ) {
179 start = new T[size];
180 finish = start + size;
181 end = start + size;
182 } else {
183 start = 0;
184 finish = 0;
185 end = 0;
186 }
187}
188
189template &lt;class T&gt;
190Q_INLINE_TEMPLATES void QValueVectorPrivate&lt;T&gt;::insert( pointer pos, const T&amp; x )
191{
192 const size_t lastSize = size();
193 const size_t n = lastSize !=0 ? 2*lastSize : 1;
194 const size_t offset = pos - start;
195 pointer newStart = new T[n];
196 pointer newFinish = newStart + offset;
197 qCopy( start, pos, newStart );
198 *newFinish = x;
199 qCopy( pos, finish, ++newFinish );
200 delete[] start;
201 start = newStart;
202 finish = newStart + lastSize + 1;
203 end = newStart + n;
204}
205
206template &lt;class T&gt;
207Q_INLINE_TEMPLATES void QValueVectorPrivate&lt;T&gt;::insert( pointer pos, size_t n, const T&amp; x )
208{
209 if ( size_t( end - finish ) &gt;= n ) {
210 // enough room
211 const size_t elems_after = finish - pos;
212 pointer old_finish = finish;
213 if ( elems_after &gt; n ) {
214 qCopy( finish - n, finish, finish );
215 finish += n;
216 qCopyBackward( pos, old_finish - n, old_finish );
217 qFill( pos, pos + n, x );
218 } else {
219 pointer filler = finish;
220 size_t i = n - elems_after;
221 for ( ; i &gt; 0; --i, ++filler )
222 *filler = x;
223 finish += n - elems_after;
224 qCopy( pos, old_finish, finish );
225 finish += elems_after;
226 qFill( pos, old_finish, x );
227 }
228 } else {
229 // not enough room
230 const size_t lastSize = size();
231 const size_t len = lastSize + QMAX( lastSize, n );
232 pointer newStart = new T[len];
233 pointer newFinish = qCopy( start, pos, newStart );
234 // fill up inserted space
235 size_t i = n;
236 for ( ; i &gt; 0; --i, ++newFinish )
237 *newFinish = x;
238 newFinish = qCopy( pos, finish, newFinish );
239 delete[] start;
240 start = newStart;
241 finish = newFinish;
242 end = newStart + len;
243 }
244}
245
246template &lt;class T&gt;
247Q_INLINE_TEMPLATES void QValueVectorPrivate&lt;T&gt;::reserve( size_t n )
248{
249 const size_t lastSize = size();
250 pointer tmp = growAndCopy( n, start, finish );
251 start = tmp;
252 finish = tmp + lastSize;
253 end = start + n;
254}
255
256template &lt;class T&gt;
257Q_INLINE_TEMPLATES Q_TYPENAME QValueVectorPrivate&lt;T&gt;::pointer QValueVectorPrivate&lt;T&gt;::growAndCopy( size_t n, pointer s, pointer f )
258{
259 pointer newStart = new T[n];
260 qCopy( s, f, newStart );
261 delete[] start;
262 return newStart;
263}
264
265template &lt;class T&gt; class QDeepCopy;
266
267template &lt;class T&gt;
268class QValueVector
269{
270public:
271 typedef T value_type;
272 typedef value_type* pointer;
273 typedef const value_type* const_pointer;
274 typedef value_type* iterator;
275 typedef const value_type* const_iterator;
276 typedef value_type&amp; reference;
277 typedef const value_type&amp; const_reference;
278 typedef size_t size_type;
279#ifndef QT_NO_STL
280 typedef ptrdiff_t difference_type;
281#else
282 typedef int difference_type;
283#endif
284
285 QValueVector()
286 {
287 sh = new QValueVectorPrivate&lt;T&gt;;
288 }
289
290 QValueVector( const QValueVector&lt;T&gt;&amp; v )
291 {
292 sh = v.sh;
293 sh-&gt;ref();
294 }
295
296 QValueVector( size_type n, const T&amp; val = T() );
297
298#ifndef QT_NO_STL
299 QValueVector( std::vector&lt;T&gt;&amp; v ) // ### remove in 4.0
300 {
301 sh = new QValueVectorPrivate&lt;T&gt;( v.size() );
302 qCopy( v.begin(), v.end(), begin() );
303 }
304
305 QValueVector( const std::vector&lt;T&gt;&amp; v )
306 {
307 sh = new QValueVectorPrivate&lt;T&gt;( v.size() );
308 qCopy( v.begin(), v.end(), begin() );
309 }
310#endif
311
312 ~QValueVector()
313 {
314 sh-&gt;derefAndDelete();
315 }
316
317 QValueVector&lt;T&gt;&amp; operator= ( const QValueVector&lt;T&gt;&amp; v )
318 {
319 v.sh-&gt;ref();
320 sh-&gt;derefAndDelete();
321 sh = v.sh;
322 return *this;
323 }
324
325#ifndef QT_NO_STL
326 QValueVector&lt;T&gt;&amp; operator= ( const std::vector&lt;T&gt;&amp; v )
327 {
328 clear();
329 resize( v.size() );
330 qCopy( v.begin(), v.end(), begin() );
331 return *this;
332 }
333#endif
334
335 size_type size() const { return sh-&gt;size(); }
336
337 bool empty() const { return sh-&gt;empty(); }
338
339 size_type capacity() const
340 {
341 return size_type( sh-&gt;capacity() );
342 }
343
344 iterator begin()
345 {
346 detach();
347 return sh-&gt;start;
348 }
349
350 const_iterator begin() const
351 {
352 return sh-&gt;start;
353 }
354
355 const_iterator constBegin() const
356 {
357 return sh-&gt;start;
358 }
359
360 iterator end()
361 {
362 detach();
363 return sh-&gt;finish;
364 }
365
366 const_iterator end() const
367 {
368 return sh-&gt;finish;
369 }
370
371 const_iterator constEnd() const
372 {
373 return sh-&gt;finish;
374 }
375
376 reference at( size_type i, bool* ok = 0 )
377 {
378 detach();
379 if ( ok )
380 *ok = ( i &lt; size() );
381 return *( begin() + i );
382 }
383
384 const_reference at( size_type i, bool* ok = 0 ) const
385 {
386 if ( ok )
387 *ok = ( i &lt; size() );
388 return *( begin() + i );
389 }
390
391 reference operator[]( size_type i )
392 {
393 detach();
394 return *( begin() + i );
395 }
396
397 const_reference operator[]( size_type i ) const
398 {
399 return *( begin() + i );
400 }
401
402 reference front()
403 {
404 Q_ASSERT( !empty() );
405 detach();
406 return *begin();
407 }
408
409 const_reference front() const
410 {
411 Q_ASSERT( !empty() );
412 return *begin();
413 }
414
415 reference back()
416 {
417 Q_ASSERT( !empty() );
418 detach();
419 return *( end() - 1 );
420 }
421
422 const_reference back() const
423 {
424 Q_ASSERT( !empty() );
425 return *( end() - 1 );
426 }
427
428 void push_back( const T&amp; x )
429 {
430 detach();
431 if ( sh-&gt;finish == sh-&gt;end ) {
432 sh-&gt;reserve( size()+size()/2+1 );
433 }
434 *sh-&gt;finish = x;
435 ++sh-&gt;finish;
436 }
437
438 void pop_back()
439 {
440 detach();
441 if ( empty() )
442 return;
443 --sh-&gt;finish;
444 }
445
446 iterator insert( iterator pos, const T&amp; x );
447 iterator insert( iterator pos, size_type n, const T&amp; x );
448
449 void reserve( size_type n )
450 {
451 if ( capacity() &lt; n ) {
452 detach();
453 sh-&gt;reserve( n );
454 }
455 }
456
457 void resize( size_type n, const T&amp; val = T() )
458 {
459 if ( n &lt; size() )
460 erase( begin() + n, end() );
461 else
462 insert( end(), n - size(), val );
463 }
464
465 void clear()
466 {
467 detach();
468 sh-&gt;clear();
469 }
470
471 iterator erase( iterator pos )
472 {
473 detach();
474 if ( pos + 1 != end() )
475 qCopy( pos + 1, sh-&gt;finish, pos );
476 --sh-&gt;finish;
477 return pos;
478 }
479
480 iterator erase( iterator first, iterator last )
481 {
482 detach();
483 qCopy( last, sh-&gt;finish, first );
484 sh-&gt;finish = sh-&gt;finish - ( last - first );
485 return first;
486 }
487
488 // ### remove in Qt 4.0
489 bool operator==( const QValueVector&lt;T&gt;&amp; x )
490 {
491 return size()==x.size() ? qEqual( constBegin(), constEnd(), x.begin()) : FALSE;
492 }
493
494 bool operator==( const QValueVector&lt;T&gt;&amp; x ) const
495 {
496 return size()==x.size() ? qEqual( begin(), end(), x.begin() ) : FALSE;
497 }
498
499 typedef T ValueType;
500 typedef ValueType *Iterator;
501 typedef const ValueType *ConstIterator;
502
503 size_type count() const { return size(); }
504 bool isEmpty() const { return empty(); }
505
506 reference first() { return front(); }
507 const_reference first() const { return front(); }
508 reference last() { return back(); }
509 const_reference last() const { return back(); }
510 void append( const T&amp; x ) { push_back( x ); }
511
512protected:
513 void detach()
514 {
515 if ( sh-&gt;count &gt; 1 ) { detachInternal(); }
516 }
517 void detachInternal();
518 QValueVectorPrivate&lt;T&gt;* sh;
519
520private:
521 friend class QDeepCopy&lt; QValueVector&lt;T&gt; &gt;;
522};
523
524template &lt;class T&gt;
525Q_INLINE_TEMPLATES QValueVector&lt;T&gt;::QValueVector( size_type n, const T&amp; val )
526{
527 sh = new QValueVectorPrivate&lt;T&gt;( n );
528 qFill( begin(), end(), val );
529}
530
531template &lt;class T&gt;
532Q_INLINE_TEMPLATES void QValueVector&lt;T&gt;::detachInternal()
533{
534 sh-&gt;deref();
535 sh = new QValueVectorPrivate&lt;T&gt;( *sh );
536}
537
538template &lt;class T&gt;
539Q_INLINE_TEMPLATES Q_TYPENAME QValueVector&lt;T&gt;::iterator QValueVector&lt;T&gt;::insert( iterator pos, const T&amp; x )
540{
541 size_type offset = pos - sh-&gt;start;
542 detach();
543 if ( pos == end() ) {
544 if ( sh-&gt;finish == sh-&gt;end )
545 push_back( x );
546 else {
547 *sh-&gt;finish = x;
548 ++sh-&gt;finish;
549 }
550 } else {
551 if ( sh-&gt;finish == sh-&gt;end ) {
552 sh-&gt;insert( pos, x );
553 } else {
554 *sh-&gt;finish = *(sh-&gt;finish - 1);
555 ++sh-&gt;finish;
556 qCopyBackward( pos, sh-&gt;finish - 2, sh-&gt;finish - 1 );
557 *pos = x;
558 }
559 }
560 return begin() + offset;
561}
562
563template &lt;class T&gt;
564Q_INLINE_TEMPLATES Q_TYPENAME QValueVector&lt;T&gt;::iterator QValueVector&lt;T&gt;::insert( iterator pos, size_type n, const T&amp; x )
565{
566 if ( n != 0 ) {
567 size_type offset = pos - sh-&gt;start;
568 detach();
569 pos = begin() + offset;
570 sh-&gt;insert( pos, n, x );
571 }
572 return pos;
573}
574
575
576#ifndef QT_NO_DATASTREAM
577template&lt;class T&gt;
578Q_INLINE_TEMPLATES QDataStream&amp; operator&gt;&gt;( QDataStream&amp; s, QValueVector&lt;T&gt;&amp; v )
579{
580 v.clear();
581 Q_UINT32 c;
582 s &gt;&gt; c;
583 v.resize( c );
584 for( Q_UINT32 i = 0; i &lt; c; ++i )
585 {
586 T t;
587 s &gt;&gt; t;
588 v[i] = t;
589 }
590 return s;
591}
592
593template&lt;class T&gt;
594Q_INLINE_TEMPLATES QDataStream&amp; operator&lt;&lt;( QDataStream&amp; s, const QValueVector&lt;T&gt;&amp; v )
595{
596 s &lt;&lt; (Q_UINT32)v.size();
597 // ### use typename QValueVector&lt;T&gt;::const_iterator once all supported
598 // ### compilers know about the 'typename' keyword.
599 const T* it = v.begin();
600 for( ; it != v.end(); ++it )
601 s &lt;&lt; *it;
602 return s;
603}
604#endif // QT_NO_DATASTREAM
605
606#define Q_DEFINED_QVALUEVECTOR
607#include "qwinexport.h"
608#endif // QVALUEVECTOR_H
609</pre>
610<!-- eof -->
611<p><address><hr><div align=center>
612<table width=100% cellspacing=0 border=0><tr>
613<td>Copyright &copy; 2007
614<a href="troll.html">Trolltech</a><td align=center><a href="trademarks.html">Trademarks</a>
615<td align=right><div align=right>Qt 3.3.8</div>
616</table></div></address></body>
617</html>
Note: See TracBrowser for help on using the repository browser.