Commit c4fe65877b53665cbc668b17db4d656d8f021373

Authored by Jürgen Knödlseder
1 parent fca0cb57

Make GNdarray::index() method public

include/GNdarray.hpp
1 /*************************************************************************** 1 /***************************************************************************
2 * GNdarray.hpp - N-dimensional array class * 2 * GNdarray.hpp - N-dimensional array class *
3 * ----------------------------------------------------------------------- * 3 * ----------------------------------------------------------------------- *
4 - * copyright (C) 2016-2018 by Juergen Knoedlseder * 4 + * copyright (C) 2016-2020 by Juergen Knoedlseder *
5 * ----------------------------------------------------------------------- * 5 * ----------------------------------------------------------------------- *
6 * * 6 * *
7 * This program is free software: you can redistribute it and/or modify * 7 * This program is free software: you can redistribute it and/or modify *
@@ -110,6 +110,7 @@ public: @@ -110,6 +110,7 @@ public:
110 const std::vector<int>& shape(void) const; 110 const std::vector<int>& shape(void) const;
111 const std::vector<int>& strides(void) const; 111 const std::vector<int>& strides(void) const;
112 void shape(const std::vector<int>& shape); 112 void shape(const std::vector<int>& shape);
  113 + int index(const std::vector<int>& i) const;
113 double& at(const int& ix); 114 double& at(const int& ix);
114 double& at(const int& ix, const int& iy); 115 double& at(const int& ix, const int& iy);
115 double& at(const int& ix, const int& iy, const int& iz); 116 double& at(const int& ix, const int& iy, const int& iz);
@@ -127,7 +128,6 @@ protected: @@ -127,7 +128,6 @@ protected:
127 void init_members(void); 128 void init_members(void);
128 void copy_members(const GNdarray& array); 129 void copy_members(const GNdarray& array);
129 void free_members(void); 130 void free_members(void);
130 - int index(const std::vector<int>& i) const;  
131 bool has_same_shape(const GNdarray& array) const; 131 bool has_same_shape(const GNdarray& array) const;
132 void require_same_shape(const std::string& method, const GNdarray& array) const; 132 void require_same_shape(const std::string& method, const GNdarray& array) const;
133 133
pyext/GNdarray.i
1 /*************************************************************************** 1 /***************************************************************************
2 * GNdarray.i - N-dimensional array class * 2 * GNdarray.i - N-dimensional array class *
3 * ----------------------------------------------------------------------- * 3 * ----------------------------------------------------------------------- *
4 - * copyright (C) 2016-2018 by Juergen Knoedlseder * 4 + * copyright (C) 2016-2020 by Juergen Knoedlseder *
5 * ----------------------------------------------------------------------- * 5 * ----------------------------------------------------------------------- *
6 * * 6 * *
7 * This program is free software: you can redistribute it and/or modify * 7 * This program is free software: you can redistribute it and/or modify *
@@ -70,6 +70,7 @@ public: @@ -70,6 +70,7 @@ public:
70 const std::vector<int>& shape(void) const; 70 const std::vector<int>& shape(void) const;
71 const std::vector<int>& strides(void) const; 71 const std::vector<int>& strides(void) const;
72 void shape(const std::vector<int>& shape); 72 void shape(const std::vector<int>& shape);
  73 + int index(const std::vector<int>& i) const;
73 double* data(void); 74 double* data(void);
74 }; 75 };
75 76
src/numerics/GNdarray.cpp
1 /*************************************************************************** 1 /***************************************************************************
2 * GNdarray.cpp - N-dimensional array class * 2 * GNdarray.cpp - N-dimensional array class *
3 * ----------------------------------------------------------------------- * 3 * ----------------------------------------------------------------------- *
4 - * copyright (C) 2016-2018 by Juergen Knoedlseder * 4 + * copyright (C) 2016-2020 by Juergen Knoedlseder *
5 * ----------------------------------------------------------------------- * 5 * ----------------------------------------------------------------------- *
6 * * 6 * *
7 * This program is free software: you can redistribute it and/or modify * 7 * This program is free software: you can redistribute it and/or modify *
@@ -741,6 +741,32 @@ const double&amp; GNdarray::at(const std::vector&lt;int&gt;&amp; i) const @@ -741,6 +741,32 @@ const double&amp; GNdarray::at(const std::vector&lt;int&gt;&amp; i) const
741 741
742 742
743 /***********************************************************************//** 743 /***********************************************************************//**
  744 + * @brief Compute array element index
  745 + *
  746 + * @param[in] i Index vector.
  747 + * @return Array element index.
  748 + *
  749 + * Computes the array element index from an index vector. The method assumes
  750 + * that the index vector has the same dimension as the array. If this
  751 + * condition is not guaranteed, the condition needs to be checked before
  752 + * calling the method.
  753 + ***************************************************************************/
  754 +int GNdarray::index(const std::vector<int>& i) const
  755 +{
  756 + // Initialise element index
  757 + int index = 0;
  758 +
  759 + // Compute element index
  760 + for (int k = 0; k < m_shape.size(); ++k) {
  761 + index += m_strides[k] * i[k];
  762 + }
  763 +
  764 + // Return index
  765 + return index;
  766 +}
  767 +
  768 +
  769 +/***********************************************************************//**
744 * @brief Print array information 770 * @brief Print array information
745 * 771 *
746 * @param[in] chatter Chattiness. 772 * @param[in] chatter Chattiness.
@@ -824,32 +850,6 @@ void GNdarray::free_members(void) @@ -824,32 +850,6 @@ void GNdarray::free_members(void)
824 850
825 851
826 /***********************************************************************//** 852 /***********************************************************************//**
827 - * @brief Compute array element index  
828 - *  
829 - * @param[in] i Index vector.  
830 - * @return Array element index.  
831 - *  
832 - * Computes the array element index from an index vector. The method assumes  
833 - * that the index vector has the same dimension as the array. If this  
834 - * condition is not guaranteed, the condition needs to be checked before  
835 - * calling the method.  
836 - ***************************************************************************/  
837 -int GNdarray::index(const std::vector<int>& i) const  
838 -{  
839 - // Initialise element index  
840 - int index = 0;  
841 -  
842 - // Compute element index  
843 - for (int k = 0; k < m_shape.size(); ++k) {  
844 - index += m_strides[k] * i[k];  
845 - }  
846 -  
847 - // Return index  
848 - return index;  
849 -}  
850 -  
851 -  
852 -/***********************************************************************//**  
853 * @brief Check if array has the same shape 853 * @brief Check if array has the same shape
854 * 854 *
855 * @param[in] array Array. 855 * @param[in] array Array.