This class an iterator class for hashes.
More...
#include <QC_HashIterator.dox.h>
|
| constructor (hash< auto > h) |
| Creates the hash iterator object.
|
|
| constructor () |
| Creates an empty hash iterator object.
|
|
| copy () |
| Creates a copy of the HashIterator object, iterating the same object as the original and in the same position.
|
|
bool | empty () |
| returns True if the hash is empty; False if not
|
|
bool | first () |
| returns True if on the first element of the hash
|
|
string | getKey () |
| returns the current key value or throws an INVALID-ITERATOR exception if the iterator is invalid
|
|
auto | getKeyValue () |
| returns the current value of the current hash key being iterated or throws an INVALID-ITERATOR exception if the iterator is invalid
|
|
auto | getValue () |
| returns the current key value or throws an INVALID-ITERATOR exception if the iterator is invalid
|
|
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
|
|
bool | last () |
| returns True if on the last element of the hash
|
|
bool | next () |
| 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.
|
|
bool | prev () |
| 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.
|
|
| reset () |
| Reset the iterator instance to its initial state.
|
|
bool | valid () |
| returns True if the iterator is currently pointing at a valid element, False if not
|
|
abstract bool | prev () |
| Moves the current position to the previous element; returns False if there are no more elements.
|
|
abstract auto | getValue () |
| returns the current value
|
|
abstract bool | next () |
| Moves the current position to the next element; returns False if there are no more elements.
|
|
abstract bool | valid () |
| returns True if the iterator is currently pointing at a valid element, False if not
|
|
abstract bool | empty () |
| returns True if the object to iterate is empty; False if not
|
|
abstract bool | first () |
| returns True if on the first element
|
|
abstract bool | last () |
| returns True if on the last element
|
|
This class an iterator class for hashes.
Call HashIterator::next() to iterate through the hash; do not use the iterator if HashIterator::next() returns False. A hash can be iterated in reverse order by calling HashIterator::prev() instead of HashIterator::next()
- Example: HashIterator basic usage
hash<auto> h = {"key1": 1, "key2": 2,};
HashIterator it(h);
while (it.next()) {
printf("getKey: %n; getKeyValue: %n; getValue: %n; getValuePair: %n\n",
it.getKey(), it.getKeyValue(), it.getValue(), it.getValuePair());
}
getKey: "key1"; getKeyValue: 1; getValue: 1; getValuePair: hash: (key : "key1", value : 1)
getKey: "key2"; getKeyValue: 2; getValue: 2; getValuePair: hash: (key : "key2", value : 2)
- Note
- In general, the HashIterator class is not designed to be accessed from multiple threads; it was created without locking for fast and efficient use when used from a single thread. For methods that would be unsafe to use in another thread, any use of such methods in threads other than the thread where the constructor was called will cause an
ITERATOR-THREAD-ERROR
to be thrown.
- See also
- HashReverseIterator
◆ constructor() [1/2]
Qore::HashIterator::constructor |
( |
| ) |
|
Creates an empty hash iterator object.
- Example:
*hash<auto> h = get_hash_or_nothing();
HashIterator hi(h);
◆ constructor() [2/2]
Qore::HashIterator::constructor |
( |
hash< auto > |
h | ) |
|
Creates the hash iterator object.
- Parameters
-
- Example:
-
◆ copy()
Qore::HashIterator::copy |
( |
| ) |
|
Creates a copy of the HashIterator object, iterating the same object as the original and in the same position.
- Example:
HashIterator ni = i.copy();
◆ empty()
bool Qore::HashIterator::empty |
( |
| ) |
|
returns True if the hash is empty; False if not
- Returns
- True if the hash is empty; False if not
- Code Flags:
- CONSTANT
- Example:
if (i.empty())
printf("the hash is empty\n");
◆ first()
bool Qore::HashIterator::first |
( |
| ) |
|
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.first())
printf("START:\n");
}
◆ getKey()
string Qore::HashIterator::getKey |
( |
| ) |
|
returns the current key value or throws an INVALID-ITERATOR
exception if the iterator is invalid
- Returns
- the current key value or throws an
INVALID-ITERATOR
exception if the iterator is invalid
- Code Flags:
- RET_VALUE_ONLY
- Example:
while (i.next()) {
printf("+ %y\n", i.getKey());
}
- Exceptions
-
INVALID-ITERATOR | the iterator is not pointing at a valid element |
ITERATOR-THREAD-ERROR | this exception is thrown if this method is called from any thread other than the thread that created the object |
◆ getKeyValue()
auto Qore::HashIterator::getKeyValue |
( |
| ) |
|
returns the current value of the current hash key being iterated or throws an INVALID-ITERATOR
exception if the iterator is invalid
- Returns
- the current value of the current hash key being iterated or throws an
INVALID-ITERATOR
exception if the iterator is invalid
- Code Flags:
- RET_VALUE_ONLY
- Example:
while (i.next()) {
printf("+ %y\n", i.getKeyValue());
}
- Exceptions
-
INVALID-ITERATOR | the iterator is not pointing at a valid element |
ITERATOR-THREAD-ERROR | this exception is thrown if this method is called from any thread other than the thread that created the object |
- Since
- Qore 0.8.6
◆ getValue()
auto Qore::HashIterator::getValue |
( |
| ) |
|
returns the current key value or throws an INVALID-ITERATOR
exception if the iterator is invalid
The current hash key can be returned with getKey().
- Returns
- the current key value or throws an
INVALID-ITERATOR
exception if the iterator is invalid
- Code Flags:
- RET_VALUE_ONLY
- Example:
while (i.next()) {
printf("+ %y\n", i.getValue());
}
- Exceptions
-
INVALID-ITERATOR | the iterator is not pointing at a valid element |
ITERATOR-THREAD-ERROR | this exception is thrown if this method is called from any thread other than the thread that created the object |
◆ getValuePair()
hash< auto > Qore::HashIterator::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
- 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
- Code Flags:
- RET_VALUE_ONLY
- Example:
while (i.next()) {
printf("+ %y\n", i.getValuePair());
}
- Exceptions
-
INVALID-ITERATOR | the iterator is not pointing at a valid element |
ITERATOR-THREAD-ERROR | this exception is thrown if this method is called from any thread other than the thread that created the object |
- Since
- Qore 0.8.6
◆ last()
bool Qore::HashIterator::last |
( |
| ) |
|
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.last())
printf("END.\n");
}
◆ next()
bool Qore::HashIterator::next |
( |
| ) |
|
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 = %y\n", i.getKey(), i.getValue());
}
- Exceptions
-
ITERATOR-THREAD-ERROR | this exception is thrown if this method is called from any thread other than the thread that created the object |
◆ prev()
bool Qore::HashIterator::prev |
( |
| ) |
|
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 = %y\n", i.getKey(), i.getValue());
}
- Exceptions
-
ITERATOR-THREAD-ERROR | this exception is thrown if this method is called from any thread other than the thread that created the object |
◆ reset()
Qore::HashIterator::reset |
( |
| ) |
|
Reset the iterator instance to its initial state.
Reset the iterator instance to its initial state
- Example
- Exceptions
-
ITERATOR-THREAD-ERROR | this exception is thrown if this method is called from any thread other than the thread that created the object |
◆ valid()
bool Qore::HashIterator::valid |
( |
| ) |
|
returns True if the iterator is currently pointing at a valid element, False if not
- Returns
- True if the iterator is currently pointing at a valid element, False if not
- Code Flags:
- CONSTANT
- Example:
if (i.valid())
printf("current value: %y\n", i.getValue());