/*
 * Copyright (C) 1999-2017 Hartmut Seichter
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#ifndef PW_CORE_MATRIXBASE_HPP
#define PW_CORE_MATRIXBASE_HPP

namespace pw {

/**
 * \brief base class for all matrix and vector operations
 */
template <typename T> class matrixbase {
public:

    typedef T value_type;
    typedef int offset_type;
    typedef unsigned int size_type;


	//! assignment constructor
    explicit matrixbase(int rows, int cols,T* ptr,bool row_major,int data_offset = 0)
        : _data(ptr)
        , _data_offset(data_offset)
        , _rows(rows)
        , _cols(cols)
        , _row_stride(row_major ? 1    : cols)
        , _col_stride(row_major ? rows : 1)
	{
	}

    inline const T& get_element(int e) const { return this->at(e); }
    inline void set_element(int e,const T &v) { this->at(e) = v; }

    //! return number of rows
    inline unsigned int rows() const { return _rows; }
    //! return number of columns
    inline unsigned int cols() const { return _cols; }

	//! get cell count
    inline unsigned int cells() const { return _rows * _cols; }

    //! get data
    inline T* data() { return _data + _data_offset; }

    //! get data
    inline const T* data() const { return _data + _data_offset; }

    //! get item at index
    inline T& at(unsigned int idx) { return data()[idx]; }

    //! get item at index
    inline const T& at(unsigned int idx) const { return data()[idx]; }

    //! get item at position
    inline T& at(unsigned int r,unsigned int c) { return data()[r * _row_stride + c * _col_stride]; }

    //! get item at position
    inline const T& at(unsigned int r,unsigned int c) const { return data()[r * _row_stride + c * _col_stride]; }

    //! fill data
    inline matrixbase& fill(const T& val) {
        for (unsigned int r = 0; r < this->rows(); r++)
            for (unsigned int c = 0; c < this->cols(); c++)
                this->at(r,c) = val;
        return *this;
    }

    //! set identity
    inline matrixbase& set_identity() {
        for (unsigned int r = 0;r < rows(); r++)
            for (unsigned int c = 0; c < cols(); c++)
                this->at(r,c) = (c == r) ? T(1) : T(0);
		return *this;
	}

    //! set strides for row and column
    inline void set_stride(int row_stride,int col_stride) {
        _row_stride = row_stride; _col_stride = col_stride;
    }

    //! return row stride (elements between consequtive rows)
    inline int row_stride() const { return _row_stride; }

    //! return column stride (elements between consequtive columns)
    inline int col_stride() const { return _col_stride; }

    //! get row major
    inline bool is_row_major() const { return row_stride() > col_stride(); }

    //! check if squared
    inline bool is_square() const { return (_rows == _cols); }

    //! flip row/column order
    inline void flip_order() { set_stride(col_stride(),row_stride()); }

    //! get the offset for data in the matrix
    inline int get_data_offset(int r,int c) const { return (r * _row_stride + c * _col_stride) + _data_offset; }

protected:

    T* _data;
    int _data_offset;

    int _cols;
    int _rows;

    int _row_stride;
    int _col_stride;

};

}


#endif
