📄 qmap.h
字号:
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(); while (it != copy.constBegin()) { --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]; Node *concreteNode = concrete(cur); concreteNode->key.~Key(); concreteNode->value.~T(); } } 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) break; update[i] = cur; } } return end();}template <class Key, class T>Q_OUTOFLINE_TEMPLATE void QMap<Key, T>::detach_helper(){ union { QMapData *d; QMapData::Node *e; } x; x.d = QMapData::createData(); if (d->size) { x.d->insertInOrder = true; QMapData::Node *update[QMapData::LastLevel + 1]; QMapData::Node *cur = e->forward[0]; update[0] = x.e; while (cur != e) { Node *concreteNode = concrete(cur); node_create(x.d, update, concreteNode->key, concreteNode->value); cur = cur->forward[0]; } x.d->insertInOrder = false; } x.d = qAtomicSetPtr(&d, x.d); if (!x.d->ref.deref()) freeData(x.d);}template <class Key, class T>Q_OUTOFLINE_TEMPLATE QMapData::Node *QMap<Key, T>::mutableFindNode(QMapData::Node *aupdate[], 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; aupdate[i] = cur; } if (next != e && !qMapLessThanKey<Key>(akey, concrete(next)->key)) { return next; } else { return e; }}template <class Key, class T>Q_OUTOFLINE_TEMPLATE QList<Key> QMap<Key, T>::keys() const{ QList<Key> res; const_iterator i = begin(); while (i != end()) { res.append(i.key()); ++i; } return res;}template <class Key, class T>Q_OUTOFLINE_TEMPLATE QList<Key> QMap<Key, T>::keys(const T &avalue) const{ QList<Key> res; const_iterator i = begin(); while (i != end()) { if (i.value() == avalue) res.append(i.key()); ++i; } return res;}template <class Key, class T>Q_OUTOFLINE_TEMPLATE const Key QMap<Key, T>::key(const T &avalue) const{ const_iterator i = begin(); while (i != end()) { if (i.value() == avalue) return i.key(); ++i; } return Key();}template <class Key, class T>Q_OUTOFLINE_TEMPLATE QList<T> QMap<Key, T>::values() const{ QList<T> res; const_iterator i = begin(); while (i != end()) { res.append(i.value()); ++i; } return res;}template <class Key, class T>Q_OUTOFLINE_TEMPLATE QList<T> QMap<Key, T>::values(const Key &akey) const{ QList<T> res; QMapData::Node *node = findNode(akey); if (node != e) { do { res.append(concrete(node)->value); node = node->forward[0]; } while (node != e && !qMapLessThanKey<Key>(akey, concrete(node)->key)); } return res;}template <class Key, class T>Q_INLINE_TEMPLATE typename QMap<Key, T>::const_iteratorQMap<Key, T>::lowerBound(const Key &akey) const{ QMapData::Node *update[QMapData::LastLevel + 1]; mutableFindNode(update, akey); return const_iterator(update[0]->forward[0]);}template <class Key, class T>Q_INLINE_TEMPLATE typename QMap<Key, T>::iterator QMap<Key, T>::lowerBound(const Key &akey){ detach(); return static_cast<QMapData::Node *>(const_cast<const QMap *>(this)->lowerBound(akey));}template <class Key, class T>Q_INLINE_TEMPLATE typename QMap<Key, T>::const_iteratorQMap<Key, T>::upperBound(const Key &akey) const{ QMapData::Node *update[QMapData::LastLevel + 1]; mutableFindNode(update, akey); QMapData::Node *node = update[0]->forward[0]; while (node != e && !qMapLessThanKey<Key>(akey, concrete(node)->key)) node = node->forward[0]; return const_iterator(node);}template <class Key, class T>Q_INLINE_TEMPLATE typename QMap<Key, T>::iterator QMap<Key, T>::upperBound(const Key &akey){ detach(); return static_cast<QMapData::Node *>(const_cast<const QMap *>(this)->upperBound(akey));}template <class Key, class T>Q_OUTOFLINE_TEMPLATE bool QMap<Key, T>::operator==(const QMap<Key, T> &other) const{ if (size() != other.size()) return false; if (d == other.d) return true; const_iterator it1 = begin(); const_iterator it2 = other.begin(); while (it1 != end()) { if (!(it1.value() == it2.value()) || qMapLessThanKey(it1.key(), it2.key()) || qMapLessThanKey(it2.key(), it1.key())) return false; ++it2; ++it1; } return true;}#ifndef QT_NO_STLtemplate <class Key, class T>Q_OUTOFLINE_TEMPLATE QMap<Key, T>::QMap(const std::map<Key, T> &other){ d = QMapData::createData(); d->insertInOrder = true; typename std::map<Key,T>::const_iterator it = other.end(); while (it != other.begin()) { --it; insert((*it).first, (*it).second); } d->insertInOrder = false;}template <class Key, class T>Q_OUTOFLINE_TEMPLATE std::map<Key, T> QMap<Key, T>::toStdMap() const{ std::map<Key, T> map; const_iterator it = end(); while (it != begin()) { --it; map.insert(std::pair<Key, T>(it.key(), it.value())); } return map;}#endif // QT_NO_STLtemplate <class Key, class T>class QMultiMap : public QMap<Key, T>{public: QMultiMap() {} QMultiMap(const QMap<Key, T> &other) : QMap<Key, T>(other) {} inline typename QMap<Key, T>::iterator replace(const Key &key, const T &value); inline typename QMap<Key, T>::iterator insert(const Key &key, const T &value); inline QMultiMap &operator+=(const QMultiMap &other) { unite(other); return *this; } inline QMultiMap operator+(const QMultiMap &other) const { QMultiMap result = *this; result += other; return result; }private: T &operator[](const Key &key); const T operator[](const Key &key) const;};template <class Key, class T>Q_INLINE_TEMPLATE Q_TYPENAME QMap<Key, T>::iterator QMultiMap<Key, T>::replace(const Key &akey, const T &avalue){ return QMap<Key, T>::insert(akey, avalue); }template <class Key, class T>Q_INLINE_TEMPLATE Q_TYPENAME QMap<Key, T>::iterator QMultiMap<Key, T>::insert(const Key &akey, const T &avalue){ return QMap<Key, T>::insertMulti(akey, avalue); }Q_DECLARE_ASSOCIATIVE_ITERATOR(Map)Q_DECLARE_MUTABLE_ASSOCIATIVE_ITERATOR(Map)QT_END_HEADER#endif // QMAP_H
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -