📄 qmap.h
字号:
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>::uniqueKeys() const{ QList<Key> res; const_iterator i = begin(); if (i != end()) { for (;;) { const Key &aKey = i.key(); res.append(aKey); do { if (++i == end()) goto break_out_of_outer_loop; } while (!(aKey < i.key())); // loop while (key == i.key()) } }break_out_of_outer_loop: return res;}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{ return key(avalue, Key());}template <class Key, class T>Q_OUTOFLINE_TEMPLATE const Key QMap<Key, T>::key(const T &avalue, const Key &defaultKey) const{ const_iterator i = begin(); while (i != end()) { if (i.value() == avalue) return i.key(); ++i; } return defaultKey;}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) { return QMap<Key, T>::insert(key, value); } inline typename QMap<Key, T>::iterator insert(const Key &key, const T &value) { return QMap<Key, T>::insertMulti(key, 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; }#ifndef Q_NO_USING_KEYWORD using QMap<Key, T>::contains; using QMap<Key, T>::remove; using QMap<Key, T>::count; using QMap<Key, T>::find; using QMap<Key, T>::constFind;#else inline bool contains(const Key &key) const { return QMap<Key, T>::contains(key); } inline int remove(const Key &key) { return QMap<Key, T>::remove(key); } inline int count(const Key &key) const { return QMap<Key, T>::count(key); } inline int count() const { return QMap<Key, T>::count(); } inline typename QMap<Key, T>::iterator find(const Key &key) { return QMap<Key, T>::find(key); } inline typename QMap<Key, T>::const_iterator find(const Key &key) const { return QMap<Key, T>::find(key); } inline typename QMap<Key, T>::const_iterator constFind(const Key &key) const { return QMap<Key, T>::constFind(key); }#endif bool contains(const Key &key, const T &value) const; int remove(const Key &key, const T &value); int count(const Key &key, const T &value) const; typename QMap<Key, T>::iterator find(const Key &key, const T &value) { typename QMap<Key, T>::iterator i(find(key)); typename QMap<Key, T>::iterator end(this->end()); while (i != end && !qMapLessThanKey<Key>(key, i.key())) { if (i.value() == value) return i; ++i; } return end; } typename QMap<Key, T>::const_iterator find(const Key &key, const T &value) const { typename QMap<Key, T>::const_iterator i(constFind(key)); typename QMap<Key, T>::const_iterator end(QMap<Key, T>::constEnd()); while (i != end && !qMapLessThanKey<Key>(key, i.key())) { if (i.value() == value) return i; ++i; } return end; } typename QMap<Key, T>::const_iterator constFind(const Key &key, const T &value) const { return find(key, value); }private: T &operator[](const Key &key); const T operator[](const Key &key) const;};template <class Key, class T>Q_INLINE_TEMPLATE bool QMultiMap<Key, T>::contains(const Key &key, const T &value) const{ return constFind(key, value) != QMap<Key, T>::constEnd();}template <class Key, class T>Q_INLINE_TEMPLATE int QMultiMap<Key, T>::remove(const Key &key, const T &value){ int n = 0; typename QMap<Key, T>::iterator i(find(key)); typename QMap<Key, T>::const_iterator end(QMap<Key, T>::constEnd()); while (i != end && !qMapLessThanKey<Key>(key, i.key())) { if (i.value() == value) { i = erase(i); ++n; } else { ++i; } } return n;}template <class Key, class T>Q_INLINE_TEMPLATE int QMultiMap<Key, T>::count(const Key &key, const T &value) const{ int n = 0; typename QMap<Key, T>::const_iterator i(constFind(key)); typename QMap<Key, T>::const_iterator end(QMap<Key, T>::constEnd()); while (i != end && !qMapLessThanKey<Key>(key, i.key())) { if (i.value() == value) ++n; ++i; } return n;}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 + -