#ifndef ARRAY_PROJECTION
#define ARRAY_PROJECTION

#include<stdio.h>


//----------------------------------------------
//         Base Class "Array"
//
//   a class for arrays of pointers,
//   pointing to objects of arithmetic type
//   (FORTRAN style, index starts with 1)
//----------------------------------------------


template<class T>                              // T should be an arithmetic type !
class Array                                    // The only reason why I make Array a template
   {                                           // is to be able to decide between FLOAT and DOUBLE
   protected:                                  // for runtime and storage reasons
   
      T **m;                                   // pointer to pointers (to data)    
      int size;                                // number of pointers
      int *refcnt;                             // number of objects referring to "this"

   public:

//  CONSTRUCTORS / DESTRUCTOR
      Array():m(0),refcnt(0) {}
      Array(int);
      Array(const Array &);
      virtual ~Array(){}
      virtual Array<T>* clone() const =0;      // "virtual constructor"
      virtual void declone() =0;               // destructor for virtually
                                               // constructed objects
      
//  OUTPUT
      virtual void print()=0;                  // screen output 

//  INFO
      int n_elements() const
         { return size; }

//  ELEMENT ACCESS
      virtual T& operator ()(int i) const = 0;

//  ASSIGNMENT    
      virtual Array<T>& operator =(Array &);
      virtual Array<T>& operator =(T);

//  SIGN
      virtual Array<T>& operator -();
      
//  SUMMATION
      friend Array<T>& operator +<>(Array<T> &, Array<T> &);
      friend Array<T>& operator -<>(Array<T> &, Array<T> &);
      virtual void operator +=(const Array<T> &);
      virtual void operator -=(const Array<T> &);
      virtual void operator +=(T);
      virtual void operator -=(T);

//  ELEMENT PRODUCT/DIVISION     
      friend Array<T>& operator *<>(Array<T> &,Array<T> &);
      friend Array<T>& operator /<>(Array<T> &,Array<T> &);
      
//  SCALAR PRODUCT
      friend Array<T>& operator *<>(Array<T> &,T);
      friend Array<T>& operator *<>(T,Array<T> &);
      virtual void operator *=(T);
      
//  CIRCULAR SHIFT
      Array<T>& operator <<(int) const;      
      Array<T>& operator >>(int) const;      

//  LINEAR SCALE      
      virtual void linear(T xa,T xb);           // creates a linear scale between xa and yb
                                                // T must be an arithmetic type !

//  STATISTICS
      virtual T maximum(int &imax);             // returns value and index (imax) of maximum
                                                // T must be an arithmetic type !  

      virtual T minimum(int &imin);            
   };


template<class T>
class Partition;


//--------------------------------------------------------
//         class "Matrix", 
//         derived from "Array",
//         friend of "Partition"
//
//   a class for matrices
//   (2D arrays of arithmetic types, 
//   stored as continuous part in memory)
//   FORTRAN style (column major, index starts with 1)
//--------------------------------------------------------


template<class T>
class Matrix : public Array<T>
   {
   protected:
   
      int nrow,ncol;                    // number of rows and columns

   public:
   
//  CONSTRUCTORS / DESTRUCTOR 
      Matrix(){}
      explicit Matrix(int nc);          // constructor for 1D-matrices
                                        // declared "explicit" to avoid
                                        // implicit use as converter T -> Matrix
      Matrix(int nc, int nr);
      Matrix(const Matrix<T> &);
         
      virtual ~Matrix();
      void decrement();

      virtual Matrix<T>* clone() const
         { return new Matrix<T>(ncol,nrow); }
   
      virtual void declone();
      

//  INFO
      int n_rows() const
         { return nrow; }
      
      int n_columns() const
         { return ncol; }
      
//  OUTPUT      
      virtual void print();            
      size_t read(FILE*);                         // reads binary file 
      size_t write(FILE*);                        // writes binary file
      
//  ELEMENT ACCESS      
      T& operator ()(int i, int j) const          // access to element i,j
         { return m[i][j]; }
         
      virtual T& operator ()(int i) const         // access to element i (from 1 to size)
         { return m[1][i]; }                      // (column is major !)
         
//  ASSIGNMENT      
      virtual Matrix<T>& operator =(Array<T> &);  // DEEP copy !
      virtual Matrix<T>& operator =(T);

//  PARTITIONS
      Partition<T> row(int) const;                                      // access to row 
      Partition<T> col(int) const;                                      // access to column
      Partition<T> operator()(int i1,int i2,int j1,int j2) const;       // access to submatrix
                                                                        // from column i1 to i2 (incl.)
                                                                        //         row j1 to j2 (incl.)

//  SHALLOW COPY
      void ref(const Matrix<T> &);                // if you really want it, here it is !
                                                  // make a SHALLOW copy and rely on 
                                                  // reference counting
                                                      
//  FAST SUMMATION      
      void add(const Matrix<T> &,const Matrix<T> &);    // somewhat faster, since no new objects
      void sub(const Matrix<T> &,const Matrix<T> &);    // are created and copied

   };   


//--------------------------------------------------------
//         class "Partition", derived from "Array"
//
//   a class for references to partitions of matrices
//   (rows, columns or submatrices)
//   behaviour is similar to Matrix (1D or 2D)
//   FORTRAN style (column major, index starts with 1)
//--------------------------------------------------------


template<class T>
class Partition : public Array<T>
   {
   friend class Matrix<T>;

   protected:
   
      int nrow,ncol;
   
   public:
   
//  CONSTRUCTORS / DESTRUCTOR
      Partition(){}
      explicit Partition(int);
      Partition(const Partition<T> &);
      Partition(const Matrix<T> &);                 // Converter Matrix -> Partition
                                                    // (not sure if needed)
                                                            
      virtual Matrix<T>* clone() const              // whenever a new object is created
         { return new Matrix<T>(ncol,nrow); }       // it will not be a "Partition" but
                                                    // a "Matrix", since "Partitions"
                                                    // cannot stand alone. They only refer
      virtual ~Partition();                         // to parts of matrices
      void decrement();
      virtual void declone();
      
//  OUTPUT      
      virtual void print();

//  INFO
      int n_rows() const
         { return nrow; }
      
      int n_columns() const
         { return ncol; }

//  ELEMENT ACCESS
      virtual T& operator ()(int i) const
         { return m[i][1]; }

//  ASSIGNMENT      
      virtual Partition<T>& operator =(Array<T> &);
      virtual Partition<T>& operator =(T);

//  SHALLOW COPY
      void ref(const Partition<T> &);    
   };

      
void error(int);                                    // print errors and exit (rude but effectful)

#include "array.cc"
#endif
