/* This file contains the implementation for class Row.

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

#include "Row.h"

#include <assert.h>
#include <iostream.h>
#include <stdlib.h>

/*------------------------------------------------------------------
   This function constructs a Row.

   Precondition:   A Row object has been declared.
   Receive:        N, the number of elements to be allocated 
                     (default 0)
   Postcondition:  In the Row containing this function the Length_ 
                     member is set to N, and the Array member 
                     contains the address of an array of N 
                     elements (or the NULL address).
-------------------------------------------------------------------*/

Row::Row(unsigned N)                      // N has default value zero
{
   Length_ = N;                           // set Length_ member
                                          // set Array member:
   if (Length_ > 0)                       //   if N is not zero
   {
      Array = new TableElement[Length_];  //     allocate a new array
      assert(Array != 0);                 //     verify success
   }
   else                                   //   otherwise
      Array = 0;                          //     set Array to NULL
}


/*------------------------------------------------------------------
   This function makes a copy of a Row.

   Precondition:  The compiler needs a copy of a Row.
   Receive:       OrigRow, the Row being copied
   Postcondition: In the Row containing this function,
                     the Length_ member is set to OrigRow.Length_,
                     and the Array member contains the address of
                     an array that is a copy of OrigRow's array
                     (or the NULL address).
------------------------------------------------------------------*/

Row::Row(const Row& OrigRow)
{
   Length_ = OrigRow.Length_;             // copy Length_ member
                                          // copy Array member:
   if (Length_ > 0)                       // if OrigRow is not empty
   {
      Array = new TableElement [Length_]; //   allocate a new array
      assert(Array != 0);                 //   verify success
      for (int i = 0; i < Length_; i++)   //   copy the elements
         Array[i] = OrigRow.Array[i];     //     from OrigRow
   }                                      //       into the array
   else                                   // otherwise
      Array = 0;                          //    set Array to NULL
} 

/*------------------------------------------------------------------
   This function tears down a Row.

   Precondition:  The Row containing this function should cease to
                     exist.
   Postcondition: The run-time storage of this Row has been
                     deallocated, and its members are set
                     appropriately as an empty Row.
------------------------------------------------------------------*/

Row::~Row(void)
{
   delete [] Array;
   Array = 0;
   Length_ = 0;
}

/*------------------------------------------------------------------
   These functions perform the subscript operation on a Row object.

   Receive: i, an integer value;
   Return:  The element of the array pointed to by Array whose
               index is i (assuming that i is a valid index).
------------------------------------------------------------------*/

TableElement& Row::operator[](unsigned i)
{
   if (i < Length_)                              // if access is valid
      return Array[i];                           //   return element
                                                 // otherwise
   cerr << "\n*** Row Subscript: invalid index " //   fatal error msg
        << i << " received!\n";
   exit (-1);
}

const TableElement& operator[](unsigned i)
{
   if (i < Length_)                              // if access is valid
      return Array[i];                           //   return element
                                                 // otherwise
   cerr << "\n*** Row Subscript: invalid index " //   fatal error msg
        << i << " received!\n";
   exit (-1);
}


/*------------------------------------------------------------------
   This function assigns a Row.

   Receive: RowObj, a Row object
   Return:  The Row containing this function, as a distinct copy of
               RowObj
------------------------------------------------------------------*/

Row& Row::operator=(const Row& RowObj)
{
   if (this != &RowObj)
   {
      Length_ = RowObj.Length_;              // copy # values

      delete [] Array;                       // deallocate old storage

      if (Length_ > 0)                       // if R not empty
      {
         Array = new TableElement [Length_]; //   allocate new array
         assert(Array != 0);                 //   verify success
         for (int c = 0; c < Length_; c++)   //   and copy RowObj
           Array[c] = RowObj.Array[c];       //     into it
      }
      else                                   // otherwise
         Array = 0;                          //   set Array to NULL
   }

   return *this;                             // permit chaining
}
