4:4
  !  ˆÐ�þ\bY²‘öcáy·�üÿÖ„N6÷ÕÏ¿Ø ?÷    //
// SPDX-License-Identifier: BSD-3-Clause
// Copyright Contributors to the OpenEXR Project.
//

// clang-format off

#ifndef _PyImathFixedMatrix_h_
#define _PyImathFixedMatrix_h_

#include <boost/python.hpp>
#include <iostream>
#include "PyImathFixedArray.h"
#include "PyImathOperators.h"

namespace PyImath {

//
// Utility class for a runtime-specified fixed sized matrix type in python
//
template <class T>
class FixedMatrix
{
    T *     _ptr;
    int     _rows;
    int     _cols;
    int     _rowStride;
    int     _colStride;
    int *   _refcount; // refcount if allocated, null if externally allocated

  public:

    FixedMatrix(T *ptr, int rows, int cols, int rowStride = 1, int colStride = 1) 
        : _ptr(ptr), _rows(rows), _cols(cols),
          _rowStride(rowStride), _colStride(colStride), _refcount(0)
    {
        // nothing
    }

    FixedMatrix(int rows, int cols)
        : _ptr(new T[rows*cols]), _rows(rows), _cols(cols),
          _rowStride(1), _colStride(1), _refcount(new int(1))
    {
        // nothing
    }

    FixedMatrix(const FixedMatrix &other)
        : _ptr(other._ptr), _rows(other._rows), _cols(other._cols),
          _rowStride(other._rowStride), _colStride(other._colStride),
          _refcount(other._refcount)
    {
        if (_refcount) *_refcount += 1;
    }
        
    const FixedMatrix &
    operator = (const FixedMatrix &other)
    {
        if (&other == this) return *this;
        unref();
        _ptr = other._ptr;
        _rows = other._rows;
        _cols = other._cols;
        _rowStride = other._rowStride;
        _colStride = other._colStride;
        _refcount = other._refcount;

        if (_refcount) *_refcount += 1;
        return *this;
    }

    void
    unref()
    {
        if (_refcount) {
            *_refcount -= 1;
            if (*_refcount == 0) {
                delete [] _ptr;
                delete _refcount;
            }
        }
        _ptr = 0;
        _rows = 0;
        _cols = 0;
        _rowStride = 0;
        _colStride = 0;
        _refcount = 0;
    }

    ~FixedMatrix()
    {
        unref();
    }
    
    Py_ssize_t convert_index(int index) const
    {
        if (index < 0) index += _rows;
        if (index >= _rows || index < 0) {
            PyErr_SetString(PyExc_IndexError, "Index out of range");
            boost::python::throw_error_already_set();
        }
        return index;
    }

    void extract_slice_indices(PyObject *index, Py_ssize_t &start, Py_ssize_t &end, Py_ssize_t &step, Py_ssize_t &slicelength) const
    {
        slicelength = 0;
        if (PySlice_Check(index)) {
#if PY_MAJOR_VERSION > 2
            PyObject *slice = index;
#else
            PySliceObject *slice = reinterpret_cast<PySliceObject *>(index);
#endif
            if (PySlice_GetIndicesEx(slice,_rows,&start,&end,&step,&slicelength) == -1) {
		    boost::python::throw_error_already_set();
            }
        } else if (PyInt_Check(index)) {
            Py_ssize_t i = convert_index(PyInt_AS_LONG(index));
            start = i; end = i+1; step = 1; slicelength = 1;
        } else {
            PyErr_SetString(PyExc_TypeError, "Object is not a slice");
	    boost::python::throw_error_already_set();
        }
        //std::cout << "Slice indices are " << start << " " << end << " " << step << " " << slicelength << std::endl;
    }

    const FixedArray<T> * getitem(int index) const
    {
        return new FixedArray<T>(const_cast<T *>(&_ptr[convert_index(index)*_rowStride*_cols*_colStride]),_cols,_colStride);
    }

    FixedMatrix  getslice(PyObject *index) const
    {
        Py_ssize_t start, end, step, slicelength;
        extract_slice_indices(index,start,end,step,slicelength);
        FixedMatrix f(slicelength,_cols);
        for (int i=0; i<slicelength; ++i) {
            for (int j=0; j<_cols; ++j) {
                f.element(i,j) = element((start+i*step),j);
            }
        }
        return f;
    }

    void
    setitem_scalar(PyObject *index, const T &data)
    {
        Py_ssize_t star