#ifndef __LATTICE2D_H
#define __LATTICE2D_H

/////////////////////////////////////////////////////////////////////////
//
// Name:    Lattice2DTemplate.h
//
// Purpose: 2-dimensional lattice template of "cells"
//
/////////////////////////////////////////////////////////////////////////

#include <iostream.h>
#include "Status.h"
#include "Dimension.h"

template <class Cell>
class Lattice2DTemplate {

public:

    //
    // Default constructor
    //
    Lattice2DTemplate() : rows(0), cols(0), lattice(0) {}


    //
    // Constructor for dimension arguments
    //
    Lattice2DTemplate(
    const Dimension_t& r, const Dimension_t& c) : rows(r), cols(c)
        { AllocateLattice(); }


    //
    // Copy constructor
    //
    Lattice2DTemplate(
    const Lattice2DTemplate<Cell>& L) : rows(L.rows), cols(L.cols)
        {
            AllocateLattice();
            CopyLattice(L);
        }


    //
    // Overloaded assignment operator
    //
    Lattice2DTemplate<Cell>&
    operator=(
    const Lattice2DTemplate<Cell>& L)
        {
            if (this != &L) {
                if (rows != L.rows || cols != L.cols) {
                    delete this;
                } else {
                    rows = L.rows;
                    cols = L.cols;
                    AllocateLattice();
                }
                CopyLattice(L);
            }
            return *this;
        }


    //
    // Destructor
    //
    ~Lattice2DTemplate()
        {
            for (int i=0; i<rows; i++) {
                delete[] lattice[i];
            }
            delete[] lattice;
        }


    //
    // Overloaded () operator - for 2D indexing
    //
    Cell&
    operator()(
    const Dimension_t&  i,
    const Dimension_t&  j)
        {
#ifdef CHECK_BOUNDS
            if (i<0 || i>=rows || j<0 || j>=cols) {
                cerr
                << "Error: Lattice2DTemplate::operator() "
                << "index out of bounds" << endl;
                if (lattice) {
                    return lattice[0][0];
                } else {
                    exit(5);
                }
            }
#endif
            return lattice[i][j];
        }


    //
    // Overloaded () operator - for 2D indexing
    // Returns a const reference.
    //
    const Cell&
    operator()(
    const Dimension_t&  i,
    const Dimension_t&  j) const
        {
#ifdef CHECK_BOUNDS
            if (i<0 || i>=rows || j<0 || j>=cols) {
                cerr
                << "Error: Lattice2DTemplate::operator() "
                << "index out of bounds" << endl;
                if (lattice) {
                    return lattice[0][0];
                } else {
                    exit(5);
                }
            }
#endif
            return lattice[i][j];
        }


    //
    // Sets the 2D lattice dimensions and allocates
    // the lattice for that size
    //
    Status_t
    SetDimensions(
    const Dimension_t&  r,
    const Dimension_t&  c)
        {
            rows = r; cols = c;
            return AllocateLattice();
        }


    //
    // Access functions...
    //

    Dimension_t Rows()      const { return rows;    }
    Dimension_t Columns()   const { return cols;    }


protected:

    Dimension_t rows, cols; // rows, columns for 2D lattice

    Cell        **lattice;  // 2D (rows x cols) of Cell


private:


    //
    //  Dynamically allocates a 2D array of Cell (pointed to
    //  by the data member lattice), the dimensions of which
    //  are given by the data members, rows & cols.
    //
    //  Notes:
    //      Assumes that the data members rows and cols have
    //      already been initialized (though this is guaranteed
    //      by the constructors).
    //
    Status_t
    AllocateLattice()
        {
            if (!(lattice = new Cell*[rows])) {
                return FAILURE;
            }
            for (Dimension_t i=0; i<rows; i++) {
                if (!(lattice[i] = new Cell[cols])) {
                    return FAILURE;
                }
            }
            return SUCCESS;
        }


    //
    // Copies the contents of the lattice owned by another
    // Lattice2DTemplate object, L, into this lattice.
    // Assumes that this and L have the same dimensions
    // and are properly allocated. This is designed to be
    // called in conjunction with the AllocateLattice
    // member function.
    //
    void
    CopyLattice(
    const Lattice2DTemplate<Cell>& L)
        {
            for (Dimension_t i=0; i<rows; i++) {
                for (Dimension_t j=0; j<cols; j++) {
                    lattice[i][j] = L.lattice[i][j];
                }
            }
        }

};



#endif // __LATTICE2D_H
