Qore Programming Language Reference Manual 1.14.0
Loading...
Searching...
No Matches
Qore::RangeIterator Class Reference

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

#include <QC_RangeIterator.dox.h>

Inheritance diagram for Qore::RangeIterator:
[legend]

Public Member Methods

 constructor (int start, int stop, int step=1, auto val)
 creates the numerical sequence iterator with the initial arguments More...
 
 constructor (int stop)
 creates the numerical sequence iterator with the initial arguments More...
 
 copy ()
 Creates a copy of the RangeIterator object, iterating the same object as the original and in the same position. More...
 
auto getValue ()
 returns the current value or throws an INVALID-ITERATOR exception if the iterator is invalid More...
 
bool next ()
 This method returns True while there are more numbers to iterate and False when the range has been completely iterated. More...
 
 reset ()
 Reset the iterator instance to its initial state (start, stop, and step). More...
 
bool valid ()
 returns True if the iterator is currently pointing at a valid element, False if not More...
 
abstract auto getValue ()
 returns the current value More...
 
abstract bool next ()
 Moves the current position to the next element; returns False if there are no more elements. More...
 
abstract bool valid ()
 returns True if the iterator is currently pointing at a valid element, False if not More...
 

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 RangeIterator class provides an iterator for loop statements with functionality similar to range(). Unlike range(), RangeIterator 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: RangeIterator basic usage
RangeIterator r(0, 2);
foreach int i in (r) {
printf("i=%d\n", i);
}
# i=0
# i=1
# i=2
See also
range()
xrange()
Since
  • Qore 0.8.6
  • Qore 0.9.5 does not include the upper limit in the range unless %broken-range is set

Member Function Documentation

◆ constructor() [1/2]

Qore::RangeIterator::constructor ( int  start,
int  stop,
int  step = 1,
auto  val 
)

creates the numerical sequence iterator with the initial arguments

Parameters
startan initial value
stopa final value
stepis the interval. Default = 1
valan optional value to be returned instead of the default integer value
Example:
RangeIterator i(5, 10, 2);
See also
xrange()
Since
Qore 0.8.11.1 this method takes the optional val argument

◆ constructor() [2/2]

Qore::RangeIterator::constructor ( int  stop)

creates the numerical sequence iterator with the initial arguments

Parameters
stopa final value
Example:
RangeIterator i(5);
See also
xrange()
Since
Qore 0.8.11.1

◆ copy()

Qore::RangeIterator::copy ( )

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

Example:
RangeIterator ni = i.copy();

◆ getValue()

auto Qore::RangeIterator::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
Note
if a value was passed to the constructor, then that value is returned instead of the current range index

Implements Qore::AbstractIterator.

◆ next()

bool Qore::RangeIterator::next ( )
virtual

This method returns True while there are more numbers to iterate and False when the range has been completely 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.

◆ reset()

Qore::RangeIterator::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).

Example
# raw RangeIterator object usage
RangeIterator r(0, 2);
foreach int i in (r) {
printf("i=%d\n", i);
if (i == 1) {
break;
}
}
# i=0
# i=1
# show the reset feature; start iterating all over again
r.reset();
foreach int i in (r) {
printf("reused i=%d\n", i);
}
# reused i=0
# reused i=1
Exceptions
ITERATOR-THREAD-ERRORthis exception is thrown if this method is called from any thread other than the thread that created the object

◆ valid()

bool Qore::RangeIterator::valid ( )
virtual

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());
}

Implements Qore::AbstractIterator.