/* Declaration of class Table.

------------------------------------------------------------------*/

#include "Row.h"
#include "Boolean.h"
#include "Strings.h"

#include <iostream.h>
#include <fstream.h>

class Table
{
protected:                          // hide the data members:

   unsigned
      Rows_,                        //   the number of rows
      Columns_;                     //   the number of columns

   Row                              //   pointer to a Row array,
      *Grid;                        //     allocated at run-time

public:                             // the public operations:

   /***** Constructors and Destructors *****/
   /*----- Class Constructor -----

      Precondition:  A Table object has been declared.
      Receive:       NumRows (optional), the number of rows in the Table
                       NumCols (optional), the number of columns in the
                       Table
                     InitValue  (default value zero), to initialize
                     the values of the Table's elements
      Postcondition: The declared Table object containing this
                       function has its Rows_ member initialized to
                       NumRows and its Columns_ member initialized
                       to NumCols.
   ----------------------------------------------------------------*/
   Table(unsigned NumRows, unsigned NumCols,
         TableElement InitValue);

	 Table(void)
	 {
			Rows_ = 0;
			Columns_ = 0;
			Grid = 0;
	 }

   /*----- Copy Constructor -----

      Precondition:  The compiler nees a copy of a table object.
      Received:      The Table (reference) object Original to be
                       copied
      Postcondition: The members of the object containing this
                       function, are copies of those of Original.
   ----------------------------------------------------------------*/
   Table(const Table& Original);

   /*----- Destructor -----

      Precondition:  This Table object should no longer exist.
      Postcondition: The run-time storage of this Table has been
                       reclaimed, and its members reset as an
                       empty Table.
   ----------------------------------------------------------------*/
   ~Table(void);

   /****** Operations *****/
   /*----- Subscript -----

      Receive: i, an index into the Table
      Return:  That Row in Table whose index is i
   ----------------------------------------------------------------*/
   Row& operator[](unsigned i);

   /*----- Assignment -----

      Receive: Tab, a (const reference) Table object
      Return:  The Table containing this function, its data members
                 containing copies of those of Tab, and a reference
                 to the object assigned.
   ----------------------------------------------------------------*/
   Table& operator=(const Table& Tan);

   /*-------------------------------------------------------------------
      This function sums the elements of a given row in a Table object.

      Receive:  Integer row, the row number
      Return:   The sum of the elements in the row-th row of the Table
                   object containing this function
   -------------------------------------------------------------------*/
   TableElement RowSum(unsigned r) const;

   /*-------------------------------------------------------------------
      This function sums the elements of a given column in a Table object.

      Receive:  Integer col, the column number
      Return:   The sum of the elements in the col-th column of the Table
                   object containing this function
   -------------------------------------------------------------------*/
   TableElement ColumnSum(unsigned c) const;

   /*----- Data member extractors -----*/

   unsigned NumberOfRows(void) const { return Rows_;}

   unsigned NumberOfColumns(void) const { return Columns_;}

   /*-------------------------------------------------------------------
      This function overloads the output operator for a Table object.

      Receive: Out, an ostream (reference) object
               Tab, a (const reference) Table object
      Return:  Out, containing the data from Tab
   ------------------------------------------------------------------*/
   friend ostream& operator<<(ostream& Out, const Table& T);

   /*----- Interactive Input -----

      Receive:    In, an istream
                  InTab, a (reference) Table object
      Input (In): A table of values.
      Return:     The Table object containing this function,
                    filled with the input values
      Assumption: InTab has been initialized appropriately with the
                    number of rows and columns needed to store the
                    input values.
   --------------------------------------------------------------------*/
   friend istream& operator>>(istream& In, Table& InTab);

   /*----- File Input -----

      Receive:      FileName, a Strings object containing the name of
                      an input file
      Input (File): The number of rows and columns and a table
                      of values
      Return:       False, if the file could not be opened; otherwise,
                      true and the Table object containing this
                      function, its Grid member containing the
                      input values
      Assumption:   The first two values in FileName are the number of
                      rows and columns, respectively.
   --------------------------------------------------------------------*/
   Boolean Load(const Strings& FileName);

   /*----- File -----
      Writes a Table object to a file FileName,
         assuming that the first two values in the file give
         the number of rows and columns of the Table.

      Receive:      FileName, a Strings object containing the name of
                       an output file
      Output(File): The number of rows and columns and a table
                       of values
      Return:       False, if the file could not be opened; otherwise,
                       true and the file filled with this Table object
   ----------------------------------------------------------------*/
   Boolean Write(const Strings& FileName) const;
};




