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

The QGArray class is an internal class for implementing the QArray class. More...

#include <qgarray.h>

Inheritance diagram for QGArray:
QArray< type > QArray< bool > QArray< char >

Classes

struct  array_data
 

Public Member Functions

 QGArray ()
 

Protected Member Functions

 QGArray (int, int)
 
 QGArray (int size)
 
 QGArray (const QGArray &a)
 
virtual ~QGArray ()
 
QGArrayoperator= (const QGArray &a)
 
virtual void detach ()
 
char * data () const
 
uint nrefs () const
 
uint size () const
 
bool isEqual (const QGArray &a) const
 
bool resize (uint newsize)
 
bool fill (const char *d, int len, uint sz)
 
QGArrayassign (const QGArray &a)
 
QGArrayassign (const char *d, uint len)
 
QGArrayduplicate (const QGArray &a)
 
QGArrayduplicate (const char *d, uint len)
 
void store (const char *d, uint len)
 
array_datasharedBlock () const
 
void setSharedBlock (array_data *p)
 
QGArraysetRawData (const char *d, uint len)
 
void resetRawData (const char *d, uint len)
 
int find (const char *d, uint index, uint sz) const
 
int contains (const char *d, uint sz) const
 
void sort (uint sz)
 
int bsearch (const char *d, uint sz) const
 
char * at (uint index) const
 
bool setExpand (uint index, const char *d, uint sz)
 
virtual array_datanewData ()
 
virtual void deleteData (array_data *p)
 

Static Private Member Functions

static void msg_index (uint)
 

Private Attributes

array_datashd
 

Friends

class QBuffer
 

Detailed Description

The QGArray class is an internal class for implementing the QArray class.

QGArray is a strictly internal class that acts as base class for the QArray template array.

It contains an array of bytes and has no notion of an array element.

Definition at line 46 of file qgarray.h.

Constructor & Destructor Documentation

QGArray::QGArray ( )

Definition at line 89 of file qgarray.cpp.

90 {
91  shd = newData();
92  CHECK_PTR( shd );
93 }
array_data * shd
Definition: qgarray.h:104
virtual array_data * newData()
Definition: qgarray.cpp:732
#define CHECK_PTR(p)
Definition: qglobal.h:601
QGArray::QGArray ( int  ,
int   
)
protected

Definition at line 103 of file qgarray.cpp.

104 {
105 }
QGArray::QGArray ( int  size)
protected

Definition at line 112 of file qgarray.cpp.

113 {
114  if ( size < 0 ) {
115 #if defined(CHECK_RANGE)
116  qWarning( "QGArray: Cannot allocate array with negative length" );
117 #endif
118  size = 0;
119  }
120  shd = newData();
121  CHECK_PTR( shd );
122  if ( size == 0 ) // zero length
123  return;
124  shd->data = NEW(char,size);
125  CHECK_PTR( shd->data );
126  shd->len = size;
127 }
array_data * shd
Definition: qgarray.h:104
void qWarning(const char *msg,...)
Definition: qglobal.cpp:409
virtual array_data * newData()
Definition: qgarray.cpp:732
uint size() const
Definition: qgarray.h:69
#define NEW(type, size)
Definition: qgarray.cpp:49
#define CHECK_PTR(p)
Definition: qglobal.h:601
QGArray::QGArray ( const QGArray a)
protected

Definition at line 134 of file qgarray.cpp.

135 {
136  shd = a.shd;
137  shd->ref();
138 }
array_data * shd
Definition: qgarray.h:104
void ref()
Definition: qshared.h:49
QGArray::~QGArray ( )
protectedvirtual

Definition at line 146 of file qgarray.cpp.

147 {
148  if ( shd && shd->deref() ) { // delete when last reference
149  if ( shd->data ) // is lost
150  DELETE(shd->data);
151  deleteData( shd );
152  }
153 }
bool deref()
Definition: qshared.h:50
#define DELETE(array)
Definition: qgarray.cpp:50
array_data * shd
Definition: qgarray.h:104
virtual void deleteData(array_data *p)
Definition: qgarray.cpp:743

Member Function Documentation

QGArray & QGArray::assign ( const QGArray a)
protected

Definition at line 284 of file qgarray.cpp.

285 {
286  a.shd->ref(); // avoid 'a = a'
287  if ( shd->deref() ) { // delete when last reference
288  if ( shd->data ) // is lost
289  DELETE(shd->data);
290  deleteData( shd );
291  }
292  shd = a.shd;
293  return *this;
294 }
bool deref()
Definition: qshared.h:50
#define DELETE(array)
Definition: qgarray.cpp:50
array_data * shd
Definition: qgarray.h:104
virtual void deleteData(array_data *p)
Definition: qgarray.cpp:743
void ref()
Definition: qshared.h:49
QGArray & QGArray::assign ( const char *  d,
uint  len 
)
protected

Definition at line 305 of file qgarray.cpp.

306 {
307  if ( shd->count > 1 ) { // disconnect this
308  shd->count--;
309  shd = newData();
310  CHECK_PTR( shd );
311  } else {
312  if ( shd->data )
313  DELETE(shd->data);
314  }
315  shd->data = (char *)d;
316  shd->len = len;
317  return *this;
318 }
#define DELETE(array)
Definition: qgarray.cpp:50
array_data * shd
Definition: qgarray.h:104
uint count
Definition: qshared.h:51
virtual array_data * newData()
Definition: qgarray.cpp:732
#define CHECK_PTR(p)
Definition: qglobal.h:601
char * QGArray::at ( uint  index) const
inlineprotected

Definition at line 108 of file qgarray.h.

109 {
110 #if defined(CHECK_RANGE)
111  if ( index >= size() ) {
112  msg_index( index );
113  index = 0;
114  }
115 #endif
116  return &shd->data[index];
117 }
static void msg_index(uint)
Definition: qgarray.cpp:717
array_data * shd
Definition: qgarray.h:104
uint size() const
Definition: qgarray.h:69
int QGArray::bsearch ( const char *  d,
uint  sz 
) const
protected

Definition at line 667 of file qgarray.cpp.

668 {
669  int numItems = size() / sz;
670  if ( !numItems )
671  return -1;
672  cmp_item_size = sz;
673  char* r = (char*)::bsearch( d, shd->data, numItems, sz, cmp_arr );
674  if ( !r )
675  return -1;
676  while( (r >= shd->data + sz) && (cmp_arr( r - sz, d ) == 0) )
677  r -= sz; // search to first of equal elements; bsearch is undef
678  return (int)(( r - shd->data ) / sz);
679 }
static int cmp_item_size
Definition: qgarray.cpp:629
array_data * shd
Definition: qgarray.h:104
uint size() const
Definition: qgarray.h:69
static int cmp_arr(const void *n1, const void *n2)
Definition: qgarray.cpp:635
int bsearch(const char *d, uint sz) const
Definition: qgarray.cpp:667
int QGArray::contains ( const char *  d,
uint  sz 
) const
protected

Definition at line 584 of file qgarray.cpp.

585 {
586  register uint i = shd->len;
587  int count = 0;
588  switch ( sz ) {
589  case 1: { // 8 bit elements
590  register char *x = data();
591  char v = *d;
592  while ( i-- ) {
593  if ( *x++ == v )
594  count++;
595  }
596  }
597  break;
598  case 2: { // 16 bit elements
599  register Q_INT16 *x = (Q_INT16*)data();
600  Q_INT16 v = *((Q_INT16*)d);
601  i /= 2;
602  while ( i-- ) {
603  if ( *x++ == v )
604  count++;
605  }
606  }
607  break;
608  case 4: { // 32 bit elements
609  register Q_INT32 *x = (Q_INT32*)data();
610  Q_INT32 v = *((Q_INT32*)d);
611  i /= 4;
612  while ( i-- ) {
613  if ( *x++ == v )
614  count++;
615  }
616  }
617  break;
618  default: { // any size elements
619  for ( i=0; i<shd->len; i+=sz ) {
620  if ( memcmp(d, &shd->data[i], sz) == 0 )
621  count++;
622  }
623  }
624  break;
625  }
626  return count;
627 }
char * data() const
Definition: qgarray.h:67
array_data * shd
Definition: qgarray.h:104
int Q_INT32
Definition: qglobal.h:419
short Q_INT16
Definition: qglobal.h:417
list x
Definition: train.py:276
unsigned uint
Definition: qglobal.h:351
char * QGArray::data ( ) const
inlineprotected

Definition at line 67 of file qgarray.h.

67 { return shd->data; }
array_data * shd
Definition: qgarray.h:104
void QGArray::deleteData ( array_data p)
protectedvirtual

Definition at line 743 of file qgarray.cpp.

744 {
745  delete p;
746  p = 0;
747 }
p
Definition: test.py:223
void QGArray::detach ( )
inlineprotectedvirtual

Reimplemented in QArray< type >, QArray< char >, and QArray< bool >.

Definition at line 65 of file qgarray.h.

65 { duplicate(*this); }
QGArray & duplicate(const QGArray &a)
Definition: qgarray.cpp:327
QGArray & QGArray::duplicate ( const QGArray a)
protected

Definition at line 327 of file qgarray.cpp.

328 {
329  if ( a.shd == shd ) { // a.duplicate(a) !
330  if ( shd->count > 1 ) {
331  shd->count--;
332  register array_data *n = newData();
333  CHECK_PTR( n );
334  if ( (n->len=shd->len) ) {
335  n->data = NEW(char,n->len);
336  CHECK_PTR( n->data );
337  if ( n->data )
338  memcpy( n->data, shd->data, n->len );
339  } else {
340  n->data = 0;
341  }
342  shd = n;
343  }
344  return *this;
345  }
346  char *oldptr = 0;
347  if ( shd->count > 1 ) { // disconnect this
348  shd->count--;
349  shd = newData();
350  CHECK_PTR( shd );
351  } else { // delete after copy was made
352  oldptr = shd->data;
353  }
354  if ( a.shd->len ) { // duplicate data
355  shd->data = NEW(char,a.shd->len);
356  CHECK_PTR( shd->data );
357  if ( shd->data )
358  memcpy( shd->data, a.shd->data, a.shd->len );
359  } else {
360  shd->data = 0;
361  }
362  shd->len = a.shd->len;
363  if ( oldptr )
364  DELETE(oldptr);
365  return *this;
366 }
#define DELETE(array)
Definition: qgarray.cpp:50
array_data * shd
Definition: qgarray.h:104
uint count
Definition: qshared.h:51
virtual array_data * newData()
Definition: qgarray.cpp:732
std::void_t< T > n
#define NEW(type, size)
Definition: qgarray.cpp:49
#define CHECK_PTR(p)
Definition: qglobal.h:601
QGArray & QGArray::duplicate ( const char *  d,
uint  len 
)
protected

Definition at line 375 of file qgarray.cpp.

376 {
377  char *data;
378  if ( d == 0 || len == 0 ) {
379  data = 0;
380  len = 0;
381  } else {
382  if ( shd->count == 1 && shd->len == len ) {
383  memcpy( shd->data, d, len ); // use same buffer
384  return *this;
385  }
386  data = NEW(char,len);
387  CHECK_PTR( data );
388  memcpy( data, d, len );
389  }
390  if ( shd->count > 1 ) { // detach
391  shd->count--;
392  shd = newData();
393  CHECK_PTR( shd );
394  } else { // just a single reference
395  if ( shd->data )
396  DELETE(shd->data);
397  }
398  shd->data = data;
399  shd->len = len;
400  return *this;
401 }
char * data() const
Definition: qgarray.h:67
#define DELETE(array)
Definition: qgarray.cpp:50
array_data * shd
Definition: qgarray.h:104
uint count
Definition: qshared.h:51
virtual array_data * newData()
Definition: qgarray.cpp:732
#define NEW(type, size)
Definition: qgarray.cpp:49
#define CHECK_PTR(p)
Definition: qglobal.h:601
bool QGArray::fill ( const char *  d,
int  len,
uint  sz 
)
protected

Definition at line 249 of file qgarray.cpp.

250 {
251  if ( len < 0 )
252  len = shd->len/sz; // default: use array length
253  else if ( !resize( len*sz ) )
254  return FALSE;
255  if ( sz == 1 ) // 8 bit elements
256  memset( data(), *d, len );
257  else if ( sz == 4 ) { // 32 bit elements
258  register Q_INT32 *x = (Q_INT32*)data();
259  Q_INT32 v = *((Q_INT32*)d);
260  while ( len-- )
261  *x++ = v;
262  } else if ( sz == 2 ) { // 16 bit elements
263  register Q_INT16 *x = (Q_INT16*)data();
264  Q_INT16 v = *((Q_INT16*)d);
265  while ( len-- )
266  *x++ = v;
267  } else { // any other size elements
268  register char *x = data();
269  while ( len-- ) { // more complicated
270  memcpy( x, d, sz );
271  x += sz;
272  }
273  }
274  return TRUE;
275 }
char * data() const
Definition: qgarray.h:67
const bool FALSE
Definition: qglobal.h:370
array_data * shd
Definition: qgarray.h:104
int Q_INT32
Definition: qglobal.h:419
bool resize(uint newsize)
Definition: qgarray.cpp:209
short Q_INT16
Definition: qglobal.h:417
list x
Definition: train.py:276
const bool TRUE
Definition: qglobal.h:371
int QGArray::find ( const char *  d,
uint  index,
uint  sz 
) const
protected

Definition at line 522 of file qgarray.cpp.

523 {
524  index *= sz;
525  if ( index >= shd->len ) {
526 #if defined(CHECK_RANGE)
527  qWarning( "QGArray::find: Index %d out of range", index/sz );
528 #endif
529  return -1;
530  }
531  register uint i;
532  uint ii;
533  switch ( sz ) {
534  case 1: { // 8 bit elements
535  register char *x = data() + index;
536  char v = *d;
537  for ( i=index; i<shd->len; i++ ) {
538  if ( *x++ == v )
539  break;
540  }
541  ii = i;
542  }
543  break;
544  case 2: { // 16 bit elements
545  register Q_INT16 *x = (Q_INT16*)(data() + index);
546  Q_INT16 v = *((Q_INT16*)d);
547  for ( i=index; i<shd->len; i+=2 ) {
548  if ( *x++ == v )
549  break;
550  }
551  ii = i/2;
552  }
553  break;
554  case 4: { // 32 bit elements
555  register Q_INT32 *x = (Q_INT32*)(data() + index);
556  Q_INT32 v = *((Q_INT32*)d);
557  for ( i=index; i<shd->len; i+=4 ) {
558  if ( *x++ == v )
559  break;
560  }
561  ii = i/4;
562  }
563  break;
564  default: { // any size elements
565  for ( i=index; i<shd->len; i+=sz ) {
566  if ( memcmp( d, &shd->data[i], sz ) == 0 )
567  break;
568  }
569  ii = i/sz;
570  }
571  break;
572  }
573  return i<shd->len ? (int)ii : -1;
574 }
char * data() const
Definition: qgarray.h:67
array_data * shd
Definition: qgarray.h:104
void qWarning(const char *msg,...)
Definition: qglobal.cpp:409
int Q_INT32
Definition: qglobal.h:419
short Q_INT16
Definition: qglobal.h:417
list x
Definition: train.py:276
unsigned uint
Definition: qglobal.h:351
bool QGArray::isEqual ( const QGArray a) const
protected

Definition at line 194 of file qgarray.cpp.

195 {
196  if ( size() != a.size() ) // different size
197  return FALSE;
198  if ( data() == a.data() ) // has same data
199  return TRUE;
200  return (size() ? memcmp( data(), a.data(), size() ) : 0) == 0;
201 }
char * data() const
Definition: qgarray.h:67
const bool FALSE
Definition: qglobal.h:370
uint size() const
Definition: qgarray.h:69
const bool TRUE
Definition: qglobal.h:371
void QGArray::msg_index ( uint  index)
staticprivate

Definition at line 717 of file qgarray.cpp.

718 {
719 #if defined(CHECK_RANGE)
720  qWarning( "QGArray::at: Absolute index %d out of range", index );
721 #else
722  Q_UNUSED( index )
723 #endif
724 }
void qWarning(const char *msg,...)
Definition: qglobal.cpp:409
#define Q_UNUSED(x)
Definition: qglobal.h:536
QGArray::array_data * QGArray::newData ( )
protectedvirtual

Definition at line 732 of file qgarray.cpp.

733 {
734  return new array_data;
735 }
uint QGArray::nrefs ( ) const
inlineprotected

Definition at line 68 of file qgarray.h.

68 { return shd->count; }
array_data * shd
Definition: qgarray.h:104
uint count
Definition: qshared.h:51
QGArray & QGArray::operator= ( const QGArray a)
inlineprotected

Definition at line 63 of file qgarray.h.

63 { return assign( a ); }
QGArray & assign(const QGArray &a)
Definition: qgarray.cpp:284
void QGArray::resetRawData ( const char *  d,
uint  len 
)
protected

Definition at line 499 of file qgarray.cpp.

500 {
501  if ( d != shd->data || len != shd->len ) {
502 #if defined(CHECK_STATE)
503  qWarning( "QGArray::resetRawData: Inconsistent arguments" );
504 #endif
505  return;
506  }
507  shd->data = 0;
508  shd->len = 0;
509 }
array_data * shd
Definition: qgarray.h:104
void qWarning(const char *msg,...)
Definition: qglobal.cpp:409
bool QGArray::resize ( uint  newsize)
protected

Definition at line 209 of file qgarray.cpp.

210 {
211  if ( newsize == shd->len ) // nothing to do
212  return TRUE;
213  if ( newsize == 0 ) { // remove array
214  duplicate( 0, 0 );
215  return TRUE;
216  }
217  if ( shd->data ) { // existing data
218 #if defined(DONT_USE_REALLOC)
219  char *newdata = NEW(char,newsize); // manual realloc
220  memcpy( newdata, shd->data, QMIN(shd->len,newsize) );
221  DELETE(shd->data);
222  shd->data = newdata;
223 #else
224  shd->data = (char *)realloc( shd->data, newsize );
225 #endif
226  } else {
227  shd->data = NEW(char,newsize);
228  }
229  CHECK_PTR( shd->data );
230  if ( !shd->data ) // no memory
231  return FALSE;
232  shd->len = newsize;
233  return TRUE;
234 }
const bool FALSE
Definition: qglobal.h:370
#define DELETE(array)
Definition: qgarray.cpp:50
array_data * shd
Definition: qgarray.h:104
#define QMIN(a, b)
Definition: qglobal.h:391
#define NEW(type, size)
Definition: qgarray.cpp:49
#define CHECK_PTR(p)
Definition: qglobal.h:601
QGArray & duplicate(const QGArray &a)
Definition: qgarray.cpp:327
const bool TRUE
Definition: qglobal.h:371
bool QGArray::setExpand ( uint  index,
const char *  d,
uint  sz 
)
protected

Definition at line 700 of file qgarray.cpp.

701 {
702  index *= sz;
703  if ( index >= shd->len ) {
704  if ( !resize( index+sz ) ) // no memory
705  return FALSE;
706  }
707  memcpy( data() + index, d, sz );
708  return TRUE;
709 }
char * data() const
Definition: qgarray.h:67
const bool FALSE
Definition: qglobal.h:370
array_data * shd
Definition: qgarray.h:104
bool resize(uint newsize)
Definition: qgarray.cpp:209
const bool TRUE
Definition: qglobal.h:371
QGArray & QGArray::setRawData ( const char *  d,
uint  len 
)
protected

Definition at line 483 of file qgarray.cpp.

484 {
485  duplicate( 0, 0 ); // set null data
486  shd->data = (char *)d;
487  shd->len = len;
488  return *this;
489 }
array_data * shd
Definition: qgarray.h:104
QGArray & duplicate(const QGArray &a)
Definition: qgarray.cpp:327
void QGArray::setSharedBlock ( array_data p)
inlineprotected

Definition at line 83 of file qgarray.h.

83 { shd=(array_data*)p; }
array_data * shd
Definition: qgarray.h:104
p
Definition: test.py:223
array_data * QGArray::sharedBlock ( ) const
inlineprotected

Definition at line 82 of file qgarray.h.

82 { return shd; }
array_data * shd
Definition: qgarray.h:104
uint QGArray::size ( ) const
inlineprotected

Definition at line 69 of file qgarray.h.

69 { return shd->len; }
array_data * shd
Definition: qgarray.h:104
void QGArray::sort ( uint  sz)
protected

Definition at line 652 of file qgarray.cpp.

653 {
654  int numItems = size() / sz;
655  if ( numItems < 2 )
656  return;
657  cmp_item_size = sz;
658  qsort( shd->data, numItems, sz, cmp_arr );
659 }
static int cmp_item_size
Definition: qgarray.cpp:629
array_data * shd
Definition: qgarray.h:104
uint size() const
Definition: qgarray.h:69
static int cmp_arr(const void *n1, const void *n2)
Definition: qgarray.cpp:635
void QGArray::store ( const char *  d,
uint  len 
)
protected

Definition at line 412 of file qgarray.cpp.

413 { // store, but not deref
414  resize( len );
415  memcpy( shd->data, d, len );
416 }
array_data * shd
Definition: qgarray.h:104
bool resize(uint newsize)
Definition: qgarray.cpp:209

Friends And Related Function Documentation

friend class QBuffer
friend

Definition at line 48 of file qgarray.h.

Member Data Documentation

array_data* QGArray::shd
private

Definition at line 104 of file qgarray.h.


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