QHash< Key, T > Class Template Reference

QHash< Key, T > Class Template Reference

#include <qcontainerfwd.h>

Class Description

template<class Key, class T>
class QHash< Key, T >

Definition at line 54 of file qcontainerfwd.h.

+ Inheritance diagram for QHash< Key, T >:

Classes

class  const_iterator
 
class  iterator
 

Public Types

typedef iterator Iterator
 
typedef const_iterator ConstIterator
 
typedef T mapped_type
 
typedef Key key_type
 
typedef qptrdiff difference_type
 
typedef int size_type
 

Public Member Functions

 QHash ()
 
 QHash (const QHash< Key, T > &other)
 
 ~QHash ()
 
QHash< Key, T > & operator= (const QHash< Key, T > &other)
 
void swap (QHash< Key, T > &other)
 
bool operator== (const QHash< Key, T > &other) const
 
bool operator!= (const QHash< Key, T > &other) const
 
int size () const
 
bool isEmpty () const
 
int capacity () const
 
void reserve (int size)
 
void squeeze ()
 
void detach ()
 
bool isDetached () const
 
void setSharable (bool sharable)
 
bool isSharedWith (const QHash< Key, T > &other) const
 
void clear ()
 
int remove (const Key &key)
 
take (const Key &key)
 
bool contains (const Key &key) const
 
const Key key (const T &value) const
 
const Key key (const T &value, const Key &defaultKey) const
 
const T value (const Key &key) const
 
const T value (const Key &key, const T &defaultValue) 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
 
iterator begin ()
 
const_iterator begin () const
 
const_iterator constBegin () const
 
iterator end ()
 
const_iterator end () const
 
const_iterator constEnd () const
 
iterator erase (iterator it)
 
int count () const
 
iterator find (const Key &key)
 
const_iterator find (const Key &key) const
 
const_iterator constFind (const Key &key) const
 
iterator insert (const Key &key, const T &value)
 
iterator insertMulti (const Key &key, const T &value)
 
QHash< Key, T > & unite (const QHash< Key, T > &other)
 
bool empty () const
 

Friends

class iterator
 
class const_iterator
 

Member Typedef Documentation

typedef iterator Iterator

Definition at line 473 of file qhash.h.

Definition at line 474 of file qhash.h.

typedef T mapped_type

Definition at line 484 of file qhash.h.

typedef Key key_type

Definition at line 485 of file qhash.h.

typedef qptrdiff difference_type

Definition at line 486 of file qhash.h.

typedef int size_type

Definition at line 487 of file qhash.h.

Constructor & Destructor Documentation

QHash ( )
inline

Definition at line 281 of file qhash.h.

281 : d(&QHashData::shared_null) { d->ref.ref(); }
QHashData * d
Definition: qhash.h:264
QBasicAtomicInt ref
Definition: qhash.h:121
static QHashData shared_null
Definition: qhash.h:151
QHash ( const QHash< Key, T > &  other)
inline

Definition at line 282 of file qhash.h.

282 : d(other.d) { d->ref.ref(); if (!d->sharable) detach(); }
QHashData * d
Definition: qhash.h:264
QBasicAtomicInt ref
Definition: qhash.h:121
uint sharable
Definition: qhash.h:127
void detach()
Definition: qhash.h:303
~QHash ( )
inline

Definition at line 283 of file qhash.h.

283 { if (!d->ref.deref()) freeData(d); }
QHashData * d
Definition: qhash.h:264
QBasicAtomicInt ref
Definition: qhash.h:121

Member Function Documentation

Q_INLINE_TEMPLATE QHash< Key, T > & operator= ( const QHash< Key, T > &  other)

Definition at line 591 of file qhash.h.

592 {
593  if (d != other.d) {
594  QHashData *o = other.d;
595  o->ref.ref();
596  if (!d->ref.deref())
597  freeData(d);
598  d = o;
599  if (!d->sharable)
600  detach_helper();
601  }
602  return *this;
603 }
QHashData * d
Definition: qhash.h:264
QBasicAtomicInt ref
Definition: qhash.h:121
uint sharable
Definition: qhash.h:127
void swap ( QHash< Key, T > &  other)
inline

Definition at line 290 of file qhash.h.

290 { qSwap(d, other.d); }
QHashData * d
Definition: qhash.h:264
Q_INLINE_TEMPLATE void qSwap(QScopedPointer< T, Cleanup > &p1, QScopedPointer< T, Cleanup > &p2)
Q_OUTOFLINE_TEMPLATE bool operator== ( const QHash< Key, T > &  other) const

Definition at line 898 of file qhash.h.

899 {
900  if (size() != other.size())
901  return false;
902  if (d == other.d)
903  return true;
904 
905  const_iterator it = begin();
906 
907  while (it != end()) {
908  const Key &akey = it.key();
909 
910  const_iterator it2 = other.find(akey);
911  do {
912  if (it2 == other.end() || !(it2.key() == akey))
913  return false;
914  if (!QTypeInfo<T>::isDummy && !(it.value() == it2.value()))
915  return false;
916  ++it;
917  ++it2;
918  } while (it != end() && it.key() == akey);
919  }
920  return true;
921 }
QHashData * d
Definition: qhash.h:264
iterator end()
Definition: qhash.h:467
iterator find(const Key &key)
Definition: qhash.h:865
const Key & key() const
Definition: qhash.h:347
friend class const_iterator
Definition: qhash.h:461
int size() const
Definition: qhash.h:295
iterator begin()
Definition: qhash.h:464
bool operator!= ( const QHash< Key, T > &  other) const
inline

Definition at line 293 of file qhash.h.

293 { return !(*this == other); }
int size ( ) const
inline

Definition at line 295 of file qhash.h.

295 { return d->size; }
QHashData * d
Definition: qhash.h:264
int size
Definition: qhash.h:122
bool isEmpty ( ) const
inline

Definition at line 297 of file qhash.h.

297 { return d->size == 0; }
QHashData * d
Definition: qhash.h:264
int size
Definition: qhash.h:122
int capacity ( ) const
inline

Definition at line 299 of file qhash.h.

299 { return d->numBuckets; }
QHashData * d
Definition: qhash.h:264
int numBuckets
Definition: qhash.h:126
Q_INLINE_TEMPLATE void reserve ( int  size)

Definition at line 846 of file qhash.h.

847 {
848  detach();
849  d->rehash(-qMax(asize, 1));
850 }
QHashData * d
Definition: qhash.h:264
void rehash(int hint)
void detach()
Definition: qhash.h:303
void squeeze ( )
inline

Definition at line 301 of file qhash.h.

301 { reserve(1); }
void reserve(int size)
Definition: qhash.h:846
void detach ( )
inline

Definition at line 303 of file qhash.h.

303 { if (d->ref != 1) detach_helper(); }
QHashData * d
Definition: qhash.h:264
QBasicAtomicInt ref
Definition: qhash.h:121
bool isDetached ( ) const
inline

Definition at line 304 of file qhash.h.

304 { return d->ref == 1; }
QHashData * d
Definition: qhash.h:264
QBasicAtomicInt ref
Definition: qhash.h:121
void setSharable ( bool  sharable)
inline

Definition at line 305 of file qhash.h.

305 { if (!sharable) detach(); d->sharable = sharable; }
QHashData * d
Definition: qhash.h:264
uint sharable
Definition: qhash.h:127
void detach()
Definition: qhash.h:303
bool isSharedWith ( const QHash< Key, T > &  other) const
inline

Definition at line 306 of file qhash.h.

306 { return d == other.d; }
QHashData * d
Definition: qhash.h:264
Q_INLINE_TEMPLATE void clear ( )

Definition at line 574 of file qhash.h.

575 {
576  *this = QHash<Key,T>();
577 }
Q_OUTOFLINE_TEMPLATE int remove ( const Key &  key)

Definition at line 784 of file qhash.h.

785 {
786  if (isEmpty()) // prevents detaching shared null
787  return 0;
788  detach();
789 
790  int oldSize = d->size;
791  Node **node = findNode(akey);
792  if (*node != e) {
793  bool deleteNext = true;
794  do {
795  Node *next = (*node)->next;
796  deleteNext = (next != e && next->key == (*node)->key);
797  deleteNode(*node);
798  *node = next;
799  --d->size;
800  } while (deleteNext);
801  d->hasShrunk();
802  }
803  return oldSize - d->size;
804 }
QHashNode< Key, T > * e
Definition: qhash.h:265
bool isEmpty() const
Definition: qhash.h:297
QHashData * d
Definition: qhash.h:264
void hasShrunk()
Definition: qhash.h:170
int size
Definition: qhash.h:122
void detach()
Definition: qhash.h:303
Q_OUTOFLINE_TEMPLATE T take ( const Key &  key)

Definition at line 807 of file qhash.h.

808 {
809  if (isEmpty()) // prevents detaching shared null
810  return T();
811  detach();
812 
813  Node **node = findNode(akey);
814  if (*node != e) {
815  T t = (*node)->value;
816  Node *next = (*node)->next;
817  deleteNode(*node);
818  *node = next;
819  --d->size;
820  d->hasShrunk();
821  return t;
822  }
823  return T();
824 }
QHashNode< Key, T > * e
Definition: qhash.h:265
bool isEmpty() const
Definition: qhash.h:297
QHashData * d
Definition: qhash.h:264
void hasShrunk()
Definition: qhash.h:170
int size
Definition: qhash.h:122
GLdouble GLdouble t
Definition: GLee.h:1181
void detach()
Definition: qhash.h:303
Q_INLINE_TEMPLATE bool contains ( const Key &  key) const

Definition at line 872 of file qhash.h.

873 {
874  return *findNode(akey) != e;
875 }
QHashNode< Key, T > * e
Definition: qhash.h:265
Q_OUTOFLINE_TEMPLATE const Key key ( const T &  value) const

Definition at line 674 of file qhash.h.

675 {
676  return key(avalue, Key());
677 }
const Key key(const T &value) const
Definition: qhash.h:674
Q_OUTOFLINE_TEMPLATE const Key key ( const T &  value,
const Key &  defaultKey 
) const

Definition at line 680 of file qhash.h.

681 {
682  const_iterator i = begin();
683  while (i != end()) {
684  if (i.value() == avalue)
685  return i.key();
686  ++i;
687  }
688 
689  return defaultValue;
690 }
iterator end()
Definition: qhash.h:467
friend class const_iterator
Definition: qhash.h:461
iterator begin()
Definition: qhash.h:464
Q_INLINE_TEMPLATE const T value ( const Key &  key) const

Definition at line 606 of file qhash.h.

607 {
608  Node *node;
609  if (d->size == 0 || (node = *findNode(akey)) == e) {
610  return T();
611  } else {
612  return node->value;
613  }
614 }
QHashNode< Key, T > * e
Definition: qhash.h:265
QHashData * d
Definition: qhash.h:264
int size
Definition: qhash.h:122
Q_INLINE_TEMPLATE const T value ( const Key &  key,
const T &  defaultValue 
) const

Definition at line 617 of file qhash.h.

618 {
619  Node *node;
620  if (d->size == 0 || (node = *findNode(akey)) == e) {
621  return adefaultValue;
622  } else {
623  return node->value;
624  }
625 }
QHashNode< Key, T > * e
Definition: qhash.h:265
QHashData * d
Definition: qhash.h:264
int size
Definition: qhash.h:122
Q_INLINE_TEMPLATE T & operator[] ( const Key &  key)

Definition at line 738 of file qhash.h.

739 {
740  detach();
741 
742  uint h;
743  Node **node = findNode(akey, &h);
744  if (*node == e) {
745  if (d->willGrow())
746  node = findNode(akey, &h);
747  return createNode(h, akey, T(), node)->value;
748  }
749  return (*node)->value;
750 }
bool willGrow()
Definition: qhash.h:160
QHashNode< Key, T > * e
Definition: qhash.h:265
QHashData * d
Definition: qhash.h:264
int int int int int int h
Definition: GLee.h:10534
T value
Definition: qhash.h:221
void detach()
Definition: qhash.h:303
Q_INLINE_TEMPLATE const T operator[] ( const Key &  key) const

Definition at line 732 of file qhash.h.

733 {
734  return value(akey);
735 }
const T value(const Key &key) const
Definition: qhash.h:606
Q_OUTOFLINE_TEMPLATE QList< Key > uniqueKeys ( ) const

Definition at line 628 of file qhash.h.

629 {
630  QList<Key> res;
631  res.reserve(size()); // May be too much, but assume short lifetime
632  const_iterator i = begin();
633  if (i != end()) {
634  for (;;) {
635  const Key &aKey = i.key();
636  res.append(aKey);
637  do {
638  if (++i == end())
639  goto break_out_of_outer_loop;
640  } while (aKey == i.key());
641  }
642  }
643 break_out_of_outer_loop:
644  return res;
645 }
iterator end()
Definition: qhash.h:467
GLuint res
Definition: GLee.h:7185
void reserve(int size)
Definition: qlist.h:496
void append(const T &t)
Definition: qlist.h:507
friend class const_iterator
Definition: qhash.h:461
int size() const
Definition: qhash.h:295
iterator begin()
Definition: qhash.h:464
Q_OUTOFLINE_TEMPLATE QList< Key > keys ( ) const

Definition at line 648 of file qhash.h.

649 {
650  QList<Key> res;
651  res.reserve(size());
652  const_iterator i = begin();
653  while (i != end()) {
654  res.append(i.key());
655  ++i;
656  }
657  return res;
658 }
iterator end()
Definition: qhash.h:467
GLuint res
Definition: GLee.h:7185
void reserve(int size)
Definition: qlist.h:496
void append(const T &t)
Definition: qlist.h:507
friend class const_iterator
Definition: qhash.h:461
int size() const
Definition: qhash.h:295
iterator begin()
Definition: qhash.h:464
Q_OUTOFLINE_TEMPLATE QList< Key > keys ( const T &  value) const

Definition at line 661 of file qhash.h.

662 {
663  QList<Key> res;
664  const_iterator i = begin();
665  while (i != end()) {
666  if (i.value() == avalue)
667  res.append(i.key());
668  ++i;
669  }
670  return res;
671 }
iterator end()
Definition: qhash.h:467
GLuint res
Definition: GLee.h:7185
void append(const T &t)
Definition: qlist.h:507
friend class const_iterator
Definition: qhash.h:461
iterator begin()
Definition: qhash.h:464
Q_OUTOFLINE_TEMPLATE QList< T > values ( ) const

Definition at line 693 of file qhash.h.

694 {
695  QList<T> res;
696  res.reserve(size());
697  const_iterator i = begin();
698  while (i != end()) {
699  res.append(i.value());
700  ++i;
701  }
702  return res;
703 }
iterator end()
Definition: qhash.h:467
GLuint res
Definition: GLee.h:7185
void reserve(int size)
Definition: qlist.h:496
void append(const T &t)
Definition: qlist.h:507
friend class const_iterator
Definition: qhash.h:461
int size() const
Definition: qhash.h:295
iterator begin()
Definition: qhash.h:464
Q_OUTOFLINE_TEMPLATE QList< T > values ( const Key &  key) const

Definition at line 706 of file qhash.h.

707 {
708  QList<T> res;
709  Node *node = *findNode(akey);
710  if (node != e) {
711  do {
712  res.append(node->value);
713  } while ((node = node->next) != e && node->key == akey);
714  }
715  return res;
716 }
QHashNode< Key, T > * e
Definition: qhash.h:265
GLuint res
Definition: GLee.h:7185
void append(const T &t)
Definition: qlist.h:507
Q_OUTOFLINE_TEMPLATE int count ( const Key &  key) const

Definition at line 719 of file qhash.h.

720 {
721  int cnt = 0;
722  Node *node = *findNode(akey);
723  if (node != e) {
724  do {
725  ++cnt;
726  } while ((node = node->next) != e && node->key == akey);
727  }
728  return cnt;
729 }
QHashNode< Key, T > * e
Definition: qhash.h:265
iterator begin ( )
inline

Definition at line 464 of file qhash.h.

464 { detach(); return iterator(d->firstNode()); }
Node * firstNode()
Definition: qhash.h:181
QHashData * d
Definition: qhash.h:264
friend class iterator
Definition: qhash.h:393
void detach()
Definition: qhash.h:303
const_iterator begin ( ) const
inline

Definition at line 465 of file qhash.h.

465 { return const_iterator(d->firstNode()); }
Node * firstNode()
Definition: qhash.h:181
QHashData * d
Definition: qhash.h:264
friend class const_iterator
Definition: qhash.h:461
const_iterator constBegin ( ) const
inline

Definition at line 466 of file qhash.h.

466 { return const_iterator(d->firstNode()); }
Node * firstNode()
Definition: qhash.h:181
QHashData * d
Definition: qhash.h:264
friend class const_iterator
Definition: qhash.h:461
iterator end ( )
inline

Definition at line 467 of file qhash.h.

467 { detach(); return iterator(e); }
QHashNode< Key, T > * e
Definition: qhash.h:265
friend class iterator
Definition: qhash.h:393
void detach()
Definition: qhash.h:303
const_iterator end ( ) const
inline

Definition at line 468 of file qhash.h.

468 { return const_iterator(e); }
QHashNode< Key, T > * e
Definition: qhash.h:265
friend class const_iterator
Definition: qhash.h:461
const_iterator constEnd ( ) const
inline

Definition at line 469 of file qhash.h.

469 { return const_iterator(e); }
QHashNode< Key, T > * e
Definition: qhash.h:265
friend class const_iterator
Definition: qhash.h:461
Q_OUTOFLINE_TEMPLATE QHash< Key, T >::iterator erase ( iterator  it)

Definition at line 827 of file qhash.h.

828 {
829  if (it == iterator(e))
830  return it;
831 
832  iterator ret = it;
833  ++ret;
834 
835  Node *node = it;
836  Node **node_ptr = reinterpret_cast<Node **>(&d->buckets[node->h % d->numBuckets]);
837  while (*node_ptr != node)
838  node_ptr = &(*node_ptr)->next;
839  *node_ptr = node->next;
840  deleteNode(node);
841  --d->size;
842  return ret;
843 }
QHashNode< Key, T > * e
Definition: qhash.h:265
QHashData * d
Definition: qhash.h:264
Node ** buckets
Definition: qhash.h:120
friend class iterator
Definition: qhash.h:393
int size
Definition: qhash.h:122
int numBuckets
Definition: qhash.h:126
int count ( ) const
inline

Definition at line 475 of file qhash.h.

475 { return d->size; }
QHashData * d
Definition: qhash.h:264
int size
Definition: qhash.h:122
Q_INLINE_TEMPLATE QHash< Key, T >::iterator find ( const Key &  key)

Definition at line 865 of file qhash.h.

866 {
867  detach();
868  return iterator(*findNode(akey));
869 }
friend class iterator
Definition: qhash.h:393
void detach()
Definition: qhash.h:303
Q_INLINE_TEMPLATE QHash< Key, T >::const_iterator find ( const Key &  key) const

Definition at line 853 of file qhash.h.

854 {
855  return const_iterator(*findNode(akey));
856 }
friend class const_iterator
Definition: qhash.h:461
Q_INLINE_TEMPLATE QHash< Key, T >::const_iterator constFind ( const Key &  key) const

Definition at line 859 of file qhash.h.

860 {
861  return const_iterator(*findNode(akey));
862 }
friend class const_iterator
Definition: qhash.h:461
Q_INLINE_TEMPLATE QHash< Key, T >::iterator insert ( const Key &  key,
const T &  value 
)

Definition at line 753 of file qhash.h.

755 {
756  detach();
757 
758  uint h;
759  Node **node = findNode(akey, &h);
760  if (*node == e) {
761  if (d->willGrow())
762  node = findNode(akey, &h);
763  return iterator(createNode(h, akey, avalue, node));
764  }
765 
766  if (!QTypeInfo<T>::isDummy)
767  (*node)->value = avalue;
768  return iterator(*node);
769 }
bool willGrow()
Definition: qhash.h:160
QHashNode< Key, T > * e
Definition: qhash.h:265
QHashData * d
Definition: qhash.h:264
int int int int int int h
Definition: GLee.h:10534
friend class iterator
Definition: qhash.h:393
void detach()
Definition: qhash.h:303
Q_INLINE_TEMPLATE QHash< Key, T >::iterator insertMulti ( const Key &  key,
const T &  value 
)

Definition at line 772 of file qhash.h.

774 {
775  detach();
776  d->willGrow();
777 
778  uint h;
779  Node **nextNode = findNode(akey, &h);
780  return iterator(createNode(h, akey, avalue, nextNode));
781 }
bool willGrow()
Definition: qhash.h:160
QHashData * d
Definition: qhash.h:264
int int int int int int h
Definition: GLee.h:10534
friend class iterator
Definition: qhash.h:393
void detach()
Definition: qhash.h:303
Q_INLINE_TEMPLATE QHash< Key, T > & unite ( const QHash< Key, T > &  other)

Definition at line 556 of file qhash.h.

557 {
558  QHash<Key, T> copy(other);
559  const_iterator it = copy.constEnd();
560  while (it != copy.constBegin()) {
561  --it;
562  insertMulti(it.key(), it.value());
563  }
564  return *this;
565 }
friend class const_iterator
Definition: qhash.h:461
iterator insertMulti(const Key &key, const T &value)
Definition: qhash.h:772
bool empty ( ) const
inline

Definition at line 489 of file qhash.h.

489 { return isEmpty(); }
bool isEmpty() const
Definition: qhash.h:297

Friends And Related Function Documentation

friend class iterator
friend

Definition at line 393 of file qhash.h.

friend class const_iterator
friend

Definition at line 461 of file qhash.h.

Member Data Documentation

Definition at line 264 of file qhash.h.

QHashNode<Key, T>* e

Definition at line 265 of file qhash.h.


The documentation for this class was generated from the following files: