Qore Programming Language Reference Manual  0.8.6
 All Classes Namespaces Functions Variables Groups Pages
Qore::XRangeIterator Class Reference

This class defines a range-like iterator to be used to iterate numerical sequences. More...

Inheritance diagram for Qore::XRangeIterator:

Public Member Functions

 constructor (int start, int stop, int step=1)
 creates the numerical sequence iterator with the initial arguments
 
any getValue ()
 returns the current value or throws an INVALID-ITERATOR exception if the iterator is invalid
 
bool next ()
 This method returns True while there are more numbers to iterate and False when the range has been completed iterated.
 
 reset ()
 Reset the iterator instance to its initial state (start, stop, and step).
 
- Public Member Functions inherited from Qore::AbstractIterator
abstract bool valid ()
 returns True if the iterator is currently pointing at a valid element, False if not
 

Detailed Description

This class defines a range-like iterator to be used to iterate numerical sequences.

The main purpose is to provide resource friendly iterator to generate numerical rows (sequences) with ascending and descending ordering.

The XRangeIterator class provides an iterator for loop statements with functionality similar to range(). Unlike range() XRangeIterator objects do not generate real lists but calculate iteration values on demand.

This results in memory-friendly handling for large numerical sequences compared to generating a list in memory and iterating that list (as with Qore::range()).

Example:
my XRangeIterator $r(2);
for my int $i in ($r)
printf("i=%d\n", $i);
# i=0
# i=1
# i=2
See Also
range()
xrange()
Since
Qore 0.8.6

Member Function Documentation

Qore::XRangeIterator::constructor ( int  start,
int  stop,
int  step = 1 
)

creates the numerical sequence iterator with the initial arguments

Parameters
startan initial value
stopa final value
stepis the interval. Default = 1
Example:
my XRangeIterator $i(5, 10, 2);
See Also
xrange()
any Qore::XRangeIterator::getValue ( )
virtual

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-ITERATORthe iterator is not pointing at a valid element
ITERATOR-THREAD-ERRORthis exception is thrown if this method is called from any thread other than the thread that created the object

Implements Qore::AbstractIterator.

bool Qore::XRangeIterator::next ( )
virtual

This method returns True while there are more numbers to iterate and False when the range has been completed iterated.

The iterator object should not be used after this method returns False.

Returns
True and False alternately unless it has no value iterate.
Example:
while ($i.next()) {
printf("value: %y\n", $i.getValue());
}
Exceptions
ITERATOR-THREAD-ERRORthis exception is thrown if this method is called from any thread other than the thread that created the object

Implements Qore::AbstractIterator.

Qore::XRangeIterator::reset ( )

Reset the iterator instance to its initial state (start, stop, and step).

Reset the iterator instance to its initial state (start, stop, and step).

Code Flags:
CONSTANT
Example
# raw XRangeIterator object usage
XRangeIterator $r(2);
foreach my int $i in ($r)
printf("i=%d\n", $i);
# i=0
# i=1
# i=2
# show the reset feature
r.reset();
foreach my int $i in (Rr)
printf("reused i=%d\n", $i);
# reused i=0
# reused i=1
# reused i=2
Exceptions
ITERATOR-THREAD-ERRORthis exception is thrown if this method is called from any thread other than the thread that created the object