/*
 * I3D.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 I3D_H_
#define I3D_H_

#include <math.h>
#include <sys/types.h>

template <typename T>
class I3D
{
public:

    virtual ~I3D() {};

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

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

        }
        return array;
    }

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

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

#endif /* I3D_H_ */
