Ai
81 Star 380 Fork 168

LibQQt应用程序开发组织/LibQQt

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
qqtorderedmap.h 33.87 KB
一键复制 编辑 原始数据 按行查看 历史
T.D.R. 提交于 2021-04-23 11:57 +08:00 . fix qqtorderedmap.h for macOS building.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098
#ifndef QQTORDEREDMAP_H
#define QQTORDEREDMAP_H
#include <qqt-local.h>
#include <QtCore/qiterator.h>
#include <QtCore/qlist.h>
#include <QtCore/qrefcount.h>
#include <QtCore/qpair.h>
#ifdef Q_MAP_DEBUG
#include <QtCore/qdebug.h>
#endif
#include <map>
#include <new>
#include <functional>
#ifdef Q_COMPILER_INITIALIZER_LISTS
#include <initializer_list>
#endif
QT_BEGIN_NAMESPACE
/*
QOrderedMap uses qMapLessThanKey() to compare keys. The default
implementation uses operator<(). For pointer types,
qMapLessThanKey() uses std::less (because operator<() on
pointers can be used only between pointers in the same array).
*/
template <class Key> inline bool QQtMapLessThanKey ( const Key& key1, const Key& key2 )
{
return false;//key1 < key2;
}
template <class Ptr> inline bool QQtMapLessThanKey ( const Ptr* key1, const Ptr* key2 )
{
return false;//std::less<const Ptr*>() ( key1, key2 );
}
template <class Key, class T>
class QQTSHARED_EXPORT QOrderedMap
{
//一个map,里面包着一个list,
//list里面存的很多key,value对,里面都是对list进行操作。
typedef QPair<Key, T> Node;
//Node root; //empty ????
QList<Node> _d; //first() ... last()
QList<Node>* d;
//end() empty
public:
inline QOrderedMap() Q_DECL_NOTHROW : d ( &_d ) { }
#ifdef Q_COMPILER_INITIALIZER_LISTS
inline QOrderedMap ( std::initializer_list<std::pair<Key, T> > list )
: d ( &_d ) {
for ( typename std::initializer_list<std::pair<Key, T> >::const_iterator it = list.begin(); it != list.end(); ++it )
insert ( it->first, it->second );
}
#endif
//QOrderedMap ( const QOrderedMap<Key, T>& other );
inline ~QOrderedMap() { }
//QOrderedMap<Key, T>& operator= ( const QOrderedMap<Key, T>& other );
#ifdef Q_COMPILER_RVALUE_REFS
inline QOrderedMap ( QOrderedMap<Key, T>&& other ) Q_DECL_NOTHROW
: d ( other.d ) {
other.d = 0;
}
inline QOrderedMap<Key, T>& operator= ( QOrderedMap<Key, T>&& other ) Q_DECL_NOTHROW
{ QOrderedMap moved ( std::move ( other ) ); swap ( moved ); return *this; }
#endif
inline void swap ( QOrderedMap<Key, T>& other ) Q_DECL_NOTHROW { qSwap ( _d, other._d ); }
//explicit QOrderedMap ( const typename std::map<Key, T>& other );
//std::map<Key, T> toStdMap() const;
//bool operator== ( const QOrderedMap<Key, T>& other ) const;
inline bool operator!= ( const QOrderedMap<Key, T>& other ) const { return ! ( *this == other ); }
inline int size() const { return d->size(); }
inline bool isEmpty() const { return d->size() == 0; }
inline void detach() { if ( d->isDetached() ) detach_helper(); }
inline bool isDetached() const { return !d->isDetached(); }
#if !defined(QT_NO_UNSHARABLE_CONTAINERS)
inline void setSharable ( bool sharable ) {
if ( sharable == d->isDetached() )
return;
if ( !sharable )
detach();
// Don't call on shared_null
d->setSharable ( sharable );
}
#endif
inline bool isSharedWith ( const QOrderedMap<Key, T>& other ) const { return d == other.d; }
//void clear();
//int remove ( const Key& key );
//T take ( const Key& key );
//bool contains ( const Key& key ) const;
//const Key key ( const T& value, const Key& defaultKey = Key() ) const;
//const T value ( const Key& key, const T& defaultValue = T() ) const;
//T& operator[] ( const Key& key );
//const T operator[] ( const Key& key ) const;
//QList<Key> uniqueKeys() const;
//QList<Key> keys() const;
//QList<Key> keys ( const T& value ) const;
//QList<T> values() const;
//QList<T> values ( const Key& key ) const;
//int count ( const Key& key ) const;
inline const Key& firstKey() const { Q_ASSERT ( !isEmpty() ); return constBegin().key(); }
inline const Key& lastKey() const { Q_ASSERT ( !isEmpty() ); return ( constEnd() - 1 ).key(); }
inline T& first() { Q_ASSERT ( !isEmpty() ); return *begin(); }
inline const T& first() const { Q_ASSERT ( !isEmpty() ); return *constBegin(); }
inline T& last() { Q_ASSERT ( !isEmpty() ); return * ( end() - 1 ); }
inline const T& last() const { Q_ASSERT ( !isEmpty() ); return * ( constEnd() - 1 ); }
class const_iterator;
class iterator
{
friend class const_iterator;
Node* i;
QList<Node>* d;
public:
typedef std::bidirectional_iterator_tag iterator_category;
typedef qptrdiff difference_type;
typedef T value_type;
typedef T* pointer;
typedef T& reference;
inline iterator() : i ( Q_NULLPTR ), d ( Q_NULLPTR ) { }
inline iterator ( Node* an, QList<Node>* ad ) : i ( an ), d ( ad ) {}
inline const Key& key() const { return i->first; }
inline T& value() const { return i->second; }
inline T& operator*() const { return i->second; }
inline T* operator->() const { return &i->second; }
inline bool operator== ( const iterator& o ) const { return i == o.i; }
inline bool operator!= ( const iterator& o ) const { return i != o.i; }
inline iterator& operator++() {
for ( typename QList<Node>::Iterator itor = d->begin();
itor != d->end(); itor++ ) {
Node* n = &*itor;
if ( n == i ) {
i = &* ( ++itor );
break;
}
}
return *this;
}
inline iterator operator++ ( int ) {
iterator r = *this;
for ( typename QList<Node>::Iterator itor = d->begin();
itor != d->end(); itor++ ) {
Node* n = &*itor;
if ( n == i ) {
i = &* ( ++itor );
break;
}
}
return r;
}
inline iterator& operator--() {
for ( typename QList<Node>::ConstIterator itor = d->constBegin();
itor <= d->constEnd(); itor++ ) {
Node* n = &*itor;
if ( n == i ) {
i = &* ( --itor );
break;
}
}
return *this;
}
inline iterator operator-- ( int ) {
iterator r = *this;
for ( typename QList<Node>::ConstIterator itor = d->constBegin();
itor <= d->constEnd(); itor++ ) {
Node* n = &*itor;
if ( n == i ) {
i = &* ( --itor );
break;
}
}
return r;
}
inline iterator operator+ ( int j ) const
{ iterator r = *this; if ( j > 0 ) while ( j-- ) ++r; else while ( j++ ) --r; return r; }
inline iterator operator- ( int j ) const { return operator+ ( -j ); }
inline iterator& operator+= ( int j ) { return *this = *this + j; }
inline iterator& operator-= ( int j ) { return *this = *this - j; }
#ifndef QT_STRICT_ITERATORS
public:
inline bool operator== ( const const_iterator& o ) const
{ return i == o.i; }
inline bool operator!= ( const const_iterator& o ) const
{ return i != o.i; }
#endif
friend class QOrderedMap<Key, T>;
};
friend class iterator;
class const_iterator
{
friend class iterator;
const QList<Node>* d;
const Node* i;
public:
typedef std::bidirectional_iterator_tag iterator_category;
typedef qptrdiff difference_type;
typedef T value_type;
typedef const T* pointer;
typedef const T& reference;
Q_DECL_CONSTEXPR inline const_iterator() : i ( Q_NULLPTR ), d ( Q_NULLPTR ) { }
inline const_iterator ( const Node* node, const QList<Node>* ad ) : i ( node ), d ( ad ) { }
#ifdef QT_STRICT_ITERATORS
explicit inline const_iterator ( const iterator& o )
#else
inline const_iterator ( const iterator& o )
#endif
{ i = o.i; d = o.d; }
inline const Key& key() const { return i->first; }
inline const T& value() const { return i->second; }
inline const T& operator*() const { return i->second; }
inline const T* operator->() const { return &i->second; }
Q_DECL_CONSTEXPR inline bool operator== ( const const_iterator& o ) const { return i == o.i; }
Q_DECL_CONSTEXPR inline bool operator!= ( const const_iterator& o ) const { return i != o.i; }
inline const_iterator& operator++() {
for ( typename QList<Node>::ConstIterator itor = d->constBegin();
itor != d->constEnd(); itor++ ) {
const Node* n = &*itor;
if ( n == i ) {
i = &* ( ++itor );
break;
}
}
return *this;
}
inline const_iterator operator++ ( int ) {
const_iterator r = *this;
for ( typename QList<Node>::ConstIterator itor = d->constBegin();
itor != d->constEnd(); itor++ ) {
const Node* n = &*itor;
if ( i == n ) {
i = &* ( ++itor );
break;
}
}
return r;
}
inline const_iterator& operator--() {
for ( typename QList<Node>::ConstIterator itor = d->constBegin();
itor <= d->constEnd(); itor++ ) {
const Node* n = &*itor;
if ( i == n ) {
i = &* ( --itor );
break;
}
}
return *this;
}
inline const_iterator operator-- ( int ) {
const_iterator r = *this;
for ( typename QList<Node>::ConstIterator itor = d->constBegin();
itor <= d->constEnd(); itor++ ) {
const Node* n = &*itor;
if ( i == n ) {
i = &* ( --itor );
break;
}
}
return r;
}
inline const_iterator operator+ ( int j ) const
{ const_iterator r = *this; if ( j > 0 ) while ( j-- ) ++r; else while ( j++ ) --r; return r; }
inline const_iterator operator- ( int j ) const { return operator+ ( -j ); }
inline const_iterator& operator+= ( int j ) { return *this = *this + j; }
inline const_iterator& operator-= ( int j ) { return *this = *this - j; }
#ifdef QT_STRICT_ITERATORS
private:
inline bool operator== ( const iterator& o ) const { return operator== ( const_iterator ( o ) ); }
inline bool operator!= ( const iterator& o ) const { return operator!= ( const_iterator ( o ) ); }
#endif
friend class QOrderedMap<Key, T>;
};
friend class const_iterator;
//need some test
class key_iterator
{
const_iterator i;
public:
typedef typename const_iterator::iterator_category iterator_category;
typedef typename const_iterator::difference_type difference_type;
typedef Key value_type;
typedef const Key* pointer;
typedef const Key& reference;
key_iterator() = default;
explicit key_iterator ( const_iterator o ) : i ( o ) { }
const Key& operator*() const { return i.key(); }
const Key* operator->() const { return &i.key(); }
bool operator== ( key_iterator o ) const { return i == o.i; }
bool operator!= ( key_iterator o ) const { return i != o.i; }
inline key_iterator& operator++() { ++i; return *this; }
inline key_iterator operator++ ( int ) { return key_iterator ( i++ );}
inline key_iterator& operator--() { --i; return *this; }
inline key_iterator operator-- ( int ) { return key_iterator ( i-- ); }
const_iterator base() const { return i; }
};
// STL style
inline iterator begin() {
detach();
typename QList<Node>::Iterator itor = d->begin();
Node* n = &*itor;
return iterator ( n, d );
}
inline const_iterator begin() const {
typename QList<Node>::Iterator itor = d->begin();
const Node* n = &*itor;
return const_iterator ( n, d );
}
inline const_iterator constBegin() const {
typename QList<Node>::ConstIterator itor = d->constBegin();
const Node* n = &*itor;
return const_iterator ( n, d );
}
inline const_iterator cbegin() const { return const_iterator ( d->begin() ); }
inline iterator end() {
detach();
typename QList<Node>::Iterator itor = d->end();
Node* n = &*itor;
return iterator ( n, d );
}
inline const_iterator end() const {
typename QList<Node>::Iterator itor = d->end();
const Node* n = &*itor;
return const_iterator ( n, d );
}
inline const_iterator constEnd() const {
typename QList<Node>::ConstIterator itor = d->constEnd();
const Node* n = &*itor;
return const_iterator ( n, d );
}
inline const_iterator cend() const { return const_iterator ( d->end() ); }
inline key_iterator keyBegin() const { return key_iterator ( begin() ); }
inline key_iterator keyEnd() const { return key_iterator ( end() ); }
//iterator erase ( iterator it );
// 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 );
//iterator insert ( const_iterator pos, const Key& key, const T& value );
//iterator insertMulti ( const Key& key, const T& value );
//iterator insertMulti ( const_iterator pos, const Key& akey, const T& avalue );
//QOrderedMap<Key, T>& unite ( const QOrderedMap<Key, T>& other );
// STL compatibility
typedef Key key_type;
typedef T mapped_type;
typedef qptrdiff difference_type;
typedef int size_type;
inline bool empty() const { return isEmpty(); }
//QPair<iterator, iterator> equal_range ( const Key& akey );
//QPair<const_iterator, const_iterator> equal_range ( const Key& akey ) const;
#ifdef Q_MAP_DEBUG
//void dump() const;
#endif
private:
//void detach_helper();
bool isValidIterator ( const const_iterator& ci ) const {
#if defined(QT_DEBUG) && !defined(Q_MAP_NO_ITERATOR_DEBUG)
typename QList<Node>::ConstIterator n = ci;
return n >= d->begin() && n <= d->end();
#else
Q_UNUSED ( ci );
return true;
#endif
}
public:
//template <class Key, class T>
inline QOrderedMap ( const QOrderedMap<Key, T>& other )
{
_d = other._d;
d = &_d;
}
//template <class Key, class T>
Q_INLINE_TEMPLATE QOrderedMap<Key, T>& operator= ( const QOrderedMap<Key, T>& other )
{
if ( _d != other._d )
{
QOrderedMap<Key, T> tmp ( other );
tmp.swap ( *this );
}
return *this;
}
//template <class Key, class T>
Q_INLINE_TEMPLATE void clear()
{
*this = QOrderedMap<Key, T>();
}
QT_WARNING_PUSH
QT_WARNING_DISABLE_CLANG ( "-Wreturn-stack-address" )
//template <class Key, class T>
Q_INLINE_TEMPLATE const T value ( const Key& akey, const T& adefaultValue = T() ) const
{
for ( typename QList<Node>::Iterator itor = d->begin();
itor != d->end(); itor++ )
{
Node* n = &*itor;
if ( n->first == akey )
{
return n->second;
}
}
return adefaultValue;
}
QT_WARNING_POP
//template <class Key, class T>
Q_INLINE_TEMPLATE const T operator[] ( const Key& akey ) const
{
return value ( akey );
}
//template <class Key, class T>
Q_INLINE_TEMPLATE T& operator[] ( const Key& akey )
{
detach();
for ( typename QList<Node>::Iterator itor = d->begin();
itor != d->end(); itor++ )
{
Node* n = &*itor;
if ( n->first == akey )
{
return n->second;
}
}
return *insert ( akey, T() );
}
//template <class Key, class T>
Q_INLINE_TEMPLATE int count ( const Key& akey ) const
{
int cnt = 0;
for ( typename QList<Node>::Iterator itor = d->begin();
itor != d->end(); itor++ )
{
Node* n = &*itor;
if ( n->first == akey )
cnt++;
}
return cnt;
}
//template <class Key, class T>
bool contains ( const Key& akey ) const
{
for ( typename QList<Node>::Iterator itor = d->begin();
itor != d->end(); itor++ )
{
Node* n = &*itor;
if ( n->first == akey )
return true;
}
return false;
}
//template <class Key, class T>
Q_INLINE_TEMPLATE typename QOrderedMap<Key, T>::iterator insert ( const Key& akey,
const T& avalue )
{
detach();
Node n;
n.first = akey;
n.second = avalue;
for ( int i = 0 ; i < d->size(); i++ )
{
Node* n_now = &d->operator [] ( i );
if ( n_now->first == akey )
{
*n_now = n;
return iterator ( n_now, d );
}
}
d->push_back ( n );
Node* z = &d->last();
return iterator ( z, d );
}
//need more test
//template <class Key, class T>
typename QOrderedMap<Key, T>::iterator insert ( const_iterator pos, const Key& akey,
const T& avalue )
{
detach();
Node n;
n.first = akey;
n.second = avalue;
for ( int i = 0 ; i < d->size(); i++ )
{
Node* n_now = &d->operator [] ( i );
if ( n_now->first == akey )
{
*n_now = n;
return iterator ( n_now, d );
}
}
for ( typename QList<Node>::Iterator itor = d->begin();
itor != d->end(); itor++ )
{
Node* n_before = itor;
const_iterator p ( n_before, d );
if ( p == pos )
{
typename QList<Node>::Iterator i = d->insert ( itor, n );
Node* cur = &*i;
return iterator ( cur, d );
}
}
d->push_back ( n );
Node* z = &d->last();
return iterator ( z, d );
}
//template <class Key, class T>
Q_INLINE_TEMPLATE typename QOrderedMap<Key, T>::iterator insertMulti ( const Key& akey,
const T& avalue )
{
detach();
#if 0
Node n;
n.first = akey;
n.second = avalue;
d->push_back ( n );
Node* z = d->last();
return iterator ( z );
#endif
return insert ( akey, avalue );
}
//need more test
//template <class Key, class T>
typename QOrderedMap<Key, T>::iterator insertMulti ( const_iterator pos, const Key& akey,
const T& avalue )
{
Node n;
n.first = akey;
n.second = avalue;
for ( typename QList<Node>::Iterator itor = d->begin();
itor != d->end(); itor++ )
{
Node* n_before = &*itor;
const_iterator p ( n_before );
if ( p == pos )
{
typename QList<Node>::Iterator i = d->insert ( itor, n );
Node* n_now = &*i;
return iterator ( n_now, d );
}
}
d->push_back ( n );
Node* n_now = &d->last();
return iterator ( n_now, d );
}
//template <class Key, class T>
Q_INLINE_TEMPLATE typename QOrderedMap<Key, T>::const_iterator constFind (
const Key& akey ) const
{
for ( typename QList<Node>::ConstIterator itor = d->constBegin();
itor != d->constEnd(); itor++ )
{
Node* n = &*itor;
if ( n->first == akey )
{
return const_iterator ( n, d );
}
}
return constEnd();
}
//template <class Key, class T>
Q_INLINE_TEMPLATE typename QOrderedMap<Key, T>::const_iterator find ( const Key& akey ) const
{
return constFind ( akey );
}
//template <class Key, class T>
Q_INLINE_TEMPLATE typename QOrderedMap<Key, T>::iterator find ( const Key& akey )
{
detach();
for ( typename QList<Node>::Iterator itor = d->begin();
itor != d->end(); itor++ )
{
Node* n = &*itor;
if ( n->first == akey )
{
return iterator ( n, d );
}
}
return end();
}
//need
//template <class Key, class T>
Q_INLINE_TEMPLATE QOrderedMap<Key, T>& unite ( const QOrderedMap<Key, T>& other )
{
QOrderedMap<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;
}
//need
//template <class Key, class T>
QPair<typename QOrderedMap<Key, T>::iterator, typename QOrderedMap<Key, T>::iterator>
equal_range ( const Key& akey )
{
detach();
Node* firstNode, *lastNode;
for ( typename QList<Node>::Iterator itor = d->begin();
itor != d->end(); itor++ )
{
Node* n = &*itor;
if ( n->first == akey )
{
firstNode = n;
break;
}
}
if ( iterator ( firstNode, d ) == end() )
return QPair<iterator, iterator> ( end(), end() );
for ( typename QList<Node>::Iterator itor = d->end() - 1;
itor >= d->begin(); itor-- )
{
Node* n = &*itor;
if ( n->first == akey )
{
lastNode = n;
break;
}
}
//d->nodeRange ( akey, &firstNode, &lastNode );
return QPair<iterator, iterator> ( iterator ( firstNode, d ), iterator ( lastNode, d ) );
}
//template <class Key, class T>
QPair<typename QOrderedMap<Key, T>::const_iterator, typename QOrderedMap<Key, T>::const_iterator>
equal_range ( const Key& akey ) const
{
Node* firstNode, *lastNode;
for ( typename QList<Node>::ConstIterator itor = d->constBegin();
itor != d->constEnd(); itor++ )
{
Node* n = &*itor;
if ( n->first == akey )
{
firstNode = n;
break;
}
}
if ( iterator ( firstNode, d ) == constEnd() )
return qMakePair ( constEnd(), constEnd() );
for ( typename QList<Node>::Iterator itor = d->constEnd() - 1;
itor >= d->constBegin(); itor-- )
{
Node* n = &*itor;
if ( n->first == akey )
{
lastNode = n;
break;
}
}
//d->nodeRange ( akey, &firstNode, &lastNode );
return qMakePair ( const_iterator ( firstNode, d ), const_iterator ( lastNode, d ) );
}
#ifdef Q_MAP_DEBUG
//template <class Key, class T>
void dump() const
{
for ( typename QList<Node>::ConstIterator itor = d->constBegin();
itor != d->constEnd(); itor++ )
{
const Node* n = &*itor;
qDebug() << n->first << n->second;
}
}
#endif
//找到key,再处理,不能直接处理list。
//template <class Key, class T>
Q_OUTOFLINE_TEMPLATE int remove ( const Key& akey )
{
detach();
typename QList<Node>::Iterator p = d->end();
for ( typename QList<Node>::Iterator itor = d->begin();
itor != d->end(); itor++ )
{
Node* n = &*itor;
if ( n->first == akey )
{
p = itor;
break;
}
}
Node* n = &*p;
int cnt = d->removeAll ( *n );
return cnt;
}
//template <class Key, class T>
Q_OUTOFLINE_TEMPLATE T take ( const Key& akey )
{
detach();
typename QList<Node>::Iterator p = d->end();
for ( typename QList<Node>::Iterator itor = d->begin();
itor != d->end(); itor++ )
{
Node* n = &*itor;
if ( n->first == akey )
{
p = itor;
break;
}
}
if ( p == d->end() )
return T();
Node* n = &*p;
T v = n->second;
d->removeAll ( *n );
return v;
}
//template <class Key, class T>
Q_OUTOFLINE_TEMPLATE typename QOrderedMap<Key, T>::iterator erase ( iterator it )
{
if ( it == iterator ( d->end(), d ) )
return it;
Node* n = &*it;
++it;
d->removeAll ( n );
return it;
}
private:
//template <class Key, class T>
Q_OUTOFLINE_TEMPLATE void detach_helper()
{
d->detach();
}
public:
//template <class Key, class T>
Q_OUTOFLINE_TEMPLATE QList<Key> uniqueKeys() const
{
QList<Key> res;
for ( typename QList<Node>::Iterator itor = d->begin();
itor != d->end(); itor++ )
{
Node* node = &*itor;
const Key& key = node->first;
if ( res.contains ( key ) )
continue;
res.push_back ( key );
}
return res;
}
//template <class Key, class T>
Q_OUTOFLINE_TEMPLATE QList<Key> keys() const
{
QList<Key> res;
const_iterator i = begin();
while ( i != end() )
{
res.push_back ( i.key() );
++i;
}
return res;
}
//template <class Key, class T>
Q_OUTOFLINE_TEMPLATE QList<Key> keys ( const T& avalue ) const
{
QList<Key> res;
const_iterator i = begin();
while ( i != end() )
{
if ( i.value() == avalue )
res.push_back ( i.key() );
++i;
}
return res;
}
//template <class Key, class T>
Q_OUTOFLINE_TEMPLATE const Key key ( const T& avalue, const Key& defaultKey = Key() ) 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> values() const
{
QList<T> res;
const_iterator i = begin();
while ( i != end() )
{
res.push_back ( i.value() );
++i;
}
return res;
}
//template <class Key, class T>
Q_OUTOFLINE_TEMPLATE QList<T> values ( const Key& akey ) const
{
QList<T> res;
const_iterator i = begin();
while ( i != end() )
{
if ( i.key() == akey )
res.push_back ( i.value() );
++i;
}
return res;
}
//template <class Key, class T>
Q_OUTOFLINE_TEMPLATE bool operator== ( const QOrderedMap<Key, T>& other ) const
{
if ( size() != other.size() )
return false;
if ( _d != other._d )
return false;
return true;
}
//template <class Key, class T>
Q_OUTOFLINE_TEMPLATE QOrderedMap ( const typename std::map<Key, T>& other )
{
d->clear();
typename std::map<Key, T>::const_iterator it = other.begin();
while ( it != other.end() )
{
Node node;
node.first = ( *it ).first;
node.second = ( *it ).second;
d->push_back ( node );
++it;
}
}
//template <class Key, class T>
Q_OUTOFLINE_TEMPLATE std::map<Key, T> toStdMap() const
{
std::map<Key, T> amap;
const_iterator it = begin();
while ( it != end() )
{
amap.insert ( amap.end(), std::pair<Key, T> ( it.key(), it.value() ) );
++it;
}
return amap;
}
};
template <class Key, class T>
class QQTSHARED_EXPORT QOrderedMultiMap : public QOrderedMap<Key, T>
{
public:
QOrderedMultiMap() Q_DECL_NOTHROW {}
#ifdef Q_COMPILER_INITIALIZER_LISTS
inline QOrderedMultiMap ( std::initializer_list<std::pair<Key, T> > list ) {
for ( typename std::initializer_list<std::pair<Key, T> >::const_iterator it = list.begin(); it != list.end(); ++it )
insert ( it->first, it->second );
}
#endif
QOrderedMultiMap ( const QOrderedMap<Key, T>& other ) : QOrderedMap<Key, T> ( other ) {}
#ifdef Q_COMPILER_RVALUE_REFS
QOrderedMultiMap ( QOrderedMap<Key, T>&& other ) Q_DECL_NOTHROW : QOrderedMap<Key, T> ( std::move ( other ) ) {}
#endif
void swap ( QOrderedMultiMap<Key, T>& other ) Q_DECL_NOTHROW { QOrderedMap<Key, T>::swap ( other ); }
inline typename QOrderedMap<Key, T>::iterator replace ( const Key& key, const T& value )
{ return QOrderedMap<Key, T>::insert ( key, value ); }
inline typename QOrderedMap<Key, T>::iterator insert ( const Key& key, const T& value )
{ return QOrderedMap<Key, T>::insertMulti ( key, value ); }
inline typename QOrderedMap<Key, T>::iterator insert ( typename QOrderedMap<Key, T>::const_iterator pos,
const Key& key,
const T& value )
{ return QOrderedMap<Key, T>::insertMulti ( pos, key, value ); }
inline QOrderedMultiMap& operator+= ( const QOrderedMultiMap& other )
{ this->unite ( other ); return *this; }
inline QOrderedMultiMap operator+ ( const QOrderedMultiMap& other ) const
{ QOrderedMultiMap result = *this; result += other; return result; }
using QOrderedMap<Key, T>::contains;
using QOrderedMap<Key, T>::remove;
using QOrderedMap<Key, T>::count;
using QOrderedMap<Key, T>::find;
using QOrderedMap<Key, T>::constFind;
//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 QOrderedMap<Key, T>::iterator find ( const Key& key, const T& value ) {
typename QOrderedMap<Key, T>::iterator i ( find ( key ) );
typename QOrderedMap<Key, T>::iterator end ( this->end() );
while ( i != end && !QQtMapLessThanKey<Key> ( key, i.key() ) ) {
if ( i.value() == value )
return i;
++i;
}
return end;
}
typename QOrderedMap<Key, T>::const_iterator find ( const Key& key, const T& value ) const {
typename QOrderedMap<Key, T>::const_iterator i ( constFind ( key ) );
typename QOrderedMap<Key, T>::const_iterator end ( QOrderedMap<Key, T>::constEnd() );
while ( i != end && !QQtMapLessThanKey<Key> ( key, i.key() ) ) {
if ( i.value() == value )
return i;
++i;
}
return end;
}
typename QOrderedMap<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;
public:
//template <class Key, class T>
Q_INLINE_TEMPLATE bool contains ( const Key& key, const T& value ) const
{
return constFind ( key, value ) != QOrderedMap<Key, T>::constEnd();
}
//template <class Key, class T>
Q_INLINE_TEMPLATE int remove ( const Key& key, const T& value )
{
int n = 0;
typename QOrderedMap<Key, T>::iterator i ( find ( key ) );
typename QOrderedMap<Key, T>::iterator end ( QOrderedMap<Key, T>::end() );
while ( i != end && !QQtMapLessThanKey<Key> ( key, i.key() ) )
{
if ( i.value() == value )
{
i = this->erase ( i );
++n;
}
else
{
++i;
}
}
return n;
}
//template <class Key, class T>
Q_INLINE_TEMPLATE int count ( const Key& key, const T& value ) const
{
int n = 0;
typename QOrderedMap<Key, T>::const_iterator i ( constFind ( key ) );
typename QOrderedMap<Key, T>::const_iterator end ( QOrderedMap<Key, T>::constEnd() );
while ( i != end && !QQtMapLessThanKey<Key> ( key, i.key() ) )
{
if ( i.value() == value )
++n;
++i;
}
return n;
}
};
Q_DECLARE_ASSOCIATIVE_ITERATOR ( OrderedMap )
Q_DECLARE_MUTABLE_ASSOCIATIVE_ITERATOR ( OrderedMap )
QT_END_NAMESPACE
#endif // QQTORDEREDMAP_H
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C/C++
1
https://gitee.com/drabel/LibQQt.git
git@gitee.com:drabel/LibQQt.git
drabel
LibQQt
LibQQt
master

搜索帮助