/* -*-c++-*- */
/* osgEarth - Geospatial SDK for OpenSceneGraph
 * Copyright 2020 Pelican Mapping
 * http://osgearth.org
 *
 * osgEarth is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
 */

#ifndef OSGEARTH_ARRAY_H
#define OSGEARTH_ARRAY_H 1

#include <algorithm>

namespace osgEarth
{
    namespace Array
    {
        // A matrix-like accessor into an area of memory. Given stride
        // and offset arguments, you can treat a region of memory as a
        // rectangular matrix and access subregions of it.
        // An array of osg::Vec3 as a matrix:
        // osg::Vec3Array vecs(16);
        // View<osg::Vec3> array(&vecs[0], 4, 4);
        // for (int j = 0; j < array.rowDim; j++)
        //    for (int i = 0; j < array.colDim; i ++)
        //        array(j, i) = ...;

        // This is quite general; a View with an appropriate offset can
        // support access to a smaller area of backing storage than
        // would be used by the full range of indices to the View, as
        // long as those indices are constrained.
        template<class ElementType>
        struct View
        {
            ElementType* const data;
            const int offset;
            const int stride;
            const unsigned rowDim;       // Number of rows
            const unsigned colDim;       // Number of columns
            View(ElementType* data_, unsigned offset_, unsigned stride_,
                 unsigned rowDim_, unsigned colDim_)
                : data(data_), offset(offset_), stride(stride_), rowDim(rowDim_), colDim(colDim_)
            {
            }

            View(ElementType* data_, unsigned rowDim_, unsigned colDim_)
                : data(data_), offset(0), stride(colDim_), rowDim(rowDim_), colDim(colDim_)
            {
            }
            // Create a view from another view
            View(const View& rhs, int xo, int yo, unsigned rowDim_, unsigned colDim_)
                : data(rhs.data), offset(rhs.offset + rhs.stride * xo + yo), stride(rhs.stride),
                  rowDim(rowDim_), colDim(colDim_)
            {
            }

            ElementType operator()(unsigned row, unsigned col) const
            {
                return *(data + (offset + static_cast<int>(stride * row + col)));
            }
            ElementType& operator()(unsigned row, unsigned col)
            {
                return *(data + (offset +  static_cast<int>(stride * row + col)));
            }
        };

        // The Matrix is a View that allocates its own storage.
        template <class ElementType>
        struct Matrix : public View<ElementType>
        {
            template <class ViewType>
            void copyFromView(const ViewType& view)
            {
                for (int j = 0; j < this->rowDim; ++j)
                {
                    for (int i = 0; i < this->colDim; ++i)
                    {
                        (*this)(j, i) = view(j, i);
                    }
                }
            }

            Matrix(unsigned rowDim_, unsigned colDim_, const ElementType& init = ElementType())
                : View<ElementType>(new ElementType[rowDim_ * colDim_], rowDim_, colDim_)
            {
                std::fill(this->data, this->data + rowDim_ * colDim_, init);
            }

            template <class ViewType>
            Matrix(unsigned rowDim_, unsigned colDim_, const ViewType& view)
                : View<ElementType>(new ElementType[rowDim_ * colDim_], rowDim_, colDim_)
            {
                copyFromView(view);
            }

            ~Matrix()
            {
                delete [] this->data;
            }
        };
    }
}

#endif // OSGEARTH_ARRAY_H
