// -*- coding: utf-8 -*- // Time-stamp: // Abstract class for 2d arrays // Two derived classes provide implementation. #ifndef ARRAY2D_H #define ARRAY2D_H #include "iterator.hpp" class Iterator; // Abstract class class Array2d { public: // Constructors // Defaut constructor : no allocation Array2d() : nlin(0), ncol(0) {}; // Constructor from dimensions Array2d(int m, int n) : nlin(m), ncol(n) {} // Copy constructor Array2d(const Array2d& a) : nlin(a.nlin), ncol(a.ncol) {} // Assignment operator Array2d& operator=(const Array2d& a); // Destructor virtual ~Array2d() {} // Returns numbers of lines and columns int ldim() const { return nlin; } int cdim() const { return ncol; } // Iterators Iterator begin(); Iterator end(); // Return an object of a non-abstract derived class static Array2d* make(int m, int n, int hint = 0); // Object Line contains everything to access an element of a line. // It contains a pointer to the beginning of the line and its length. class Line { public: // Constructor Line(double* q, int n) : p(q), ncol(n) {} // Access operators double& operator[](int j); const double& operator[](int j) const; private: double* p; // Pointer to the beginning of the line int ncol; // Number of columns }; // Access operator // Each of this operator return an object of the internal class Line // This object of class line has also an operator[](int) which returns // a reference to the element of the given line. // Therefore, if a is an object of the class Array2d // a[i][j] returns a reference of the value indexed by the pair (i,j) // The evaluation of a[i] return an object l of class Line which // contains a pointer to the beginning of the line and its length. // Then the expression l[j] returns the reference to the element. // Accessor for non-const objects virtual Line operator[](int i) = 0; // Accessor for const objects virtual const Line operator[](int i) const = 0; protected: // Allocation of the array virtual void alloc() = 0; // Free the array virtual void free() = 0; int nlin; // Number of lines int ncol; // Number of columns }; #endif