All Classes Namespaces Functions Variables Groups Pages
Qore::RangeIterator Class Reference

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

Inheritance diagram for Qore::RangeIterator:
[legend]

## Public Member Functions

| constructor (int start, int stop, int step=1)
| creates the numerical sequence iterator with the initial arguments
| copy ()
| Creates a copy of the RangeIterator object, iterating the same object as the original and in the same position.
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 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:
  my RangeIterator $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::RangeIterator::constructor | ( | int | start,
|  | int | stop,
|  | int | step = `1`
| ) |  |

creates the numerical sequence iterator with the initial arguments

Parameters

  start | an initial value
  stop | a final value
  step | is the interval. Default = 1

Example:
  my RangeIterator $i(5, 10, 2);

See Also
  xrange()

Qore::RangeIterator::copy | ( |  | ) |

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

Example:
  my RangeIterator $ni = $i.copy();

any 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-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

Implements Qore::AbstractIterator.

bool Qore::RangeIterator::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-ERROR | this exception is thrown if this method is called from any thread other than the thread that created the object

Implements Qore::AbstractIterator.

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
  my RangeIterator $r(2);
  foreach my 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 my int $i in ($r)
  printf("reused i=%d\n", $i);
  # reused i=0
  # reused i=1
  # reused i=2

Exceptions

  ITERATOR-THREAD-ERROR | this exception is thrown if this method is called from any thread other than the thread that created the object