Public Member Functions | Protected Member Functions | Static Private Member Functions | Private Attributes | Friends | List of all members
QGVector Class Reference

The QGVector class is an internal class for implementing Qt collection classes. More...

#include <qgvector.h>

Inheritance diagram for QGVector:
QCollection QVector< type > QVector< QList< IndexWord > > QStrVec QStrIVec

Public Member Functions

QDataStreamread (QDataStream &)
 
QDataStreamwrite (QDataStream &) const
 
virtual int compareItems (Item, Item)
 
- Public Member Functions inherited from QCollection
bool autoDelete () const
 
void setAutoDelete (bool enable)
 

Protected Member Functions

 QGVector ()
 
 QGVector (uint size)
 
 QGVector (const QGVector &v)
 
 ~QGVector ()
 
QGVectoroperator= (const QGVector &v)
 
Itemdata () const
 
uint size () const
 
uint count () const
 
bool insert (uint index, Item)
 
bool remove (uint index)
 
Item take (uint index)
 
void clear ()
 
bool resize (uint newsize)
 
bool fill (Item, int flen)
 
void sort ()
 
int bsearch (Item) const
 
int findRef (Item, uint index) const
 
int find (Item, uint index) const
 
uint containsRef (Item) const
 
uint contains (Item) const
 
Item at (uint index) const
 
bool insertExpand (uint index, Item)
 
void toList (QGList *) const
 
virtual QDataStreamread (QDataStream &, Item &)
 
virtual QDataStreamwrite (QDataStream &, Item) const
 
- Protected Member Functions inherited from QCollection
 QCollection ()
 
 QCollection (const QCollection &)
 
virtual ~QCollection ()
 
virtual Item newItem (Item)
 
virtual void deleteItem (Item)
 

Static Private Member Functions

static void warningIndexRange (uint)
 

Private Attributes

Itemvec
 
uint len
 
uint numItems
 

Friends

class QGList
 

Additional Inherited Members

- Public Types inherited from QCollection
typedef void * Item
 
- Protected Attributes inherited from QCollection
bool del_item
 

Detailed Description

The QGVector class is an internal class for implementing Qt collection classes.

QGVector is a strictly internal class that acts as a base class for the QVector collection class.

QGVector has some virtual functions that may be reimplemented in subclasses to to customize behavior.

Definition at line 46 of file qgvector.h.

Constructor & Destructor Documentation

QGVector::QGVector ( )
protected

Definition at line 154 of file qgvector.cpp.

155 {
156  vec = 0;
157  len = numItems = 0;
158 }
Item * vec
Definition: qgvector.h:103
uint numItems
Definition: qgvector.h:105
uint len
Definition: qgvector.h:104
QGVector::QGVector ( uint  size)
protected

Definition at line 163 of file qgvector.cpp.

164 {
165  len = size;
166  numItems = 0;
167  if ( len == 0 ) { // zero length
168  vec = 0;
169  return;
170  }
171  vec = NEW(Item,len);
172  CHECK_PTR( vec );
173  memset( (void*)vec, 0, len*sizeof(Item) ); // fill with nulls
174 }
#define NEW(type, size)
Definition: qgvector.cpp:51
uint size() const
Definition: qgvector.h:65
Item * vec
Definition: qgvector.h:103
#define CHECK_PTR(p)
Definition: qglobal.h:601
uint numItems
Definition: qgvector.h:105
uint len
Definition: qgvector.h:104
void * Item
Definition: qcollection.h:60
QGVector::QGVector ( const QGVector v)
protected

Definition at line 180 of file qgvector.cpp.

181  : QCollection( a )
182 {
183  len = a.len;
184  numItems = a.numItems;
185  vec = NEW(Item,len);
186  CHECK_PTR( vec );
187  for ( uint i=0; i<len; i++ ) {
188  vec[i] = a.vec[i] ? newItem( a.vec[i] ) : 0;
189  CHECK_PTR( vec[i] );
190  }
191 }
virtual Item newItem(Item)
#define NEW(type, size)
Definition: qgvector.cpp:51
const double a
Item * vec
Definition: qgvector.h:103
#define CHECK_PTR(p)
Definition: qglobal.h:601
uint numItems
Definition: qgvector.h:105
uint len
Definition: qgvector.h:104
unsigned uint
Definition: qglobal.h:351
void * Item
Definition: qcollection.h:60
QGVector::~QGVector ( )
protected

Definition at line 197 of file qgvector.cpp.

198 {
199  clear();
200 }
void clear()
Definition: qgvector.cpp:313

Member Function Documentation

Item QGVector::at ( uint  index) const
inlineprotected

Definition at line 85 of file qgvector.h.

86  {
87 #if defined(CHECK_RANGE)
88  if ( index >= len )
90 #endif
91  return vec[index];
92  }
Item * vec
Definition: qgvector.h:103
uint len
Definition: qgvector.h:104
static void warningIndexRange(uint)
Definition: qgvector.cpp:578
int QGVector::bsearch ( Item  d) const
protected

Definition at line 439 of file qgvector.cpp.

440 {
441  if ( !len )
442  return -1;
443  if ( !d ) {
444 #if defined(CHECK_NULL)
445  qWarning( "QGVector::bsearch: Cannot search for null object" );
446 #endif
447  return -1;
448  }
449  int n1 = 0;
450  int n2 = len - 1;
451  int mid = 0;
452  bool found = FALSE;
453  while ( n1 <= n2 ) {
454  int res;
455  mid = (n1 + n2)/2;
456  if ( vec[mid] == 0 ) // null item greater
457  res = -1;
458  else
459  res = ((QGVector*)this)->compareItems( d, vec[mid] );
460  if ( res < 0 )
461  n2 = mid - 1;
462  else if ( res > 0 )
463  n1 = mid + 1;
464  else { // found it
465  found = TRUE;
466  break;
467  }
468  }
469  if ( !found )
470  return -1;
471  // search to first of equal items
472  while ( (mid - 1 >= 0) && !((QGVector*)this)->compareItems(d, vec[mid-1]) )
473  mid--;
474  return mid;
475 }
const bool FALSE
Definition: qglobal.h:370
void qWarning(const char *msg,...)
Definition: qglobal.cpp:409
The QGVector class is an internal class for implementing Qt collection classes.
Definition: qgvector.h:46
Item * vec
Definition: qgvector.h:103
virtual int compareItems(Item, Item)
Definition: qgvector.cpp:110
uint len
Definition: qgvector.h:104
const bool TRUE
Definition: qglobal.h:371
void QGVector::clear ( )
protectedvirtual

Removes all objects from the collection. The objects will be deleted if auto-delete has been enabled.

See also
setAutoDelete()

Implements QCollection.

Reimplemented in QVector< type >, and QVector< QList< IndexWord > >.

Definition at line 313 of file qgvector.cpp.

314 {
315  if ( vec ) {
316  for ( uint i=0; i<len; i++ ) { // delete each item
317  if ( vec[i] )
318  deleteItem( vec[i] );
319  }
320  DELETE(vec);
321  vec = 0;
322  len = numItems = 0;
323  }
324 }
#define DELETE(array)
Definition: qgvector.cpp:52
Item * vec
Definition: qgvector.h:103
uint numItems
Definition: qgvector.h:105
virtual void deleteItem(Item)
uint len
Definition: qgvector.h:104
unsigned uint
Definition: qglobal.h:351
int QGVector::compareItems ( Item  d1,
Item  d2 
)
virtual

This virtual function compares two list items.

Returns:

  • 0 if item1 == item2
  • non-zero if item1 != item2

This function returns int rather than bool so that reimplementations can return one of three values and use it to sort by:

  • 0 if item1 == item2
  • > 0 (positive integer) if item1 > item2
  • < 0 (negative integer) if item1 < item2

The QVector::sort() and QVector::bsearch() functions require that compareItems() is implemented as described here.

This function should not modify the vector because some const functions call compareItems().

Reimplemented in QStrIVec, and QStrVec.

Definition at line 110 of file qgvector.cpp.

111 {
112  return d1 != d2; // compare pointers
113 }
uint QGVector::contains ( Item  d) const
protected

Definition at line 536 of file qgvector.cpp.

537 {
538  uint count = 0;
539  for ( uint i=0; i<len; i++ ) {
540  if ( vec[i] == 0 && d == 0 ) // count null items
541  count++;
542  if ( vec[i] && ((QGVector*)this)->compareItems( vec[i], d ) == 0 )
543  count++;
544  }
545  return count;
546 }
The QGVector class is an internal class for implementing Qt collection classes.
Definition: qgvector.h:46
uint count() const
Definition: qgvector.h:66
Item * vec
Definition: qgvector.h:103
virtual int compareItems(Item, Item)
Definition: qgvector.cpp:110
uint len
Definition: qgvector.h:104
unsigned uint
Definition: qglobal.h:351
uint QGVector::containsRef ( Item  d) const
protected

Definition at line 522 of file qgvector.cpp.

523 {
524  uint count = 0;
525  for ( uint i=0; i<len; i++ ) {
526  if ( vec[i] == d )
527  count++;
528  }
529  return count;
530 }
uint count() const
Definition: qgvector.h:66
Item * vec
Definition: qgvector.h:103
uint len
Definition: qgvector.h:104
unsigned uint
Definition: qglobal.h:351
uint QGVector::count ( ) const
inlineprotectedvirtual

Returns the number of objects in the collection.

Implements QCollection.

Reimplemented in QVector< type >, and QVector< QList< IndexWord > >.

Definition at line 66 of file qgvector.h.

66 { return numItems; }
uint numItems
Definition: qgvector.h:105
Item * QGVector::data ( ) const
inlineprotected

Definition at line 64 of file qgvector.h.

64 { return vec; }
Item * vec
Definition: qgvector.h:103
bool QGVector::fill ( Item  d,
int  flen 
)
protected

Definition at line 377 of file qgvector.cpp.

378 {
379  if ( flen < 0 )
380  flen = len; // default: use vector length
381  else if ( !resize( flen ) )
382  return FALSE;
383  for ( uint i=0; i<(uint)flen; i++ ) // insert d at every index
384  insert( i, d );
385  return TRUE;
386 }
const bool FALSE
Definition: qglobal.h:370
bool insert(uint index, Item)
Definition: qgvector.cpp:246
bool resize(uint newsize)
Definition: qgvector.cpp:330
uint len
Definition: qgvector.h:104
unsigned uint
Definition: qglobal.h:351
const bool TRUE
Definition: qglobal.h:371
int QGVector::find ( Item  d,
uint  index 
) const
protected

Definition at line 501 of file qgvector.cpp.

502 {
503 #if defined(CHECK_RANGE)
504  if ( index >= len ) { // range error
505  qWarning( "QGVector::find: Index %d out of range", index );
506  return -1;
507  }
508 #endif
509  for ( uint i=index; i<len; i++ ) {
510  if ( vec[i] == 0 && d == 0 ) // found null item
511  return i;
512  if ( vec[i] && ((QGVector*)this)->compareItems( vec[i], d ) == 0 )
513  return i;
514  }
515  return -1;
516 }
void qWarning(const char *msg,...)
Definition: qglobal.cpp:409
The QGVector class is an internal class for implementing Qt collection classes.
Definition: qgvector.h:46
Item * vec
Definition: qgvector.h:103
virtual int compareItems(Item, Item)
Definition: qgvector.cpp:110
uint len
Definition: qgvector.h:104
unsigned uint
Definition: qglobal.h:351
int QGVector::findRef ( Item  d,
uint  index 
) const
protected

Definition at line 482 of file qgvector.cpp.

483 {
484 #if defined(CHECK_RANGE)
485  if ( index >= len ) { // range error
486  qWarning( "QGVector::findRef: Index %d out of range", index );
487  return -1;
488  }
489 #endif
490  for ( uint i=index; i<len; i++ ) {
491  if ( vec[i] == d )
492  return i;
493  }
494  return -1;
495 }
void qWarning(const char *msg,...)
Definition: qglobal.cpp:409
Item * vec
Definition: qgvector.h:103
uint len
Definition: qgvector.h:104
unsigned uint
Definition: qglobal.h:351
bool QGVector::insert ( uint  index,
Item  d 
)
protected

Definition at line 246 of file qgvector.cpp.

247 {
248 #if defined(CHECK_RANGE)
249  if ( index >= len ) { // range error
250  qWarning( "QGVector::insert: Index %d out of range", index );
251  return FALSE;
252  }
253 #endif
254  if ( vec[index] ) { // remove old item
255  deleteItem( vec[index] );
256  numItems--;
257  }
258  if ( d ) {
259  vec[index] = newItem( d );
260  CHECK_PTR( vec[index] );
261  numItems++;
262  return vec[index] != 0;
263  } else {
264  vec[index] = 0; // reset item
265  }
266  return TRUE;
267 }
virtual Item newItem(Item)
const bool FALSE
Definition: qglobal.h:370
void qWarning(const char *msg,...)
Definition: qglobal.cpp:409
Item * vec
Definition: qgvector.h:103
#define CHECK_PTR(p)
Definition: qglobal.h:601
uint numItems
Definition: qgvector.h:105
virtual void deleteItem(Item)
uint len
Definition: qgvector.h:104
const bool TRUE
Definition: qglobal.h:371
bool QGVector::insertExpand ( uint  index,
Item  d 
)
protected

Definition at line 553 of file qgvector.cpp.

554 {
555  if ( index >= len ) {
556  if ( !resize( index+1 ) ) // no memory
557  return FALSE;
558  }
559  insert( index, d );
560  return TRUE;
561 }
const bool FALSE
Definition: qglobal.h:370
bool insert(uint index, Item)
Definition: qgvector.cpp:246
bool resize(uint newsize)
Definition: qgvector.cpp:330
uint len
Definition: qgvector.h:104
const bool TRUE
Definition: qglobal.h:371
QGVector & QGVector::operator= ( const QGVector v)
protected

Definition at line 207 of file qgvector.cpp.

208 { // assign from other vector
209  clear(); // first delete old vector
210  len = v.len;
211  numItems = v.numItems;
212  vec = NEW(Item,len); // create new vector
213  CHECK_PTR( vec );
214  for ( uint i=0; i<len; i++ ) { // copy elements
215  vec[i] = v.vec[i] ? newItem( v.vec[i] ) : 0;
216  CHECK_PTR( vec[i] );
217  }
218  return *this;
219 }
virtual Item newItem(Item)
#define NEW(type, size)
Definition: qgvector.cpp:51
Item * vec
Definition: qgvector.h:103
#define CHECK_PTR(p)
Definition: qglobal.h:601
void clear()
Definition: qgvector.cpp:313
uint numItems
Definition: qgvector.h:105
uint len
Definition: qgvector.h:104
unsigned uint
Definition: qglobal.h:351
void * Item
Definition: qcollection.h:60
QDataStream & QGVector::read ( QDataStream s)

Definition at line 606 of file qgvector.cpp.

607 {
608  uint num;
609  s >> num; // read number of items
610  clear(); // clear vector
611  resize( num );
612  for (uint i=0; i<num; i++) { // read all items
613  Item d;
614  read( s, d );
615  CHECK_PTR( d );
616  if ( !d ) // no memory
617  break;
618  vec[i] = d;
619  }
620  return s;
621 }
bool resize(uint newsize)
Definition: qgvector.cpp:330
QDataStream & read(QDataStream &)
Definition: qgvector.cpp:606
Item * vec
Definition: qgvector.h:103
#define CHECK_PTR(p)
Definition: qglobal.h:601
void clear()
Definition: qgvector.cpp:313
unsigned uint
Definition: qglobal.h:351
static QCString * s
Definition: config.cpp:1042
void * Item
Definition: qcollection.h:60
QDataStream & QGVector::read ( QDataStream s,
Item d 
)
protectedvirtual

Reads a collection/vector item from the stream s and returns a reference to the stream.

The default implementation sets item to 0.

See also
write()

Reimplemented in QStrVec.

Definition at line 125 of file qgvector.cpp.

126 { // read item from stream
127  d = 0;
128  return s;
129 }
static QCString * s
Definition: config.cpp:1042
bool QGVector::remove ( uint  index)
protected

Definition at line 273 of file qgvector.cpp.

274 {
275 #if defined(CHECK_RANGE)
276  if ( index >= len ) { // range error
277  qWarning( "QGVector::remove: Index %d out of range", index );
278  return FALSE;
279  }
280 #endif
281  if ( vec[index] ) { // valid item
282  deleteItem( vec[index] ); // delete it
283  vec[index] = 0; // reset pointer
284  numItems--;
285  }
286  return TRUE;
287 }
const bool FALSE
Definition: qglobal.h:370
void qWarning(const char *msg,...)
Definition: qglobal.cpp:409
Item * vec
Definition: qgvector.h:103
uint numItems
Definition: qgvector.h:105
virtual void deleteItem(Item)
uint len
Definition: qgvector.h:104
const bool TRUE
Definition: qglobal.h:371
bool QGVector::resize ( uint  newsize)
protected

Definition at line 330 of file qgvector.cpp.

331 {
332  if ( newsize == len ) // nothing to do
333  return TRUE;
334  if ( vec ) { // existing data
335  if ( newsize < len ) { // shrink vector
336  uint i = newsize;
337  while ( i < len ) { // delete lost items
338  if ( vec[i] ) {
339  deleteItem( vec[i] );
340  numItems--;
341  }
342  i++;
343  }
344  }
345  if ( newsize == 0 ) { // vector becomes empty
346  DELETE(vec);
347  vec = 0;
348  len = numItems = 0;
349  return TRUE;
350  }
351 #if defined(DONT_USE_REALLOC)
352  Item *newvec = NEW(Item,newsize); // manual realloc
353  memcpy( newvec, vec, (len < newsize ? len : newsize)*sizeof(Item) );
354  DELETE(vec);
355  vec = newvec;
356 #else
357  vec = (Item*)realloc( (char *)vec, newsize*sizeof(Item) );
358 #endif
359  } else { // create new vector
360  vec = NEW(Item,newsize);
361  len = numItems = 0;
362  }
363  CHECK_PTR( vec );
364  if ( !vec ) // no memory
365  return FALSE;
366  if ( newsize > len ) // init extra space added
367  memset( (void*)&vec[len], 0, (newsize-len)*sizeof(Item) );
368  len = newsize;
369  return TRUE;
370 }
#define DELETE(array)
Definition: qgvector.cpp:52
const bool FALSE
Definition: qglobal.h:370
#define NEW(type, size)
Definition: qgvector.cpp:51
Item * vec
Definition: qgvector.h:103
#define CHECK_PTR(p)
Definition: qglobal.h:601
uint numItems
Definition: qgvector.h:105
virtual void deleteItem(Item)
uint len
Definition: qgvector.h:104
unsigned uint
Definition: qglobal.h:351
const bool TRUE
Definition: qglobal.h:371
void * Item
Definition: qcollection.h:60
uint QGVector::size ( ) const
inlineprotected

Definition at line 65 of file qgvector.h.

65 { return len; }
uint len
Definition: qgvector.h:104
void QGVector::sort ( )
protected

Definition at line 410 of file qgvector.cpp.

411 {
412  if ( count() == 0 ) // no elements
413  return;
414  register Item *start = &vec[0];
415  register Item *end = &vec[len-1];
416  Item tmp;
417  while ( TRUE ) { // put all zero elements behind
418  while ( start < end && *start != 0 )
419  start++;
420  while ( end > start && *end == 0 )
421  end--;
422  if ( start < end ) {
423  tmp = *start;
424  *start = *end;
425  *end = tmp;
426  } else {
427  break;
428  }
429  }
430  sort_vec = (QGVector*)this;
431  qsort( vec, count(), sizeof(Item), cmp_vec );
432  sort_vec = 0;
433 }
end
while True: pbar.update(maxval-len(onlies[E][S])) #print iS, "/", len(onlies[E][S]) found = False for...
static QGVector * sort_vec
Definition: qgvector.cpp:389
static int cmp_vec(const void *n1, const void *n2)
Definition: qgvector.cpp:396
The QGVector class is an internal class for implementing Qt collection classes.
Definition: qgvector.h:46
string tmp
Definition: languages.py:63
uint count() const
Definition: qgvector.h:66
Item * vec
Definition: qgvector.h:103
uint len
Definition: qgvector.h:104
const bool TRUE
Definition: qglobal.h:371
void * Item
Definition: qcollection.h:60
QCollection::Item QGVector::take ( uint  index)
protected

Definition at line 293 of file qgvector.cpp.

294 {
295 #if defined(CHECK_RANGE)
296  if ( index >= len ) { // range error
297  qWarning( "QGVector::take: Index %d out of range", index );
298  return 0;
299  }
300 #endif
301  Item d = vec[index]; // don't delete item
302  if ( d )
303  numItems--;
304  vec[index] = 0;
305  return d;
306 }
void qWarning(const char *msg,...)
Definition: qglobal.cpp:409
Item * vec
Definition: qgvector.h:103
uint numItems
Definition: qgvector.h:105
uint len
Definition: qgvector.h:104
void * Item
Definition: qcollection.h:60
void QGVector::toList ( QGList list) const
protected

Definition at line 568 of file qgvector.cpp.

569 {
570  list->clear();
571  for ( uint i=0; i<len; i++ ) {
572  if ( vec[i] )
573  list->append( vec[i] );
574  }
575 }
void append(QCollection::Item)
Definition: qglist.cpp:364
Item * vec
Definition: qgvector.h:103
uint len
Definition: qgvector.h:104
void clear()
Definition: qglist.cpp:652
unsigned uint
Definition: qglobal.h:351
void QGVector::warningIndexRange ( uint  i)
staticprivate

Definition at line 578 of file qgvector.cpp.

579 {
580 #if defined(CHECK_RANGE)
581  qWarning( "QGVector::operator[]: Index %d out of range", i );
582 #else
583  Q_UNUSED( i )
584 #endif
585 }
void qWarning(const char *msg,...)
Definition: qglobal.cpp:409
#define Q_UNUSED(x)
Definition: qglobal.h:536
QDataStream & QGVector::write ( QDataStream s) const

Definition at line 627 of file qgvector.cpp.

628 { // write vector to stream
629  uint num = count();
630  s << num; // number of items to write
631  num = size();
632  for (uint i=0; i<num; i++) { // write non-null items
633  if ( vec[i] )
634  write( s, vec[i] );
635  }
636  return s;
637 }
uint size() const
Definition: qgvector.h:65
uint count() const
Definition: qgvector.h:66
Item * vec
Definition: qgvector.h:103
QDataStream & write(QDataStream &) const
Definition: qgvector.cpp:627
unsigned uint
Definition: qglobal.h:351
static QCString * s
Definition: config.cpp:1042
QDataStream & QGVector::write ( QDataStream s,
Item   
) const
protectedvirtual

Writes a collection/vector item to the stream s and returns a reference to the stream.

The default implementation does nothing.

See also
read()

Reimplemented in QStrVec.

Definition at line 140 of file qgvector.cpp.

141 { // write item to stream
142  return s;
143 }
static QCString * s
Definition: config.cpp:1042

Friends And Related Function Documentation

friend class QGList
friend

Definition at line 48 of file qgvector.h.

Member Data Documentation

uint QGVector::len
private

Definition at line 104 of file qgvector.h.

uint QGVector::numItems
private

Definition at line 105 of file qgvector.h.

Item* QGVector::vec
private

Definition at line 103 of file qgvector.h.


The documentation for this class was generated from the following files: