📄 q3gdict.cpp
字号:
/*! Removes all items from the dictionary.*/void Q3GDict::clear(){ if (!numItems) return; numItems = 0; // disable remove() function for (uint j=0; j<vlen; j++) { // destroy hash table if (vec[j]) { switch (keytype) { case StringKey: { Q3StringBucket *n=(Q3StringBucket *)vec[j]; while (n) { Q3StringBucket *next = (Q3StringBucket*)n->getNext(); deleteItem(n->getData()); delete n; n = next; } } break; case AsciiKey: { Q3AsciiBucket *n=(Q3AsciiBucket *)vec[j]; while (n) { Q3AsciiBucket *next = (Q3AsciiBucket*)n->getNext(); if (copyk) delete [] (char *)n->getKey(); deleteItem(n->getData()); delete n; n = next; } } break; case IntKey: { Q3IntBucket *n=(Q3IntBucket *)vec[j]; while (n) { Q3IntBucket *next = (Q3IntBucket*)n->getNext(); deleteItem(n->getData()); delete n; n = next; } } break; case PtrKey: { Q3PtrBucket *n=(Q3PtrBucket *)vec[j]; while (n) { Q3PtrBucket *next = (Q3PtrBucket*)n->getNext(); deleteItem(n->getData()); delete n; n = next; } } break; } vec[j] = 0; // detach list of buckets } } if (iterators && iterators->count()) { // invalidate all iterators Q3GDictIterator *i = iterators->first(); while (i) { i->curNode = 0; i = iterators->next(); } }}/*! Outputs debug statistics.*/void Q3GDict::statistics() const{#if defined(QT_DEBUG) QString line; line.fill(QLatin1Char('-'), 60); double real, ideal; qDebug("%s", line.ascii()); qDebug("DICTIONARY STATISTICS:"); if (count() == 0) { qDebug("Empty!"); qDebug("%s", line.ascii()); return; } real = 0.0; ideal = (float)count()/(2.0*size())*(count()+2.0*size()-1); uint i = 0; while (i<size()) { Q3BaseBucket *n = vec[i]; int b = 0; while (n) { // count number of buckets b++; n = n->getNext(); } real = real + (double)b * ((double)b+1.0)/2.0; char buf[80], *pbuf; if (b > 78) b = 78; pbuf = buf; while (b--) *pbuf++ = '*'; *pbuf = '\0'; qDebug("%s", buf); i++; } qDebug("Array size = %d", size()); qDebug("# items = %d", count()); qDebug("Real dist = %g", real); qDebug("Rand dist = %g", ideal); qDebug("Real/Rand = %g", real/ideal); qDebug("%s", line.ascii());#endif // QT_DEBUG}/***************************************************************************** Q3GDict stream functions *****************************************************************************/#ifndef QT_NO_DATASTREAMQDataStream &operator>>(QDataStream &s, Q3GDict &dict){ return dict.read(s);}QDataStream &operator<<(QDataStream &s, const Q3GDict &dict){ return dict.write(s);}#if defined(Q_CC_DEC) && defined(__alpha) && (__DECCXX_VER-0 >= 50190001)#pragma message disable narrowptr#endif/*! Reads a dictionary from the stream \a s.*/QDataStream &Q3GDict::read(QDataStream &s){ uint num; s >> num; // read number of items clear(); // clear dict while (num--) { // read all items Item d; switch (keytype) { case StringKey: { QString k; s >> k; read(s, d); look_string(k, d, op_insert); } break; case AsciiKey: { char *k; s >> k; read(s, d); look_ascii(k, d, op_insert); if (copyk) delete [] k; } break; case IntKey: { Q_UINT32 k; s >> k; read(s, d); look_int(k, d, op_insert); } break; case PtrKey: { Q_UINT32 k; s >> k; read(s, d); // ### cannot insert 0 - this renders the thing // useless since all pointers are written as 0, // but hey, serializing pointers? can it be done // at all, ever? if (k) look_ptr((void *)(ulong)k, d, op_insert); } break; } } return s;}/*! Writes the dictionary to the stream \a s.*/QDataStream& Q3GDict::write(QDataStream &s) const{ s << count(); // write number of items uint i = 0; while (i<size()) { Q3BaseBucket *n = vec[i]; while (n) { // write all buckets switch (keytype) { case StringKey: s << ((Q3StringBucket*)n)->getKey(); break; case AsciiKey: s << ((Q3AsciiBucket*)n)->getKey(); break; case IntKey: s << (Q_UINT32)((Q3IntBucket*)n)->getKey(); break; case PtrKey: s << (Q_UINT32)0; // ### cannot serialize a pointer break; } write(s, n->getData()); // write data n = n->getNext(); } i++; } return s;}#endif //QT_NO_DATASTREAM/***************************************************************************** Q3GDictIterator member functions *****************************************************************************//*! \class Q3GDictIterator qgdict.h \reentrant \brief The Q3GDictIterator class is an internal class for implementing QDictIterator and QIntDictIterator. \internal Q3GDictIterator is a strictly internal class that does the heavy work for QDictIterator and QIntDictIterator.*//*! Constructs an iterator that operates on the dictionary \a d.*/Q3GDictIterator::Q3GDictIterator(const Q3GDict &d){ dict = (Q3GDict *)&d; // get reference to dict toFirst(); // set to first noe if (!dict->iterators) { dict->iterators = new Q3GDItList; // create iterator list Q_CHECK_PTR(dict->iterators); } dict->iterators->append(this); // attach iterator to dict}/*! Constructs a copy of the iterator \a it.*/Q3GDictIterator::Q3GDictIterator(const Q3GDictIterator &it){ dict = it.dict; curNode = it.curNode; curIndex = it.curIndex; if (dict) dict->iterators->append(this); // attach iterator to dict}/*! Assigns a copy of the iterator \a it and returns a reference to this iterator.*/Q3GDictIterator &Q3GDictIterator::operator=(const Q3GDictIterator &it){ if (dict) // detach from old dict dict->iterators->removeRef(this); dict = it.dict; curNode = it.curNode; curIndex = it.curIndex; if (dict) dict->iterators->append(this); // attach to new list return *this;}/*! Destroys the iterator.*/Q3GDictIterator::~Q3GDictIterator(){ if (dict) // detach iterator from dict dict->iterators->removeRef(this);}/*! Sets the iterator to point to the first item in the dictionary.*/Q3PtrCollection::Item Q3GDictIterator::toFirst(){ if (!dict) {#if defined(QT_CHECK_NULL) qWarning("Q3GDictIterator::toFirst: Dictionary has been deleted");#endif return 0; } if (dict->count() == 0) { // empty dictionary curNode = 0; return 0; } register uint i = 0; register Q3BaseBucket **v = dict->vec; while (!(*v++)) i++; curNode = dict->vec[i]; curIndex = i; return curNode->getData();}/*! Moves to the next item (postfix).*/Q3PtrCollection::Item Q3GDictIterator::operator()(){ if (!dict) {#if defined(QT_CHECK_NULL) qWarning("Q3GDictIterator::operator(): Dictionary has been deleted");#endif return 0; } if (!curNode) return 0; Q3PtrCollection::Item d = curNode->getData(); this->operator++(); return d;}/*! Moves to the next item (prefix).*/Q3PtrCollection::Item Q3GDictIterator::operator++(){ if (!dict) {#if defined(QT_CHECK_NULL) qWarning("Q3GDictIterator::operator++: Dictionary has been deleted");#endif return 0; } if (!curNode) return 0; curNode = curNode->getNext(); if (!curNode) { // no next bucket register uint i = curIndex + 1; // look from next vec element register Q3BaseBucket **v = &dict->vec[i]; while (i < dict->size() && !(*v++)) i++; if (i == dict->size()) { // nothing found curNode = 0; return 0; } curNode = dict->vec[i]; curIndex = i; } return curNode->getData();}/*! Moves \a jumps positions forward.*/Q3PtrCollection::Item Q3GDictIterator::operator+=(uint jumps){ while (curNode && jumps--) operator++(); return curNode ? curNode->getData() : 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -