Qore Programming Language Reference Manual  0.9.4.6
Qore::HashReverseIterator Class Reference

This class an iterator class for hashes. More...

Inheritance diagram for Qore::HashReverseIterator:

Public Member Methods

 constructor (hash< auto > h)
 Creates the hash iterator object. More...
 
 constructor ()
 Creates an empty iterator object. More...
 
 copy ()
 Creates a copy of the HashReverseIterator object, iterating the same object as the original and in the same position. More...
 
bool first ()
 returns True if on the last element of the hash More...
 
bool last ()
 returns True if on the first element of the hash More...
 
bool next ()
 Moves the current position to the previous element in the hash; returns False if there are no more elements; if the iterator is not pointing at a valid element before this call, the iterator will be positioned on the last element in the hash if the hash is not empty. More...
 
bool prev ()
 Moves the current position to the next element in the hash; returns False if there are no more elements; if the iterator is not pointing at a valid element before this call, the iterator will be positioned on the first element in the hash if the hash is not empty. More...
 
- Public Member Methods inherited from Qore::HashIterator
 constructor (hash< auto > h)
 Creates the hash iterator object. More...
 
 constructor ()
 Creates an empty hash iterator object. More...
 
 copy ()
 Creates a copy of the HashIterator object, iterating the same object as the original and in the same position. More...
 
bool empty ()
 returns True if the hash is empty; False if not More...
 
string getKey ()
 returns the current key value or throws an INVALID-ITERATOR exception if the iterator is invalid More...
 
auto getKeyValue ()
 returns the current value of the current hash key being iterated or throws an INVALID-ITERATOR exception if the iterator is invalid More...
 
auto getValue ()
 returns the current key value or throws an INVALID-ITERATOR exception if the iterator is invalid More...
 
hash< auto > getValuePair ()
 returns a hash with the current key and value (a hash with 2 keys: "key" and "value") or throws an INVALID-ITERATOR exception if the iterator is invalid More...
 
 reset ()
 Reset the iterator instance to its initial state. More...
 
bool valid ()
 returns True if the iterator is currently pointing at a valid element, False if not More...
 

Detailed Description

This class an iterator class for hashes.

Call HashReverseIterator::next() to iterate through the hash in reverse order; do not use the iterator if HashReverseIterator::next() returns False. A hash can be iterated in reverse order by calling HashReverseIterator::prev() instead of HashReverseIterator::next()

Example: HashReverseIterator basic usage
hash<auto> h = ( "key1" : 1, "key2" : 2, );
HashReverseIterator it(h);
while (it.next()) {
printf("getKey: %n; getKeyValue: %n; getValue: %n; getValuePair: %n\n",
it.getKey(), it.getKeyValue(), it.getValue(), it.getValuePair());
}
getKey: "key2"; getKeyValue: 2; getValue: 2; getValuePair: hash: (key : "key2", value : 2)
getKey: "key1"; getKeyValue: 1; getValue: 1; getValuePair: hash: (key : "key1", value : 1)
Note
See also
HashIterator

Member Function Documentation

◆ constructor() [1/2]

Qore::HashReverseIterator::constructor ( hash< auto >  h)

Creates the hash iterator object.

Parameters
hthe hash to iterate
Example:
HashReverseIterator hi(h);

◆ constructor() [2/2]

Qore::HashReverseIterator::constructor ( )

Creates an empty iterator object.

Example:
*hash<auto> h = get_hash_or_nothing();
HashReverseIterator hi(h);

◆ copy()

Qore::HashReverseIterator::copy ( )

Creates a copy of the HashReverseIterator object, iterating the same object as the original and in the same position.

Example:
HashReverseIterator ni = i.copy();

◆ first()

bool Qore::HashReverseIterator::first ( )
virtual

returns True if on the last element of the hash

Returns
True if on the last element of the hash
Code Flags:
CONSTANT
Example:
while (i.next()) {
if (i.first())
printf("START:\n");
}

Reimplemented from Qore::HashIterator.

◆ last()

bool Qore::HashReverseIterator::last ( )
virtual

returns True if on the first element of the hash

Returns
True if on the first element of the hash
Code Flags:
CONSTANT
Example:
while (i.next()) {
if (i.last())
printf("END.\n");
}

Reimplemented from Qore::HashIterator.

◆ next()

bool Qore::HashReverseIterator::next ( )
virtual

Moves the current position to the previous element in the hash; returns False if there are no more elements; if the iterator is not pointing at a valid element before this call, the iterator will be positioned on the last element in the hash if the hash is not empty.

This method will return True again after it returns False once if the hash is not empty, otherwise it will always return False. The iterator object should not be used after this method returns False

Returns
False if there are no more elements in the hash (in which case the iterator object is invalid and should not be used); True if successful (meaning that the iterator object is valid)
Example:
while (i.prev()) {
printf(" + %y\n", i.getValue());
}
Note
HashReverseIterator::next() is the opposite of HashIterator::next(); it is functionally equivalent to HashIterator::prev(); HashReverseIterator::next() iterates through the hash in reverse order
Exceptions
ITERATOR-THREAD-ERRORthis exception is thrown if this method is called from any thread other than the thread that created the object

Reimplemented from Qore::HashIterator.

◆ prev()

bool Qore::HashReverseIterator::prev ( )
virtual

Moves the current position to the next element in the hash; returns False if there are no more elements; if the iterator is not pointing at a valid element before this call, the iterator will be positioned on the first element in the hash if the hash is not empty.

This method will return True again after it returns False once if hash is not empty, otherwise it will always return False. The iterator object should not be used after this method returns False

Returns
False if there are no more elements in the hash (in which case the iterator object is invalid and should not be used); True if successful (meaning that the iterator object is valid)
Example:
while (i.next()) {
printf(" + %y\n", i.getValue());
}
Note
HashReverseIterator::prev() is the opposite of HashIterator::prev(); it is functionally equivalent to HashIterator::next(); HashReverseIterator::prev() iterates through the hash in forward order
Exceptions
ITERATOR-THREAD-ERRORthis exception is thrown if this method is called from any thread other than the thread that created the object

Reimplemented from Qore::HashIterator.