//-*-c++-*- #ifndef ARRAY2_H #define ARRAY2_H /** * @file array2.h * @brief Basic dynamic 2-dimensional array of type T - COLUMNWISE ORDERING * @author James R. Diebel, Stanford University * * - History: * - 21 March 2006 - Started */ #include #include /** * @namespace sla * @brief Simple Linear Algebra - library of matrix and vector classes * plus some random utility functions and other containers */ namespace sla { ///////////////////////////////////////////////////////////////////////////// // DECLARATIONS ///////////////////////////////////////////////////////////////////////////// // Forward declarations template class Array; // K-dimensional array template class Array2; // 2-dimensional array //////////////////// Array2 //////////////////// /** * A 2-Dimensional Array */ template class Array2: public Array { public: // Constructors/Destructor Array2(); Array2(const int n1, const int n2); //Array2(const Array2& a) { set(a); } // Mutators void setSize(const int n1, const int n2); T& operator () (const int i1, const int i2); // Accessors T& operator () (const int i1, const int i2) const; // Helper functions int indexOf(const int i1, const int i2) const; bool isInBounds(const int i1, const int i2) const; bool onSameRow(const int k1, const int k2) const; // inherit member data and functions of parent using Array::x; using Array::isInBounds; using Array::reset; using Array::operator (); using Array::size; using Array::setSize; protected: // inherit member data and functions of parent using Array::dim; }; //////////////////////////////////////////////////////////////////////////// // DEFINITIONS //////////////////////////////////////////////////////////////////////////// //////////////////// Array2 //////////////////// // Constructors/Destructor /** Ctor that does no initalization. */ template inline Array2::Array2() : Array() {} /** Ctor that starts with given dimensions * @param nn_ is the size */ template inline Array2::Array2(const int n1, const int n2) : Array() { setSize(n1,n2); } // Mutators /** Set the size of the array * @param n1, n2 the sizes of the array in each dimension */ template inline void Array2::setSize(const int n1, const int n2) { Array::setSize(Vec2i(n1,n2)); if (AR_PO>2) std::cout << "Created Array2 of size (" << n1 << ", " << n2 << ")." << std::endl << std::flush; } /** Returns mutable reference to array element * @param i1,i2 are the indices * @return a reference to the array element */ template inline T& Array2::operator () (const int i1, const int i2) { return x[indexOf(i1,i2)]; } // Accessors /** Returns inmutable reference to array element * @param i1,i2 are the indices * @return the value of the array element */ template inline T& Array2::operator () (const int i1, const int i2) const { return x[indexOf(i1,i2)]; } // Helper functions (used internally only) /** Returns linear index of given array indices * @param are the indices to be converted * @return the linear index in the data array x */ template inline int Array2::indexOf(const int i1, const int i2) const { #if AR_CHECK_BOUNDS if (i1<0 || i1>=dim(0) || i2<0 || i2>=dim(1)) { std::cerr << "Array2: Error. Indices (" << i1 << ", " << i2 << ") exceed bounds [0, " << dim(0) << "] or [0, " << dim(1) << "]." << std::endl << std::flush; assert(0); exit(1); } #endif return i1+i2*dim(0); } /** Checks whether the given indices are within bounds * @params i1, i2 are the indices to be checked * @return a bool: true if indices are in bounds, false if out of bounds */ template inline bool Array2::isInBounds(const int i1, const int i2) const { return (i1>=0 && i1=0 && i2