📄 q3gdict.cpp
字号:
} return 0; // not found } if (op == op_replace) { // replace if (vec[index] != 0) // maybe something there remove_ascii(key); } // op_insert or op_replace n = new Q3AsciiBucket(copyk ? qstrdup(key) : key,newItem(d),vec[index]); Q_CHECK_PTR(n);#if defined(QT_CHECK_NULL) if (n->getData() == 0) qWarning("QAsciiDict: Cannot insert null item");#endif vec[index] = n; numItems++; return n->getData();}Q3PtrCollection::Item Q3GDict::look_int(long key, Q3PtrCollection::Item d, int op){ Q3IntBucket *n; int index = (int)((ulong)key % vlen); // simple hash if (op == op_find) { // find for (n=(Q3IntBucket*)vec[index]; n; n=(Q3IntBucket*)n->getNext()) { if (n->getKey() == key) return n->getData(); // item found } return 0; // not found } if (op == op_replace) { // replace if (vec[index] != 0) // maybe something there remove_int(key); } // op_insert or op_replace n = new Q3IntBucket(key,newItem(d),vec[index]); Q_CHECK_PTR(n);#if defined(QT_CHECK_NULL) if (n->getData() == 0) qWarning("QIntDict: Cannot insert null item");#endif vec[index] = n; numItems++; return n->getData();}Q3PtrCollection::Item Q3GDict::look_ptr(void *key, Q3PtrCollection::Item d, int op){ Q3PtrBucket *n; int index = (int)((ulong)key % vlen); // simple hash if (op == op_find) { // find for (n=(Q3PtrBucket*)vec[index]; n; n=(Q3PtrBucket*)n->getNext()) { if (n->getKey() == key) return n->getData(); // item found } return 0; // not found } if (op == op_replace) { // replace if (vec[index] != 0) // maybe something there remove_ptr(key); } // op_insert or op_replace n = new Q3PtrBucket(key,newItem(d),vec[index]); Q_CHECK_PTR(n);#if defined(QT_CHECK_NULL) if (n->getData() == 0) qWarning("Q3PtrDict: Cannot insert null item");#endif vec[index] = n; numItems++; return n->getData();}/*! Changes the size of the hashtable to \a newsize. The contents of the dictionary are preserved, but all iterators on the dictionary become invalid.*/void Q3GDict::resize(uint newsize){ // Save old information Q3BaseBucket **old_vec = vec; uint old_vlen = vlen; bool old_copyk = copyk; vec = new Q3BaseBucket *[vlen = newsize]; Q_CHECK_PTR(vec); memset((char*)vec, 0, vlen*sizeof(Q3BaseBucket*)); numItems = 0; copyk = false; // Reinsert every item from vec, deleting vec as we go for (uint index = 0; index < old_vlen; index++) { switch (keytype) { case StringKey: { Q3StringBucket *n=(Q3StringBucket *)old_vec[index]; while (n) { look_string(n->getKey(), n->getData(), op_insert); Q3StringBucket *t=(Q3StringBucket *)n->getNext(); delete n; n = t; } } break; case AsciiKey: { Q3AsciiBucket *n=(Q3AsciiBucket *)old_vec[index]; while (n) { look_ascii(n->getKey(), n->getData(), op_insert); Q3AsciiBucket *t=(Q3AsciiBucket *)n->getNext(); delete n; n = t; } } break; case IntKey: { Q3IntBucket *n=(Q3IntBucket *)old_vec[index]; while (n) { look_int(n->getKey(), n->getData(), op_insert); Q3IntBucket *t=(Q3IntBucket *)n->getNext(); delete n; n = t; } } break; case PtrKey: { Q3PtrBucket *n=(Q3PtrBucket *)old_vec[index]; while (n) { look_ptr(n->getKey(), n->getData(), op_insert); Q3PtrBucket *t=(Q3PtrBucket *)n->getNext(); delete n; n = t; } } break; } } delete [] old_vec; // Restore state copyk = old_copyk; // Invalidate all iterators, since order is lost if (iterators && iterators->count()) { Q3GDictIterator *i = iterators->first(); while (i) { i->toFirst(); i = iterators->next(); } }}/*! Unlinks the bucket with the specified key (and specified data pointer, if it is set).*/void Q3GDict::unlink_common(int index, Q3BaseBucket *node, Q3BaseBucket *prev){ if (iterators && iterators->count()) { // update iterators Q3GDictIterator *i = iterators->first(); while (i) { // invalidate all iterators if (i->curNode == node) // referring to pending node i->operator++(); i = iterators->next(); } } if (prev) // unlink node prev->setNext(node->getNext()); else vec[index] = node->getNext(); numItems--;}Q3StringBucket *Q3GDict::unlink_string(const QString &key, Q3PtrCollection::Item d){ if (numItems == 0) // nothing in dictionary return 0; Q3StringBucket *n; Q3StringBucket *prev = 0; int index = hashKeyString(key) % vlen; if (cases) { for (n=(Q3StringBucket*)vec[index]; n; n=(Q3StringBucket*)n->getNext()) { bool found = (key == n->getKey()); if (found && d) found = (n->getData() == d); if (found) { unlink_common(index,n,prev); return n; } prev = n; } } else { QString k = key.lower(); for (n=(Q3StringBucket*)vec[index]; n; n=(Q3StringBucket*)n->getNext()) { bool found = (k == n->getKey().lower()); if (found && d) found = (n->getData() == d); if (found) { unlink_common(index,n,prev); return n; } prev = n; } } return 0;}Q3AsciiBucket *Q3GDict::unlink_ascii(const char *key, Q3PtrCollection::Item d){ if (numItems == 0) // nothing in dictionary return 0; Q3AsciiBucket *n; Q3AsciiBucket *prev = 0; int index = hashKeyAscii(key) % vlen; for (n=(Q3AsciiBucket *)vec[index]; n; n=(Q3AsciiBucket *)n->getNext()) { bool found = (cases ? qstrcmp(n->getKey(),key) : qstricmp(n->getKey(),key)) == 0; if (found && d) found = (n->getData() == d); if (found) { unlink_common(index,n,prev); return n; } prev = n; } return 0;}Q3IntBucket *Q3GDict::unlink_int(long key, Q3PtrCollection::Item d){ if (numItems == 0) // nothing in dictionary return 0; Q3IntBucket *n; Q3IntBucket *prev = 0; int index = (int)((ulong)key % vlen); for (n=(Q3IntBucket *)vec[index]; n; n=(Q3IntBucket *)n->getNext()) { bool found = (n->getKey() == key); if (found && d) found = (n->getData() == d); if (found) { unlink_common(index,n,prev); return n; } prev = n; } return 0;}Q3PtrBucket *Q3GDict::unlink_ptr(void *key, Q3PtrCollection::Item d){ if (numItems == 0) // nothing in dictionary return 0; Q3PtrBucket *n; Q3PtrBucket *prev = 0; int index = (int)((ulong)key % vlen); for (n=(Q3PtrBucket *)vec[index]; n; n=(Q3PtrBucket *)n->getNext()) { bool found = (n->getKey() == key); if (found && d) found = (n->getData() == d); if (found) { unlink_common(index,n,prev); return n; } prev = n; } return 0;}/*! Removes the item with the specified \a key. If \a item is not null, the remove will match the \a item as well (used to remove an item when several items have the same key).*/bool Q3GDict::remove_string(const QString &key, Q3PtrCollection::Item item){ Q3StringBucket *n = unlink_string(key, item); if (n) { deleteItem(n->getData()); delete n; return true; } else { return false; }}bool Q3GDict::remove_ascii(const char *key, Q3PtrCollection::Item item){ Q3AsciiBucket *n = unlink_ascii(key, item); if (n) { if (copyk) delete [] (char *)n->getKey(); deleteItem(n->getData()); delete n; } return n != 0;}bool Q3GDict::remove_int(long key, Q3PtrCollection::Item item){ Q3IntBucket *n = unlink_int(key, item); if (n) { deleteItem(n->getData()); delete n; } return n != 0;}bool Q3GDict::remove_ptr(void *key, Q3PtrCollection::Item item){ Q3PtrBucket *n = unlink_ptr(key, item); if (n) { deleteItem(n->getData()); delete n; } return n != 0;}Q3PtrCollection::Item Q3GDict::take_string(const QString &key){ Q3StringBucket *n = unlink_string(key); Item d; if (n) { d = n->getData(); delete n; } else { d = 0; } return d;}Q3PtrCollection::Item Q3GDict::take_ascii(const char *key){ Q3AsciiBucket *n = unlink_ascii(key); Item d; if (n) { if (copyk) delete [] (char *)n->getKey(); d = n->getData(); delete n; } else { d = 0; } return d;}Q3PtrCollection::Item Q3GDict::take_int(long key){ Q3IntBucket *n = unlink_int(key); Item d; if (n) { d = n->getData(); delete n; } else { d = 0; } return d;}Q3PtrCollection::Item Q3GDict::take_ptr(void *key){ Q3PtrBucket *n = unlink_ptr(key); Item d; if (n) { d = n->getData(); delete n; } else { d = 0; } return d;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -