CopperSpice API  1.7.2
QMutableMultiHashIterator< Key, Val, Hash, KeyEqual > Class Template Reference

Java style non-const iterator for QMultiHash. More...

Public Methods

 QMutableMultiHashIterator (QMultiHash< Key, Val, Hash, KeyEqual > &hash)
 
 ~QMutableMultiHashIterator ()
 
bool findNext (const Val &value)
 
bool findPrevious (const Val &value)
 
bool hasNext () const
 
bool hasPrevious () const
 
const Key & key () const
 
Item next ()
 
QMutableMultiHashIterator & operator= (QMultiHash< Key, Val, Hash, KeyEqual > &hash)
 
Item peekNext () const
 
Item peekPrevious () const
 
Item previous ()
 
void remove ()
 
void setValue (const Val &value)
 
void toBack ()
 
void toFront ()
 
Val & value ()
 
const Val & value () const
 

Detailed Description

template<typename Key, typename Val, typename Hash, typename KeyEqual>
class QMutableMultiHashIterator< Key, Val, Hash, KeyEqual >

The QMutableMultiHashIterator class provides a Java style non-const iterator for QMultiHash. QMultiHash has both Java style iterators and STL style iterators.

QMultiHashIterator allows you to iterate over a QMultiHash. If you want to modify the hash as you iterate over it, use QMutableMultiHashIterator instead.

After construction the iterator is located at the very beginning of the hash, before the first item. The following code shows how to iterate over all the elements sequentially.

while (iter.hasNext()) {
iter.next();
qDebug() << iter.key() << ": " << iter.value();
}

The next() method returns the next item in the hash and advances the iterator. The key() and value() methods return the key and value of the last item which was jumped over.

Unlike STL style iterators, Java style iterators point between items rather than directly at items. The first call to next() advances the iterator to the position between the first and second item, and returns the first item; the second call to next() advances the iterator to the position between the second and third item, and so on.

Here is how to iterate over the elements in reverse order:

iter.toBack();
while (iter.hasPrevious()) {
iter.previous();
qDebug() << iter.key() << ": " << iter.value();
}

If you want to find all occurrences of a particular value use findNext() or findPrevious():

while (iter.findNext(widget)) {
qDebug() << "Found widget " << widget << " under key " << iter.key();
}

If you want to remove items as you iterate over the hash, use remove(). If you want to modify the value of an item, use setValue().

while (iter.hasNext()) {
iter.next();
if (iter.key() == iter.value()) {
iter.remove();
}
}

The example above removes all (key, value) pairs where the key and the value are the same.

See also
QMultiHashIterator

Constructor & Destructor Documentation

template<typename Key , typename Val , typename Hash , typename KeyEqual >
QMutableMultiHashIterator< Key, Val, Hash, KeyEqual >::QMutableMultiHashIterator ( QMultiHash< Key, Val, Hash, KeyEqual > &  hash)
inline

Constructs an iterator for traversing hash. The iterator is set to be at the front of the hash (before the first item).

See also
operator=()
template<typename Key , typename Val , typename Hash , typename KeyEqual >
QMutableMultiHashIterator< Key, Val, Hash, KeyEqual >::~QMutableMultiHashIterator ( )
inline

Destroys the iterator.

See also
operator=()

Method Documentation

template<typename Key , typename Val , typename Hash , typename KeyEqual >
bool QMutableMultiHashIterator< Key, Val, Hash, KeyEqual >::findNext ( const Val &  value)
inline

Searches for value starting from the current iterator position forward. Returns true if a (key, value) pair with value value is found, otherwise returns false.

After the call, if value was found, the iterator is positioned just after the matching item, otherwise the iterator is positioned at the back of the container.

See also
findPrevious()
template<typename Key , typename Val , typename Hash , typename KeyEqual >
bool QMutableMultiHashIterator< Key, Val, Hash, KeyEqual >::findPrevious ( const Val &  value)
inline

Searches for value starting from the current iterator position backward. Returns true if a (key, value) pair with value value is found, otherwise returns false.

After the call, if value was found, the iterator is positioned just before the matching item, otherwise the iterator is positioned at the front of the container.

See also
findNext()
template<typename Key , typename Val , typename Hash , typename KeyEqual >
bool QMutableMultiHashIterator< Key, Val, Hash, KeyEqual >::hasNext ( ) const
inline

Returns true if there is at least one item ahead of the iterator, i.e. the iterator is not at the back of the container, otherwise returns false.

See also
hasPrevious(), next()
template<typename Key , typename Val , typename Hash , typename KeyEqual >
bool QMutableMultiHashIterator< Key, Val, Hash, KeyEqual >::hasPrevious ( ) const
inline

Returns true if there is at least one item behind the iterator, i.e. the iterator is not at the front of the container, otherwise returns false.

See also
hasNext(), previous()
template<typename Key , typename Val , typename Hash , typename KeyEqual >
const Key & QMutableMultiHashIterator< Key, Val, Hash, KeyEqual >::key ( ) const
inline

Returns the key of the last item that was jumped over using one of the traversal methods (next(), previous(), findNext(), findPrevious()).

After a call to next() or findNext(), key() is equivalent to peekPrevious().key(). After a call to previous() or findPrevious(), key() is equivalent to peekNext().key().

See also
value()
template<typename Key , typename Val , typename Hash , typename KeyEqual >
Item QMutableMultiHashIterator< Key, Val, Hash, KeyEqual >::next ( )
inline

Returns the next item and advances the iterator by one position.

Call key() on the return value to obtain the item's key, and value() to obtain the value.

Calling this method on an iterator located at the back of the container leads to undefined behavior.

See also
hasNext(), peekNext(), previous()
template<typename Key , typename Val , typename Hash , typename KeyEqual >
QMutableMultiHashIterator & QMutableMultiHashIterator< Key, Val, Hash, KeyEqual >::operator= ( QMultiHash< Key, Val, Hash, KeyEqual > &  hash)
inline

Makes the iterator operate on hash. The iterator is set to be at the front of the hash (before the first item).

See also
toFront(), toBack()
template<typename Key , typename Val , typename Hash , typename KeyEqual >
Item QMutableMultiHashIterator< Key, Val, Hash, KeyEqual >::peekNext ( ) const
inline

Returns a reference to the next item without moving the iterator.

Call key() on the return value to obtain the item's key, and value() to obtain the value.

Calling this method on an iterator located at the back of the container leads to undefined behavior.

See also
hasNext(), next(), peekPrevious()
template<typename Key , typename Val , typename Hash , typename KeyEqual >
Item QMutableMultiHashIterator< Key, Val, Hash, KeyEqual >::peekPrevious ( ) const
inline

Returns the previous item without moving the iterator.

Call key() on the return value to obtain the item's key, and value() to obtain the value.

Calling this method on an iterator located at the front of the container leads to undefined behavior.

See also
hasPrevious(), previous(), peekNext()
template<typename Key , typename Val , typename Hash , typename KeyEqual >
Item QMutableMultiHashIterator< Key, Val, Hash, KeyEqual >::previous ( )
inline

Returns the previous item and moves the iterator back by one position.

Call key() on the return value to obtain the item's key, and value() to obtain the value.

Calling this method on an iterator located at the front of the container leads to undefined behavior.

See also
hasPrevious(), peekPrevious(), next()
template<typename Key , typename Val , typename Hash , typename KeyEqual >
void QMutableMultiHashIterator< Key, Val, Hash, KeyEqual >::remove ( )
inline

Removes the last item that was jumped over using one of the traversal methods (next(), previous(), findNext(), findPrevious()).

See also
setValue()
template<typename Key , typename Val , typename Hash , typename KeyEqual >
void QMutableMultiHashIterator< Key, Val, Hash, KeyEqual >::setValue ( const Val &  value)
inline

Replaces the value of the last item that was jumped over using one of the traversal methods with value.

The traversal methods are next(), previous(), findNext(), and findPrevious().

See also
key(), value(), remove()
template<typename Key , typename Val , typename Hash , typename KeyEqual >
void QMutableMultiHashIterator< Key, Val, Hash, KeyEqual >::toBack ( )
inline

Moves the iterator to the back of the container (after the last item).

See also
toFront(), previous()
template<typename Key , typename Val , typename Hash , typename KeyEqual >
void QMutableMultiHashIterator< Key, Val, Hash, KeyEqual >::toFront ( )
inline

Moves the iterator to the front of the container (before the first item).

See also
toBack(), next()
template<typename Key , typename Val , typename Hash , typename KeyEqual >
Val & QMutableMultiHashIterator< Key, Val, Hash, KeyEqual >::value ( )
inline

Returns a non-const reference to the value of the last item that was jumped over using one of the traversal methods.

template<typename Key , typename Val , typename Hash , typename KeyEqual >
const Val & QMutableMultiHashIterator< Key, Val, Hash, KeyEqual >::value ( ) const
inline

Returns the value of the last item that was jumped over using one of the traversal methods (next(), previous(), findNext(), findPrevious()).

After a call to next() or findNext(), value() is equivalent to peekPrevious().value(). After a call to previous() or findPrevious(), value() is equivalent to peekNext().value().

See also
key(), setValue()