This class an iterator class for lists.
More...
#include <QC_ListIterator.dox.h>
|
| constructor (softlist< auto > l) |
| Creates the list iterator object.
|
|
| copy () |
| Creates a copy of the ListIterator object, iterating the same object as the original and in the same position.
|
|
bool | empty () |
| returns True if the list is empty; False if not
|
|
bool | first () |
| returns True if on the first element of the list
|
|
auto | getValue () |
| returns the current value or throws an INVALID-ITERATOR exception if the iterator is invalid
|
|
int | index () |
| returns the current iterator position in the list or -1 if not pointing at a valid element
|
|
bool | last () |
| returns True if on the last element of the list
|
|
int | max () |
| returns the number of elements in the list
|
|
bool | next () |
| Moves the current position to the next element in the list; 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 list if the list is not empty.
|
|
bool | prev () |
| Moves the current position to the previous element in the list; 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 list if the list is not empty.
|
|
| reset () |
| Reset the iterator instance to its initial state.
|
|
bool | set (int pos) |
| sets the new position in the list; if the position is invalid then the method returns False, meaning the iterator is not valid, otherwise it returns True
|
|
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 lists.
Call ListIterator::next() to iterate through the list; do not use the iterator if ListIterator::next() returns False. A list can be iterated in reverse order by calling ListIterator::prev() instead of ListIterator::next()
- Example: ListIterator basic usage
list data = (1, "foo", 2);
ListIterator it(data);
while (it.next()) {
printf("iter: %n\n", it.getValue());
}
iter: 1
iter: "foo"
iter: 2
- Note
- In general, the ListIterator 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
- ListReverseIterator
◆ constructor()
Creates the list iterator object.
- Parameters
-
- Example:
- Note
- the constructor's argument is softlist so that it can also accept NOTHING
◆ copy()
Qore::ListIterator::copy |
( |
| ) |
|
Creates a copy of the ListIterator object, iterating the same object as the original and in the same position.
- Example:
ListIterator ni = i.copy();
◆ empty()
bool Qore::ListIterator::empty |
( |
| ) |
|
returns True if the list is empty; False if not
- Returns
- True if the list is empty; False if not
- Code Flags:
- CONSTANT
- Example:
if (i.empty())
printf("the list is empty\n");
◆ first()
bool Qore::ListIterator::first |
( |
| ) |
|
returns True if on the first element of the list
- Returns
- True if on the first element of the list
- Code Flags:
- CONSTANT
- Example:
while (i.next()) {
if (i.first())
printf("START:\n");
}
◆ getValue()
auto Qore::ListIterator::getValue |
( |
| ) |
|
returns the current value or throws an INVALID-ITERATOR
exception if the iterator is invalid
- Returns
- the current 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 |
◆ index()
int Qore::ListIterator::index |
( |
| ) |
|
returns the current iterator position in the list or -1 if not pointing at a valid element
- Returns
- the current iterator position in the list or -1 if not pointing at a valid element
- Code Flags:
- CONSTANT
- Example:
while (i.next()) {
printf("+ %d/%d: %y\n", i.index(), i.max(), i.getValue());
}
◆ last()
bool Qore::ListIterator::last |
( |
| ) |
|
returns True if on the last element of the list
- Returns
- True if on the last element of the list
- Code Flags:
- CONSTANT
- Example:
while (i.next()) {
if (i.last())
printf("END.\n");
}
◆ max()
int Qore::ListIterator::max |
( |
| ) |
|
returns the number of elements in the list
- Returns
- the number of elements in the list
- Code Flags:
- CONSTANT
- Example:
while (i.next()) {
printf("+ %d/%d: %y\n", i.index(), i.max(), i.getValue());
}
◆ next()
bool Qore::ListIterator::next |
( |
| ) |
|
Moves the current position to the next element in the list; 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 list if the list is not empty.
This method will return True again after it returns False once if list 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 list (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());
}
- 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::ListIterator::prev |
( |
| ) |
|
Moves the current position to the previous element in the list; 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 list if the list is not empty.
This method will return True again after it returns False once if the list 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 list (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());
}
- 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::ListIterator::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 |
◆ set()
bool Qore::ListIterator::set |
( |
int |
pos | ) |
|
sets the new position in the list; if the position is invalid then the method returns False, meaning the iterator is not valid, otherwise it returns True
- Parameters
-
pos | the new position for the iterator with 0 as the first element |
- Returns
- False, meaning the iterator is not valid, otherwise it returns True
- Example:
if (!i.set(pos))
throw "INVALID-POSITION", sprintf("%d is an invalid position", pos);
- 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::ListIterator::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());