- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files
- Directories
- Examples

- Class List
- Class Hierarchy
- Class Members

Classes | Public Types | Public Member Functions | Friends

# QHash< Key, T > Class Template Reference

Search for all occurrences

## Detailed Description

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

Definition at line 259 of file qhash.h.

`#include <qhash.h>`

Inheritance diagram for QHash< Key, T >:
[legend]

List of all members.

## 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)
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)
T | 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

Search for all occurrences

Definition at line 469 of file qhash.h.

typedef const_iterator ConstIterator

Search for all occurrences

Definition at line 470 of file qhash.h.

typedef T mapped_type

Search for all occurrences

Definition at line 480 of file qhash.h.

typedef Key key_type

Search for all occurrences

Definition at line 481 of file qhash.h.

typedef qptrdiff difference_type

Search for all occurrences

Definition at line 482 of file qhash.h.

typedef int size_type

Search for all occurrences

Definition at line 483 of file qhash.h.

## Constructor & Destructor Documentation

QHash | ( |  | ) | `[inline]`

Definition at line 282 of file qhash.h.

    : d(&QHashData::shared_null) { d->ref.ref(); }

QHash | ( | const QHash< Key, T > & | other | ) | `[inline]`

Definition at line 283 of file qhash.h.

    : d(other.d) { d->ref.ref(); if (!d->sharable) detach(); }

~QHash | ( |  | ) | `[inline]`

Definition at line 284 of file qhash.h.

    { if (!d->ref.deref()) freeData(d); }

## Member Function Documentation

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

Definition at line 589 of file qhash.h.

    {
        if (d != other.d) {
            QHashData *o = other.d;
            o->ref.ref();
            if (!d->ref.deref())
                freeData(d);
            d = o;
            if (!d->sharable)
                detach_helper();
        }
        return *this;
    }

Q_OUTOFLINE_TEMPLATE bool operator== | ( | const QHash< Key, T > & | other | ) | const

Definition at line 896 of file qhash.h.

    {
        if (size() != other.size())
            return false;
        if (d == other.d)
            return true;

        const_iterator it = begin();

        while (it != end()) {
            const Key &akey = it.key();

            const_iterator it2 = other.find(akey);
            do {
                if (it2 == other.end() || !(it2.key() == akey))
                    return false;
                if (!QTypeInfo<T>::isDummy && !(it.value() == it2.value()))
                    return false;
                ++it;
                ++it2;
            } while (it != end() && it.key() == akey);
        }
        return true;
    }

bool operator!= | ( | const QHash< Key, T > & | other | ) | const `[inline]`

Definition at line 289 of file qhash.h.

    { return !(*this == other); }

int size | ( |  | ) | const `[inline]`

Search for all occurrences

Definition at line 291 of file qhash.h.

    { return d->size; }

bool isEmpty | ( |  | ) | const `[inline]`

Search for all occurrences

Definition at line 293 of file qhash.h.

    { return d->size == 0; }

int capacity | ( |  | ) | const `[inline]`

Search for all occurrences

Definition at line 295 of file qhash.h.

    { return d->numBuckets; }

Q_INLINE_TEMPLATE void reserve | ( | int | size | ) |

Search for all occurrences

Definition at line 844 of file qhash.h.

    {
        detach();
        d->rehash(-qMax(asize, 1));
    }

void squeeze | ( |  | ) | `[inline]`

Search for all occurrences

Definition at line 297 of file qhash.h.

    { reserve(1); }

void detach | ( |  | ) | `[inline]`

Search for all occurrences

Definition at line 299 of file qhash.h.

    { if (d->ref != 1) detach_helper(); }

bool isDetached | ( |  | ) | const `[inline]`

Search for all occurrences

Definition at line 300 of file qhash.h.

    { return d->ref == 1; }

void setSharable | ( | bool | sharable | ) | `[inline]`

Search for all occurrences

Definition at line 301 of file qhash.h.

    { if (!sharable) detach(); d->sharable = sharable; }

bool isSharedWith | ( | const QHash< Key, T > & | other | ) | const `[inline]`

Search for all occurrences

Definition at line 302 of file qhash.h.

    { return d == other.d; }

Q_INLINE_TEMPLATE void clear | ( |  | ) |

Search for all occurrences

Definition at line 572 of file qhash.h.

    {
        *this = QHash<Key,T>();
    }

Q_OUTOFLINE_TEMPLATE int remove | ( | const Key & | key | ) |

Search for all occurrences

Definition at line 782 of file qhash.h.

    {
        if (isEmpty()) // prevents detaching shared null
            return 0;
        detach();

        int oldSize = d->size;
        Node **node = findNode(akey);
        if (*node != e) {
            bool deleteNext = true;
            do {
                Node *next = (*node)->next;
                deleteNext = (next != e && next->key == (*node)->key);
                deleteNode(*node);
                *node = next;
                --d->size;
            } while (deleteNext);
            d->hasShrunk();
        }
        return oldSize - d->size;
    }

Q_OUTOFLINE_TEMPLATE T take | ( | const Key & | key | ) |

Search for all occurrences

Definition at line 805 of file qhash.h.

    {
        if (isEmpty()) // prevents detaching shared null
            return T();
        detach();

        Node **node = findNode(akey);
        if (*node != e) {
            T t = (*node)->value;
            Node *next = (*node)->next;
            deleteNode(*node);
            *node = next;
            --d->size;
            d->hasShrunk();
            return t;
        }
        return T();
    }

Q_INLINE_TEMPLATE bool contains | ( | const Key & | key | ) | const

Search for all occurrences

Definition at line 870 of file qhash.h.

    {
        return *findNode(akey) != e;
    }

Q_OUTOFLINE_TEMPLATE const Key key | ( | const T & | value | ) | const

Search for all occurrences

Definition at line 672 of file qhash.h.

    {
        return key(avalue, Key());
    }

Q_OUTOFLINE_TEMPLATE const Key key | ( | const T & | value,
|  | const Key & | defaultKey
| ) |  | const

Search for all occurrences

Definition at line 678 of file qhash.h.

    {
        const_iterator i = begin();
        while (i != end()) {
            if (i.value() == avalue)
                return i.key();
            ++i;
        }

        return defaultValue;
    }

Q_INLINE_TEMPLATE const T value | ( | const Key & | key | ) | const

Search for all occurrences

Definition at line 604 of file qhash.h.

    {
        Node *node;
        if (d->size == 0 || (node = *findNode(akey)) == e) {
            return T();
        } else {
            return node->value;
        }
    }

Q_INLINE_TEMPLATE const T value | ( | const Key & | key,
|  | const T & | defaultValue
| ) |  | const

Search for all occurrences

Definition at line 615 of file qhash.h.

    {
        Node *node;
        if (d->size == 0 || (node = *findNode(akey)) == e) {
            return adefaultValue;
        } else {
            return node->value;
        }
    }

Q_INLINE_TEMPLATE T & operator[] | ( | const Key & | key | ) |

Definition at line 736 of file qhash.h.

    {
        detach();

        uint h;
        Node **node = findNode(akey, &h);
        if (*node == e) {
            if (d->willGrow())
                node = findNode(akey, &h);
            return createNode(h, akey, T(), node)->value;
        }
        return (*node)->value;
    }

Q_INLINE_TEMPLATE const T operator[] | ( | const Key & | key | ) | const

Definition at line 730 of file qhash.h.

    {
        return value(akey);
    }

Q_OUTOFLINE_TEMPLATE QList< Key > uniqueKeys | ( |  | ) | const

Search for all occurrences

Definition at line 626 of file qhash.h.

    {
        QList<Key> res;
        res.reserve(size()); // May be too much, but assume short lifetime
        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());
            }
        }
    break_out_of_outer_loop:
        return res;
    }

Q_OUTOFLINE_TEMPLATE QList< Key > keys | ( |  | ) | const

Search for all occurrences

Definition at line 646 of file qhash.h.

    {
        QList<Key> res;
        res.reserve(size());
        const_iterator i = begin();
        while (i != end()) {
            res.append(i.key());
            ++i;
        }
        return res;
    }

Q_OUTOFLINE_TEMPLATE QList< Key > keys | ( | const T & | value | ) | const

Search for all occurrences

Definition at line 659 of file qhash.h.

    {
        QList<Key> res;
        const_iterator i = begin();
        while (i != end()) {
            if (i.value() == avalue)
                res.append(i.key());
            ++i;
        }
        return res;
    }

Q_OUTOFLINE_TEMPLATE QList< T > values | ( |  | ) | const

Search for all occurrences

Definition at line 691 of file qhash.h.

    {
        QList<T> res;
        res.reserve(size());
        const_iterator i = begin();
        while (i != end()) {
            res.append(i.value());
            ++i;
        }
        return res;
    }

Q_OUTOFLINE_TEMPLATE QList< T > values | ( | const Key & | key | ) | const

Search for all occurrences

Definition at line 704 of file qhash.h.

    {
        QList<T> res;
        Node *node = *findNode(akey);
        if (node != e) {
            do {
                res.append(node->value);
            } while ((node = node->next) != e && node->key == akey);
        }
        return res;
    }

Q_OUTOFLINE_TEMPLATE int count | ( | const Key & | key | ) | const

Search for all occurrences

Definition at line 717 of file qhash.h.

    {
        int cnt = 0;
        Node *node = *findNode(akey);
        if (node != e) {
            do {
                ++cnt;
            } while ((node = node->next) != e && node->key == akey);
        }
        return cnt;
    }

iterator begin | ( |  | ) | `[inline]`

Search for all occurrences

Definition at line 460 of file qhash.h.

    { detach(); return iterator(d->firstNode()); }

const_iterator begin | ( |  | ) | const `[inline]`

Search for all occurrences

Definition at line 461 of file qhash.h.

    { return const_iterator(d->firstNode()); }

const_iterator constBegin | ( |  | ) | const `[inline]`

Search for all occurrences

Definition at line 462 of file qhash.h.

    { return const_iterator(d->firstNode()); }

iterator end | ( |  | ) | `[inline]`

Search for all occurrences

Definition at line 463 of file qhash.h.

    { detach(); return iterator(e); }

const_iterator end | ( |  | ) | const `[inline]`

Search for all occurrences

Definition at line 464 of file qhash.h.

    { return const_iterator(e); }

const_iterator constEnd | ( |  | ) | const `[inline]`

Search for all occurrences

Definition at line 465 of file qhash.h.

    { return const_iterator(e); }

Q_OUTOFLINE_TEMPLATE QHash< Key, T >::iterator erase | ( | iterator | it | ) |

Search for all occurrences

Definition at line 825 of file qhash.h.

    {
        if (it == iterator(e))
            return it;

        iterator ret = it;
        ++ret;

        Node *node = it;
        Node **node_ptr = reinterpret_cast<Node **>(&d->buckets[node->h % d->numBuckets]);
        while (*node_ptr != node)
            node_ptr = &(*node_ptr)->next;
        *node_ptr = node->next;
        deleteNode(node);
        --d->size;
        return ret;
    }

int count | ( |  | ) | const `[inline]`

Search for all occurrences

Definition at line 471 of file qhash.h.

    { return d->size; }

Q_INLINE_TEMPLATE QHash< Key, T >::iterator find | ( | const Key & | key | ) |

Search for all occurrences

Definition at line 863 of file qhash.h.

    {
        detach();
        return iterator(*findNode(akey));
    }

Q_INLINE_TEMPLATE QHash< Key, T >::const_iterator find | ( | const Key & | key | ) | const

Search for all occurrences

Definition at line 851 of file qhash.h.

    {
        return const_iterator(*findNode(akey));
    }

Q_INLINE_TEMPLATE QHash< Key, T >::const_iterator constFind | ( | const Key & | key | ) | const

Search for all occurrences

Definition at line 857 of file qhash.h.

    {
        return const_iterator(*findNode(akey));
    }

Q_INLINE_TEMPLATE QHash< Key, T >::iterator insert | ( | const Key & | key,
|  | const T & | value
| ) |  |

Search for all occurrences

Reimplemented in QMultiHash< Key, T >.

Definition at line 751 of file qhash.h.

    {
        detach();

        uint h;
        Node **node = findNode(akey, &h);
        if (*node == e) {
            if (d->willGrow())
                node = findNode(akey, &h);
            return iterator(createNode(h, akey, avalue, node));
        }

        if (!QTypeInfo<T>::isDummy)
            (*node)->value = avalue;
        return iterator(*node);
    }

Q_INLINE_TEMPLATE QHash< Key, T >::iterator insertMulti | ( | const Key & | key,
|  | const T & | value
| ) |  |

Search for all occurrences

Definition at line 770 of file qhash.h.

    {
        detach();
        d->willGrow();

        uint h;
        Node **nextNode = findNode(akey, &h);
        return iterator(createNode(h, akey, avalue, nextNode));
    }

Q_INLINE_TEMPLATE QHash< Key, T > & unite | ( | const QHash< Key, T > & | other | ) |

Search for all occurrences

Definition at line 554 of file qhash.h.

    {
        QHash<Key, T> copy(other);
        const_iterator it = copy.constEnd();
        while (it != copy.constBegin()) {
            --it;
            insertMulti(it.key(), it.value());
        }
        return *this;
    }

bool empty | ( |  | ) | const `[inline]`

Search for all occurrences

Definition at line 485 of file qhash.h.

    { return isEmpty(); }

## Friends And Related Function Documentation

friend class iterator `[friend]`

Search for all occurrences

Definition at line 389 of file qhash.h.

friend class const_iterator `[friend]`

Search for all occurrences

Definition at line 457 of file qhash.h.

## Member Data Documentation

QHashData* d

Search for all occurrences

Definition at line 265 of file qhash.h.

QHashNode<Key, T>* e

Search for all occurrences

Definition at line 266 of file qhash.h.

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

- QtCore/qhash.h