/*
 * I2D.h
 *
 * Interface for classes that contain 2D pointer-based matrices.
 * Contains static methods for creating and deleting 2D pointer-based matrices.
 *
 *  Created on: Feb 7, 2014
 *      Author: godin
 */

#ifndef I2D_H_
#define I2D_H_

#include <stddef.h>

template <typename T>
class I2D
{
public:

    virtual ~I2D() {};

    virtual T** getPtr2d() = 0;
    virtual T* getPtr1d() = 0;
    virtual int getM() const = 0;
    virtual int getN() const = 0;

    // Allocates a 2D pointer array of size m*n
    // If extra is true, and extra entry is allocated at array[m][0];
    static T** New2D( int m, int n, bool extra = false )
    {
        T** array = NULL;
        if( m * n > 0 )
        {
            array = new T*[m + ( extra ? 1 : 0 ) ];
            array[0] = new T[m * n + ( extra ? 1 : 0 )];
            for( int i = 1; i < m  + ( extra ? 1 : 0 ); ++i )
            {
                array[i] = array[i - 1] + n;
            }
        }
        return array;
    }

    static void Delete2D( T** array )
    {
        if( NULL != array )
        {
            delete[] array[0];
            delete[] array;
        }
    }

protected:
    // Protected constructors for abstract base class
    I2D() {}
    I2D( const I2D& ) {}
};

;

#endif /* I2D_H_ */
