Qore Programming Language Reference Manual 0.8.8

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes

- Class List
- Class Index
- Class Hierarchy
- Class Members

All Classes Namespaces Functions Variables Groups Pages
Public Member Functions | List of all members
Qore::ListHashReverseIterator Class Reference

This class a reverse iterator class for lists of hashes as returned by Qore::SQL::Datasource::selectRows() and Qore::SQL::DatasourcePool::selectRows(), both of which return hashes with keys giving column names where the key values are lists of column values. More...

Inheritance diagram for Qore::ListHashReverseIterator:
[legend]

## Public Member Functions
| constructor (softlist l)
| Creates the list hash iterator object. More...
| copy ()
| Creates a copy of the ListHashReverseIterator object, iterating the same object as the original and in the same position. More...
bool | first ()
| returns True if on the first element being iterated (ie the last element in the list) More...
bool | last ()
| returns True if on the last element being iterated (ie the first element in the list) More...
any | memberGate (string key)
| This method allows the iterator to be dereferenced directly as a hash for the current row being iterated, as memberGate methods are called implicitly when an unknown member is accessed from outside the class. More...
bool | next ()
| Moves the current position to the next element in the result 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. More...
bool | prev ()
| Moves the current position to the previous element in the result 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. More...
Public Member Functions inherited from Qore::ListHashIterator
| constructor (softlist l)
| Creates the hash list iterator object. More...
| copy ()
| Creates a copy of the ListHashIterator object, iterating the same object as the original and in the same position. More...
bool | empty ()
| returns True if the result list is empty; False if not More...
any | getKeyValue (string key)
| Returns the current value for the column given as an argument. More...
hash | getRow ()
| returns the current row value as a hash or throws an `INVALID-ITERATOR` exception if the iterator is invalid More...
hash | getValue ()
| returns the current row value as a hash or throws an `INVALID-ITERATOR` exception if the iterator is invalid More...
int | index ()
| returns the current iterator position in the list or -1 if not pointing at a valid element More...
int | max ()
| returns the number of elements in the list More...
any | memberGate (string key)
| This method allows the iterator to be dereferenced directly as a hash for the current row being iterated, as memberGate methods are called implicitly when an unknown member is accessed from outside the class. More...
| reset ()
| Reset the iterator instance to its initial state. More...
bool | set (int pos)
| sets the new position in the result list; if the position is invalid then the method returns False, meaning the iterator is not valid, otherwise it returns True More...
bool | valid ()
| returns True if the iterator is currently pointing at a valid element, False if not More...

## Detailed Description

This class a reverse iterator class for lists of hashes as returned by Qore::SQL::Datasource::selectRows() and Qore::SQL::DatasourcePool::selectRows(), both of which return hashes with keys giving column names where the key values are lists of column values.

Call ListHashReverseIterator::next() to iterate through the lists of column values assigned to each hash key in reverse order; do not use the iterator if ListHashReverseIterator::next() returns False. A result list can be iterated in reverse order by calling ListHashReverseIterator::prev() instead of ListHashReverseIterator::next()

Example: ListHashReverseIterator basic usage
  my list $data = (
  ( "column1" : 1, "column2" : "a"),
  ( "column1" : 2, "column2" : "b"),
  );
  my ListHashReverseIterator $it($data);
  while ($it.next()) {
  printf("iter %d: %n\n", $it.index(), $it.getValue());
  }
  iter 1: hash: (column1 : 2, column2 : "b")
  iter 0: hash: (column1 : 1, column2 : "a")

Note
  - In general, the ListHashReverseIterator 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.
  - ListHashReverseIterator is functionally equivalent to ListHashIterator, but the effect of ListHashReverseIterator::next() and ListHashReverseIterator::prev() are the opposite of ListHashIterator::next() and ListHashIterator::prev(); that is ListHashReverseIterator::next() will iterate through the hash in reverse order, while ListHashReverseIterator::prev() iterates in forward order. Additionally the meanings of the return values for ListHashReverseIterator::first() and ListHashReverseIterator::last() are swapped in respect to ListHashIterator::first() and ListHashIterator::last().

See Also
  ListHashIterator

## Member Function Documentation

Qore::ListHashReverseIterator::constructor | ( | softlist | l | ) |

Creates the list hash iterator object.

Parameters

  l | the list of hashes to iterate

Example:
  my ListHashIterator $i($l);

Note
  the constructor's argument is softlist so that it can also accept NOTHING

Qore::ListHashReverseIterator::copy | ( |  | ) |

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

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

bool Qore::ListHashReverseIterator::first | ( |  | ) |
| virtual

returns True if on the first element being iterated (ie the last element in the list)

Returns
  True if on the first element being iterated (ie the last element in the list)

Code Flags:
  CONSTANT

Example:
  while ($i.next()) {
  if ($i.first())
  printf("START:\n");
  }

Reimplemented from Qore::ListHashIterator.

bool Qore::ListHashReverseIterator::last | ( |  | ) |
| virtual

returns True if on the last element being iterated (ie the first element in the list)

Returns
  True if on the last element being iterated (ie the first element in the list)

Code Flags:
  CONSTANT

Example:
  while ($i.next()) {
  if ($i.last())
  printf("END.\n");
  }

Reimplemented from Qore::ListHashIterator.

any Qore::ListHashReverseIterator::memberGate | ( | string | key | ) |

This method allows the iterator to be dereferenced directly as a hash for the current row being iterated, as memberGate methods are called implicitly when an unknown member is accessed from outside the class.

Code Flags:
  RET_VALUE_ONLY

Parameters

  key | the column name for the value to retrieve

Returns
  the current column value of the given row

Example:
  while ($i.next()) {
  printf("%d: value: %y", $i.index(), $i.value);
  }

Exceptions

  ITERATOR-THREAD-ERROR | this exception is thrown if this method is called from any thread other than the thread that created the object
  INVALID-ITERATOR | the iterator is not pointing at a valid element or the list being iterated does not contain a hash element at the current iterator position or the given hash key does not exist

Note
  equivalent to ListHashIterator::getKeyValue() when called explicitly

bool Qore::ListHashReverseIterator::next | ( |  | ) |
| virtual

Moves the current position to the next element in the result 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 result 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(" + row %d: %y\n", $i.index(), $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

Reimplemented from Qore::ListHashIterator.

bool Qore::ListHashReverseIterator::prev | ( |  | ) |
| virtual

Moves the current position to the previous element in the result 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 result 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(" + row %d: %y\n", $i.index(), $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

Reimplemented from Qore::ListHashIterator.