// FILE. . . . . d:/hak/hlt/src/hlt/math/matrix/sources/ColumnIterator.java
// EDIT BY . . . Hassan Ait-Kaci
// ON MACHINE. . Hak-Laptop
// STARTED ON. . Thu Nov 28 13:42:19 2019


/**
 * @version     Last modified on Thu Nov 28 23:48:39 2019 by hak
 * @author      <a href="mailto:hak@acm.org">Hassan A&iuml;t-Kaci</a>
 * @copyright   &copy; <a href="http://www.hassan-ait-kaci.net/">by the author</a>
 */

package hlt.math.matrix;

import hlt.language.util.IntIterator;

/**
 * This is a class implementing the <a
 * href="https://www.hassan-ait-kaci.net/hlt/doc/hlt/code/language/util/IntIterator.html"><tt>hlt.language.util.IntIterator</tt></a>
 * interface for iterating through the columns of the matrix of a
 * <tt>BipartiteGraph</tt> for a given row.
 *
 * @see RowIterator
 */
public class ColumnIterator implements IntIterator
{
  /**
   * The <tt>BipartiteGraph</tt> concerned by this
   * <tt>ColumnIterator</tt>.
   */
  private BipartiteGraph graph;

  /**
   * The row index concerned by this <tt>ColumnIterator</tt>.
   */
  private int row;

  /**
   * The number of columns per row in the matrix of <tt>graph</tt>.
   */
  private int cols;

  /**
   * The index of the last non-zero column for <tt>row</tt> in
   * <tt>graph</tt>, or <tt>-1</tt> if there is none.
   */
  private int last;

  /**
   * Current column in this <tt>ColumnIterator</tt>.
   */
  private int col;

  /**
   * Construct a <tt>ColumnIterator</tt> for <tt>row</tt> in the given
   * <tt>graph</tt>.
   */
  public ColumnIterator (BipartiteGraph graph, int row)
  {
    this.graph = graph;
    this.row = row;
    cols = graph.data().length;
    last = graph.lastNonZeroColInRow(row); // -1 if only 0.0s in this row
    col = 0;
  }

  /**
   * Return <tt>true</tt> iff this <tt>ColumnIterator</tt> has more
   * indices remaining.
   */
  public final boolean hasNext ()
    {
      if (col > last)
	return false;
      
      while (col <= last && !graph.isEdge(row,col))
	col++;

      return col <= last; // NB: if all 0.0s in row, this returns false (since last = -1)
    }

  /**
   * Return the last non-zero column index in <tt>row</tt>, or
   * <tt>-1</tt> if none exists.
   */
  public final int last ()
  {
    return last;
  }

  /**
   * Return the next column index in this <tt>ColumnIterator</tt>, or
   * <tt>-1</tt> if there is none remaining.
   */
  public final int next ()
  {        
    return col == cols ? -1 : col++;
  }

}
