📄 qmap.h
字号:
// more Qt typedef iterator Iterator; typedef const_iterator ConstIterator; inline int count() const { return d->size; } iterator find(const Key &key); const_iterator find(const Key &key) const; const_iterator constFind(const Key &key) const; iterator lowerBound(const Key &key); const_iterator lowerBound(const Key &key) const; iterator upperBound(const Key &key); const_iterator upperBound(const Key &key) const; iterator insert(const Key &key, const T &value);#ifdef QT3_SUPPORT QT3_SUPPORT iterator insert(const Key &key, const T &value, bool overwrite);#endif iterator insertMulti(const Key &key, const T &value);#ifdef QT3_SUPPORT inline QT3_SUPPORT iterator replace(const Key &key, const T &value) { return insert(key, value); }#endif QMap<Key, T> &unite(const QMap<Key, T> &other); // STL compatibility typedef Key key_type; typedef T mapped_type; typedef ptrdiff_t difference_type; typedef int size_type; inline bool empty() const { return isEmpty(); }#ifdef QT_QMAP_DEBUG inline void dump() const { d->dump(); }#endifprivate: void detach_helper(); void freeData(QMapData *d); QMapData::Node *findNode(const Key &key) const; QMapData::Node *mutableFindNode(QMapData::Node *update[], const Key &key) const; QMapData::Node *node_create(QMapData *d, QMapData::Node *update[], const Key &key, const T &value);};template <class Key, class T>Q_INLINE_TEMPLATE QMap<Key, T> &QMap<Key, T>::operator=(const QMap<Key, T> &other){ if (d != other.d) { QMapData *x = other.d; x->ref.ref(); x = qAtomicSetPtr(&d, x); if (!x->ref.deref()) freeData(x); if (!d->sharable) detach_helper(); } return *this;}template <class Key, class T>Q_INLINE_TEMPLATE void QMap<Key, T>::clear(){ *this = QMap<Key, T>();}template <class Key, class T>Q_INLINE_TEMPLATE typename QMapData::Node *QMap<Key, T>::node_create(QMapData *adt, QMapData::Node *aupdate[], const Key &akey, const T &avalue){ QMapData::Node *abstractNode = adt->node_create(aupdate, payload()); Node *concreteNode = concrete(abstractNode); new (&concreteNode->key) Key(akey); new (&concreteNode->value) T(avalue); return abstractNode;}template <class Key, class T>Q_INLINE_TEMPLATE QMapData::Node *QMap<Key, T>::findNode(const Key &akey) const{ QMapData::Node *cur = e; QMapData::Node *next = e; for (int i = d->topLevel; i >= 0; i--) { while ((next = cur->forward[i]) != e && qMapLessThanKey<Key>(concrete(next)->key, akey)) cur = next; } if (next != e && !qMapLessThanKey<Key>(akey, concrete(next)->key)) { return next; } else { return e; }}template <class Key, class T>Q_INLINE_TEMPLATE const T QMap<Key, T>::value(const Key &akey) const{ QMapData::Node *node; if (d->size == 0 || (node = findNode(akey)) == e) { return T(); } else { return concrete(node)->value; }}template <class Key, class T>Q_INLINE_TEMPLATE const T QMap<Key, T>::value(const Key &akey, const T &adefaultValue) const{ QMapData::Node *node; if (d->size == 0 || (node = findNode(akey)) == e) { return adefaultValue; } else { return concrete(node)->value; }}template <class Key, class T>Q_INLINE_TEMPLATE const T QMap<Key, T>::operator[](const Key &akey) const{ return value(akey);}template <class Key, class T>Q_INLINE_TEMPLATE T &QMap<Key, T>::operator[](const Key &akey){ detach(); QMapData::Node *update[QMapData::LastLevel + 1]; QMapData::Node *node = mutableFindNode(update, akey); if (node == e) node = node_create(d, update, akey, T()); return concrete(node)->value;}template <class Key, class T>Q_INLINE_TEMPLATE int QMap<Key, T>::count(const Key &akey) const{ int cnt = 0; QMapData::Node *node = findNode(akey); if (node != e) { do { ++cnt; node = node->forward[0]; } while (node != e && !qMapLessThanKey<Key>(akey, concrete(node)->key)); } return cnt;}template <class Key, class T>Q_INLINE_TEMPLATE bool QMap<Key, T>::contains(const Key &akey) const{ return findNode(akey) != e;}template <class Key, class T>Q_INLINE_TEMPLATE typename QMap<Key, T>::iterator QMap<Key, T>::insert(const Key &akey, const T &avalue){ detach(); QMapData::Node *update[QMapData::LastLevel + 1]; QMapData::Node *node = mutableFindNode(update, akey); if (node == e) { node = node_create(d, update, akey, avalue); } else { concrete(node)->value = avalue; } return iterator(node);}#ifdef QT3_SUPPORTtemplate <class Key, class T>Q_INLINE_TEMPLATE typename QMap<Key, T>::iterator QMap<Key, T>::insert(const Key &akey, const T &avalue, bool aoverwrite){ detach(); QMapData::Node *update[QMapData::LastLevel + 1]; QMapData::Node *node = mutableFindNode(update, akey); if (node == e) { node = node_create(d, update, akey, avalue); } else { if (aoverwrite) concrete(node)->value = avalue; } return iterator(node);}#endiftemplate <class Key, class T>Q_INLINE_TEMPLATE typename QMap<Key, T>::iterator QMap<Key, T>::insertMulti(const Key &akey, const T &avalue){ detach(); QMapData::Node *update[QMapData::LastLevel + 1]; mutableFindNode(update, akey); return iterator(node_create(d, update, akey, avalue));}template <class Key, class T>Q_INLINE_TEMPLATE typename QMap<Key, T>::const_iterator QMap<Key, T>::find(const Key &akey) const{ return const_iterator(findNode(akey));}template <class Key, class T>Q_INLINE_TEMPLATE typename QMap<Key, T>::const_iterator QMap<Key, T>::constFind(const Key &akey) const{ return const_iterator(findNode(akey));}template <class Key, class T>Q_INLINE_TEMPLATE typename QMap<Key, T>::iterator QMap<Key, T>::find(const Key &akey){ detach(); return iterator(findNode(akey));}template <class Key, class T>Q_INLINE_TEMPLATE QMap<Key, T> &QMap<Key, T>::unite(const QMap<Key, T> &other){ QMap<Key, T> copy(other); const_iterator it = copy.constEnd(); const const_iterator b = copy.constBegin(); while (it != b) { --it; insertMulti(it.key(), it.value()); } return *this;}template <class Key, class T>Q_OUTOFLINE_TEMPLATE void QMap<Key, T>::freeData(QMapData *x){ if (QTypeInfo<Key>::isComplex || QTypeInfo<T>::isComplex) { QMapData::Node *y = reinterpret_cast<QMapData::Node *>(x); QMapData::Node *cur = y; QMapData::Node *next = cur->forward[0]; while (next != y) { cur = next; next = cur->forward[0];#if defined(_MSC_VER) && (_MSC_VER >= 1300)#pragma warning(disable:4189)#endif Node *concreteNode = concrete(cur); concreteNode->key.~Key(); concreteNode->value.~T();#if defined(_MSC_VER) && (_MSC_VER >= 1300)#pragma warning(default:4189)#endif } } x->continueFreeData(payload());}template <class Key, class T>Q_OUTOFLINE_TEMPLATE int QMap<Key, T>::remove(const Key &akey){ detach(); QMapData::Node *update[QMapData::LastLevel + 1]; QMapData::Node *cur = e; QMapData::Node *next = e; int oldSize = d->size; for (int i = d->topLevel; i >= 0; i--) { while ((next = cur->forward[i]) != e && qMapLessThanKey<Key>(concrete(next)->key, akey)) cur = next; update[i] = cur; } if (next != e && !qMapLessThanKey<Key>(akey, concrete(next)->key)) { bool deleteNext = true; do { cur = next; next = cur->forward[0]; deleteNext = (next != e && !qMapLessThanKey<Key>(concrete(cur)->key, concrete(next)->key)); concrete(cur)->key.~Key(); concrete(cur)->value.~T(); d->node_delete(update, payload(), cur); } while (deleteNext); } return oldSize - d->size;}template <class Key, class T>Q_OUTOFLINE_TEMPLATE T QMap<Key, T>::take(const Key &akey){ detach(); QMapData::Node *update[QMapData::LastLevel + 1]; QMapData::Node *cur = e; QMapData::Node *next = e; for (int i = d->topLevel; i >= 0; i--) { while ((next = cur->forward[i]) != e && qMapLessThanKey<Key>(concrete(next)->key, akey)) cur = next; update[i] = cur; } if (next != e && !qMapLessThanKey<Key>(akey, concrete(next)->key)) { T t = concrete(next)->value; concrete(next)->key.~Key(); concrete(next)->value.~T(); d->node_delete(update, payload(), next); return t; } return T();}template <class Key, class T>Q_OUTOFLINE_TEMPLATE typename QMap<Key, T>::iterator QMap<Key, T>::erase(iterator it){ QMapData::Node *update[QMapData::LastLevel + 1]; QMapData::Node *cur = e; QMapData::Node *next = e; if (it == iterator(e)) return it; for (int i = d->topLevel; i >= 0; i--) { while ((next = cur->forward[i]) != e && qMapLessThanKey<Key>(concrete(next)->key, it.key())) cur = next; update[i] = cur; } while (next != e) { cur = next; next = cur->forward[0]; if (cur == it) { concrete(cur)->key.~Key(); concrete(cur)->value.~T(); d->node_delete(update, payload(), cur); return iterator(next); } for (int i = 0; i <= d->topLevel; ++i) { if (update[i]->forward[i] != cur)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -