Return one active-set item

When the iterator status is SET_RETONE, the iterator function can return one item of the active set. When the iterator function obtains this iterator-status value, it can perform the iteration tasks needed to generate one item of the active set.

You can perform these iterator tasks directly in the iterator function or you can declare a separate iterator-value-return function, which the iterator function calls when it receives the SET_RETONE iterator-status value. Declare the iterator-value-return function to return the same data type as the main iterator function. The database server puts the return value of this function in the active set.

The following code fragment implements an iterator-value-return function, named fibGen_retone(), that the fibGen() iterator function (The fibGen() iterator function) calls each time it obtains the SET_RETONE iterator status.
Figure 1: The fibGen_retone() value-return function
/* fibGen_retone(): 
 *    Generates the next number in the series. Compares it with the stop
 *    value to check if the end condition is met. Then performs following
 *    calculations:
 *        num1 = num2;
 *       num2 = next number in the series.
 */
mi_integer fibGen_retone(fparam)
   MI_FPARAM *fparam;
{
   fibState   *fibstate;
   mi_integer next;

   fibstate = (fibState *)mi_fp_funcstate(fparam);

   /* Generate next Fibonacci number */
   if ( fibstate->fib_ncomputed < 2 ) 
      return((fibstate->fib_ncomputed++ == 0) ? 0 : 1);

   /* Update user state for next iteration */
   next = fibstate->fib_prec1 + fibstate->fib_prec2;

   if ( next == 0 ) 
      {
      fibstate->fib_prec1 = 0;
      fibstate->fib_prec2 = 1;
      }
   else 
      {
      fibstate->fib_prec1 = fibstate->fib_prec2;
      fibstate->fib_prec2 = next;
      }
   return (next);

Each item in the active set that the fibGen() function generates is one call to the fibGen_retone() function. The fibGen_retone() function returns one number of the Fibonacci series. It uses the mi_fp_funcstate() function to obtain the user-state pointer from the MI_FPARAM structure. This user-state pointer points to a fibstate structure (which the fibGen_init() function in The fibGen_init() initialization function allocated).

From the information in the fibstate structure, the fibGen_retone() function determines the next Fibonacci number and stores it in the next variable. The function then updates the fibstate structure for the next iteration of fibGen(). Finally, the function returns one item of the active set: the value of next.